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