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
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 ;
4544import 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+
4861import 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 }
0 commit comments