jdk/src/share/classes/sun/security/pkcs11/SunPKCS11.java
author weijun
Thu, 11 Nov 2010 15:51:12 +0800
changeset 7179 4afb81e50183
parent 7043 5e2d1edeb2c7
child 8542 62c7b10ce177
permissions -rw-r--r--
6987827: security/util/Resources.java needs improvement Reviewed-by: valeriep
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
     2
 * Copyright (c) 2003, 2010, 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: 3353
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: 3353
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: 3353
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3353
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3353
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.interfaces.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.crypto.interfaces.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.security.auth.Subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.security.auth.login.LoginException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.security.auth.login.FailedLoginException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.security.auth.callback.Callback;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.security.auth.callback.CallbackHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.security.auth.callback.ConfirmationCallback;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.security.auth.callback.PasswordCallback;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.security.auth.callback.TextOutputCallback;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import sun.security.util.Debug;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import sun.security.util.ResourcesMgr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import sun.security.pkcs11.Secmod.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import sun.security.pkcs11.wrapper.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import static sun.security.pkcs11.wrapper.PKCS11Constants.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * PKCS#11 provider main class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
public final class SunPKCS11 extends AuthProvider {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private static final long serialVersionUID = -1354835039035306505L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    static final Debug debug = Debug.getInstance("sunpkcs11");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private static int dummyConfigId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    // the PKCS11 object through which we make the native calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    final PKCS11 p11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    // name of the configuration file
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private final String configName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    // configuration information
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    final Config config;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    // id of the PKCS#11 slot we are using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    final long slotID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private CallbackHandler pHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private final Object LOCK_HANDLER = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    final boolean removable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    final Module nssModule;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    final boolean nssUseSecmodTrust;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private volatile Token token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private TokenPoller poller;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    Token getToken() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        return token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public SunPKCS11() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        super("SunPKCS11-Dummy", 1.7d, "SunPKCS11-Dummy");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        throw new ProviderException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            ("SunPKCS11 requires configuration file argument");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    public SunPKCS11(String configName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        this(checkNull(configName), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public SunPKCS11(InputStream configStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        this(getDummyConfigName(), checkNull(configStream));
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 static <T> T checkNull(T obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (obj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        return obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    private static synchronized String getDummyConfigName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        int id = ++dummyConfigId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        return "---DummyConfig-" + id + "---";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   123
     * @deprecated use new SunPKCS11(String) or new SunPKCS11(InputStream)
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   124
     *         instead
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    public SunPKCS11(String configName, InputStream configStream) {
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   128
        super("SunPKCS11-" +
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   129
            Config.getConfig(configName, configStream).getName(),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            1.7d, Config.getConfig(configName, configStream).getDescription());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        this.configName = configName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        this.config = Config.removeConfig(configName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            System.out.println("SunPKCS11 loading " + configName);
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 library = config.getLibrary();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        String functionList = config.getFunctionList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        long slotID = config.getSlotID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        int slotListIndex = config.getSlotListIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        boolean useSecmod = config.getNssUseSecmod();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        boolean nssUseSecmodTrust = config.getNssUseSecmodTrust();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        Module nssModule = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        // Initialization via Secmod. The way this works is as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        // SunPKCS11 is either in normal mode or in NSS Secmod mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        // Secmod is activated by specifying one or more of the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        // options in the config file:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        // nssUseSecmod, nssSecmodDirectory, nssLibrary, nssModule
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        // XXX add more explanation here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        // If we are in Secmod mode and configured to use either the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        // nssKeyStore or the nssTrustAnchors module, we automatically
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   158
        // switch to using the NSS trust attributes for trusted certs
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   159
        // (KeyStore).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if (useSecmod) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            // note: Config ensures library/slot/slotListIndex not specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            // in secmod mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            Secmod secmod = Secmod.getInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            DbMode nssDbMode = config.getNssDbMode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                String nssLibraryDirectory = config.getNssLibraryDirectory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                String nssSecmodDirectory = config.getNssSecmodDirectory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                if (secmod.isInitialized()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    if (nssSecmodDirectory != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                        String s = secmod.getConfigDir();
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   174
                        if ((s != null) &&
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   175
                                (s.equals(nssSecmodDirectory) == false)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                            throw new ProviderException("Secmod directory "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                                + nssSecmodDirectory
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   178
                                + " invalid, NSS already initialized with "
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   179
                                + s);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                    if (nssLibraryDirectory != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                        String s = secmod.getLibDir();
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   184
                        if ((s != null) &&
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   185
                                (s.equals(nssLibraryDirectory) == false)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                            throw new ProviderException("NSS library directory "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                                + nssLibraryDirectory
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   188
                                + " invalid, NSS already initialized with "
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   189
                                + s);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                    if (nssDbMode != DbMode.NO_DB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                        if (nssSecmodDirectory == null) {
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   195
                            throw new ProviderException(
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   196
                                "Secmod not initialized and "
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   197
                                 + "nssSecmodDirectory not specified");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                        if (nssSecmodDirectory != null) {
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   201
                            throw new ProviderException(
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   202
                                "nssSecmodDirectory must not be "
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   203
                                + "specified in noDb mode");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                    }
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   206
                    secmod.initialize(nssDbMode, nssSecmodDirectory,
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   207
                        nssLibraryDirectory);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                // XXX which exception to throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                throw new ProviderException("Could not initialize NSS", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            List<Module> modules = secmod.getModules();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            if (config.getShowInfo()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                System.out.println("NSS modules: " + modules);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            String moduleName = config.getNssModule();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            if (moduleName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                nssModule = secmod.getModule(ModuleType.FIPS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                if (nssModule != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                    moduleName = "fips";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                } else {
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   224
                    moduleName = (nssDbMode == DbMode.NO_DB) ?
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   225
                        "crypto" : "keystore";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            if (moduleName.equals("fips")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                nssModule = secmod.getModule(ModuleType.FIPS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                nssUseSecmodTrust = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                functionList = "FC_GetFunctionList";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            } else if (moduleName.equals("keystore")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                nssModule = secmod.getModule(ModuleType.KEYSTORE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                nssUseSecmodTrust = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            } else if (moduleName.equals("crypto")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                nssModule = secmod.getModule(ModuleType.CRYPTO);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            } else if (moduleName.equals("trustanchors")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                // XXX should the option be called trustanchor or trustanchors??
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                nssModule = secmod.getModule(ModuleType.TRUSTANCHOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                nssUseSecmodTrust = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            } else if (moduleName.startsWith("external-")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                int moduleIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    moduleIndex = Integer.parseInt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                            (moduleName.substring("external-".length()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                    moduleIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                if (moduleIndex < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                    throw new ProviderException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                            ("Invalid external module: " + moduleName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                int k = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                for (Module module : modules) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    if (module.getType() == ModuleType.EXTERNAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                        if (++k == moduleIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                            nssModule = module;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                if (nssModule == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    throw new ProviderException("Invalid module " + moduleName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                        + ": only " + k + " external NSS modules available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            } else {
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   267
                throw new ProviderException(
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   268
                    "Unknown NSS module: " + moduleName);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            if (nssModule == null) {
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   271
                throw new ProviderException(
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   272
                    "NSS module not available: " + moduleName);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            if (nssModule.hasInitializedProvider()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                throw new ProviderException("Secmod module already configured");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            library = nssModule.libraryName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            slotListIndex = nssModule.slot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        this.nssUseSecmodTrust = nssUseSecmodTrust;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        this.nssModule = nssModule;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        File libraryFile = new File(library);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        // if the filename is a simple filename without path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        // (e.g. "libpkcs11.so"), it may refer to a library somewhere on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        // OS library search path. Omit the test for file existance as that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        // only looks in the current directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        if (libraryFile.getName().equals(library) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            if (new File(library).isFile() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                String msg = "Library " + library + " does not exist";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                if (config.getHandleStartupErrors() == Config.ERR_HALT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    throw new ProviderException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    throw new UnsupportedOperationException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                debug.println("Initializing PKCS#11 library " + library);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            CK_C_INITIALIZE_ARGS initArgs = new CK_C_INITIALIZE_ARGS();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            String nssArgs = config.getNssArgs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            if (nssArgs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                initArgs.pReserved = nssArgs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            // request multithreaded access first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            initArgs.flags = CKF_OS_LOCKING_OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            PKCS11 tmpPKCS11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            try {
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   312
                tmpPKCS11 = PKCS11.getInstance(
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   313
                    library, functionList, initArgs,
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   314
                    config.getOmitInitialize());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                    debug.println("Multi-threaded initialization failed: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                if (config.getAllowSingleThreadedModules() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                    throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                // fall back to single threaded access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                if (nssArgs == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                    // if possible, use null initArgs for better compatibility
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                    initArgs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                    initArgs.flags = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                }
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   329
                tmpPKCS11 = PKCS11.getInstance(library,
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   330
                    functionList, initArgs, config.getOmitInitialize());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            p11 = tmpPKCS11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            CK_INFO p11Info = p11.C_GetInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            if (p11Info.cryptokiVersion.major < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                throw new ProviderException("Only PKCS#11 v2.0 and later "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                + "supported, library version is v" + p11Info.cryptokiVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            boolean showInfo = config.getShowInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            if (showInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                System.out.println("Information for provider " + getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                System.out.println("Library info:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                System.out.println(p11Info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            if ((slotID < 0) || showInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                long[] slots = p11.C_GetSlotList(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                if (showInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    System.out.println("All slots: " + toString(slots));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                    slots = p11.C_GetSlotList(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    System.out.println("Slots with tokens: " + toString(slots));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                if (slotID < 0) {
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   353
                    if ((slotListIndex < 0)
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   354
                            || (slotListIndex >= slots.length)) {
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   355
                        throw new ProviderException("slotListIndex is "
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   356
                            + slotListIndex
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                            + " but token only has " + slots.length + " slots");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                    slotID = slots[slotListIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            this.slotID = slotID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            CK_SLOT_INFO slotInfo = p11.C_GetSlotInfo(slotID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            removable = (slotInfo.flags & CKF_REMOVABLE_DEVICE) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            initToken(slotInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            if (nssModule != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                nssModule.setProvider(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            if (config.getHandleStartupErrors() == Config.ERR_IGNORE_ALL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                throw new UnsupportedOperationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                        ("Initialization failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                throw new ProviderException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                        ("Initialization failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    private static String toString(long[] longs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        if (longs.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            return "(none)";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        sb.append(longs[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        for (int i = 1; i < longs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            sb.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            sb.append(longs[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        return this == obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        return System.identityHashCode(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    private static String[] s(String s1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        return new String[] {s1};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    private static String[] s(String s1, String s2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        return new String[] {s1, s2};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    private static final class Descriptor {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        final String type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        final String algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        final String className;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        final String[] aliases;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        final int[] mechanisms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        private Descriptor(String type, String algorithm, String className,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                String[] aliases, int[] mechanisms) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            this.algorithm = algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            this.className = className;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            this.aliases = aliases;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            this.mechanisms = mechanisms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        private P11Service service(Token token, int mechanism) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            return new P11Service
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                (token, type, algorithm, className, aliases, mechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            return type + "." + algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    // Map from mechanism to List of Descriptors that should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    // registered if the mechanism is supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    private final static Map<Integer,List<Descriptor>> descriptors =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        new HashMap<Integer,List<Descriptor>>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    private static int[] m(long m1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        return new int[] {(int)m1};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    private static int[] m(long m1, long m2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        return new int[] {(int)m1, (int)m2};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    private static int[] m(long m1, long m2, long m3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        return new int[] {(int)m1, (int)m2, (int)m3};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    private static int[] m(long m1, long m2, long m3, long m4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        return new int[] {(int)m1, (int)m2, (int)m3, (int)m4};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    private static void d(String type, String algorithm, String className,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            int[] m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        register(new Descriptor(type, algorithm, className, null, m));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    private static void d(String type, String algorithm, String className,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            String[] aliases, int[] m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        register(new Descriptor(type, algorithm, className, aliases, m));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    private static void register(Descriptor d) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        for (int i = 0; i < d.mechanisms.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            int m = d.mechanisms[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            Integer key = Integer.valueOf(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            List<Descriptor> list = descriptors.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            if (list == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                list = new ArrayList<Descriptor>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                descriptors.put(key, list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            list.add(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    private final static String MD  = "MessageDigest";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    private final static String SIG = "Signature";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    private final static String KPG = "KeyPairGenerator";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    private final static String KG  = "KeyGenerator";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    private final static String AGP = "AlgorithmParameters";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    private final static String KF  = "KeyFactory";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    private final static String SKF = "SecretKeyFactory";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    private final static String CIP = "Cipher";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    private final static String MAC = "Mac";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    private final static String KA  = "KeyAgreement";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    private final static String KS  = "KeyStore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    private final static String SR  = "SecureRandom";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        // names of all the implementation classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        // use local variables, only used here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        String P11Digest           = "sun.security.pkcs11.P11Digest";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        String P11MAC              = "sun.security.pkcs11.P11MAC";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        String P11KeyPairGenerator = "sun.security.pkcs11.P11KeyPairGenerator";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        String P11KeyGenerator     = "sun.security.pkcs11.P11KeyGenerator";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        String P11RSAKeyFactory    = "sun.security.pkcs11.P11RSAKeyFactory";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        String P11DSAKeyFactory    = "sun.security.pkcs11.P11DSAKeyFactory";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        String P11DHKeyFactory     = "sun.security.pkcs11.P11DHKeyFactory";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        String P11KeyAgreement     = "sun.security.pkcs11.P11KeyAgreement";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        String P11SecretKeyFactory = "sun.security.pkcs11.P11SecretKeyFactory";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        String P11Cipher           = "sun.security.pkcs11.P11Cipher";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        String P11RSACipher        = "sun.security.pkcs11.P11RSACipher";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        String P11Signature        = "sun.security.pkcs11.P11Signature";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        // XXX register all aliases
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        d(MD, "MD2",            P11Digest,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                m(CKM_MD2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        d(MD, "MD5",            P11Digest,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                m(CKM_MD5));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        d(MD, "SHA1",           P11Digest,              s("SHA", "SHA-1"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                m(CKM_SHA_1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        d(MD, "SHA-256",        P11Digest,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                m(CKM_SHA256));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        d(MD, "SHA-384",        P11Digest,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                m(CKM_SHA384));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        d(MD, "SHA-512",        P11Digest,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                m(CKM_SHA512));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        d(MAC, "HmacMD5",       P11MAC,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                m(CKM_MD5_HMAC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        d(MAC, "HmacSHA1",      P11MAC,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                m(CKM_SHA_1_HMAC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        d(MAC, "HmacSHA256",    P11MAC,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                m(CKM_SHA256_HMAC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        d(MAC, "HmacSHA384",    P11MAC,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                m(CKM_SHA384_HMAC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        d(MAC, "HmacSHA512",    P11MAC,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                m(CKM_SHA512_HMAC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        d(MAC, "SslMacMD5",     P11MAC,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                m(CKM_SSL3_MD5_MAC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        d(MAC, "SslMacSHA1",    P11MAC,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                m(CKM_SSL3_SHA1_MAC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        d(KPG, "RSA",           P11KeyPairGenerator,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                m(CKM_RSA_PKCS_KEY_PAIR_GEN));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        d(KPG, "DSA",           P11KeyPairGenerator,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                m(CKM_DSA_KEY_PAIR_GEN));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        d(KPG, "DH",            P11KeyPairGenerator,    s("DiffieHellman"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                m(CKM_DH_PKCS_KEY_PAIR_GEN));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        d(KPG, "EC",            P11KeyPairGenerator,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                m(CKM_EC_KEY_PAIR_GEN));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        d(KG,  "ARCFOUR",       P11KeyGenerator,        s("RC4"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                m(CKM_RC4_KEY_GEN));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        d(KG,  "DES",           P11KeyGenerator,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                m(CKM_DES_KEY_GEN));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        d(KG,  "DESede",        P11KeyGenerator,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                m(CKM_DES3_KEY_GEN, CKM_DES2_KEY_GEN));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        d(KG,  "AES",           P11KeyGenerator,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                m(CKM_AES_KEY_GEN));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        d(KG,  "Blowfish",      P11KeyGenerator,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                m(CKM_BLOWFISH_KEY_GEN));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        // register (Secret)KeyFactories if there are any mechanisms
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        // for a particular algorithm that we support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        d(KF, "RSA",            P11RSAKeyFactory,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                m(CKM_RSA_PKCS_KEY_PAIR_GEN, CKM_RSA_PKCS, CKM_RSA_X_509));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        d(KF, "DSA",            P11DSAKeyFactory,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                m(CKM_DSA_KEY_PAIR_GEN, CKM_DSA, CKM_DSA_SHA1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        d(KF, "DH",             P11DHKeyFactory,        s("DiffieHellman"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                m(CKM_DH_PKCS_KEY_PAIR_GEN, CKM_DH_PKCS_DERIVE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        d(KF, "EC",             P11DHKeyFactory,
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   576
                m(CKM_EC_KEY_PAIR_GEN, CKM_ECDH1_DERIVE,
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   577
                    CKM_ECDSA, CKM_ECDSA_SHA1));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        // AlgorithmParameters for EC.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        // Only needed until we have an EC implementation in the SUN provider.
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   581
        d(AGP, "EC",            "sun.security.ec.ECParameters",
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   582
                                                s("1.2.840.10045.2.1"),
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   583
                m(CKM_EC_KEY_PAIR_GEN, CKM_ECDH1_DERIVE,
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   584
                    CKM_ECDSA, CKM_ECDSA_SHA1));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        d(KA, "DH",             P11KeyAgreement,        s("DiffieHellman"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                m(CKM_DH_PKCS_DERIVE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        d(KA, "ECDH",           "sun.security.pkcs11.P11ECDHKeyAgreement",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                m(CKM_ECDH1_DERIVE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        d(SKF, "ARCFOUR",       P11SecretKeyFactory,    s("RC4"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                m(CKM_RC4));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        d(SKF, "DES",           P11SecretKeyFactory,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                m(CKM_DES_CBC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        d(SKF, "DESede",        P11SecretKeyFactory,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                m(CKM_DES3_CBC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        d(SKF, "AES",           P11SecretKeyFactory,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                m(CKM_AES_CBC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        d(SKF, "Blowfish",      P11SecretKeyFactory,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                m(CKM_BLOWFISH_CBC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        // XXX attributes for Ciphers (supported modes, padding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        d(CIP, "ARCFOUR",                       P11Cipher,      s("RC4"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                m(CKM_RC4));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        d(CIP, "DES/CBC/NoPadding",             P11Cipher,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                m(CKM_DES_CBC));
289
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents: 2
diff changeset
   607
        d(CIP, "DES/CBC/PKCS5Padding",          P11Cipher,
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents: 2
diff changeset
   608
                m(CKM_DES_CBC_PAD, CKM_DES_CBC));
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents: 2
diff changeset
   609
        d(CIP, "DES/ECB",                       P11Cipher,      s("DES"),
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents: 2
diff changeset
   610
                m(CKM_DES_ECB));
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents: 2
diff changeset
   611
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        d(CIP, "DESede/CBC/NoPadding",          P11Cipher,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                m(CKM_DES3_CBC));
289
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents: 2
diff changeset
   614
        d(CIP, "DESede/CBC/PKCS5Padding",       P11Cipher,
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents: 2
diff changeset
   615
                m(CKM_DES3_CBC_PAD, CKM_DES3_CBC));
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents: 2
diff changeset
   616
        d(CIP, "DESede/ECB",                    P11Cipher,      s("DESede"),
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents: 2
diff changeset
   617
                m(CKM_DES3_ECB));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        d(CIP, "AES/CBC/NoPadding",             P11Cipher,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                m(CKM_AES_CBC));
289
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents: 2
diff changeset
   620
        d(CIP, "AES/CBC/PKCS5Padding",          P11Cipher,
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents: 2
diff changeset
   621
                m(CKM_AES_CBC_PAD, CKM_AES_CBC));
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents: 2
diff changeset
   622
        d(CIP, "AES/ECB",                       P11Cipher,      s("AES"),
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents: 2
diff changeset
   623
                m(CKM_AES_ECB));
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents: 2
diff changeset
   624
        d(CIP, "Blowfish/CBC",                  P11Cipher,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                m(CKM_BLOWFISH_CBC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        // XXX RSA_X_509, RSA_OAEP not yet supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        d(CIP, "RSA/ECB/PKCS1Padding",          P11RSACipher,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                m(CKM_RSA_PKCS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        d(SIG, "RawDSA",        P11Signature,           s("NONEwithDSA"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                m(CKM_DSA));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        d(SIG, "DSA",           P11Signature,           s("SHA1withDSA"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                m(CKM_DSA_SHA1, CKM_DSA));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        d(SIG, "NONEwithECDSA", P11Signature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                m(CKM_ECDSA));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        d(SIG, "SHA1withECDSA", P11Signature,           s("ECDSA"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                m(CKM_ECDSA_SHA1, CKM_ECDSA));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        d(SIG, "SHA256withECDSA",       P11Signature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                m(CKM_ECDSA));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        d(SIG, "SHA384withECDSA",       P11Signature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                m(CKM_ECDSA));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        d(SIG, "SHA512withECDSA",       P11Signature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                m(CKM_ECDSA));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        d(SIG, "MD2withRSA",    P11Signature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                m(CKM_MD2_RSA_PKCS, CKM_RSA_PKCS, CKM_RSA_X_509));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        d(SIG, "MD5withRSA",    P11Signature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                m(CKM_MD5_RSA_PKCS, CKM_RSA_PKCS, CKM_RSA_X_509));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        d(SIG, "SHA1withRSA",   P11Signature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                m(CKM_SHA1_RSA_PKCS, CKM_RSA_PKCS, CKM_RSA_X_509));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        d(SIG, "SHA256withRSA", P11Signature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                m(CKM_SHA256_RSA_PKCS, CKM_RSA_PKCS, CKM_RSA_X_509));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        d(SIG, "SHA384withRSA", P11Signature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                m(CKM_SHA384_RSA_PKCS, CKM_RSA_PKCS, CKM_RSA_X_509));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        d(SIG, "SHA512withRSA", P11Signature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                m(CKM_SHA512_RSA_PKCS, CKM_RSA_PKCS, CKM_RSA_X_509));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   658
        /*
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   659
         * TLS 1.2 uses a different hash algorithm than 1.0/1.1 for the
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   660
         * PRF calculations.  As of 2010, there is no PKCS11-level
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   661
         * support for TLS 1.2 PRF calculations, and no known OS's have
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   662
         * an internal variant we could use.  Therefore for TLS 1.2, we
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   663
         * are updating JSSE to request different provider algorithms
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   664
         * (e.g. "SunTls12Prf"), and currently only SunJCE has these
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   665
         * TLS 1.2 algorithms.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   666
         *
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   667
         * If we reused the names such as "SunTlsPrf", the PKCS11
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   668
         * providers would need be updated to fail correctly when
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   669
         * presented with the wrong version number (via
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   670
         * Provider.Service.supportsParameters()), and we would also
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   671
         * need to add the appropriate supportsParamters() checks into
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   672
         * KeyGenerators (not currently there).
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   673
         *
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   674
         * In the future, if PKCS11 support is added, we will restructure
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   675
         * this.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   676
         */
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   677
        d(KG, "SunTlsRsaPremasterSecret",
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   678
                    "sun.security.pkcs11.P11TlsRsaPremasterSecretGenerator",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                m(CKM_SSL3_PRE_MASTER_KEY_GEN, CKM_TLS_PRE_MASTER_KEY_GEN));
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   680
        d(KG, "SunTlsMasterSecret",
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   681
                    "sun.security.pkcs11.P11TlsMasterSecretGenerator",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                m(CKM_SSL3_MASTER_KEY_DERIVE, CKM_TLS_MASTER_KEY_DERIVE,
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   683
                    CKM_SSL3_MASTER_KEY_DERIVE_DH,
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   684
                    CKM_TLS_MASTER_KEY_DERIVE_DH));
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   685
        d(KG, "SunTlsKeyMaterial",
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   686
                    "sun.security.pkcs11.P11TlsKeyMaterialGenerator",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                m(CKM_SSL3_KEY_AND_MAC_DERIVE, CKM_TLS_KEY_AND_MAC_DERIVE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        d(KG, "SunTlsPrf", "sun.security.pkcs11.P11TlsPrfGenerator",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                m(CKM_TLS_PRF, CKM_NSS_TLS_PRF_GENERAL));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    // background thread that periodically checks for token insertion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    // if no token is present. We need to do that in a separate thread because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    // the insertion check may block for quite a long time on some tokens.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
    private static class TokenPoller implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        private final SunPKCS11 provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        private volatile boolean enabled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        private TokenPoller(SunPKCS11 provider) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            this.provider = provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            enabled = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            int interval = provider.config.getInsertionCheckInterval();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            while (enabled) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                    Thread.sleep(interval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                if (enabled == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                    provider.initToken(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                    // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        void disable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            enabled = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    // create the poller thread, if not already active
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
    private void createPoller() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        if (poller != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        TokenPoller poller = new TokenPoller(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        Thread t = new Thread(poller, "Poller " + getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        t.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        t.setPriority(Thread.MIN_PRIORITY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        t.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        this.poller = poller;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
    // destroy the poller thread, if active
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    private void destroyPoller() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        if (poller != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            poller.disable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            poller = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    private boolean hasValidToken() {
487
df198b4170f7 6524501: inconsistency with PKCS#11 spec - 0-value flags in CK_SLOT_INFO struct returned by C_GetSlotInfo()
valeriep
parents: 289
diff changeset
   747
        /* Commented out to work with Solaris softtoken impl which
df198b4170f7 6524501: inconsistency with PKCS#11 spec - 0-value flags in CK_SLOT_INFO struct returned by C_GetSlotInfo()
valeriep
parents: 289
diff changeset
   748
           returns 0-value flags, e.g. both REMOVABLE_DEVICE and
df198b4170f7 6524501: inconsistency with PKCS#11 spec - 0-value flags in CK_SLOT_INFO struct returned by C_GetSlotInfo()
valeriep
parents: 289
diff changeset
   749
           TOKEN_PRESENT are false, when it can't access the token.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        if (removable == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        }
487
df198b4170f7 6524501: inconsistency with PKCS#11 spec - 0-value flags in CK_SLOT_INFO struct returned by C_GetSlotInfo()
valeriep
parents: 289
diff changeset
   753
        */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        Token token = this.token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        return (token != null) && token.isValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    // destroy the token. Called if we detect that it has been removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
    synchronized void uninitToken(Token token) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        if (this.token != token) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            // mismatch, our token must already be destroyed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        destroyPoller();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        this.token = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        // unregister all algorithms
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        AccessController.doPrivileged(new PrivilegedAction<Object>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        createPoller();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    // test if a token is present and initialize this provider for it if so.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
    // does nothing if no token is found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    // called from constructor and by poller
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
    private void initToken(CK_SLOT_INFO slotInfo) throws PKCS11Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        if (slotInfo == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            slotInfo = p11.C_GetSlotInfo(slotID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        }
487
df198b4170f7 6524501: inconsistency with PKCS#11 spec - 0-value flags in CK_SLOT_INFO struct returned by C_GetSlotInfo()
valeriep
parents: 289
diff changeset
   783
        if (removable && (slotInfo.flags & CKF_TOKEN_PRESENT) == 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            createPoller();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        destroyPoller();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        boolean showInfo = config.getShowInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        if (showInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            System.out.println("Slot info for slot " + slotID + ":");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            System.out.println(slotInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        final Token token = new Token(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        if (showInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            System.out.println
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                ("Token info for token in slot " + slotID + ":");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            System.out.println(token.tokenInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        long[] supportedMechanisms = p11.C_GetMechanismList(slotID);
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   800
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   801
        // Create a map from the various Descriptors to the "most
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   802
        // preferred" mechanism that was defined during the
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   803
        // static initialization.  For example, DES/CBC/PKCS5Padding
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   804
        // could be mapped to CKM_DES_CBC_PAD or CKM_DES_CBC.  Prefer
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   805
        // the earliest entry.  When asked for "DES/CBC/PKCS5Padding", we
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   806
        // return a CKM_DES_CBC_PAD.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        final Map<Descriptor,Integer> supportedAlgs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                                        new HashMap<Descriptor,Integer>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        for (int i = 0; i < supportedMechanisms.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            long longMech = supportedMechanisms[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            boolean isEnabled = config.isEnabled(longMech);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            if (showInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                CK_MECHANISM_INFO mechInfo =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                        p11.C_GetMechanismInfo(slotID, longMech);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                System.out.println("Mechanism " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                        Functions.getMechanismName(longMech) + ":");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                if (isEnabled == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                    System.out.println("DISABLED in configuration");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                System.out.println(mechInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            if (isEnabled == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            // we do not know of mechs with the upper 32 bits set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            if (longMech >>> 32 != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            int mech = (int)longMech;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            Integer integerMech = Integer.valueOf(mech);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            List<Descriptor> ds = descriptors.get(integerMech);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
            if (ds == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            for (Descriptor d : ds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                Integer oldMech = supportedAlgs.get(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                if (oldMech == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                    supportedAlgs.put(d, integerMech);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                }
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   841
                // See if there is something "more preferred"
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   842
                // than what we currently have in the supportedAlgs
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   843
                // map.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                int intOldMech = oldMech.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                for (int j = 0; j < d.mechanisms.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                    int nextMech = d.mechanisms[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                    if (mech == nextMech) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                        supportedAlgs.put(d, integerMech);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                    } else if (intOldMech == nextMech) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        // register algorithms in provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        AccessController.doPrivileged(new PrivilegedAction<Object>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                for (Map.Entry<Descriptor,Integer> entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                        : supportedAlgs.entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
                    Descriptor d = entry.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
                    int mechanism = entry.getValue().intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                    Service s = d.service(token, mechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                    putService(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                if (((token.tokenInfo.flags & CKF_RNG) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
                        && config.isEnabled(PCKM_SECURERANDOM)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
                        && !token.sessionManager.lowMaxSessions()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                    // do not register SecureRandom if the token does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
                    // not support many sessions. if we did, we might
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
                    // run out of sessions in the middle of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
                    // nextBytes() call where we cannot fail over.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                    putService(new P11Service(token, SR, "PKCS11",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                        "sun.security.pkcs11.P11SecureRandom", null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                        PCKM_SECURERANDOM));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                if (config.isEnabled(PCKM_KEYSTORE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
                    putService(new P11Service(token, KS, "PKCS11",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                        "sun.security.pkcs11.P11KeyStore",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                        s("PKCS11-" + config.getName()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                        PCKM_KEYSTORE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        this.token = token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
    private static final class P11Service extends Service {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        private final Token token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        private final long mechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        P11Service(Token token, String type, String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                String className, String[] al, long mechanism) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
            super(token.provider, type, algorithm, className, toList(al), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            this.token = token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
            this.mechanism = mechanism & 0xFFFFFFFFL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        private static List<String> toList(String[] aliases) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
            return (aliases == null) ? null : Arrays.asList(aliases);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   909
        public Object newInstance(Object param)
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   910
                throws NoSuchAlgorithmException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
            if (token.isValid() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                throw new NoSuchAlgorithmException("Token has been removed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
                return newInstance0(param);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
                throw new NoSuchAlgorithmException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        public Object newInstance0(Object param) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
                PKCS11Exception, NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            String algorithm = getAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            String type = getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
            if (type == MD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
                return new P11Digest(token, algorithm, mechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
            } else if (type == CIP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                if (algorithm.startsWith("RSA")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                    return new P11RSACipher(token, algorithm, mechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
                    return new P11Cipher(token, algorithm, mechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
            } else if (type == SIG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                return new P11Signature(token, algorithm, mechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
            } else if (type == MAC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
                return new P11Mac(token, algorithm, mechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
            } else if (type == KPG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                return new P11KeyPairGenerator(token, algorithm, mechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
            } else if (type == KA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                if (algorithm.equals("ECDH")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                    return new P11ECDHKeyAgreement(token, algorithm, mechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                    return new P11KeyAgreement(token, algorithm, mechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
            } else if (type == KF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
                return token.getKeyFactory(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            } else if (type == SKF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
                return new P11SecretKeyFactory(token, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
            } else if (type == KG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                // reference equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                if (algorithm == "SunTlsRsaPremasterSecret") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                    return new P11TlsRsaPremasterSecretGenerator(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                        token, algorithm, mechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                } else if (algorithm == "SunTlsMasterSecret") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                    return new P11TlsMasterSecretGenerator(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                        token, algorithm, mechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
                } else if (algorithm == "SunTlsKeyMaterial") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                    return new P11TlsKeyMaterialGenerator(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
                        token, algorithm, mechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
                } else if (algorithm == "SunTlsPrf") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                    return new P11TlsPrfGenerator(token, algorithm, mechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
                    return new P11KeyGenerator(token, algorithm, mechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
            } else if (type == SR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                return token.getRandom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
            } else if (type == KS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                return token.getKeyStore();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
            } else if (type == AGP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
                return new sun.security.ec.ECParameters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                throw new NoSuchAlgorithmException("Unknown type: " + type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        public boolean supportsParameter(Object param) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
            if ((param == null) || (token.isValid() == false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
            if (param instanceof Key == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
                throw new InvalidParameterException("Parameter must be a Key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
            String algorithm = getAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
            String type = getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
            Key key = (Key)param;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
            String keyAlgorithm = key.getAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
            // RSA signatures and cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
            if (((type == CIP) && algorithm.startsWith("RSA"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
                    || (type == SIG) && algorithm.endsWith("RSA")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
                if (keyAlgorithm.equals("RSA") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
                return isLocalKey(key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
                        || (key instanceof RSAPrivateKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                        || (key instanceof RSAPublicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            // EC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
            if (((type == KA) && algorithm.equals("ECDH"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
                    || ((type == SIG) && algorithm.endsWith("ECDSA"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
                if (keyAlgorithm.equals("EC") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
                return isLocalKey(key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
                        || (key instanceof ECPrivateKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
                        || (key instanceof ECPublicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
            // DSA signatures
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
            if ((type == SIG) && algorithm.endsWith("DSA")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
                if (keyAlgorithm.equals("DSA") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
                return isLocalKey(key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
                        || (key instanceof DSAPrivateKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
                        || (key instanceof DSAPublicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
            // MACs and symmetric ciphers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
            if ((type == CIP) || (type == MAC)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                // do not check algorithm name, mismatch is unlikely anyway
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                return isLocalKey(key) || "RAW".equals(key.getFormat());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            // DH key agreement
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            if (type == KA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
                if (keyAlgorithm.equals("DH") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
                return isLocalKey(key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
                        || (key instanceof DHPrivateKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
                        || (key instanceof DHPublicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
            // should not reach here,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
            // unknown engine type or algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            throw new AssertionError
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
                ("SunPKCS11 error: " + type + ", " + algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        private boolean isLocalKey(Key key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
            return (key instanceof P11Key) && (((P11Key)key).token == token);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
            return super.toString() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
                " (" + Functions.getMechanismName(mechanism) + ")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     * Log in to this provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     * <p> If the token expects a PIN to be supplied by the caller,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     * the <code>handler</code> implementation must support
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     * a <code>PasswordCallback</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     * <p> To determine if the token supports a protected authentication path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * the CK_TOKEN_INFO flag, CKF_PROTECTED_AUTHENTICATION_PATH, is consulted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     * @param subject this parameter is ignored
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     * @param handler the <code>CallbackHandler</code> used by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     *  this provider to communicate with the caller
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     * @exception LoginException if the login operation fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     * @exception SecurityException if the does not pass a security check for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     *  <code>SecurityPermission("authProvider.<i>name</i>")</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     *  where <i>name</i> is the value returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     *  this provider's <code>getName</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
    public void login(Subject subject, CallbackHandler handler)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
        throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
        // security check
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
                debug.println("checking login permission");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
            sm.checkPermission(new SecurityPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
                        ("authProvider." + this.getName()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        if (hasValidToken() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
            throw new LoginException("No token present");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        // see if a login is required
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        if ((token.tokenInfo.flags & CKF_LOGIN_REQUIRED) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
                debug.println("login operation not required for token - " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
                                "ignoring login request");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        // see if user already logged in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
            if (token.isLoggedInNow(null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
                // user already logged in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
                    debug.println("user already logged in");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
            // ignore - fall thru and attempt login
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
        // get the pin if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        char[] pin = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        if ((token.tokenInfo.flags & CKF_PROTECTED_AUTHENTICATION_PATH) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
            // get password
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
            CallbackHandler myHandler = getCallbackHandler(handler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
            if (myHandler == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
                // XXX PolicyTool is dependent on this message text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
                throw new LoginException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
                        ("no password provided, and no callback handler " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
                        "available for retrieving password");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
            java.text.MessageFormat form = new java.text.MessageFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
                        (ResourcesMgr.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 7043
diff changeset
  1126
                        ("PKCS11.Token.providerName.Password."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
            Object[] source = { getName() };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
            PasswordCallback pcall = new PasswordCallback(form.format(source),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
                                                        false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
            Callback[] callbacks = { pcall };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
                myHandler.handle(callbacks);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
                LoginException le = new LoginException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
                        ("Unable to perform password callback");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
                le.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
                throw le;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
            pin = pcall.getPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
            pcall.clearPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
            if (pin == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
                    debug.println("caller passed NULL pin");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        // perform token login
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
        Session session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
            session = token.getOpSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
            // pin is NULL if using CKF_PROTECTED_AUTHENTICATION_PATH
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
            p11.C_Login(session.id(), CKU_USER, pin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
                debug.println("login succeeded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
        } catch (PKCS11Exception pe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
            if (pe.getErrorCode() == CKR_USER_ALREADY_LOGGED_IN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
                // let this one go
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
                    debug.println("user already logged in");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
            } else if (pe.getErrorCode() == CKR_PIN_INCORRECT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
                FailedLoginException fle = new FailedLoginException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
                fle.initCause(pe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
                throw fle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
                LoginException le = new LoginException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
                le.initCause(pe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
                throw le;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
            token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
            if (pin != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
                Arrays.fill(pin, ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
        // we do not store the PIN in the subject for now
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
     * Log out from this provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     * @exception LoginException if the logout operation fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     * @exception SecurityException if the does not pass a security check for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
     *  <code>SecurityPermission("authProvider.<i>name</i>")</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
     *  where <i>name</i> is the value returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
     *  this provider's <code>getName</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
    public void logout() throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
        // security check
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
            sm.checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
                (new SecurityPermission("authProvider." + this.getName()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
        if (hasValidToken() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
            // app may call logout for cleanup, allow
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
        if ((token.tokenInfo.flags & CKF_LOGIN_REQUIRED) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
                debug.println("logout operation not required for token - " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
                                "ignoring logout request");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
            if (token.isLoggedInNow(null) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
                    debug.println("user not logged in");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
            // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
        // perform token logout
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
        Session session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
            session = token.getOpSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
            p11.C_Logout(session.id());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
                debug.println("logout succeeded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
        } catch (PKCS11Exception pe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
            if (pe.getErrorCode() == CKR_USER_NOT_LOGGED_IN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
                // let this one go
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
                    debug.println("user not logged in");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
            LoginException le = new LoginException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
            le.initCause(pe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
            throw le;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
            token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     * Set a <code>CallbackHandler</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
     * <p> The provider uses this handler if one is not passed to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
     * <code>login</code> method.  The provider also uses this handler
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
     * if it invokes <code>login</code> on behalf of callers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
     * In either case if a handler is not set via this method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
     * the provider queries the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
     * <i>auth.login.defaultCallbackHandler</i> security property
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
     * for the fully qualified class name of a default handler implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
     * If the security property is not set,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
     * the provider is assumed to have alternative means
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
     * for obtaining authentication information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
     * @param handler a <code>CallbackHandler</code> for obtaining
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
     *          authentication information, which may be <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
     * @exception SecurityException if the caller does not pass a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
     *  security check for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
     *  <code>SecurityPermission("authProvider.<i>name</i>")</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
     *  where <i>name</i> is the value returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
     *  this provider's <code>getName</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
    public void setCallbackHandler(CallbackHandler handler) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
        // security check
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
            sm.checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
                (new SecurityPermission("authProvider." + this.getName()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
        synchronized (LOCK_HANDLER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
            pHandler = handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
    private CallbackHandler getCallbackHandler(CallbackHandler handler) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
        // get default handler if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
        if (handler != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
            return handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
            debug.println("getting provider callback handler");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
        synchronized (LOCK_HANDLER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
            // see if handler was set via setCallbackHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
            if (pHandler != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
                return pHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
                    debug.println("getting default callback handler");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
                CallbackHandler myHandler = AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
                    (new PrivilegedExceptionAction<CallbackHandler>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
                    public CallbackHandler run() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
                        String defaultHandler =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
                                java.security.Security.getProperty
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
                                ("auth.login.defaultCallbackHandler");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
                        if (defaultHandler == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
                            defaultHandler.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
                            // ok
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
                            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
                                debug.println("no default handler set");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
                            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
                        Class c = Class.forName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
                                (defaultHandler,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
                                true,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
                                Thread.currentThread().getContextClassLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
                        return (CallbackHandler)c.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
                // save it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
                pHandler = myHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
                return myHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
            } catch (PrivilegedActionException pae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
                // ok
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
                    debug.println("Unable to load default callback handler");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
                    pae.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
    private Object writeReplace() throws ObjectStreamException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
        return new SunPKCS11Rep(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
     * Serialized representation of the SunPKCS11 provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
    private static class SunPKCS11Rep implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
        static final long serialVersionUID = -2896606995897745419L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
        private final String providerName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
        private final String configName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
        SunPKCS11Rep(SunPKCS11 provider) throws NotSerializableException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
            providerName = provider.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
            configName = provider.configName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
            if (Security.getProvider(providerName) != provider) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
                throw new NotSerializableException("Only SunPKCS11 providers "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
                    + "installed in java.security.Security can be serialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
        private Object readResolve() throws ObjectStreamException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
            SunPKCS11 p = (SunPKCS11)Security.getProvider(providerName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
            if ((p == null) || (p.configName.equals(configName) == false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
                throw new NotSerializableException("Could not find "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
                        + providerName + " in installed providers");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
            return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
}