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