jdk/src/share/classes/javax/crypto/EncryptedPrivateKeyInfo.java
author jjg
Mon, 15 Aug 2011 11:48:20 -0700
changeset 10336 0bb1999251f8
parent 5506 202f599c92aa
permissions -rw-r--r--
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror Reviewed-by: xuelei, mullan Contributed-by: alexandre.boulgakov@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
     2
 * Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package 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
        }
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    83
        this.encoded = encoded.clone();
2
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 {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   146
            this.encryptedData = encryptedData.clone();
2
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 {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   186
            this.encryptedData = encryptedData.clone();
2
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() {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   225
        return this.encryptedData.clone();
2
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 {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   250
            encoded = cipher.doFinal(encryptedData);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            checkPKCS8Encoding(encoded);
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   252
        } catch (GeneralSecurityException |
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   253
                 IOException |
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   254
                 IllegalStateException ex) {
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   255
            throw new InvalidKeySpecException(
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   256
                    "Cannot retrieve the PKCS8EncodedKeySpec", ex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        return new PKCS8EncodedKeySpec(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    private PKCS8EncodedKeySpec getKeySpecImpl(Key decryptKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        Provider provider) throws NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        byte[] encoded = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        Cipher c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            if (provider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                // use the most preferred one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                c = Cipher.getInstance(algid.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                c = Cipher.getInstance(algid.getName(), provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            c.init(Cipher.DECRYPT_MODE, decryptKey, algid.getParameters());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            encoded = c.doFinal(encryptedData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            checkPKCS8Encoding(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        } catch (NoSuchAlgorithmException nsae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            // rethrow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            throw nsae;
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   279
        } catch (GeneralSecurityException | IOException ex) {
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   280
            throw new InvalidKeyException(
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   281
                    "Cannot retrieve the PKCS8EncodedKeySpec", ex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        return new PKCS8EncodedKeySpec(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * Extract the enclosed PKCS8EncodedKeySpec object from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * encrypted data and return it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * @param decryptKey key used for decrypting the encrypted data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @return the PKCS8EncodedKeySpec object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @exception NullPointerException if <code>decryptKey</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @exception NoSuchAlgorithmException if cannot find appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * cipher to decrypt the encrypted data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @exception InvalidKeyException if <code>decryptKey</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * cannot be used to decrypt the encrypted data or the decryption
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * result is not a valid PKCS8KeySpec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    public PKCS8EncodedKeySpec getKeySpec(Key decryptKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        throws NoSuchAlgorithmException, InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        if (decryptKey == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            throw new NullPointerException("decryptKey is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        return getKeySpecImpl(decryptKey, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * Extract the enclosed PKCS8EncodedKeySpec object from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * encrypted data and return it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * @param decryptKey key used for decrypting the encrypted data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * @param providerName the name of provider whose Cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * implementation will be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @return the PKCS8EncodedKeySpec object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * @exception NullPointerException if <code>decryptKey</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * or <code>providerName</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * @exception NoSuchProviderException if no provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * <code>providerName</code> is registered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @exception NoSuchAlgorithmException if cannot find appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * cipher to decrypt the encrypted data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * @exception InvalidKeyException if <code>decryptKey</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * cannot be used to decrypt the encrypted data or the decryption
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * result is not a valid PKCS8KeySpec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    public PKCS8EncodedKeySpec getKeySpec(Key decryptKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        String providerName) throws NoSuchProviderException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        NoSuchAlgorithmException, InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        if (decryptKey == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            throw new NullPointerException("decryptKey is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        if (providerName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            throw new NullPointerException("provider is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        Provider provider = Security.getProvider(providerName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        if (provider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            throw new NoSuchProviderException("provider " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                providerName + " not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        return getKeySpecImpl(decryptKey, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * Extract the enclosed PKCS8EncodedKeySpec object from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * encrypted data and return it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @param decryptKey key used for decrypting the encrypted data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * @param provider the name of provider whose Cipher implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * will be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * @return the PKCS8EncodedKeySpec object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * @exception NullPointerException if <code>decryptKey</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * or <code>provider</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * @exception NoSuchAlgorithmException if cannot find appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * cipher to decrypt the encrypted data in <code>provider</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * @exception InvalidKeyException if <code>decryptKey</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * cannot be used to decrypt the encrypted data or the decryption
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * result is not a valid PKCS8KeySpec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    public PKCS8EncodedKeySpec getKeySpec(Key decryptKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        Provider provider) throws NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        if (decryptKey == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            throw new NullPointerException("decryptKey is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        if (provider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            throw new NullPointerException("provider is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        return getKeySpecImpl(decryptKey, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * Returns the ASN.1 encoding of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * @return the ASN.1 encoding. Returns a new array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * each time this method is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * @exception IOException if error occurs when constructing its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * ASN.1 encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    public byte[] getEncoded() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        if (this.encoded == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            DerOutputStream out = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            DerOutputStream tmp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            // encode encryption algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            algid.encode(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            // encode encrypted data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            tmp.putOctetString(encryptedData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            // wrap everything into a SEQUENCE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            out.write(DerValue.tag_Sequence, tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            this.encoded = out.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        }
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   396
        return this.encoded.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    private static void checkTag(DerValue val, byte tag, String valName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        if (val.getTag() != tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            throw new IOException("invalid key encoding - wrong tag for " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                                  valName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   407
    @SuppressWarnings("fallthrough")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    private static void checkPKCS8Encoding(byte[] encodedKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        DerInputStream in = new DerInputStream(encodedKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        DerValue[] values = in.getSequence(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        switch (values.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        case 4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            checkTag(values[3], DerValue.TAG_CONTEXT, "attributes");
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   416
            /* fall through */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            checkTag(values[0], DerValue.tag_Integer, "version");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            DerInputStream algid = values[1].toDerInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            algid.getOID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            if (algid.available() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                algid.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            checkTag(values[2], DerValue.tag_OctetString, "privateKey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            throw new IOException("invalid key encoding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
}