src/java.base/share/classes/sun/security/pkcs/PKCS8Key.java
author darcy
Thu, 29 Aug 2019 10:52:21 -0700
changeset 57950 4612a3cfb927
parent 48080 18a4438eb690
permissions -rw-r--r--
8229999: Apply java.io.Serial annotations to security types in java.base Reviewed-by: rriggs, mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
57950
4612a3cfb927 8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents: 48080
diff changeset
     2
 * Copyright (c) 1996, 2019, 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.pkcs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Properties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.math.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.Key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.KeyRep;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.PrivateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.KeyFactory;
45986
b9015c99d2dd 8176760: Better handling of PKCS8 material
valeriep
parents: 37782
diff changeset
    35
import java.security.MessageDigest;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.Security;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.Provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.InvalidKeyException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.NoSuchAlgorithmException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.security.spec.InvalidKeySpecException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.security.spec.PKCS8EncodedKeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
34687
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 31538
diff changeset
    43
import sun.security.util.HexDumpEncoder;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import sun.security.x509.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * Holds a PKCS#8 key, for example a private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @author Dave Brownell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @author Benjamin Renaud
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
public class PKCS8Key implements PrivateKey {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /** use serialVersionUID from JDK 1.1. for interoperability */
57950
4612a3cfb927 8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents: 48080
diff changeset
    56
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private static final long serialVersionUID = -3836890099307167124L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /* The algorithm information (name, parameters, etc). */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    protected AlgorithmId algid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /* The key bytes, without the algorithm information */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    protected byte[] key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /* The encoded for the key. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    protected byte[] encodedKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /* The version for this key */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public static final BigInteger version = BigInteger.ZERO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * Default constructor.  The key constructed must have its key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * and algorithm initialized before it may be used, for example
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * by using <code>decode</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public PKCS8Key() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Build and initialize as a "default" key.  All PKCS#8 key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * data is stored and transmitted losslessly, but no knowledge
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * about this particular algorithm is available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 25859
diff changeset
    83
    private PKCS8Key (AlgorithmId algid, byte[] key)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        this.algid = algid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        this.key = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        encode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Binary backwards compatibility. New uses should call parseKey().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public static PKCS8Key parse (DerValue in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        PrivateKey key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        key = parseKey(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (key instanceof PKCS8Key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            return (PKCS8Key)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        throw new IOException("Provider did not return PKCS8Key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * Construct PKCS#8 subject public key from a DER value.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * the runtime environment is configured with a specific class for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * this kind of key, a subclass is returned.  Otherwise, a generic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * PKCS8Key object is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * <P>This mechanism gurantees that keys (and algorithms) may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * freely manipulated and transferred, without risk of losing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * information.  Also, when a key (or algorithm) needs some special
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * handling, that specific need can be accomodated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @param in the DER-encoded SubjectPublicKeyInfo value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @exception IOException on data format errors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public static PrivateKey parseKey (DerValue in) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        AlgorithmId algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        PrivateKey privKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        if (in.tag != DerValue.tag_Sequence)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            throw new IOException ("corrupt private key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        BigInteger parsedVersion = in.data.getBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (!version.equals(parsedVersion)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            throw new IOException("version mismatch: (supported: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                                  Debug.toHexString(version) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                                  ", parsed: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                                  Debug.toHexString(parsedVersion));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        algorithm = AlgorithmId.parse (in.data.getDerValue ());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            privKey = buildPKCS8Key (algorithm, in.data.getOctetString ());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        } catch (InvalidKeyException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            throw new IOException("corrupt private key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if (in.data.available () != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            throw new IOException ("excess private key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        return privKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Parse the key bits.  This may be redefined by subclasses to take
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * advantage of structure within the key.  For example, RSA public
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * keys encapsulate two unsigned integers (modulus and exponent) as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * DER values within the <code>key</code> bits; Diffie-Hellman and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * DSS/DSA keys encapsulate a single unsigned integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * <P>This function is called when creating PKCS#8 SubjectPublicKeyInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * values using the PKCS8Key member functions, such as <code>parse</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * and <code>decode</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @exception IOException if a parsing error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @exception InvalidKeyException if the key encoding is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    protected void parseKeyBits () throws IOException, InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        encode();
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
     * Factory interface, building the kind of key associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * specific algorithm ID or else returning this generic base class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * See the description above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    static PrivateKey buildPKCS8Key (AlgorithmId algid, byte[] key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    throws IOException, InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
         * Use the algid and key parameters to produce the ASN.1 encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
         * of the key, which will then be used as the input to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
         * key factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        DerOutputStream pkcs8EncodedKeyStream = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        encode(pkcs8EncodedKeyStream, algid, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        PKCS8EncodedKeySpec pkcs8KeySpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            = new PKCS8EncodedKeySpec(pkcs8EncodedKeyStream.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            // Instantiate the key factory of the appropriate algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            KeyFactory keyFac = KeyFactory.getInstance(algid.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            // Generate the private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            return keyFac.generatePrivate(pkcs8KeySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            // Return generic PKCS8Key with opaque key data (see below)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        } catch (InvalidKeySpecException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            // Return generic PKCS8Key with opaque key data (see below)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
         * Try again using JDK1.1-style for backwards compatibility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        String classname = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            Properties props;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            String keytype;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            Provider sunProvider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            sunProvider = Security.getProvider("SUN");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            if (sunProvider == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                throw new InstantiationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            classname = sunProvider.getProperty("PrivateKey.PKCS#8." +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
              algid.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            if (classname == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                throw new InstantiationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   213
            Class<?> keyClass = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                keyClass = Class.forName(classname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                ClassLoader cl = ClassLoader.getSystemClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                if (cl != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                    keyClass = cl.loadClass(classname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
37782
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 34687
diff changeset
   223
            @SuppressWarnings("deprecation")
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 34687
diff changeset
   224
            Object      inst = (keyClass != null) ? keyClass.newInstance() : null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            PKCS8Key    result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            if (inst instanceof PKCS8Key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                result = (PKCS8Key) inst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                result.algid = algid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                result.key = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                result.parseKeyBits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        } catch (InstantiationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        } catch (IllegalAccessException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            // this should not happen.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            throw new IOException (classname + " [internal error]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        PKCS8Key result = new PKCS8Key();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        result.algid = algid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        result.key = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * Returns the algorithm to be used with this key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    public String getAlgorithm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        return algid.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * Returns the algorithm ID to be used with this key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    public AlgorithmId  getAlgorithmId () { return algid; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * PKCS#8 sequence on the DER output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    public final void encode(DerOutputStream out) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        encode(out, this.algid, this.key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * Returns the DER-encoded form of the key as a byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    public synchronized byte[] getEncoded() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        byte[] result = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            result = encode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        } catch (InvalidKeyException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * Returns the format for this key: "PKCS#8"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    public String getFormat() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        return "PKCS#8";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * Returns the DER-encoded form of the key as a byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * @exception InvalidKeyException if an encoding error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    public byte[] encode() throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        if (encodedKey == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                DerOutputStream out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                out = new DerOutputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                encode (out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                encodedKey = out.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                throw new InvalidKeyException ("IOException : " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                                               e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        return encodedKey.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * Initialize an PKCS8Key object from an input stream.  The data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * on that input stream must be encoded using DER, obeying the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * PKCS#8 format: a sequence consisting of a version, an algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * ID and a bit string which holds the key.  (That bit string is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * often used to encapsulate another DER encoded sequence.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * <P>Subclasses should not normally redefine this method; they should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * instead provide a <code>parseKeyBits</code> method to parse any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * fields inside the <code>key</code> member.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * @param in an input stream with a DER-encoded PKCS#8
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * SubjectPublicKeyInfo value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * @exception InvalidKeyException if a parsing error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    public void decode(InputStream in) throws InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        DerValue        val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            val = new DerValue (in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            if (val.tag != DerValue.tag_Sequence)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                throw new InvalidKeyException ("invalid key format");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            BigInteger version = val.data.getBigInteger();
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   335
            if (!version.equals(PKCS8Key.version)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                throw new IOException("version mismatch: (supported: " +
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   337
                                      Debug.toHexString(PKCS8Key.version) +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                                      ", parsed: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                                      Debug.toHexString(version));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            algid = AlgorithmId.parse (val.data.getDerValue ());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            key = val.data.getOctetString ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            parseKeyBits ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            if (val.data.available () != 0)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                // OPTIONAL attributes not supported yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            throw new InvalidKeyException("IOException : " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                                          e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    public void decode(byte[] encodedKey) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        decode(new ByteArrayInputStream(encodedKey));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
57950
4612a3cfb927 8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents: 48080
diff changeset
   359
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    protected Object writeReplace() throws java.io.ObjectStreamException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        return new KeyRep(KeyRep.Type.PRIVATE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                        getAlgorithm(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                        getFormat(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                        getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * Serialization read ... PKCS#8 keys serialize as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * themselves, and they're parsed when they get read back.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     */
57950
4612a3cfb927 8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents: 48080
diff changeset
   371
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    private void readObject (ObjectInputStream stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            decode(stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        } catch (InvalidKeyException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            throw new IOException("deserialized key is invalid: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                                  e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * Produce PKCS#8 encoding from algorithm id and key material.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    static void encode(DerOutputStream out, AlgorithmId algid, byte[] key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            DerOutputStream tmp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            tmp.putInteger(version);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            algid.encode(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            tmp.putOctetString(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            out.write(DerValue.tag_Sequence, tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * Compares two private keys. This returns false if the object with which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * to compare is not of type <code>Key</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * Otherwise, the encoding of this key object is compared with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * encoding of the given key object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * @param object the object with which to compare
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * @return <code>true</code> if this key has the same encoding as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * object argument; <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    public boolean equals(Object object) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        if (this == object) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        if (object instanceof Key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            // this encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            byte[] b1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            if (encodedKey != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                b1 = encodedKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                b1 = getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            // that encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            byte[] b2 = ((Key)object).getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
45986
b9015c99d2dd 8176760: Better handling of PKCS8 material
valeriep
parents: 37782
diff changeset
   425
            // time-constant comparison
b9015c99d2dd 8176760: Better handling of PKCS8 material
valeriep
parents: 37782
diff changeset
   426
            return MessageDigest.isEqual(b1, b2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * Calculates a hash code value for this object. Objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * which are equal will also have the same hashcode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        int retval = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        byte[] b1 = getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        for (int i = 1; i < b1.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            retval += b1[i] * i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        return(retval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
}