Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-aarch32-jdk8u
Path: blob/jdk8u272-b10-aarch32-20201026/jdk/test/java/security/cert/CertPathBuilder/selfIssued/KeyUsageMatters.java
48803 views
1
/*
2
* Copyright (c) 2009, 2014, 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
// Security properties, once set, cannot revert to unset. To avoid
26
// conflicts with tests running in the same VM isolate this test by
27
// running it in otherVM mode.
28
//
29
30
/**
31
* @test
32
* @bug 6852744 8133489
33
* @summary PIT b61: PKI test suite fails because self signed certificates
34
* are being rejected
35
* @run main/othervm -Djava.security.debug=certpath KeyUsageMatters subca
36
* @run main/othervm -Djava.security.debug=certpath KeyUsageMatters subci
37
* @run main/othervm -Djava.security.debug=certpath KeyUsageMatters alice
38
* @author Xuelei Fan
39
*/
40
41
import java.io.*;
42
import java.net.SocketException;
43
import java.util.*;
44
import java.security.Security;
45
import java.security.cert.*;
46
import java.security.cert.CertPathValidatorException.BasicReason;
47
import sun.security.util.DerInputStream;
48
49
/**
50
* KeyUsage extension plays a important rule during looking for the issuer
51
* of a certificate or CRL. A certificate issuer should have the keyCertSign
52
* bit set, and a CRL issuer should have the cRLSign bit set.
53
*
54
* Sometime, a delegated CRL issuer would also have the keyCertSign bit set,
55
* as would be troublesome to find the proper CRL issuer during certificate
56
* path build if the delegated CRL issuer is a self-issued certificate, for
57
* it is hard to identify it from its issuer by the "issuer" field only.
58
*
59
* The fix of 6852744 should addresses above issue, and allow a delegated CRL
60
* issuer to have keyCertSign bit set.
61
*
62
* In the test case, the delegated CRL issuers have cRLSign bit set only, and
63
* the CAs have the keyCertSign bit set only, it is expected to work before
64
* and after the bug fix of 6852744.
65
*/
66
public final class KeyUsageMatters {
67
68
// the trust anchor
69
static String selfSignedCertStr =
70
"-----BEGIN CERTIFICATE-----\n" +
71
"MIICPjCCAaegAwIBAgIBADANBgkqhkiG9w0BAQQFADAfMQswCQYDVQQGEwJVUzEQ\n" +
72
"MA4GA1UEChMHRXhhbXBsZTAeFw0wOTA0MjcwMjI0MzJaFw0zMDA0MDcwMjI0MzJa\n" +
73
"MB8xCzAJBgNVBAYTAlVTMRAwDgYDVQQKEwdFeGFtcGxlMIGfMA0GCSqGSIb3DQEB\n" +
74
"AQUAA4GNADCBiQKBgQC4OTag24sTxL2tXTNuvpmUEtdxrYAZoFsslFQ60T+WD9wQ\n" +
75
"Jeiw87FSPsR2vxRuv0j8DNm2a4h7LNNIFcLurfNldbz5pvgZ7VqdbbUMPE9qP85n\n" +
76
"jgDl4woyRTSUeRI4A7O0CO6NpES21dtbdhroWQrEkHxpnrDPxsxrz5gf2m3gqwID\n" +
77
"AQABo4GJMIGGMB0GA1UdDgQWBBSCJd0hpl5PdAD9IZS+Hzng4lXLGzBHBgNVHSME\n" +
78
"QDA+gBSCJd0hpl5PdAD9IZS+Hzng4lXLG6EjpCEwHzELMAkGA1UEBhMCVVMxEDAO\n" +
79
"BgNVBAoTB0V4YW1wbGWCAQAwDwYDVR0TAQH/BAUwAwEB/zALBgNVHQ8EBAMCAgQw\n" +
80
"DQYJKoZIhvcNAQEEBQADgYEAluy6HIjWcq009lTLmhp+Np6dxU78pInBK8RZkza0\n" +
81
"484qGaxFGD3UGyZkI5uWmsH2XuMbuox5khfIq6781gmkPBHXBIEtJN8eLusOHEye\n" +
82
"iE8h7WI+N3qa6Pj56WionMrioqC/3X+b06o147bbhx8U0vkYv/HyPaITOFfMXTdz\n" +
83
"Vjw=\n" +
84
"-----END CERTIFICATE-----";
85
86
// the sub-ca
87
static String subCaCertStr =
88
"-----BEGIN CERTIFICATE-----\n" +
89
"MIICUDCCAbmgAwIBAgIBAzANBgkqhkiG9w0BAQQFADAfMQswCQYDVQQGEwJVUzEQ\n" +
90
"MA4GA1UEChMHRXhhbXBsZTAeFw0wOTA0MjcwMjI0MzRaFw0yOTAxMTIwMjI0MzRa\n" +
91
"MDExCzAJBgNVBAYTAlVTMRAwDgYDVQQKEwdFeGFtcGxlMRAwDgYDVQQLEwdDbGFz\n" +
92
"cy0xMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCiAJnAQW2ad3ZMKUhSJVZj\n" +
93
"8pBqxTcHSTwAVguQkDglsN/OIwUpvR5Jgp3lpRWUEt6idEp0FZzORpvtjt3pr5MG\n" +
94
"Eg2CDptekC5BSPS+fIAIKlncB3HwOiFFhH6b3wTydDCdEd2fvsi4QMOSVrIYMeA8\n" +
95
"P/mCz6kRhfUQPE0CMmOUewIDAQABo4GJMIGGMB0GA1UdDgQWBBT0/nNP8WpyxmYr\n" +
96
"IBp4tN8y08jw2jBHBgNVHSMEQDA+gBSCJd0hpl5PdAD9IZS+Hzng4lXLG6EjpCEw\n" +
97
"HzELMAkGA1UEBhMCVVMxEDAOBgNVBAoTB0V4YW1wbGWCAQAwDwYDVR0TAQH/BAUw\n" +
98
"AwEB/zALBgNVHQ8EBAMCAgQwDQYJKoZIhvcNAQEEBQADgYEAS9PzI6B39R/U9fRj\n" +
99
"UExzN1FXNP5awnAPtiv34kSCL6n6MryqkfG+8aaAOdZsSjmTylNFaF7cW/Xp1VBF\n" +
100
"hq0bg/SbEAbK7+UwL8GSC3crhULHLbh+1iFdVTEwxCw5YmB8ji3BaZ/WKW/PkjCZ\n" +
101
"7cXP6VDeZMG6oRQ4hbOcixoFPXo=\n" +
102
"-----END CERTIFICATE-----";
103
104
// a delegated CRL issuer, it's a self-issued certificate of trust anchor
105
static String topCrlIssuerCertStr =
106
"-----BEGIN CERTIFICATE-----\n" +
107
"MIICKzCCAZSgAwIBAgIBAjANBgkqhkiG9w0BAQQFADAfMQswCQYDVQQGEwJVUzEQ\n" +
108
"MA4GA1UEChMHRXhhbXBsZTAeFw0wOTA0MjcwMjI0MzNaFw0yOTAxMTIwMjI0MzNa\n" +
109
"MB8xCzAJBgNVBAYTAlVTMRAwDgYDVQQKEwdFeGFtcGxlMIGfMA0GCSqGSIb3DQEB\n" +
110
"AQUAA4GNADCBiQKBgQDMJeBMBybHykI/YpwUJ4O9euqDSLb1kpWpceBS8TVqvgBC\n" +
111
"SgUJWtFZL0i6bdvF6mMdlbuBkGzhXqHiVAi96/zRLbUC9F8SMEJ6MuD+YhQ0ZFTQ\n" +
112
"atKy8zf8O9XzztelLJ26Gqb7QPV133WY3haAqHtCXOhEKkCN16NOYNC37DTaJwID\n" +
113
"AQABo3cwdTAdBgNVHQ4EFgQULXSWzXzUOIpOJpzbSCpW42IJUugwRwYDVR0jBEAw\n" +
114
"PoAUgiXdIaZeT3QA/SGUvh854OJVyxuhI6QhMB8xCzAJBgNVBAYTAlVTMRAwDgYD\n" +
115
"VQQKEwdFeGFtcGxlggEAMAsGA1UdDwQEAwIBAjANBgkqhkiG9w0BAQQFAAOBgQAY\n" +
116
"eMnf5AHSNlyUlzXk8o2S0h4gCuvKX6C3kFfKuZcWvFAbx4yQOWLS2s15/nzR4+AP\n" +
117
"FGX3lgJjROyAh7fGedTQK+NFWwkM2ag1g3hXktnlnT1qHohi0w31nVBJxXEDO/Ck\n" +
118
"uJTpJGt8XxxbFaw5v7cHy7XuTAeU/sekvjEiNHW00Q==\n" +
119
"-----END CERTIFICATE-----";
120
121
// a delegated CRL issuer, it's a self-issued certificate of sub-ca
122
static String subCrlIssuerCertStr =
123
"-----BEGIN CERTIFICATE-----\n" +
124
"MIICPTCCAaagAwIBAgIBBDANBgkqhkiG9w0BAQQFADAfMQswCQYDVQQGEwJVUzEQ\n" +
125
"MA4GA1UEChMHRXhhbXBsZTAeFw0wOTA0MjcwMjI0MzRaFw0yOTAxMTIwMjI0MzRa\n" +
126
"MDExCzAJBgNVBAYTAlVTMRAwDgYDVQQKEwdFeGFtcGxlMRAwDgYDVQQLEwdDbGFz\n" +
127
"cy0xMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDWUtDQx2MB/7arDiquMJyd\n" +
128
"LWwSg6p8sg5z6wKrC1v47MT4DBhFX+0RUgTMUdQgYpgxGpczn+6y4zfV76064S0N\n" +
129
"4L/IQ+SunTW1w4yRGjB+xkyyJmWAqijG1nr+Dgkv5nxPI+9Er5lHcoVWVMEcvvRm\n" +
130
"6jIBQdldVlSgv+VgUnFm5wIDAQABo3cwdTAdBgNVHQ4EFgQUkV3Qqtk7gIot9n60\n" +
131
"jX6dloxrfMEwRwYDVR0jBEAwPoAUgiXdIaZeT3QA/SGUvh854OJVyxuhI6QhMB8x\n" +
132
"CzAJBgNVBAYTAlVTMRAwDgYDVQQKEwdFeGFtcGxlggEAMAsGA1UdDwQEAwIBAjAN\n" +
133
"BgkqhkiG9w0BAQQFAAOBgQADu4GM8EdmIKhC7FRvk5jF90zfvZ38wbXBzCjKI4jX\n" +
134
"QJrhne1bfyeNNm5c1w+VKidT+XzBzBGH7ZqYzoZmzRIfcbLKX2brEBKiukeeAyL3\n" +
135
"bctQtbp19tX+uu2dQberD188AAysKTkHcJUV+rRsTwVJ9vcYKxoRxKk8DhH7ZS3M\n" +
136
"rg==\n" +
137
"-----END CERTIFICATE-----";
138
139
// the target EE certificate
140
static String targetCertStr =
141
"-----BEGIN CERTIFICATE-----\n" +
142
"MIICNzCCAaCgAwIBAgIBAjANBgkqhkiG9w0BAQQFADAxMQswCQYDVQQGEwJVUzEQ\n" +
143
"MA4GA1UEChMHRXhhbXBsZTEQMA4GA1UECxMHQ2xhc3MtMTAeFw0wOTA0MjcwMjI0\n" +
144
"MzZaFw0yOTAxMTIwMjI0MzZaMEExCzAJBgNVBAYTAlVTMRAwDgYDVQQKEwdFeGFt\n" +
145
"cGxlMRAwDgYDVQQLEwdDbGFzcy0xMQ4wDAYDVQQDEwVBbGljZTCBnzANBgkqhkiG\n" +
146
"9w0BAQEFAAOBjQAwgYkCgYEAvYSaU3oiE4Pxp/aUIXwMqOwSiWkZ+O3aTu13hRtK\n" +
147
"ZyR+Wtj63IuvaigAC4uC+zBypF93ThjwCzVR2qKDQaQzV8CLleO96gStt7Y+i3G2\n" +
148
"V3IUGgrVCqeK7N6nNYu0wW84sibcPqG/TIy0UoaQMqgB21xtRF+1DUVlFh4Z89X/\n" +
149
"pskCAwEAAaNPME0wCwYDVR0PBAQDAgPoMB0GA1UdDgQWBBSynMEdcal/e9TmvlNE\n" +
150
"4suXGA4+hjAfBgNVHSMEGDAWgBT0/nNP8WpyxmYrIBp4tN8y08jw2jANBgkqhkiG\n" +
151
"9w0BAQQFAAOBgQB/jru7E/+piSmUwByw5qbZsoQZVcgR97pd2TErNJpJMAX2oIHR\n" +
152
"wJH6w4NuYs27+fEAX7wK4whc6EUH/w1SI6o28F2rG6HqYQPPZ2E2WqwbBQL9nYE3\n" +
153
"Vfzu/G9axTUQXFbf90h80UErA+mZVxqc2xtymLuH0YEaMZImtRZ2MXHfXg==\n" +
154
"-----END CERTIFICATE-----";
155
156
// CRL issued by the delegated CRL issuer, topCrlIssuerCertStr
157
static String topCrlStr =
158
"-----BEGIN X509 CRL-----\n" +
159
"MIIBGzCBhQIBATANBgkqhkiG9w0BAQQFADAfMQswCQYDVQQGEwJVUzEQMA4GA1UE\n" +
160
"ChMHRXhhbXBsZRcNMDkwNDI3MDIzODA0WhcNMjgwNjI2MDIzODA0WjAiMCACAQUX\n" +
161
"DTA5MDQyNzAyMzgwMFowDDAKBgNVHRUEAwoBBKAOMAwwCgYDVR0UBAMCAQIwDQYJ\n" +
162
"KoZIhvcNAQEEBQADgYEAoarfzXEtw3ZDi4f9U8eSvRIipHSyxOrJC7HR/hM5VhmY\n" +
163
"CErChny6x9lBVg9s57tfD/P9PSzBLusCcHwHMAbMOEcTltVVKUWZnnbumpywlYyg\n" +
164
"oKLrE9+yCOkYUOpiRlz43/3vkEL5hjIKMcDSZnPKBZi1h16Yj2hPe9GMibNip54=\n" +
165
"-----END X509 CRL-----";
166
167
// CRL issued by the delegated CRL issuer, subCrlIssuerCertStr
168
static String subCrlStr =
169
"-----BEGIN X509 CRL-----\n" +
170
"MIIBLTCBlwIBATANBgkqhkiG9w0BAQQFADAxMQswCQYDVQQGEwJVUzEQMA4GA1UE\n" +
171
"ChMHRXhhbXBsZTEQMA4GA1UECxMHQ2xhc3MtMRcNMDkwNDI3MDIzODA0WhcNMjgw\n" +
172
"NjI2MDIzODA0WjAiMCACAQQXDTA5MDQyNzAyMzgwMVowDDAKBgNVHRUEAwoBBKAO\n" +
173
"MAwwCgYDVR0UBAMCAQIwDQYJKoZIhvcNAQEEBQADgYEAeS+POqYEIHIIJcsLxuUr\n" +
174
"aJFzQ/ujH0QmnyMNEL3Uavyq4VQuAahF+w6aTPb5UBzms0uX8NAvD2vNoUJvmJOX\n" +
175
"nGKuq4Q1DFj82E7/9d25nXdWGOmFvFCRVO+St2Xe5n8CJuZNBiz388FDSIOiFSCa\n" +
176
"ARGr6Qu68MYGtLMC6ZqP3u0=\n" +
177
"-----END X509 CRL-----";
178
179
private static Set<TrustAnchor> generateTrustAnchors()
180
throws CertificateException {
181
// generate certificate from cert string
182
CertificateFactory cf = CertificateFactory.getInstance("X.509");
183
184
ByteArrayInputStream is =
185
new ByteArrayInputStream(selfSignedCertStr.getBytes());
186
Certificate selfSignedCert = cf.generateCertificate(is);
187
188
// generate a trust anchor
189
TrustAnchor anchor =
190
new TrustAnchor((X509Certificate)selfSignedCert, null);
191
192
return Collections.singleton(anchor);
193
}
194
195
private static CertStore generateCertificateStore() throws Exception {
196
Collection entries = new HashSet();
197
198
// generate certificate from certificate string
199
CertificateFactory cf = CertificateFactory.getInstance("X.509");
200
201
ByteArrayInputStream is;
202
203
is = new ByteArrayInputStream(targetCertStr.getBytes());
204
Certificate cert = cf.generateCertificate(is);
205
entries.add(cert);
206
207
is = new ByteArrayInputStream(subCaCertStr.getBytes());
208
cert = cf.generateCertificate(is);
209
entries.add(cert);
210
211
is = new ByteArrayInputStream(selfSignedCertStr.getBytes());
212
cert = cf.generateCertificate(is);
213
entries.add(cert);
214
215
is = new ByteArrayInputStream(topCrlIssuerCertStr.getBytes());
216
cert = cf.generateCertificate(is);
217
entries.add(cert);
218
219
is = new ByteArrayInputStream(subCrlIssuerCertStr.getBytes());
220
cert = cf.generateCertificate(is);
221
entries.add(cert);
222
223
// generate CRL from CRL string
224
is = new ByteArrayInputStream(topCrlStr.getBytes());
225
Collection mixes = cf.generateCRLs(is);
226
entries.addAll(mixes);
227
228
is = new ByteArrayInputStream(subCrlStr.getBytes());
229
mixes = cf.generateCRLs(is);
230
entries.addAll(mixes);
231
232
return CertStore.getInstance("Collection",
233
new CollectionCertStoreParameters(entries));
234
}
235
236
private static X509CertSelector generateSelector(String name)
237
throws Exception {
238
X509CertSelector selector = new X509CertSelector();
239
240
// generate certificate from certificate string
241
CertificateFactory cf = CertificateFactory.getInstance("X.509");
242
ByteArrayInputStream is = null;
243
if (name.equals("subca")) {
244
is = new ByteArrayInputStream(subCaCertStr.getBytes());
245
} else if (name.equals("subci")) {
246
is = new ByteArrayInputStream(subCrlIssuerCertStr.getBytes());
247
} else {
248
is = new ByteArrayInputStream(targetCertStr.getBytes());
249
}
250
251
X509Certificate target = (X509Certificate)cf.generateCertificate(is);
252
byte[] extVal = target.getExtensionValue("2.5.29.14");
253
if (extVal != null) {
254
DerInputStream in = new DerInputStream(extVal);
255
byte[] subjectKID = in.getOctetString();
256
selector.setSubjectKeyIdentifier(subjectKID);
257
} else {
258
// unlikely to happen.
259
throw new Exception("unexpected certificate: no SKID extension");
260
}
261
262
return selector;
263
}
264
265
private static boolean match(String name, Certificate cert)
266
throws Exception {
267
X509CertSelector selector = new X509CertSelector();
268
269
// generate certificate from certificate string
270
CertificateFactory cf = CertificateFactory.getInstance("X.509");
271
ByteArrayInputStream is = null;
272
if (name.equals("subca")) {
273
is = new ByteArrayInputStream(subCaCertStr.getBytes());
274
} else if (name.equals("subci")) {
275
is = new ByteArrayInputStream(subCrlIssuerCertStr.getBytes());
276
} else {
277
is = new ByteArrayInputStream(targetCertStr.getBytes());
278
}
279
X509Certificate target = (X509Certificate)cf.generateCertificate(is);
280
281
return target.equals(cert);
282
}
283
284
285
public static void main(String[] args) throws Exception {
286
// MD5 is used in this test case, don't disable MD5 algorithm.
287
Security.setProperty(
288
"jdk.certpath.disabledAlgorithms", "MD2, RSA keySize < 1024");
289
290
CertPathBuilder builder = CertPathBuilder.getInstance("PKIX");
291
292
X509CertSelector selector = generateSelector(args[0]);
293
294
Set<TrustAnchor> anchors = generateTrustAnchors();
295
CertStore certs = generateCertificateStore();
296
297
298
PKIXBuilderParameters params =
299
new PKIXBuilderParameters(anchors, selector);
300
params.addCertStore(certs);
301
params.setRevocationEnabled(true);
302
params.setDate(new Date(109, 5, 1)); // 2009-05-01
303
Security.setProperty("ocsp.enable", "false");
304
System.setProperty("com.sun.security.enableCRLDP", "true");
305
306
PKIXCertPathBuilderResult result =
307
(PKIXCertPathBuilderResult)builder.build(params);
308
309
if (!match(args[0], result.getCertPath().getCertificates().get(0))) {
310
throw new Exception("unexpected certificate");
311
}
312
}
313
}
314
315