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