|
1 | 1 | /*
|
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. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
4 | 4 | *
|
5 | 5 | * This code is free software; you can redistribute it and/or modify it
|
|
26 | 26 | * @bug 8311546
|
27 | 27 | * @summary Adopt de-facto standards on x509 Name Constraints with leading dot. Certs
|
28 | 28 | * can be generated by running generate-certs.sh
|
29 |
| - * @library /test/lib |
30 |
| - * @modules java.base/sun.security.x509 |
| 29 | + * @run main LeadingPeriod |
31 | 30 | */
|
32 | 31 |
|
33 | 32 | import java.io.*;
|
|
38 | 37 |
|
39 | 38 | public class LeadingPeriod {
|
40 | 39 |
|
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"); |
57 | 44 | }
|
58 | 45 |
|
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")); |
65 | 49 |
|
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)); |
68 | 53 |
|
69 |
| - Set<TrustAnchor> anchors = Collections.singleton(anchor); |
| 54 | + TrustAnchor anchor = new TrustAnchor((X509Certificate) caCert, null); |
70 | 55 |
|
71 |
| - PKIXParameters params = new PKIXParameters(anchors); |
| 56 | + PKIXParameters params = new PKIXParameters(Collections.singleton(anchor)); |
72 | 57 |
|
73 |
| - // disable certificate revocation checking |
| 58 | + // Disable certificate revocation checking |
74 | 59 | params.setRevocationEnabled(false);
|
75 | 60 |
|
76 |
| - return params; |
77 |
| - } |
| 61 | + // Set validity date, so that validation won't fail when cert expires |
| 62 | + params.setDate(((X509Certificate)targetCert).getNotBefore()); |
78 | 63 |
|
79 |
| - public static void main(String[] args) throws Exception { |
| 64 | + CertPath path = cf.generateCertPath(List.of(targetCert, caCert)); |
80 | 65 |
|
81 | 66 | 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); |
111 | 68 | }
|
112 | 69 | }
|
0 commit comments