jdk/src/share/classes/sun/security/ec/ECKeyFactory.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2006 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        final Provider p = new Provider("SunEC-Internal", 1.0d, null) {};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            public Void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                p.put("KeyFactory.EC", "sun.security.ec.ECKeyFactory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                p.put("AlgorithmParameters.EC", "sun.security.ec.ECParameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                p.put("Alg.Alias.AlgorithmParameters.1.2.840.10045.2.1", "EC");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            INSTANCE = KeyFactory.getInstance("EC", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        ecInternalProvider = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public ECKeyFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Static method to convert Key into a useable instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * ECPublicKey or ECPrivateKey. Check the key and convert it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * to a Sun key if necessary. If the key is not an EC key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * or cannot be used, throw an InvalidKeyException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * The difference between this method and engineTranslateKey() is that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * we do not convert keys of other providers that are already an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * instance of ECPublicKey or ECPrivateKey.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * To be used by future Java ECDSA and ECDH implementations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public static ECKey toECKey(Key key) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        if (key instanceof ECKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            ECKey ecKey = (ECKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            checkKey(ecKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            return ecKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            return (ECKey)INSTANCE.translateKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Check that the given EC key is valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private static void checkKey(ECKey key) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        // check for subinterfaces, omit additional checks for our keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (key instanceof ECPublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            if (key instanceof ECPublicKeyImpl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        } else if (key instanceof ECPrivateKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            if (key instanceof ECPrivateKeyImpl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            throw new InvalidKeyException("Neither a public nor a private key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        // ECKey does not extend Key, so we need to do a cast
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        String keyAlg = ((Key)key).getAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (keyAlg.equals("EC") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            throw new InvalidKeyException("Not an EC key: " + keyAlg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        // XXX further sanity checks about whether this key uses supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        // fields, point formats, etc. would go here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * Translate an EC key into a Sun EC key. If conversion is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * not possible, throw an InvalidKeyException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * See also JCA doc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    protected Key engineTranslateKey(Key key) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if (key == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            throw new InvalidKeyException("Key must not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        String keyAlg = key.getAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if (keyAlg.equals("EC") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            throw new InvalidKeyException("Not an EC key: " + keyAlg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if (key instanceof PublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            return implTranslatePublicKey((PublicKey)key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        } else if (key instanceof PrivateKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            return implTranslatePrivateKey((PrivateKey)key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            throw new InvalidKeyException("Neither a public nor a private key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    // see JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    protected PublicKey engineGeneratePublic(KeySpec keySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            throws InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            return implGeneratePublic(keySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        } catch (InvalidKeySpecException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            throw new InvalidKeySpecException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    // see JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            throws InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            return implGeneratePrivate(keySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        } catch (InvalidKeySpecException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            throw new InvalidKeySpecException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    // internal implementation of translateKey() for public keys. See JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    private PublicKey implTranslatePublicKey(PublicKey key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        if (key instanceof ECPublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            if (key instanceof ECPublicKeyImpl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            ECPublicKey ecKey = (ECPublicKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            return new ECPublicKeyImpl(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                ecKey.getW(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                ecKey.getParams()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        } else if ("X.509".equals(key.getFormat())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            byte[] encoded = key.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            return new ECPublicKeyImpl(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            throw new InvalidKeyException("Public keys must be instance "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                + "of ECPublicKey or have X.509 encoding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    // internal implementation of translateKey() for private keys. See JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    private PrivateKey implTranslatePrivateKey(PrivateKey key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        if (key instanceof ECPrivateKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            if (key instanceof ECPrivateKeyImpl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            ECPrivateKey ecKey = (ECPrivateKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            return new ECPrivateKeyImpl(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                ecKey.getS(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                ecKey.getParams()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        } else if ("PKCS#8".equals(key.getFormat())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            return new ECPrivateKeyImpl(key.getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            throw new InvalidKeyException("Private keys must be instance "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                + "of ECPrivateKey or have PKCS#8 encoding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    // internal implementation of generatePublic. See JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    private PublicKey implGeneratePublic(KeySpec keySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            throws GeneralSecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        if (keySpec instanceof X509EncodedKeySpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            X509EncodedKeySpec x509Spec = (X509EncodedKeySpec)keySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            return new ECPublicKeyImpl(x509Spec.getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        } else if (keySpec instanceof ECPublicKeySpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            ECPublicKeySpec ecSpec = (ECPublicKeySpec)keySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            return new ECPublicKeyImpl(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                ecSpec.getW(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                ecSpec.getParams()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            throw new InvalidKeySpecException("Only ECPublicKeySpec "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                + "and X509EncodedKeySpec supported for EC public keys");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    // internal implementation of generatePrivate. See JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    private PrivateKey implGeneratePrivate(KeySpec keySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            throws GeneralSecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        if (keySpec instanceof PKCS8EncodedKeySpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            PKCS8EncodedKeySpec pkcsSpec = (PKCS8EncodedKeySpec)keySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            return new ECPrivateKeyImpl(pkcsSpec.getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        } else if (keySpec instanceof ECPrivateKeySpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            ECPrivateKeySpec ecSpec = (ECPrivateKeySpec)keySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            return new ECPrivateKeyImpl(ecSpec.getS(), ecSpec.getParams());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            throw new InvalidKeySpecException("Only ECPrivateKeySpec "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                + "and PKCS8EncodedKeySpec supported for EC private keys");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    protected <T extends KeySpec> T engineGetKeySpec(Key key, Class<T> keySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            throws InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            // convert key to one of our keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            // this also verifies that the key is a valid EC key and ensures
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            // that the encoding is X.509/PKCS#8 for public/private keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            key = engineTranslateKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        } catch (InvalidKeyException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            throw new InvalidKeySpecException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        if (key instanceof ECPublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            ECPublicKey ecKey = (ECPublicKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            if (ECPublicKeySpec.class.isAssignableFrom(keySpec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                return (T) new ECPublicKeySpec(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                    ecKey.getW(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                    ecKey.getParams()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            } else if (X509EncodedKeySpec.class.isAssignableFrom(keySpec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                return (T) new X509EncodedKeySpec(key.getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                        ("KeySpec must be ECPublicKeySpec or "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                        + "X509EncodedKeySpec for EC public keys");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        } else if (key instanceof ECPrivateKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            if (PKCS8EncodedKeySpec.class.isAssignableFrom(keySpec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                return (T) new PKCS8EncodedKeySpec(key.getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            } else if (ECPrivateKeySpec.class.isAssignableFrom(keySpec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                ECPrivateKey ecKey = (ECPrivateKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                return (T) new ECPrivateKeySpec(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                    ecKey.getS(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                    ecKey.getParams()
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
                throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                        ("KeySpec must be ECPrivateKeySpec or "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                        + "PKCS8EncodedKeySpec for EC private keys");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            // should not occur, caught in engineTranslateKey()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            throw new InvalidKeySpecException("Neither public nor private key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
}