Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/net/ssl/HttpsURLConnection/CriticalSubjectAltName.java
41152 views
1
/*
2
* Copyright (c) 2001, 2015, 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
// SunJSSE does not support dynamic system properties, no way to re-use
26
// system properties in samevm/agentvm mode.
27
//
28
29
/*
30
* @test
31
* @bug 6668231
32
* @summary Presence of a critical subjectAltName causes JSSE's SunX509 to
33
* fail trusted checks
34
* @run main/othervm CriticalSubjectAltName
35
* @author Xuelei Fan
36
*/
37
38
/*
39
* This test depends on binary keystore, crisubn.jks and trusted.jks. Because
40
* JAVA keytool cannot generate X509 certificate with SubjectAltName extension,
41
* the certificates are generated with openssl toolkits and then imported into
42
* JAVA keystore.
43
*
44
* The crisubn.jks holds a private key entry and the corresponding X509
45
* certificate issued with an empty Subject field, and a critical
46
* SubjectAltName extension.
47
*
48
* The trusted.jks holds the trusted certificate.
49
*/
50
import java.io.*;
51
import java.net.*;
52
import javax.net.ssl.*;
53
import java.security.Security;
54
import java.security.cert.Certificate;
55
56
public class CriticalSubjectAltName implements HostnameVerifier {
57
/*
58
* =============================================================
59
* Set the various variables needed for the tests, then
60
* specify what tests to run on each side.
61
*/
62
63
/*
64
* Should we run the client or server in a separate thread?
65
* Both sides can throw exceptions, but do you have a preference
66
* as to which side should be the main thread.
67
*/
68
static boolean separateServerThread = true;
69
70
/*
71
* Where do we find the keystores?
72
*/
73
static String pathToStores = "./";
74
static String keyStoreFile = "crisubn.jks";
75
static String trustStoreFile = "trusted.jks";
76
static String passwd = "passphrase";
77
78
/*
79
* Is the server ready to serve?
80
*/
81
volatile static boolean serverReady = false;
82
83
/*
84
* Turn on SSL debugging?
85
*/
86
static boolean debug = false;
87
88
/*
89
* If the client or server is doing some kind of object creation
90
* that the other side depends on, and that thread prematurely
91
* exits, you may experience a hang. The test harness will
92
* terminate all hung threads after its timeout has expired,
93
* currently 3 minutes by default, but you might try to be
94
* smart about it....
95
*/
96
97
/*
98
* Define the server side of the test.
99
*
100
* If the server prematurely exits, serverReady will be set to true
101
* to avoid infinite hangs.
102
*/
103
void doServerSide() throws Exception {
104
SSLServerSocketFactory sslssf =
105
(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
106
SSLServerSocket sslServerSocket =
107
(SSLServerSocket) sslssf.createServerSocket(serverPort);
108
serverPort = sslServerSocket.getLocalPort();
109
110
/*
111
* Signal Client, we're ready for his connect.
112
*/
113
serverReady = true;
114
115
SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();
116
OutputStream sslOS = sslSocket.getOutputStream();
117
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(sslOS));
118
bw.write("HTTP/1.1 200 OK\r\n\r\n\r\n");
119
bw.flush();
120
Thread.sleep(5000);
121
sslSocket.close();
122
}
123
124
/*
125
* Define the client side of the test.
126
*
127
* If the server prematurely exits, serverReady will be set to true
128
* to avoid infinite hangs.
129
*/
130
void doClientSide() throws Exception {
131
132
/*
133
* Wait for server to get started.
134
*/
135
while (!serverReady) {
136
Thread.sleep(50);
137
}
138
139
URL url = new URL("https://localhost:"+serverPort+"/index.html");
140
HttpsURLConnection urlc = (HttpsURLConnection)url.openConnection();
141
urlc.setHostnameVerifier(this);
142
urlc.getInputStream();
143
144
if (urlc.getResponseCode() == -1) {
145
throw new RuntimeException("getResponseCode() returns -1");
146
}
147
}
148
149
/*
150
* =============================================================
151
* The remainder is just support stuff
152
*/
153
154
// use any free port by default
155
volatile int serverPort = 0;
156
157
volatile Exception serverException = null;
158
volatile Exception clientException = null;
159
160
public static void main(String[] args) throws Exception {
161
// MD5 is used in this test case, don't disable MD5 algorithm.
162
Security.setProperty("jdk.certpath.disabledAlgorithms",
163
"MD2, RSA keySize < 1024");
164
Security.setProperty("jdk.tls.disabledAlgorithms",
165
"SSLv3, RC4, DH keySize < 768");
166
167
String keyFilename =
168
System.getProperty("test.src", "./") + "/" + pathToStores +
169
"/" + keyStoreFile;
170
String trustFilename =
171
System.getProperty("test.src", "./") + "/" + pathToStores +
172
"/" + trustStoreFile;
173
174
System.setProperty("javax.net.ssl.keyStore", keyFilename);
175
System.setProperty("javax.net.ssl.keyStorePassword", passwd);
176
System.setProperty("javax.net.ssl.trustStore", trustFilename);
177
System.setProperty("javax.net.ssl.trustStorePassword", passwd);
178
179
if (debug)
180
System.setProperty("javax.net.debug", "all");
181
182
/*
183
* Start the tests.
184
*/
185
new CriticalSubjectAltName();
186
}
187
188
Thread clientThread = null;
189
Thread serverThread = null;
190
191
/*
192
* Primary constructor, used to drive remainder of the test.
193
*
194
* Fork off the other side, then do your work.
195
*/
196
CriticalSubjectAltName() throws Exception {
197
if (separateServerThread) {
198
startServer(true);
199
startClient(false);
200
} else {
201
startClient(true);
202
startServer(false);
203
}
204
205
/*
206
* Wait for other side to close down.
207
*/
208
if (separateServerThread) {
209
serverThread.join();
210
} else {
211
clientThread.join();
212
}
213
214
/*
215
* When we get here, the test is pretty much over.
216
*
217
* If the main thread excepted, that propagates back
218
* immediately. If the other thread threw an exception, we
219
* should report back.
220
*/
221
if (serverException != null)
222
throw serverException;
223
if (clientException != null)
224
throw clientException;
225
}
226
227
void startServer(boolean newThread) throws Exception {
228
if (newThread) {
229
serverThread = new Thread() {
230
public void run() {
231
try {
232
doServerSide();
233
} catch (Exception e) {
234
/*
235
* Our server thread just died.
236
*
237
* Release the client, if not active already...
238
*/
239
System.err.println("Server died...");
240
serverReady = true;
241
serverException = e;
242
}
243
}
244
};
245
serverThread.start();
246
} else {
247
doServerSide();
248
}
249
}
250
251
void startClient(boolean newThread) throws Exception {
252
if (newThread) {
253
clientThread = new Thread() {
254
public void run() {
255
try {
256
doClientSide();
257
} catch (Exception e) {
258
/*
259
* Our client thread just died.
260
*/
261
System.err.println("Client died...");
262
clientException = e;
263
}
264
}
265
};
266
clientThread.start();
267
} else {
268
doClientSide();
269
}
270
}
271
272
// Simple test method to blindly agree that hostname and certname match
273
public boolean verify(String hostname, SSLSession session) {
274
return true;
275
}
276
277
}
278
279