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