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/ResponderId/ResponderIdTests.java
38862 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 OCSP Stapling for TLS (ResponderId tests)
28
*/
29
30
import java.io.*;
31
import java.security.cert.*;
32
import java.security.KeyPair;
33
import java.security.KeyPairGenerator;
34
import java.util.AbstractMap;
35
import java.util.Arrays;
36
import java.util.Map;
37
import java.util.List;
38
import java.util.ArrayList;
39
import javax.security.auth.x500.X500Principal;
40
import sun.security.x509.KeyIdentifier;
41
import sun.security.provider.certpath.ResponderId;
42
43
/*
44
* NOTE: this test uses Sun private classes which are subject to change.
45
*/
46
public class ResponderIdTests {
47
48
private static final boolean debug = true;
49
50
// Source certificate created with the following command:
51
// keytool -genkeypair -alias test1 -keyalg rsa -keysize 2048 \
52
// -validity 7300 -keystore test1.jks \
53
// -dname "CN=SelfSignedResponder, OU=Validation Services, O=FakeCompany"
54
private static final String RESP_CERT_1 =
55
"-----BEGIN CERTIFICATE-----\n" +
56
"MIIDQzCCAiugAwIBAgIEXTqCCjANBgkqhkiG9w0BAQsFADBSMRQwEgYDVQQKEwtG\n" +
57
"YWtlQ29tcGFueTEcMBoGA1UECxMTVmFsaWRhdGlvbiBTZXJ2aWNlczEcMBoGA1UE\n" +
58
"AxMTU2VsZlNpZ25lZFJlc3BvbmRlcjAeFw0xNDA4MTcwNDM2MzBaFw0zNDA4MTIw\n" +
59
"NDM2MzBaMFIxFDASBgNVBAoTC0Zha2VDb21wYW55MRwwGgYDVQQLExNWYWxpZGF0\n" +
60
"aW9uIFNlcnZpY2VzMRwwGgYDVQQDExNTZWxmU2lnbmVkUmVzcG9uZGVyMIIBIjAN\n" +
61
"BgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApt2Cmw2k9tviLxaxE8aWNuoosWKL\n" +
62
"h+K4mNcDGKSoiChsqRqeJEnOxijDZqyFwfkaXvpAduFqYjz+Lij2HumvAjHDTui6\n" +
63
"bGcbsndRDPjvVo1S7f1oWsg7oiA8Lzmjl452S7UNBsDX5Dt1e84Xxwi40B1J2y8D\n" +
64
"FRPfYRWRlC1Z4kzqkBBa7JhANS+W8KDstFZxL4AwWH/byNwB5dl2j04ohg/Ar54e\n" +
65
"mu08PIH3hmi0pAu5wn9ariA7UA5lFWRJzvgGXV5J+QVEFuvKmeJ/Q6tU5OBJGw98\n" +
66
"zjd7F5B0iE+rJHTNF1aGaQfIorz04onV2WjH2VZA18AaMwqlY2br1SBdTQIDAQAB\n" +
67
"oyEwHzAdBgNVHQ4EFgQUG09HasSTYaTIh/CxxV/rcJV1LvowDQYJKoZIhvcNAQEL\n" +
68
"BQADggEBAIcUomNpZxGkocIzzybLyeyC6vLF1k0/unuPAHZLDP3o2JTstPhLHOCg\n" +
69
"FYw1VG2i23pjwKK2x/o80tJAOmW6vowbAPnNmtNIYO3gB/ZGiKeORoGKBCRDNvFa\n" +
70
"6ZrWxwTzT3EpVwRe7ameES0uP8+S4q2P5LhwMIMw7vGHoOQJgkAh/NUiCli1qRnJ\n" +
71
"FYd6cHMJJK5gF2FqQ7tdbA26pS06bkIEvil2M5wyKKWOydOa/pr1LgMf9KxljJ8J\n" +
72
"XlAOO/mGZGkYmWnQaQuBIDyWunWYlhsyCXMa8AScgs0uUeQp19tO7R0f03q/JXoZ\n" +
73
"1At1gZiMS7SdQaRWP5q+FunAeFWjsFE=\n" +
74
"-----END CERTIFICATE-----";
75
76
private static final String RESP_CERT_1_SUBJ =
77
"CN=SelfSignedResponder, OU=Validation Services, O=FakeCompany";
78
79
private static X509Certificate cert = null;
80
81
// The expected DER-encoding for a byName ResponderId derived
82
// from RESP_CERT_1
83
private static final byte[] EXP_NAME_ID_BYTES = {
84
-95, 84, 48, 82, 49, 20, 48, 18,
85
6, 3, 85, 4, 10, 19, 11, 70,
86
97, 107, 101, 67, 111, 109, 112, 97,
87
110, 121, 49, 28, 48, 26, 6, 3,
88
85, 4, 11, 19, 19, 86, 97, 108,
89
105, 100, 97, 116, 105, 111, 110, 32,
90
83, 101, 114, 118, 105, 99, 101, 115,
91
49, 28, 48, 26, 6, 3, 85, 4,
92
3, 19, 19, 83, 101, 108, 102, 83,
93
105, 103, 110, 101, 100, 82, 101, 115,
94
112, 111, 110, 100, 101, 114
95
};
96
97
// The expected DER-encoding for a byKey ResponderId derived
98
// from RESP_CERT_1
99
private static final byte[] EXP_KEY_ID_BYTES = {
100
-94, 22, 4, 20, 27, 79, 71, 106,
101
-60, -109, 97, -92, -56, -121, -16, -79,
102
-59, 95, -21, 112, -107, 117, 46, -6
103
};
104
105
// The DER encoding of a byKey ResponderId, but using an
106
// incorrect explicit tagging (CONTEXT CONSTRUCTED 3)
107
private static final byte[] INV_EXPLICIT_TAG_KEY_ID = {
108
-93, 22, 4, 20, 27, 79, 71, 106,
109
-60, -109, 97, -92, -56, -121, -16, -79,
110
-59, 95, -21, 112, -107, 117, 46, -6
111
};
112
113
// These two ResponderId objects will have objects attached to them
114
// after the pos_CtorByName and pos_CtorByKeyId tests run. Those
115
// two tests should always be the first two that run.
116
public static ResponderId respByName;
117
public static ResponderId respByKeyId;
118
119
public static void main(String[] args) throws Exception {
120
List<TestCase> testList = new ArrayList<>();
121
122
testList.add(pos_CtorByName);
123
testList.add(pos_CtorByKeyId);
124
testList.add(pos_CtorByEncoding);
125
testList.add(neg_CtorByEncoding);
126
testList.add(pos_Equality);
127
testList.add(pos_GetEncoded);
128
testList.add(pos_GetRespName);
129
testList.add(pos_GetRespKeyId);
130
131
// Load the certificate object we can use for subsequent tests
132
CertificateFactory cf = CertificateFactory.getInstance("X.509");
133
cert = (X509Certificate)cf.generateCertificate(
134
new ByteArrayInputStream(RESP_CERT_1.getBytes()));
135
136
System.out.println("============ Tests ============");
137
int testNo = 0;
138
int numberFailed = 0;
139
Map.Entry<Boolean, String> result;
140
for (TestCase test : testList) {
141
System.out.println("Test " + ++testNo + ": " + test.getName());
142
result = test.runTest();
143
System.out.print("Result: " + (result.getKey() ? "PASS" : "FAIL"));
144
System.out.println(" " +
145
(result.getValue() != null ? result.getValue() : ""));
146
System.out.println("-------------------------------------------");
147
if (!result.getKey()) {
148
numberFailed++;
149
}
150
}
151
System.out.println("End Results: " + (testList.size() - numberFailed) +
152
" Passed" + ", " + numberFailed + " Failed.");
153
if (numberFailed > 0) {
154
throw new RuntimeException(
155
"One or more tests failed, see test output for details");
156
}
157
}
158
159
private static void dumpHexBytes(byte[] data) {
160
if (data != null) {
161
for (int i = 0; i < data.length; i++) {
162
if (i % 16 == 0 && i != 0) {
163
System.out.print("\n");
164
}
165
System.out.print(String.format("%02X ", data[i]));
166
}
167
System.out.print("\n");
168
}
169
}
170
171
public interface TestCase {
172
String getName();
173
Map.Entry<Boolean, String> runTest();
174
}
175
176
public static final TestCase pos_CtorByName = new TestCase() {
177
@Override
178
public String getName() {
179
return "CTOR Test (by-name)";
180
}
181
182
@Override
183
public Map.Entry<Boolean, String> runTest() {
184
Boolean pass = Boolean.FALSE;
185
String message = null;
186
try {
187
respByName = new ResponderId(cert.getSubjectX500Principal());
188
pass = Boolean.TRUE;
189
} catch (Exception e) {
190
e.printStackTrace(System.out);
191
message = e.getClass().getName();
192
}
193
194
return new AbstractMap.SimpleEntry<>(pass, message);
195
}
196
};
197
198
public static final TestCase pos_CtorByKeyId = new TestCase() {
199
@Override
200
public String getName() {
201
return "CTOR Test (by-keyID)";
202
}
203
204
@Override
205
public Map.Entry<Boolean, String> runTest() {
206
Boolean pass = Boolean.FALSE;
207
String message = null;
208
try {
209
respByKeyId = new ResponderId(cert.getPublicKey());
210
pass = Boolean.TRUE;
211
} catch (Exception e) {
212
e.printStackTrace(System.out);
213
message = e.getClass().getName();
214
}
215
216
return new AbstractMap.SimpleEntry<>(pass, message);
217
}
218
};
219
220
public static final TestCase pos_CtorByEncoding = new TestCase() {
221
@Override
222
public String getName() {
223
return "CTOR Test (encoded bytes)";
224
}
225
226
@Override
227
public Map.Entry<Boolean, String> runTest() {
228
Boolean pass = Boolean.FALSE;
229
String message = null;
230
try {
231
ResponderId ridByNameBytes = new ResponderId(EXP_NAME_ID_BYTES);
232
ResponderId ridByKeyIdBytes = new ResponderId(EXP_KEY_ID_BYTES);
233
234
if (!ridByNameBytes.equals(respByName)) {
235
throw new RuntimeException(
236
"Equals failed: respNameFromBytes vs. respByName");
237
} else if (!ridByKeyIdBytes.equals(respByKeyId)) {
238
throw new RuntimeException(
239
"Equals failed: respKeyFromBytes vs. respByKeyId");
240
}
241
pass = Boolean.TRUE;
242
} catch (Exception e) {
243
e.printStackTrace(System.out);
244
message = e.getClass().getName();
245
}
246
247
return new AbstractMap.SimpleEntry<>(pass, message);
248
}
249
};
250
251
public static final TestCase neg_CtorByEncoding = new TestCase() {
252
@Override
253
public String getName() {
254
return "CTOR Test (by encoding, unknown explicit tag)";
255
}
256
257
@Override
258
public Map.Entry<Boolean, String> runTest() {
259
Boolean pass = Boolean.FALSE;
260
String message = null;
261
try {
262
ResponderId ridByKeyIdBytes =
263
new ResponderId(INV_EXPLICIT_TAG_KEY_ID);
264
throw new RuntimeException("Expected IOException not thrown");
265
} catch (IOException ioe) {
266
// Make sure it's the IOException we're looking for
267
if (ioe.getMessage().contains("Invalid ResponderId content")) {
268
pass = Boolean.TRUE;
269
} else {
270
ioe.printStackTrace(System.out);
271
message = ioe.getClass().getName();
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
283
public static final TestCase pos_Equality = new TestCase() {
284
@Override
285
public String getName() {
286
return "Simple Equality Test";
287
}
288
289
@Override
290
public Map.Entry<Boolean, String> runTest() {
291
Boolean pass = Boolean.FALSE;
292
String message = null;
293
294
try {
295
// byName ResponderId equality test
296
ResponderId compName =
297
new ResponderId(new X500Principal(RESP_CERT_1_SUBJ));
298
if (!respByName.equals(compName)) {
299
message = "ResponderId mismatch in byName comparison";
300
} else if (respByKeyId.equals(compName)) {
301
message = "Invalid ResponderId match in byKeyId comparison";
302
} else {
303
pass = Boolean.TRUE;
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
public static final TestCase pos_GetEncoded = new TestCase() {
315
@Override
316
public String getName() {
317
return "Get Encoded Value";
318
}
319
320
@Override
321
public Map.Entry<Boolean, String> runTest() {
322
Boolean pass = Boolean.FALSE;
323
String message = null;
324
325
try {
326
// Pull out byName and byKey encodings, they should match
327
// the expected values
328
if (!Arrays.equals(respByName.getEncoded(), EXP_NAME_ID_BYTES)) {
329
message = "ResponderId byName encoding did not " +
330
"match expected value";
331
} else if (!Arrays.equals(respByKeyId.getEncoded(), EXP_KEY_ID_BYTES)) {
332
message = "ResponderId byKeyId encoding did not " +
333
"match expected value";
334
} else {
335
pass = Boolean.TRUE;
336
}
337
} catch (Exception e) {
338
e.printStackTrace(System.out);
339
message = e.getClass().getName();
340
}
341
342
return new AbstractMap.SimpleEntry<>(pass, message);
343
}
344
};
345
346
public static final TestCase pos_GetRespName = new TestCase() {
347
@Override
348
public String getName() {
349
return "Get Underlying Responder Name";
350
}
351
352
@Override
353
public Map.Entry<Boolean, String> runTest() {
354
Boolean pass = Boolean.FALSE;
355
String message = null;
356
357
try {
358
// Test methods for pulling out the underlying
359
// X500Principal object
360
X500Principal testPrincipal =
361
new X500Principal(RESP_CERT_1_SUBJ);
362
if (!respByName.getResponderName().equals(testPrincipal)) {
363
message = "ResponderId Name did not match expected value";
364
} else if (respByKeyId.getResponderName() != null) {
365
message = "Non-null responder name returned from " +
366
"ResponderId constructed byKey";
367
} else {
368
pass = Boolean.TRUE;
369
}
370
} catch (Exception e) {
371
e.printStackTrace(System.out);
372
message = e.getClass().getName();
373
}
374
375
return new AbstractMap.SimpleEntry<>(pass, message);
376
}
377
};
378
379
public static final TestCase pos_GetRespKeyId = new TestCase() {
380
@Override
381
public String getName() {
382
return "Get Underlying Responder Key ID";
383
}
384
385
@Override
386
public Map.Entry<Boolean, String> runTest() {
387
Boolean pass = Boolean.FALSE;
388
String message = null;
389
390
try {
391
// Test methods for pulling out the underlying
392
// KeyIdentifier object. Note: There is a minute chance that
393
// an RSA public key, once hashed into a key ID might collide
394
// with the one extracted from the certificate used to create
395
// respByKeyId. This is so unlikely to happen it is considered
396
// virtually impossible.
397
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
398
kpg.initialize(2048);
399
KeyPair rsaKey = kpg.generateKeyPair();
400
KeyIdentifier testKeyId = new KeyIdentifier(rsaKey.getPublic());
401
402
if (respByKeyId.getKeyIdentifier().equals(testKeyId)) {
403
message = "Unexpected match in ResponderId Key ID";
404
} else if (respByName.getKeyIdentifier() != null) {
405
message = "Non-null key ID returned from " +
406
"ResponderId constructed byName";
407
} else {
408
pass = Boolean.TRUE;
409
}
410
} catch (Exception e) {
411
e.printStackTrace(System.out);
412
message = e.getClass().getName();
413
}
414
415
return new AbstractMap.SimpleEntry<>(pass, message);
416
}
417
};
418
419
}
420
421