jdk/src/share/classes/sun/security/pkcs11/SunPKCS11.java
author valeriep
Tue, 04 Sep 2012 18:41:06 -0700
changeset 13672 604588823b5a
parent 12685 8a448b5b9006
child 18772 ab43f18c524e
permissions -rw-r--r--
7044060: Need to support NSA Suite B Cryptography algorithms Summary: Add support for DSA parameter generation and OIDs for NSA Suite B algorithms. Reviewed-by: vinnie
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
11509
02fb543bc69d 7033170: Cipher.getMaxAllowedKeyLength(String) throws NoSuchAlgorithmException
valeriep
parents: 10701
diff changeset
     2
 * Copyright (c) 2003, 2012, 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
            }
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 12042
diff changeset
   345
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            if ((slotID < 0) || showInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                long[] slots = p11.C_GetSlotList(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                if (showInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                    System.out.println("All slots: " + toString(slots));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    slots = p11.C_GetSlotList(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                    System.out.println("Slots with tokens: " + toString(slots));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                if (slotID < 0) {
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   354
                    if ((slotListIndex < 0)
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   355
                            || (slotListIndex >= slots.length)) {
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   356
                        throw new ProviderException("slotListIndex is "
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   357
                            + slotListIndex
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                            + " but token only has " + slots.length + " slots");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                    slotID = slots[slotListIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            this.slotID = slotID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            CK_SLOT_INFO slotInfo = p11.C_GetSlotInfo(slotID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            removable = (slotInfo.flags & CKF_REMOVABLE_DEVICE) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            initToken(slotInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            if (nssModule != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                nssModule.setProvider(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            if (config.getHandleStartupErrors() == Config.ERR_IGNORE_ALL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                throw new UnsupportedOperationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                        ("Initialization failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                throw new ProviderException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                        ("Initialization failed", e);
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    private static String toString(long[] longs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        if (longs.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            return "(none)";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        sb.append(longs[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        for (int i = 1; i < longs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            sb.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            sb.append(longs[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        return this == obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        return System.identityHashCode(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 12685
diff changeset
   402
    private static String[] s(String ...aliases) {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 12685
diff changeset
   403
        return aliases;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    private static final class Descriptor {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        final String type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        final String algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        final String className;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        final String[] aliases;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        final int[] mechanisms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        private Descriptor(String type, String algorithm, String className,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                String[] aliases, int[] mechanisms) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            this.algorithm = algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            this.className = className;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            this.aliases = aliases;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            this.mechanisms = mechanisms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        private P11Service service(Token token, int mechanism) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            return new P11Service
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                (token, type, algorithm, className, aliases, mechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            return type + "." + algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    // Map from mechanism to List of Descriptors that should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    // registered if the mechanism is supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    private final static Map<Integer,List<Descriptor>> descriptors =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        new HashMap<Integer,List<Descriptor>>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    private static int[] m(long m1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        return new int[] {(int)m1};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    private static int[] m(long m1, long m2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        return new int[] {(int)m1, (int)m2};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    private static int[] m(long m1, long m2, long m3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        return new int[] {(int)m1, (int)m2, (int)m3};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    private static int[] m(long m1, long m2, long m3, long m4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        return new int[] {(int)m1, (int)m2, (int)m3, (int)m4};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    private static void d(String type, String algorithm, String className,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            int[] m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        register(new Descriptor(type, algorithm, className, null, m));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    private static void d(String type, String algorithm, String className,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            String[] aliases, int[] m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        register(new Descriptor(type, algorithm, className, aliases, m));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    private static void register(Descriptor d) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        for (int i = 0; i < d.mechanisms.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            int m = d.mechanisms[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            Integer key = Integer.valueOf(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            List<Descriptor> list = descriptors.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            if (list == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                list = new ArrayList<Descriptor>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                descriptors.put(key, list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            list.add(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    private final static String MD  = "MessageDigest";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    private final static String SIG = "Signature";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    private final static String KPG = "KeyPairGenerator";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    private final static String KG  = "KeyGenerator";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    private final static String AGP = "AlgorithmParameters";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    private final static String KF  = "KeyFactory";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    private final static String SKF = "SecretKeyFactory";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    private final static String CIP = "Cipher";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    private final static String MAC = "Mac";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    private final static String KA  = "KeyAgreement";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    private final static String KS  = "KeyStore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    private final static String SR  = "SecureRandom";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        // names of all the implementation classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        // use local variables, only used here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        String P11Digest           = "sun.security.pkcs11.P11Digest";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        String P11MAC              = "sun.security.pkcs11.P11MAC";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        String P11KeyPairGenerator = "sun.security.pkcs11.P11KeyPairGenerator";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        String P11KeyGenerator     = "sun.security.pkcs11.P11KeyGenerator";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        String P11RSAKeyFactory    = "sun.security.pkcs11.P11RSAKeyFactory";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        String P11DSAKeyFactory    = "sun.security.pkcs11.P11DSAKeyFactory";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        String P11DHKeyFactory     = "sun.security.pkcs11.P11DHKeyFactory";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        String P11KeyAgreement     = "sun.security.pkcs11.P11KeyAgreement";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        String P11SecretKeyFactory = "sun.security.pkcs11.P11SecretKeyFactory";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        String P11Cipher           = "sun.security.pkcs11.P11Cipher";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        String P11RSACipher        = "sun.security.pkcs11.P11RSACipher";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        String P11Signature        = "sun.security.pkcs11.P11Signature";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        // XXX register all aliases
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        d(MD, "MD2",            P11Digest,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                m(CKM_MD2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        d(MD, "MD5",            P11Digest,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                m(CKM_MD5));
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 12685
diff changeset
   520
        d(MD, "SHA1",           P11Digest,
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 12685
diff changeset
   521
                s("SHA", "SHA-1", "1.3.14.3.2.26", "OID.1.3.14.3.2.26"),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                m(CKM_SHA_1));
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 12042
diff changeset
   523
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 12042
diff changeset
   524
        d(MD, "SHA-224",        P11Digest,
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 12042
diff changeset
   525
                s("2.16.840.1.101.3.4.2.4", "OID.2.16.840.1.101.3.4.2.4"),
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 12042
diff changeset
   526
                m(CKM_SHA224));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        d(MD, "SHA-256",        P11Digest,
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 12042
diff changeset
   528
                s("2.16.840.1.101.3.4.2.1", "OID.2.16.840.1.101.3.4.2.1"),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                m(CKM_SHA256));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        d(MD, "SHA-384",        P11Digest,
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 12042
diff changeset
   531
                s("2.16.840.1.101.3.4.2.2", "OID.2.16.840.1.101.3.4.2.2"),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                m(CKM_SHA384));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        d(MD, "SHA-512",        P11Digest,
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 12042
diff changeset
   534
                s("2.16.840.1.101.3.4.2.3", "OID.2.16.840.1.101.3.4.2.3"),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                m(CKM_SHA512));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        d(MAC, "HmacMD5",       P11MAC,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                m(CKM_MD5_HMAC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        d(MAC, "HmacSHA1",      P11MAC,
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 12685
diff changeset
   540
                s("1.2.840.113549.2.7", "OID.1.2.840.113549.2.7"),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                m(CKM_SHA_1_HMAC));
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 12042
diff changeset
   542
        d(MAC, "HmacSHA224",    P11MAC,
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 12042
diff changeset
   543
                s("1.2.840.113549.2.8", "OID.1.2.840.113549.2.8"),
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 12042
diff changeset
   544
                m(CKM_SHA224_HMAC));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        d(MAC, "HmacSHA256",    P11MAC,
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 12042
diff changeset
   546
                s("1.2.840.113549.2.9", "OID.1.2.840.113549.2.9"),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                m(CKM_SHA256_HMAC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        d(MAC, "HmacSHA384",    P11MAC,
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 12042
diff changeset
   549
                s("1.2.840.113549.2.10", "OID.1.2.840.113549.2.10"),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                m(CKM_SHA384_HMAC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        d(MAC, "HmacSHA512",    P11MAC,
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 12042
diff changeset
   552
                s("1.2.840.113549.2.11", "OID.1.2.840.113549.2.11"),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                m(CKM_SHA512_HMAC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        d(MAC, "SslMacMD5",     P11MAC,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                m(CKM_SSL3_MD5_MAC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        d(MAC, "SslMacSHA1",    P11MAC,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                m(CKM_SSL3_SHA1_MAC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        d(KPG, "RSA",           P11KeyPairGenerator,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                m(CKM_RSA_PKCS_KEY_PAIR_GEN));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        d(KPG, "DSA",           P11KeyPairGenerator,
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 12685
diff changeset
   562
                s("1.3.14.3.2.12", "1.2.840.10040.4.1", "OID.1.2.840.10040.4.1"),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                m(CKM_DSA_KEY_PAIR_GEN));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        d(KPG, "DH",            P11KeyPairGenerator,    s("DiffieHellman"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                m(CKM_DH_PKCS_KEY_PAIR_GEN));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        d(KPG, "EC",            P11KeyPairGenerator,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                m(CKM_EC_KEY_PAIR_GEN));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        d(KG,  "ARCFOUR",       P11KeyGenerator,        s("RC4"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                m(CKM_RC4_KEY_GEN));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        d(KG,  "DES",           P11KeyGenerator,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                m(CKM_DES_KEY_GEN));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        d(KG,  "DESede",        P11KeyGenerator,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                m(CKM_DES3_KEY_GEN, CKM_DES2_KEY_GEN));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        d(KG,  "AES",           P11KeyGenerator,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                m(CKM_AES_KEY_GEN));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        d(KG,  "Blowfish",      P11KeyGenerator,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                m(CKM_BLOWFISH_KEY_GEN));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        // register (Secret)KeyFactories if there are any mechanisms
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        // for a particular algorithm that we support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        d(KF, "RSA",            P11RSAKeyFactory,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                m(CKM_RSA_PKCS_KEY_PAIR_GEN, CKM_RSA_PKCS, CKM_RSA_X_509));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        d(KF, "DSA",            P11DSAKeyFactory,
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 12685
diff changeset
   585
                s("1.3.14.3.2.12", "1.2.840.10040.4.1", "OID.1.2.840.10040.4.1"),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                m(CKM_DSA_KEY_PAIR_GEN, CKM_DSA, CKM_DSA_SHA1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        d(KF, "DH",             P11DHKeyFactory,        s("DiffieHellman"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                m(CKM_DH_PKCS_KEY_PAIR_GEN, CKM_DH_PKCS_DERIVE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        d(KF, "EC",             P11DHKeyFactory,
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   590
                m(CKM_EC_KEY_PAIR_GEN, CKM_ECDH1_DERIVE,
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   591
                    CKM_ECDSA, CKM_ECDSA_SHA1));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        // AlgorithmParameters for EC.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        // 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
   595
        d(AGP, "EC",            "sun.security.ec.ECParameters",
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   596
                                                s("1.2.840.10045.2.1"),
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   597
                m(CKM_EC_KEY_PAIR_GEN, CKM_ECDH1_DERIVE,
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   598
                    CKM_ECDSA, CKM_ECDSA_SHA1));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        d(KA, "DH",             P11KeyAgreement,        s("DiffieHellman"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                m(CKM_DH_PKCS_DERIVE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        d(KA, "ECDH",           "sun.security.pkcs11.P11ECDHKeyAgreement",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                m(CKM_ECDH1_DERIVE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        d(SKF, "ARCFOUR",       P11SecretKeyFactory,    s("RC4"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                m(CKM_RC4));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        d(SKF, "DES",           P11SecretKeyFactory,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                m(CKM_DES_CBC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        d(SKF, "DESede",        P11SecretKeyFactory,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                m(CKM_DES3_CBC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        d(SKF, "AES",           P11SecretKeyFactory,
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 12685
diff changeset
   612
                s("2.16.840.1.101.3.4.1", "OID.2.16.840.1.101.3.4.1"),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                m(CKM_AES_CBC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        d(SKF, "Blowfish",      P11SecretKeyFactory,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                m(CKM_BLOWFISH_CBC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        // XXX attributes for Ciphers (supported modes, padding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        d(CIP, "ARCFOUR",                       P11Cipher,      s("RC4"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                m(CKM_RC4));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        d(CIP, "DES/CBC/NoPadding",             P11Cipher,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                m(CKM_DES_CBC));
289
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents: 2
diff changeset
   622
        d(CIP, "DES/CBC/PKCS5Padding",          P11Cipher,
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents: 2
diff changeset
   623
                m(CKM_DES_CBC_PAD, CKM_DES_CBC));
11509
02fb543bc69d 7033170: Cipher.getMaxAllowedKeyLength(String) throws NoSuchAlgorithmException
valeriep
parents: 10701
diff changeset
   624
        d(CIP, "DES/ECB/NoPadding",             P11Cipher,
02fb543bc69d 7033170: Cipher.getMaxAllowedKeyLength(String) throws NoSuchAlgorithmException
valeriep
parents: 10701
diff changeset
   625
                m(CKM_DES_ECB));
02fb543bc69d 7033170: Cipher.getMaxAllowedKeyLength(String) throws NoSuchAlgorithmException
valeriep
parents: 10701
diff changeset
   626
        d(CIP, "DES/ECB/PKCS5Padding",          P11Cipher,      s("DES"),
289
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents: 2
diff changeset
   627
                m(CKM_DES_ECB));
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents: 2
diff changeset
   628
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        d(CIP, "DESede/CBC/NoPadding",          P11Cipher,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                m(CKM_DES3_CBC));
289
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents: 2
diff changeset
   631
        d(CIP, "DESede/CBC/PKCS5Padding",       P11Cipher,
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents: 2
diff changeset
   632
                m(CKM_DES3_CBC_PAD, CKM_DES3_CBC));
11509
02fb543bc69d 7033170: Cipher.getMaxAllowedKeyLength(String) throws NoSuchAlgorithmException
valeriep
parents: 10701
diff changeset
   633
        d(CIP, "DESede/ECB/NoPadding",          P11Cipher,
02fb543bc69d 7033170: Cipher.getMaxAllowedKeyLength(String) throws NoSuchAlgorithmException
valeriep
parents: 10701
diff changeset
   634
                m(CKM_DES3_ECB));
02fb543bc69d 7033170: Cipher.getMaxAllowedKeyLength(String) throws NoSuchAlgorithmException
valeriep
parents: 10701
diff changeset
   635
        d(CIP, "DESede/ECB/PKCS5Padding",       P11Cipher,      s("DESede"),
289
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents: 2
diff changeset
   636
                m(CKM_DES3_ECB));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        d(CIP, "AES/CBC/NoPadding",             P11Cipher,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                m(CKM_AES_CBC));
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 12685
diff changeset
   639
        d(CIP, "AES_128/CBC/NoPadding",          P11Cipher,
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 12685
diff changeset
   640
                s("2.16.840.1.101.3.4.1.2", "OID.2.16.840.1.101.3.4.1.2"),
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 12685
diff changeset
   641
                m(CKM_AES_CBC));
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 12685
diff changeset
   642
        d(CIP, "AES_192/CBC/NoPadding",          P11Cipher,
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 12685
diff changeset
   643
                s("2.16.840.1.101.3.4.1.22", "OID.2.16.840.1.101.3.4.1.22"),
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 12685
diff changeset
   644
                m(CKM_AES_CBC));
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 12685
diff changeset
   645
        d(CIP, "AES_256/CBC/NoPadding",          P11Cipher,
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 12685
diff changeset
   646
                s("2.16.840.1.101.3.4.1.42", "OID.2.16.840.1.101.3.4.1.42"),
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 12685
diff changeset
   647
                m(CKM_AES_CBC));
289
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents: 2
diff changeset
   648
        d(CIP, "AES/CBC/PKCS5Padding",          P11Cipher,
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents: 2
diff changeset
   649
                m(CKM_AES_CBC_PAD, CKM_AES_CBC));
11509
02fb543bc69d 7033170: Cipher.getMaxAllowedKeyLength(String) throws NoSuchAlgorithmException
valeriep
parents: 10701
diff changeset
   650
        d(CIP, "AES/ECB/NoPadding",             P11Cipher,
02fb543bc69d 7033170: Cipher.getMaxAllowedKeyLength(String) throws NoSuchAlgorithmException
valeriep
parents: 10701
diff changeset
   651
                m(CKM_AES_ECB));
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 12685
diff changeset
   652
        d(CIP, "AES_128/ECB/NoPadding",          P11Cipher,
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 12685
diff changeset
   653
                s("2.16.840.1.101.3.4.1.1", "OID.2.16.840.1.101.3.4.1.1"),
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 12685
diff changeset
   654
                m(CKM_AES_ECB));
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 12685
diff changeset
   655
        d(CIP, "AES_192/ECB/NoPadding",          P11Cipher,
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 12685
diff changeset
   656
                s("2.16.840.1.101.3.4.1.21", "OID.2.16.840.1.101.3.4.1.21"),
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 12685
diff changeset
   657
                m(CKM_AES_ECB));
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 12685
diff changeset
   658
        d(CIP, "AES_256/ECB/NoPadding",          P11Cipher,
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 12685
diff changeset
   659
                s("2.16.840.1.101.3.4.1.41", "OID.2.16.840.1.101.3.4.1.41"),
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 12685
diff changeset
   660
                m(CKM_AES_ECB));
11509
02fb543bc69d 7033170: Cipher.getMaxAllowedKeyLength(String) throws NoSuchAlgorithmException
valeriep
parents: 10701
diff changeset
   661
        d(CIP, "AES/ECB/PKCS5Padding",          P11Cipher,      s("AES"),
289
b1c8ad73578f 4898461: Support for ECB and CBC/PKCS5Padding
valeriep
parents: 2
diff changeset
   662
                m(CKM_AES_ECB));
8542
62c7b10ce177 6604496: Support for CKM_AES_CTR (counter mode)
valeriep
parents: 7179
diff changeset
   663
        d(CIP, "AES/CTR/NoPadding",             P11Cipher,
62c7b10ce177 6604496: Support for CKM_AES_CTR (counter mode)
valeriep
parents: 7179
diff changeset
   664
                m(CKM_AES_CTR));
11509
02fb543bc69d 7033170: Cipher.getMaxAllowedKeyLength(String) throws NoSuchAlgorithmException
valeriep
parents: 10701
diff changeset
   665
        d(CIP, "Blowfish/CBC/NoPadding",        P11Cipher,
02fb543bc69d 7033170: Cipher.getMaxAllowedKeyLength(String) throws NoSuchAlgorithmException
valeriep
parents: 10701
diff changeset
   666
                m(CKM_BLOWFISH_CBC));
02fb543bc69d 7033170: Cipher.getMaxAllowedKeyLength(String) throws NoSuchAlgorithmException
valeriep
parents: 10701
diff changeset
   667
        d(CIP, "Blowfish/CBC/PKCS5Padding",     P11Cipher,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                m(CKM_BLOWFISH_CBC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        // XXX RSA_X_509, RSA_OAEP not yet supported
8578
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 8542
diff changeset
   671
        d(CIP, "RSA/ECB/PKCS1Padding",          P11RSACipher,   s("RSA"),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                m(CKM_RSA_PKCS));
8578
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 8542
diff changeset
   673
        d(CIP, "RSA/ECB/NoPadding",             P11RSACipher,
f5d3509ad92b 6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents: 8542
diff changeset
   674
                m(CKM_RSA_X_509));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 12685
diff changeset
   676
        d(SIG, "RawDSA",        P11Signature,   s("NONEwithDSA"),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                m(CKM_DSA));
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 12685
diff changeset
   678
        d(SIG, "DSA",           P11Signature,
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 12685
diff changeset
   679
                s("SHA1withDSA", "1.3.14.3.2.13", "1.3.14.3.2.27",
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 12685
diff changeset
   680
                  "1.2.840.10040.4.3", "OID.1.2.840.10040.4.3"),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                m(CKM_DSA_SHA1, CKM_DSA));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        d(SIG, "NONEwithECDSA", P11Signature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                m(CKM_ECDSA));
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 12685
diff changeset
   684
        d(SIG, "SHA1withECDSA", P11Signature,
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 12685
diff changeset
   685
                s("ECDSA", "1.2.840.10045.4.1", "OID.1.2.840.10045.4.1"),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                m(CKM_ECDSA_SHA1, CKM_ECDSA));
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 12042
diff changeset
   687
        d(SIG, "SHA224withECDSA",       P11Signature,
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 12042
diff changeset
   688
                s("1.2.840.10045.4.3.1", "OID.1.2.840.10045.4.3.1"),
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 12042
diff changeset
   689
                m(CKM_ECDSA));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        d(SIG, "SHA256withECDSA",       P11Signature,
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 12042
diff changeset
   691
                s("1.2.840.10045.4.3.2", "OID.1.2.840.10045.4.3.2"),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                m(CKM_ECDSA));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        d(SIG, "SHA384withECDSA",       P11Signature,
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 12042
diff changeset
   694
                s("1.2.840.10045.4.3.3", "OID.1.2.840.10045.4.3.3"),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                m(CKM_ECDSA));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        d(SIG, "SHA512withECDSA",       P11Signature,
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 12042
diff changeset
   697
                s("1.2.840.10045.4.3.4", "OID.1.2.840.10045.4.3.4"),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                m(CKM_ECDSA));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        d(SIG, "MD2withRSA",    P11Signature,
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 12685
diff changeset
   700
                s("1.2.840.113549.1.1.2", "OID.1.2.840.113549.1.1.2"),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                m(CKM_MD2_RSA_PKCS, CKM_RSA_PKCS, CKM_RSA_X_509));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        d(SIG, "MD5withRSA",    P11Signature,
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 12685
diff changeset
   703
                s("1.2.840.113549.1.1.4", "OID.1.2.840.113549.1.1.4"),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                m(CKM_MD5_RSA_PKCS, CKM_RSA_PKCS, CKM_RSA_X_509));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        d(SIG, "SHA1withRSA",   P11Signature,
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 12685
diff changeset
   706
                s("1.2.840.113549.1.1.5", "OID.1.2.840.113549.1.1.5",
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 12685
diff changeset
   707
                  "1.3.14.3.2.29"),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                m(CKM_SHA1_RSA_PKCS, CKM_RSA_PKCS, CKM_RSA_X_509));
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 12042
diff changeset
   709
        d(SIG, "SHA224withRSA", P11Signature,
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 12042
diff changeset
   710
                s("1.2.840.113549.1.1.14", "OID.1.2.840.113549.1.1.14"),
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 12042
diff changeset
   711
                m(CKM_SHA224_RSA_PKCS, CKM_RSA_PKCS, CKM_RSA_X_509));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        d(SIG, "SHA256withRSA", P11Signature,
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 12042
diff changeset
   713
                s("1.2.840.113549.1.1.11", "OID.1.2.840.113549.1.1.11"),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                m(CKM_SHA256_RSA_PKCS, CKM_RSA_PKCS, CKM_RSA_X_509));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        d(SIG, "SHA384withRSA", P11Signature,
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 12042
diff changeset
   716
                s("1.2.840.113549.1.1.12", "OID.1.2.840.113549.1.1.12"),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                m(CKM_SHA384_RSA_PKCS, CKM_RSA_PKCS, CKM_RSA_X_509));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        d(SIG, "SHA512withRSA", P11Signature,
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 12042
diff changeset
   719
                s("1.2.840.113549.1.1.13", "OID.1.2.840.113549.1.1.13"),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                m(CKM_SHA512_RSA_PKCS, CKM_RSA_PKCS, CKM_RSA_X_509));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   722
        /*
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   723
         * 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
   724
         * PRF calculations.  As of 2010, there is no PKCS11-level
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   725
         * 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
   726
         * 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
   727
         * are updating JSSE to request different provider algorithms
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   728
         * (e.g. "SunTls12Prf"), and currently only SunJCE has these
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   729
         * TLS 1.2 algorithms.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   730
         *
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   731
         * If we reused the names such as "SunTlsPrf", the PKCS11
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   732
         * providers would need be updated to fail correctly when
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   733
         * presented with the wrong version number (via
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   734
         * Provider.Service.supportsParameters()), and we would also
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   735
         * need to add the appropriate supportsParamters() checks into
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   736
         * KeyGenerators (not currently there).
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   737
         *
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   738
         * In the future, if PKCS11 support is added, we will restructure
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   739
         * this.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   740
         */
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   741
        d(KG, "SunTlsRsaPremasterSecret",
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   742
                    "sun.security.pkcs11.P11TlsRsaPremasterSecretGenerator",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                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
   744
        d(KG, "SunTlsMasterSecret",
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   745
                    "sun.security.pkcs11.P11TlsMasterSecretGenerator",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                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
   747
                    CKM_SSL3_MASTER_KEY_DERIVE_DH,
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   748
                    CKM_TLS_MASTER_KEY_DERIVE_DH));
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   749
        d(KG, "SunTlsKeyMaterial",
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   750
                    "sun.security.pkcs11.P11TlsKeyMaterialGenerator",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                m(CKM_SSL3_KEY_AND_MAC_DERIVE, CKM_TLS_KEY_AND_MAC_DERIVE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        d(KG, "SunTlsPrf", "sun.security.pkcs11.P11TlsPrfGenerator",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                m(CKM_TLS_PRF, CKM_NSS_TLS_PRF_GENERAL));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
    // background thread that periodically checks for token insertion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
    // if no token is present. We need to do that in a separate thread because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    // the insertion check may block for quite a long time on some tokens.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
    private static class TokenPoller implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        private final SunPKCS11 provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        private volatile boolean enabled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        private TokenPoller(SunPKCS11 provider) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            this.provider = provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            enabled = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            int interval = provider.config.getInsertionCheckInterval();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            while (enabled) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                    Thread.sleep(interval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                if (enabled == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                    provider.initToken(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                    // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        void disable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            enabled = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
    // create the poller thread, if not already active
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
    private void createPoller() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        if (poller != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        TokenPoller poller = new TokenPoller(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        Thread t = new Thread(poller, "Poller " + getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        t.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        t.setPriority(Thread.MIN_PRIORITY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        t.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        this.poller = poller;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
    // destroy the poller thread, if active
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
    private void destroyPoller() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        if (poller != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            poller.disable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            poller = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
    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
   811
        /* 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
   812
           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
   813
           TOKEN_PRESENT are false, when it can't access the token.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        if (removable == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        }
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
   817
        */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        Token token = this.token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        return (token != null) && token.isValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    // destroy the token. Called if we detect that it has been removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
    synchronized void uninitToken(Token token) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        if (this.token != token) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            // mismatch, our token must already be destroyed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        destroyPoller();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        this.token = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        // unregister all algorithms
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        AccessController.doPrivileged(new PrivilegedAction<Object>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
            public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        createPoller();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
    // test if a token is present and initialize this provider for it if so.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    // does nothing if no token is found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
    // called from constructor and by poller
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
    private void initToken(CK_SLOT_INFO slotInfo) throws PKCS11Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        if (slotInfo == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            slotInfo = p11.C_GetSlotInfo(slotID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        }
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
   847
        if (removable && (slotInfo.flags & CKF_TOKEN_PRESENT) == 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
            createPoller();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        destroyPoller();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        boolean showInfo = config.getShowInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        if (showInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            System.out.println("Slot info for slot " + slotID + ":");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
            System.out.println(slotInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        final Token token = new Token(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        if (showInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            System.out.println
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                ("Token info for token in slot " + slotID + ":");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
            System.out.println(token.tokenInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        long[] supportedMechanisms = p11.C_GetMechanismList(slotID);
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   864
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   865
        // Create a map from the various Descriptors to the "most
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   866
        // preferred" mechanism that was defined during the
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   867
        // static initialization.  For example, DES/CBC/PKCS5Padding
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   868
        // 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
   869
        // the earliest entry.  When asked for "DES/CBC/PKCS5Padding", we
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   870
        // return a CKM_DES_CBC_PAD.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        final Map<Descriptor,Integer> supportedAlgs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
                                        new HashMap<Descriptor,Integer>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        for (int i = 0; i < supportedMechanisms.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
            long longMech = supportedMechanisms[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
            boolean isEnabled = config.isEnabled(longMech);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
            if (showInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                CK_MECHANISM_INFO mechInfo =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                        p11.C_GetMechanismInfo(slotID, longMech);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                System.out.println("Mechanism " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
                        Functions.getMechanismName(longMech) + ":");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                if (isEnabled == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                    System.out.println("DISABLED in configuration");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                System.out.println(mechInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
            if (isEnabled == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
            // we do not know of mechs with the upper 32 bits set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            if (longMech >>> 32 != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
            int mech = (int)longMech;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
            Integer integerMech = Integer.valueOf(mech);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
            List<Descriptor> ds = descriptors.get(integerMech);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
            if (ds == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            for (Descriptor d : ds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                Integer oldMech = supportedAlgs.get(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                if (oldMech == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                    supportedAlgs.put(d, integerMech);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                }
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   905
                // See if there is something "more preferred"
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   906
                // than what we currently have in the supportedAlgs
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 487
diff changeset
   907
                // map.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                int intOldMech = oldMech.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                for (int j = 0; j < d.mechanisms.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                    int nextMech = d.mechanisms[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                    if (mech == nextMech) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                        supportedAlgs.put(d, integerMech);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
                    } else if (intOldMech == nextMech) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
                }
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        // register algorithms in provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        AccessController.doPrivileged(new PrivilegedAction<Object>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
                for (Map.Entry<Descriptor,Integer> entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
                        : supportedAlgs.entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                    Descriptor d = entry.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                    int mechanism = entry.getValue().intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                    Service s = d.service(token, mechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
                    putService(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                if (((token.tokenInfo.flags & CKF_RNG) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
                        && config.isEnabled(PCKM_SECURERANDOM)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                        && !token.sessionManager.lowMaxSessions()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                    // do not register SecureRandom if the token does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
                    // not support many sessions. if we did, we might
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                    // run out of sessions in the middle of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                    // nextBytes() call where we cannot fail over.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
                    putService(new P11Service(token, SR, "PKCS11",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                        "sun.security.pkcs11.P11SecureRandom", null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                        PCKM_SECURERANDOM));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                if (config.isEnabled(PCKM_KEYSTORE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                    putService(new P11Service(token, KS, "PKCS11",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                        "sun.security.pkcs11.P11KeyStore",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
                        s("PKCS11-" + config.getName()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
                        PCKM_KEYSTORE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        this.token = token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
    private static final class P11Service extends Service {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        private final Token token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        private final long mechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        P11Service(Token token, String type, String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
                String className, String[] al, long mechanism) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
            super(token.provider, type, algorithm, className, toList(al), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
            this.token = token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
            this.mechanism = mechanism & 0xFFFFFFFFL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        private static List<String> toList(String[] aliases) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
            return (aliases == null) ? null : Arrays.asList(aliases);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   973
        public Object newInstance(Object param)
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   974
                throws NoSuchAlgorithmException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
            if (token.isValid() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
                throw new NoSuchAlgorithmException("Token has been removed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                return newInstance0(param);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
            } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
                throw new NoSuchAlgorithmException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        public Object newInstance0(Object param) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
                PKCS11Exception, NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
            String algorithm = getAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
            String type = getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
            if (type == MD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
                return new P11Digest(token, algorithm, mechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
            } else if (type == CIP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
                if (algorithm.startsWith("RSA")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
                    return new P11RSACipher(token, algorithm, mechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                    return new P11Cipher(token, algorithm, mechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            } else if (type == SIG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
                return new P11Signature(token, algorithm, mechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
            } else if (type == MAC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
                return new P11Mac(token, algorithm, mechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
            } else if (type == KPG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                return new P11KeyPairGenerator(token, algorithm, mechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
            } else if (type == KA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
                if (algorithm.equals("ECDH")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
                    return new P11ECDHKeyAgreement(token, algorithm, mechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
                    return new P11KeyAgreement(token, algorithm, mechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
            } else if (type == KF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
                return token.getKeyFactory(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
            } else if (type == SKF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
                return new P11SecretKeyFactory(token, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
            } else if (type == KG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
                // reference equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                if (algorithm == "SunTlsRsaPremasterSecret") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                    return new P11TlsRsaPremasterSecretGenerator(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
                        token, algorithm, mechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                } else if (algorithm == "SunTlsMasterSecret") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                    return new P11TlsMasterSecretGenerator(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
                        token, algorithm, mechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
                } else if (algorithm == "SunTlsKeyMaterial") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
                    return new P11TlsKeyMaterialGenerator(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
                        token, algorithm, mechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
                } else if (algorithm == "SunTlsPrf") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
                    return new P11TlsPrfGenerator(token, algorithm, mechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
                    return new P11KeyGenerator(token, algorithm, mechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
            } else if (type == SR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
                return token.getRandom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
            } else if (type == KS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
                return token.getKeyStore();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
            } else if (type == AGP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
                return new sun.security.ec.ECParameters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
                throw new NoSuchAlgorithmException("Unknown type: " + type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
            }
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 boolean supportsParameter(Object param) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
            if ((param == null) || (token.isValid() == false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
            if (param instanceof Key == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                throw new InvalidParameterException("Parameter must be a Key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
            String algorithm = getAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
            String type = getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
            Key key = (Key)param;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
            String keyAlgorithm = key.getAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
            // RSA signatures and cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
            if (((type == CIP) && algorithm.startsWith("RSA"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
                    || (type == SIG) && algorithm.endsWith("RSA")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                if (keyAlgorithm.equals("RSA") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                return isLocalKey(key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
                        || (key instanceof RSAPrivateKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
                        || (key instanceof RSAPublicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
            // EC
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
            if (((type == KA) && algorithm.equals("ECDH"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
                    || ((type == SIG) && algorithm.endsWith("ECDSA"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
                if (keyAlgorithm.equals("EC") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
                return isLocalKey(key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
                        || (key instanceof ECPrivateKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
                        || (key instanceof ECPublicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
            // DSA signatures
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
            if ((type == SIG) && algorithm.endsWith("DSA")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
                if (keyAlgorithm.equals("DSA") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
                return isLocalKey(key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
                        || (key instanceof DSAPrivateKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
                        || (key instanceof DSAPublicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
            // MACs and symmetric ciphers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
            if ((type == CIP) || (type == MAC)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
                // do not check algorithm name, mismatch is unlikely anyway
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
                return isLocalKey(key) || "RAW".equals(key.getFormat());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
            // DH key agreement
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
            if (type == KA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
                if (keyAlgorithm.equals("DH") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
                return isLocalKey(key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
                        || (key instanceof DHPrivateKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
                        || (key instanceof DHPublicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
            // should not reach here,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
            // unknown engine type or algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
            throw new AssertionError
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
                ("SunPKCS11 error: " + type + ", " + algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        private boolean isLocalKey(Key key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
            return (key instanceof P11Key) && (((P11Key)key).token == token);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
            return super.toString() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
                " (" + Functions.getMechanismName(mechanism) + ")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     * Log in to this provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
     * <p> If the token expects a PIN to be supplied by the caller,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
     * the <code>handler</code> implementation must support
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
     * a <code>PasswordCallback</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
     * <p> To determine if the token supports a protected authentication path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
     * the CK_TOKEN_INFO flag, CKF_PROTECTED_AUTHENTICATION_PATH, is consulted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
     * @param subject this parameter is ignored
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
     * @param handler the <code>CallbackHandler</code> used by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     *  this provider to communicate with the caller
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     * @exception LoginException if the login operation fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     * @exception SecurityException if the does not pass a security check for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     *  <code>SecurityPermission("authProvider.<i>name</i>")</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
     *  where <i>name</i> is the value returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
     *  this provider's <code>getName</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
    public void login(Subject subject, CallbackHandler handler)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
        throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        // security check
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
                debug.println("checking login permission");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
            sm.checkPermission(new SecurityPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
                        ("authProvider." + this.getName()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
        if (hasValidToken() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
            throw new LoginException("No token present");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
        // see if a login is required
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        if ((token.tokenInfo.flags & CKF_LOGIN_REQUIRED) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
                debug.println("login operation not required for token - " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
                                "ignoring login request");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
        // see if user already logged in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
            if (token.isLoggedInNow(null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
                // user already logged in
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
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
            // ignore - fall thru and attempt login
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        // get the pin if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        char[] pin = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        if ((token.tokenInfo.flags & CKF_PROTECTED_AUTHENTICATION_PATH) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
            // get password
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
            CallbackHandler myHandler = getCallbackHandler(handler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
            if (myHandler == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
                // XXX PolicyTool is dependent on this message text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
                throw new LoginException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
                        ("no password provided, and no callback handler " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
                        "available for retrieving password");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
            java.text.MessageFormat form = new java.text.MessageFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
                        (ResourcesMgr.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 7043
diff changeset
  1190
                        ("PKCS11.Token.providerName.Password."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
            Object[] source = { getName() };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
            PasswordCallback pcall = new PasswordCallback(form.format(source),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
                                                        false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
            Callback[] callbacks = { pcall };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
                myHandler.handle(callbacks);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
                LoginException le = new LoginException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
                        ("Unable to perform password callback");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
                le.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
                throw le;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
            pin = pcall.getPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
            pcall.clearPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
            if (pin == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
                    debug.println("caller passed NULL pin");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
        // perform token login
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
        Session session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
            session = token.getOpSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
            // pin is NULL if using CKF_PROTECTED_AUTHENTICATION_PATH
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
            p11.C_Login(session.id(), CKU_USER, pin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
                debug.println("login succeeded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
        } catch (PKCS11Exception pe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
            if (pe.getErrorCode() == CKR_USER_ALREADY_LOGGED_IN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
                // let this one go
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
                    debug.println("user already logged in");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
            } else if (pe.getErrorCode() == CKR_PIN_INCORRECT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
                FailedLoginException fle = new FailedLoginException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
                fle.initCause(pe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
                throw fle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
                LoginException le = new LoginException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
                le.initCause(pe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
                throw le;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
            token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
            if (pin != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
                Arrays.fill(pin, ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
        // we do not store the PIN in the subject for now
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
     * Log out from this provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
     * @exception LoginException if the logout operation fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
     * @exception SecurityException if the does not pass a security check for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     *  <code>SecurityPermission("authProvider.<i>name</i>")</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
     *  where <i>name</i> is the value returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
     *  this provider's <code>getName</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
    public void logout() throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
        // security check
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
            sm.checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
                (new SecurityPermission("authProvider." + this.getName()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
        if (hasValidToken() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
            // app may call logout for cleanup, allow
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
        if ((token.tokenInfo.flags & CKF_LOGIN_REQUIRED) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
                debug.println("logout operation not required for token - " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
                                "ignoring logout request");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
            if (token.isLoggedInNow(null) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
                    debug.println("user not logged in");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
            // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
        // perform token logout
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
        Session session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
            session = token.getOpSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
            p11.C_Logout(session.id());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
                debug.println("logout succeeded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
        } catch (PKCS11Exception pe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
            if (pe.getErrorCode() == CKR_USER_NOT_LOGGED_IN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
                // let this one go
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
                    debug.println("user not logged in");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
            LoginException le = new LoginException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
            le.initCause(pe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
            throw le;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
            token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
     * Set a <code>CallbackHandler</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
     * <p> The provider uses this handler if one is not passed to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
     * <code>login</code> method.  The provider also uses this handler
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
     * if it invokes <code>login</code> on behalf of callers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
     * In either case if a handler is not set via this method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
     * the provider queries the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
     * <i>auth.login.defaultCallbackHandler</i> security property
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
     * for the fully qualified class name of a default handler implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
     * If the security property is not set,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
     * the provider is assumed to have alternative means
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
     * for obtaining authentication information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
     * @param handler a <code>CallbackHandler</code> for obtaining
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
     *          authentication information, which may be <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
     * @exception SecurityException if the caller does not pass a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
     *  security check for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
     *  <code>SecurityPermission("authProvider.<i>name</i>")</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
     *  where <i>name</i> is the value returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
     *  this provider's <code>getName</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
    public void setCallbackHandler(CallbackHandler handler) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
        // security check
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
            sm.checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
                (new SecurityPermission("authProvider." + this.getName()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
        synchronized (LOCK_HANDLER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
            pHandler = handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
        }
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 CallbackHandler getCallbackHandler(CallbackHandler handler) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
        // get default handler if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
        if (handler != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
            return handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
            debug.println("getting provider callback handler");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
        synchronized (LOCK_HANDLER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
            // see if handler was set via setCallbackHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
            if (pHandler != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
                return pHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
                    debug.println("getting default callback handler");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
                CallbackHandler myHandler = AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
                    (new PrivilegedExceptionAction<CallbackHandler>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
                    public CallbackHandler run() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
                        String defaultHandler =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
                                java.security.Security.getProperty
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
                                ("auth.login.defaultCallbackHandler");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
                        if (defaultHandler == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
                            defaultHandler.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
                            // ok
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
                            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
                                debug.println("no default handler set");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
                            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
10701
2c8dde454979 7090499: missing rawtypes warnings in anonymous inner class
chegar
parents: 8578
diff changeset
  1398
                        Class<?> c = Class.forName
2c8dde454979 7090499: missing rawtypes warnings in anonymous inner class
chegar
parents: 8578
diff changeset
  1399
                                   (defaultHandler,
2c8dde454979 7090499: missing rawtypes warnings in anonymous inner class
chegar
parents: 8578
diff changeset
  1400
                                   true,
2c8dde454979 7090499: missing rawtypes warnings in anonymous inner class
chegar
parents: 8578
diff changeset
  1401
                                   Thread.currentThread().getContextClassLoader());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
                        return (CallbackHandler)c.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
                // save it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
                pHandler = myHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
                return myHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
            } catch (PrivilegedActionException pae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
                // ok
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
                    debug.println("Unable to load default callback handler");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
                    pae.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
    private Object writeReplace() throws ObjectStreamException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
        return new SunPKCS11Rep(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
     * Serialized representation of the SunPKCS11 provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
    private static class SunPKCS11Rep implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
        static final long serialVersionUID = -2896606995897745419L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
        private final String providerName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
        private final String configName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
        SunPKCS11Rep(SunPKCS11 provider) throws NotSerializableException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
            providerName = provider.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
            configName = provider.configName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
            if (Security.getProvider(providerName) != provider) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
                throw new NotSerializableException("Only SunPKCS11 providers "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
                    + "installed in java.security.Security can be serialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
        private Object readResolve() throws ObjectStreamException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
            SunPKCS11 p = (SunPKCS11)Security.getProvider(providerName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
            if ((p == null) || (p.configName.equals(configName) == false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
                throw new NotSerializableException("Could not find "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
                        + providerName + " in installed providers");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
            return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
}