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