Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/protocol/https/NewImpl/ComHostnameVerifier.java
38889 views
1
/*
2
* Copyright (c) 2001, 2018, 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
// SunJSSE does not support dynamic system properties, no way to re-use
25
// system properties in samevm/agentvm mode.
26
27
/*
28
* @test
29
* @bug 4474255 4484246
30
* @summary When an application enables anonymous SSL cipher suite,
31
* Hostname verification is not required
32
* @run main/othervm ComHostnameVerifier
33
*/
34
35
import java.io.*;
36
import java.net.*;
37
import java.security.Security;
38
import javax.net.ssl.*;
39
import javax.security.cert.*;
40
import com.sun.net.ssl.HostnameVerifier;
41
import com.sun.net.ssl.HttpsURLConnection;
42
43
/**
44
* Use com.net.net.ssl.HostnameVerifier
45
*/
46
public class ComHostnameVerifier {
47
48
/*
49
* =============================================================
50
* Set the various variables needed for the tests, then
51
* specify what tests to run on each side.
52
*/
53
54
/*
55
* Should we run the client or server in a separate thread?
56
* Both sides can throw exceptions, but do you have a preference
57
* as to which side should be the main thread.
58
*/
59
static boolean separateServerThread = true;
60
61
/*
62
* Is the server ready to serve?
63
*/
64
volatile static boolean serverReady = false;
65
66
/*
67
* Turn on SSL debugging?
68
*/
69
static boolean debug = false;
70
71
/*
72
* If the client or server is doing some kind of object creation
73
* that the other side depends on, and that thread prematurely
74
* exits, you may experience a hang. The test harness will
75
* terminate all hung threads after its timeout has expired,
76
* currently 3 minutes by default, but you might try to be
77
* smart about it....
78
*/
79
80
/**
81
* Returns the path to the file obtained from
82
* parsing the HTML header.
83
*/
84
private static String getPath(DataInputStream in)
85
throws IOException
86
{
87
String line = in.readLine();
88
if (line == null)
89
return null;
90
String path = "";
91
// extract class from GET line
92
if (line.startsWith("GET /")) {
93
line = line.substring(5, line.length()-1).trim();
94
int index = line.indexOf(' ');
95
if (index != -1) {
96
path = line.substring(0, index);
97
}
98
}
99
100
// eat the rest of header
101
do {
102
line = in.readLine();
103
} while ((line.length() != 0) &&
104
(line.charAt(0) != '\r') && (line.charAt(0) != '\n'));
105
106
if (path.length() != 0) {
107
return path;
108
} else {
109
throw new IOException("Malformed Header");
110
}
111
}
112
113
/**
114
* Returns an array of bytes containing the bytes for
115
* the file represented by the argument <b>path</b>.
116
*
117
* In our case, we just pretend to send something back.
118
*
119
* @return the bytes for the file
120
* @exception FileNotFoundException if the file corresponding
121
* to <b>path</b> could not be loaded.
122
*/
123
private byte[] getBytes(String path)
124
throws IOException
125
{
126
return "Hello world, I am here".getBytes();
127
}
128
129
/*
130
* Define the server side of the test.
131
*
132
* If the server prematurely exits, serverReady will be set to true
133
* to avoid infinite hangs.
134
*/
135
void doServerSide() throws Exception {
136
137
SSLServerSocketFactory sslssf =
138
(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
139
SSLServerSocket sslServerSocket =
140
(SSLServerSocket) sslssf.createServerSocket(serverPort);
141
serverPort = sslServerSocket.getLocalPort();
142
143
String ciphers[]= { "SSL_DH_anon_WITH_3DES_EDE_CBC_SHA" };
144
sslServerSocket.setEnabledCipherSuites(ciphers);
145
146
/*
147
* Signal Client, we're ready for his connect.
148
*/
149
serverReady = true;
150
151
SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();
152
DataOutputStream out =
153
new DataOutputStream(sslSocket.getOutputStream());
154
155
try {
156
// get path to class file from header
157
DataInputStream in =
158
new DataInputStream(sslSocket.getInputStream());
159
String path = getPath(in);
160
// retrieve bytecodes
161
byte[] bytecodes = getBytes(path);
162
// send bytecodes in response (assumes HTTP/1.0 or later)
163
try {
164
out.writeBytes("HTTP/1.0 200 OK\r\n");
165
out.writeBytes("Content-Length: " + bytecodes.length + "\r\n");
166
out.writeBytes("Content-Type: text/html\r\n\r\n");
167
out.write(bytecodes);
168
out.flush();
169
} catch (IOException ie) {
170
ie.printStackTrace();
171
return;
172
}
173
174
} catch (Exception e) {
175
e.printStackTrace();
176
// write out error response
177
out.writeBytes("HTTP/1.0 400 " + e.getMessage() + "\r\n");
178
out.writeBytes("Content-Type: text/html\r\n\r\n");
179
out.flush();
180
} finally {
181
// close the socket
182
System.out.println("Server closing socket");
183
sslSocket.close();
184
serverReady = false;
185
}
186
}
187
188
private static class ComSunHTTPSHandlerFactory implements URLStreamHandlerFactory {
189
private static String SUPPORTED_PROTOCOL = "https";
190
191
public URLStreamHandler createURLStreamHandler(String protocol) {
192
if (!protocol.equalsIgnoreCase(SUPPORTED_PROTOCOL))
193
return null;
194
195
return new com.sun.net.ssl.internal.www.protocol.https.Handler();
196
}
197
}
198
199
/*
200
* Define the client side of the test.
201
*
202
* If the server prematurely exits, serverReady will be set to true
203
* to avoid infinite hangs.
204
*/
205
void doClientSide() throws Exception {
206
/*
207
* Wait for server to get started.
208
*/
209
while (!serverReady) {
210
Thread.sleep(50);
211
}
212
213
URL.setURLStreamHandlerFactory(new ComSunHTTPSHandlerFactory());
214
215
System.setProperty("https.cipherSuites",
216
"SSL_DH_anon_WITH_3DES_EDE_CBC_SHA");
217
218
// use the default hostname verifier
219
220
URL url = new URL("https://" + "localhost:" + serverPort +
221
"/etc/hosts");
222
URLConnection urlc = url.openConnection();
223
224
if (!(urlc instanceof com.sun.net.ssl.HttpsURLConnection)) {
225
throw new Exception(
226
"URLConnection ! instanceof " +
227
"com.sun.net.ssl.HttpsURLConnection");
228
}
229
230
BufferedReader in = null;
231
try {
232
in = new BufferedReader(new InputStreamReader(
233
urlc.getInputStream()));
234
String inputLine;
235
System.out.print("Client reading... ");
236
while ((inputLine = in.readLine()) != null)
237
System.out.println(inputLine);
238
System.out.println("Cipher Suite: " +
239
((HttpsURLConnection)urlc).getCipherSuite());
240
in.close();
241
} catch (SSLException e) {
242
if (in != null)
243
in.close();
244
throw e;
245
}
246
System.out.println("Client reports: SUCCESS");
247
}
248
249
/*
250
* =============================================================
251
* The remainder is just support stuff
252
*/
253
254
// use any free port by default
255
volatile int serverPort = 0;
256
257
volatile Exception serverException = null;
258
volatile Exception clientException = null;
259
260
public static void main(String[] args) throws Exception {
261
// re-enable 3DES
262
Security.setProperty("jdk.tls.disabledAlgorithms", "");
263
264
if (debug)
265
System.setProperty("javax.net.debug", "all");
266
267
/*
268
* Start the tests.
269
*/
270
new ComHostnameVerifier();
271
}
272
273
Thread clientThread = null;
274
Thread serverThread = null;
275
276
/*
277
* Primary constructor, used to drive remainder of the test.
278
*
279
* Fork off the other side, then do your work.
280
*/
281
ComHostnameVerifier() throws Exception {
282
if (separateServerThread) {
283
startServer(true);
284
startClient(false);
285
} else {
286
startClient(true);
287
startServer(false);
288
}
289
290
/*
291
* Wait for other side to close down.
292
*/
293
if (separateServerThread) {
294
serverThread.join();
295
} else {
296
clientThread.join();
297
}
298
299
/*
300
* When we get here, the test is pretty much over.
301
*
302
* If the main thread excepted, that propagates back
303
* immediately. If the other thread threw an exception, we
304
* should report back.
305
*/
306
if (serverException != null) {
307
System.out.print("Server Exception:");
308
throw serverException;
309
}
310
if (clientException != null) {
311
System.out.print("Client Exception:");
312
throw clientException;
313
}
314
}
315
316
void startServer(boolean newThread) throws Exception {
317
if (newThread) {
318
serverThread = new Thread() {
319
public void run() {
320
try {
321
doServerSide();
322
} catch (Exception e) {
323
/*
324
* Our server thread just died.
325
*
326
* Release the client, if not active already...
327
*/
328
System.err.println("Server died...");
329
serverReady = true;
330
serverException = e;
331
}
332
}
333
};
334
serverThread.start();
335
} else {
336
doServerSide();
337
}
338
}
339
340
void startClient(boolean newThread) throws Exception {
341
if (newThread) {
342
clientThread = new Thread() {
343
public void run() {
344
try {
345
doClientSide();
346
} catch (Exception e) {
347
/*
348
* Our client thread just died.
349
*/
350
System.err.println("Client died...");
351
clientException = e;
352
}
353
}
354
};
355
clientThread.start();
356
} else {
357
doClientSide();
358
}
359
}
360
}
361
362