jdk/src/jdk.crypto.pkcs11/share/classes/sun/security/pkcs11/Secmod.java
author alanbur
Wed, 21 Sep 2016 14:22:11 +0200
changeset 41107 567f832618d7
parent 31270 e6470b24700d
child 41205 2867bb7f5b53
permissions -rw-r--r--
8165161: Solaris: /usr/ccs /opt/sfw and /opt/csw are dead, references should be expunged Reviewed-by: rriggs, erikj
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) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            File configBase = new File(configDir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            if (configBase.isDirectory() == false ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                throw new IOException("configDir must be a directory: " + configDir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            File secmodFile = new File(configBase, "secmod.db");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            if (secmodFile.isFile() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                throw new FileNotFoundException(secmodFile.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        if (DEBUG) System.out.println("lib: " + platformPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        nssHandle = nssLoadLibrary(platformPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        if (DEBUG) System.out.println("handle: " + nssHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        fetchVersions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        if (supported == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            throw new IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                ("The specified version of NSS is incompatible, "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                + "3.7 or later required");
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("dir: " + configDir);
18772
ab43f18c524e 7165807: Non optimized initialization of NSS crypto library leads to scalability issues
vinnie
parents: 16734
diff changeset
   220
        boolean initok = nssInitialize(dbMode.functionName, nssHandle,
ab43f18c524e 7165807: Non optimized initialization of NSS crypto library leads to scalability issues
vinnie
parents: 16734
diff changeset
   221
            configDir, nssOptimizeSpace);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        if (DEBUG) System.out.println("init: " + initok);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        if (initok == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            throw new IOException("NSS initialization failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        this.configDir = configDir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        this.nssLibDir = nssLibDir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * Return an immutable list of all available modules.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @throws IllegalStateException if this Secmod is misconfigured
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *   or not initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    public synchronized List<Module> getModules() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            if (isInitialized() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                throw new IllegalStateException("NSS not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            // IOException if misconfigured
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            throw new IllegalStateException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        if (modules == null) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9849
diff changeset
   247
            @SuppressWarnings("unchecked")
9849
eb437e9fba66 7003952: SEC: securely load DLLs and launch executables using fully qualified path
valeriep
parents: 5506
diff changeset
   248
            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
   249
                nssLibDir);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            this.modules = Collections.unmodifiableList(modules);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        return modules;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    private static byte[] getDigest(X509Certificate cert, String algorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            MessageDigest md = MessageDigest.getInstance(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            return md.digest(cert.getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            throw new ProviderException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    boolean isTrusted(X509Certificate cert, TrustType trustType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        Bytes bytes = new Bytes(getDigest(cert, "SHA-1"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        TrustAttributes attr = getModuleTrust(ModuleType.KEYSTORE, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        if (attr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            attr = getModuleTrust(ModuleType.FIPS, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            if (attr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                attr = getModuleTrust(ModuleType.TRUSTANCHOR, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        return (attr == null) ? false : attr.isTrusted(trustType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    private TrustAttributes getModuleTrust(ModuleType type, Bytes bytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        Module module = getModule(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        TrustAttributes t = (module == null) ? null : module.getTrust(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        return t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * Constants describing the different types of NSS modules.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * For this API, NSS modules are classified as either one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * of the internal modules delivered as part of NSS or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * as an external module provided by a 3rd party.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    public static enum ModuleType {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
         * The NSS Softtoken crypto module. This is the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
         * slot of the softtoken object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
         * This module provides
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
         * implementations for cryptographic algorithms but no KeyStore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        CRYPTO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
         * The NSS Softtoken KeyStore module. This is the second
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
         * slot of the softtoken object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
         * This module provides
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
         * implementations for cryptographic algorithms (after login)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
         * and the KeyStore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        KEYSTORE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
         * The NSS Softtoken module in FIPS mode. Note that in FIPS mode the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
         * softtoken presents only one slot, not separate CRYPTO and KEYSTORE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
         * slots as in non-FIPS mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        FIPS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
         * The NSS builtin trust anchor module. This is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
         * NSSCKBI object. It provides no crypto functions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        TRUSTANCHOR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
         * An external module.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        EXTERNAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * Returns the first module of the specified type. If no such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * module exists, this method returns null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * @throws IllegalStateException if this Secmod is misconfigured
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *   or not initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    public Module getModule(ModuleType type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        for (Module module : getModules()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            if (module.getType() == type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                return module;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    static final String TEMPLATE_EXTERNAL =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        "library = %s\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        + "name = \"%s\"\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        + "slotListIndex = %d\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    static final String TEMPLATE_TRUSTANCHOR =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        "library = %s\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        + "name = \"NSS Trust Anchors\"\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        + "slotListIndex = 0\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        + "enabledMechanisms = { KeyStore }\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        + "nssUseSecmodTrust = true\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    static final String TEMPLATE_CRYPTO =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        "library = %s\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        + "name = \"NSS SoftToken Crypto\"\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        + "slotListIndex = 0\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        + "disabledMechanisms = { KeyStore }\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    static final String TEMPLATE_KEYSTORE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        "library = %s\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        + "name = \"NSS SoftToken KeyStore\"\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        + "slotListIndex = 1\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        + "nssUseSecmodTrust = true\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    static final String TEMPLATE_FIPS =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        "library = %s\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        + "name = \"NSS FIPS SoftToken\"\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        + "slotListIndex = 0\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        + "nssUseSecmodTrust = true\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * A representation of one PKCS#11 slot in a PKCS#11 module.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    public static final class Module {
9849
eb437e9fba66 7003952: SEC: securely load DLLs and launch executables using fully qualified path
valeriep
parents: 5506
diff changeset
   371
        // path of the native library
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        final String libraryName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        // descriptive name used by NSS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        final String commonName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        final int slot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        final ModuleType type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        private String config;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        private SunPKCS11 provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        // trust attributes. Used for the KEYSTORE and TRUSTANCHOR modules only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        private Map<Bytes,TrustAttributes> trust;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
9849
eb437e9fba66 7003952: SEC: securely load DLLs and launch executables using fully qualified path
valeriep
parents: 5506
diff changeset
   384
        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
   385
                boolean fips, int slot) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            ModuleType type;
9849
eb437e9fba66 7003952: SEC: securely load DLLs and launch executables using fully qualified path
valeriep
parents: 5506
diff changeset
   387
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            if ((libraryName == null) || (libraryName.length() == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                // must be softtoken
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                libraryName = System.mapLibraryName(SOFTTOKEN_LIB_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                if (fips == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                    type = (slot == 0) ? ModuleType.CRYPTO : ModuleType.KEYSTORE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                    type = ModuleType.FIPS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                    if (slot != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                        throw new RuntimeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                            ("Slot index should be 0 for FIPS slot");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                if (libraryName.endsWith(System.mapLibraryName(TRUST_LIB_NAME))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                        || commonName.equals("Builtin Roots Module")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                    type = ModuleType.TRUSTANCHOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                    type = ModuleType.EXTERNAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                if (fips) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                    throw new RuntimeException("FIPS flag set for non-internal "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                        + "module: " + libraryName + ", " + commonName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            }
13565
6298d600104f 7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents: 12813
diff changeset
   412
            // On Ubuntu the libsoftokn3 library is located in a subdirectory
6298d600104f 7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents: 12813
diff changeset
   413
            // of the system libraries directory. (Since Ubuntu 11.04.)
6298d600104f 7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents: 12813
diff changeset
   414
            File libraryFile = new File(libraryDir, libraryName);
6298d600104f 7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents: 12813
diff changeset
   415
            if (!libraryFile.isFile()) {
6298d600104f 7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents: 12813
diff changeset
   416
               File failover = new File(libraryDir, "nss/" + libraryName);
6298d600104f 7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents: 12813
diff changeset
   417
               if (failover.isFile()) {
6298d600104f 7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents: 12813
diff changeset
   418
                   libraryFile = failover;
6298d600104f 7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents: 12813
diff changeset
   419
               }
6298d600104f 7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents: 12813
diff changeset
   420
            }
6298d600104f 7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents: 12813
diff changeset
   421
            this.libraryName = libraryFile.getPath();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            this.commonName = commonName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            this.slot = slot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            initConfiguration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        private void initConfiguration() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            switch (type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            case EXTERNAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                config = String.format(TEMPLATE_EXTERNAL, libraryName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                                            commonName + " " + slot, slot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            case CRYPTO:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                config = String.format(TEMPLATE_CRYPTO, libraryName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            case KEYSTORE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                config = String.format(TEMPLATE_KEYSTORE, libraryName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            case FIPS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                config = String.format(TEMPLATE_FIPS, libraryName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            case TRUSTANCHOR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                config = String.format(TEMPLATE_TRUSTANCHOR, libraryName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                throw new RuntimeException("Unknown module type: " + type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
         * Get the configuration for this module. This is a string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
         * in the SunPKCS11 configuration format. It can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
         * customized with additional options and then made
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
         * current using the setConfiguration() method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        public synchronized String getConfiguration() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            return config;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
         * Set the configuration for this module.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
         * @throws IllegalStateException if the associated provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
         *   instance has already been created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        public synchronized void setConfiguration(String config) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            if (provider != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                throw new IllegalStateException("Provider instance already created");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            this.config = config;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
         * Return the pathname of the native library that implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
         * this module. For example, /usr/lib/libpkcs11.so.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        public String getLibraryName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            return libraryName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
         * Returns the type of this module.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        public ModuleType getType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            return type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
         * Returns the provider instance that is associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
         * module. The first call to this method creates the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
         * instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        public synchronized Provider getProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            if (provider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                provider = newProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            return provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        synchronized boolean hasInitializedProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            return provider != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        void setProvider(SunPKCS11 p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            if (provider != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                throw new ProviderException("Secmod provider already initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            provider = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        private SunPKCS11 newProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            try {
31270
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 25859
diff changeset
   517
                return new SunPKCS11(new Config("--" + config));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                // XXX
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                throw new ProviderException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        synchronized void setTrust(Token token, X509Certificate cert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            Bytes bytes = new Bytes(getDigest(cert, "SHA-1"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            TrustAttributes attr = getTrust(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            if (attr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                attr = new TrustAttributes(token, cert, bytes, CKT_NETSCAPE_TRUSTED_DELEGATOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                trust.put(bytes, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                // does it already have the correct trust settings?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                if (attr.isTrusted(TrustType.ALL) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                    // XXX not yet implemented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                    throw new ProviderException("Cannot change existing trust attributes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        TrustAttributes getTrust(Bytes hash) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            if (trust == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                // If provider is not set, create a temporary provider to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                // retrieve the trust information. This can happen if we need
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                // to get the trust information for the trustanchor module
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                // because we need to look for user customized settings in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                // keystore module (which may not have a provider created yet).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                // Creating a temporary provider and then dropping it on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                // floor immediately is flawed, but it's the best we can do
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                // for now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                    SunPKCS11 p = provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                    if (p == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                        p = newProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                        trust = Secmod.getTrust(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                    } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                        throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            return trust.get(hash);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            commonName + " (" + type + ", " + libraryName + ", slot " + slot + ")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        }
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * Constants representing NSS trust categories.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    public static enum TrustType {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        /** Trusted for all purposes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        ALL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        /** Trusted for SSL client authentication */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        CLIENT_AUTH,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        /** Trusted for SSL server authentication */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        SERVER_AUTH,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        /** Trusted for code signing */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        CODE_SIGNING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        /** Trusted for email protection */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        EMAIL_PROTECTION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    public static enum DbMode {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        READ_WRITE("NSS_InitReadWrite"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        READ_ONLY ("NSS_Init"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        NO_DB     ("NSS_NoDB_Init");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        final String functionName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        DbMode(String functionName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            this.functionName = functionName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * A LoadStoreParameter for use with the NSS Softtoken or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * NSS TrustAnchor KeyStores.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * It allows the set of trusted certificates that are returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * the KeyStore to be specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    public static final class KeyStoreLoadParameter implements LoadStoreParameter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        final TrustType trustType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        final ProtectionParameter protection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        public KeyStoreLoadParameter(TrustType trustType, char[] password) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            this(trustType, new PasswordProtection(password));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        public KeyStoreLoadParameter(TrustType trustType, ProtectionParameter prot) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            if (trustType == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                throw new NullPointerException("trustType must not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            this.trustType = trustType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            this.protection = prot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        public ProtectionParameter getProtectionParameter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            return protection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        public TrustType getTrustType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            return trustType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    static class TrustAttributes {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        final long handle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        final long clientAuth, serverAuth, codeSigning, emailProtection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        final byte[] shaHash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        TrustAttributes(Token token, X509Certificate cert, Bytes bytes, long trustValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            Session session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                session = token.getOpSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                // XXX use KeyStore TrustType settings to determine which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                // attributes to set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                CK_ATTRIBUTE[] attrs = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                    new CK_ATTRIBUTE(CKA_TOKEN, true),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                    new CK_ATTRIBUTE(CKA_CLASS, CKO_NETSCAPE_TRUST),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                    new CK_ATTRIBUTE(CKA_NETSCAPE_TRUST_SERVER_AUTH, trustValue),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                    new CK_ATTRIBUTE(CKA_NETSCAPE_TRUST_CODE_SIGNING, trustValue),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                    new CK_ATTRIBUTE(CKA_NETSCAPE_TRUST_EMAIL_PROTECTION, trustValue),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                    new CK_ATTRIBUTE(CKA_NETSCAPE_TRUST_CLIENT_AUTH, trustValue),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                    new CK_ATTRIBUTE(CKA_NETSCAPE_CERT_SHA1_HASH, bytes.b),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                    new CK_ATTRIBUTE(CKA_NETSCAPE_CERT_MD5_HASH, getDigest(cert, "MD5")),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                    new CK_ATTRIBUTE(CKA_ISSUER, cert.getIssuerX500Principal().getEncoded()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                    new CK_ATTRIBUTE(CKA_SERIAL_NUMBER, cert.getSerialNumber().toByteArray()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                    // XXX per PKCS#11 spec, the serial number should be in ASN.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                handle = token.p11.C_CreateObject(session.id(), attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                shaHash = bytes.b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                clientAuth = trustValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                serverAuth = trustValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                codeSigning = trustValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                emailProtection = trustValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                throw new ProviderException("Could not create trust object", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        TrustAttributes(Token token, Session session, long handle)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                        throws PKCS11Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            this.handle = handle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            CK_ATTRIBUTE[] attrs = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                new CK_ATTRIBUTE(CKA_NETSCAPE_TRUST_SERVER_AUTH),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                new CK_ATTRIBUTE(CKA_NETSCAPE_TRUST_CODE_SIGNING),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                new CK_ATTRIBUTE(CKA_NETSCAPE_TRUST_EMAIL_PROTECTION),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                new CK_ATTRIBUTE(CKA_NETSCAPE_CERT_SHA1_HASH),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            token.p11.C_GetAttributeValue(session.id(), handle, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            serverAuth = attrs[0].getLong();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            codeSigning = attrs[1].getLong();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            emailProtection = attrs[2].getLong();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            shaHash = attrs[3].getByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            attrs = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                new CK_ATTRIBUTE(CKA_NETSCAPE_TRUST_CLIENT_AUTH),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            long c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                token.p11.C_GetAttributeValue(session.id(), handle, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                c = attrs[0].getLong();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                // trust anchor module does not support this attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                c = serverAuth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            clientAuth = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        Bytes getHash() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            return new Bytes(shaHash);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        boolean isTrusted(TrustType type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            switch (type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            case CLIENT_AUTH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                return isTrusted(clientAuth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            case SERVER_AUTH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                return isTrusted(serverAuth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            case CODE_SIGNING:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                return isTrusted(codeSigning);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            case EMAIL_PROTECTION:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                return isTrusted(emailProtection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            case ALL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                return isTrusted(TrustType.CLIENT_AUTH)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                    && isTrusted(TrustType.SERVER_AUTH)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                    && isTrusted(TrustType.CODE_SIGNING)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                    && isTrusted(TrustType.EMAIL_PROTECTION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        private boolean isTrusted(long l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            // XXX CKT_TRUSTED?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            return (l == CKT_NETSCAPE_TRUSTED_DELEGATOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
    private static class Bytes {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        final byte[] b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        Bytes(byte[] b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            this.b = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            return Arrays.hashCode(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            if (this == o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            if (o instanceof Bytes == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            Bytes other = (Bytes)o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            return Arrays.equals(this.b, other.b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
    private static Map<Bytes,TrustAttributes> getTrust(SunPKCS11 provider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            throws PKCS11Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        Map<Bytes,TrustAttributes> trustMap = new HashMap<Bytes,TrustAttributes>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        Token token = provider.getToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        Session session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            session = token.getOpSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            int MAX_NUM = 8192;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
            CK_ATTRIBUTE[] attrs = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                new CK_ATTRIBUTE(CKA_CLASS, CKO_NETSCAPE_TRUST),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            token.p11.C_FindObjectsInit(session.id(), attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            long[] handles = token.p11.C_FindObjects(session.id(), MAX_NUM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            token.p11.C_FindObjectsFinal(session.id());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            if (DEBUG) System.out.println("handles: " + handles.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            for (long handle : handles) {
20487
38bed27b2815 8009438: sun/security/pkcs11/Secmod tests failing on Ubuntu 12.04
ascarpino
parents: 18772
diff changeset
   758
                try {
38bed27b2815 8009438: sun/security/pkcs11/Secmod tests failing on Ubuntu 12.04
ascarpino
parents: 18772
diff changeset
   759
                    TrustAttributes trust = new TrustAttributes(token, session, handle);
38bed27b2815 8009438: sun/security/pkcs11/Secmod tests failing on Ubuntu 12.04
ascarpino
parents: 18772
diff changeset
   760
                    trustMap.put(trust.getHash(), trust);
38bed27b2815 8009438: sun/security/pkcs11/Secmod tests failing on Ubuntu 12.04
ascarpino
parents: 18772
diff changeset
   761
                } catch (PKCS11Exception e) {
38bed27b2815 8009438: sun/security/pkcs11/Secmod tests failing on Ubuntu 12.04
ascarpino
parents: 18772
diff changeset
   762
                    // skip put on pkcs11 error
38bed27b2815 8009438: sun/security/pkcs11/Secmod tests failing on Ubuntu 12.04
ascarpino
parents: 18772
diff changeset
   763
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        return trustMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
    private static native long nssGetLibraryHandle(String libraryName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
    private static native long nssLoadLibrary(String name) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
    private static native boolean nssVersionCheck(long handle, String minVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
18772
ab43f18c524e 7165807: Non optimized initialization of NSS crypto library leads to scalability issues
vinnie
parents: 16734
diff changeset
   777
    private static native boolean nssInitialize(String functionName, long handle, String configDir, boolean nssOptimizeSpace);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
9849
eb437e9fba66 7003952: SEC: securely load DLLs and launch executables using fully qualified path
valeriep
parents: 5506
diff changeset
   779
    private static native Object nssGetModuleList(long handle, String libDir);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
}