src/java.base/share/classes/sun/security/x509/X509Key.java
author darcy
Wed, 09 Oct 2019 09:57:41 -0700
changeset 58519 6e017b301287
parent 57950 4612a3cfb927
child 58679 9c3209ff7550
permissions -rw-r--r--
8231262: Suppress warnings on non-serializable instance fields in security libs serializable classes Reviewed-by: mullan, chegar
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: 5298
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: 5298
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: 5298
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5298
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5298
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.x509;
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.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Properties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.Key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.PublicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.KeyFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.Security;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.Provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.InvalidKeyException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.NoSuchAlgorithmException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.spec.InvalidKeySpecException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.spec.X509EncodedKeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
34687
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
    41
import sun.security.util.HexDumpEncoder;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * Holds an X.509 key, for example a public key found in an X.509
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * certificate.  Includes a description of the algorithm to be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * with the key; these keys normally are used as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * "SubjectPublicKeyInfo".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <P>While this class can represent any kind of X.509 key, it may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * desirable to provide subclasses which understand how to parse keying
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * data.   For example, RSA public keys have two members, one for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * public modulus and one for the prime exponent.  If such a class is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * provided, it is used when parsing X.509 keys.  If one is not provided,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * the key still parses correctly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @author David Brownell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
public class X509Key implements PublicKey {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /** 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
    62
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private static final long serialVersionUID = -5359250853002055002L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /* The algorithm information (name, parameters, etc). */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    protected AlgorithmId algid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * The key bytes, without the algorithm information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * @deprecated Use the BitArray form which does not require keys to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * be byte aligned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * @see sun.security.x509.X509Key#setKey(BitArray)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * @see sun.security.x509.X509Key#getKey()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    protected byte[] key = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * The number of bits unused in the last byte of the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * Added to keep the byte[] key form consistent with the BitArray
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * form. Can de deleted when byte[] key is deleted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
19375
cdfdb9c0590e 8022461: Fix lint warnings in sun.security.{provider,rsa,x509}
juh
parents: 10336
diff changeset
    83
    @Deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private int unusedBits = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /* BitArray form of key */
58519
6e017b301287 8231262: Suppress warnings on non-serializable instance fields in security libs serializable classes
darcy
parents: 57950
diff changeset
    87
    private transient BitArray bitStringKey = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /* The encoding for the key. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    protected byte[] encodedKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Default constructor.  The key constructed must have its key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * and algorithm initialized before it may be used, for example
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * by using <code>decode</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public X509Key() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * Build and initialize as a "default" key.  All X.509 key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * data is stored and transmitted losslessly, but no knowledge
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * about this particular algorithm is available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private X509Key(AlgorithmId algid, BitArray key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        this.algid = algid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        setKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        encode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * Sets the key in the BitArray form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    protected void setKey(BitArray key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        this.bitStringKey = (BitArray)key.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
         * Do this to keep the byte array form consistent with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
         * this. Can delete when byte[] key is deleted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        this.key = key.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        int remaining = key.length() % 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        this.unusedBits =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            ((remaining == 0) ? 0 : 8 - remaining);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Gets the key. The key may or may not be byte aligned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @return a BitArray containing the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    protected BitArray getKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
         * Do this for consistency in case a subclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
         * modifies byte[] key directly. Remove when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
         * byte[] key is deleted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
         * Note: the consistency checks fail when the subclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
         * modifies a non byte-aligned key (into a byte-aligned key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
         * using the deprecated byte[] key field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        this.bitStringKey = new BitArray(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                          this.key.length * 8 - this.unusedBits,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                          this.key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        return (BitArray)bitStringKey.clone();
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
     * Construct X.509 subject public key from a DER value.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * the runtime environment is configured with a specific class for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * this kind of key, a subclass is returned.  Otherwise, a generic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * X509Key object is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * <P>This mechanism gurantees that keys (and algorithms) may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * freely manipulated and transferred, without risk of losing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * information.  Also, when a key (or algorithm) needs some special
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * handling, that specific need can be accomodated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @param in the DER-encoded SubjectPublicKeyInfo value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @exception IOException on data format errors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public static PublicKey parse(DerValue in) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        AlgorithmId     algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        PublicKey       subjectKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if (in.tag != DerValue.tag_Sequence)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            throw new IOException("corrupt subject key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        algorithm = AlgorithmId.parse(in.data.getDerValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            subjectKey = buildX509Key(algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                                      in.data.getUnalignedBitString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        } catch (InvalidKeyException e) {
5298
fbe659f73cfd 6944361: Missing CKR_ values in PKCS11Exception
andrew
parents: 2
diff changeset
   175
            throw new IOException("subject key, " + e.getMessage(), e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        if (in.data.available() != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            throw new IOException("excess subject key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        return subjectKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * Parse the key bits.  This may be redefined by subclasses to take
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * advantage of structure within the key.  For example, RSA public
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * keys encapsulate two unsigned integers (modulus and exponent) as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * DER values within the <code>key</code> bits; Diffie-Hellman and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * DSS/DSA keys encapsulate a single unsigned integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * <P>This function is called when creating X.509 SubjectPublicKeyInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * values using the X509Key member functions, such as <code>parse</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * and <code>decode</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @exception IOException on parsing errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @exception InvalidKeyException on invalid key encodings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    protected void parseKeyBits() throws IOException, InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        encode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * Factory interface, building the kind of key associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * specific algorithm ID or else returning this generic base class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * See the description above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    static PublicKey buildX509Key(AlgorithmId algid, BitArray key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
      throws IOException, InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
         * Use the algid and key parameters to produce the ASN.1 encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
         * of the key, which will then be used as the input to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
         * key factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        DerOutputStream x509EncodedKeyStream = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        encode(x509EncodedKeyStream, algid, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        X509EncodedKeySpec x509KeySpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            = new X509EncodedKeySpec(x509EncodedKeyStream.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            // Instantiate the key factory of the appropriate algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            KeyFactory keyFac = KeyFactory.getInstance(algid.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            // Generate the public key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            return keyFac.generatePublic(x509KeySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            // Return generic X509Key with opaque key data (see below)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        } catch (InvalidKeySpecException e) {
5298
fbe659f73cfd 6944361: Missing CKR_ values in PKCS11Exception
andrew
parents: 2
diff changeset
   228
            throw new InvalidKeyException(e.getMessage(), e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
         * Try again using JDK1.1-style for backwards compatibility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        String classname = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            Properties props;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            String keytype;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            Provider sunProvider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            sunProvider = Security.getProvider("SUN");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            if (sunProvider == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                throw new InstantiationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            classname = sunProvider.getProperty("PublicKey.X.509." +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
              algid.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            if (classname == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                throw new InstantiationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7668
diff changeset
   249
            Class<?> keyClass = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                keyClass = Class.forName(classname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                ClassLoader cl = ClassLoader.getSystemClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                if (cl != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    keyClass = cl.loadClass(classname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
37782
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 34687
diff changeset
   259
            @SuppressWarnings("deprecation")
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 34687
diff changeset
   260
            Object      inst = (keyClass != null) ? keyClass.newInstance() : null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            X509Key     result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            if (inst instanceof X509Key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                result = (X509Key) inst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                result.algid = algid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                result.setKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                result.parseKeyBits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        } catch (InstantiationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        } catch (IllegalAccessException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            // this should not happen.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            throw new IOException (classname + " [internal error]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        X509Key result = new X509Key(algid, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * Returns the algorithm to be used with this key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    public String getAlgorithm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        return algid.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * Returns the algorithm ID to be used with this key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    public AlgorithmId  getAlgorithmId() { return algid; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * Encode SubjectPublicKeyInfo sequence on the DER output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * @exception IOException on encoding errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    public final void encode(DerOutputStream out) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        encode(out, this.algid, getKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * Returns the DER-encoded form of the key as a byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    public byte[] getEncoded() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            return getEncodedInternal().clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        } catch (InvalidKeyException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            // XXX
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    public byte[] getEncodedInternal() throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        byte[] encoded = encodedKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        if (encoded == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                DerOutputStream out = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                encode(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                encoded = out.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                throw new InvalidKeyException("IOException : " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                                               e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            encodedKey = encoded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        return encoded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * Returns the format for this key: "X.509"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    public String getFormat() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        return "X.509";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * Returns the DER-encoded form of the key as a byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * @exception InvalidKeyException on encoding errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    public byte[] encode() throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        return getEncodedInternal().clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * Returns a printable representation of the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    public String toString()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        HexDumpEncoder  encoder = new HexDumpEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        return "algorithm = " + algid.toString()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            + ", unparsed keybits = \n" + encoder.encodeBuffer(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * Initialize an X509Key object from an input stream.  The data on that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * input stream must be encoded using DER, obeying the X.509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * <code>SubjectPublicKeyInfo</code> format.  That is, the data is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * sequence consisting of an algorithm ID and a bit string which holds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * the key.  (That bit string is often used to encapsulate another DER
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * encoded sequence.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * <P>Subclasses should not normally redefine this method; they should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * instead provide a <code>parseKeyBits</code> method to parse any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * fields inside the <code>key</code> member.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * <P>The exception to this rule is that since private keys need not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * be encoded using the X.509 <code>SubjectPublicKeyInfo</code> format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * private keys may override this method, <code>encode</code>, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * of course <code>getFormat</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * @param in an input stream with a DER-encoded X.509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     *          SubjectPublicKeyInfo value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * @exception InvalidKeyException on parsing errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    public void decode(InputStream in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    throws InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        DerValue        val;
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
            val = new DerValue(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            if (val.tag != DerValue.tag_Sequence)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                throw new InvalidKeyException("invalid key format");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            algid = AlgorithmId.parse(val.data.getDerValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            setKey(val.data.getUnalignedBitString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            parseKeyBits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            if (val.data.available() != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                throw new InvalidKeyException ("excess key data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            throw new InvalidKeyException("IOException: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                                          e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    public void decode(byte[] encodedKey) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        decode(new ByteArrayInputStream(encodedKey));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * Serialization write ... X.509 keys serialize as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * themselves, and they're parsed when they get read back.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     */
57950
4612a3cfb927 8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents: 48080
diff changeset
   409
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    private void writeObject(ObjectOutputStream stream) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        stream.write(getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * Serialization read ... X.509 keys serialize as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * themselves, and they're parsed when they get read back.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     */
57950
4612a3cfb927 8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents: 48080
diff changeset
   418
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    private void readObject(ObjectInputStream stream) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            decode(stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        } catch (InvalidKeyException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            throw new IOException("deserialized key is invalid: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                                  e.getMessage());
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
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        if (this == obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        if (obj instanceof Key == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            byte[] thisEncoded = this.getEncodedInternal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            byte[] otherEncoded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            if (obj instanceof X509Key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                otherEncoded = ((X509Key)obj).getEncodedInternal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                otherEncoded = ((Key)obj).getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            return Arrays.equals(thisEncoded, otherEncoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        } catch (InvalidKeyException e) {
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * Calculates a hash code value for the object. Objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * which are equal will also have the same hashcode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            byte[] b1 = getEncodedInternal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            int r = b1.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            for (int i = 0; i < b1.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                r += (b1[i] & 0xff) * 37;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        } catch (InvalidKeyException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            // should not happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * Produce SubjectPublicKey encoding from algorithm id and key material.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    static void encode(DerOutputStream out, AlgorithmId algid, BitArray key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            DerOutputStream tmp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            algid.encode(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            tmp.putUnalignedBitString(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            out.write(DerValue.tag_Sequence, tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
}