jdk/src/share/classes/javax/crypto/EncryptedPrivateKeyInfo.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 10336 0bb1999251f8
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2001, 2007, 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 javax.crypto;
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.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.security.x509.AlgorithmId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.security.util.DerValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.security.util.DerInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.util.DerOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * This class implements the <code>EncryptedPrivateKeyInfo</code> type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * as defined in PKCS #8.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <p>Its ASN.1 definition is as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * EncryptedPrivateKeyInfo ::=  SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *     encryptionAlgorithm   AlgorithmIdentifier,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *     encryptedData   OCTET STRING }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * AlgorithmIdentifier  ::=  SEQUENCE  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *     algorithm              OBJECT IDENTIFIER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *     parameters             ANY DEFINED BY algorithm OPTIONAL  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @author Valerie Peng
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * @see java.security.spec.PKCS8EncodedKeySpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
public class EncryptedPrivateKeyInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // the "encryptionAlgorithm" field
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private AlgorithmId algid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    // the "encryptedData" field
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private byte[] encryptedData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    // the ASN.1 encoded contents of this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private byte[] encoded = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Constructs (i.e., parses) an <code>EncryptedPrivateKeyInfo</code> from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * its ASN.1 encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * @param encoded the ASN.1 encoding of this object. The contents of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * the array are copied to protect against subsequent modification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * @exception NullPointerException if the <code>encoded</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @exception IOException if error occurs when parsing the ASN.1 encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public EncryptedPrivateKeyInfo(byte[] encoded)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        if (encoded == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            throw new NullPointerException("the encoded parameter " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                                           "must be non-null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        this.encoded = (byte[])encoded.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        DerValue val = new DerValue(this.encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        DerValue[] seq = new DerValue[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        seq[0] = val.data.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        seq[1] = val.data.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        if (val.data.available() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            throw new IOException("overrun, bytes = " + val.data.available());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        this.algid = AlgorithmId.parse(seq[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if (seq[0].data.available() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            throw new IOException("encryptionAlgorithm field overrun");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        this.encryptedData = seq[1].getOctetString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (seq[1].data.available() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            throw new IOException("encryptedData field overrun");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * Constructs an <code>EncryptedPrivateKeyInfo</code> from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * encryption algorithm name and the encrypted data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * <p>Note: This constructor will use null as the value of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * algorithm parameters. If the encryption algorithm has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * parameters whose value is not null, a different constructor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * e.g. EncryptedPrivateKeyInfo(AlgorithmParameters, byte[]),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * should be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @param algName encryption algorithm name. See Appendix A in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * <a href=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *   "{@docRoot}/../technotes/guides/security/crypto/CryptoSpec.html#AppA">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * Java Cryptography Architecture Reference Guide</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * for information about standard Cipher algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @param encryptedData encrypted data. The contents of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * <code>encrypedData</code> are copied to protect against subsequent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * modification when constructing this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @exception NullPointerException if <code>algName</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * <code>encryptedData</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @exception IllegalArgumentException if <code>encryptedData</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * is empty, i.e. 0-length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @exception NoSuchAlgorithmException if the specified algName is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * not supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public EncryptedPrivateKeyInfo(String algName, byte[] encryptedData)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if (algName == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                throw new NullPointerException("the algName parameter " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                                               "must be non-null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        this.algid = AlgorithmId.get(algName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (encryptedData == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            throw new NullPointerException("the encryptedData " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                                           "parameter must be non-null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        } else if (encryptedData.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            throw new IllegalArgumentException("the encryptedData " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                                                "parameter must not be empty");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            this.encryptedData = (byte[])encryptedData.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        // delay the generation of ASN.1 encoding until
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        // getEncoded() is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        this.encoded = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Constructs an <code>EncryptedPrivateKeyInfo</code> from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * encryption algorithm parameters and the encrypted data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @param algParams the algorithm parameters for the encryption
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * algorithm. <code>algParams.getEncoded()</code> should return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * the ASN.1 encoded bytes of the <code>parameters</code> field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * of the <code>AlgorithmIdentifer</code> component of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * <code>EncryptedPrivateKeyInfo</code> type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @param encryptedData encrypted data. The contents of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * <code>encrypedData</code> are copied to protect against
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * subsequent modification when constructing this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @exception NullPointerException if <code>algParams</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * <code>encryptedData</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @exception IllegalArgumentException if <code>encryptedData</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * is empty, i.e. 0-length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @exception NoSuchAlgorithmException if the specified algName of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * the specified <code>algParams</code> parameter is not supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    public EncryptedPrivateKeyInfo(AlgorithmParameters algParams,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        byte[] encryptedData) throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if (algParams == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            throw new NullPointerException("algParams must be non-null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        this.algid = AlgorithmId.get(algParams);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        if (encryptedData == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            throw new NullPointerException("encryptedData must be non-null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        } else if (encryptedData.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            throw new IllegalArgumentException("the encryptedData " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                                                "parameter must not be empty");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            this.encryptedData = (byte[])encryptedData.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        // delay the generation of ASN.1 encoding until
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        // getEncoded() is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        this.encoded = null;
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * Returns the encryption algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * <p>Note: Standard name is returned instead of the specified one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * in the constructor when such mapping is available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * See Appendix A in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * <a href=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *   "{@docRoot}/../technotes/guides/security/crypto/CryptoSpec.html#AppA">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * Java Cryptography Architecture Reference Guide</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * for information about standard Cipher algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @return the encryption algorithm name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    public String getAlgName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        return this.algid.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * Returns the algorithm parameters used by the encryption algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @return the algorithm parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    public AlgorithmParameters getAlgParameters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        return this.algid.getParameters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * Returns the encrypted data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @return the encrypted data. Returns a new array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * each time this method is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    public byte[] getEncryptedData() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        return (byte[])this.encryptedData.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * Extract the enclosed PKCS8EncodedKeySpec object from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * encrypted data and return it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * <br>Note: In order to successfully retrieve the enclosed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * PKCS8EncodedKeySpec object, <code>cipher</code> needs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * to be initialized to either Cipher.DECRYPT_MODE or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * Cipher.UNWRAP_MODE, with the same key and parameters used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * for generating the encrypted data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @param cipher the initialized cipher object which will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * used for decrypting the encrypted data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @return the PKCS8EncodedKeySpec object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @exception NullPointerException if <code>cipher</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @exception InvalidKeySpecException if the given cipher is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * inappropriate for the encrypted data or the encrypted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * data is corrupted and cannot be decrypted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    public PKCS8EncodedKeySpec getKeySpec(Cipher cipher)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        throws InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        byte[] encoded = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            encoded = cipher.doFinal((byte[])encryptedData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            checkPKCS8Encoding(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        } catch (GeneralSecurityException gse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            InvalidKeySpecException ikse = new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                InvalidKeySpecException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    "Cannot retrieve the PKCS8EncodedKeySpec");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            ikse.initCause(gse);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            throw ikse;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            InvalidKeySpecException ikse = new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                InvalidKeySpecException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    "Cannot retrieve the PKCS8EncodedKeySpec");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            ikse.initCause(ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            throw ikse;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        } catch (IllegalStateException ise) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            InvalidKeySpecException ikse = new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                InvalidKeySpecException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                    "Cannot retrieve the PKCS8EncodedKeySpec");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            ikse.initCause(ise);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            throw ikse;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        return new PKCS8EncodedKeySpec(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    private PKCS8EncodedKeySpec getKeySpecImpl(Key decryptKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        Provider provider) throws NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        byte[] encoded = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        Cipher c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            if (provider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                // use the most preferred one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                c = Cipher.getInstance(algid.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                c = Cipher.getInstance(algid.getName(), provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            c.init(Cipher.DECRYPT_MODE, decryptKey, algid.getParameters());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            encoded = c.doFinal(encryptedData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            checkPKCS8Encoding(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        } catch (NoSuchAlgorithmException nsae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            // rethrow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            throw nsae;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        } catch (GeneralSecurityException gse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            InvalidKeyException ike = new InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                ("Cannot retrieve the PKCS8EncodedKeySpec");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            ike.initCause(gse);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            throw ike;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            InvalidKeyException ike = new InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                ("Cannot retrieve the PKCS8EncodedKeySpec");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            ike.initCause(ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            throw ike;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        return new PKCS8EncodedKeySpec(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * Extract the enclosed PKCS8EncodedKeySpec object from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * encrypted data and return it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * @param decryptKey key used for decrypting the encrypted data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * @return the PKCS8EncodedKeySpec object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * @exception NullPointerException if <code>decryptKey</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * @exception NoSuchAlgorithmException if cannot find appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * cipher to decrypt the encrypted data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @exception InvalidKeyException if <code>decryptKey</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * cannot be used to decrypt the encrypted data or the decryption
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * result is not a valid PKCS8KeySpec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    public PKCS8EncodedKeySpec getKeySpec(Key decryptKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        throws NoSuchAlgorithmException, InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        if (decryptKey == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            throw new NullPointerException("decryptKey is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        return getKeySpecImpl(decryptKey, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * Extract the enclosed PKCS8EncodedKeySpec object from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * encrypted data and return it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * @param decryptKey key used for decrypting the encrypted data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * @param providerName the name of provider whose Cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * implementation will be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @return the PKCS8EncodedKeySpec object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @exception NullPointerException if <code>decryptKey</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * or <code>providerName</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * @exception NoSuchProviderException if no provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * <code>providerName</code> is registered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * @exception NoSuchAlgorithmException if cannot find appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * cipher to decrypt the encrypted data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * @exception InvalidKeyException if <code>decryptKey</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * cannot be used to decrypt the encrypted data or the decryption
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * result is not a valid PKCS8KeySpec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    public PKCS8EncodedKeySpec getKeySpec(Key decryptKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        String providerName) throws NoSuchProviderException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        NoSuchAlgorithmException, InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        if (decryptKey == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            throw new NullPointerException("decryptKey is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        if (providerName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            throw new NullPointerException("provider is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        Provider provider = Security.getProvider(providerName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        if (provider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            throw new NoSuchProviderException("provider " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                providerName + " not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        return getKeySpecImpl(decryptKey, provider);
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
     * Extract the enclosed PKCS8EncodedKeySpec object from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * encrypted data and return it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @param decryptKey key used for decrypting the encrypted data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * @param provider the name of provider whose Cipher implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * will be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * @return the PKCS8EncodedKeySpec object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * @exception NullPointerException if <code>decryptKey</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * or <code>provider</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * @exception NoSuchAlgorithmException if cannot find appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * cipher to decrypt the encrypted data in <code>provider</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * @exception InvalidKeyException if <code>decryptKey</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * cannot be used to decrypt the encrypted data or the decryption
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * result is not a valid PKCS8KeySpec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    public PKCS8EncodedKeySpec getKeySpec(Key decryptKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        Provider provider) throws NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        if (decryptKey == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            throw new NullPointerException("decryptKey is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        if (provider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            throw new NullPointerException("provider is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        return getKeySpecImpl(decryptKey, provider);
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
     * Returns the ASN.1 encoding of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * @return the ASN.1 encoding. Returns a new array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * each time this method is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * @exception IOException if error occurs when constructing its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * ASN.1 encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    public byte[] getEncoded() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        if (this.encoded == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            DerOutputStream out = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            DerOutputStream tmp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            // encode encryption algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            algid.encode(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            // encode encrypted data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            tmp.putOctetString(encryptedData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            // wrap everything into a SEQUENCE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            out.write(DerValue.tag_Sequence, tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            this.encoded = out.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        return (byte[])this.encoded.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    private static void checkTag(DerValue val, byte tag, String valName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        if (val.getTag() != tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            throw new IOException("invalid key encoding - wrong tag for " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                                  valName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    private static void checkPKCS8Encoding(byte[] encodedKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        DerInputStream in = new DerInputStream(encodedKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        DerValue[] values = in.getSequence(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        switch (values.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        case 4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            checkTag(values[3], DerValue.TAG_CONTEXT, "attributes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            checkTag(values[0], DerValue.tag_Integer, "version");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            DerInputStream algid = values[1].toDerInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            algid.getOID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            if (algid.available() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                algid.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            checkTag(values[2], DerValue.tag_OctetString, "privateKey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            throw new IOException("invalid key encoding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
}