src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/Secmod.java
author mbalao
Tue, 26 Jun 2018 08:10:45 +0800
changeset 50773 4bf4c7918063
parent 48248 55b9b1e184c6
permissions -rw-r--r--
8195607: sun/security/pkcs11/Secmod/TestNssDbSqlite.java failed with "NSS initialization failed" on NSS 3.34.1 Reviewed-by: valeriep, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
31270
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 25859
diff changeset
     2
 * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.pkcs11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.KeyStore.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.cert.X509Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.pkcs11.wrapper.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import static sun.security.pkcs11.wrapper.PKCS11Constants.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
12813
c10ab96dcf41 7170969: Add @GenerateNativeHeader to classes whose fields need to be exported for JNI
erikj
parents: 10336
diff changeset
    38
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * The Secmod class defines the interface to the native NSS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * library and the configuration information it stores in its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * secmod.db file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <p>Example code:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *   Secmod secmod = Secmod.getInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *   if (secmod.isInitialized() == false) {
41107
567f832618d7 8165161: Solaris: /usr/ccs /opt/sfw and /opt/csw are dead, references should be expunged
alanbur
parents: 31270
diff changeset
    48
 *       secmod.initialize("/home/myself/.mozilla");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *   Provider p = secmod.getModule(ModuleType.KEYSTORE).getProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *   KeyStore ks = KeyStore.getInstance("PKCS11", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *   ks.load(null, password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @since   1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
public final class Secmod {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private final static boolean DEBUG = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private final static Secmod INSTANCE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        sun.security.pkcs11.wrapper.PKCS11.loadNative();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        INSTANCE = new Secmod();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private final static String NSS_LIB_NAME = "nss3";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private final static String SOFTTOKEN_LIB_NAME = "softokn3";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private final static String TRUST_LIB_NAME = "nssckbi";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    // handle to be passed to the native code, 0 means not initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private long nssHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    // whether this is a supported version of NSS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private boolean supported;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    // list of the modules
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private List<Module> modules;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private String configDir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private String nssLibDir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private Secmod() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Return the singleton Secmod instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public static Secmod getInstance() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        return INSTANCE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private boolean isLoaded() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (nssHandle == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            nssHandle = nssGetLibraryHandle(System.mapLibraryName(NSS_LIB_NAME));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            if (nssHandle != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                fetchVersions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        return (nssHandle != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private void fetchVersions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        supported = nssVersionCheck(nssHandle, "3.7");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * Test whether this Secmod has been initialized. Returns true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * if NSS has been initialized using either the initialize() method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * or by directly calling the native NSS APIs. The latter may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * the case if the current process contains components that use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * NSS directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @throws IOException if an incompatible version of NSS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *   has been loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public synchronized boolean isInitialized() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        // NSS does not allow us to check if it is initialized already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        // assume that if it is loaded it is also initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        if (isLoaded() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        if (supported == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            throw new IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                ("An incompatible version of NSS is already loaded, "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                + "3.7 or later required");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    String getConfigDir() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        return configDir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    String getLibDir() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        return nssLibDir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * Initialize this Secmod.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @param configDir the directory containing the NSS configuration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *   files such as secmod.db
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @param nssLibDir the directory containing the NSS libraries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *   (libnss3.so or nss3.dll) or null if the library is on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *   the system default shared library path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @throws IOException if NSS has already been initialized,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *   the specified directories are invalid, or initialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *   fails for any other reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public void initialize(String configDir, String nssLibDir)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            throws IOException {
18772
ab43f18c524e 7165807: Non optimized initialization of NSS crypto library leads to scalability issues
vinnie
parents: 16734
diff changeset
   161
        initialize(DbMode.READ_WRITE, configDir, nssLibDir, false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
18772
ab43f18c524e 7165807: Non optimized initialization of NSS crypto library leads to scalability issues
vinnie
parents: 16734
diff changeset
   164
    public void initialize(DbMode dbMode, String configDir, String nssLibDir)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            throws IOException {
18772
ab43f18c524e 7165807: Non optimized initialization of NSS crypto library leads to scalability issues
vinnie
parents: 16734
diff changeset
   166
        initialize(dbMode, configDir, nssLibDir, false);
ab43f18c524e 7165807: Non optimized initialization of NSS crypto library leads to scalability issues
vinnie
parents: 16734
diff changeset
   167
    }
ab43f18c524e 7165807: Non optimized initialization of NSS crypto library leads to scalability issues
vinnie
parents: 16734
diff changeset
   168
ab43f18c524e 7165807: Non optimized initialization of NSS crypto library leads to scalability issues
vinnie
parents: 16734
diff changeset
   169
    public synchronized void initialize(DbMode dbMode, String configDir,
ab43f18c524e 7165807: Non optimized initialization of NSS crypto library leads to scalability issues
vinnie
parents: 16734
diff changeset
   170
        String nssLibDir, boolean nssOptimizeSpace) throws IOException {
ab43f18c524e 7165807: Non optimized initialization of NSS crypto library leads to scalability issues
vinnie
parents: 16734
diff changeset
   171
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        if (isInitialized()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            throw new IOException("NSS is already initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        if (dbMode == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if ((dbMode != DbMode.NO_DB) && (configDir == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        String platformLibName = System.mapLibraryName("nss3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        String platformPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if (nssLibDir == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            platformPath = platformLibName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            File base = new File(nssLibDir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            if (base.isDirectory() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                throw new IOException("nssLibDir must be a directory:" + nssLibDir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            File platformFile = new File(base, platformLibName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            if (platformFile.isFile() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                throw new FileNotFoundException(platformFile.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            platformPath = platformFile.getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        if (configDir != null) {
48248
55b9b1e184c6 8165996: PKCS11 using NSS throws an error regarding secmod.db when NSS uses sqlite
weijun
parents: 47216
diff changeset
   199
            String configDirPath = null;
50773
4bf4c7918063 8195607: sun/security/pkcs11/Secmod/TestNssDbSqlite.java failed with "NSS initialization failed" on NSS 3.34.1
mbalao
parents: 48248
diff changeset
   200
            String sqlPrefix = "sql:";
48248
55b9b1e184c6 8165996: PKCS11 using NSS throws an error regarding secmod.db when NSS uses sqlite
weijun
parents: 47216
diff changeset
   201
            if (!configDir.startsWith(sqlPrefix)) {
55b9b1e184c6 8165996: PKCS11 using NSS throws an error regarding secmod.db when NSS uses sqlite
weijun
parents: 47216
diff changeset
   202
                configDirPath = configDir;
55b9b1e184c6 8165996: PKCS11 using NSS throws an error regarding secmod.db when NSS uses sqlite
weijun
parents: 47216
diff changeset
   203
            } else {
55b9b1e184c6 8165996: PKCS11 using NSS throws an error regarding secmod.db when NSS uses sqlite
weijun
parents: 47216
diff changeset
   204
                StringBuilder configDirPathSB = new StringBuilder(configDir);
55b9b1e184c6 8165996: PKCS11 using NSS throws an error regarding secmod.db when NSS uses sqlite
weijun
parents: 47216
diff changeset
   205
                configDirPath = configDirPathSB.substring(sqlPrefix.length());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            }
48248
55b9b1e184c6 8165996: PKCS11 using NSS throws an error regarding secmod.db when NSS uses sqlite
weijun
parents: 47216
diff changeset
   207
            File configBase = new File(configDirPath);
55b9b1e184c6 8165996: PKCS11 using NSS throws an error regarding secmod.db when NSS uses sqlite
weijun
parents: 47216
diff changeset
   208
            if (configBase.isDirectory() == false ) {
55b9b1e184c6 8165996: PKCS11 using NSS throws an error regarding secmod.db when NSS uses sqlite
weijun
parents: 47216
diff changeset
   209
                throw new IOException("configDir must be a directory: " + configDirPath);
55b9b1e184c6 8165996: PKCS11 using NSS throws an error regarding secmod.db when NSS uses sqlite
weijun
parents: 47216
diff changeset
   210
            }
55b9b1e184c6 8165996: PKCS11 using NSS throws an error regarding secmod.db when NSS uses sqlite
weijun
parents: 47216
diff changeset
   211
            if (!configDir.startsWith(sqlPrefix)) {
55b9b1e184c6 8165996: PKCS11 using NSS throws an error regarding secmod.db when NSS uses sqlite
weijun
parents: 47216
diff changeset
   212
                File secmodFile = new File(configBase, "secmod.db");
55b9b1e184c6 8165996: PKCS11 using NSS throws an error regarding secmod.db when NSS uses sqlite
weijun
parents: 47216
diff changeset
   213
                if (secmodFile.isFile() == false) {
55b9b1e184c6 8165996: PKCS11 using NSS throws an error regarding secmod.db when NSS uses sqlite
weijun
parents: 47216
diff changeset
   214
                    throw new FileNotFoundException(secmodFile.getPath());
55b9b1e184c6 8165996: PKCS11 using NSS throws an error regarding secmod.db when NSS uses sqlite
weijun
parents: 47216
diff changeset
   215
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        if (DEBUG) System.out.println("lib: " + platformPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        nssHandle = nssLoadLibrary(platformPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        if (DEBUG) System.out.println("handle: " + nssHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        fetchVersions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        if (supported == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            throw new IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                ("The specified version of NSS is incompatible, "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                + "3.7 or later required");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        if (DEBUG) System.out.println("dir: " + configDir);
18772
ab43f18c524e 7165807: Non optimized initialization of NSS crypto library leads to scalability issues
vinnie
parents: 16734
diff changeset
   230
        boolean initok = nssInitialize(dbMode.functionName, nssHandle,
ab43f18c524e 7165807: Non optimized initialization of NSS crypto library leads to scalability issues
vinnie
parents: 16734
diff changeset
   231
            configDir, nssOptimizeSpace);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        if (DEBUG) System.out.println("init: " + initok);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        if (initok == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            throw new IOException("NSS initialization failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        this.configDir = configDir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        this.nssLibDir = nssLibDir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * Return an immutable list of all available modules.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @throws IllegalStateException if this Secmod is misconfigured
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *   or not initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    public synchronized List<Module> getModules() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            if (isInitialized() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                throw new IllegalStateException("NSS not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            // IOException if misconfigured
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            throw new IllegalStateException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        if (modules == null) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9849
diff changeset
   257
            @SuppressWarnings("unchecked")
9849
eb437e9fba66 7003952: SEC: securely load DLLs and launch executables using fully qualified path
valeriep
parents: 5506
diff changeset
   258
            List<Module> modules = (List<Module>)nssGetModuleList(nssHandle,
eb437e9fba66 7003952: SEC: securely load DLLs and launch executables using fully qualified path
valeriep
parents: 5506
diff changeset
   259
                nssLibDir);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            this.modules = Collections.unmodifiableList(modules);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        return modules;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    private static byte[] getDigest(X509Certificate cert, String algorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            MessageDigest md = MessageDigest.getInstance(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            return md.digest(cert.getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            throw new ProviderException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    boolean isTrusted(X509Certificate cert, TrustType trustType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        Bytes bytes = new Bytes(getDigest(cert, "SHA-1"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        TrustAttributes attr = getModuleTrust(ModuleType.KEYSTORE, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        if (attr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            attr = getModuleTrust(ModuleType.FIPS, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            if (attr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                attr = getModuleTrust(ModuleType.TRUSTANCHOR, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        return (attr == null) ? false : attr.isTrusted(trustType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    private TrustAttributes getModuleTrust(ModuleType type, Bytes bytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        Module module = getModule(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        TrustAttributes t = (module == null) ? null : module.getTrust(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        return t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * Constants describing the different types of NSS modules.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * For this API, NSS modules are classified as either one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * of the internal modules delivered as part of NSS or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * as an external module provided by a 3rd party.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    public static enum ModuleType {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
         * The NSS Softtoken crypto module. This is the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
         * slot of the softtoken object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
         * This module provides
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
         * implementations for cryptographic algorithms but no KeyStore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        CRYPTO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
         * The NSS Softtoken KeyStore module. This is the second
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
         * slot of the softtoken object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
         * This module provides
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
         * implementations for cryptographic algorithms (after login)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
         * and the KeyStore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        KEYSTORE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
         * The NSS Softtoken module in FIPS mode. Note that in FIPS mode the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
         * softtoken presents only one slot, not separate CRYPTO and KEYSTORE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
         * slots as in non-FIPS mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        FIPS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
         * The NSS builtin trust anchor module. This is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
         * NSSCKBI object. It provides no crypto functions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        TRUSTANCHOR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
         * An external module.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        EXTERNAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * Returns the first module of the specified type. If no such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * module exists, this method returns null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @throws IllegalStateException if this Secmod is misconfigured
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *   or not initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    public Module getModule(ModuleType type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        for (Module module : getModules()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            if (module.getType() == type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                return module;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    static final String TEMPLATE_EXTERNAL =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        "library = %s\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        + "name = \"%s\"\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        + "slotListIndex = %d\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    static final String TEMPLATE_TRUSTANCHOR =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        "library = %s\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        + "name = \"NSS Trust Anchors\"\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        + "slotListIndex = 0\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        + "enabledMechanisms = { KeyStore }\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        + "nssUseSecmodTrust = true\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    static final String TEMPLATE_CRYPTO =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        "library = %s\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        + "name = \"NSS SoftToken Crypto\"\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        + "slotListIndex = 0\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        + "disabledMechanisms = { KeyStore }\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    static final String TEMPLATE_KEYSTORE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        "library = %s\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        + "name = \"NSS SoftToken KeyStore\"\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        + "slotListIndex = 1\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        + "nssUseSecmodTrust = true\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    static final String TEMPLATE_FIPS =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        "library = %s\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        + "name = \"NSS FIPS SoftToken\"\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        + "slotListIndex = 0\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        + "nssUseSecmodTrust = true\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * A representation of one PKCS#11 slot in a PKCS#11 module.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    public static final class Module {
9849
eb437e9fba66 7003952: SEC: securely load DLLs and launch executables using fully qualified path
valeriep
parents: 5506
diff changeset
   381
        // path of the native library
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        final String libraryName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        // descriptive name used by NSS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        final String commonName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        final int slot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        final ModuleType type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        private String config;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        private SunPKCS11 provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        // trust attributes. Used for the KEYSTORE and TRUSTANCHOR modules only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        private Map<Bytes,TrustAttributes> trust;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
9849
eb437e9fba66 7003952: SEC: securely load DLLs and launch executables using fully qualified path
valeriep
parents: 5506
diff changeset
   394
        Module(String libraryDir, String libraryName, String commonName,
eb437e9fba66 7003952: SEC: securely load DLLs and launch executables using fully qualified path
valeriep
parents: 5506
diff changeset
   395
                boolean fips, int slot) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            ModuleType type;
9849
eb437e9fba66 7003952: SEC: securely load DLLs and launch executables using fully qualified path
valeriep
parents: 5506
diff changeset
   397
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            if ((libraryName == null) || (libraryName.length() == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                // must be softtoken
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                libraryName = System.mapLibraryName(SOFTTOKEN_LIB_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                if (fips == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                    type = (slot == 0) ? ModuleType.CRYPTO : ModuleType.KEYSTORE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                    type = ModuleType.FIPS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                    if (slot != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                        throw new RuntimeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                            ("Slot index should be 0 for FIPS slot");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                if (libraryName.endsWith(System.mapLibraryName(TRUST_LIB_NAME))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                        || commonName.equals("Builtin Roots Module")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                    type = ModuleType.TRUSTANCHOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                    type = ModuleType.EXTERNAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                if (fips) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                    throw new RuntimeException("FIPS flag set for non-internal "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                        + "module: " + libraryName + ", " + commonName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            }
13565
6298d600104f 7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents: 12813
diff changeset
   422
            // On Ubuntu the libsoftokn3 library is located in a subdirectory
6298d600104f 7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents: 12813
diff changeset
   423
            // of the system libraries directory. (Since Ubuntu 11.04.)
6298d600104f 7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents: 12813
diff changeset
   424
            File libraryFile = new File(libraryDir, libraryName);
6298d600104f 7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents: 12813
diff changeset
   425
            if (!libraryFile.isFile()) {
6298d600104f 7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents: 12813
diff changeset
   426
               File failover = new File(libraryDir, "nss/" + libraryName);
6298d600104f 7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents: 12813
diff changeset
   427
               if (failover.isFile()) {
6298d600104f 7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents: 12813
diff changeset
   428
                   libraryFile = failover;
6298d600104f 7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents: 12813
diff changeset
   429
               }
6298d600104f 7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents: 12813
diff changeset
   430
            }
6298d600104f 7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents: 12813
diff changeset
   431
            this.libraryName = libraryFile.getPath();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            this.commonName = commonName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            this.slot = slot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            initConfiguration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        private void initConfiguration() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            switch (type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            case EXTERNAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                config = String.format(TEMPLATE_EXTERNAL, libraryName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                                            commonName + " " + slot, slot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            case CRYPTO:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                config = String.format(TEMPLATE_CRYPTO, libraryName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            case KEYSTORE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                config = String.format(TEMPLATE_KEYSTORE, libraryName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            case FIPS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                config = String.format(TEMPLATE_FIPS, libraryName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            case TRUSTANCHOR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                config = String.format(TEMPLATE_TRUSTANCHOR, libraryName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                throw new RuntimeException("Unknown module type: " + type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
         * Get the configuration for this module. This is a string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
         * in the SunPKCS11 configuration format. It can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
         * customized with additional options and then made
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
         * current using the setConfiguration() method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        public synchronized String getConfiguration() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            return config;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
         * Set the configuration for this module.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
         * @throws IllegalStateException if the associated provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
         *   instance has already been created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        public synchronized void setConfiguration(String config) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            if (provider != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                throw new IllegalStateException("Provider instance already created");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            this.config = config;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
         * Return the pathname of the native library that implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
         * this module. For example, /usr/lib/libpkcs11.so.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        public String getLibraryName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            return libraryName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
         * Returns the type of this module.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        public ModuleType getType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            return type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
         * Returns the provider instance that is associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
         * module. The first call to this method creates the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
         * instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        public synchronized Provider getProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            if (provider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                provider = newProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            return provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        synchronized boolean hasInitializedProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            return provider != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        void setProvider(SunPKCS11 p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            if (provider != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                throw new ProviderException("Secmod provider already initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            provider = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        private SunPKCS11 newProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            try {
31270
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 25859
diff changeset
   527
                return new SunPKCS11(new Config("--" + config));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                // XXX
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                throw new ProviderException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        synchronized void setTrust(Token token, X509Certificate cert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            Bytes bytes = new Bytes(getDigest(cert, "SHA-1"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            TrustAttributes attr = getTrust(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            if (attr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                attr = new TrustAttributes(token, cert, bytes, CKT_NETSCAPE_TRUSTED_DELEGATOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                trust.put(bytes, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                // does it already have the correct trust settings?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                if (attr.isTrusted(TrustType.ALL) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                    // XXX not yet implemented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                    throw new ProviderException("Cannot change existing trust attributes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        TrustAttributes getTrust(Bytes hash) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            if (trust == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                // If provider is not set, create a temporary provider to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                // retrieve the trust information. This can happen if we need
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                // to get the trust information for the trustanchor module
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                // because we need to look for user customized settings in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                // keystore module (which may not have a provider created yet).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                // Creating a temporary provider and then dropping it on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                // floor immediately is flawed, but it's the best we can do
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                // for now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                    SunPKCS11 p = provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                    if (p == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                        p = newProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                        trust = Secmod.getTrust(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                    } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                        throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            return trust.get(hash);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            commonName + " (" + type + ", " + libraryName + ", slot " + slot + ")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * Constants representing NSS trust categories.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    public static enum TrustType {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        /** Trusted for all purposes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        ALL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        /** Trusted for SSL client authentication */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        CLIENT_AUTH,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        /** Trusted for SSL server authentication */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        SERVER_AUTH,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        /** Trusted for code signing */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        CODE_SIGNING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        /** Trusted for email protection */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        EMAIL_PROTECTION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    public static enum DbMode {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        READ_WRITE("NSS_InitReadWrite"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        READ_ONLY ("NSS_Init"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        NO_DB     ("NSS_NoDB_Init");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        final String functionName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        DbMode(String functionName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            this.functionName = functionName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * A LoadStoreParameter for use with the NSS Softtoken or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * NSS TrustAnchor KeyStores.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * It allows the set of trusted certificates that are returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * the KeyStore to be specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    public static final class KeyStoreLoadParameter implements LoadStoreParameter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        final TrustType trustType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        final ProtectionParameter protection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        public KeyStoreLoadParameter(TrustType trustType, char[] password) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            this(trustType, new PasswordProtection(password));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        public KeyStoreLoadParameter(TrustType trustType, ProtectionParameter prot) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            if (trustType == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                throw new NullPointerException("trustType must not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            this.trustType = trustType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            this.protection = prot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        public ProtectionParameter getProtectionParameter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            return protection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        public TrustType getTrustType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            return trustType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    static class TrustAttributes {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        final long handle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        final long clientAuth, serverAuth, codeSigning, emailProtection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        final byte[] shaHash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        TrustAttributes(Token token, X509Certificate cert, Bytes bytes, long trustValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            Session session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                session = token.getOpSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                // XXX use KeyStore TrustType settings to determine which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                // attributes to set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                CK_ATTRIBUTE[] attrs = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                    new CK_ATTRIBUTE(CKA_TOKEN, true),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                    new CK_ATTRIBUTE(CKA_CLASS, CKO_NETSCAPE_TRUST),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                    new CK_ATTRIBUTE(CKA_NETSCAPE_TRUST_SERVER_AUTH, trustValue),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                    new CK_ATTRIBUTE(CKA_NETSCAPE_TRUST_CODE_SIGNING, trustValue),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                    new CK_ATTRIBUTE(CKA_NETSCAPE_TRUST_EMAIL_PROTECTION, trustValue),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                    new CK_ATTRIBUTE(CKA_NETSCAPE_TRUST_CLIENT_AUTH, trustValue),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                    new CK_ATTRIBUTE(CKA_NETSCAPE_CERT_SHA1_HASH, bytes.b),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                    new CK_ATTRIBUTE(CKA_NETSCAPE_CERT_MD5_HASH, getDigest(cert, "MD5")),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                    new CK_ATTRIBUTE(CKA_ISSUER, cert.getIssuerX500Principal().getEncoded()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                    new CK_ATTRIBUTE(CKA_SERIAL_NUMBER, cert.getSerialNumber().toByteArray()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                    // XXX per PKCS#11 spec, the serial number should be in ASN.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                handle = token.p11.C_CreateObject(session.id(), attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                shaHash = bytes.b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                clientAuth = trustValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                serverAuth = trustValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                codeSigning = trustValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                emailProtection = trustValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                throw new ProviderException("Could not create trust object", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        TrustAttributes(Token token, Session session, long handle)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                        throws PKCS11Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            this.handle = handle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            CK_ATTRIBUTE[] attrs = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                new CK_ATTRIBUTE(CKA_NETSCAPE_TRUST_SERVER_AUTH),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                new CK_ATTRIBUTE(CKA_NETSCAPE_TRUST_CODE_SIGNING),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                new CK_ATTRIBUTE(CKA_NETSCAPE_TRUST_EMAIL_PROTECTION),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                new CK_ATTRIBUTE(CKA_NETSCAPE_CERT_SHA1_HASH),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            token.p11.C_GetAttributeValue(session.id(), handle, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            serverAuth = attrs[0].getLong();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            codeSigning = attrs[1].getLong();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            emailProtection = attrs[2].getLong();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            shaHash = attrs[3].getByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            attrs = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                new CK_ATTRIBUTE(CKA_NETSCAPE_TRUST_CLIENT_AUTH),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            long c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                token.p11.C_GetAttributeValue(session.id(), handle, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                c = attrs[0].getLong();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                // trust anchor module does not support this attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                c = serverAuth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            clientAuth = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        Bytes getHash() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            return new Bytes(shaHash);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        boolean isTrusted(TrustType type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            switch (type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            case CLIENT_AUTH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                return isTrusted(clientAuth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            case SERVER_AUTH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                return isTrusted(serverAuth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            case CODE_SIGNING:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                return isTrusted(codeSigning);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            case EMAIL_PROTECTION:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                return isTrusted(emailProtection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            case ALL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                return isTrusted(TrustType.CLIENT_AUTH)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                    && isTrusted(TrustType.SERVER_AUTH)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                    && isTrusted(TrustType.CODE_SIGNING)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                    && isTrusted(TrustType.EMAIL_PROTECTION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        private boolean isTrusted(long l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            // XXX CKT_TRUSTED?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            return (l == CKT_NETSCAPE_TRUSTED_DELEGATOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
    private static class Bytes {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        final byte[] b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        Bytes(byte[] b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            this.b = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            return Arrays.hashCode(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            if (this == o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            if (o instanceof Bytes == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            Bytes other = (Bytes)o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            return Arrays.equals(this.b, other.b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    private static Map<Bytes,TrustAttributes> getTrust(SunPKCS11 provider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            throws PKCS11Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        Map<Bytes,TrustAttributes> trustMap = new HashMap<Bytes,TrustAttributes>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        Token token = provider.getToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        Session session = null;
41205
2867bb7f5b53 8149802: Signature.verify() doesn't reset the signature object on exception
valeriep
parents: 41107
diff changeset
   756
        boolean exceptionOccurred = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            session = token.getOpSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            int MAX_NUM = 8192;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            CK_ATTRIBUTE[] attrs = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                new CK_ATTRIBUTE(CKA_CLASS, CKO_NETSCAPE_TRUST),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            token.p11.C_FindObjectsInit(session.id(), attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            long[] handles = token.p11.C_FindObjects(session.id(), MAX_NUM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            token.p11.C_FindObjectsFinal(session.id());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            if (DEBUG) System.out.println("handles: " + handles.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            for (long handle : handles) {
20487
38bed27b2815 8009438: sun/security/pkcs11/Secmod tests failing on Ubuntu 12.04
ascarpino
parents: 18772
diff changeset
   769
                try {
38bed27b2815 8009438: sun/security/pkcs11/Secmod tests failing on Ubuntu 12.04
ascarpino
parents: 18772
diff changeset
   770
                    TrustAttributes trust = new TrustAttributes(token, session, handle);
38bed27b2815 8009438: sun/security/pkcs11/Secmod tests failing on Ubuntu 12.04
ascarpino
parents: 18772
diff changeset
   771
                    trustMap.put(trust.getHash(), trust);
38bed27b2815 8009438: sun/security/pkcs11/Secmod tests failing on Ubuntu 12.04
ascarpino
parents: 18772
diff changeset
   772
                } catch (PKCS11Exception e) {
38bed27b2815 8009438: sun/security/pkcs11/Secmod tests failing on Ubuntu 12.04
ascarpino
parents: 18772
diff changeset
   773
                    // skip put on pkcs11 error
38bed27b2815 8009438: sun/security/pkcs11/Secmod tests failing on Ubuntu 12.04
ascarpino
parents: 18772
diff changeset
   774
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            }
41205
2867bb7f5b53 8149802: Signature.verify() doesn't reset the signature object on exception
valeriep
parents: 41107
diff changeset
   776
            exceptionOccurred = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        } finally {
41205
2867bb7f5b53 8149802: Signature.verify() doesn't reset the signature object on exception
valeriep
parents: 41107
diff changeset
   778
            if (exceptionOccurred) {
2867bb7f5b53 8149802: Signature.verify() doesn't reset the signature object on exception
valeriep
parents: 41107
diff changeset
   779
                token.killSession(session);
2867bb7f5b53 8149802: Signature.verify() doesn't reset the signature object on exception
valeriep
parents: 41107
diff changeset
   780
            } else {
2867bb7f5b53 8149802: Signature.verify() doesn't reset the signature object on exception
valeriep
parents: 41107
diff changeset
   781
                token.releaseSession(session);
2867bb7f5b53 8149802: Signature.verify() doesn't reset the signature object on exception
valeriep
parents: 41107
diff changeset
   782
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        return trustMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
    private static native long nssGetLibraryHandle(String libraryName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
    private static native long nssLoadLibrary(String name) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
    private static native boolean nssVersionCheck(long handle, String minVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
18772
ab43f18c524e 7165807: Non optimized initialization of NSS crypto library leads to scalability issues
vinnie
parents: 16734
diff changeset
   793
    private static native boolean nssInitialize(String functionName, long handle, String configDir, boolean nssOptimizeSpace);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
9849
eb437e9fba66 7003952: SEC: securely load DLLs and launch executables using fully qualified path
valeriep
parents: 5506
diff changeset
   795
    private static native Object nssGetModuleList(long handle, String libDir);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
}