Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/src/java.base/share/classes/sun/security/pkcs/ContentInfo.java
67766 views
1
/*
2
* Copyright (c) 1996, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package sun.security.pkcs;
27
28
import java.io.*;
29
30
import sun.security.util.*;
31
32
/**
33
* A ContentInfo type, as defined in PKCS#7.
34
*
35
* @author Benjamin Renaud
36
*/
37
38
public class ContentInfo {
39
40
// pkcs7 pre-defined content types
41
public static ObjectIdentifier PKCS7_OID =
42
ObjectIdentifier.of(KnownOIDs.PKCS7);
43
public static ObjectIdentifier DATA_OID =
44
ObjectIdentifier.of(KnownOIDs.Data);
45
public static ObjectIdentifier SIGNED_DATA_OID =
46
ObjectIdentifier.of(KnownOIDs.SignedData);
47
public static ObjectIdentifier ENVELOPED_DATA_OID =
48
ObjectIdentifier.of(KnownOIDs.EnvelopedData);
49
public static ObjectIdentifier SIGNED_AND_ENVELOPED_DATA_OID =
50
ObjectIdentifier.of(KnownOIDs.SignedAndEnvelopedData);
51
public static ObjectIdentifier DIGESTED_DATA_OID =
52
ObjectIdentifier.of(KnownOIDs.DigestedData);
53
public static ObjectIdentifier ENCRYPTED_DATA_OID =
54
ObjectIdentifier.of(KnownOIDs.EncryptedData);
55
56
// this is for backwards-compatibility with JDK 1.1.x
57
public static ObjectIdentifier OLD_SIGNED_DATA_OID =
58
ObjectIdentifier.of(KnownOIDs.JDK_OLD_SignedData);
59
public static ObjectIdentifier OLD_DATA_OID =
60
ObjectIdentifier.of(KnownOIDs.JDK_OLD_Data);
61
62
// The ASN.1 systax for the Netscape Certificate Sequence data type is
63
// defined at:
64
// http://wp.netscape.com/eng/security/comm4-cert-download.html
65
public static ObjectIdentifier NETSCAPE_CERT_SEQUENCE_OID =
66
ObjectIdentifier.of(KnownOIDs.NETSCAPE_CertSequence);
67
68
// timestamp token (id-ct-TSTInfo) from RFC 3161
69
public static ObjectIdentifier TIMESTAMP_TOKEN_INFO_OID =
70
ObjectIdentifier.of(KnownOIDs.TimeStampTokenInfo);
71
72
ObjectIdentifier contentType;
73
DerValue content; // OPTIONAL
74
75
public ContentInfo(ObjectIdentifier contentType, DerValue content) {
76
this.contentType = contentType;
77
this.content = content;
78
}
79
80
/**
81
* Make a contentInfo of type data.
82
*/
83
public ContentInfo(byte[] bytes) {
84
DerValue octetString = new DerValue(DerValue.tag_OctetString, bytes);
85
this.contentType = DATA_OID;
86
this.content = octetString;
87
}
88
89
/**
90
* Parses a PKCS#7 content info.
91
*/
92
public ContentInfo(DerInputStream derin)
93
throws IOException, ParsingException
94
{
95
this(derin, false);
96
}
97
98
/**
99
* Parses a PKCS#7 content info.
100
*
101
* <p>This constructor is used only for backwards compatibility with
102
* PKCS#7 blocks that were generated using JDK1.1.x.
103
*
104
* @param derin the ASN.1 encoding of the content info.
105
* @param oldStyle flag indicating whether or not the given content info
106
* is encoded according to JDK1.1.x.
107
*/
108
public ContentInfo(DerInputStream derin, boolean oldStyle)
109
throws IOException, ParsingException
110
{
111
DerInputStream disType;
112
DerInputStream disTaggedContent;
113
DerValue type;
114
DerValue taggedContent;
115
DerValue[] typeAndContent;
116
DerValue[] contents;
117
118
typeAndContent = derin.getSequence(2);
119
if (typeAndContent.length < 1 || typeAndContent.length > 2) {
120
throw new ParsingException("Invalid length for ContentInfo");
121
}
122
123
// Parse the content type
124
type = typeAndContent[0];
125
disType = new DerInputStream(type.toByteArray());
126
contentType = disType.getOID();
127
128
if (oldStyle) {
129
// JDK1.1.x-style encoding
130
if (typeAndContent.length > 1) { // content is OPTIONAL
131
content = typeAndContent[1];
132
}
133
} else {
134
// This is the correct, standards-compliant encoding.
135
// Parse the content (OPTIONAL field).
136
// Skip the [0] EXPLICIT tag by pretending that the content is the
137
// one and only element in an implicitly tagged set
138
if (typeAndContent.length > 1) { // content is OPTIONAL
139
taggedContent = typeAndContent[1];
140
disTaggedContent
141
= new DerInputStream(taggedContent.toByteArray());
142
contents = disTaggedContent.getSet(1, true);
143
if (contents.length != 1) {
144
throw new ParsingException("ContentInfo encoding error");
145
}
146
content = contents[0];
147
}
148
}
149
}
150
151
public DerValue getContent() {
152
return content;
153
}
154
155
public ObjectIdentifier getContentType() {
156
return contentType;
157
}
158
159
public byte[] getData() throws IOException {
160
if (contentType.equals(DATA_OID) ||
161
contentType.equals(OLD_DATA_OID) ||
162
contentType.equals(TIMESTAMP_TOKEN_INFO_OID)) {
163
if (content == null)
164
return null;
165
else
166
return content.getOctetString();
167
}
168
throw new IOException("content type is not DATA: " + contentType);
169
}
170
171
public void encode(DerOutputStream out) throws IOException {
172
DerOutputStream contentDerCode;
173
DerOutputStream seq;
174
175
seq = new DerOutputStream();
176
seq.putOID(contentType);
177
178
// content is optional, it could be external
179
if (content != null) {
180
DerValue taggedContent = null;
181
contentDerCode = new DerOutputStream();
182
content.encode(contentDerCode);
183
184
// Add the [0] EXPLICIT tag in front of the content encoding
185
taggedContent = new DerValue((byte)0xA0,
186
contentDerCode.toByteArray());
187
seq.putDerValue(taggedContent);
188
}
189
190
out.write(DerValue.tag_Sequence, seq);
191
}
192
193
/**
194
* Returns a byte array representation of the data held in
195
* the content field.
196
*/
197
public byte[] getContentBytes() throws IOException {
198
if (content == null)
199
return null;
200
201
DerValue v = new DerValue(content.toByteArray());
202
return v.getOctetString();
203
}
204
205
public String toString() {
206
String out = "";
207
208
out += "Content Info Sequence\n\tContent type: " + contentType + "\n";
209
out += "\tContent: " + content;
210
return out;
211
}
212
}
213
214