jdk/src/share/classes/sun/security/ec/ECKeyFactory.java
author jjg
Mon, 15 Aug 2011 11:48:20 -0700
changeset 10336 0bb1999251f8
parent 5506 202f599c92aa
child 13661 7c894680910a
permissions -rw-r--r--
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror Reviewed-by: xuelei, mullan Contributed-by: alexandre.boulgakov@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
     2
 * Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    // Used by translateKey() and the SunPKCS11 provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public final static KeyFactory INSTANCE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    // Internal provider object we can obtain the KeyFactory and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    // AlgorithmParameters from. Used by ECParameters and AlgorithmId.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    // This can go away once we have EC always available in the SUN provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // Used by ECParameters and AlgorithmId.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    public final static Provider ecInternalProvider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    static {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    64
        final Provider p = new Provider("SunEC-Internal", 1.0d, null) {
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    65
            private static final long serialVersionUID = 970685700309471261L;
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    66
        };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            public Void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                p.put("KeyFactory.EC", "sun.security.ec.ECKeyFactory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                p.put("AlgorithmParameters.EC", "sun.security.ec.ECParameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                p.put("Alg.Alias.AlgorithmParameters.1.2.840.10045.2.1", "EC");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            INSTANCE = KeyFactory.getInstance("EC", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        ecInternalProvider = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    public ECKeyFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * Static method to convert Key into a useable instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * ECPublicKey or ECPrivateKey. Check the key and convert it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * to a Sun key if necessary. If the key is not an EC key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * or cannot be used, throw an InvalidKeyException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * The difference between this method and engineTranslateKey() is that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * we do not convert keys of other providers that are already an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * instance of ECPublicKey or ECPrivateKey.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * To be used by future Java ECDSA and ECDH implementations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public static ECKey toECKey(Key key) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        if (key instanceof ECKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            ECKey ecKey = (ECKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            checkKey(ecKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            return ecKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            return (ECKey)INSTANCE.translateKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * Check that the given EC key is valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private static void checkKey(ECKey key) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        // check for subinterfaces, omit additional checks for our keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (key instanceof ECPublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            if (key instanceof ECPublicKeyImpl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        } else if (key instanceof ECPrivateKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            if (key instanceof ECPrivateKeyImpl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            throw new InvalidKeyException("Neither a public nor a private key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        // ECKey does not extend Key, so we need to do a cast
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        String keyAlg = ((Key)key).getAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        if (keyAlg.equals("EC") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            throw new InvalidKeyException("Not an EC key: " + keyAlg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        // XXX further sanity checks about whether this key uses supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        // fields, point formats, etc. would go here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * Translate an EC key into a Sun EC key. If conversion is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * not possible, throw an InvalidKeyException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * See also JCA doc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    protected Key engineTranslateKey(Key key) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        if (key == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            throw new InvalidKeyException("Key must not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        String keyAlg = key.getAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (keyAlg.equals("EC") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            throw new InvalidKeyException("Not an EC key: " + keyAlg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if (key instanceof PublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            return implTranslatePublicKey((PublicKey)key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        } else if (key instanceof PrivateKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            return implTranslatePrivateKey((PrivateKey)key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            throw new InvalidKeyException("Neither a public nor a private key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    // see JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    protected PublicKey engineGeneratePublic(KeySpec keySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            throws InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            return implGeneratePublic(keySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        } catch (InvalidKeySpecException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            throw new InvalidKeySpecException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    // see JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            throws InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            return implGeneratePrivate(keySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        } catch (InvalidKeySpecException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            throw new InvalidKeySpecException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    // internal implementation of translateKey() for public keys. See JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    private PublicKey implTranslatePublicKey(PublicKey key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        if (key instanceof ECPublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            if (key instanceof ECPublicKeyImpl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            ECPublicKey ecKey = (ECPublicKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            return new ECPublicKeyImpl(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                ecKey.getW(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                ecKey.getParams()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        } else if ("X.509".equals(key.getFormat())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            byte[] encoded = key.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            return new ECPublicKeyImpl(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            throw new InvalidKeyException("Public keys must be instance "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                + "of ECPublicKey or have X.509 encoding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    // internal implementation of translateKey() for private keys. See JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    private PrivateKey implTranslatePrivateKey(PrivateKey key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        if (key instanceof ECPrivateKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            if (key instanceof ECPrivateKeyImpl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            ECPrivateKey ecKey = (ECPrivateKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            return new ECPrivateKeyImpl(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                ecKey.getS(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                ecKey.getParams()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        } else if ("PKCS#8".equals(key.getFormat())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            return new ECPrivateKeyImpl(key.getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            throw new InvalidKeyException("Private keys must be instance "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                + "of ECPrivateKey or have PKCS#8 encoding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    // internal implementation of generatePublic. See JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    private PublicKey implGeneratePublic(KeySpec keySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            throws GeneralSecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        if (keySpec instanceof X509EncodedKeySpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            X509EncodedKeySpec x509Spec = (X509EncodedKeySpec)keySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            return new ECPublicKeyImpl(x509Spec.getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        } else if (keySpec instanceof ECPublicKeySpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            ECPublicKeySpec ecSpec = (ECPublicKeySpec)keySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            return new ECPublicKeyImpl(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                ecSpec.getW(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                ecSpec.getParams()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            throw new InvalidKeySpecException("Only ECPublicKeySpec "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                + "and X509EncodedKeySpec supported for EC public keys");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    // internal implementation of generatePrivate. See JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    private PrivateKey implGeneratePrivate(KeySpec keySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            throws GeneralSecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        if (keySpec instanceof PKCS8EncodedKeySpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            PKCS8EncodedKeySpec pkcsSpec = (PKCS8EncodedKeySpec)keySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            return new ECPrivateKeyImpl(pkcsSpec.getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        } else if (keySpec instanceof ECPrivateKeySpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            ECPrivateKeySpec ecSpec = (ECPrivateKeySpec)keySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            return new ECPrivateKeyImpl(ecSpec.getS(), ecSpec.getParams());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            throw new InvalidKeySpecException("Only ECPrivateKeySpec "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                + "and PKCS8EncodedKeySpec supported for EC private keys");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    protected <T extends KeySpec> T engineGetKeySpec(Key key, Class<T> keySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            throws InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            // convert key to one of our keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            // this also verifies that the key is a valid EC key and ensures
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            // that the encoding is X.509/PKCS#8 for public/private keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            key = engineTranslateKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        } catch (InvalidKeyException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            throw new InvalidKeySpecException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        if (key instanceof ECPublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            ECPublicKey ecKey = (ECPublicKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            if (ECPublicKeySpec.class.isAssignableFrom(keySpec)) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   267
                return keySpec.cast(new ECPublicKeySpec(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    ecKey.getW(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                    ecKey.getParams()
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   270
                ));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            } 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
   272
                return keySpec.cast(new X509EncodedKeySpec(key.getEncoded()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                        ("KeySpec must be ECPublicKeySpec or "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                        + "X509EncodedKeySpec for EC public keys");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        } else if (key instanceof ECPrivateKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            if (PKCS8EncodedKeySpec.class.isAssignableFrom(keySpec)) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   280
                return keySpec.cast(new PKCS8EncodedKeySpec(key.getEncoded()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            } else if (ECPrivateKeySpec.class.isAssignableFrom(keySpec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                ECPrivateKey ecKey = (ECPrivateKey)key;
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   283
                return keySpec.cast(new ECPrivateKeySpec(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                    ecKey.getS(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                    ecKey.getParams()
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   286
                ));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                        ("KeySpec must be ECPrivateKeySpec or "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                        + "PKCS8EncodedKeySpec for EC private keys");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            // should not occur, caught in engineTranslateKey()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            throw new InvalidKeySpecException("Neither public nor private key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
}