jdk/src/share/classes/sun/security/pkcs11/P11Key.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8773 d8a2b71afa0c
child 10336 0bb1999251f8
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8773
diff changeset
     2
 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5291
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5291
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5291
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5291
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5291
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.pkcs11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
    29
import java.lang.ref.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.interfaces.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.crypto.interfaces.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.crypto.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.security.rsa.RSAPublicKeyImpl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import sun.security.internal.interfaces.TlsMasterSecret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import sun.security.pkcs11.wrapper.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import static sun.security.pkcs11.wrapper.PKCS11Constants.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
4809
c00eed67999d 6763530: Cannot decode PublicKey (Proider SunPKCS11, curve prime256v1)
vinnie
parents: 2180
diff changeset
    48
import sun.security.util.DerValue;
c00eed67999d 6763530: Cannot decode PublicKey (Proider SunPKCS11, curve prime256v1)
vinnie
parents: 2180
diff changeset
    49
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * Key implementation classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * In PKCS#11, the components of private and secret keys may or may not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * be accessible. If they are, we use the algorithm specific key classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * (e.g. DSAPrivateKey) for compatibility with existing applications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * If the components are not accessible, we use a generic class that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * only implements PrivateKey (or SecretKey). Whether the components of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * key are extractable is automatically determined when the key object is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
abstract class P11Key implements Key {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private final static String PUBLIC = "public";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private final static String PRIVATE = "private";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private final static String SECRET = "secret";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    // type of key, one of (PUBLIC, PRIVATE, SECRET)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    final String type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    // token instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    final Token token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    // algorithm name, returned by getAlgorithm(), etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    final String algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    // key id
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    final long keyID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    // effective key length of the key, e.g. 56 for a DES key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    final int keyLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    // flags indicating whether the key is a token object, sensitive, extractable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    final boolean tokenObject, sensitive, extractable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
5291
d6df082f6524 6918573: sun.security.pkcs11.P11RSACipher.finalize() is a scalability blocker
valeriep
parents: 4809
diff changeset
    88
    // phantom reference notification clean up for session keys
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
    89
    private final SessionKeyRef sessionKeyRef;
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
    90
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    P11Key(String type, Session session, long keyID, String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            int keyLength, CK_ATTRIBUTE[] attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        this.token = session.token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        this.keyID = keyID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        this.algorithm = algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        this.keyLength = keyLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        boolean tokenObject = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        boolean sensitive = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        boolean extractable = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        int n = (attributes == null) ? 0 : attributes.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            CK_ATTRIBUTE attr = attributes[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            if (attr.type == CKA_TOKEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                tokenObject = attr.getBoolean();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            } else if (attr.type == CKA_SENSITIVE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                sensitive = attr.getBoolean();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            } else if (attr.type == CKA_EXTRACTABLE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                extractable = attr.getBoolean();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        this.tokenObject = tokenObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        this.sensitive = sensitive;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        this.extractable = extractable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (tokenObject == false) {
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   116
            sessionKeyRef = new SessionKeyRef(this, keyID, session);
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   117
        } else {
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   118
            sessionKeyRef = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    // see JCA spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public final String getAlgorithm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        return algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    // see JCA spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public final byte[] getEncoded() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        byte[] b = getEncodedInternal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        return (b == null) ? null : (byte[])b.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    abstract byte[] getEncodedInternal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (this == obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        // equals() should never throw exceptions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        if (token.isValid() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (obj instanceof Key == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        String thisFormat = getFormat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (thisFormat == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            // no encoding, key only equal to itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            // XXX getEncoded() for unextractable keys will change that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        Key other = (Key)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        if (thisFormat.equals(other.getFormat()) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        byte[] thisEnc = this.getEncodedInternal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        byte[] otherEnc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (obj instanceof P11Key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            otherEnc = ((P11Key)other).getEncodedInternal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            otherEnc = other.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        return Arrays.equals(thisEnc, otherEnc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        // hashCode() should never throw exceptions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (token.isValid() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        byte[] b1 = getEncodedInternal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        if (b1 == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        int r = b1.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        for (int i = 0; i < b1.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            r += (b1[i] & 0xff) * 37;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    protected Object writeReplace() throws ObjectStreamException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        KeyRep.Type type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        String format = getFormat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        if (isPrivate() && "PKCS#8".equals(format)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            type = KeyRep.Type.PRIVATE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        } else if (isPublic() && "X.509".equals(format)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            type = KeyRep.Type.PUBLIC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        } else if (isSecret() && "RAW".equals(format)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            type = KeyRep.Type.SECRET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            // XXX short term serialization for unextractable keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            throw new NotSerializableException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                ("Cannot serialize sensitive and unextractable keys");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        return new KeyRep(type, getAlgorithm(), format, getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        String s1 = token.provider.getName() + " " + algorithm + " " + type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                + " key, " + keyLength + " bits";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        s1 += " (id " + keyID + ", "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                + (tokenObject ? "token" : "session") + " object";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        if (isPublic()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            s1 += ")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            s1 += ", " + (sensitive ? "" : "not ") + "sensitive";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            s1 += ", " + (extractable ? "" : "un") + "extractable)";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        return s1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    int keyLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        return keyLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    boolean isPublic() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        return type == PUBLIC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    boolean isPrivate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        return type == PRIVATE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    boolean isSecret() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        return type == SECRET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    void fetchAttributes(CK_ATTRIBUTE[] attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        Session tempSession = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            tempSession = token.getOpSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            token.p11.C_GetAttributeValue(tempSession.id(), keyID, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            throw new ProviderException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            token.releaseSession(tempSession);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    private final static CK_ATTRIBUTE[] A0 = new CK_ATTRIBUTE[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    private static CK_ATTRIBUTE[] getAttributes(Session session, long keyID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            CK_ATTRIBUTE[] knownAttributes, CK_ATTRIBUTE[] desiredAttributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        if (knownAttributes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            knownAttributes = A0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        for (int i = 0; i < desiredAttributes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            // For each desired attribute, check to see if we have the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            // available already. If everything is here, we save a native call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            CK_ATTRIBUTE attr = desiredAttributes[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            for (CK_ATTRIBUTE known : knownAttributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                if ((attr.type == known.type) && (known.pValue != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                    attr.pValue = known.pValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    break; // break inner for loop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            if (attr.pValue == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                // nothing found, need to call C_GetAttributeValue()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                for (int j = 0; j < i; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    // clear values copied from knownAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                    desiredAttributes[j].pValue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                    session.token.p11.C_GetAttributeValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                            (session.id(), keyID, desiredAttributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                    throw new ProviderException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                break; // break loop, goto return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        return desiredAttributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    static SecretKey secretKey(Session session, long keyID, String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            int keyLength, CK_ATTRIBUTE[] attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        attributes = getAttributes(session, keyID, attributes, new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            new CK_ATTRIBUTE(CKA_TOKEN),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            new CK_ATTRIBUTE(CKA_SENSITIVE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            new CK_ATTRIBUTE(CKA_EXTRACTABLE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        return new P11SecretKey(session, keyID, algorithm, keyLength, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    static SecretKey masterSecretKey(Session session, long keyID, String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            int keyLength, CK_ATTRIBUTE[] attributes, int major, int minor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        attributes = getAttributes(session, keyID, attributes, new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            new CK_ATTRIBUTE(CKA_TOKEN),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            new CK_ATTRIBUTE(CKA_SENSITIVE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            new CK_ATTRIBUTE(CKA_EXTRACTABLE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        return new P11TlsMasterSecretKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                (session, keyID, algorithm, keyLength, attributes, major, minor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    // we assume that all components of public keys are always accessible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    static PublicKey publicKey(Session session, long keyID, String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            int keyLength, CK_ATTRIBUTE[] attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        if (algorithm.equals("RSA")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            return new P11RSAPublicKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                (session, keyID, algorithm, keyLength, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        } else if (algorithm.equals("DSA")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            return new P11DSAPublicKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                (session, keyID, algorithm, keyLength, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        } else if (algorithm.equals("DH")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            return new P11DHPublicKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                (session, keyID, algorithm, keyLength, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        } else if (algorithm.equals("EC")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            return new P11ECPublicKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                (session, keyID, algorithm, keyLength, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            throw new ProviderException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                ("Unknown public key algorithm " + algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    static PrivateKey privateKey(Session session, long keyID, String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            int keyLength, CK_ATTRIBUTE[] attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        attributes = getAttributes(session, keyID, attributes, new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            new CK_ATTRIBUTE(CKA_TOKEN),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            new CK_ATTRIBUTE(CKA_SENSITIVE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            new CK_ATTRIBUTE(CKA_EXTRACTABLE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        if (attributes[1].getBoolean() || (attributes[2].getBoolean() == false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            return new P11PrivateKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                (session, keyID, algorithm, keyLength, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            if (algorithm.equals("RSA")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                // XXX better test for RSA CRT keys (single getAttributes() call)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                // we need to determine whether this is a CRT key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                // see if we can obtain the public exponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                // this should also be readable for sensitive/extractable keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                CK_ATTRIBUTE[] attrs2 = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    new CK_ATTRIBUTE(CKA_PUBLIC_EXPONENT),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                boolean crtKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                    session.token.p11.C_GetAttributeValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                        (session.id(), keyID, attrs2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                    crtKey = (attrs2[0].pValue instanceof byte[]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                    // ignore, assume not available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    crtKey = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                if (crtKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                    return new P11RSAPrivateKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                            (session, keyID, algorithm, keyLength, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    return new P11RSAPrivateNonCRTKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                            (session, keyID, algorithm, keyLength, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            } else if (algorithm.equals("DSA")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                return new P11DSAPrivateKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                        (session, keyID, algorithm, keyLength, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            } else if (algorithm.equals("DH")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                return new P11DHPrivateKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                        (session, keyID, algorithm, keyLength, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            } else if (algorithm.equals("EC")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                return new P11ECPrivateKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                        (session, keyID, algorithm, keyLength, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                throw new ProviderException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                        ("Unknown private key algorithm " + algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    // class for sensitive and unextractable private keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    private static final class P11PrivateKey extends P11Key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                                                implements PrivateKey {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        P11PrivateKey(Session session, long keyID, String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                int keyLength, CK_ATTRIBUTE[] attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            super(PRIVATE, session, keyID, algorithm, keyLength, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        // XXX temporary encoding for serialization purposes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        public String getFormat() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        byte[] getEncodedInternal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    private static class P11SecretKey extends P11Key implements SecretKey {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        private volatile byte[] encoded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        P11SecretKey(Session session, long keyID, String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                int keyLength, CK_ATTRIBUTE[] attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            super(SECRET, session, keyID, algorithm, keyLength, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        public String getFormat() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            if (sensitive || (extractable == false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                return "RAW";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        byte[] getEncodedInternal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            if (getFormat() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            byte[] b = encoded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            if (b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                    b = encoded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                    if (b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                        Session tempSession = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                            tempSession = token.getOpSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                            CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                                new CK_ATTRIBUTE(CKA_VALUE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                            token.p11.C_GetAttributeValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                                (tempSession.id(), keyID, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                            b = attributes[0].getByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                            throw new ProviderException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                            token.releaseSession(tempSession);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                        encoded = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            return b;
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
    private static class P11TlsMasterSecretKey extends P11SecretKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            implements TlsMasterSecret {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        private final int majorVersion, minorVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        P11TlsMasterSecretKey(Session session, long keyID, String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                int keyLength, CK_ATTRIBUTE[] attributes, int major, int minor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            super(session, keyID, algorithm, keyLength, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            this.majorVersion = major;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            this.minorVersion = minor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        public int getMajorVersion() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            return majorVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        public int getMinorVersion() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            return minorVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    // RSA CRT private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    private static final class P11RSAPrivateKey extends P11Key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                implements RSAPrivateCrtKey {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        private BigInteger n, e, d, p, q, pe, qe, coeff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        private byte[] encoded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        P11RSAPrivateKey(Session session, long keyID, String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                int keyLength, CK_ATTRIBUTE[] attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            super(PRIVATE, session, keyID, algorithm, keyLength, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        private synchronized void fetchValues() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            if (n != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                new CK_ATTRIBUTE(CKA_MODULUS),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                new CK_ATTRIBUTE(CKA_PUBLIC_EXPONENT),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                new CK_ATTRIBUTE(CKA_PRIVATE_EXPONENT),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                new CK_ATTRIBUTE(CKA_PRIME_1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                new CK_ATTRIBUTE(CKA_PRIME_2),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                new CK_ATTRIBUTE(CKA_EXPONENT_1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                new CK_ATTRIBUTE(CKA_EXPONENT_2),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                new CK_ATTRIBUTE(CKA_COEFFICIENT),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            fetchAttributes(attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            n = attributes[0].getBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            e = attributes[1].getBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            d = attributes[2].getBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            p = attributes[3].getBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            q = attributes[4].getBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            pe = attributes[5].getBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            qe = attributes[6].getBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            coeff = attributes[7].getBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        public String getFormat() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            return "PKCS#8";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        synchronized byte[] getEncodedInternal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            if (encoded == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                fetchValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                    // XXX make constructor in SunRsaSign provider public
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                    // and call it directly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                    KeyFactory factory = KeyFactory.getInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                        ("RSA", P11Util.getSunRsaSignProvider());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                    Key newKey = factory.translateKey(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                    encoded = newKey.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                    throw new ProviderException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            return encoded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        public BigInteger getModulus() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            fetchValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        public BigInteger getPublicExponent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            fetchValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        public BigInteger getPrivateExponent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            fetchValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            return d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        public BigInteger getPrimeP() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            fetchValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        public BigInteger getPrimeQ() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            fetchValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            return q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        public BigInteger getPrimeExponentP() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            fetchValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            return pe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        public BigInteger getPrimeExponentQ() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            fetchValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            return qe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        public BigInteger getCrtCoefficient() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            fetchValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            return coeff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            fetchValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            StringBuilder sb = new StringBuilder(super.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            sb.append("\n  modulus:          ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            sb.append(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            sb.append("\n  public exponent:  ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            sb.append(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            sb.append("\n  private exponent: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            sb.append(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            sb.append("\n  prime p:          ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            sb.append(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            sb.append("\n  prime q:          ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            sb.append(q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            sb.append("\n  prime exponent p: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            sb.append(pe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            sb.append("\n  prime exponent q: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            sb.append(qe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            sb.append("\n  crt coefficient:  ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            sb.append(coeff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    // RSA non-CRT private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    private static final class P11RSAPrivateNonCRTKey extends P11Key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                implements RSAPrivateKey {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        private BigInteger n, d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        private byte[] encoded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        P11RSAPrivateNonCRTKey(Session session, long keyID, String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                int keyLength, CK_ATTRIBUTE[] attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            super(PRIVATE, session, keyID, algorithm, keyLength, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        private synchronized void fetchValues() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            if (n != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                new CK_ATTRIBUTE(CKA_MODULUS),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                new CK_ATTRIBUTE(CKA_PRIVATE_EXPONENT),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            fetchAttributes(attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            n = attributes[0].getBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            d = attributes[1].getBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        public String getFormat() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            return "PKCS#8";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        synchronized byte[] getEncodedInternal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            if (encoded == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                fetchValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                    // XXX make constructor in SunRsaSign provider public
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                    // and call it directly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                    KeyFactory factory = KeyFactory.getInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                        ("RSA", P11Util.getSunRsaSignProvider());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                    Key newKey = factory.translateKey(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                    encoded = newKey.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                    throw new ProviderException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            return encoded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        public BigInteger getModulus() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            fetchValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        public BigInteger getPrivateExponent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            fetchValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            return d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            fetchValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            StringBuilder sb = new StringBuilder(super.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            sb.append("\n  modulus:          ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            sb.append(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            sb.append("\n  private exponent: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            sb.append(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    private static final class P11RSAPublicKey extends P11Key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                                                implements RSAPublicKey {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        private BigInteger n, e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        private byte[] encoded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        P11RSAPublicKey(Session session, long keyID, String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                int keyLength, CK_ATTRIBUTE[] attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            super(PUBLIC, session, keyID, algorithm, keyLength, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        private synchronized void fetchValues() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            if (n != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                new CK_ATTRIBUTE(CKA_MODULUS),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                new CK_ATTRIBUTE(CKA_PUBLIC_EXPONENT),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            fetchAttributes(attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            n = attributes[0].getBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            e = attributes[1].getBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        public String getFormat() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            return "X.509";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        synchronized byte[] getEncodedInternal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            if (encoded == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                fetchValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                    encoded = new RSAPublicKeyImpl(n, e).getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                } catch (InvalidKeyException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                    throw new ProviderException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            return encoded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        public BigInteger getModulus() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            fetchValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        public BigInteger getPublicExponent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            fetchValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            fetchValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            return super.toString() +  "\n  modulus: " + n
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                + "\n  public exponent: " + e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    private static final class P11DSAPublicKey extends P11Key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                                                implements DSAPublicKey {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        private BigInteger y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        private DSAParams params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        private byte[] encoded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        P11DSAPublicKey(Session session, long keyID, String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                int keyLength, CK_ATTRIBUTE[] attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            super(PUBLIC, session, keyID, algorithm, keyLength, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        private synchronized void fetchValues() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            if (y != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                new CK_ATTRIBUTE(CKA_VALUE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                new CK_ATTRIBUTE(CKA_PRIME),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                new CK_ATTRIBUTE(CKA_SUBPRIME),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                new CK_ATTRIBUTE(CKA_BASE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            fetchAttributes(attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            y = attributes[0].getBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            params = new DSAParameterSpec(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                attributes[1].getBigInteger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                attributes[2].getBigInteger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                attributes[3].getBigInteger()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        public String getFormat() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            return "X.509";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        synchronized byte[] getEncodedInternal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            if (encoded == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                fetchValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                    Key key = new sun.security.provider.DSAPublicKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                            (y, params.getP(), params.getQ(), params.getG());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                    encoded = key.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                } catch (InvalidKeyException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                    throw new ProviderException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            return encoded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        public BigInteger getY() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            fetchValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            return y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        public DSAParams getParams() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            fetchValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            return params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            fetchValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            return super.toString() +  "\n  y: " + y + "\n  p: " + params.getP()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                + "\n  q: " + params.getQ() + "\n  g: " + params.getG();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
    private static final class P11DSAPrivateKey extends P11Key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                                                implements DSAPrivateKey {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        private BigInteger x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        private DSAParams params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        private byte[] encoded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        P11DSAPrivateKey(Session session, long keyID, String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                int keyLength, CK_ATTRIBUTE[] attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            super(PRIVATE, session, keyID, algorithm, keyLength, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        private synchronized void fetchValues() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
            if (x != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                new CK_ATTRIBUTE(CKA_VALUE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                new CK_ATTRIBUTE(CKA_PRIME),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                new CK_ATTRIBUTE(CKA_SUBPRIME),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                new CK_ATTRIBUTE(CKA_BASE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            fetchAttributes(attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            x = attributes[0].getBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            params = new DSAParameterSpec(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                attributes[1].getBigInteger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                attributes[2].getBigInteger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                attributes[3].getBigInteger()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        public String getFormat() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            return "PKCS#8";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        synchronized byte[] getEncodedInternal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            if (encoded == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                fetchValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                    Key key = new sun.security.provider.DSAPrivateKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                            (x, params.getP(), params.getQ(), params.getG());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                    encoded = key.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                } catch (InvalidKeyException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                    throw new ProviderException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            return encoded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        public BigInteger getX() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            fetchValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        public DSAParams getParams() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            fetchValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            return params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            fetchValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            return super.toString() +  "\n  x: " + x + "\n  p: " + params.getP()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                + "\n  q: " + params.getQ() + "\n  g: " + params.getG();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
    private static final class P11DHPrivateKey extends P11Key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                                                implements DHPrivateKey {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        private BigInteger x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        private DHParameterSpec params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        private byte[] encoded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        P11DHPrivateKey(Session session, long keyID, String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                int keyLength, CK_ATTRIBUTE[] attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            super(PRIVATE, session, keyID, algorithm, keyLength, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        private synchronized void fetchValues() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            if (x != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
            CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                new CK_ATTRIBUTE(CKA_VALUE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                new CK_ATTRIBUTE(CKA_PRIME),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                new CK_ATTRIBUTE(CKA_BASE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
            fetchAttributes(attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            x = attributes[0].getBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            params = new DHParameterSpec(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                attributes[1].getBigInteger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                attributes[2].getBigInteger()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
            );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        public String getFormat() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            return "PKCS#8";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        synchronized byte[] getEncodedInternal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            if (encoded == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                fetchValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                    DHPrivateKeySpec spec = new DHPrivateKeySpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                        (x, params.getP(), params.getG());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                    KeyFactory kf = KeyFactory.getInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                        ("DH", P11Util.getSunJceProvider());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                    Key key = kf.generatePrivate(spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                    encoded = key.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                    throw new ProviderException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
            return encoded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        public BigInteger getX() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            fetchValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
            return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        public DHParameterSpec getParams() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
            fetchValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
            return params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
            fetchValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
            return super.toString() +  "\n  x: " + x + "\n  p: " + params.getP()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                + "\n  g: " + params.getG();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
    private static final class P11DHPublicKey extends P11Key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                                                implements DHPublicKey {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        private BigInteger y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        private DHParameterSpec params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        private byte[] encoded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        P11DHPublicKey(Session session, long keyID, String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                int keyLength, CK_ATTRIBUTE[] attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
            super(PUBLIC, session, keyID, algorithm, keyLength, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        private synchronized void fetchValues() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
            token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
            if (y != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
            CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                new CK_ATTRIBUTE(CKA_VALUE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                new CK_ATTRIBUTE(CKA_PRIME),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                new CK_ATTRIBUTE(CKA_BASE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            fetchAttributes(attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
            y = attributes[0].getBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
            params = new DHParameterSpec(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                attributes[1].getBigInteger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                attributes[2].getBigInteger()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        public String getFormat() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
            token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
            return "X.509";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        synchronized byte[] getEncodedInternal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
            token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
            if (encoded == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                fetchValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                    DHPublicKeySpec spec = new DHPublicKeySpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                        (y, params.getP(), params.getG());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                    KeyFactory kf = KeyFactory.getInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                        ("DH", P11Util.getSunJceProvider());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                    Key key = kf.generatePublic(spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                    encoded = key.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                    throw new ProviderException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
            return encoded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        public BigInteger getY() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
            fetchValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
            return y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        public DHParameterSpec getParams() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
            fetchValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
            return params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            fetchValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            return super.toString() +  "\n  y: " + y + "\n  p: " + params.getP()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
                + "\n  g: " + params.getG();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
    private static final class P11ECPrivateKey extends P11Key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
                                                implements ECPrivateKey {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        private BigInteger s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        private ECParameterSpec params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        private byte[] encoded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        P11ECPrivateKey(Session session, long keyID, String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                int keyLength, CK_ATTRIBUTE[] attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
            super(PRIVATE, session, keyID, algorithm, keyLength, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        private synchronized void fetchValues() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
            token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            if (s != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
            CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                new CK_ATTRIBUTE(CKA_VALUE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
                new CK_ATTRIBUTE(CKA_EC_PARAMS, params),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
            fetchAttributes(attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
            s = attributes[0].getBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                params = P11ECKeyFactory.decodeParameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                            (attributes[1].getByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
                throw new RuntimeException("Could not parse key values", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        public String getFormat() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
            token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
            return "PKCS#8";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        synchronized byte[] getEncodedInternal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
            token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
            if (encoded == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
                fetchValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
                    Key key = new sun.security.ec.ECPrivateKeyImpl(s, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
                    encoded = key.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                } catch (InvalidKeyException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
                    throw new ProviderException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
            return encoded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        public BigInteger getS() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
            fetchValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
            return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        public ECParameterSpec getParams() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
            fetchValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
            return params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
            fetchValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        return super.toString()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
            + "\n  private value:  " + s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
            + "\n  parameters: " + params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
    private static final class P11ECPublicKey extends P11Key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
                                                implements ECPublicKey {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        private ECPoint w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        private ECParameterSpec params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        private byte[] encoded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        P11ECPublicKey(Session session, long keyID, String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
                int keyLength, CK_ATTRIBUTE[] attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
            super(PUBLIC, session, keyID, algorithm, keyLength, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
        private synchronized void fetchValues() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
            token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
            if (w != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
                new CK_ATTRIBUTE(CKA_EC_POINT),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
                new CK_ATTRIBUTE(CKA_EC_PARAMS),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
            fetchAttributes(attributes);
8773
d8a2b71afa0c 7016078: javax.net.ssl.SSLException: Received fatal alert: internal_error starting JDK 7 b126
vinnie
parents: 5506
diff changeset
  1002
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
                params = P11ECKeyFactory.decodeParameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
                            (attributes[1].getByteArray());
8773
d8a2b71afa0c 7016078: javax.net.ssl.SSLException: Received fatal alert: internal_error starting JDK 7 b126
vinnie
parents: 5506
diff changeset
  1006
d8a2b71afa0c 7016078: javax.net.ssl.SSLException: Received fatal alert: internal_error starting JDK 7 b126
vinnie
parents: 5506
diff changeset
  1007
                /*
d8a2b71afa0c 7016078: javax.net.ssl.SSLException: Received fatal alert: internal_error starting JDK 7 b126
vinnie
parents: 5506
diff changeset
  1008
                 * An uncompressed EC point may be in either of two formats.
d8a2b71afa0c 7016078: javax.net.ssl.SSLException: Received fatal alert: internal_error starting JDK 7 b126
vinnie
parents: 5506
diff changeset
  1009
                 * First try the OCTET STRING encoding:
d8a2b71afa0c 7016078: javax.net.ssl.SSLException: Received fatal alert: internal_error starting JDK 7 b126
vinnie
parents: 5506
diff changeset
  1010
                 *   04 <length> 04 <X-coordinate> <Y-coordinate>
d8a2b71afa0c 7016078: javax.net.ssl.SSLException: Received fatal alert: internal_error starting JDK 7 b126
vinnie
parents: 5506
diff changeset
  1011
                 *
d8a2b71afa0c 7016078: javax.net.ssl.SSLException: Received fatal alert: internal_error starting JDK 7 b126
vinnie
parents: 5506
diff changeset
  1012
                 * Otherwise try the raw encoding:
d8a2b71afa0c 7016078: javax.net.ssl.SSLException: Received fatal alert: internal_error starting JDK 7 b126
vinnie
parents: 5506
diff changeset
  1013
                 *   04 <X-coordinate> <Y-coordinate>
d8a2b71afa0c 7016078: javax.net.ssl.SSLException: Received fatal alert: internal_error starting JDK 7 b126
vinnie
parents: 5506
diff changeset
  1014
                 */
d8a2b71afa0c 7016078: javax.net.ssl.SSLException: Received fatal alert: internal_error starting JDK 7 b126
vinnie
parents: 5506
diff changeset
  1015
                byte[] ecKey = attributes[0].getByteArray();
4809
c00eed67999d 6763530: Cannot decode PublicKey (Proider SunPKCS11, curve prime256v1)
vinnie
parents: 2180
diff changeset
  1016
8773
d8a2b71afa0c 7016078: javax.net.ssl.SSLException: Received fatal alert: internal_error starting JDK 7 b126
vinnie
parents: 5506
diff changeset
  1017
                try {
d8a2b71afa0c 7016078: javax.net.ssl.SSLException: Received fatal alert: internal_error starting JDK 7 b126
vinnie
parents: 5506
diff changeset
  1018
                    DerValue wECPoint = new DerValue(ecKey);
d8a2b71afa0c 7016078: javax.net.ssl.SSLException: Received fatal alert: internal_error starting JDK 7 b126
vinnie
parents: 5506
diff changeset
  1019
                    if (wECPoint.getTag() != DerValue.tag_OctetString)
d8a2b71afa0c 7016078: javax.net.ssl.SSLException: Received fatal alert: internal_error starting JDK 7 b126
vinnie
parents: 5506
diff changeset
  1020
                        throw new IOException("Unexpected tag: " +
d8a2b71afa0c 7016078: javax.net.ssl.SSLException: Received fatal alert: internal_error starting JDK 7 b126
vinnie
parents: 5506
diff changeset
  1021
                            wECPoint.getTag());
d8a2b71afa0c 7016078: javax.net.ssl.SSLException: Received fatal alert: internal_error starting JDK 7 b126
vinnie
parents: 5506
diff changeset
  1022
d8a2b71afa0c 7016078: javax.net.ssl.SSLException: Received fatal alert: internal_error starting JDK 7 b126
vinnie
parents: 5506
diff changeset
  1023
                    w = P11ECKeyFactory.decodePoint
d8a2b71afa0c 7016078: javax.net.ssl.SSLException: Received fatal alert: internal_error starting JDK 7 b126
vinnie
parents: 5506
diff changeset
  1024
                        (wECPoint.getDataBytes(), params.getCurve());
d8a2b71afa0c 7016078: javax.net.ssl.SSLException: Received fatal alert: internal_error starting JDK 7 b126
vinnie
parents: 5506
diff changeset
  1025
d8a2b71afa0c 7016078: javax.net.ssl.SSLException: Received fatal alert: internal_error starting JDK 7 b126
vinnie
parents: 5506
diff changeset
  1026
                } catch (IOException e) {
d8a2b71afa0c 7016078: javax.net.ssl.SSLException: Received fatal alert: internal_error starting JDK 7 b126
vinnie
parents: 5506
diff changeset
  1027
                    // Failover
d8a2b71afa0c 7016078: javax.net.ssl.SSLException: Received fatal alert: internal_error starting JDK 7 b126
vinnie
parents: 5506
diff changeset
  1028
                    w = P11ECKeyFactory.decodePoint(ecKey, params.getCurve());
d8a2b71afa0c 7016078: javax.net.ssl.SSLException: Received fatal alert: internal_error starting JDK 7 b126
vinnie
parents: 5506
diff changeset
  1029
                }
4809
c00eed67999d 6763530: Cannot decode PublicKey (Proider SunPKCS11, curve prime256v1)
vinnie
parents: 2180
diff changeset
  1030
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
                throw new RuntimeException("Could not parse key values", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        public String getFormat() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
            token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
            return "X.509";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
        synchronized byte[] getEncodedInternal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
            if (encoded == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
                fetchValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                    Key key = new sun.security.ec.ECPublicKeyImpl(w, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                    encoded = key.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                } catch (InvalidKeyException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
                    throw new ProviderException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
            return encoded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
        public ECPoint getW() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
            fetchValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
            return w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        public ECParameterSpec getParams() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
            fetchValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
            return params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
            fetchValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
            return super.toString()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
                + "\n  public x coord: " + w.getAffineX()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
                + "\n  public y coord: " + w.getAffineY()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
                + "\n  parameters: " + params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
    }
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1068
}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
5291
d6df082f6524 6918573: sun.security.pkcs11.P11RSACipher.finalize() is a scalability blocker
valeriep
parents: 4809
diff changeset
  1070
/*
d6df082f6524 6918573: sun.security.pkcs11.P11RSACipher.finalize() is a scalability blocker
valeriep
parents: 4809
diff changeset
  1071
 * NOTE: Must use PhantomReference here and not WeakReference
d6df082f6524 6918573: sun.security.pkcs11.P11RSACipher.finalize() is a scalability blocker
valeriep
parents: 4809
diff changeset
  1072
 * otherwise the key maybe cleared before other objects which
d6df082f6524 6918573: sun.security.pkcs11.P11RSACipher.finalize() is a scalability blocker
valeriep
parents: 4809
diff changeset
  1073
 * still use these keys during finalization such as SSLSocket.
d6df082f6524 6918573: sun.security.pkcs11.P11RSACipher.finalize() is a scalability blocker
valeriep
parents: 4809
diff changeset
  1074
 */
d6df082f6524 6918573: sun.security.pkcs11.P11RSACipher.finalize() is a scalability blocker
valeriep
parents: 4809
diff changeset
  1075
final class SessionKeyRef extends PhantomReference<P11Key>
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1076
    implements Comparable<SessionKeyRef> {
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1077
    private static ReferenceQueue<P11Key> refQueue =
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1078
        new ReferenceQueue<P11Key>();
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1079
    private static Set<SessionKeyRef> refList =
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1080
        Collections.synchronizedSortedSet(new TreeSet<SessionKeyRef>());
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1081
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1082
    static ReferenceQueue<P11Key> referenceQueue() {
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1083
        return refQueue;
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1084
    }
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1085
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1086
    private static void drainRefQueueBounded() {
5291
d6df082f6524 6918573: sun.security.pkcs11.P11RSACipher.finalize() is a scalability blocker
valeriep
parents: 4809
diff changeset
  1087
        while (true) {
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1088
            SessionKeyRef next = (SessionKeyRef) refQueue.poll();
5291
d6df082f6524 6918573: sun.security.pkcs11.P11RSACipher.finalize() is a scalability blocker
valeriep
parents: 4809
diff changeset
  1089
            if (next == null) break;
d6df082f6524 6918573: sun.security.pkcs11.P11RSACipher.finalize() is a scalability blocker
valeriep
parents: 4809
diff changeset
  1090
            next.dispose();
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1091
        }
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1092
    }
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1093
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1094
    // handle to the native key
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1095
    private long keyID;
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1096
    private Session session;
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1097
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1098
    SessionKeyRef(P11Key key , long keyID, Session session) {
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1099
        super(key, refQueue);
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1100
        this.keyID = keyID;
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1101
        this.session = session;
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1102
        this.session.addObject();
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1103
        refList.add(this);
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1104
        // TBD: run at some interval and not every time?
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1105
        drainRefQueueBounded();
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1106
    }
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1107
5291
d6df082f6524 6918573: sun.security.pkcs11.P11RSACipher.finalize() is a scalability blocker
valeriep
parents: 4809
diff changeset
  1108
    private void dispose() {
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1109
        refList.remove(this);
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1110
        if (session.token.isValid()) {
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1111
            Session newSession = null;
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1112
            try {
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1113
                newSession = session.token.getOpSession();
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1114
                session.token.p11.C_DestroyObject(newSession.id(), keyID);
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1115
            } catch (PKCS11Exception e) {
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1116
                // ignore
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1117
            } finally {
5291
d6df082f6524 6918573: sun.security.pkcs11.P11RSACipher.finalize() is a scalability blocker
valeriep
parents: 4809
diff changeset
  1118
                this.clear();
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1119
                session.token.releaseSession(newSession);
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1120
                session.removeObject();
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1121
            }
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1122
        }
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1123
    }
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1124
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1125
    public int compareTo(SessionKeyRef other) {
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1126
        if (this.keyID == other.keyID) {
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1127
            return 0;
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1128
        } else {
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1129
            return (this.keyID < other.keyID) ? -1 : 1;
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1130
        }
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1131
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
}