jdk/src/share/classes/sun/security/ec/ECParameters.java
author mullan
Thu, 30 Aug 2012 14:40:57 -0700
changeset 13661 7c894680910a
parent 12436 1dc3d95b3bc9
child 13813 ca3a2b5731d0
permissions -rw-r--r--
6995421: Eliminate the static dependency to sun.security.ec.ECKeyFactory Reviewed-by: mullan, vinnie Contributed-by: stephen.flores@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
13661
7c894680910a 6995421: Eliminate the static dependency to sun.security.ec.ECKeyFactory
mullan
parents: 12436
diff changeset
     2
 * Copyright (c) 2006, 2012, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.ec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * This class implements encoding and decoding of Elliptic Curve parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * as specified in RFC 3279.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * However, only named curves are currently supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * ASN.1 from RFC 3279 follows. Note that X9.62 (2005) has added some additional
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * options.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *    EcpkParameters ::= CHOICE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *      ecParameters  ECParameters,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *      namedCurve    OBJECT IDENTIFIER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *      implicitlyCA  NULL }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *    ECParameters ::= SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *       version   ECPVer,          -- version is always 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *       fieldID   FieldID,         -- identifies the finite field over
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *                                  -- which the curve is defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *       curve     Curve,           -- coefficients a and b of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *                                  -- elliptic curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *       base      ECPoint,         -- specifies the base point P
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *                                  -- on the elliptic curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *       order     INTEGER,         -- the order n of the base point
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *       cofactor  INTEGER OPTIONAL -- The integer h = #E(Fq)/n
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *    ECPVer ::= INTEGER {ecpVer1(1)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *    Curve ::= SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *       a         FieldElement,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *       b         FieldElement,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *       seed      BIT STRING OPTIONAL }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *    FieldElement ::= OCTET STRING
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *    ECPoint ::= OCTET STRING
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * @since   1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
public final class ECParameters extends AlgorithmParametersSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public ECParameters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    // Used by SunPKCS11 and SunJSSE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public static ECPoint decodePoint(byte[] data, EllipticCurve curve)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        if ((data.length == 0) || (data[0] != 4)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            throw new IOException("Only uncompressed point format supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
12436
1dc3d95b3bc9 7152169: TEST_BUG: sun/security/tools/jarsigner/ec.sh occasionally fail due to keytool error on sparc
vinnie
parents: 10336
diff changeset
    90
        int n = data.length / 2;
1dc3d95b3bc9 7152169: TEST_BUG: sun/security/tools/jarsigner/ec.sh occasionally fail due to keytool error on sparc
vinnie
parents: 10336
diff changeset
    91
        if (n > ((curve.getField().getFieldSize() + 7 ) >> 3)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            throw new IOException("Point does not match field size");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        byte[] xb = new byte[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        byte[] yb = new byte[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        System.arraycopy(data, 1, xb, 0, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        System.arraycopy(data, n + 1, yb, 0, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        return new ECPoint(new BigInteger(1, xb), new BigInteger(1, yb));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    // Used by SunPKCS11 and SunJSSE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    public static byte[] encodePoint(ECPoint point, EllipticCurve curve) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        // get field size in bytes (rounding up)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        int n = (curve.getField().getFieldSize() + 7) >> 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        byte[] xb = trimZeroes(point.getAffineX().toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        byte[] yb = trimZeroes(point.getAffineY().toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        if ((xb.length > n) || (yb.length > n)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            throw new RuntimeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                ("Point coordinates do not match field size");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        byte[] b = new byte[1 + (n << 1)];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        b[0] = 4; // uncompressed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        System.arraycopy(xb, 0, b, n - xb.length + 1, xb.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        System.arraycopy(yb, 0, b, b.length - yb.length, yb.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    // Copied from the SunPKCS11 code - should be moved to a common location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    // trim leading (most significant) zeroes from the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    static byte[] trimZeroes(byte[] b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        while ((i < b.length - 1) && (b[i] == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (i == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        byte[] t = new byte[b.length - i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        System.arraycopy(b, i, t, 0, t.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        return t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    // Convert the given ECParameterSpec object to a NamedCurve object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    // If params does not represent a known named curve, return null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    // Used by SunPKCS11.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public static NamedCurve getNamedCurve(ECParameterSpec params) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if ((params instanceof NamedCurve) || (params == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            return (NamedCurve)params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        // This is a hack to allow SunJSSE to work with 3rd party crypto
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        // providers for ECC and not just SunPKCS11.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        // This can go away once we decide how to expose curve names in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        // public API.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        // Note that it assumes that the 3rd party provider encodes named
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        // curves using the short form, not explicitly. If it did that, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        // the SunJSSE TLS ECC extensions are wrong, which could lead to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        // interoperability problems.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        int fieldSize = params.getCurve().getField().getFieldSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        for (ECParameterSpec namedCurve : NamedCurve.knownECParameterSpecs()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            // ECParameterSpec does not define equals, so check all the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            // components ourselves.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            // Quick field size check first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            if (namedCurve.getCurve().getField().getFieldSize() != fieldSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            if (namedCurve.getCurve().equals(params.getCurve()) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            if (namedCurve.getGenerator().equals(params.getGenerator()) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            if (namedCurve.getOrder().equals(params.getOrder()) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            if (namedCurve.getCofactor() != params.getCofactor()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            // everything matches our named curve, return it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            return (NamedCurve)namedCurve;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        // no match found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    // Used by SunJSSE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    public static String getCurveName(ECParameterSpec params) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        NamedCurve curve = getNamedCurve(params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        return (curve == null) ? null : curve.getObjectIdentifier().toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    // Used by SunPKCS11.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public static byte[] encodeParameters(ECParameterSpec params) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        NamedCurve curve = getNamedCurve(params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if (curve == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            throw new RuntimeException("Not a known named curve: " + params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        return curve.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    // Used by SunPKCS11.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    public static ECParameterSpec decodeParameters(byte[] params) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        DerValue encodedParams = new DerValue(params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        if (encodedParams.tag == DerValue.tag_ObjectId) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            ObjectIdentifier oid = encodedParams.getOID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            ECParameterSpec spec = NamedCurve.getECParameterSpec(oid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            if (spec == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                throw new IOException("Unknown named curve: " + oid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            return spec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        throw new IOException("Only named ECParameters supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        // The code below is incomplete.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        // It is left as a starting point for a complete parsing implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        if (encodedParams.tag != DerValue.tag_Sequence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            throw new IOException("Unsupported EC parameters, tag: " + encodedParams.tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        encodedParams.data.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        DerInputStream in = encodedParams.data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        int version = in.getInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        if (version != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            throw new IOException("Unsupported EC parameters version: " + version);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        ECField field = parseField(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        EllipticCurve curve = parseCurve(in, field);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        ECPoint point = parsePoint(in, curve);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        BigInteger order = in.getBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        int cofactor = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (in.available() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            cofactor = in.getInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        // XXX HashAlgorithm optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        if (encodedParams.data.available() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            throw new IOException("encoded params have " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                                  encodedParams.data.available() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                                  " extra bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        return new ECParameterSpec(curve, point, order, cofactor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    private static final ObjectIdentifier fieldTypePrime =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        ObjectIdentifier.newInternal(new int[] {1, 2, 840, 10045, 1, 1});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    private static final ObjectIdentifier fieldTypeChar2 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        ObjectIdentifier.newInternal(new int[] {1, 2, 840, 10045, 1, 2});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    private static ECField parseField(DerInputStream in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        DerValue v = in.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        ObjectIdentifier oid = v.data.getOID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        if (oid.equals(fieldTypePrime) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            throw new IOException("Only prime fields supported: " + oid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        BigInteger fieldSize = v.data.getBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        return new ECFieldFp(fieldSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    private static EllipticCurve parseCurve(DerInputStream in, ECField field)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        DerValue v = in.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        byte[] ab = v.data.getOctetString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        byte[] bb = v.data.getOctetString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        return new EllipticCurve(field, new BigInteger(1, ab), new BigInteger(1, bb));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    private static ECPoint parsePoint(DerInputStream in, EllipticCurve curve)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        byte[] data = in.getOctetString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        return decodePoint(data, curve);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    // used by ECPublicKeyImpl and ECPrivateKeyImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    static AlgorithmParameters getAlgorithmParameters(ECParameterSpec spec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        try {
13661
7c894680910a 6995421: Eliminate the static dependency to sun.security.ec.ECKeyFactory
mullan
parents: 12436
diff changeset
   279
            AlgorithmParameters params =
7c894680910a 6995421: Eliminate the static dependency to sun.security.ec.ECKeyFactory
mullan
parents: 12436
diff changeset
   280
                AlgorithmParameters.getInstance("EC", "SunEC");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            params.init(spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            return params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            throw new InvalidKeyException("EC parameters error", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    // AlgorithmParameterSpi methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    // The parameters these AlgorithmParameters object represents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    // Currently, it is always an instance of NamedCurve.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    private ECParameterSpec paramSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    protected void engineInit(AlgorithmParameterSpec paramSpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            throws InvalidParameterSpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        if (paramSpec instanceof ECParameterSpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            this.paramSpec = getNamedCurve((ECParameterSpec)paramSpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            if (this.paramSpec == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                throw new InvalidParameterSpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                    ("Not a supported named curve: " + paramSpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        } else if (paramSpec instanceof ECGenParameterSpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            String name = ((ECGenParameterSpec)paramSpec).getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            ECParameterSpec spec = NamedCurve.getECParameterSpec(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            if (spec == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                throw new InvalidParameterSpecException("Unknown curve: " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            this.paramSpec = spec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        } else if (paramSpec == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            throw new InvalidParameterSpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                ("paramSpec must not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            throw new InvalidParameterSpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                ("Only ECParameterSpec and ECGenParameterSpec supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    protected void engineInit(byte[] params) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        paramSpec = decodeParameters(params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    protected void engineInit(byte[] params, String decodingMethod) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        engineInit(params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    protected <T extends AlgorithmParameterSpec> T engineGetParameterSpec(Class<T> spec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            throws InvalidParameterSpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        if (spec.isAssignableFrom(ECParameterSpec.class)) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   329
            return spec.cast(paramSpec);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        } else if (spec.isAssignableFrom(ECGenParameterSpec.class)) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   331
            return spec.cast(new ECGenParameterSpec(getCurveName(paramSpec)));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            throw new InvalidParameterSpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                ("Only ECParameterSpec and ECGenParameterSpec supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    protected byte[] engineGetEncoded() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        return encodeParameters(paramSpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    protected byte[] engineGetEncoded(String encodingMethod) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        return engineGetEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    protected String engineToString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        return paramSpec.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
}