src/jdk.crypto.ec/share/classes/sun/security/ec/ECPrivateKeyImpl.java
author bpb
Fri, 14 Sep 2018 09:00:22 -0700
changeset 51746 07ae9da7a230
parent 47216 71c04702a3d5
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:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
20813
0ad12d66a652 8011071: Better crypto provider handling
ascarpino
parents: 17491
diff changeset
     2
 * Copyright (c) 2006, 2013, 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.interfaces.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
25669
daa21271c03b 8035166: Remove dependency on EC classes from pkcs11 provider
valeriep
parents: 20813
diff changeset
    35
import sun.security.util.DerInputStream;
daa21271c03b 8035166: Remove dependency on EC classes from pkcs11 provider
valeriep
parents: 20813
diff changeset
    36
import sun.security.util.DerOutputStream;
daa21271c03b 8035166: Remove dependency on EC classes from pkcs11 provider
valeriep
parents: 20813
diff changeset
    37
import sun.security.util.DerValue;
daa21271c03b 8035166: Remove dependency on EC classes from pkcs11 provider
valeriep
parents: 20813
diff changeset
    38
import sun.security.util.ECParameters;
daa21271c03b 8035166: Remove dependency on EC classes from pkcs11 provider
valeriep
parents: 20813
diff changeset
    39
import sun.security.util.ECUtil;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.security.x509.AlgorithmId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.security.pkcs.PKCS8Key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * Key implementation for EC private keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * ASN.1 syntax for EC private keys from SEC 1 v1.5 (draft):
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * EXPLICIT TAGS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * ECPrivateKey ::= SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *   version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *   privateKey OCTET STRING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *   parameters [0] ECDomainParameters {{ SECGCurveNames }} OPTIONAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *   publicKey [1] BIT STRING OPTIONAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * We currently ignore the optional parameters and publicKey fields. We
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * require that the parameters are encoded as part of the AlgorithmIdentifier,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * not in the private key structure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * @since   1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
public final class ECPrivateKeyImpl extends PKCS8Key implements ECPrivateKey {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private static final long serialVersionUID = 88695385615075129L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private BigInteger s;       // private value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private ECParameterSpec params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
17491
7a33824ec8c5 7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents: 5506
diff changeset
    74
     * Construct a key from its encoding. Called by the ECKeyFactory.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
17491
7a33824ec8c5 7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents: 5506
diff changeset
    76
    ECPrivateKeyImpl(byte[] encoded) throws InvalidKeyException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        decode(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * Construct a key from its components. Used by the
17491
7a33824ec8c5 7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents: 5506
diff changeset
    82
     * KeyFactory.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
17491
7a33824ec8c5 7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents: 5506
diff changeset
    84
    ECPrivateKeyImpl(BigInteger s, ECParameterSpec params)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        this.s = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        this.params = params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        // generate the encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        algid = new AlgorithmId
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            (AlgorithmId.EC_oid, ECParameters.getAlgorithmParameters(params));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            DerOutputStream out = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            out.putInteger(1); // version 1
17491
7a33824ec8c5 7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents: 5506
diff changeset
    94
            byte[] privBytes = ECUtil.trimZeroes(s.toByteArray());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            out.putOctetString(privBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            DerValue val =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                new DerValue(DerValue.tag_Sequence, out.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            key = val.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        } catch (IOException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            // should never occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            throw new InvalidKeyException(exc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    // see JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public String getAlgorithm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        return "EC";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    // see JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public BigInteger getS() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    // see JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public ECParameterSpec getParams() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        return params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Parse the key. Called by PKCS8Key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    protected void parseKeyBits() throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            DerInputStream in = new DerInputStream(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            DerValue derValue = in.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            if (derValue.tag != DerValue.tag_Sequence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                throw new IOException("Not a SEQUENCE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            DerInputStream data = derValue.data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            int version = data.getInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            if (version != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                throw new IOException("Version must be 1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            byte[] privData = data.getOctetString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            s = new BigInteger(1, privData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            while (data.available() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                DerValue value = data.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                if (value.isContextSpecific((byte)0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                    // ignore for now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                } else if (value.isContextSpecific((byte)1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    // ignore for now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    throw new InvalidKeyException("Unexpected value: " + value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            AlgorithmParameters algParams = this.algid.getParameters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            if (algParams == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                throw new InvalidKeyException("EC domain parameters must be "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    + "encoded in the algorithm identifier");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            params = algParams.getParameterSpec(ECParameterSpec.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            throw new InvalidKeyException("Invalid EC private key", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        } catch (InvalidParameterSpecException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            throw new InvalidKeyException("Invalid EC private key", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
}