|
1 /* |
|
2 * Copyright (c) 2012, 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 7068321 |
|
32 * @summary Support TLS Server Name Indication (SNI) Extension in JSSE Server |
|
33 * @library ../templates |
|
34 * @build SSLCapabilities SSLExplorer |
|
35 * @run main/othervm SSLSocketExplorerFailure SSLv2Hello,SSLv3 |
|
36 * @run main/othervm SSLSocketExplorerFailure SSLv3 |
|
37 * @run main/othervm SSLSocketExplorerFailure TLSv1 |
|
38 * @run main/othervm SSLSocketExplorerFailure TLSv1.1 |
|
39 * @run main/othervm SSLSocketExplorerFailure TLSv1.2 |
|
40 */ |
|
41 |
|
42 import java.io.*; |
|
43 import java.nio.*; |
|
44 import java.nio.channels.*; |
|
45 import java.util.*; |
|
46 import java.net.*; |
|
47 import javax.net.ssl.*; |
|
48 |
|
49 public class SSLSocketExplorerFailure { |
|
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 * Where do we find the keystores? |
|
66 */ |
|
67 static String pathToStores = "../etc"; |
|
68 static String keyStoreFile = "keystore"; |
|
69 static String trustStoreFile = "truststore"; |
|
70 static String passwd = "passphrase"; |
|
71 |
|
72 /* |
|
73 * Is the server ready to serve? |
|
74 */ |
|
75 volatile static boolean serverReady = false; |
|
76 |
|
77 /* |
|
78 * Turn on SSL debugging? |
|
79 */ |
|
80 static boolean debug = false; |
|
81 |
|
82 /* |
|
83 * If the client or server is doing some kind of object creation |
|
84 * that the other side depends on, and that thread prematurely |
|
85 * exits, you may experience a hang. The test harness will |
|
86 * terminate all hung threads after its timeout has expired, |
|
87 * currently 3 minutes by default, but you might try to be |
|
88 * smart about it.... |
|
89 */ |
|
90 |
|
91 /* |
|
92 * Define the server side of the test. |
|
93 * |
|
94 * If the server prematurely exits, serverReady will be set to true |
|
95 * to avoid infinite hangs. |
|
96 */ |
|
97 void doServerSide() throws Exception { |
|
98 |
|
99 ServerSocket serverSocket = new ServerSocket(serverPort); |
|
100 |
|
101 // Signal Client, we're ready for his connect. |
|
102 serverPort = serverSocket.getLocalPort(); |
|
103 serverReady = true; |
|
104 |
|
105 Socket socket = serverSocket.accept(); |
|
106 InputStream ins = socket.getInputStream(); |
|
107 |
|
108 byte[] buffer = new byte[0xFF]; |
|
109 int position = 0; |
|
110 SSLCapabilities capabilities = null; |
|
111 boolean failed = false; |
|
112 try { |
|
113 // Read the header of TLS record |
|
114 while (position < SSLExplorer.RECORD_HEADER_SIZE) { |
|
115 int count = SSLExplorer.RECORD_HEADER_SIZE - position; |
|
116 int n = ins.read(buffer, position, count); |
|
117 if (n < 0) { |
|
118 throw new Exception("unexpected end of stream!"); |
|
119 } |
|
120 position += n; |
|
121 } |
|
122 |
|
123 int recordLength = SSLExplorer.getRequiredSize(buffer, 0, position); |
|
124 if (buffer.length < recordLength) { |
|
125 buffer = Arrays.copyOf(buffer, recordLength); |
|
126 } |
|
127 |
|
128 while (position < recordLength) { |
|
129 int count = recordLength - position; |
|
130 int n = ins.read(buffer, position, count); |
|
131 if (n < 0) { |
|
132 throw new Exception("unexpected end of stream!"); |
|
133 } |
|
134 position += n; |
|
135 } |
|
136 |
|
137 capabilities = SSLExplorer.explore(buffer, 0, recordLength);; |
|
138 if (capabilities != null) { |
|
139 System.out.println("Record version: " + |
|
140 capabilities.getRecordVersion()); |
|
141 System.out.println("Hello version: " + |
|
142 capabilities.getHelloVersion()); |
|
143 } |
|
144 |
|
145 // want an I/O exception |
|
146 throw new IOException("We just want a I/O exception"); |
|
147 } catch (Exception e) { |
|
148 failed = true; |
|
149 } |
|
150 |
|
151 // off course, the above explore failed. Faile to failure handler |
|
152 SSLContext context = SSLContext.getInstance("TLS"); |
|
153 context.init(null, null, null); |
|
154 SSLSocketFactory sslsf = context.getSocketFactory(); |
|
155 ByteArrayInputStream bais = |
|
156 new ByteArrayInputStream(buffer, 0, position); |
|
157 SSLSocket sslSocket = (SSLSocket)sslsf.createSocket(socket, bais, true); |
|
158 |
|
159 try { |
|
160 InputStream sslIS = sslSocket.getInputStream(); |
|
161 OutputStream sslOS = sslSocket.getOutputStream(); |
|
162 |
|
163 sslIS.read(); |
|
164 if (!failed) { |
|
165 sslOS.write(85); |
|
166 sslOS.flush(); |
|
167 } else { |
|
168 sslSocket.close(); |
|
169 } |
|
170 } catch (Exception e) { |
|
171 System.out.println("server exception " + e); |
|
172 } finally { |
|
173 sslSocket.close(); |
|
174 serverSocket.close(); |
|
175 } |
|
176 } |
|
177 |
|
178 |
|
179 /* |
|
180 * Define the client side of the test. |
|
181 * |
|
182 * If the server prematurely exits, serverReady will be set to true |
|
183 * to avoid infinite hangs. |
|
184 */ |
|
185 void doClientSide() throws Exception { |
|
186 |
|
187 /* |
|
188 * Wait for server to get started. |
|
189 */ |
|
190 while (!serverReady) { |
|
191 Thread.sleep(50); |
|
192 } |
|
193 |
|
194 SSLSocketFactory sslsf = |
|
195 (SSLSocketFactory) SSLSocketFactory.getDefault(); |
|
196 SSLSocket sslSocket = (SSLSocket) |
|
197 sslsf.createSocket("localhost", serverPort); |
|
198 |
|
199 // enable the specified TLS protocol |
|
200 sslSocket.setEnabledProtocols(supportedProtocols); |
|
201 |
|
202 try { |
|
203 InputStream sslIS = sslSocket.getInputStream(); |
|
204 OutputStream sslOS = sslSocket.getOutputStream(); |
|
205 |
|
206 sslOS.write(280); |
|
207 sslOS.flush(); |
|
208 sslIS.read(); |
|
209 } catch (Exception e) { |
|
210 System.out.println("client exception " + e); |
|
211 } finally { |
|
212 sslSocket.close(); |
|
213 } |
|
214 } |
|
215 |
|
216 private static String[] supportedProtocols; // supported protocols |
|
217 |
|
218 private static void parseArguments(String[] args) { |
|
219 supportedProtocols = args[0].split(","); |
|
220 } |
|
221 |
|
222 |
|
223 /* |
|
224 * ============================================================= |
|
225 * The remainder is just support stuff |
|
226 */ |
|
227 |
|
228 // use any free port by default |
|
229 volatile int serverPort = 0; |
|
230 |
|
231 volatile Exception serverException = null; |
|
232 volatile Exception clientException = null; |
|
233 |
|
234 public static void main(String[] args) throws Exception { |
|
235 String keyFilename = |
|
236 System.getProperty("test.src", ".") + "/" + pathToStores + |
|
237 "/" + keyStoreFile; |
|
238 String trustFilename = |
|
239 System.getProperty("test.src", ".") + "/" + pathToStores + |
|
240 "/" + trustStoreFile; |
|
241 |
|
242 System.setProperty("javax.net.ssl.keyStore", keyFilename); |
|
243 System.setProperty("javax.net.ssl.keyStorePassword", passwd); |
|
244 System.setProperty("javax.net.ssl.trustStore", trustFilename); |
|
245 System.setProperty("javax.net.ssl.trustStorePassword", passwd); |
|
246 |
|
247 if (debug) |
|
248 System.setProperty("javax.net.debug", "all"); |
|
249 |
|
250 /* |
|
251 * Get the customized arguments. |
|
252 */ |
|
253 parseArguments(args); |
|
254 |
|
255 /* |
|
256 * Start the tests. |
|
257 */ |
|
258 new SSLSocketExplorerFailure(); |
|
259 } |
|
260 |
|
261 Thread clientThread = null; |
|
262 Thread serverThread = null; |
|
263 |
|
264 /* |
|
265 * Primary constructor, used to drive remainder of the test. |
|
266 * |
|
267 * Fork off the other side, then do your work. |
|
268 */ |
|
269 SSLSocketExplorerFailure() throws Exception { |
|
270 try { |
|
271 if (separateServerThread) { |
|
272 startServer(true); |
|
273 startClient(false); |
|
274 } else { |
|
275 startClient(true); |
|
276 startServer(false); |
|
277 } |
|
278 } catch (Exception e) { |
|
279 // swallow for now. Show later |
|
280 } |
|
281 |
|
282 /* |
|
283 * Wait for other side to close down. |
|
284 */ |
|
285 if (separateServerThread) { |
|
286 serverThread.join(); |
|
287 } else { |
|
288 clientThread.join(); |
|
289 } |
|
290 |
|
291 /* |
|
292 * When we get here, the test is pretty much over. |
|
293 * Which side threw the error? |
|
294 */ |
|
295 Exception local; |
|
296 Exception remote; |
|
297 String whichRemote; |
|
298 |
|
299 if (separateServerThread) { |
|
300 remote = serverException; |
|
301 local = clientException; |
|
302 whichRemote = "server"; |
|
303 } else { |
|
304 remote = clientException; |
|
305 local = serverException; |
|
306 whichRemote = "client"; |
|
307 } |
|
308 |
|
309 /* |
|
310 * If both failed, return the curthread's exception, but also |
|
311 * print the remote side Exception |
|
312 */ |
|
313 if ((local != null) && (remote != null)) { |
|
314 System.out.println(whichRemote + " also threw:"); |
|
315 remote.printStackTrace(); |
|
316 System.out.println(); |
|
317 throw local; |
|
318 } |
|
319 |
|
320 if (remote != null) { |
|
321 throw remote; |
|
322 } |
|
323 |
|
324 if (local != null) { |
|
325 throw local; |
|
326 } |
|
327 } |
|
328 |
|
329 void startServer(boolean newThread) throws Exception { |
|
330 if (newThread) { |
|
331 serverThread = new Thread() { |
|
332 public void run() { |
|
333 try { |
|
334 doServerSide(); |
|
335 } catch (Exception e) { |
|
336 /* |
|
337 * Our server thread just died. |
|
338 * |
|
339 * Release the client, if not active already... |
|
340 */ |
|
341 System.err.println("Server died..."); |
|
342 serverReady = true; |
|
343 serverException = e; |
|
344 } |
|
345 } |
|
346 }; |
|
347 serverThread.start(); |
|
348 } else { |
|
349 try { |
|
350 doServerSide(); |
|
351 } catch (Exception e) { |
|
352 serverException = e; |
|
353 } finally { |
|
354 serverReady = true; |
|
355 } |
|
356 } |
|
357 } |
|
358 |
|
359 void startClient(boolean newThread) throws Exception { |
|
360 if (newThread) { |
|
361 clientThread = new Thread() { |
|
362 public void run() { |
|
363 try { |
|
364 doClientSide(); |
|
365 } catch (Exception e) { |
|
366 /* |
|
367 * Our client thread just died. |
|
368 */ |
|
369 System.err.println("Client died..."); |
|
370 clientException = e; |
|
371 } |
|
372 } |
|
373 }; |
|
374 clientThread.start(); |
|
375 } else { |
|
376 try { |
|
377 doClientSide(); |
|
378 } catch (Exception e) { |
|
379 clientException = e; |
|
380 } |
|
381 } |
|
382 } |
|
383 } |