author | asmotrak |
Wed, 15 Apr 2015 07:34:24 +0000 | |
changeset 29902 | dc24eacaae11 |
parent 23052 | 241885315119 |
child 31706 | 895170f33881 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
29902 | 2 |
* Copyright (c) 2003, 2015, 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 4495742 |
|
27 |
* @summary Add non-blocking SSL/TLS functionality, usable with any |
|
28 |
* I/O abstraction |
|
29 |
* |
|
30 |
* This is a bit hacky, meant to test various conditions. The main |
|
31 |
* thing I wanted to do with this was to do buffer reads/writes |
|
32 |
* when buffers were not empty. (buffer.position() = 10) |
|
33 |
* The code could certainly be tightened up a lot. |
|
34 |
* |
|
35 |
* @author Brad Wetmore |
|
29902 | 36 |
* |
37 |
* @run main/othervm ConnectionTest |
|
2 | 38 |
*/ |
39 |
||
40 |
import javax.net.ssl.*; |
|
41 |
import javax.net.ssl.SSLEngineResult.*; |
|
42 |
import java.io.*; |
|
43 |
import java.security.*; |
|
44 |
import java.nio.*; |
|
45 |
||
46 |
public class ConnectionTest { |
|
47 |
||
48 |
private SSLContext sslc; |
|
49 |
private SSLEngine ssle1; |
|
50 |
private SSLEngine ssle2; |
|
51 |
||
23052
241885315119
8032473: Restructure JSSE regression test hierarchy in jdk test
xuelei
parents:
6856
diff
changeset
|
52 |
private static String pathToStores = "../etc"; |
2 | 53 |
private static String keyStoreFile = "keystore"; |
54 |
private static String trustStoreFile = "truststore"; |
|
55 |
private static String passwd = "passphrase"; |
|
56 |
||
57 |
private static String keyFilename = |
|
58 |
System.getProperty("test.src", "./") + "/" + pathToStores + |
|
59 |
"/" + keyStoreFile; |
|
60 |
private static String trustFilename = |
|
61 |
System.getProperty("test.src", "./") + "/" + pathToStores + |
|
62 |
"/" + trustStoreFile; |
|
63 |
||
64 |
private ByteBuffer appIn1, appOut1; |
|
65 |
private ByteBuffer appIn2, appOut2; |
|
66 |
private ByteBuffer oneToTwo, twoToOne; |
|
67 |
private ByteBuffer emptyBuffer; |
|
68 |
||
69 |
private ByteBuffer oneToTwoShifter, twoToOneShifter; |
|
70 |
||
71 |
private String hostname = "hostname"; |
|
72 |
private int portNumber = 77; |
|
73 |
||
74 |
public ConnectionTest() |
|
75 |
throws Exception { |
|
76 |
||
77 |
sslc = getSSLContext(); |
|
78 |
ssle1 = sslc.createSSLEngine(hostname, portNumber); |
|
79 |
ssle2 = sslc.createSSLEngine(); |
|
80 |
||
81 |
ssle1.setEnabledCipherSuites(new String [] { |
|
82 |
"SSL_RSA_WITH_RC4_128_MD5"}); |
|
83 |
||
84 |
createBuffers(); |
|
85 |
} |
|
86 |
||
87 |
private SSLContext getSSLContext() throws Exception { |
|
88 |
KeyStore ks = KeyStore.getInstance("JKS"); |
|
89 |
KeyStore ts = KeyStore.getInstance("JKS"); |
|
90 |
char[] passphrase = "passphrase".toCharArray(); |
|
91 |
||
92 |
ks.load(new FileInputStream(keyFilename), passphrase); |
|
93 |
ts.load(new FileInputStream(trustFilename), passphrase); |
|
94 |
||
95 |
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); |
|
96 |
kmf.init(ks, passphrase); |
|
97 |
||
98 |
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509"); |
|
99 |
tmf.init(ts); |
|
100 |
||
101 |
SSLContext sslCtx = SSLContext.getInstance("TLS"); |
|
102 |
||
103 |
sslCtx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); |
|
104 |
||
105 |
return sslCtx; |
|
106 |
} |
|
107 |
||
108 |
private void createBuffers() { |
|
109 |
// Size the buffers as appropriate. |
|
110 |
SSLSession session = ssle1.getSession(); |
|
111 |
int appBufferMax = session.getApplicationBufferSize(); |
|
112 |
int netBufferMax = session.getPacketBufferSize(); |
|
113 |
||
114 |
appIn1 = ByteBuffer.allocateDirect(appBufferMax + 10); |
|
115 |
appIn2 = ByteBuffer.allocateDirect(appBufferMax + 10); |
|
116 |
||
117 |
appIn1.position(10); |
|
118 |
appIn2.position(10); |
|
119 |
||
120 |
oneToTwo = ByteBuffer.allocateDirect(netBufferMax + 10); |
|
121 |
twoToOne = ByteBuffer.allocateDirect(netBufferMax + 10); |
|
122 |
||
123 |
oneToTwo.position(10); |
|
124 |
twoToOne.position(10); |
|
125 |
oneToTwoShifter = oneToTwo.slice(); |
|
126 |
twoToOneShifter = twoToOne.slice(); |
|
127 |
||
128 |
appOut1 = ByteBuffer.wrap("Hi Engine2, I'm SSLEngine1".getBytes()); |
|
129 |
appOut2 = ByteBuffer.wrap("Hello Engine1, I'm SSLEngine2".getBytes()); |
|
130 |
||
131 |
emptyBuffer = ByteBuffer.allocate(10); |
|
132 |
emptyBuffer.limit(5); |
|
133 |
emptyBuffer.position(emptyBuffer.limit()); |
|
134 |
||
135 |
System.out.println("AppOut1 = " + appOut1); |
|
136 |
System.out.println("AppOut2 = " + appOut2); |
|
137 |
System.out.println(); |
|
138 |
} |
|
139 |
||
140 |
private void checkResult(SSLEngineResult result, Status status, |
|
141 |
HandshakeStatus hsStatus, int consumed, int produced, |
|
142 |
boolean done) throws Exception { |
|
143 |
||
144 |
if ((status != null) && (result.getStatus() != status)) { |
|
145 |
throw new Exception("Unexpected Status: need = " + status + |
|
146 |
" got = " + result.getStatus()); |
|
147 |
} |
|
148 |
||
149 |
if ((hsStatus != null) && (result.getHandshakeStatus() != hsStatus)) { |
|
150 |
throw new Exception("Unexpected hsStatus: need = " + hsStatus + |
|
151 |
" got = " + result.getHandshakeStatus()); |
|
152 |
} |
|
153 |
||
154 |
if ((consumed != -1) && (consumed != result.bytesConsumed())) { |
|
155 |
throw new Exception("Unexpected consumed: need = " + consumed + |
|
156 |
" got = " + result.bytesConsumed()); |
|
157 |
} |
|
158 |
||
159 |
if ((produced != -1) && (produced != result.bytesProduced())) { |
|
160 |
throw new Exception("Unexpected produced: need = " + produced + |
|
161 |
" got = " + result.bytesProduced()); |
|
162 |
} |
|
163 |
||
164 |
if (done && (hsStatus == HandshakeStatus.FINISHED)) { |
|
165 |
throw new Exception( |
|
166 |
"Handshake already reported finished"); |
|
167 |
} |
|
168 |
||
169 |
} |
|
170 |
||
171 |
private boolean isHandshaking(SSLEngine e) { |
|
172 |
return (e.getHandshakeStatus() != HandshakeStatus.NOT_HANDSHAKING); |
|
173 |
} |
|
174 |
||
175 |
private void test() throws Exception { |
|
176 |
ssle1.setUseClientMode(true); |
|
177 |
ssle2.setUseClientMode(false); |
|
178 |
ssle2.setNeedClientAuth(true); |
|
179 |
||
180 |
System.out.println("Testing for early unwrap/wrap"); |
|
181 |
SSLEngineResult result1 = ssle1.unwrap(twoToOne, appIn1); |
|
182 |
SSLEngineResult result2 = ssle2.wrap(appOut2, oneToTwo); |
|
183 |
||
184 |
/* |
|
185 |
* These should not consume/produce data, because they |
|
186 |
* are client and server, respectively, and don't |
|
187 |
* start handshaking this way. |
|
188 |
*/ |
|
189 |
checkResult(result1, Status.OK, HandshakeStatus.NEED_WRAP, |
|
190 |
0, 0, false); |
|
191 |
checkResult(result2, Status.OK, HandshakeStatus.NEED_UNWRAP, |
|
192 |
0, 0, false); |
|
193 |
||
194 |
System.out.println("Doing Initial Handshake"); |
|
195 |
||
196 |
boolean done1 = false; |
|
197 |
boolean done2 = false; |
|
198 |
||
199 |
/* |
|
200 |
* Do initial handshaking |
|
201 |
*/ |
|
202 |
while (isHandshaking(ssle1) || |
|
203 |
isHandshaking(ssle2)) { |
|
204 |
||
205 |
System.out.println("================"); |
|
206 |
||
207 |
result1 = ssle1.wrap(emptyBuffer, oneToTwo); |
|
208 |
checkResult(result1, null, null, 0, -1, done1); |
|
209 |
result2 = ssle2.wrap(emptyBuffer, twoToOne); |
|
210 |
checkResult(result2, null, null, 0, -1, done2); |
|
211 |
||
212 |
if (result1.getHandshakeStatus() == HandshakeStatus.FINISHED) { |
|
213 |
done1 = true; |
|
214 |
} |
|
215 |
||
216 |
if (result2.getHandshakeStatus() == HandshakeStatus.FINISHED) { |
|
217 |
done2 = true; |
|
218 |
} |
|
219 |
||
220 |
System.out.println("wrap1 = " + result1); |
|
221 |
System.out.println("wrap2 = " + result2); |
|
222 |
||
223 |
if (result1.getHandshakeStatus() == HandshakeStatus.NEED_TASK) { |
|
224 |
Runnable runnable; |
|
225 |
while ((runnable = ssle1.getDelegatedTask()) != null) { |
|
226 |
runnable.run(); |
|
227 |
} |
|
228 |
} |
|
229 |
||
230 |
if (result2.getHandshakeStatus() == HandshakeStatus.NEED_TASK) { |
|
231 |
Runnable runnable; |
|
232 |
while ((runnable = ssle2.getDelegatedTask()) != null) { |
|
233 |
runnable.run(); |
|
234 |
} |
|
235 |
} |
|
236 |
||
237 |
oneToTwo.flip(); |
|
238 |
twoToOne.flip(); |
|
239 |
||
240 |
oneToTwo.position(10); |
|
241 |
twoToOne.position(10); |
|
242 |
||
243 |
System.out.println("----"); |
|
244 |
||
245 |
result1 = ssle1.unwrap(twoToOne, appIn1); |
|
246 |
checkResult(result1, null, null, -1, 0, done1); |
|
247 |
result2 = ssle2.unwrap(oneToTwo, appIn2); |
|
248 |
checkResult(result2, null, null, -1, 0, done2); |
|
249 |
||
250 |
if (result1.getHandshakeStatus() == HandshakeStatus.FINISHED) { |
|
251 |
done1 = true; |
|
252 |
} |
|
253 |
||
254 |
if (result2.getHandshakeStatus() == HandshakeStatus.FINISHED) { |
|
255 |
done2 = true; |
|
256 |
} |
|
257 |
||
258 |
if (result1.getHandshakeStatus() == HandshakeStatus.NEED_TASK) { |
|
259 |
Runnable runnable; |
|
260 |
while ((runnable = ssle1.getDelegatedTask()) != null) { |
|
261 |
runnable.run(); |
|
262 |
} |
|
263 |
} |
|
264 |
||
265 |
if (result2.getHandshakeStatus() == HandshakeStatus.NEED_TASK) { |
|
266 |
Runnable runnable; |
|
267 |
while ((runnable = ssle2.getDelegatedTask()) != null) { |
|
268 |
runnable.run(); |
|
269 |
} |
|
270 |
} |
|
271 |
||
272 |
System.out.println("unwrap1 = " + result1); |
|
273 |
System.out.println("unwrap2 = " + result2); |
|
274 |
||
275 |
oneToTwoShifter.position(oneToTwo.position() - 10); |
|
276 |
oneToTwoShifter.limit(oneToTwo.limit() - 10); |
|
277 |
twoToOneShifter.position(twoToOne.position() - 10); |
|
278 |
twoToOneShifter.limit(twoToOne.limit() - 10); |
|
279 |
oneToTwoShifter.compact(); |
|
280 |
twoToOneShifter.compact(); |
|
281 |
oneToTwo.position(oneToTwoShifter.position() + 10); |
|
282 |
oneToTwo.limit(oneToTwoShifter.limit() + 10); |
|
283 |
twoToOne.position(twoToOneShifter.position() + 10); |
|
284 |
twoToOne.limit(twoToOneShifter.limit() + 10); |
|
285 |
} |
|
286 |
||
287 |
System.out.println("\nDONE HANDSHAKING"); |
|
288 |
System.out.println("================"); |
|
289 |
||
290 |
if (!done1 || !done2) { |
|
291 |
throw new Exception("Both should be true:\n" + |
|
292 |
" done1 = " + done1 + " done2 = " + done2); |
|
293 |
} |
|
294 |
||
295 |
String host = ssle1.getPeerHost(); |
|
296 |
int port = ssle1.getPeerPort(); |
|
297 |
if (!host.equals(hostname) || (port != portNumber)) { |
|
298 |
throw new Exception("unexpected host/port " + host + ":" + port); |
|
299 |
} |
|
300 |
||
301 |
host = ssle2.getPeerHost(); |
|
302 |
port = ssle2.getPeerPort(); |
|
303 |
if ((host != null) || (port != -1)) { |
|
304 |
throw new Exception("unexpected host/port " + host + ":" + port); |
|
305 |
} |
|
306 |
||
307 |
SSLSession ssls1 = ssle1.getSession(); |
|
308 |
||
309 |
host = ssls1.getPeerHost(); |
|
310 |
port = ssls1.getPeerPort(); |
|
311 |
if (!host.equals(hostname) || (port != portNumber)) { |
|
312 |
throw new Exception("unexpected host/port " + host + ":" + port); |
|
313 |
} |
|
314 |
||
315 |
SSLSession ssls2 = ssle2.getSession(); |
|
316 |
||
317 |
host = ssls2.getPeerHost(); |
|
318 |
port = ssls2.getPeerPort(); |
|
319 |
if ((host != null) || (port != -1)) { |
|
320 |
throw new Exception("unexpected host/port " + host + ":" + port); |
|
321 |
} |
|
322 |
||
323 |
/* |
|
324 |
* Should be able to write/read a small buffer like this. |
|
325 |
*/ |
|
326 |
int appOut1Len = appOut1.remaining(); |
|
327 |
int appOut2Len = appOut2.remaining(); |
|
328 |
int net1Len; |
|
329 |
int net2Len; |
|
330 |
||
331 |
result1 = ssle1.wrap(appOut1, oneToTwo); |
|
332 |
checkResult(result1, Status.OK, HandshakeStatus.NOT_HANDSHAKING, |
|
333 |
appOut1Len, -1, false); |
|
334 |
result2 = ssle2.wrap(appOut2, twoToOne); |
|
335 |
checkResult(result2, Status.OK, HandshakeStatus.NOT_HANDSHAKING, |
|
336 |
appOut2Len, -1, false); |
|
337 |
net1Len = result1.bytesProduced(); |
|
338 |
net2Len = result2.bytesProduced(); |
|
339 |
||
340 |
System.out.println("wrap1 = " + result1); |
|
341 |
System.out.println("wrap2 = " + result2); |
|
342 |
||
343 |
oneToTwo.flip(); |
|
344 |
twoToOne.flip(); |
|
345 |
||
346 |
oneToTwo.position(10); |
|
347 |
twoToOne.position(10); |
|
348 |
||
349 |
System.out.println("----"); |
|
350 |
||
351 |
result1 = ssle1.unwrap(twoToOne, appIn1); |
|
352 |
checkResult(result1, Status.OK, HandshakeStatus.NOT_HANDSHAKING, |
|
353 |
net2Len, appOut2Len, false); |
|
354 |
result2 = ssle2.unwrap(oneToTwo, appIn2); |
|
355 |
checkResult(result2, Status.OK, HandshakeStatus.NOT_HANDSHAKING, |
|
356 |
net1Len, appOut1Len, false); |
|
357 |
||
358 |
System.out.println("unwrap1 = " + result1); |
|
359 |
System.out.println("unwrap2 = " + result2); |
|
360 |
||
361 |
oneToTwoShifter.position(oneToTwo.position() - 10); |
|
362 |
oneToTwoShifter.limit(oneToTwo.limit() - 10); |
|
363 |
twoToOneShifter.position(twoToOne.position() - 10); |
|
364 |
twoToOneShifter.limit(twoToOne.limit() - 10); |
|
365 |
oneToTwoShifter.compact(); |
|
366 |
twoToOneShifter.compact(); |
|
367 |
oneToTwo.position(oneToTwoShifter.position() + 10); |
|
368 |
oneToTwo.limit(oneToTwoShifter.limit() + 10); |
|
369 |
twoToOne.position(twoToOneShifter.position() + 10); |
|
370 |
twoToOne.limit(twoToOneShifter.limit() + 10); |
|
371 |
||
372 |
ssls2.invalidate(); |
|
373 |
ssle2.beginHandshake(); |
|
374 |
||
375 |
System.out.println("\nRENEGOTIATING"); |
|
376 |
System.out.println("============="); |
|
377 |
||
378 |
done1 = false; |
|
379 |
done2 = false; |
|
380 |
||
381 |
appIn1.clear(); |
|
382 |
appIn2.clear(); |
|
383 |
||
384 |
/* |
|
385 |
* Do a quick test to see if this can do a switch |
|
386 |
* into client mode, at this point, you shouldn't be able |
|
387 |
* to switch back. |
|
388 |
*/ |
|
389 |
try { |
|
390 |
System.out.println("Try to change client mode"); |
|
391 |
ssle2.setUseClientMode(true); |
|
392 |
throw new Exception("Should have thrown IllegalArgumentException"); |
|
393 |
} catch (IllegalArgumentException e) { |
|
394 |
System.out.println("Caught correct IllegalArgumentException"); |
|
395 |
} |
|
396 |
||
397 |
while (isHandshaking(ssle1) || |
|
398 |
isHandshaking(ssle2)) { |
|
399 |
||
400 |
System.out.println("================"); |
|
401 |
||
402 |
result1 = ssle1.wrap(emptyBuffer, oneToTwo); |
|
403 |
checkResult(result1, null, null, 0, -1, done1); |
|
404 |
result2 = ssle2.wrap(emptyBuffer, twoToOne); |
|
405 |
checkResult(result2, null, null, 0, -1, done2); |
|
406 |
||
407 |
if (result1.getHandshakeStatus() == HandshakeStatus.FINISHED) { |
|
408 |
done1 = true; |
|
409 |
} |
|
410 |
||
411 |
if (result2.getHandshakeStatus() == HandshakeStatus.FINISHED) { |
|
412 |
done2 = true; |
|
413 |
} |
|
414 |
||
415 |
System.out.println("wrap1 = " + result1); |
|
416 |
System.out.println("wrap2 = " + result2); |
|
417 |
||
418 |
if (result1.getHandshakeStatus() == HandshakeStatus.NEED_TASK) { |
|
419 |
Runnable runnable; |
|
420 |
while ((runnable = ssle1.getDelegatedTask()) != null) { |
|
421 |
runnable.run(); |
|
422 |
} |
|
423 |
} |
|
424 |
||
425 |
if (result2.getHandshakeStatus() == HandshakeStatus.NEED_TASK) { |
|
426 |
Runnable runnable; |
|
427 |
while ((runnable = ssle2.getDelegatedTask()) != null) { |
|
428 |
runnable.run(); |
|
429 |
} |
|
430 |
} |
|
431 |
||
432 |
oneToTwo.flip(); |
|
433 |
twoToOne.flip(); |
|
434 |
||
435 |
oneToTwo.position(10); |
|
436 |
twoToOne.position(10); |
|
437 |
||
438 |
System.out.println("----"); |
|
439 |
||
440 |
result1 = ssle1.unwrap(twoToOne, appIn1); |
|
441 |
checkResult(result1, null, null, -1, 0, done1); |
|
442 |
result2 = ssle2.unwrap(oneToTwo, appIn2); |
|
443 |
checkResult(result2, null, null, -1, 0, done2); |
|
444 |
||
445 |
if (result1.getHandshakeStatus() == HandshakeStatus.FINISHED) { |
|
446 |
done1 = true; |
|
447 |
} |
|
448 |
||
449 |
if (result2.getHandshakeStatus() == HandshakeStatus.FINISHED) { |
|
450 |
done2 = true; |
|
451 |
} |
|
452 |
||
453 |
System.out.println("unwrap1 = " + result1); |
|
454 |
System.out.println("unwrap2 = " + result2); |
|
455 |
||
456 |
if (result1.getHandshakeStatus() == HandshakeStatus.NEED_TASK) { |
|
457 |
Runnable runnable; |
|
458 |
while ((runnable = ssle1.getDelegatedTask()) != null) { |
|
459 |
runnable.run(); |
|
460 |
} |
|
461 |
} |
|
462 |
||
463 |
if (result2.getHandshakeStatus() == HandshakeStatus.NEED_TASK) { |
|
464 |
Runnable runnable; |
|
465 |
while ((runnable = ssle2.getDelegatedTask()) != null) { |
|
466 |
runnable.run(); |
|
467 |
} |
|
468 |
} |
|
469 |
||
470 |
oneToTwoShifter.position(oneToTwo.position() - 10); |
|
471 |
oneToTwoShifter.limit(oneToTwo.limit() - 10); |
|
472 |
twoToOneShifter.position(twoToOne.position() - 10); |
|
473 |
twoToOneShifter.limit(twoToOne.limit() - 10); |
|
474 |
oneToTwoShifter.compact(); |
|
475 |
twoToOneShifter.compact(); |
|
476 |
oneToTwo.position(oneToTwoShifter.position() + 10); |
|
477 |
oneToTwo.limit(oneToTwoShifter.limit() + 10); |
|
478 |
twoToOne.position(twoToOneShifter.position() + 10); |
|
479 |
twoToOne.limit(twoToOneShifter.limit() + 10); |
|
480 |
} |
|
481 |
||
482 |
host = ssle1.getPeerHost(); |
|
483 |
port = ssle1.getPeerPort(); |
|
484 |
if (!host.equals(hostname) || (port != portNumber)) { |
|
485 |
throw new Exception("unexpected host/port " + host + ":" + port); |
|
486 |
} |
|
487 |
||
488 |
host = ssle2.getPeerHost(); |
|
489 |
port = ssle2.getPeerPort(); |
|
490 |
if ((host != null) || (port != -1)) { |
|
491 |
throw new Exception("unexpected host/port " + host + ":" + port); |
|
492 |
} |
|
493 |
||
494 |
SSLSession ssls3 = ssle2.getSession(); |
|
495 |
||
496 |
host = ssls1.getPeerHost(); |
|
497 |
port = ssls1.getPeerPort(); |
|
498 |
if (!host.equals(hostname) || (port != portNumber)) { |
|
499 |
throw new Exception("unexpected host/port " + host + ":" + port); |
|
500 |
} |
|
501 |
||
502 |
SSLSession ssls4 = ssle2.getSession(); |
|
503 |
||
504 |
host = ssls2.getPeerHost(); |
|
505 |
port = ssls2.getPeerPort(); |
|
506 |
if ((host != null) || (port != -1)) { |
|
507 |
throw new Exception("unexpected host/port " + host + ":" + port); |
|
508 |
} |
|
509 |
||
510 |
System.out.println("\nDoing close"); |
|
511 |
System.out.println("==========="); |
|
512 |
||
513 |
ssle1.closeOutbound(); |
|
514 |
ssle2.closeOutbound(); |
|
515 |
||
516 |
oneToTwo.flip(); |
|
517 |
twoToOne.flip(); |
|
518 |
oneToTwo.position(10); |
|
519 |
twoToOne.position(10); |
|
520 |
||
521 |
appIn1.clear(); |
|
522 |
appIn2.clear(); |
|
523 |
||
524 |
System.out.println("LAST UNWRAP"); |
|
525 |
result1 = ssle1.unwrap(twoToOne, appIn1); |
|
526 |
checkResult(result1, Status.BUFFER_UNDERFLOW, |
|
527 |
HandshakeStatus.NEED_WRAP, 0, 0, false); |
|
528 |
result2 = ssle2.unwrap(oneToTwo, appIn2); |
|
529 |
checkResult(result2, Status.BUFFER_UNDERFLOW, |
|
530 |
HandshakeStatus.NEED_WRAP, 0, 0, false); |
|
531 |
||
532 |
System.out.println("unwrap1 = " + result1); |
|
533 |
System.out.println("unwrap2 = " + result2); |
|
534 |
||
535 |
oneToTwoShifter.position(oneToTwo.position() - 10); |
|
536 |
oneToTwoShifter.limit(oneToTwo.limit() - 10); |
|
537 |
twoToOneShifter.position(twoToOne.position() - 10); |
|
538 |
twoToOneShifter.limit(twoToOne.limit() - 10); |
|
539 |
oneToTwoShifter.compact(); |
|
540 |
twoToOneShifter.compact(); |
|
541 |
oneToTwo.position(oneToTwoShifter.position() + 10); |
|
542 |
oneToTwo.limit(oneToTwoShifter.limit() + 10); |
|
543 |
twoToOne.position(twoToOneShifter.position() + 10); |
|
544 |
twoToOne.limit(twoToOneShifter.limit() + 10); |
|
545 |
||
546 |
System.out.println("LAST WRAP"); |
|
547 |
result1 = ssle1.wrap(appOut1, oneToTwo); |
|
548 |
checkResult(result1, Status.CLOSED, HandshakeStatus.NEED_UNWRAP, |
|
549 |
0, -1, false); |
|
550 |
result2 = ssle2.wrap(appOut2, twoToOne); |
|
551 |
checkResult(result2, Status.CLOSED, HandshakeStatus.NEED_UNWRAP, |
|
552 |
0, -1, false); |
|
553 |
||
554 |
System.out.println("wrap1 = " + result1); |
|
555 |
System.out.println("wrap2 = " + result2); |
|
556 |
||
557 |
net1Len = result1.bytesProduced(); |
|
558 |
net2Len = result2.bytesProduced(); |
|
559 |
||
560 |
oneToTwo.flip(); |
|
561 |
twoToOne.flip(); |
|
562 |
||
563 |
oneToTwo.position(10); |
|
564 |
twoToOne.position(10); |
|
565 |
||
566 |
result1 = ssle1.unwrap(twoToOne, appIn1); |
|
567 |
checkResult(result1, Status.CLOSED, HandshakeStatus.NOT_HANDSHAKING, |
|
568 |
net1Len, 0, false); |
|
569 |
result2 = ssle2.unwrap(oneToTwo, appIn2); |
|
570 |
checkResult(result2, Status.CLOSED, HandshakeStatus.NOT_HANDSHAKING, |
|
571 |
net2Len, 0, false); |
|
572 |
||
573 |
System.out.println("unwrap1 = " + result1); |
|
574 |
System.out.println("unwrap2 = " + result2); |
|
575 |
||
576 |
oneToTwoShifter.position(oneToTwo.position() - 10); |
|
577 |
oneToTwoShifter.limit(oneToTwo.limit() - 10); |
|
578 |
twoToOneShifter.position(twoToOne.position() - 10); |
|
579 |
twoToOneShifter.limit(twoToOne.limit() - 10); |
|
580 |
oneToTwoShifter.compact(); |
|
581 |
twoToOneShifter.compact(); |
|
582 |
oneToTwo.position(oneToTwoShifter.position() + 10); |
|
583 |
oneToTwo.limit(oneToTwoShifter.limit() + 10); |
|
584 |
twoToOne.position(twoToOneShifter.position() + 10); |
|
585 |
twoToOne.limit(twoToOneShifter.limit() + 10); |
|
586 |
||
587 |
System.out.println("EXTRA WRAP"); |
|
588 |
result1 = ssle1.wrap(appOut1, oneToTwo); |
|
589 |
checkResult(result1, Status.CLOSED, HandshakeStatus.NOT_HANDSHAKING, |
|
590 |
0, 0, false); |
|
591 |
result2 = ssle2.wrap(appOut2, twoToOne); |
|
592 |
checkResult(result2, Status.CLOSED, HandshakeStatus.NOT_HANDSHAKING, |
|
593 |
0, 0, false); |
|
594 |
||
595 |
System.out.println("wrap1 = " + result1); |
|
596 |
System.out.println("wrap2 = " + result2); |
|
597 |
||
598 |
oneToTwo.flip(); |
|
599 |
twoToOne.flip(); |
|
600 |
oneToTwo.position(10); |
|
601 |
twoToOne.position(10); |
|
602 |
||
603 |
System.out.println("EXTRA UNWRAP"); |
|
604 |
result1 = ssle1.unwrap(twoToOne, appIn1); |
|
605 |
checkResult(result1, Status.CLOSED, HandshakeStatus.NOT_HANDSHAKING, |
|
606 |
0, 0, false); |
|
607 |
result2 = ssle2.unwrap(oneToTwo, appIn2); |
|
608 |
checkResult(result2, Status.CLOSED, HandshakeStatus.NOT_HANDSHAKING, |
|
609 |
0, 0, false); |
|
610 |
||
611 |
System.out.println("unwrap1 = " + result1); |
|
612 |
System.out.println("unwrap2 = " + result2); |
|
613 |
||
614 |
checkSession(ssls1, ssls2, ssls3, ssls4); |
|
615 |
System.out.println(ssle1); |
|
616 |
System.out.println(ssle2); |
|
617 |
} |
|
618 |
||
619 |
private static void checkSession(SSLSession ssls1, SSLSession ssls2, |
|
620 |
SSLSession ssls3, SSLSession ssls4) throws Exception { |
|
621 |
System.out.println("\nSession Info for SSLEngine1"); |
|
622 |
System.out.println(ssls1); |
|
623 |
System.out.println(ssls1.getCreationTime()); |
|
624 |
String peer1 = ssls1.getPeerHost(); |
|
625 |
System.out.println(peer1); |
|
626 |
String protocol1 = ssls1.getProtocol(); |
|
627 |
System.out.println(protocol1); |
|
628 |
java.security.cert.Certificate cert1 = ssls1.getPeerCertificates()[0]; |
|
629 |
System.out.println(cert1); |
|
630 |
String ciphersuite1 = ssls1.getCipherSuite(); |
|
631 |
System.out.println(ciphersuite1); |
|
632 |
System.out.println(); |
|
633 |
||
634 |
System.out.println("\nSession Info for SSLEngine2"); |
|
635 |
System.out.println(ssls2); |
|
636 |
System.out.println(ssls2.getCreationTime()); |
|
637 |
String peer2 = ssls2.getPeerHost(); |
|
638 |
System.out.println(peer2); |
|
639 |
String protocol2 = ssls2.getProtocol(); |
|
640 |
System.out.println(protocol2); |
|
641 |
java.security.cert.Certificate cert2 = ssls2.getPeerCertificates()[0]; |
|
642 |
System.out.println(cert2); |
|
643 |
String ciphersuite2 = ssls2.getCipherSuite(); |
|
644 |
System.out.println(ciphersuite2); |
|
645 |
System.out.println(); |
|
646 |
||
647 |
if (peer1.equals(peer2)) { |
|
648 |
throw new Exception("peer hostnames not equal"); |
|
649 |
} |
|
650 |
||
651 |
if (!protocol1.equals(protocol2)) { |
|
652 |
throw new Exception("protocols not equal"); |
|
653 |
} |
|
654 |
||
655 |
if (!cert1.equals(cert2)) { |
|
656 |
throw new Exception("certs not equal"); |
|
657 |
} |
|
658 |
||
659 |
if (!ciphersuite1.equals(ciphersuite2)) { |
|
660 |
throw new Exception("ciphersuites not equal"); |
|
661 |
} |
|
662 |
||
663 |
System.out.println("\nSession Info for SSLEngine3"); |
|
664 |
System.out.println(ssls3); |
|
665 |
System.out.println("\nSession Info for SSLEngine4"); |
|
666 |
System.out.println(ssls4); |
|
667 |
||
668 |
if (ssls3.equals(ssls1) || ssls4.equals(ssls2)) { |
|
669 |
throw new Exception("sessions should not be equals"); |
|
670 |
} |
|
671 |
} |
|
672 |
||
673 |
public static void main(String args[]) throws Exception { |
|
29902 | 674 |
// reset the security property to make sure that the algorithms |
675 |
// and keys used in this test are not disabled. |
|
676 |
Security.setProperty("jdk.tls.disabledAlgorithms", ""); |
|
677 |
||
2 | 678 |
ConnectionTest ct = new ConnectionTest(); |
679 |
ct.test(); |
|
680 |
} |
|
681 |
} |