Skip to content

Commit ab69d2d

Browse files
committed
JDK-8365072: Refactor tests to use PEM API (Phase 2)
1. test/jdk/java/security/cert/CertPathBuilder/selfIssued/StatusLoopDependency.java 2. test/jdk/java/security/cert/CertPathValidator/indirectCRL/CircularCRLTwoLevel.java 3. test/jdk/java/security/cert/CertPathValidator/indirectCRL/CircularCRLTwoLevelRevoked.java
1 parent 0ad919c commit ab69d2d

File tree

3 files changed

+127
-127
lines changed

3 files changed

+127
-127
lines changed

test/jdk/java/security/cert/CertPathBuilder/selfIssued/StatusLoopDependency.java

Lines changed: 46 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009, 2014, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2009, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -33,18 +33,31 @@
3333
* @summary PIT b61: PKI test suite fails because self signed certificates
3434
* are being rejected
3535
* @modules java.base/sun.security.util
36+
* @enablePreview
3637
* @run main/othervm StatusLoopDependency subca
3738
* @run main/othervm StatusLoopDependency subci
3839
* @run main/othervm StatusLoopDependency alice
39-
* @author Xuelei Fan
4040
*/
4141

42-
import java.io.*;
43-
import java.net.SocketException;
44-
import java.util.*;
42+
import java.security.DEREncodable;
43+
import java.security.PEMDecoder;
4544
import java.security.Security;
46-
import java.security.cert.*;
47-
import java.security.cert.CertPathValidatorException.BasicReason;
45+
import java.security.cert.CertPathBuilder;
46+
import java.security.cert.CertStore;
47+
import java.security.cert.Certificate;
48+
import java.security.cert.CollectionCertStoreParameters;
49+
import java.security.cert.PKIXBuilderParameters;
50+
import java.security.cert.PKIXCertPathBuilderResult;
51+
import java.security.cert.TrustAnchor;
52+
import java.security.cert.X509CRL;
53+
import java.security.cert.X509CertSelector;
54+
import java.security.cert.X509Certificate;
55+
import java.util.Collection;
56+
import java.util.Collections;
57+
import java.util.Date;
58+
import java.util.HashSet;
59+
import java.util.Set;
60+
4861
import sun.security.util.DerInputStream;
4962

5063
/**
@@ -183,79 +196,63 @@ public final class StatusLoopDependency {
183196
"N9AvUXxGxU4DruoJuFPcrCI=\n" +
184197
"-----END X509 CRL-----";
185198

186-
private static Set<TrustAnchor> generateTrustAnchors()
187-
throws CertificateException {
188-
// generate certificate from cert string
189-
CertificateFactory cf = CertificateFactory.getInstance("X.509");
199+
private static final PEMDecoder pemDecoder = PEMDecoder.of();
190200

191-
ByteArrayInputStream is =
192-
new ByteArrayInputStream(selfSignedCertStr.getBytes());
193-
Certificate selfSignedCert = cf.generateCertificate(is);
201+
private static Set<TrustAnchor> generateTrustAnchors() {
202+
X509Certificate selfSignedCert = pemDecoder.decode(selfSignedCertStr, X509Certificate.class);
194203

195204
// generate a trust anchor
196205
TrustAnchor anchor =
197-
new TrustAnchor((X509Certificate)selfSignedCert, null);
206+
new TrustAnchor(selfSignedCert, null);
198207

199208
return Collections.singleton(anchor);
200209
}
201210

202211
private static CertStore generateCertificateStore() throws Exception {
203-
Collection entries = new HashSet();
204-
205-
// generate certificate from certificate string
206-
CertificateFactory cf = CertificateFactory.getInstance("X.509");
207212

208-
ByteArrayInputStream is;
213+
Collection<DEREncodable> entries = new HashSet<>();
209214

210-
is = new ByteArrayInputStream(targetCertStr.getBytes());
211-
Certificate cert = cf.generateCertificate(is);
215+
DEREncodable cert = pemDecoder.decode(targetCertStr, X509Certificate.class);
212216
entries.add(cert);
213217

214-
is = new ByteArrayInputStream(subCaCertStr.getBytes());
215-
cert = cf.generateCertificate(is);
218+
cert = pemDecoder.decode(subCaCertStr, X509Certificate.class);
216219
entries.add(cert);
217220

218-
is = new ByteArrayInputStream(selfSignedCertStr.getBytes());
219-
cert = cf.generateCertificate(is);
221+
cert = pemDecoder.decode(selfSignedCertStr, X509Certificate.class);
220222
entries.add(cert);
221223

222-
is = new ByteArrayInputStream(topCrlIssuerCertStr.getBytes());
223-
cert = cf.generateCertificate(is);
224+
cert = pemDecoder.decode(topCrlIssuerCertStr, X509Certificate.class);
224225
entries.add(cert);
225226

226-
is = new ByteArrayInputStream(subCrlIssuerCertStr.getBytes());
227-
cert = cf.generateCertificate(is);
227+
cert = pemDecoder.decode(subCrlIssuerCertStr, X509Certificate.class);
228228
entries.add(cert);
229229

230230
// generate CRL from CRL string
231-
is = new ByteArrayInputStream(topCrlStr.getBytes());
232-
Collection mixes = cf.generateCRLs(is);
233-
entries.addAll(mixes);
231+
DEREncodable mixes = pemDecoder.decode(topCrlStr, X509CRL.class);
232+
entries.add(mixes);
234233

235-
is = new ByteArrayInputStream(subCrlStr.getBytes());
236-
mixes = cf.generateCRLs(is);
237-
entries.addAll(mixes);
234+
mixes = pemDecoder.decode(subCrlStr, X509CRL.class);
235+
entries.add(mixes);
238236

239237
return CertStore.getInstance("Collection",
240-
new CollectionCertStoreParameters(entries));
238+
new CollectionCertStoreParameters(entries));
241239
}
242240

243241
private static X509CertSelector generateSelector(String name)
244242
throws Exception {
245243
X509CertSelector selector = new X509CertSelector();
246244

247245
// generate certificate from certificate string
248-
CertificateFactory cf = CertificateFactory.getInstance("X.509");
249-
ByteArrayInputStream is = null;
246+
String cert;
250247
if (name.equals("subca")) {
251-
is = new ByteArrayInputStream(subCaCertStr.getBytes());
248+
cert = subCaCertStr;
252249
} else if (name.equals("subci")) {
253-
is = new ByteArrayInputStream(subCrlIssuerCertStr.getBytes());
250+
cert = subCrlIssuerCertStr;
254251
} else {
255-
is = new ByteArrayInputStream(targetCertStr.getBytes());
252+
cert = targetCertStr;
256253
}
257254

258-
X509Certificate target = (X509Certificate)cf.generateCertificate(is);
255+
X509Certificate target = pemDecoder.decode(cert, X509Certificate.class);
259256
byte[] extVal = target.getExtensionValue("2.5.29.14");
260257
if (extVal != null) {
261258
DerInputStream in = new DerInputStream(extVal);
@@ -269,21 +266,18 @@ private static X509CertSelector generateSelector(String name)
269266
return selector;
270267
}
271268

272-
private static boolean match(String name, Certificate cert)
273-
throws Exception {
274-
X509CertSelector selector = new X509CertSelector();
269+
private static boolean match(String name, Certificate cert) {
275270

276271
// generate certificate from certificate string
277-
CertificateFactory cf = CertificateFactory.getInstance("X.509");
278-
ByteArrayInputStream is = null;
272+
String newCert;
279273
if (name.equals("subca")) {
280-
is = new ByteArrayInputStream(subCaCertStr.getBytes());
274+
newCert = subCaCertStr;
281275
} else if (name.equals("subci")) {
282-
is = new ByteArrayInputStream(subCrlIssuerCertStr.getBytes());
276+
newCert = subCrlIssuerCertStr;
283277
} else {
284-
is = new ByteArrayInputStream(targetCertStr.getBytes());
278+
newCert = targetCertStr;
285279
}
286-
X509Certificate target = (X509Certificate)cf.generateCertificate(is);
280+
X509Certificate target = pemDecoder.decode(newCert, X509Certificate.class);
287281

288282
return target.equals(cert);
289283
}

test/jdk/java/security/cert/CertPathValidator/indirectCRL/CircularCRLTwoLevel.java

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009, 2014, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2009, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -32,16 +32,34 @@
3232
*
3333
* @bug 6720721
3434
* @summary CRL check with circular depency support needed
35+
* @enablePreview
3536
* @run main/othervm CircularCRLTwoLevel
3637
* @author Xuelei Fan
3738
*/
3839

39-
import java.io.*;
40-
import java.net.SocketException;
41-
import java.util.*;
40+
import java.security.DEREncodable;
41+
import java.security.PEMDecoder;
4242
import java.security.Security;
43-
import java.security.cert.*;
43+
import java.security.cert.CertPath;
44+
import java.security.cert.CertPathValidator;
45+
import java.security.cert.CertPathValidatorException;
4446
import java.security.cert.CertPathValidatorException.BasicReason;
47+
import java.security.cert.CertStore;
48+
import java.security.cert.Certificate;
49+
import java.security.cert.CertificateException;
50+
import java.security.cert.CertificateFactory;
51+
import java.security.cert.CollectionCertStoreParameters;
52+
import java.security.cert.PKIXParameters;
53+
import java.security.cert.TrustAnchor;
54+
import java.security.cert.X509CRL;
55+
import java.security.cert.X509Certificate;
56+
import java.util.Arrays;
57+
import java.util.Collection;
58+
import java.util.Collections;
59+
import java.util.Date;
60+
import java.util.HashSet;
61+
import java.util.List;
62+
import java.util.Set;
4563

4664
public class CircularCRLTwoLevel {
4765

@@ -149,68 +167,53 @@ public class CircularCRLTwoLevel {
149167
"ARGr6Qu68MYGtLMC6ZqP3u0=\n" +
150168
"-----END X509 CRL-----";
151169

170+
private static final PEMDecoder pemDecoder = PEMDecoder.of();
171+
152172
private static CertPath generateCertificatePath()
153173
throws CertificateException {
154174
// generate certificate from cert strings
155175
CertificateFactory cf = CertificateFactory.getInstance("X.509");
156176

157-
ByteArrayInputStream is;
158-
159-
is = new ByteArrayInputStream(targetCertStr.getBytes());
160-
Certificate targetCert = cf.generateCertificate(is);
161-
162-
is = new ByteArrayInputStream(subCaCertStr.getBytes());
163-
Certificate subCaCert = cf.generateCertificate(is);
164-
165-
is = new ByteArrayInputStream(selfSignedCertStr.getBytes());
166-
Certificate selfSignedCert = cf.generateCertificate(is);
177+
Certificate targetCert = pemDecoder.decode(targetCertStr, X509Certificate.class);
178+
Certificate subCaCert = pemDecoder.decode(subCaCertStr, X509Certificate.class);
179+
Certificate selfSignedCert = pemDecoder.decode(selfSignedCertStr, X509Certificate.class);
167180

168181
// generate certification path
169-
List<Certificate> list = Arrays.asList(new Certificate[] {
170-
targetCert, subCaCert, selfSignedCert});
182+
List<Certificate> list = Arrays.asList(targetCert, subCaCert, selfSignedCert);
171183

172184
return cf.generateCertPath(list);
173185
}
174186

175187
private static Set<TrustAnchor> generateTrustAnchors()
176188
throws CertificateException {
177189
// generate certificate from cert string
178-
CertificateFactory cf = CertificateFactory.getInstance("X.509");
179190

180-
ByteArrayInputStream is =
181-
new ByteArrayInputStream(selfSignedCertStr.getBytes());
182-
Certificate selfSignedCert = cf.generateCertificate(is);
191+
final X509Certificate selfSignedCert = pemDecoder.decode(selfSignedCertStr, X509Certificate.class);
183192

184193
// generate a trust anchor
185194
TrustAnchor anchor =
186-
new TrustAnchor((X509Certificate)selfSignedCert, null);
195+
new TrustAnchor(selfSignedCert, null);
187196

188197
return Collections.singleton(anchor);
189198
}
190199

191200
private static CertStore generateCertificateStore() throws Exception {
192-
Collection entries = new HashSet();
201+
Collection<DEREncodable> entries = new HashSet<>();
193202

194203
// generate CRL from CRL string
195-
CertificateFactory cf = CertificateFactory.getInstance("X.509");
196204

197-
ByteArrayInputStream is =
198-
new ByteArrayInputStream(topCrlStr.getBytes());
199-
Collection mixes = cf.generateCRLs(is);
200-
entries.addAll(mixes);
205+
DEREncodable mixes = pemDecoder.decode(topCrlStr, X509CRL.class);
206+
entries.add(mixes);
201207

202-
is = new ByteArrayInputStream(subCrlStr.getBytes());
203-
mixes = cf.generateCRLs(is);
204-
entries.addAll(mixes);
208+
mixes = pemDecoder.decode(subCrlStr, X509CRL.class);
209+
entries.add(mixes);
205210

206211
// intermediate certs
207-
is = new ByteArrayInputStream(topCrlIssuerCertStr.getBytes());
208-
mixes = cf.generateCertificates(is);
209-
entries.addAll(mixes);
212+
mixes = pemDecoder.decode(topCrlIssuerCertStr, X509Certificate.class);
213+
entries.add(mixes);
210214

211-
is = new ByteArrayInputStream(subCrlIssuerCertStr.getBytes());
212-
mixes = cf.generateCertificates(is);
213-
entries.addAll(mixes);
215+
mixes = pemDecoder.decode(subCrlIssuerCertStr, X509Certificate.class);
216+
entries.add(mixes);
214217

215218
return CertStore.getInstance("Collection",
216219
new CollectionCertStoreParameters(entries));

0 commit comments

Comments
 (0)