jdk/src/share/classes/sun/security/pkcs11/P11ECKeyFactory.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 5506 202f599c92aa
child 10336 0bb1999251f8
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 5506
diff changeset
     2
 * Copyright (c) 2006, 2010, 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: 4809
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: 4809
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: 4809
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4809
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4809
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.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.interfaces.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.ec.ECPublicKeyImpl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.security.ec.ECParameters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.security.ec.NamedCurve;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import static sun.security.pkcs11.TemplateManager.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.security.pkcs11.wrapper.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import static sun.security.pkcs11.wrapper.PKCS11Constants.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
4809
c00eed67999d 6763530: Cannot decode PublicKey (Proider SunPKCS11, curve prime256v1)
vinnie
parents: 2
diff changeset
    43
import sun.security.util.DerValue;
c00eed67999d 6763530: Cannot decode PublicKey (Proider SunPKCS11, curve prime256v1)
vinnie
parents: 2
diff changeset
    44
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * EC KeyFactory implemenation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @since   1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
final class P11ECKeyFactory extends P11KeyFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    P11ECKeyFactory(Token token, String algorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        super(token, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    static ECParameterSpec getECParameterSpec(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        return NamedCurve.getECParameterSpec(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    static ECParameterSpec getECParameterSpec(int keySize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        return NamedCurve.getECParameterSpec(keySize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    // Check that spec is a known supported curve and convert it to our
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    // ECParameterSpec subclass. If not possible, return null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    static ECParameterSpec getECParameterSpec(ECParameterSpec spec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        return ECParameters.getNamedCurve(spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    static ECParameterSpec decodeParameters(byte[] params) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        return ECParameters.decodeParameters(params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    static byte[] encodeParameters(ECParameterSpec params) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        return ECParameters.encodeParameters(params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    static ECPoint decodePoint(byte[] encoded, EllipticCurve curve) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        return ECParameters.decodePoint(encoded, curve);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    // Used by ECDH KeyAgreement
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    static byte[] getEncodedPublicValue(PublicKey key) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        if (key instanceof ECPublicKeyImpl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            return ((ECPublicKeyImpl)key).getEncodedPublicValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        } else if (key instanceof ECPublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            ECPublicKey ecKey = (ECPublicKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            ECPoint w = ecKey.getW();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            ECParameterSpec params = ecKey.getParams();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            return ECParameters.encodePoint(w, params.getCurve());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            // should never occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            throw new InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                ("Key class not yet supported: " + key.getClass().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    PublicKey implTranslatePublicKey(PublicKey key) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            if (key instanceof ECPublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                ECPublicKey ecKey = (ECPublicKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                return generatePublic(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                    ecKey.getW(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                    ecKey.getParams()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            } else if ("X.509".equals(key.getFormat())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                // let Sun provider parse for us, then recurse
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                byte[] encoded = key.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                key = new sun.security.ec.ECPublicKeyImpl(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                return implTranslatePublicKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                throw new InvalidKeyException("PublicKey must be instance "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                        + "of ECPublicKey or have X.509 encoding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            throw new InvalidKeyException("Could not create EC public key", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    PrivateKey implTranslatePrivateKey(PrivateKey key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            if (key instanceof ECPrivateKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                ECPrivateKey ecKey = (ECPrivateKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                return generatePrivate(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    ecKey.getS(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    ecKey.getParams()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            } else if ("PKCS#8".equals(key.getFormat())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                // let Sun provider parse for us, then recurse
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                byte[] encoded = key.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                key = new sun.security.ec.ECPrivateKeyImpl(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                return implTranslatePrivateKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                throw new InvalidKeyException("PrivateKey must be instance "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                        + "of ECPrivateKey or have PKCS#8 encoding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            throw new InvalidKeyException("Could not create EC private key", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    // see JCA spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    protected PublicKey engineGeneratePublic(KeySpec keySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            throws InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (keySpec instanceof X509EncodedKeySpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                byte[] encoded = ((X509EncodedKeySpec)keySpec).getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                PublicKey key = new sun.security.ec.ECPublicKeyImpl(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                return implTranslatePublicKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            } catch (InvalidKeyException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                        ("Could not create EC public key", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (keySpec instanceof ECPublicKeySpec == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            throw new InvalidKeySpecException("Only ECPublicKeySpec and "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                + "X509EncodedKeySpec supported for EC public keys");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            ECPublicKeySpec ec = (ECPublicKeySpec)keySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            return generatePublic(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                ec.getW(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                ec.getParams()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                ("Could not create EC public key", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    // see JCA spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            throws InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        if (keySpec instanceof PKCS8EncodedKeySpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                byte[] encoded = ((PKCS8EncodedKeySpec)keySpec).getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                PrivateKey key = new sun.security.ec.ECPrivateKeyImpl(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                return implTranslatePrivateKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                        ("Could not create EC private key", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if (keySpec instanceof ECPrivateKeySpec == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            throw new InvalidKeySpecException("Only ECPrivateKeySpec and "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                + "PKCS8EncodedKeySpec supported for EC private keys");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            ECPrivateKeySpec ec = (ECPrivateKeySpec)keySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            return generatePrivate(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                ec.getS(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                ec.getParams()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                ("Could not create EC private key", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    private PublicKey generatePublic(ECPoint point, ECParameterSpec params) throws PKCS11Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        byte[] encodedParams = ECParameters.encodeParameters(params);
4809
c00eed67999d 6763530: Cannot decode PublicKey (Proider SunPKCS11, curve prime256v1)
vinnie
parents: 2
diff changeset
   206
        byte[] encodedPoint = null;
c00eed67999d 6763530: Cannot decode PublicKey (Proider SunPKCS11, curve prime256v1)
vinnie
parents: 2
diff changeset
   207
        DerValue pkECPoint = new DerValue(DerValue.tag_OctetString,
c00eed67999d 6763530: Cannot decode PublicKey (Proider SunPKCS11, curve prime256v1)
vinnie
parents: 2
diff changeset
   208
            ECParameters.encodePoint(point, params.getCurve()));
c00eed67999d 6763530: Cannot decode PublicKey (Proider SunPKCS11, curve prime256v1)
vinnie
parents: 2
diff changeset
   209
c00eed67999d 6763530: Cannot decode PublicKey (Proider SunPKCS11, curve prime256v1)
vinnie
parents: 2
diff changeset
   210
        try {
c00eed67999d 6763530: Cannot decode PublicKey (Proider SunPKCS11, curve prime256v1)
vinnie
parents: 2
diff changeset
   211
            encodedPoint = pkECPoint.toByteArray();
c00eed67999d 6763530: Cannot decode PublicKey (Proider SunPKCS11, curve prime256v1)
vinnie
parents: 2
diff changeset
   212
        } catch (IOException e) {
c00eed67999d 6763530: Cannot decode PublicKey (Proider SunPKCS11, curve prime256v1)
vinnie
parents: 2
diff changeset
   213
            throw new IllegalArgumentException("Could not DER encode point", e);
c00eed67999d 6763530: Cannot decode PublicKey (Proider SunPKCS11, curve prime256v1)
vinnie
parents: 2
diff changeset
   214
        }
c00eed67999d 6763530: Cannot decode PublicKey (Proider SunPKCS11, curve prime256v1)
vinnie
parents: 2
diff changeset
   215
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            new CK_ATTRIBUTE(CKA_CLASS, CKO_PUBLIC_KEY),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_EC),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            new CK_ATTRIBUTE(CKA_EC_POINT, encodedPoint),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            new CK_ATTRIBUTE(CKA_EC_PARAMS, encodedParams),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        attributes = token.getAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                (O_IMPORT, CKO_PUBLIC_KEY, CKK_EC, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        Session session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            session = token.getObjSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            long keyID = token.p11.C_CreateObject(session.id(), attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            return P11Key.publicKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                (session, keyID, "EC", params.getCurve().getField().getFieldSize(), attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    private PrivateKey generatePrivate(BigInteger s, ECParameterSpec params) throws PKCS11Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        byte[] encodedParams = ECParameters.encodeParameters(params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            new CK_ATTRIBUTE(CKA_CLASS, CKO_PRIVATE_KEY),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_EC),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            new CK_ATTRIBUTE(CKA_VALUE, s),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            new CK_ATTRIBUTE(CKA_EC_PARAMS, encodedParams),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        attributes = token.getAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                (O_IMPORT, CKO_PRIVATE_KEY, CKK_EC, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        Session session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            session = token.getObjSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            long keyID = token.p11.C_CreateObject(session.id(), attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            return P11Key.privateKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                (session, keyID, "EC", params.getCurve().getField().getFieldSize(), attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    KeySpec implGetPublicKeySpec(P11Key key, Class keySpec, Session[] session)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            throws PKCS11Exception, InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        if (ECPublicKeySpec.class.isAssignableFrom(keySpec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            session[0] = token.getObjSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                new CK_ATTRIBUTE(CKA_EC_POINT),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                new CK_ATTRIBUTE(CKA_EC_PARAMS),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            token.p11.C_GetAttributeValue(session[0].id(), key.keyID, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                ECParameterSpec params = decodeParameters(attributes[1].getByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                ECPoint point = decodePoint(attributes[0].getByteArray(), params.getCurve());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                return new ECPublicKeySpec(point, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                throw new InvalidKeySpecException("Could not parse key", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        } else { // X.509 handled in superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            throw new InvalidKeySpecException("Only ECPublicKeySpec and "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                + "X509EncodedKeySpec supported for EC public keys");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    KeySpec implGetPrivateKeySpec(P11Key key, Class keySpec, Session[] session)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            throws PKCS11Exception, InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        if (ECPrivateKeySpec.class.isAssignableFrom(keySpec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            session[0] = token.getObjSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                new CK_ATTRIBUTE(CKA_VALUE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                new CK_ATTRIBUTE(CKA_EC_PARAMS),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            token.p11.C_GetAttributeValue(session[0].id(), key.keyID, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                ECParameterSpec params = decodeParameters(attributes[1].getByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                return new ECPrivateKeySpec(attributes[0].getBigInteger(), params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                throw new InvalidKeySpecException("Could not parse key", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        } else { // PKCS#8 handled in superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            throw new InvalidKeySpecException("Only ECPrivateKeySpec "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                + "and PKCS8EncodedKeySpec supported for EC private keys");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    KeyFactory implGetSoftwareFactory() throws GeneralSecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        return sun.security.ec.ECKeyFactory.INSTANCE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
}