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