|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2023, 2024, 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
|
|
29 | 29 |
|
30 | 30 | /**
|
31 | 31 | * @test
|
32 |
| - * @bug 8179502 |
| 32 | + * @bug 8179502 8337826 |
33 | 33 | * @summary Enhance OCSP, CRL and Certificate Fetch Timeouts
|
34 | 34 | * @modules java.base/sun.security.x509
|
35 | 35 | * java.base/sun.security.provider.certpath
|
36 | 36 | * java.base/sun.security.util
|
37 | 37 | * @library ../../../../../java/security/testlibrary
|
38 | 38 | * @build CertificateBuilder SimpleOCSPServer
|
39 |
| - * @run main/othervm OCSPTimeout 1000 true |
40 |
| - * @run main/othervm -Dcom.sun.security.ocsp.readtimeout=5 |
41 |
| - * OCSPTimeout 1000 true |
42 |
| - * @run main/othervm -Dcom.sun.security.ocsp.readtimeout=1 |
43 |
| - * OCSPTimeout 5000 false |
44 |
| - * @run main/othervm -Dcom.sun.security.ocsp.readtimeout=1s |
45 |
| - * OCSPTimeout 5000 false |
46 |
| - * @run main/othervm -Dcom.sun.security.ocsp.readtimeout=1500ms |
47 |
| - * OCSPTimeout 5000 false |
48 |
| - * @run main/othervm -Dcom.sun.security.ocsp.readtimeout=4500ms |
49 |
| - * OCSPTimeout 1000 true |
| 39 | + * @run main/othervm -Djava.security.debug=certpath OCSPTimeout 1000 true |
| 40 | + * @run main/othervm -Djava.security.debug=certpath |
| 41 | + * -Dcom.sun.security.ocsp.readtimeout=5 OCSPTimeout 1000 true |
| 42 | + * @run main/othervm -Djava.security.debug=certpath |
| 43 | + * -Dcom.sun.security.ocsp.readtimeout=1 OCSPTimeout 5000 false |
| 44 | + * @run main/othervm -Djava.security.debug=certpath |
| 45 | + * -Dcom.sun.security.ocsp.readtimeout=1s OCSPTimeout 5000 false |
| 46 | + * @run main/othervm -Djava.security.debug=certpath |
| 47 | + * -Dcom.sun.security.ocsp.readtimeout=1500ms OCSPTimeout 5000 false |
| 48 | + * @run main/othervm -Djava.security.debug=certpath |
| 49 | + * -Dcom.sun.security.ocsp.readtimeout=4500ms OCSPTimeout 1000 true |
50 | 50 | */
|
51 | 51 |
|
52 | 52 | import java.io.*;
|
@@ -82,62 +82,72 @@ public class OCSPTimeout {
|
82 | 82 | static SimpleOCSPServer rootOcsp; // Root CA OCSP Responder
|
83 | 83 | static int rootOcspPort; // Port number for root OCSP
|
84 | 84 |
|
85 |
| - public static void main(String args[]) throws Exception { |
| 85 | + public static void main(String[] args) throws Exception { |
86 | 86 | int ocspTimeout = 15000;
|
87 | 87 | boolean expected = false;
|
88 | 88 |
|
89 | 89 | createPKI();
|
90 | 90 |
|
91 |
| - if (args[0] != null) { |
92 |
| - ocspTimeout = Integer.parseInt(args[0]); |
93 |
| - } |
94 |
| - rootOcsp.setDelay(ocspTimeout); |
95 |
| - |
96 |
| - expected = (args[1] != null && Boolean.parseBoolean(args[1])); |
97 |
| - log("Test case expects to " + (expected ? "pass" : "fail")); |
98 |
| - |
99 |
| - // validate chain |
100 |
| - CertPathValidator cpv = CertPathValidator.getInstance("PKIX"); |
101 |
| - PKIXRevocationChecker prc = |
102 |
| - (PKIXRevocationChecker) cpv.getRevocationChecker(); |
103 |
| - prc.setOptions(EnumSet.of(NO_FALLBACK, SOFT_FAIL)); |
104 |
| - PKIXParameters params = |
105 |
| - new PKIXParameters(Set.of(new TrustAnchor(rootCert, null))); |
106 |
| - params.addCertPathChecker(prc); |
107 |
| - CertificateFactory cf = CertificateFactory.getInstance("X.509"); |
108 |
| - CertPath cp = cf.generateCertPath(List.of(eeCert)); |
109 |
| - cpv.validate(cp, params); |
110 |
| - |
111 |
| - // unwrap soft fail exceptions and check for SocketTimeoutException |
112 |
| - List<CertPathValidatorException> softExc = prc.getSoftFailExceptions(); |
113 |
| - if (expected) { |
114 |
| - if (softExc.size() > 0) { |
115 |
| - throw new RuntimeException("Expected to pass, found " + |
116 |
| - softExc.size() + " soft fail exceptions"); |
| 91 | + try { |
| 92 | + if (args[0] != null) { |
| 93 | + ocspTimeout = Integer.parseInt(args[0]); |
117 | 94 | }
|
118 |
| - } else { |
119 |
| - // If we expect to fail the validation then there should be a |
120 |
| - // SocketTimeoutException |
121 |
| - boolean found = false; |
122 |
| - for (CertPathValidatorException softFail : softExc) { |
123 |
| - log("CPVE: " + softFail); |
124 |
| - Throwable cause = softFail.getCause(); |
125 |
| - log("Cause: " + cause); |
126 |
| - while (cause != null) { |
127 |
| - if (cause instanceof SocketTimeoutException) { |
128 |
| - found = true; |
129 |
| - break; |
| 95 | + rootOcsp.setDelay(ocspTimeout); |
| 96 | + |
| 97 | + expected = (args[1] != null && Boolean.parseBoolean(args[1])); |
| 98 | + log("Test case expects to " + (expected ? "pass" : "fail")); |
| 99 | + |
| 100 | + // validate chain |
| 101 | + CertPathValidator cpv = CertPathValidator.getInstance("PKIX"); |
| 102 | + PKIXRevocationChecker prc = |
| 103 | + (PKIXRevocationChecker) cpv.getRevocationChecker(); |
| 104 | + prc.setOptions(EnumSet.of(NO_FALLBACK, SOFT_FAIL)); |
| 105 | + PKIXParameters params = |
| 106 | + new PKIXParameters(Set.of(new TrustAnchor(rootCert, null))); |
| 107 | + params.addCertPathChecker(prc); |
| 108 | + CertificateFactory cf = CertificateFactory.getInstance("X.509"); |
| 109 | + CertPath cp = cf.generateCertPath(List.of(eeCert)); |
| 110 | + cpv.validate(cp, params); |
| 111 | + |
| 112 | + // unwrap soft fail exceptions and check for SocketTimeoutException |
| 113 | + List<CertPathValidatorException> softExc = prc.getSoftFailExceptions(); |
| 114 | + if (expected) { |
| 115 | + if (!softExc.isEmpty()) { |
| 116 | + log("Expected to pass, found " + softExc.size() + |
| 117 | + " soft fail exceptions"); |
| 118 | + for (CertPathValidatorException cpve : softExc) { |
| 119 | + log("Exception: " + cpve); |
130 | 120 | }
|
131 |
| - cause = cause.getCause(); |
| 121 | + throw new RuntimeException("Expected to pass, found " + |
| 122 | + softExc.size() + " soft fail exceptions"); |
132 | 123 | }
|
133 |
| - if (found) { |
134 |
| - break; |
| 124 | + } else { |
| 125 | + // If we expect to fail the validation then there should be a |
| 126 | + // SocketTimeoutException |
| 127 | + boolean found = false; |
| 128 | + for (CertPathValidatorException softFail : softExc) { |
| 129 | + log("CPVE: " + softFail); |
| 130 | + Throwable cause = softFail.getCause(); |
| 131 | + log("Cause: " + cause); |
| 132 | + while (cause != null) { |
| 133 | + if (cause instanceof SocketTimeoutException) { |
| 134 | + found = true; |
| 135 | + break; |
| 136 | + } |
| 137 | + cause = cause.getCause(); |
| 138 | + } |
| 139 | + if (found) { |
| 140 | + break; |
| 141 | + } |
135 | 142 | }
|
136 |
| - } |
137 | 143 |
|
138 |
| - if (!found) { |
139 |
| - throw new RuntimeException("SocketTimeoutException not thrown"); |
| 144 | + if (!found) { |
| 145 | + throw new RuntimeException("SocketTimeoutException not thrown"); |
| 146 | + } |
140 | 147 | }
|
| 148 | + } finally { |
| 149 | + rootOcsp.stop(); |
| 150 | + rootOcsp.shutdownNow(); |
141 | 151 | }
|
142 | 152 | }
|
143 | 153 |
|
|
0 commit comments