Path: blob/master/src/java.base/share/classes/com/sun/crypto/provider/EncryptedPrivateKeyInfo.java
41161 views
/*1* Copyright (c) 1998, 2011, 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 com.sun.crypto.provider;2627import java.io.*;28import sun.security.x509.AlgorithmId;29import sun.security.util.*;3031/**32* This class implements the <code>EncryptedPrivateKeyInfo</code> type,33* which is defined in PKCS #8 as follows:34*35* <pre>36* EncryptedPrivateKeyInfo ::= SEQUENCE {37* encryptionAlgorithm AlgorithmIdentifier,38* encryptedData OCTET STRING }39* </pre>40*41* @author Jan Luehe42*/43final class EncryptedPrivateKeyInfo {4445// the "encryptionAlgorithm" field46private AlgorithmId algid;4748// the "encryptedData" field49private byte[] encryptedData;5051// the ASN.1 encoded contents of this class52private byte[] encoded;5354/**55* Constructs (i.e., parses) an <code>EncryptedPrivateKeyInfo</code> from56* its encoding.57*/58EncryptedPrivateKeyInfo(byte[] encoded) throws IOException {59DerValue val = new DerValue(encoded);6061DerValue[] seq = new DerValue[2];6263seq[0] = val.data.getDerValue();64seq[1] = val.data.getDerValue();6566if (val.data.available() != 0) {67throw new IOException("overrun, bytes = " + val.data.available());68}6970this.algid = AlgorithmId.parse(seq[0]);71if (seq[0].data.available() != 0) {72throw new IOException("encryptionAlgorithm field overrun");73}7475this.encryptedData = seq[1].getOctetString();76if (seq[1].data.available() != 0)77throw new IOException("encryptedData field overrun");7879this.encoded = encoded.clone();80}8182/**83* Constructs an <code>EncryptedPrivateKeyInfo</code> from the84* encryption algorithm and the encrypted data.85*/86EncryptedPrivateKeyInfo(AlgorithmId algid, byte[] encryptedData) {87this.algid = algid;88this.encryptedData = encryptedData.clone();89this.encoded = null; // lazy generation of encoding90}9192/**93* Returns the encryption algorithm.94*/95AlgorithmId getAlgorithm() {96return this.algid;97}9899/**100* Returns the encrypted data.101*/102byte[] getEncryptedData() {103return this.encryptedData.clone();104}105106/**107* Returns the ASN.1 encoding of this class.108*/109byte[] getEncoded()110throws IOException111{112if (this.encoded != null) return this.encoded.clone();113114DerOutputStream out = new DerOutputStream();115DerOutputStream tmp = new DerOutputStream();116117// encode encryption algorithm118algid.encode(tmp);119120// encode encrypted data121tmp.putOctetString(encryptedData);122123// wrap everything into a SEQUENCE124out.write(DerValue.tag_Sequence, tmp);125this.encoded = out.toByteArray();126127return this.encoded.clone();128}129}130131132