001: /*
002: * @(#)EncryptedPrivateKeyInfo.java 1.14 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package sun.security.pkcs;
029:
030: import java.io.*;
031: import sun.security.x509.*;
032: import sun.security.util.DerValue;
033: import sun.security.util.DerOutputStream;
034:
035: /**
036: * This class implements the <code>EncryptedPrivateKeyInfo</code> type,
037: * which is defined in PKCS #8 as follows:
038: *
039: * <pre>
040: * EncryptedPrivateKeyInfo ::= SEQUENCE {
041: * encryptionAlgorithm AlgorithmIdentifier,
042: * encryptedData OCTET STRING }
043: * </pre>
044: *
045: * @author Jan Luehe
046: *
047: * @version 1.8, 00/02/02
048: */
049:
050: public class EncryptedPrivateKeyInfo {
051:
052: // the "encryptionAlgorithm" field
053: private AlgorithmId algid;
054:
055: // the "encryptedData" field
056: private byte[] encryptedData;
057:
058: // the ASN.1 encoded contents of this class
059: private byte[] encoded;
060:
061: /**
062: * Constructs (i.e., parses) an <code>EncryptedPrivateKeyInfo</code> from
063: * its encoding.
064: */
065: public EncryptedPrivateKeyInfo(byte[] encoded) throws IOException {
066: if (encoded == null) {
067: throw new IllegalArgumentException(
068: "encoding must not be null");
069: }
070:
071: DerValue val = new DerValue(encoded);
072:
073: DerValue[] seq = new DerValue[2];
074:
075: seq[0] = val.data.getDerValue();
076: seq[1] = val.data.getDerValue();
077:
078: if (val.data.available() != 0) {
079: throw new IOException("overrun, bytes = "
080: + val.data.available());
081: }
082:
083: this .algid = AlgorithmId.parse(seq[0]);
084: if (seq[0].data.available() != 0) {
085: throw new IOException("encryptionAlgorithm field overrun");
086: }
087:
088: this .encryptedData = seq[1].getOctetString();
089: if (seq[1].data.available() != 0)
090: throw new IOException("encryptedData field overrun");
091:
092: this .encoded = (byte[]) encoded.clone();
093: }
094:
095: /**
096: * Constructs an <code>EncryptedPrivateKeyInfo</code> from the
097: * encryption algorithm and the encrypted data.
098: */
099: public EncryptedPrivateKeyInfo(AlgorithmId algid,
100: byte[] encryptedData) {
101: this .algid = algid;
102: this .encryptedData = (byte[]) encryptedData.clone();
103: }
104:
105: /**
106: * Returns the encryption algorithm.
107: */
108: public AlgorithmId getAlgorithm() {
109: return this .algid;
110: }
111:
112: /**
113: * Returns the encrypted data.
114: */
115: public byte[] getEncryptedData() {
116: return (byte[]) this .encryptedData.clone();
117: }
118:
119: /**
120: * Returns the ASN.1 encoding of this class.
121: */
122: public byte[] getEncoded() throws IOException {
123: if (this .encoded != null)
124: return (byte[]) this .encoded.clone();
125:
126: DerOutputStream out = new DerOutputStream();
127: DerOutputStream tmp = new DerOutputStream();
128:
129: // encode encryption algorithm
130: algid.encode(tmp);
131:
132: // encode encrypted data
133: tmp.putOctetString(encryptedData);
134:
135: // wrap everything into a SEQUENCE
136: out.write(DerValue.tag_Sequence, tmp);
137: this .encoded = out.toByteArray();
138:
139: return (byte[]) this .encoded.clone();
140: }
141:
142: public boolean equals(Object other) {
143: if (this == other)
144: return true;
145: if (!(other instanceof EncryptedPrivateKeyInfo))
146: return false;
147: try {
148: byte[] this EncrInfo = this .getEncoded();
149: byte[] otherEncrInfo = ((EncryptedPrivateKeyInfo) other)
150: .getEncoded();
151:
152: if (this EncrInfo.length != otherEncrInfo.length)
153: return false;
154: for (int i = 0; i < this EncrInfo.length; i++)
155: if (this EncrInfo[i] != otherEncrInfo[i])
156: return false;
157: return true;
158: } catch (IOException e) {
159: return false;
160: }
161: }
162:
163: /**
164: * Returns a hashcode for this EncryptedPrivateKeyInfo.
165: *
166: * @return a hashcode for this EncryptedPrivateKeyInfo.
167: */
168: public int hashCode() {
169: int retval = 0;
170:
171: for (int i = 0; i < this.encryptedData.length; i++)
172: retval += this.encryptedData[i] * i;
173: return retval;
174: }
175: }
|