Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/sun/security/x509/CertificateExtensions.java
41159 views
1
/*
2
* Copyright (c) 1997, 2019, 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.x509;
27
28
import java.io.IOException;
29
import java.io.OutputStream;
30
import java.lang.reflect.Constructor;
31
import java.lang.reflect.Field;
32
import java.lang.reflect.InvocationTargetException;
33
import java.security.cert.CertificateException;
34
import java.util.*;
35
36
import sun.security.util.HexDumpEncoder;
37
38
import sun.security.util.*;
39
40
/**
41
* This class defines the Extensions attribute for the Certificate.
42
*
43
* @author Amit Kapoor
44
* @author Hemma Prafullchandra
45
* @see CertAttrSet
46
*/
47
public class CertificateExtensions implements CertAttrSet<Extension> {
48
/**
49
* Identifier for this attribute, to be used with the
50
* get, set, delete methods of Certificate, x509 type.
51
*/
52
public static final String IDENT = "x509.info.extensions";
53
/**
54
* name
55
*/
56
public static final String NAME = "extensions";
57
58
private static final Debug debug = Debug.getInstance("x509");
59
60
private Map<String,Extension> map = Collections.synchronizedMap(
61
new TreeMap<String,Extension>());
62
private boolean unsupportedCritExt = false;
63
64
private Map<String,Extension> unparseableExtensions;
65
66
/**
67
* Default constructor.
68
*/
69
public CertificateExtensions() { }
70
71
/**
72
* Create the object, decoding the values from the passed DER stream.
73
*
74
* @param in the DerInputStream to read the Extension from.
75
* @exception IOException on decoding errors.
76
*/
77
public CertificateExtensions(DerInputStream in) throws IOException {
78
init(in);
79
}
80
81
// helper routine
82
private void init(DerInputStream in) throws IOException {
83
84
DerValue[] exts = in.getSequence(5);
85
86
for (int i = 0; i < exts.length; i++) {
87
Extension ext = new Extension(exts[i]);
88
parseExtension(ext);
89
}
90
}
91
92
private static Class<?>[] PARAMS = {Boolean.class, Object.class};
93
94
// Parse the encoded extension
95
private void parseExtension(Extension ext) throws IOException {
96
try {
97
Class<?> extClass = OIDMap.getClass(ext.getExtensionId());
98
if (extClass == null) { // Unsupported extension
99
if (ext.isCritical()) {
100
unsupportedCritExt = true;
101
}
102
if (map.put(ext.getExtensionId().toString(), ext) == null) {
103
return;
104
} else {
105
throw new IOException("Duplicate extensions not allowed");
106
}
107
}
108
Constructor<?> cons = extClass.getConstructor(PARAMS);
109
110
Object[] passed = new Object[] {Boolean.valueOf(ext.isCritical()),
111
ext.getExtensionValue()};
112
CertAttrSet<?> certExt = (CertAttrSet<?>)
113
cons.newInstance(passed);
114
if (map.put(certExt.getName(), (Extension)certExt) != null) {
115
throw new IOException("Duplicate extensions not allowed");
116
}
117
} catch (InvocationTargetException invk) {
118
Throwable e = invk.getCause();
119
if (ext.isCritical() == false) {
120
// ignore errors parsing non-critical extensions
121
if (unparseableExtensions == null) {
122
unparseableExtensions = new TreeMap<String,Extension>();
123
}
124
unparseableExtensions.put(ext.getExtensionId().toString(),
125
new UnparseableExtension(ext, e));
126
if (debug != null) {
127
debug.println("Debug info only." +
128
" Error parsing extension: " + ext);
129
e.printStackTrace();
130
HexDumpEncoder h = new HexDumpEncoder();
131
System.err.println(h.encodeBuffer(ext.getExtensionValue()));
132
}
133
return;
134
}
135
if (e instanceof IOException) {
136
throw (IOException)e;
137
} else {
138
throw new IOException(e);
139
}
140
} catch (IOException e) {
141
throw e;
142
} catch (Exception e) {
143
throw new IOException(e);
144
}
145
}
146
147
/**
148
* Encode the extensions in DER form to the stream, setting
149
* the context specific tag as needed in the X.509 v3 certificate.
150
*
151
* @param out the DerOutputStream to marshal the contents to.
152
* @exception CertificateException on encoding errors.
153
* @exception IOException on errors.
154
*/
155
public void encode(OutputStream out)
156
throws CertificateException, IOException {
157
encode(out, false);
158
}
159
160
/**
161
* Encode the extensions in DER form to the stream.
162
*
163
* @param out the DerOutputStream to marshal the contents to.
164
* @param isCertReq if true then no context specific tag is added.
165
* @exception CertificateException on encoding errors.
166
* @exception IOException on errors.
167
*/
168
public void encode(OutputStream out, boolean isCertReq)
169
throws CertificateException, IOException {
170
DerOutputStream extOut = new DerOutputStream();
171
Collection<Extension> allExts = map.values();
172
Object[] objs = allExts.toArray();
173
174
for (int i = 0; i < objs.length; i++) {
175
if (objs[i] instanceof CertAttrSet)
176
((CertAttrSet)objs[i]).encode(extOut);
177
else if (objs[i] instanceof Extension)
178
((Extension)objs[i]).encode(extOut);
179
else
180
throw new CertificateException("Illegal extension object");
181
}
182
183
DerOutputStream seq = new DerOutputStream();
184
seq.write(DerValue.tag_Sequence, extOut);
185
186
DerOutputStream tmp;
187
if (!isCertReq) { // certificate
188
tmp = new DerOutputStream();
189
tmp.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)3),
190
seq);
191
} else
192
tmp = seq; // pkcs#10 certificateRequest
193
194
out.write(tmp.toByteArray());
195
}
196
197
/**
198
* Set the attribute value.
199
* @param name the extension name used in the cache.
200
* @param obj the object to set.
201
* @exception IOException if the object could not be cached.
202
*/
203
public void set(String name, Object obj) throws IOException {
204
if (obj instanceof Extension) {
205
map.put(name, (Extension)obj);
206
} else {
207
throw new IOException("Unknown extension type.");
208
}
209
}
210
211
/**
212
* Get the attribute value.
213
* @param name the extension name used in the lookup.
214
* @exception IOException if named extension is not found.
215
*/
216
public Extension get(String name) throws IOException {
217
Extension obj = map.get(name);
218
if (obj == null) {
219
throw new IOException("No extension found with name " + name);
220
}
221
return (obj);
222
}
223
224
// Similar to get(String), but throw no exception, might return null.
225
// Used in X509CertImpl::getExtension(OID).
226
Extension getExtension(String name) {
227
return map.get(name);
228
}
229
230
/**
231
* Delete the attribute value.
232
* @param name the extension name used in the lookup.
233
* @exception IOException if named extension is not found.
234
*/
235
public void delete(String name) throws IOException {
236
Object obj = map.get(name);
237
if (obj == null) {
238
throw new IOException("No extension found with name " + name);
239
}
240
map.remove(name);
241
}
242
243
public String getNameByOid(ObjectIdentifier oid) throws IOException {
244
for (String name: map.keySet()) {
245
if (map.get(name).getExtensionId().equals(oid)) {
246
return name;
247
}
248
}
249
return null;
250
}
251
252
/**
253
* Return an enumeration of names of attributes existing within this
254
* attribute.
255
*/
256
public Enumeration<Extension> getElements() {
257
return Collections.enumeration(map.values());
258
}
259
260
/**
261
* Return a collection view of the extensions.
262
* @return a collection view of the extensions in this Certificate.
263
*/
264
public Collection<Extension> getAllExtensions() {
265
return map.values();
266
}
267
268
public Map<String,Extension> getUnparseableExtensions() {
269
if (unparseableExtensions == null) {
270
return Collections.emptyMap();
271
} else {
272
return unparseableExtensions;
273
}
274
}
275
276
/**
277
* Return the name of this attribute.
278
*/
279
public String getName() {
280
return NAME;
281
}
282
283
/**
284
* Return true if a critical extension is found that is
285
* not supported, otherwise return false.
286
*/
287
public boolean hasUnsupportedCriticalExtension() {
288
return unsupportedCritExt;
289
}
290
291
/**
292
* Compares this CertificateExtensions for equality with the specified
293
* object. If the {@code other} object is an
294
* {@code instanceof} {@code CertificateExtensions}, then
295
* all the entries are compared with the entries from this.
296
*
297
* @param other the object to test for equality with this
298
* CertificateExtensions.
299
* @return true iff all the entries match that of the Other,
300
* false otherwise.
301
*/
302
public boolean equals(Object other) {
303
if (this == other)
304
return true;
305
if (!(other instanceof CertificateExtensions))
306
return false;
307
Collection<Extension> otherC =
308
((CertificateExtensions)other).getAllExtensions();
309
Object[] objs = otherC.toArray();
310
311
int len = objs.length;
312
if (len != map.size())
313
return false;
314
315
Extension otherExt, thisExt;
316
String key = null;
317
for (int i = 0; i < len; i++) {
318
if (objs[i] instanceof CertAttrSet)
319
key = ((CertAttrSet)objs[i]).getName();
320
otherExt = (Extension)objs[i];
321
if (key == null)
322
key = otherExt.getExtensionId().toString();
323
thisExt = map.get(key);
324
if (thisExt == null)
325
return false;
326
if (! thisExt.equals(otherExt))
327
return false;
328
}
329
return this.getUnparseableExtensions().equals(
330
((CertificateExtensions)other).getUnparseableExtensions());
331
}
332
333
/**
334
* Returns a hashcode value for this CertificateExtensions.
335
*
336
* @return the hashcode value.
337
*/
338
public int hashCode() {
339
return map.hashCode() + getUnparseableExtensions().hashCode();
340
}
341
342
/**
343
* Returns a string representation of this {@code CertificateExtensions}
344
* object in the form of a set of entries, enclosed in braces and separated
345
* by the ASCII characters "<code>,&nbsp;</code>" (comma and space).
346
* <p>Overrides to {@code toString} method of {@code Object}.
347
*
348
* @return a string representation of this CertificateExtensions.
349
*/
350
public String toString() {
351
return map.toString();
352
}
353
354
}
355
356
class UnparseableExtension extends Extension {
357
private String name;
358
private String exceptionDescription;
359
360
public UnparseableExtension(Extension ext, Throwable why) {
361
super(ext);
362
363
name = "";
364
try {
365
Class<?> extClass = OIDMap.getClass(ext.getExtensionId());
366
if (extClass != null) {
367
Field field = extClass.getDeclaredField("NAME");
368
name = (String)(field.get(null)) + " ";
369
}
370
} catch (Exception e) {
371
// If we cannot find the name, just ignore it
372
}
373
374
this.exceptionDescription = why.toString();
375
}
376
377
@Override public String toString() {
378
return super.toString() +
379
"Unparseable " + name + "extension due to\n" +
380
exceptionDescription + "\n\n" +
381
new HexDumpEncoder().encodeBuffer(getExtensionValue());
382
}
383
}
384
385