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