jdk/src/share/classes/sun/security/pkcs/PKCS8Key.java
author jjg
Mon, 15 Aug 2011 11:48:20 -0700
changeset 10336 0bb1999251f8
parent 5506 202f599c92aa
child 20813 0ad12d66a652
permissions -rw-r--r--
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror Reviewed-by: xuelei, mullan Contributed-by: alexandre.boulgakov@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
     2
 * Copyright (c) 1996, 2011, 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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.Security;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.Provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.InvalidKeyException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.NoSuchAlgorithmException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.spec.InvalidKeySpecException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.security.spec.PKCS8EncodedKeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import sun.misc.HexDumpEncoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import sun.security.x509.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * Holds a PKCS#8 key, for example a private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @author Dave Brownell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @author Benjamin Renaud
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
public class PKCS8Key implements PrivateKey {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /** use serialVersionUID from JDK 1.1. for interoperability */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private static final long serialVersionUID = -3836890099307167124L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /* The algorithm information (name, parameters, etc). */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    protected AlgorithmId algid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /* The key bytes, without the algorithm information */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    protected byte[] key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /* The encoded for the key. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    protected byte[] encodedKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /* The version for this key */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public static final BigInteger version = BigInteger.ZERO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Default constructor.  The key constructed must have its key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * and algorithm initialized before it may be used, for example
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * by using <code>decode</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public PKCS8Key() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * Build and initialize as a "default" key.  All PKCS#8 key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * data is stored and transmitted losslessly, but no knowledge
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * about this particular algorithm is available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private PKCS8Key (AlgorithmId algid, byte key [])
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        this.algid = algid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        this.key = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        encode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * Binary backwards compatibility. New uses should call parseKey().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public static PKCS8Key parse (DerValue in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        PrivateKey key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        key = parseKey(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        if (key instanceof PKCS8Key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            return (PKCS8Key)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        throw new IOException("Provider did not return PKCS8Key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Construct PKCS#8 subject public key from a DER value.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * the runtime environment is configured with a specific class for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * this kind of key, a subclass is returned.  Otherwise, a generic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * PKCS8Key object is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * <P>This mechanism gurantees that keys (and algorithms) may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * freely manipulated and transferred, without risk of losing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * information.  Also, when a key (or algorithm) needs some special
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * handling, that specific need can be accomodated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @param in the DER-encoded SubjectPublicKeyInfo value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @exception IOException on data format errors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public static PrivateKey parseKey (DerValue in) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        AlgorithmId algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        PrivateKey privKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (in.tag != DerValue.tag_Sequence)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            throw new IOException ("corrupt private key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        BigInteger parsedVersion = in.data.getBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (!version.equals(parsedVersion)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            throw new IOException("version mismatch: (supported: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                                  Debug.toHexString(version) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                                  ", parsed: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                                  Debug.toHexString(parsedVersion));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        algorithm = AlgorithmId.parse (in.data.getDerValue ());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            privKey = buildPKCS8Key (algorithm, in.data.getOctetString ());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        } catch (InvalidKeyException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            throw new IOException("corrupt private key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        if (in.data.available () != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            throw new IOException ("excess private key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        return privKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Parse the key bits.  This may be redefined by subclasses to take
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * advantage of structure within the key.  For example, RSA public
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * keys encapsulate two unsigned integers (modulus and exponent) as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * DER values within the <code>key</code> bits; Diffie-Hellman and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * DSS/DSA keys encapsulate a single unsigned integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * <P>This function is called when creating PKCS#8 SubjectPublicKeyInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * values using the PKCS8Key member functions, such as <code>parse</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * and <code>decode</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @exception IOException if a parsing error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @exception InvalidKeyException if the key encoding is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    protected void parseKeyBits () throws IOException, InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        encode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Factory interface, building the kind of key associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * specific algorithm ID or else returning this generic base class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * See the description above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    static PrivateKey buildPKCS8Key (AlgorithmId algid, byte[] key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    throws IOException, InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
         * Use the algid and key parameters to produce the ASN.1 encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
         * of the key, which will then be used as the input to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
         * key factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        DerOutputStream pkcs8EncodedKeyStream = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        encode(pkcs8EncodedKeyStream, algid, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        PKCS8EncodedKeySpec pkcs8KeySpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            = new PKCS8EncodedKeySpec(pkcs8EncodedKeyStream.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            // Instantiate the key factory of the appropriate algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            KeyFactory keyFac = KeyFactory.getInstance(algid.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            // Generate the private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            return keyFac.generatePrivate(pkcs8KeySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            // Return generic PKCS8Key with opaque key data (see below)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        } catch (InvalidKeySpecException 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
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
         * Try again using JDK1.1-style for backwards compatibility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        String classname = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            Properties props;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            String keytype;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            Provider sunProvider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            sunProvider = Security.getProvider("SUN");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            if (sunProvider == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                throw new InstantiationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            classname = sunProvider.getProperty("PrivateKey.PKCS#8." +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
              algid.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            if (classname == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                throw new InstantiationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   211
            Class<?> keyClass = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                keyClass = Class.forName(classname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                ClassLoader cl = ClassLoader.getSystemClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                if (cl != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                    keyClass = cl.loadClass(classname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            Object      inst = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            PKCS8Key    result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            if (keyClass != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                inst = keyClass.newInstance();
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
     * Returns a printable representation of the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    public String toString ()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        HexDumpEncoder  encoder = new HexDumpEncoder ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        return "algorithm = " + algid.toString ()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            + ", unparsed keybits = \n" + encoder.encodeBuffer (key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * Initialize an PKCS8Key object from an input stream.  The data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * on that input stream must be encoded using DER, obeying the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * PKCS#8 format: a sequence consisting of a version, an algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * ID and a bit string which holds the key.  (That bit string is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * often used to encapsulate another DER encoded sequence.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * <P>Subclasses should not normally redefine this method; they should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * instead provide a <code>parseKeyBits</code> method to parse any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * fields inside the <code>key</code> member.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * @param in an input stream with a DER-encoded PKCS#8
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * SubjectPublicKeyInfo value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * @exception InvalidKeyException if a parsing error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    public void decode(InputStream in) throws InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        DerValue        val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            val = new DerValue (in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            if (val.tag != DerValue.tag_Sequence)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                throw new InvalidKeyException ("invalid key format");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            BigInteger version = val.data.getBigInteger();
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   345
            if (!version.equals(PKCS8Key.version)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                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
   347
                                      Debug.toHexString(PKCS8Key.version) +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                                      ", parsed: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                                      Debug.toHexString(version));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            algid = AlgorithmId.parse (val.data.getDerValue ());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            key = val.data.getOctetString ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            parseKeyBits ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            if (val.data.available () != 0)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                // OPTIONAL attributes not supported yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            // e.printStackTrace ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            throw new InvalidKeyException("IOException : " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                                          e.getMessage());
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
    public void decode(byte[] encodedKey) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        decode(new ByteArrayInputStream(encodedKey));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    protected Object writeReplace() throws java.io.ObjectStreamException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        return new KeyRep(KeyRep.Type.PRIVATE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                        getAlgorithm(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                        getFormat(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                        getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * Serialization read ... PKCS#8 keys serialize as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * themselves, and they're parsed when they get read back.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    private void readObject (ObjectInputStream stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            decode(stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        } catch (InvalidKeyException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            throw new IOException("deserialized key is invalid: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                                  e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        }
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
     * Produce PKCS#8 encoding from algorithm id and key material.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    static void encode(DerOutputStream out, AlgorithmId algid, byte[] key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            DerOutputStream tmp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            tmp.putInteger(version);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            algid.encode(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            tmp.putOctetString(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            out.write(DerValue.tag_Sequence, tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * Compares two private keys. This returns false if the object with which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * to compare is not of type <code>Key</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * Otherwise, the encoding of this key object is compared with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * encoding of the given key object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * @param object the object with which to compare
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * @return <code>true</code> if this key has the same encoding as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * object argument; <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    public boolean equals(Object object) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        if (this == object) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        if (object instanceof Key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            // this encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            byte[] b1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            if (encodedKey != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                b1 = encodedKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                b1 = getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            // that encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            byte[] b2 = ((Key)object).getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            // do the comparison
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            if (b1.length != b2.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            for (i = 0; i < b1.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                if (b1[i] != b2[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * Calculates a hash code value for this object. Objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * which are equal will also have the same hashcode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        int retval = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        byte[] b1 = getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        for (int i = 1; i < b1.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            retval += b1[i] * i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        return(retval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
}