Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/pkcs/ContentInfo.java
38830 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 types40private static int[] pkcs7 = {1, 2, 840, 113549, 1, 7};41private static int[] data = {1, 2, 840, 113549, 1, 7, 1};42private static int[] sdata = {1, 2, 840, 113549, 1, 7, 2};43private static int[] edata = {1, 2, 840, 113549, 1, 7, 3};44private static int[] sedata = {1, 2, 840, 113549, 1, 7, 4};45private static int[] ddata = {1, 2, 840, 113549, 1, 7, 5};46private static int[] crdata = {1, 2, 840, 113549, 1, 7, 6};47private static int[] nsdata = {2, 16, 840, 1, 113730, 2, 5};48// timestamp token (id-ct-TSTInfo) from RFC 316149private static int[] tstInfo = {1, 2, 840, 113549, 1, 9, 16, 1, 4};50// this is for backwards-compatibility with JDK 1.1.x51private static final int[] OLD_SDATA = {1, 2, 840, 1113549, 1, 7, 2};52private static final int[] OLD_DATA = {1, 2, 840, 1113549, 1, 7, 1};53public static ObjectIdentifier PKCS7_OID;54public static ObjectIdentifier DATA_OID;55public static ObjectIdentifier SIGNED_DATA_OID;56public static ObjectIdentifier ENVELOPED_DATA_OID;57public static ObjectIdentifier SIGNED_AND_ENVELOPED_DATA_OID;58public static ObjectIdentifier DIGESTED_DATA_OID;59public static ObjectIdentifier ENCRYPTED_DATA_OID;60public static ObjectIdentifier OLD_SIGNED_DATA_OID;61public static ObjectIdentifier OLD_DATA_OID;62public static ObjectIdentifier NETSCAPE_CERT_SEQUENCE_OID;63public static ObjectIdentifier TIMESTAMP_TOKEN_INFO_OID;6465static {66PKCS7_OID = ObjectIdentifier.newInternal(pkcs7);67DATA_OID = ObjectIdentifier.newInternal(data);68SIGNED_DATA_OID = ObjectIdentifier.newInternal(sdata);69ENVELOPED_DATA_OID = ObjectIdentifier.newInternal(edata);70SIGNED_AND_ENVELOPED_DATA_OID = ObjectIdentifier.newInternal(sedata);71DIGESTED_DATA_OID = ObjectIdentifier.newInternal(ddata);72ENCRYPTED_DATA_OID = ObjectIdentifier.newInternal(crdata);73OLD_SIGNED_DATA_OID = ObjectIdentifier.newInternal(OLD_SDATA);74OLD_DATA_OID = ObjectIdentifier.newInternal(OLD_DATA);75/**76* The ASN.1 systax for the Netscape Certificate Sequence77* data type is defined78* <a href=http://wp.netscape.com/eng/security/comm4-cert-download.html>79* here.</a>80*/81NETSCAPE_CERT_SEQUENCE_OID = ObjectIdentifier.newInternal(nsdata);82TIMESTAMP_TOKEN_INFO_OID = ObjectIdentifier.newInternal(tstInfo);83}8485ObjectIdentifier contentType;86DerValue content; // OPTIONAL8788public ContentInfo(ObjectIdentifier contentType, DerValue content) {89this.contentType = contentType;90this.content = content;91}9293/**94* Make a contentInfo of type data.95*/96public ContentInfo(byte[] bytes) {97DerValue octetString = new DerValue(DerValue.tag_OctetString, bytes);98this.contentType = DATA_OID;99this.content = octetString;100}101102/**103* Parses a PKCS#7 content info.104*/105public ContentInfo(DerInputStream derin)106throws IOException, ParsingException107{108this(derin, false);109}110111/**112* Parses a PKCS#7 content info.113*114* <p>This constructor is used only for backwards compatibility with115* PKCS#7 blocks that were generated using JDK1.1.x.116*117* @param derin the ASN.1 encoding of the content info.118* @param oldStyle flag indicating whether or not the given content info119* is encoded according to JDK1.1.x.120*/121public ContentInfo(DerInputStream derin, boolean oldStyle)122throws IOException, ParsingException123{124DerInputStream disType;125DerInputStream disTaggedContent;126DerValue type;127DerValue taggedContent;128DerValue[] typeAndContent;129DerValue[] contents;130131typeAndContent = derin.getSequence(2);132if (typeAndContent.length < 1 || typeAndContent.length > 2) {133throw new ParsingException("Invalid length for ContentInfo");134}135136// Parse the content type137type = typeAndContent[0];138disType = new DerInputStream(type.toByteArray());139contentType = disType.getOID();140141if (oldStyle) {142// JDK1.1.x-style encoding143content = typeAndContent[1];144} else {145// This is the correct, standards-compliant encoding.146// Parse the content (OPTIONAL field).147// Skip the [0] EXPLICIT tag by pretending that the content is the148// one and only element in an implicitly tagged set149if (typeAndContent.length > 1) { // content is OPTIONAL150taggedContent = typeAndContent[1];151disTaggedContent152= new DerInputStream(taggedContent.toByteArray());153contents = disTaggedContent.getSet(1, true);154if (contents.length != 1) {155throw new ParsingException("ContentInfo encoding error");156}157content = contents[0];158}159}160}161162public DerValue getContent() {163return content;164}165166public ObjectIdentifier getContentType() {167return contentType;168}169170public byte[] getData() throws IOException {171if (contentType.equals((Object)DATA_OID) ||172contentType.equals((Object)OLD_DATA_OID) ||173contentType.equals((Object)TIMESTAMP_TOKEN_INFO_OID)) {174if (content == null)175return null;176else177return content.getOctetString();178}179throw new IOException("content type is not DATA: " + contentType);180}181182public void encode(DerOutputStream out) throws IOException {183DerOutputStream contentDerCode;184DerOutputStream seq;185186seq = new DerOutputStream();187seq.putOID(contentType);188189// content is optional, it could be external190if (content != null) {191DerValue taggedContent = null;192contentDerCode = new DerOutputStream();193content.encode(contentDerCode);194195// Add the [0] EXPLICIT tag in front of the content encoding196taggedContent = new DerValue((byte)0xA0,197contentDerCode.toByteArray());198seq.putDerValue(taggedContent);199}200201out.write(DerValue.tag_Sequence, seq);202}203204/**205* Returns a byte array representation of the data held in206* the content field.207*/208public byte[] getContentBytes() throws IOException {209if (content == null)210return null;211212DerInputStream dis = new DerInputStream(content.toByteArray());213return dis.getOctetString();214}215216public String toString() {217String out = "";218219out += "Content Info Sequence\n\tContent type: " + contentType + "\n";220out += "\tContent: " + content;221return out;222}223}224225226