Path: blob/master/src/java.base/share/classes/sun/security/pkcs/EncryptedPrivateKeyInfo.java
41159 views
/*1* Copyright (c) 1998, 1999, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.pkcs;2627import java.io.*;28import sun.security.x509.*;29import sun.security.util.DerValue;30import sun.security.util.DerOutputStream;3132/**33* This class implements the <code>EncryptedPrivateKeyInfo</code> type,34* which is defined in PKCS #8 as follows:35*36* <pre>37* EncryptedPrivateKeyInfo ::= SEQUENCE {38* encryptionAlgorithm AlgorithmIdentifier,39* encryptedData OCTET STRING }40* </pre>41*42* @author Jan Luehe43*44*/4546public class EncryptedPrivateKeyInfo {4748// the "encryptionAlgorithm" field49private AlgorithmId algid;5051// the "encryptedData" field52private byte[] encryptedData;5354// the ASN.1 encoded contents of this class55private byte[] encoded;5657/**58* Constructs (i.e., parses) an <code>EncryptedPrivateKeyInfo</code> from59* its encoding.60*/61public EncryptedPrivateKeyInfo(byte[] encoded)62throws IOException63{64if (encoded == null) {65throw new IllegalArgumentException("encoding must not be null");66}6768DerValue val = new DerValue(encoded);6970DerValue[] seq = new DerValue[2];7172seq[0] = val.data.getDerValue();73seq[1] = val.data.getDerValue();7475if (val.data.available() != 0) {76throw new IOException("overrun, bytes = " + val.data.available());77}7879this.algid = AlgorithmId.parse(seq[0]);80if (seq[0].data.available() != 0) {81throw new IOException("encryptionAlgorithm field overrun");82}8384this.encryptedData = seq[1].getOctetString();85if (seq[1].data.available() != 0)86throw new IOException("encryptedData field overrun");8788this.encoded = encoded.clone();89}9091/**92* Constructs an <code>EncryptedPrivateKeyInfo</code> from the93* encryption algorithm and the encrypted data.94*/95public EncryptedPrivateKeyInfo(AlgorithmId algid, byte[] encryptedData) {96this.algid = algid;97this.encryptedData = encryptedData.clone();98}99100/**101* Returns the encryption algorithm.102*/103public AlgorithmId getAlgorithm() {104return this.algid;105}106107/**108* Returns the encrypted data.109*/110public byte[] getEncryptedData() {111return this.encryptedData.clone();112}113114/**115* Returns the ASN.1 encoding of this class.116*/117public byte[] getEncoded()118throws IOException119{120if (this.encoded != null) return this.encoded.clone();121122DerOutputStream out = new DerOutputStream();123DerOutputStream tmp = new DerOutputStream();124125// encode encryption algorithm126algid.encode(tmp);127128// encode encrypted data129tmp.putOctetString(encryptedData);130131// wrap everything into a SEQUENCE132out.write(DerValue.tag_Sequence, tmp);133this.encoded = out.toByteArray();134135return this.encoded.clone();136}137138public boolean equals(Object other) {139if (this == other)140return true;141if (!(other instanceof EncryptedPrivateKeyInfo))142return false;143try {144byte[] thisEncrInfo = this.getEncoded();145byte[] otherEncrInfo146= ((EncryptedPrivateKeyInfo)other).getEncoded();147148if (thisEncrInfo.length != otherEncrInfo.length)149return false;150for (int i = 0; i < thisEncrInfo.length; i++)151if (thisEncrInfo[i] != otherEncrInfo[i])152return false;153return true;154} catch (IOException e) {155return false;156}157}158159/**160* Returns a hashcode for this EncryptedPrivateKeyInfo.161*162* @return a hashcode for this EncryptedPrivateKeyInfo.163*/164public int hashCode() {165int retval = 0;166167for (int i = 0; i < this.encryptedData.length; i++)168retval += this.encryptedData[i] * i;169return retval;170}171}172173174