Path: blob/master/src/java.base/share/classes/sun/security/pkcs/ContentInfo.java
41159 views
/*1* Copyright (c) 1996, 2020, 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.*;2829import sun.security.util.*;3031/**32* A ContentInfo type, as defined in PKCS#7.33*34* @author Benjamin Renaud35*/3637public class ContentInfo {3839// pkcs7 pre-defined content types40public static ObjectIdentifier PKCS7_OID =41ObjectIdentifier.of(KnownOIDs.PKCS7);42public static ObjectIdentifier DATA_OID =43ObjectIdentifier.of(KnownOIDs.Data);44public static ObjectIdentifier SIGNED_DATA_OID =45ObjectIdentifier.of(KnownOIDs.SignedData);46public static ObjectIdentifier ENVELOPED_DATA_OID =47ObjectIdentifier.of(KnownOIDs.EnvelopedData);48public static ObjectIdentifier SIGNED_AND_ENVELOPED_DATA_OID =49ObjectIdentifier.of(KnownOIDs.SignedAndEnvelopedData);50public static ObjectIdentifier DIGESTED_DATA_OID =51ObjectIdentifier.of(KnownOIDs.DigestedData);52public static ObjectIdentifier ENCRYPTED_DATA_OID =53ObjectIdentifier.of(KnownOIDs.EncryptedData);5455// this is for backwards-compatibility with JDK 1.1.x56public static ObjectIdentifier OLD_SIGNED_DATA_OID =57ObjectIdentifier.of(KnownOIDs.JDK_OLD_SignedData);58public static ObjectIdentifier OLD_DATA_OID =59ObjectIdentifier.of(KnownOIDs.JDK_OLD_Data);6061// The ASN.1 systax for the Netscape Certificate Sequence data type is62// defined at:63// http://wp.netscape.com/eng/security/comm4-cert-download.html64public static ObjectIdentifier NETSCAPE_CERT_SEQUENCE_OID =65ObjectIdentifier.of(KnownOIDs.NETSCAPE_CertSequence);6667// timestamp token (id-ct-TSTInfo) from RFC 316168public static ObjectIdentifier TIMESTAMP_TOKEN_INFO_OID =69ObjectIdentifier.of(KnownOIDs.TimeStampTokenInfo);7071ObjectIdentifier contentType;72DerValue content; // OPTIONAL7374public ContentInfo(ObjectIdentifier contentType, DerValue content) {75this.contentType = contentType;76this.content = content;77}7879/**80* Make a contentInfo of type data.81*/82public ContentInfo(byte[] bytes) {83DerValue octetString = new DerValue(DerValue.tag_OctetString, bytes);84this.contentType = DATA_OID;85this.content = octetString;86}8788/**89* Parses a PKCS#7 content info.90*/91public ContentInfo(DerInputStream derin)92throws IOException, ParsingException93{94this(derin, false);95}9697/**98* Parses a PKCS#7 content info.99*100* <p>This constructor is used only for backwards compatibility with101* PKCS#7 blocks that were generated using JDK1.1.x.102*103* @param derin the ASN.1 encoding of the content info.104* @param oldStyle flag indicating whether or not the given content info105* is encoded according to JDK1.1.x.106*/107public ContentInfo(DerInputStream derin, boolean oldStyle)108throws IOException, ParsingException109{110DerInputStream disType;111DerInputStream disTaggedContent;112DerValue type;113DerValue taggedContent;114DerValue[] typeAndContent;115DerValue[] contents;116117typeAndContent = derin.getSequence(2);118if (typeAndContent.length < 1 || typeAndContent.length > 2) {119throw new ParsingException("Invalid length for ContentInfo");120}121122// Parse the content type123type = typeAndContent[0];124disType = new DerInputStream(type.toByteArray());125contentType = disType.getOID();126127if (oldStyle) {128// JDK1.1.x-style encoding129content = typeAndContent[1];130} else {131// This is the correct, standards-compliant encoding.132// Parse the content (OPTIONAL field).133// Skip the [0] EXPLICIT tag by pretending that the content is the134// one and only element in an implicitly tagged set135if (typeAndContent.length > 1) { // content is OPTIONAL136taggedContent = typeAndContent[1];137disTaggedContent138= new DerInputStream(taggedContent.toByteArray());139contents = disTaggedContent.getSet(1, true);140if (contents.length != 1) {141throw new ParsingException("ContentInfo encoding error");142}143content = contents[0];144}145}146}147148public DerValue getContent() {149return content;150}151152public ObjectIdentifier getContentType() {153return contentType;154}155156public byte[] getData() throws IOException {157if (contentType.equals(DATA_OID) ||158contentType.equals(OLD_DATA_OID) ||159contentType.equals(TIMESTAMP_TOKEN_INFO_OID)) {160if (content == null)161return null;162else163return content.getOctetString();164}165throw new IOException("content type is not DATA: " + contentType);166}167168public void encode(DerOutputStream out) throws IOException {169DerOutputStream contentDerCode;170DerOutputStream seq;171172seq = new DerOutputStream();173seq.putOID(contentType);174175// content is optional, it could be external176if (content != null) {177DerValue taggedContent = null;178contentDerCode = new DerOutputStream();179content.encode(contentDerCode);180181// Add the [0] EXPLICIT tag in front of the content encoding182taggedContent = new DerValue((byte)0xA0,183contentDerCode.toByteArray());184seq.putDerValue(taggedContent);185}186187out.write(DerValue.tag_Sequence, seq);188}189190/**191* Returns a byte array representation of the data held in192* the content field.193*/194public byte[] getContentBytes() throws IOException {195if (content == null)196return null;197198DerValue v = new DerValue(content.toByteArray());199return v.getOctetString();200}201202public String toString() {203String out = "";204205out += "Content Info Sequence\n\tContent type: " + contentType + "\n";206out += "\tContent: " + content;207return out;208}209}210211212