jdk/src/share/classes/sun/security/pkcs11/Token.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
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-2006 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.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.ref.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.security.auth.login.LoginException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.jca.JCAUtil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.security.pkcs11.wrapper.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import static sun.security.pkcs11.wrapper.PKCS11Constants.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * PKCS#11 token.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
class Token implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    // need to be serializable to allow SecureRandom to be serialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static final long serialVersionUID = 2541527649100571747L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    // how often to check if the token is still present (in ms)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    // this is different from checking if a token has been inserted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    // that is done in SunPKCS11. Currently 50 ms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private final static long CHECK_INTERVAL = 50;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    final SunPKCS11 provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    final PKCS11 p11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    final Config config;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    final CK_TOKEN_INFO tokenInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    // session manager to pool sessions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    final SessionManager sessionManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    // template manager to customize the attributes used when creating objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private final TemplateManager templateManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    // flag indicating whether we need to explicitly cancel operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    // we started on the token. If false, we assume operations are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    // automatically cancelled once we start another one
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    final boolean explicitCancel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    // translation cache for secret keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    final KeyCache secretCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    // translation cache for asymmetric keys (public and private)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    final KeyCache privateCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    // cached instances of the various key factories, initialized on demand
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private volatile P11KeyFactory rsaFactory, dsaFactory, dhFactory, ecFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    // table which maps mechanisms to the corresponding cached
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    // MechanismInfo objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private final Map<Long, CK_MECHANISM_INFO> mechInfoMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    // single SecureRandomSpi instance we use per token
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    // initialized on demand (if supported)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private volatile P11SecureRandom secureRandom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    // single KeyStoreSpi instance we use per provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    // initialized on demand
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private volatile P11KeyStore keyStore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    // whether this token is a removable token
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private final boolean removable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    // for removable tokens: whether this token is valid or has been removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private volatile boolean valid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    // for removable tokens: time last checked for token presence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    private long lastPresentCheck;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    // unique token id, used for serialization only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private byte[] tokenId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    // flag indicating whether the token is write protected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private boolean writeProtected;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    // flag indicating whether we are logged in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private volatile boolean loggedIn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    // time we last checked login status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private long lastLoginCheck;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    // mutex for token-present-check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private final static Object CHECK_LOCK = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    // object for indicating unsupported mechanism in 'mechInfoMap'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private final static CK_MECHANISM_INFO INVALID_MECH =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        new CK_MECHANISM_INFO(0, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    Token(SunPKCS11 provider) throws PKCS11Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        this.provider = provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        this.removable = provider.removable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        this.valid = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        p11 = provider.p11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        config = provider.config;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        tokenInfo = p11.C_GetTokenInfo(provider.slotID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        writeProtected = (tokenInfo.flags & CKF_WRITE_PROTECTED) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        // create session manager and open a test session
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        SessionManager sessionManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            sessionManager = new SessionManager(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            Session s = sessionManager.getOpSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            sessionManager.releaseSession(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            if (writeProtected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            // token might not permit RW sessions even though
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            // CKF_WRITE_PROTECTED is not set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            writeProtected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            sessionManager = new SessionManager(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            Session s = sessionManager.getOpSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            sessionManager.releaseSession(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        this.sessionManager = sessionManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        secretCache = new KeyCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        privateCache = new KeyCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        templateManager = config.getTemplateManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        explicitCancel = config.getExplicitCancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        mechInfoMap = Collections.synchronizedMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            (new HashMap<Long, CK_MECHANISM_INFO>(10));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    boolean isWriteProtected() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        return writeProtected;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    // return whether we are logged in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    // uses cached result if current. session is optional and may be null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    boolean isLoggedIn(Session session) throws PKCS11Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        // volatile load first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        boolean loggedIn = this.loggedIn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        long time = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if (time - lastLoginCheck > CHECK_INTERVAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            loggedIn = isLoggedInNow(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            lastLoginCheck = time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        return loggedIn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    // return whether we are logged in now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    // does not use cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    boolean isLoggedInNow(Session session) throws PKCS11Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        boolean allocSession = (session == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            if (allocSession) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                session = getOpSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            CK_SESSION_INFO info = p11.C_GetSessionInfo(session.id());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            boolean loggedIn = (info.state == CKS_RO_USER_FUNCTIONS) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                                (info.state == CKS_RW_USER_FUNCTIONS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            this.loggedIn = loggedIn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            return loggedIn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            if (allocSession) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    // ensure that we are logged in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    // call provider.login() if not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    void ensureLoggedIn(Session session) throws PKCS11Exception, LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        if (isLoggedIn(session) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            provider.login(null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    // return whether this token object is valid (i.e. token not removed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    // returns value from last check, does not perform new check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    boolean isValid() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        if (removable == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        return valid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    void ensureValid() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        if (isValid() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            throw new ProviderException("Token has been removed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    // return whether a token is present (i.e. token not removed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    // returns cached value if current, otherwise performs new check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    boolean isPresent(Session session) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        if (removable == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        if (valid == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        long time = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        if ((time - lastPresentCheck) >= CHECK_INTERVAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            synchronized (CHECK_LOCK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                if ((time - lastPresentCheck) >= CHECK_INTERVAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                    boolean ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                        // check if token still present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                        CK_SLOT_INFO slotInfo =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                                provider.p11.C_GetSlotInfo(provider.slotID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                        if ((slotInfo.flags & CKF_TOKEN_PRESENT) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                            // if the token has been removed and re-inserted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                            // the token should return an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                            CK_SESSION_INFO sessInfo =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                                    provider.p11.C_GetSessionInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                                    (session.idInternal());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                            ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                        // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                    valid = ok;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                    lastPresentCheck = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                    if (ok == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                        destroy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        return valid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    void destroy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        valid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        provider.uninitToken(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    Session getObjSession() throws PKCS11Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        return sessionManager.getObjSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    Session getOpSession() throws PKCS11Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        return sessionManager.getOpSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    Session releaseSession(Session session) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        return sessionManager.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    Session killSession(Session session) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        return sessionManager.killSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    CK_ATTRIBUTE[] getAttributes(String op, long type, long alg,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            CK_ATTRIBUTE[] attrs) throws PKCS11Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        CK_ATTRIBUTE[] newAttrs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                    templateManager.getAttributes(op, type, alg, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        for (CK_ATTRIBUTE attr : newAttrs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            if (attr.type == CKA_TOKEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                if (attr.getBoolean()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                        ensureLoggedIn(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                    } catch (LoginException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                        throw new ProviderException("Login failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                // break once we have found a CKA_TOKEN attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        return newAttrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    P11KeyFactory getKeyFactory(String algorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        P11KeyFactory f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        if (algorithm.equals("RSA")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            f = rsaFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            if (f == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                f = new P11RSAKeyFactory(this, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                rsaFactory = f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        } else if (algorithm.equals("DSA")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            f = dsaFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            if (f == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                f = new P11DSAKeyFactory(this, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                dsaFactory = f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        } else if (algorithm.equals("DH")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            f = dhFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            if (f == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                f = new P11DHKeyFactory(this, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                dhFactory = f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        } else if (algorithm.equals("EC")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            f = ecFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            if (f == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                f = new P11ECKeyFactory(this, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                ecFactory = f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            throw new ProviderException("Unknown algorithm " + algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        return f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    P11SecureRandom getRandom() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        if (secureRandom == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            secureRandom = new P11SecureRandom(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        return secureRandom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    P11KeyStore getKeyStore() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        if (keyStore == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            keyStore = new P11KeyStore(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        return keyStore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    CK_MECHANISM_INFO getMechanismInfo(long mechanism) throws PKCS11Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        CK_MECHANISM_INFO result = mechInfoMap.get(mechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        if (result == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                result = p11.C_GetMechanismInfo(provider.slotID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                                                mechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                mechInfoMap.put(mechanism, result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                if (e.getErrorCode() != PKCS11Constants.CKR_MECHANISM_INVALID) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                    throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                    mechInfoMap.put(mechanism, INVALID_MECH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        } else if (result == INVALID_MECH) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            result = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    private synchronized byte[] getTokenId() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        if (tokenId == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            SecureRandom random = JCAUtil.getSecureRandom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            tokenId = new byte[20];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            random.nextBytes(tokenId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            serializedTokens.add(new WeakReference<Token>(this));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        return tokenId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    // list of all tokens that have been serialized within this VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    // NOTE that elements are never removed from this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    // the assumption is that the number of tokens that are serialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    // is relatively small
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    private static final List<Reference<Token>> serializedTokens =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        new ArrayList<Reference<Token>>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    private Object writeReplace() throws ObjectStreamException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        if (isValid() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            throw new NotSerializableException("Token has been removed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        return new TokenRep(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    // serialized representation of a token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    // tokens can only be de-serialized within the same VM invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    // and if the token has not been removed in the meantime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    private static class TokenRep implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        private static final long serialVersionUID = 3503721168218219807L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        private final byte[] tokenId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        TokenRep(Token token) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            tokenId = token.getTokenId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        private Object readResolve() throws ObjectStreamException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            for (Reference<Token> tokenRef : serializedTokens) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                Token token = tokenRef.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                if ((token != null) && token.isValid()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                    if (Arrays.equals(token.getTokenId(), tokenId)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                        return token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            throw new NotSerializableException("Could not find token");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
}