src/jdk.crypto.ec/share/classes/sun/security/ec/ECKeyFactory.java
author mgronlun
Tue, 08 Oct 2019 11:57:11 +0200
branchJEP-349-branch
changeset 58495 465ba4fefe62
parent 47216 71c04702a3d5
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
13661
7c894680910a 6995421: Eliminate the static dependency to sun.security.ec.ECKeyFactory
mullan
parents: 10336
diff changeset
     2
 * Copyright (c) 2006, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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.ec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.interfaces.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * KeyFactory for EC keys. Keys must be instances of PublicKey or PrivateKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * and getAlgorithm() must return "EC". For such keys, it supports conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * between the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * For public keys:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *  . PublicKey with an X.509 encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *  . ECPublicKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *  . ECPublicKeySpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *  . X509EncodedKeySpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * For private keys:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *  . PrivateKey with a PKCS#8 encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *  . ECPrivateKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *  . ECPrivateKeySpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *  . PKCS8EncodedKeySpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @since   1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
public final class ECKeyFactory extends KeyFactorySpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
13661
7c894680910a 6995421: Eliminate the static dependency to sun.security.ec.ECKeyFactory
mullan
parents: 10336
diff changeset
    54
    // Used by translateKey()
7c894680910a 6995421: Eliminate the static dependency to sun.security.ec.ECKeyFactory
mullan
parents: 10336
diff changeset
    55
    private static KeyFactory instance;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
13661
7c894680910a 6995421: Eliminate the static dependency to sun.security.ec.ECKeyFactory
mullan
parents: 10336
diff changeset
    57
    private static KeyFactory getInstance() {
7c894680910a 6995421: Eliminate the static dependency to sun.security.ec.ECKeyFactory
mullan
parents: 10336
diff changeset
    58
        if (instance == null) {
7c894680910a 6995421: Eliminate the static dependency to sun.security.ec.ECKeyFactory
mullan
parents: 10336
diff changeset
    59
            try {
7c894680910a 6995421: Eliminate the static dependency to sun.security.ec.ECKeyFactory
mullan
parents: 10336
diff changeset
    60
                instance = KeyFactory.getInstance("EC", "SunEC");
7c894680910a 6995421: Eliminate the static dependency to sun.security.ec.ECKeyFactory
mullan
parents: 10336
diff changeset
    61
            } catch (NoSuchProviderException e) {
7c894680910a 6995421: Eliminate the static dependency to sun.security.ec.ECKeyFactory
mullan
parents: 10336
diff changeset
    62
                throw new RuntimeException(e);
7c894680910a 6995421: Eliminate the static dependency to sun.security.ec.ECKeyFactory
mullan
parents: 10336
diff changeset
    63
            } catch (NoSuchAlgorithmException e) {
7c894680910a 6995421: Eliminate the static dependency to sun.security.ec.ECKeyFactory
mullan
parents: 10336
diff changeset
    64
                throw new RuntimeException(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        }
13661
7c894680910a 6995421: Eliminate the static dependency to sun.security.ec.ECKeyFactory
mullan
parents: 10336
diff changeset
    67
7c894680910a 6995421: Eliminate the static dependency to sun.security.ec.ECKeyFactory
mullan
parents: 10336
diff changeset
    68
        return instance;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public ECKeyFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Static method to convert Key into a useable instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * ECPublicKey or ECPrivateKey. Check the key and convert it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * to a Sun key if necessary. If the key is not an EC key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * or cannot be used, throw an InvalidKeyException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * The difference between this method and engineTranslateKey() is that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * we do not convert keys of other providers that are already an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * instance of ECPublicKey or ECPrivateKey.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * To be used by future Java ECDSA and ECDH implementations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public static ECKey toECKey(Key key) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        if (key instanceof ECKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            ECKey ecKey = (ECKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            checkKey(ecKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            return ecKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        } else {
13661
7c894680910a 6995421: Eliminate the static dependency to sun.security.ec.ECKeyFactory
mullan
parents: 10336
diff changeset
    93
            /*
7c894680910a 6995421: Eliminate the static dependency to sun.security.ec.ECKeyFactory
mullan
parents: 10336
diff changeset
    94
             * We don't call the engineTranslateKey method directly
7c894680910a 6995421: Eliminate the static dependency to sun.security.ec.ECKeyFactory
mullan
parents: 10336
diff changeset
    95
             * because KeyFactory.translateKey adds code to loop through
7c894680910a 6995421: Eliminate the static dependency to sun.security.ec.ECKeyFactory
mullan
parents: 10336
diff changeset
    96
             * all key factories.
7c894680910a 6995421: Eliminate the static dependency to sun.security.ec.ECKeyFactory
mullan
parents: 10336
diff changeset
    97
             */
7c894680910a 6995421: Eliminate the static dependency to sun.security.ec.ECKeyFactory
mullan
parents: 10336
diff changeset
    98
            return (ECKey)getInstance().translateKey(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * Check that the given EC key is valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private static void checkKey(ECKey key) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        // check for subinterfaces, omit additional checks for our keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        if (key instanceof ECPublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            if (key instanceof ECPublicKeyImpl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        } else if (key instanceof ECPrivateKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            if (key instanceof ECPrivateKeyImpl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            throw new InvalidKeyException("Neither a public nor a private key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        // ECKey does not extend Key, so we need to do a cast
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        String keyAlg = ((Key)key).getAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (keyAlg.equals("EC") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            throw new InvalidKeyException("Not an EC key: " + keyAlg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        // XXX further sanity checks about whether this key uses supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        // fields, point formats, etc. would go here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Translate an EC key into a Sun EC key. If conversion is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * not possible, throw an InvalidKeyException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * See also JCA doc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    protected Key engineTranslateKey(Key key) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if (key == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            throw new InvalidKeyException("Key must not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        String keyAlg = key.getAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (keyAlg.equals("EC") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            throw new InvalidKeyException("Not an EC key: " + keyAlg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        if (key instanceof PublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            return implTranslatePublicKey((PublicKey)key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        } else if (key instanceof PrivateKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            return implTranslatePrivateKey((PrivateKey)key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            throw new InvalidKeyException("Neither a public nor a private key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    // see JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    protected PublicKey engineGeneratePublic(KeySpec keySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            throws InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            return implGeneratePublic(keySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        } catch (InvalidKeySpecException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            throw new InvalidKeySpecException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    // see JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            throws InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            return implGeneratePrivate(keySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        } catch (InvalidKeySpecException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            throw new InvalidKeySpecException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    // internal implementation of translateKey() for public keys. See JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    private PublicKey implTranslatePublicKey(PublicKey key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        if (key instanceof ECPublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            if (key instanceof ECPublicKeyImpl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            ECPublicKey ecKey = (ECPublicKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            return new ECPublicKeyImpl(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                ecKey.getW(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                ecKey.getParams()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        } else if ("X.509".equals(key.getFormat())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            byte[] encoded = key.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            return new ECPublicKeyImpl(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            throw new InvalidKeyException("Public keys must be instance "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                + "of ECPublicKey or have X.509 encoding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    // internal implementation of translateKey() for private keys. See JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    private PrivateKey implTranslatePrivateKey(PrivateKey key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        if (key instanceof ECPrivateKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            if (key instanceof ECPrivateKeyImpl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            ECPrivateKey ecKey = (ECPrivateKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            return new ECPrivateKeyImpl(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                ecKey.getS(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                ecKey.getParams()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        } else if ("PKCS#8".equals(key.getFormat())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            return new ECPrivateKeyImpl(key.getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            throw new InvalidKeyException("Private keys must be instance "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                + "of ECPrivateKey or have PKCS#8 encoding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    // internal implementation of generatePublic. See JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    private PublicKey implGeneratePublic(KeySpec keySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            throws GeneralSecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        if (keySpec instanceof X509EncodedKeySpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            X509EncodedKeySpec x509Spec = (X509EncodedKeySpec)keySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            return new ECPublicKeyImpl(x509Spec.getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        } else if (keySpec instanceof ECPublicKeySpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            ECPublicKeySpec ecSpec = (ECPublicKeySpec)keySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            return new ECPublicKeyImpl(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                ecSpec.getW(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                ecSpec.getParams()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            throw new InvalidKeySpecException("Only ECPublicKeySpec "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                + "and X509EncodedKeySpec supported for EC public keys");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    // internal implementation of generatePrivate. See JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    private PrivateKey implGeneratePrivate(KeySpec keySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            throws GeneralSecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        if (keySpec instanceof PKCS8EncodedKeySpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            PKCS8EncodedKeySpec pkcsSpec = (PKCS8EncodedKeySpec)keySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            return new ECPrivateKeyImpl(pkcsSpec.getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        } else if (keySpec instanceof ECPrivateKeySpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            ECPrivateKeySpec ecSpec = (ECPrivateKeySpec)keySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            return new ECPrivateKeyImpl(ecSpec.getS(), ecSpec.getParams());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            throw new InvalidKeySpecException("Only ECPrivateKeySpec "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                + "and PKCS8EncodedKeySpec supported for EC private keys");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    protected <T extends KeySpec> T engineGetKeySpec(Key key, Class<T> keySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            throws InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            // convert key to one of our keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            // this also verifies that the key is a valid EC key and ensures
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            // that the encoding is X.509/PKCS#8 for public/private keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            key = engineTranslateKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        } catch (InvalidKeyException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            throw new InvalidKeySpecException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        if (key instanceof ECPublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            ECPublicKey ecKey = (ECPublicKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            if (ECPublicKeySpec.class.isAssignableFrom(keySpec)) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   260
                return keySpec.cast(new ECPublicKeySpec(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    ecKey.getW(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    ecKey.getParams()
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   263
                ));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            } else if (X509EncodedKeySpec.class.isAssignableFrom(keySpec)) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   265
                return keySpec.cast(new X509EncodedKeySpec(key.getEncoded()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                        ("KeySpec must be ECPublicKeySpec or "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                        + "X509EncodedKeySpec for EC public keys");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        } else if (key instanceof ECPrivateKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            if (PKCS8EncodedKeySpec.class.isAssignableFrom(keySpec)) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   273
                return keySpec.cast(new PKCS8EncodedKeySpec(key.getEncoded()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            } else if (ECPrivateKeySpec.class.isAssignableFrom(keySpec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                ECPrivateKey ecKey = (ECPrivateKey)key;
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   276
                return keySpec.cast(new ECPrivateKeySpec(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                    ecKey.getS(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                    ecKey.getParams()
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   279
                ));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                        ("KeySpec must be ECPrivateKeySpec or "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                        + "PKCS8EncodedKeySpec for EC private keys");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            // should not occur, caught in engineTranslateKey()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            throw new InvalidKeySpecException("Neither public nor private key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
}