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