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