Skip to content

Commit a6be907

Browse files
committedJan 15, 2025
8347424: Fix and rewrite sun/security/x509/DNSName/LeadingPeriod.java test
Reviewed-by: mullan, bperez
1 parent 4257215 commit a6be907

File tree

1 file changed

+19
-62
lines changed

1 file changed

+19
-62
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2023, 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
@@ -26,8 +26,7 @@
2626
* @bug 8311546
2727
* @summary Adopt de-facto standards on x509 Name Constraints with leading dot. Certs
2828
* can be generated by running generate-certs.sh
29-
* @library /test/lib
30-
* @modules java.base/sun.security.x509
29+
* @run main LeadingPeriod
3130
*/
3231

3332
import java.io.*;
@@ -38,75 +37,33 @@
3837

3938
public class LeadingPeriod {
4039

41-
private static CertPath makeCertPath(String targetCertStr,
42-
PKIXParameters params) throws CertificateException {
43-
// generate certificate from cert strings
44-
CertificateFactory cf = CertificateFactory.getInstance("X.509");
45-
46-
ByteArrayInputStream is;
47-
48-
is = new ByteArrayInputStream(targetCertStr.getBytes());
49-
Certificate targetCert = cf.generateCertificate(is);
50-
// set validity date so that validation won't fail when cert expires
51-
params.setDate(((X509Certificate)targetCert).getNotBefore());
52-
53-
// generate certification path
54-
List<Certificate> list = List.of(targetCert);
55-
56-
return cf.generateCertPath(list);
40+
public static void main(String[] args) throws Exception {
41+
String certs = System.getProperty("test.src", "./") + "/certs/";
42+
validate(certs + "withoutLeadingPeriod");
43+
validate(certs + "withLeadingPeriod");
5744
}
5845

59-
private static PKIXParameters genParams(String caStr) throws Exception {
60-
// generate certificate from cert string
61-
CertificateFactory cf = CertificateFactory.getInstance("X.509");
62-
63-
ByteArrayInputStream is = new ByteArrayInputStream(caStr.getBytes());
64-
Certificate selfSignedCert = cf.generateCertificate(is);
46+
public static void validate(String certPath) throws Exception {
47+
byte[] targetCertBytes = Files.readAllBytes(Paths.get(certPath + "/leaf.pem"));
48+
byte[] caCertBytes = Files.readAllBytes(Paths.get(certPath + "/ca.pem"));
6549

66-
// generate a trust anchor
67-
TrustAnchor anchor = new TrustAnchor((X509Certificate) selfSignedCert, null);
50+
CertificateFactory cf = CertificateFactory.getInstance("X.509");
51+
Certificate caCert = cf.generateCertificate(new ByteArrayInputStream(caCertBytes));
52+
Certificate targetCert = cf.generateCertificate(new ByteArrayInputStream(targetCertBytes));
6853

69-
Set<TrustAnchor> anchors = Collections.singleton(anchor);
54+
TrustAnchor anchor = new TrustAnchor((X509Certificate) caCert, null);
7055

71-
PKIXParameters params = new PKIXParameters(anchors);
56+
PKIXParameters params = new PKIXParameters(Collections.singleton(anchor));
7257

73-
// disable certificate revocation checking
58+
// Disable certificate revocation checking
7459
params.setRevocationEnabled(false);
7560

76-
return params;
77-
}
61+
// Set validity date, so that validation won't fail when cert expires
62+
params.setDate(((X509Certificate)targetCert).getNotBefore());
7863

79-
public static void main(String[] args) throws Exception {
64+
CertPath path = cf.generateCertPath(List.of(targetCert, caCert));
8065

8166
CertPathValidator validator = CertPathValidator.getInstance("PKIX");
82-
83-
// Load certs with a NameConstraint where DNS value does not begin with a period
84-
Path targetFromCAWithoutPeriodPath = Paths.get(System.getProperty(
85-
"test.src", "./") + "/certs/withoutLeadingPeriod/leaf.pem");
86-
String targetFromCAWithoutPeriod = Files.readString(targetFromCAWithoutPeriodPath);
87-
88-
Path caWithoutLeadingPeriodPath = Paths.get(System.getProperty(
89-
"test.src", "./") + "/certs/withoutLeadingPeriod/ca.pem");
90-
String caWithoutLeadingPeriod = Files.readString(caWithoutLeadingPeriodPath);
91-
92-
PKIXParameters paramsForCAWithoutLeadingPeriod = genParams(caWithoutLeadingPeriod);
93-
CertPath pathWithoutLeadingPeriod = makeCertPath(
94-
targetFromCAWithoutPeriod, paramsForCAWithoutLeadingPeriod);
95-
96-
validator.validate(pathWithoutLeadingPeriod, paramsForCAWithoutLeadingPeriod);
97-
98-
// Load certificates with a NameConstraint where the DNS value does begin with a period
99-
Path targetFromCAWithPeriodPath = Paths.get(System.getProperty(
100-
"test.src", "./") + "/certs/withLeadingPeriod/leaf.pem");
101-
String targetFromCAWithPeriod = Files.readString(targetFromCAWithPeriodPath);
102-
103-
Path caWithLeadingPeriodPath = Paths.get(System.getProperty(
104-
"test.src", "./") + "/certs/withLeadingPeriod/ca.pem");
105-
String caWithLeadingPeriod = Files.readString(caWithLeadingPeriodPath);
106-
107-
PKIXParameters paramsForCAWithLeadingPeriod = genParams(caWithLeadingPeriod);
108-
CertPath pathWithLeadingPeriod = makeCertPath(targetFromCAWithPeriod, paramsForCAWithLeadingPeriod);
109-
110-
validator.validate(pathWithLeadingPeriod, paramsForCAWithLeadingPeriod);
67+
validator.validate(path, params);
11168
}
11269
}

0 commit comments

Comments
 (0)
Please sign in to comment.