Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/x509/URICertStore/CRLReadTimeout.java
41153 views
1
/*
2
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/*
25
* @test
26
* @bug 8191808
27
* @summary check that CRL download is interrupted if it takes too long
28
* @library /test/lib
29
* @run main/othervm -Dcom.sun.security.crl.readtimeout=1 CRLReadTimeout
30
*/
31
32
import java.io.File;
33
import java.io.InputStream;
34
import java.io.IOException;
35
import java.net.InetSocketAddress;
36
import java.net.SocketTimeoutException;
37
import java.security.KeyStore;
38
import java.security.cert.CertificateFactory;
39
import java.security.cert.CertPath;
40
import java.security.cert.CertPathValidator;
41
import java.security.cert.CertPathValidatorException;
42
import java.security.cert.PKIXParameters;
43
import java.security.cert.PKIXRevocationChecker;
44
import static java.security.cert.PKIXRevocationChecker.Option.*;
45
import java.security.cert.TrustAnchor;
46
import java.security.cert.X509Certificate;
47
import java.util.EnumSet;
48
import java.util.List;
49
import java.util.Set;
50
import com.sun.net.httpserver.HttpServer;
51
52
import jdk.test.lib.SecurityTools;
53
import jdk.test.lib.process.OutputAnalyzer;
54
55
public class CRLReadTimeout {
56
57
public static void main(String[] args) throws Exception {
58
59
String timeout = System.getProperty("com.sun.security.crl.readtimeout");
60
if (timeout == null) {
61
timeout = "15";
62
}
63
System.out.println("Testing timeout of " + timeout + " seconds");
64
65
CrlHttpServer crlServer = new CrlHttpServer(Integer.parseInt(timeout));
66
try {
67
crlServer.start();
68
testTimeout(crlServer.getPort());
69
} finally {
70
crlServer.stop();
71
}
72
}
73
74
private static void testTimeout(int port) throws Exception {
75
76
// create certificate chain with two certs, root and end-entity
77
keytool("-alias duke -dname CN=duke -genkey -keyalg RSA");
78
keytool("-alias root -dname CN=root -genkey -keyalg RSA");
79
keytool("-certreq -alias duke -file duke.req");
80
// set CRL URI to local server
81
keytool("-gencert -infile duke.req -alias root -rfc -outfile duke.cert "
82
+ "-ext crl=uri:http://localhost:" + port + "/crl");
83
keytool("-importcert -file duke.cert -alias duke");
84
85
KeyStore ks = KeyStore.getInstance(new File("ks"),
86
"changeit".toCharArray());
87
X509Certificate cert = (X509Certificate)ks.getCertificate("duke");
88
X509Certificate root = (X509Certificate)ks.getCertificate("root");
89
90
// validate chain
91
CertPathValidator cpv = CertPathValidator.getInstance("PKIX");
92
PKIXRevocationChecker prc =
93
(PKIXRevocationChecker)cpv.getRevocationChecker();
94
prc.setOptions(EnumSet.of(PREFER_CRLS, NO_FALLBACK, SOFT_FAIL));
95
PKIXParameters params =
96
new PKIXParameters(Set.of(new TrustAnchor(root, null)));
97
params.addCertPathChecker(prc);
98
CertificateFactory cf = CertificateFactory.getInstance("X.509");
99
CertPath cp = cf.generateCertPath(List.of(cert));
100
cpv.validate(cp, params);
101
102
// unwrap soft fail exceptions and check for SocketTimeoutException
103
boolean expected = false;
104
for (CertPathValidatorException softFail:prc.getSoftFailExceptions()) {
105
Throwable cause = softFail.getCause();
106
while (cause != null) {
107
if (cause instanceof SocketTimeoutException) {
108
expected = true;
109
break;
110
}
111
cause = cause.getCause();
112
}
113
if (expected) {
114
break;
115
}
116
}
117
if (!expected) {
118
throw new Exception("SocketTimeoutException not thrown");
119
}
120
}
121
122
private static OutputAnalyzer keytool(String cmd) throws Exception {
123
return SecurityTools.keytool("-storepass changeit "
124
+ "-keystore ks " + cmd);
125
}
126
127
private static class CrlHttpServer {
128
129
private final HttpServer server;
130
private final int timeout;
131
132
public CrlHttpServer(int timeout) throws IOException {
133
server = HttpServer.create();
134
this.timeout = timeout;
135
}
136
137
public void start() throws IOException {
138
server.bind(new InetSocketAddress(0), 0);
139
server.createContext("/", t -> {
140
try (InputStream is = t.getRequestBody()) {
141
is.readAllBytes();
142
}
143
try {
144
// sleep for 2 seconds longer to force timeout
145
Thread.sleep((timeout + 2)*1000);
146
} catch (InterruptedException ie) {
147
throw new IOException(ie);
148
}
149
});
150
server.setExecutor(null);
151
server.start();
152
}
153
154
public void stop() {
155
server.stop(0);
156
}
157
158
int getPort() {
159
return server.getAddress().getPort();
160
}
161
}
162
}
163
164