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