src/jdk.crypto.ec/share/classes/sun/security/ec/ECKeyPairGenerator.java
author bpb
Fri, 14 Sep 2018 09:00:22 -0700
changeset 51746 07ae9da7a230
parent 47421 f9e03aef3a49
child 52946 752e57845ad2
permissions -rw-r--r--
8210741: Typo in Java API documentation of java.nio.file.Paths Reviewed-by: alanb, rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
     1
/*
45883
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 25859
diff changeset
     2
 * Copyright (c) 2009, 2017, Oracle and/or its affiliates. All rights reserved.
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
     4
 *
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
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: 3863
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3863
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    10
 *
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    15
 * accompanied this code).
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    16
 *
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3863
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3863
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3863
diff changeset
    23
 * questions.
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    24
 */
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    25
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    26
package sun.security.ec;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    27
45883
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 25859
diff changeset
    28
import java.io.IOException;
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    29
import java.math.BigInteger;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    30
import java.security.*;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    31
import java.security.spec.AlgorithmParameterSpec;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    32
import java.security.spec.ECGenParameterSpec;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    33
import java.security.spec.ECParameterSpec;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    34
import java.security.spec.ECPoint;
45883
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 25859
diff changeset
    35
import java.security.spec.InvalidParameterSpecException;
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    36
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    37
import sun.security.ec.ECPrivateKeyImpl;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    38
import sun.security.ec.ECPublicKeyImpl;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    39
import sun.security.jca.JCAUtil;
25669
daa21271c03b 8035166: Remove dependency on EC classes from pkcs11 provider
valeriep
parents: 25530
diff changeset
    40
import sun.security.util.ECParameters;
17491
7a33824ec8c5 7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents: 9508
diff changeset
    41
import sun.security.util.ECUtil;
47421
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
    42
import static sun.security.util.SecurityProviderConstants.DEF_EC_KEY_SIZE;
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    43
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    44
/**
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    45
 * EC keypair generator.
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    46
 * Standard algorithm, minimum key length is 112 bits, maximum is 571 bits.
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    47
 *
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    48
 * @since 1.7
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    49
 */
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    50
public final class ECKeyPairGenerator extends KeyPairGeneratorSpi {
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    51
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    52
    private static final int KEY_SIZE_MIN = 112; // min bits (see ecc_impl.h)
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    53
    private static final int KEY_SIZE_MAX = 571; // max bits (see ecc_impl.h)
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    54
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    55
    // used to seed the keypair generator
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    56
    private SecureRandom random;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    57
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    58
    // size of the key to generate, KEY_SIZE_MIN <= keySize <= KEY_SIZE_MAX
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    59
    private int keySize;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    60
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    61
    // parameters specified via init, if any
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    62
    private AlgorithmParameterSpec params = null;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    63
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    64
    /**
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    65
     * Constructs a new ECKeyPairGenerator.
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    66
     */
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    67
    public ECKeyPairGenerator() {
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    68
        // initialize to default in case the app does not call initialize()
47421
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
    69
        initialize(DEF_EC_KEY_SIZE, null);
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    70
    }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    71
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    72
    // initialize the generator. See JCA doc
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    73
    @Override
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    74
    public void initialize(int keySize, SecureRandom random) {
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    75
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    76
        checkKeySize(keySize);
17491
7a33824ec8c5 7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents: 9508
diff changeset
    77
        this.params = ECUtil.getECParameterSpec(null, keySize);
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    78
        if (params == null) {
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    79
            throw new InvalidParameterException(
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    80
                "No EC parameters available for key size " + keySize + " bits");
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    81
        }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    82
        this.random = random;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    83
    }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    84
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    85
    // second initialize method. See JCA doc
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    86
    @Override
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    87
    public void initialize(AlgorithmParameterSpec params, SecureRandom random)
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    88
            throws InvalidAlgorithmParameterException {
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    89
45883
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 25859
diff changeset
    90
        ECParameterSpec ecSpec = null;
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 25859
diff changeset
    91
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    92
        if (params instanceof ECParameterSpec) {
45883
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 25859
diff changeset
    93
            ecSpec = ECUtil.getECParameterSpec(null,
17491
7a33824ec8c5 7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents: 9508
diff changeset
    94
                                                    (ECParameterSpec)params);
45883
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 25859
diff changeset
    95
            if (ecSpec == null) {
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    96
                throw new InvalidAlgorithmParameterException(
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    97
                    "Unsupported curve: " + params);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    98
            }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    99
        } else if (params instanceof ECGenParameterSpec) {
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   100
            String name = ((ECGenParameterSpec)params).getName();
45883
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 25859
diff changeset
   101
            ecSpec = ECUtil.getECParameterSpec(null, name);
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 25859
diff changeset
   102
            if (ecSpec == null) {
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   103
                throw new InvalidAlgorithmParameterException(
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   104
                    "Unknown curve name: " + name);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   105
            }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   106
        } else {
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   107
            throw new InvalidAlgorithmParameterException(
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   108
                "ECParameterSpec or ECGenParameterSpec required for EC");
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   109
        }
45883
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 25859
diff changeset
   110
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 25859
diff changeset
   111
        // Not all known curves are supported by the native implementation
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 25859
diff changeset
   112
        ensureCurveIsSupported(ecSpec);
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 25859
diff changeset
   113
        this.params = ecSpec;
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 25859
diff changeset
   114
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   115
        this.keySize =
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   116
            ((ECParameterSpec)this.params).getCurve().getField().getFieldSize();
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   117
        this.random = random;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   118
    }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   119
45883
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 25859
diff changeset
   120
    private static void ensureCurveIsSupported(ECParameterSpec ecSpec)
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 25859
diff changeset
   121
        throws InvalidAlgorithmParameterException {
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 25859
diff changeset
   122
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 25859
diff changeset
   123
        AlgorithmParameters ecParams = ECUtil.getECParameters(null);
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 25859
diff changeset
   124
        byte[] encodedParams;
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 25859
diff changeset
   125
        try {
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 25859
diff changeset
   126
            ecParams.init(ecSpec);
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 25859
diff changeset
   127
            encodedParams = ecParams.getEncoded();
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 25859
diff changeset
   128
        } catch (InvalidParameterSpecException ex) {
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 25859
diff changeset
   129
            throw new InvalidAlgorithmParameterException(
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 25859
diff changeset
   130
                "Unsupported curve: " + ecSpec.toString());
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 25859
diff changeset
   131
        } catch (IOException ex) {
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 25859
diff changeset
   132
            throw new RuntimeException(ex);
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 25859
diff changeset
   133
        }
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 25859
diff changeset
   134
        if (!isCurveSupported(encodedParams)) {
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 25859
diff changeset
   135
            throw new InvalidAlgorithmParameterException(
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 25859
diff changeset
   136
                "Unsupported curve: " + ecParams.toString());
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 25859
diff changeset
   137
        }
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 25859
diff changeset
   138
    }
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 25859
diff changeset
   139
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   140
    // generate the keypair. See JCA doc
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   141
    @Override
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   142
    public KeyPair generateKeyPair() {
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   143
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   144
        byte[] encodedParams =
17491
7a33824ec8c5 7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents: 9508
diff changeset
   145
            ECUtil.encodeECParameterSpec(null, (ECParameterSpec)params);
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   146
3863
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents: 3492
diff changeset
   147
        // seed is twice the key size (in bytes) plus 1
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents: 3492
diff changeset
   148
        byte[] seed = new byte[(((keySize + 7) >> 3) + 1) * 2];
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   149
        if (random == null) {
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   150
            random = JCAUtil.getSecureRandom();
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   151
        }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   152
        random.nextBytes(seed);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   153
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
   154
        try {
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   155
25530
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 23010
diff changeset
   156
            Object[] keyBytes = generateECKeyPair(keySize, encodedParams, seed);
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   157
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
   158
            // The 'params' object supplied above is equivalent to the native
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
   159
            // one so there is no need to fetch it.
25530
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 23010
diff changeset
   160
            // keyBytes[0] is the encoding of the native private key
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 23010
diff changeset
   161
            BigInteger s = new BigInteger(1, (byte[])keyBytes[0]);
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
   162
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   163
            PrivateKey privateKey =
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   164
                new ECPrivateKeyImpl(s, (ECParameterSpec)params);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   165
25530
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 23010
diff changeset
   166
            // keyBytes[1] is the encoding of the native public key
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 23010
diff changeset
   167
            ECPoint w = ECUtil.decodePoint((byte[])keyBytes[1],
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   168
                ((ECParameterSpec)params).getCurve());
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   169
            PublicKey publicKey =
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   170
                new ECPublicKeyImpl(w, (ECParameterSpec)params);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   171
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   172
            return new KeyPair(publicKey, privateKey);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   173
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   174
        } catch (Exception e) {
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   175
            throw new ProviderException(e);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   176
        }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   177
    }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   178
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   179
    private void checkKeySize(int keySize) throws InvalidParameterException {
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   180
        if (keySize < KEY_SIZE_MIN) {
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   181
            throw new InvalidParameterException
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   182
                ("Key size must be at least " + KEY_SIZE_MIN + " bits");
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   183
        }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   184
        if (keySize > KEY_SIZE_MAX) {
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   185
            throw new InvalidParameterException
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   186
                ("Key size must be at most " + KEY_SIZE_MAX + " bits");
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   187
        }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   188
        this.keySize = keySize;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   189
    }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   190
45883
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 25859
diff changeset
   191
    /**
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 25859
diff changeset
   192
     * Checks whether the curve in the encoded parameters is supported by the
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 25859
diff changeset
   193
     * native implementation.
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 25859
diff changeset
   194
     *
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 25859
diff changeset
   195
     * @param encodedParams encoded parameters in the same form accepted
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 25859
diff changeset
   196
     *    by generateECKeyPair
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 25859
diff changeset
   197
     * @return true if and only if generateECKeyPair will succeed for
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 25859
diff changeset
   198
     *    the supplied parameters
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 25859
diff changeset
   199
     */
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 25859
diff changeset
   200
    private static native boolean isCurveSupported(byte[] encodedParams);
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 25859
diff changeset
   201
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   202
    /*
25530
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 23010
diff changeset
   203
     * Generates the keypair and returns a 2-element array of encoding bytes.
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 23010
diff changeset
   204
     * The first one is for the private key, the second for the public key.
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   205
     */
25530
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 23010
diff changeset
   206
    private static native Object[] generateECKeyPair(int keySize,
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
   207
        byte[] encodedParams, byte[] seed) throws GeneralSecurityException;
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   208
}