Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/provider/certpath/Extensions/OCSPNonceExtensionTests.java
41154 views
1
/*
2
* Copyright (c) 2015, 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.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/*
25
* @test
26
* @bug 8046321 8256895
27
* @summary Unit tests for OCSPNonceExtension objects
28
* @modules java.base/sun.security.provider.certpath
29
* java.base/sun.security.util
30
* java.base/sun.security.x509
31
*/
32
33
import java.security.cert.Extension;
34
import java.io.ByteArrayOutputStream;
35
import java.io.IOException;
36
import java.util.*;
37
38
import sun.security.util.DerValue;
39
import sun.security.util.DerInputStream;
40
import sun.security.util.ObjectIdentifier;
41
import sun.security.provider.certpath.OCSPNonceExtension;
42
import sun.security.x509.PKIXExtensions;
43
44
public class OCSPNonceExtensionTests {
45
public static final boolean DEBUG = true;
46
public static final String OCSP_NONCE_OID = "1.3.6.1.5.5.7.48.1.2";
47
public static final String ELEMENT_NONCE = "nonce";
48
public static final String EXT_NAME = "OCSPNonce";
49
50
// DER encoding for OCSP nonce extension:
51
// OID = 1.3.6.1.5.5.7.48.1.2
52
// Critical = true
53
// 48 bytes of 0xDEADBEEF
54
public static final byte[] OCSP_NONCE_DER = {
55
48, 66, 6, 9, 43, 6, 1, 5,
56
5, 7, 48, 1, 2, 1, 1, -1,
57
4, 50, 4, 48, -34, -83, -66, -17,
58
-34, -83, -66, -17, -34, -83, -66, -17,
59
-34, -83, -66, -17, -34, -83, -66, -17,
60
-34, -83, -66, -17, -34, -83, -66, -17,
61
-34, -83, -66, -17, -34, -83, -66, -17,
62
-34, -83, -66, -17, -34, -83, -66, -17,
63
-34, -83, -66, -17,
64
};
65
66
// 16 bytes of 0xDEADBEEF
67
public static final byte[] DEADBEEF_16 = {
68
-34, -83, -66, -17, -34, -83, -66, -17,
69
-34, -83, -66, -17, -34, -83, -66, -17,
70
};
71
72
// DER encoded extension using 16 bytes of DEADBEEF
73
public static final byte[] OCSP_NONCE_DB16 = {
74
48, 31, 6, 9, 43, 6, 1, 5,
75
5, 7, 48, 1, 2, 4, 18, 4,
76
16, -34, -83, -66, -17, -34, -83, -66,
77
-17, -34, -83, -66, -17, -34, -83, -66,
78
-17
79
};
80
81
public static void main(String [] args) throws Exception {
82
Map<String, TestCase> testList =
83
new LinkedHashMap<String, TestCase>() {{
84
put("CTOR Test (provide length)", testCtorByLength);
85
put("CTOR Test (provide nonce bytes)", testCtorByValue);
86
put("CTOR Test (set criticality forms)", testCtorCritForms);
87
put("CTOR Test (provide extension DER encoding)",
88
testCtorSuperByDerValue);
89
put("Test getName() method", testGetName);
90
}};
91
92
System.out.println("============ Tests ============");
93
int testNo = 0;
94
int numberFailed = 0;
95
Map.Entry<Boolean, String> result;
96
for (String testName : testList.keySet()) {
97
System.out.println("Test " + ++testNo + ": " + testName);
98
result = testList.get(testName).runTest();
99
System.out.print("Result: " + (result.getKey() ? "PASS" : "FAIL"));
100
System.out.println(" " +
101
(result.getValue() != null ? result.getValue() : ""));
102
System.out.println("-------------------------------------------");
103
if (!result.getKey()) {
104
numberFailed++;
105
}
106
}
107
System.out.println("End Results: " + (testList.size() - numberFailed) +
108
" Passed" + ", " + numberFailed + " Failed.");
109
if (numberFailed > 0) {
110
throw new RuntimeException(
111
"One or more tests failed, see test output for details");
112
}
113
}
114
115
private static void dumpHexBytes(byte[] data) {
116
if (data != null) {
117
for (int i = 0; i < data.length; i++) {
118
if (i % 16 == 0 && i != 0) {
119
System.out.print("\n");
120
}
121
System.out.print(String.format("%02X ", data[i]));
122
}
123
System.out.print("\n");
124
}
125
}
126
127
private static void debuglog(String message) {
128
if (DEBUG) {
129
System.out.println(message);
130
}
131
}
132
133
public static void verifyExtStructure(byte[] derData) throws IOException {
134
debuglog("verifyASN1Extension() received " + derData.length + " bytes");
135
DerInputStream dis = new DerInputStream(derData);
136
137
// The sequenceItems array should be either two or three elements
138
// long. If three, then the criticality bit setting has been asserted.
139
DerValue[] sequenceItems = dis.getSequence(3);
140
debuglog("Found sequence containing " + sequenceItems.length +
141
" elements");
142
if (sequenceItems.length != 2 && sequenceItems.length != 3) {
143
throw new RuntimeException("Incorrect number of items found in " +
144
"the SEQUENCE (Got " + sequenceItems.length +
145
", expected 2 or 3 items)");
146
}
147
148
int seqIndex = 0;
149
ObjectIdentifier extOid = sequenceItems[seqIndex++].getOID();
150
debuglog("Found OID: " + extOid.toString());
151
if (!extOid.equals((Object)PKIXExtensions.OCSPNonce_Id)) {
152
throw new RuntimeException("Incorrect OID (Got " +
153
extOid.toString() + ", expected " +
154
PKIXExtensions.OCSPNonce_Id.toString() + ")");
155
}
156
157
if (sequenceItems.length == 3) {
158
// Non-default criticality bit setting should be at index 1
159
boolean isCrit = sequenceItems[seqIndex++].getBoolean();
160
debuglog("Found BOOLEAN (critical): " + isCrit);
161
}
162
163
// The extnValue is an encapsulating OCTET STRING that contains the
164
// extension's value. For the OCSP Nonce, that value itself is also
165
// an OCTET STRING consisting of the random bytes.
166
DerValue extnValue =
167
new DerValue(sequenceItems[seqIndex++].getOctetString());
168
byte[] nonceData = extnValue.getOctetString();
169
debuglog("Found " + nonceData.length + " bytes of nonce data");
170
}
171
172
public interface TestCase {
173
Map.Entry<Boolean, String> runTest();
174
}
175
176
public static final TestCase testCtorByLength = new TestCase() {
177
@Override
178
public Map.Entry<Boolean, String> runTest() {
179
Boolean pass = Boolean.FALSE;
180
String message = null;
181
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
182
// Try sending in a negative length
183
try {
184
Extension negLenNonce = new OCSPNonceExtension(-8);
185
throw new RuntimeException(
186
"Accepted a negative length nonce");
187
} catch (IllegalArgumentException iae) { }
188
189
// How about a zero length?
190
try {
191
Extension zeroLenNonce = new OCSPNonceExtension(0);
192
throw new RuntimeException("Accepted a zero length nonce");
193
} catch (IllegalArgumentException iae) { }
194
195
// Length of the nonce exceeds the maximum 32 bytes
196
try {
197
Extension bigLenNonce = new OCSPNonceExtension(33);
198
throw new RuntimeException("Accepted a larger than 32 bytes of nonce");
199
} catch (IllegalArgumentException iae) { }
200
201
// Valid input to constructor
202
Extension nonceByLen = new OCSPNonceExtension(32);
203
204
// Verify overall encoded extension structure
205
nonceByLen.encode(baos);
206
verifyExtStructure(baos.toByteArray());
207
208
// Verify the name, elements, and data conform to
209
// expected values for this specific object.
210
boolean crit = nonceByLen.isCritical();
211
String oid = nonceByLen.getId();
212
DerValue nonceData = new DerValue(nonceByLen.getValue());
213
214
if (crit) {
215
message = "Extension incorrectly marked critical";
216
} else if (!oid.equals(OCSP_NONCE_OID)) {
217
message = "Incorrect OID (Got " + oid + ", Expected " +
218
OCSP_NONCE_OID + ")";
219
} else if (nonceData.getTag() != DerValue.tag_OctetString) {
220
message = "Incorrect nonce data tag type (Got " +
221
String.format("0x%02X", nonceData.getTag()) +
222
", Expected 0x04)";
223
} else if (nonceData.getOctetString().length != 32) {
224
message = "Incorrect nonce byte length (Got " +
225
nonceData.getOctetString().length +
226
", Expected 32)";
227
} else {
228
pass = Boolean.TRUE;
229
}
230
} catch (Exception e) {
231
e.printStackTrace(System.out);
232
message = e.getClass().getName();
233
}
234
235
return new AbstractMap.SimpleEntry<>(pass, message);
236
}
237
};
238
239
public static final TestCase testCtorByValue = new TestCase() {
240
@Override
241
public Map.Entry<Boolean, String> runTest() {
242
Boolean pass = Boolean.FALSE;
243
String message = null;
244
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
245
246
// Try giving a null value for the nonce
247
try {
248
Extension nullNonce = new OCSPNonceExtension(null);
249
throw new RuntimeException("Accepted a null nonce");
250
} catch (NullPointerException npe) { }
251
252
// How about a zero-length byte array?
253
try {
254
Extension zeroLenNonce =
255
new OCSPNonceExtension(new byte[0]);
256
throw new RuntimeException("Accepted a zero length nonce");
257
} catch (IllegalArgumentException iae) { }
258
259
OCSPNonceExtension nonceByValue =
260
new OCSPNonceExtension(DEADBEEF_16);
261
262
// Verify overall encoded extension structure
263
nonceByValue.encode(baos);
264
verifyExtStructure(baos.toByteArray());
265
266
// Verify the name, elements, and data conform to
267
// expected values for this specific object.
268
boolean crit = nonceByValue.isCritical();
269
String oid = nonceByValue.getId();
270
byte[] nonceData = nonceByValue.getNonceValue();
271
272
if (crit) {
273
message = "Extension incorrectly marked critical";
274
} else if (!oid.equals(OCSP_NONCE_OID)) {
275
message = "Incorrect OID (Got " + oid + ", Expected " +
276
OCSP_NONCE_OID + ")";
277
} else if (!Arrays.equals(nonceData, DEADBEEF_16)) {
278
message = "Returned nonce value did not match input";
279
} else {
280
pass = Boolean.TRUE;
281
}
282
} catch (Exception e) {
283
e.printStackTrace(System.out);
284
message = e.getClass().getName();
285
}
286
287
return new AbstractMap.SimpleEntry<>(pass, message);
288
}
289
};
290
291
public static final TestCase testCtorCritForms = new TestCase() {
292
@Override
293
public Map.Entry<Boolean, String> runTest() {
294
Boolean pass = Boolean.FALSE;
295
String message = null;
296
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
297
Extension nonceByLength = new OCSPNonceExtension(true, 32);
298
Extension nonceByValue =
299
new OCSPNonceExtension(true, DEADBEEF_16);
300
pass = nonceByLength.isCritical() && nonceByValue.isCritical();
301
if (!pass) {
302
message = "nonceByLength or nonceByValue was not marked " +
303
"critical as expected";
304
}
305
} catch (Exception e) {
306
e.printStackTrace(System.out);
307
message = e.getClass().getName();
308
}
309
310
return new AbstractMap.SimpleEntry<>(pass, message);
311
}
312
};
313
314
315
public static final TestCase testCtorSuperByDerValue = new TestCase() {
316
@Override
317
public Map.Entry<Boolean, String> runTest() {
318
Boolean pass = Boolean.FALSE;
319
String message = null;
320
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
321
Extension nonceByDer = new sun.security.x509.Extension(
322
new DerValue(OCSP_NONCE_DER));
323
324
// Verify overall encoded extension structure
325
nonceByDer.encode(baos);
326
verifyExtStructure(baos.toByteArray());
327
328
// Verify the name, elements, and data conform to
329
// expected values for this specific object.
330
boolean crit = nonceByDer.isCritical();
331
String oid = nonceByDer.getId();
332
DerValue nonceData = new DerValue(nonceByDer.getValue());
333
334
if (!crit) {
335
message = "Extension lacks expected criticality setting";
336
} else if (!oid.equals(OCSP_NONCE_OID)) {
337
message = "Incorrect OID (Got " + oid + ", Expected " +
338
OCSP_NONCE_OID + ")";
339
} else if (nonceData.getTag() != DerValue.tag_OctetString) {
340
message = "Incorrect nonce data tag type (Got " +
341
String.format("0x%02X", nonceData.getTag()) +
342
", Expected 0x04)";
343
} else if (nonceData.getOctetString().length != 48) {
344
message = "Incorrect nonce byte length (Got " +
345
nonceData.getOctetString().length +
346
", Expected 48)";
347
} else {
348
pass = Boolean.TRUE;
349
}
350
} catch (Exception e) {
351
e.printStackTrace(System.out);
352
message = e.getClass().getName();
353
}
354
355
return new AbstractMap.SimpleEntry<>(pass, message);
356
}
357
};
358
359
public static final TestCase testGetName = new TestCase() {
360
@Override
361
public Map.Entry<Boolean, String> runTest() {
362
Boolean pass = Boolean.FALSE;
363
String message = null;
364
try {
365
OCSPNonceExtension nonceByLen = new OCSPNonceExtension(32);
366
pass = new Boolean(nonceByLen.getName().equals(EXT_NAME));
367
} catch (Exception e) {
368
e.printStackTrace(System.out);
369
message = e.getClass().getName();
370
}
371
372
return new AbstractMap.SimpleEntry<>(pass, message);
373
}
374
};
375
}
376
377