author | xuelei |
Mon, 25 Jun 2018 13:41:39 -0700 | |
changeset 50768 | 68fa3d4026ea |
parent 47216 | 71c04702a3d5 |
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 4948079 |
|
27 |
* @summary SSLEngineResult needs updating [none yet] |
|
50768 | 28 |
* @ignore the dependent implementation details are changed |
10915 | 29 |
* @run main/othervm -Djsse.enableCBCProtection=false CheckStatus |
30 |
* |
|
2 | 31 |
* @author Brad Wetmore |
32 |
*/ |
|
33 |
||
50768 | 34 |
/* |
35 |
* This is a simple hack to test a bunch of conditions and check |
|
36 |
* their return codes. |
|
37 |
*/ |
|
2 | 38 |
import javax.net.ssl.*; |
39 |
import javax.net.ssl.SSLEngineResult.*; |
|
40 |
import java.io.*; |
|
41 |
import java.security.*; |
|
42 |
import java.nio.*; |
|
43 |
||
44 |
public class CheckStatus { |
|
45 |
||
46 |
private static boolean debug = true; |
|
47 |
||
48 |
private SSLContext sslc; |
|
49 |
private SSLEngine ssle1; // client |
|
50 |
private SSLEngine ssle2; // server |
|
51 |
||
23052
241885315119
8032473: Restructure JSSE regression test hierarchy in jdk test
xuelei
parents:
10915
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 appOut1; // write side of ssle1 |
|
65 |
private ByteBuffer appIn1; // read side of ssle1 |
|
66 |
private ByteBuffer appOut2; // write side of ssle2 |
|
67 |
private ByteBuffer appIn2; // read side of ssle2 |
|
68 |
||
69 |
private ByteBuffer oneToTwo; // "reliable" transport ssle1->ssle2 |
|
70 |
private ByteBuffer twoToOne; // "reliable" transport ssle2->ssle1 |
|
71 |
||
72 |
/* |
|
73 |
* Majority of the test case is here, setup is done below. |
|
74 |
*/ |
|
75 |
||
76 |
private void createSSLEngines() throws Exception { |
|
77 |
ssle1 = sslc.createSSLEngine("client", 1); |
|
78 |
ssle1.setUseClientMode(true); |
|
79 |
||
80 |
ssle2 = sslc.createSSLEngine("server", 2); |
|
81 |
ssle2.setUseClientMode(false); |
|
82 |
} |
|
83 |
||
84 |
private boolean isHandshaking(SSLEngine e) { |
|
85 |
return (e.getHandshakeStatus() != HandshakeStatus.NOT_HANDSHAKING); |
|
86 |
} |
|
87 |
||
88 |
private void checkResult(ByteBuffer bbIn, ByteBuffer bbOut, |
|
89 |
SSLEngineResult result, |
|
90 |
Status status, HandshakeStatus hsStatus, |
|
91 |
int consumed, int produced) |
|
92 |
throws Exception { |
|
93 |
||
94 |
if ((status != null) && (result.getStatus() != status)) { |
|
95 |
throw new Exception("Unexpected Status: need = " + status + |
|
96 |
" got = " + result.getStatus()); |
|
97 |
} |
|
98 |
||
99 |
if ((hsStatus != null) && (result.getHandshakeStatus() != hsStatus)) { |
|
100 |
throw new Exception("Unexpected hsStatus: need = " + hsStatus + |
|
101 |
" got = " + result.getHandshakeStatus()); |
|
102 |
} |
|
103 |
||
104 |
if ((consumed != -1) && (consumed != result.bytesConsumed())) { |
|
105 |
throw new Exception("Unexpected consumed: need = " + consumed + |
|
106 |
" got = " + result.bytesConsumed()); |
|
107 |
} |
|
108 |
||
109 |
if ((produced != -1) && (produced != result.bytesProduced())) { |
|
110 |
throw new Exception("Unexpected produced: need = " + produced + |
|
111 |
" got = " + result.bytesProduced()); |
|
112 |
} |
|
113 |
||
114 |
if ((consumed != -1) && (bbIn.position() != result.bytesConsumed())) { |
|
115 |
throw new Exception("Consumed " + bbIn.position() + |
|
116 |
" != " + consumed); |
|
117 |
} |
|
118 |
||
119 |
if ((produced != -1) && (bbOut.position() != result.bytesProduced())) { |
|
120 |
throw new Exception("produced " + bbOut.position() + |
|
121 |
" != " + produced); |
|
122 |
} |
|
123 |
} |
|
124 |
||
125 |
private void test() throws Exception { |
|
126 |
createSSLEngines(); |
|
127 |
createBuffers(); |
|
128 |
||
129 |
SSLEngineResult result1; // ssle1's results from last operation |
|
130 |
SSLEngineResult result2; // ssle2's results from last operation |
|
131 |
||
132 |
String [] suite1 = new String [] { |
|
133 |
"SSL_RSA_WITH_RC4_128_MD5" }; |
|
134 |
String [] suite2 = new String [] { |
|
135 |
"SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA" }; |
|
136 |
||
137 |
ssle1.setEnabledCipherSuites(suite1); |
|
138 |
ssle2.setEnabledCipherSuites(suite1); |
|
139 |
||
140 |
log("================"); |
|
141 |
||
142 |
log("unexpected empty unwrap"); |
|
143 |
twoToOne.limit(0); |
|
144 |
result1 = ssle1.unwrap(twoToOne, appIn1); |
|
145 |
checkResult(twoToOne, appIn1, result1, |
|
146 |
Status.OK, HandshakeStatus.NEED_WRAP, 0, 0); |
|
147 |
twoToOne.limit(twoToOne.capacity()); |
|
148 |
||
149 |
log("======================================"); |
|
150 |
log("client hello"); |
|
151 |
result1 = ssle1.wrap(appOut1, oneToTwo); |
|
152 |
checkResult(appOut1, oneToTwo, result1, |
|
153 |
Status.OK, HandshakeStatus.NEED_UNWRAP, 0, -1); |
|
154 |
||
155 |
oneToTwo.flip(); |
|
156 |
result2 = ssle2.unwrap(oneToTwo, appIn2); |
|
157 |
||
158 |
checkResult(oneToTwo, appIn2, result2, |
|
159 |
Status.OK, HandshakeStatus.NEED_TASK, result1.bytesProduced(), 0); |
|
160 |
runDelegatedTasks(ssle2); |
|
161 |
||
162 |
oneToTwo.compact(); |
|
163 |
||
164 |
log("Check for unwrap when wrap needed"); |
|
165 |
result2 = ssle2.unwrap(oneToTwo, appIn2); |
|
166 |
checkResult(oneToTwo, appIn2, result2, |
|
167 |
Status.OK, HandshakeStatus.NEED_WRAP, 0, 0); |
|
168 |
||
169 |
log("======================================"); |
|
170 |
log("ServerHello"); |
|
171 |
||
172 |
result2 = ssle2.wrap(appOut2, twoToOne); |
|
173 |
checkResult(appOut2, twoToOne, result2, |
|
174 |
Status.OK, HandshakeStatus.NEED_UNWRAP, 0, -1); |
|
175 |
twoToOne.flip(); |
|
176 |
||
177 |
result1 = ssle1.unwrap(twoToOne, appIn1); |
|
178 |
checkResult(twoToOne, appIn1, result1, |
|
179 |
Status.OK, HandshakeStatus.NEED_TASK, result2.bytesProduced(), 0); |
|
180 |
twoToOne.compact(); |
|
181 |
||
182 |
runDelegatedTasks(ssle1); |
|
183 |
||
184 |
log("======================================"); |
|
185 |
log("Key Exchange"); |
|
186 |
result1 = ssle1.wrap(appOut1, oneToTwo); |
|
187 |
checkResult(appOut1, oneToTwo, result1, |
|
188 |
Status.OK, HandshakeStatus.NEED_WRAP, 0, -1); |
|
189 |
||
190 |
oneToTwo.flip(); |
|
191 |
result2 = ssle2.unwrap(oneToTwo, appIn2); |
|
192 |
||
193 |
checkResult(oneToTwo, appIn2, result2, |
|
194 |
Status.OK, HandshakeStatus.NEED_TASK, result1.bytesProduced(), 0); |
|
195 |
runDelegatedTasks(ssle2); |
|
196 |
||
197 |
oneToTwo.compact(); |
|
198 |
||
199 |
log("======================================"); |
|
200 |
log("CCS"); |
|
201 |
result1 = ssle1.wrap(appOut1, oneToTwo); |
|
202 |
checkResult(appOut1, oneToTwo, result1, |
|
203 |
Status.OK, HandshakeStatus.NEED_WRAP, 0, -1); |
|
204 |
||
205 |
oneToTwo.flip(); |
|
206 |
result2 = ssle2.unwrap(oneToTwo, appIn2); |
|
207 |
||
208 |
checkResult(oneToTwo, appIn2, result2, |
|
209 |
Status.OK, HandshakeStatus.NEED_UNWRAP, |
|
210 |
result1.bytesProduced(), 0); |
|
211 |
||
212 |
oneToTwo.compact(); |
|
213 |
||
214 |
log("======================================"); |
|
215 |
log("Finished"); |
|
216 |
result1 = ssle1.wrap(appOut1, oneToTwo); |
|
217 |
checkResult(appOut1, oneToTwo, result1, |
|
218 |
Status.OK, HandshakeStatus.NEED_UNWRAP, 0, -1); |
|
219 |
||
220 |
oneToTwo.flip(); |
|
221 |
result2 = ssle2.unwrap(oneToTwo, appIn2); |
|
222 |
||
223 |
checkResult(oneToTwo, appIn2, result2, |
|
224 |
Status.OK, HandshakeStatus.NEED_WRAP, result1.bytesProduced(), 0); |
|
225 |
||
226 |
oneToTwo.compact(); |
|
227 |
||
228 |
log("======================================"); |
|
229 |
log("CCS"); |
|
230 |
||
231 |
result2 = ssle2.wrap(appOut2, twoToOne); |
|
232 |
checkResult(appOut2, twoToOne, result2, |
|
233 |
Status.OK, HandshakeStatus.NEED_WRAP, 0, -1); |
|
234 |
twoToOne.flip(); |
|
235 |
||
236 |
result1 = ssle1.unwrap(twoToOne, appIn1); |
|
237 |
checkResult(twoToOne, appIn1, result1, |
|
238 |
Status.OK, HandshakeStatus.NEED_UNWRAP, result2.bytesProduced(), 0); |
|
239 |
twoToOne.compact(); |
|
240 |
||
241 |
log("======================================"); |
|
242 |
log("FINISHED"); |
|
243 |
||
244 |
result2 = ssle2.wrap(appOut2, twoToOne); |
|
245 |
checkResult(appOut2, twoToOne, result2, |
|
246 |
Status.OK, HandshakeStatus.FINISHED, 0, -1); |
|
247 |
twoToOne.flip(); |
|
248 |
||
249 |
result1 = ssle1.unwrap(twoToOne, appIn1); |
|
250 |
checkResult(twoToOne, appIn1, result1, |
|
251 |
Status.OK, HandshakeStatus.FINISHED, result2.bytesProduced(), 0); |
|
252 |
twoToOne.compact(); |
|
253 |
||
254 |
log("======================================"); |
|
255 |
log("Check Session/Ciphers"); |
|
256 |
||
257 |
String suite = ssle1.getSession().getCipherSuite(); |
|
258 |
if (!suite.equals(suite1[0])) { |
|
259 |
throw new Exception("suites not equal: " + suite + "/" + |
|
260 |
suite1[0]); |
|
261 |
} |
|
262 |
||
263 |
suite = ssle2.getSession().getCipherSuite(); |
|
264 |
if (!suite.equals(suite1[0])) { |
|
265 |
throw new Exception("suites not equal: " + suite + "/" + |
|
266 |
suite1[0]); |
|
267 |
} |
|
268 |
||
269 |
log("======================================"); |
|
270 |
log("DATA"); |
|
271 |
||
272 |
result1 = ssle1.wrap(appOut1, oneToTwo); |
|
273 |
checkResult(appOut1, oneToTwo, result1, |
|
274 |
Status.OK, HandshakeStatus.NOT_HANDSHAKING, |
|
275 |
appOut1.capacity(), -1); |
|
276 |
oneToTwo.flip(); |
|
277 |
||
278 |
result2 = ssle2.wrap(appOut2, twoToOne); |
|
279 |
checkResult(appOut2, twoToOne, result2, |
|
280 |
Status.OK, HandshakeStatus.NOT_HANDSHAKING, |
|
281 |
appOut2.capacity(), -1); |
|
282 |
twoToOne.flip(); |
|
283 |
||
284 |
SSLEngineResult result3 = ssle1.unwrap(twoToOne, appIn1); |
|
285 |
checkResult(twoToOne, appIn1, result3, |
|
286 |
Status.OK, HandshakeStatus.NOT_HANDSHAKING, |
|
287 |
result2.bytesProduced(), result2.bytesConsumed()); |
|
288 |
twoToOne.compact(); |
|
289 |
||
290 |
SSLEngineResult result4 = ssle2.unwrap(oneToTwo, appIn2); |
|
291 |
checkResult(oneToTwo, appIn2, result4, |
|
292 |
Status.OK, HandshakeStatus.NOT_HANDSHAKING, |
|
293 |
result1.bytesProduced(), result1.bytesConsumed()); |
|
294 |
oneToTwo.compact(); |
|
295 |
||
296 |
appIn1.clear(); |
|
297 |
appIn2.clear(); |
|
298 |
appOut1.rewind(); |
|
299 |
appOut2.rewind(); |
|
300 |
||
301 |
log("======================================"); |
|
302 |
log("RENEGOTIATE"); |
|
303 |
||
304 |
ssle2.getSession().invalidate(); |
|
305 |
ssle2.setNeedClientAuth(true); |
|
306 |
||
307 |
ssle1.setEnabledCipherSuites(suite2); |
|
308 |
ssle2.setEnabledCipherSuites(suite2); |
|
309 |
||
310 |
ssle2.beginHandshake(); |
|
311 |
||
312 |
log("======================================"); |
|
313 |
log("HelloRequest"); |
|
314 |
||
315 |
result2 = ssle2.wrap(appOut2, twoToOne); |
|
316 |
checkResult(appOut2, twoToOne, result2, |
|
317 |
Status.OK, HandshakeStatus.NEED_UNWRAP, 0, -1); |
|
318 |
twoToOne.flip(); |
|
319 |
||
320 |
result1 = ssle1.unwrap(twoToOne, appIn1); |
|
321 |
checkResult(twoToOne, appIn1, result1, |
|
322 |
Status.OK, HandshakeStatus.NEED_TASK, result2.bytesProduced(), 0); |
|
323 |
twoToOne.compact(); |
|
324 |
||
325 |
runDelegatedTasks(ssle1); |
|
326 |
||
327 |
log("======================================"); |
|
328 |
log("ClientHello"); |
|
329 |
||
330 |
result1 = ssle1.wrap(appOut1, oneToTwo); |
|
331 |
checkResult(appOut1, oneToTwo, result1, |
|
332 |
Status.OK, HandshakeStatus.NEED_UNWRAP, 0, -1); |
|
333 |
||
334 |
oneToTwo.flip(); |
|
335 |
result2 = ssle2.unwrap(oneToTwo, appIn2); |
|
336 |
||
337 |
checkResult(oneToTwo, appIn2, result2, |
|
338 |
Status.OK, HandshakeStatus.NEED_TASK, result1.bytesProduced(), 0); |
|
339 |
runDelegatedTasks(ssle2); |
|
340 |
||
341 |
oneToTwo.compact(); |
|
342 |
||
343 |
log("======================================"); |
|
344 |
log("CLIENT->SERVER DATA IN MIDDLE OF HANDSHAKE"); |
|
345 |
||
346 |
result1 = ssle1.wrap(appOut1, oneToTwo); |
|
347 |
checkResult(appOut1, oneToTwo, result1, |
|
348 |
Status.OK, HandshakeStatus.NEED_UNWRAP, |
|
349 |
appOut1.capacity(), -1); |
|
350 |
oneToTwo.flip(); |
|
351 |
||
352 |
result4 = ssle2.unwrap(oneToTwo, appIn2); |
|
353 |
checkResult(oneToTwo, appIn2, result4, |
|
354 |
Status.OK, HandshakeStatus.NEED_WRAP, |
|
355 |
result1.bytesProduced(), result1.bytesConsumed()); |
|
356 |
oneToTwo.compact(); |
|
357 |
||
358 |
appIn2.clear(); |
|
359 |
appOut1.rewind(); |
|
360 |
||
361 |
log("======================================"); |
|
362 |
log("ServerHello"); |
|
363 |
||
364 |
result2 = ssle2.wrap(appOut2, twoToOne); |
|
365 |
checkResult(appOut2, twoToOne, result2, |
|
366 |
Status.OK, HandshakeStatus.NEED_UNWRAP, 0, -1); |
|
367 |
twoToOne.flip(); |
|
368 |
||
369 |
result1 = ssle1.unwrap(twoToOne, appIn1); |
|
370 |
checkResult(twoToOne, appIn1, result1, |
|
371 |
Status.OK, HandshakeStatus.NEED_TASK, result2.bytesProduced(), 0); |
|
372 |
twoToOne.compact(); |
|
373 |
||
374 |
runDelegatedTasks(ssle1); |
|
375 |
||
376 |
log("======================================"); |
|
377 |
log("SERVER->CLIENT DATA IN MIDDLE OF HANDSHAKE"); |
|
378 |
||
379 |
result2 = ssle2.wrap(appOut2, twoToOne); |
|
380 |
checkResult(appOut2, twoToOne, result2, |
|
381 |
Status.OK, HandshakeStatus.NEED_UNWRAP, |
|
382 |
appOut2.capacity(), -1); |
|
383 |
twoToOne.flip(); |
|
384 |
||
385 |
result3 = ssle1.unwrap(twoToOne, appIn1); |
|
386 |
checkResult(twoToOne, appIn1, result3, |
|
387 |
Status.OK, HandshakeStatus.NEED_WRAP, |
|
388 |
result2.bytesProduced(), result2.bytesConsumed()); |
|
389 |
twoToOne.compact(); |
|
390 |
||
391 |
appIn1.clear(); |
|
392 |
appOut2.rewind(); |
|
393 |
||
394 |
log("======================================"); |
|
30904 | 395 |
log("Client Cert and Key Exchange"); |
2 | 396 |
result1 = ssle1.wrap(appOut1, oneToTwo); |
397 |
checkResult(appOut1, oneToTwo, result1, |
|
398 |
Status.OK, HandshakeStatus.NEED_WRAP, 0, -1); |
|
399 |
||
400 |
oneToTwo.flip(); |
|
401 |
result2 = ssle2.unwrap(oneToTwo, appIn2); |
|
402 |
||
403 |
checkResult(oneToTwo, appIn2, result2, |
|
404 |
Status.OK, HandshakeStatus.NEED_TASK, result1.bytesProduced(), 0); |
|
405 |
runDelegatedTasks(ssle2); |
|
406 |
||
407 |
oneToTwo.compact(); |
|
408 |
||
409 |
log("======================================"); |
|
410 |
log("CCS"); |
|
411 |
result1 = ssle1.wrap(appOut1, oneToTwo); |
|
412 |
checkResult(appOut1, oneToTwo, result1, |
|
413 |
Status.OK, HandshakeStatus.NEED_WRAP, 0, -1); |
|
414 |
||
415 |
oneToTwo.flip(); |
|
416 |
result2 = ssle2.unwrap(oneToTwo, appIn2); |
|
417 |
||
418 |
checkResult(oneToTwo, appIn2, result2, |
|
419 |
Status.OK, HandshakeStatus.NEED_UNWRAP, |
|
420 |
result1.bytesProduced(), 0); |
|
421 |
||
422 |
oneToTwo.compact(); |
|
423 |
||
424 |
log("======================================"); |
|
425 |
log("Finished"); |
|
426 |
result1 = ssle1.wrap(appOut1, oneToTwo); |
|
427 |
checkResult(appOut1, oneToTwo, result1, |
|
428 |
Status.OK, HandshakeStatus.NEED_UNWRAP, 0, -1); |
|
429 |
||
430 |
oneToTwo.flip(); |
|
431 |
result2 = ssle2.unwrap(oneToTwo, appIn2); |
|
432 |
||
433 |
checkResult(oneToTwo, appIn2, result2, |
|
434 |
Status.OK, HandshakeStatus.NEED_WRAP, result1.bytesProduced(), 0); |
|
435 |
||
436 |
oneToTwo.compact(); |
|
437 |
||
438 |
log("======================================"); |
|
439 |
log("CCS"); |
|
440 |
||
441 |
result2 = ssle2.wrap(appOut2, twoToOne); |
|
442 |
checkResult(appOut2, twoToOne, result2, |
|
443 |
Status.OK, HandshakeStatus.NEED_WRAP, 0, -1); |
|
444 |
twoToOne.flip(); |
|
445 |
||
446 |
result1 = ssle1.unwrap(twoToOne, appIn1); |
|
447 |
checkResult(twoToOne, appIn1, result1, |
|
448 |
Status.OK, HandshakeStatus.NEED_UNWRAP, result2.bytesProduced(), 0); |
|
449 |
twoToOne.compact(); |
|
450 |
||
451 |
log("======================================"); |
|
452 |
log("FINISHED"); |
|
453 |
||
454 |
result2 = ssle2.wrap(appOut2, twoToOne); |
|
455 |
checkResult(appOut2, twoToOne, result2, |
|
456 |
Status.OK, HandshakeStatus.FINISHED, 0, -1); |
|
457 |
twoToOne.flip(); |
|
458 |
||
459 |
result1 = ssle1.unwrap(twoToOne, appIn1); |
|
460 |
checkResult(twoToOne, appIn1, result1, |
|
461 |
Status.OK, HandshakeStatus.FINISHED, result2.bytesProduced(), 0); |
|
462 |
twoToOne.compact(); |
|
463 |
||
464 |
log("======================================"); |
|
465 |
log("Check Session/Ciphers"); |
|
466 |
||
467 |
suite = ssle1.getSession().getCipherSuite(); |
|
468 |
if (!suite.equals(suite2[0])) { |
|
469 |
throw new Exception("suites not equal: " + suite + "/" + |
|
470 |
suite2[0]); |
|
471 |
} |
|
472 |
||
473 |
suite = ssle2.getSession().getCipherSuite(); |
|
474 |
if (!suite.equals(suite2[0])) { |
|
475 |
throw new Exception("suites not equal: " + suite + "/" + |
|
476 |
suite2[0]); |
|
477 |
} |
|
478 |
||
479 |
log("======================================"); |
|
480 |
log("DATA USING NEW SESSION"); |
|
481 |
||
482 |
result1 = ssle1.wrap(appOut1, oneToTwo); |
|
483 |
checkResult(appOut1, oneToTwo, result1, |
|
484 |
Status.OK, HandshakeStatus.NOT_HANDSHAKING, |
|
485 |
appOut1.capacity(), -1); |
|
486 |
oneToTwo.flip(); |
|
487 |
||
488 |
result2 = ssle2.wrap(appOut2, twoToOne); |
|
489 |
checkResult(appOut2, twoToOne, result2, |
|
490 |
Status.OK, HandshakeStatus.NOT_HANDSHAKING, |
|
491 |
appOut2.capacity(), -1); |
|
492 |
twoToOne.flip(); |
|
493 |
||
494 |
result3 = ssle1.unwrap(twoToOne, appIn1); |
|
495 |
checkResult(twoToOne, appIn1, result3, |
|
496 |
Status.OK, HandshakeStatus.NOT_HANDSHAKING, |
|
497 |
result2.bytesProduced(), result2.bytesConsumed()); |
|
498 |
twoToOne.compact(); |
|
499 |
||
500 |
result4 = ssle2.unwrap(oneToTwo, appIn2); |
|
501 |
checkResult(oneToTwo, appIn2, result4, |
|
502 |
Status.OK, HandshakeStatus.NOT_HANDSHAKING, |
|
503 |
result1.bytesProduced(), result1.bytesConsumed()); |
|
504 |
oneToTwo.compact(); |
|
505 |
||
506 |
appIn1.clear(); |
|
507 |
appIn2.clear(); |
|
508 |
appOut1.rewind(); |
|
509 |
appOut2.rewind(); |
|
510 |
||
511 |
log("======================================"); |
|
512 |
log("CN"); |
|
513 |
||
514 |
if (isHandshaking(ssle1)) { |
|
515 |
throw new Exception("ssle1 IS handshaking"); |
|
516 |
} |
|
517 |
||
518 |
if (isHandshaking(ssle2)) { |
|
519 |
throw new Exception("ssle2 IS handshaking"); |
|
520 |
} |
|
521 |
||
522 |
ssle2.closeOutbound(); |
|
523 |
||
524 |
if (!isHandshaking(ssle2)) { |
|
525 |
throw new Exception("ssle1 IS NOT handshaking"); |
|
526 |
} |
|
527 |
||
528 |
appOut1.rewind(); |
|
529 |
appOut2.rewind(); |
|
530 |
||
531 |
result2 = ssle2.wrap(appOut2, twoToOne); |
|
532 |
checkResult(appOut2, twoToOne, result2, |
|
533 |
Status.CLOSED, HandshakeStatus.NEED_UNWRAP, 0, -1); |
|
534 |
twoToOne.flip(); |
|
535 |
||
536 |
if (ssle1.isInboundDone()) { |
|
537 |
throw new Exception("ssle1 inboundDone"); |
|
538 |
} |
|
539 |
||
540 |
result1 = ssle1.unwrap(twoToOne, appIn1); |
|
541 |
checkResult(twoToOne, appIn1, result1, |
|
542 |
Status.CLOSED, HandshakeStatus.NEED_WRAP, |
|
543 |
result2.bytesProduced(), 0); |
|
544 |
twoToOne.compact(); |
|
545 |
||
546 |
if (!ssle1.isInboundDone()) { |
|
547 |
throw new Exception("ssle1 inboundDone"); |
|
548 |
} |
|
549 |
||
550 |
if (!isHandshaking(ssle1)) { |
|
551 |
throw new Exception("ssle1 IS NOT handshaking"); |
|
552 |
} |
|
553 |
||
554 |
result2 = ssle2.wrap(appOut2, twoToOne); |
|
555 |
checkResult(appOut2, twoToOne, result2, |
|
556 |
Status.CLOSED, HandshakeStatus.NEED_UNWRAP, 0, 0); |
|
557 |
twoToOne.flip(); |
|
558 |
||
559 |
log("======================================"); |
|
560 |
log("CN response"); |
|
561 |
||
562 |
if (ssle1.isOutboundDone()) { |
|
563 |
throw new Exception("ssle1 outboundDone"); |
|
564 |
} |
|
565 |
||
566 |
result1 = ssle1.wrap(appOut1, oneToTwo); |
|
567 |
checkResult(appOut1, oneToTwo, result1, |
|
568 |
Status.CLOSED, HandshakeStatus.NOT_HANDSHAKING, 0, -1); |
|
569 |
||
570 |
if (!ssle1.isOutboundDone()) { |
|
571 |
throw new Exception("ssle1 outboundDone is NOT done"); |
|
572 |
} |
|
573 |
||
574 |
if (isHandshaking(ssle1)) { |
|
575 |
throw new Exception("ssle1 IS handshaking"); |
|
576 |
} |
|
577 |
||
578 |
oneToTwo.flip(); |
|
579 |
||
580 |
if (!ssle2.isOutboundDone()) { |
|
581 |
throw new Exception("ssle1 outboundDone"); |
|
582 |
} |
|
583 |
||
584 |
if (ssle2.isInboundDone()) { |
|
585 |
throw new Exception("ssle1 inboundDone"); |
|
586 |
} |
|
587 |
||
588 |
result2 = ssle2.unwrap(oneToTwo, appIn2); |
|
589 |
||
590 |
checkResult(oneToTwo, appIn2, result2, |
|
591 |
Status.CLOSED, HandshakeStatus.NOT_HANDSHAKING, |
|
592 |
result1.bytesProduced(), 0); |
|
593 |
||
594 |
if (!ssle2.isOutboundDone()) { |
|
595 |
throw new Exception("ssle1 outboundDone is NOT done"); |
|
596 |
} |
|
597 |
||
598 |
if (!ssle2.isInboundDone()) { |
|
599 |
throw new Exception("ssle1 inboundDone is NOT done"); |
|
600 |
} |
|
601 |
||
602 |
if (isHandshaking(ssle2)) { |
|
603 |
throw new Exception("ssle1 IS handshaking"); |
|
604 |
} |
|
605 |
||
606 |
oneToTwo.compact(); |
|
607 |
} |
|
608 |
||
609 |
public static void main(String args[]) throws Exception { |
|
29902 | 610 |
// reset the security property to make sure that the algorithms |
611 |
// and keys used in this test are not disabled. |
|
612 |
Security.setProperty("jdk.tls.disabledAlgorithms", ""); |
|
2 | 613 |
|
614 |
CheckStatus cs; |
|
615 |
||
616 |
cs = new CheckStatus(); |
|
617 |
||
618 |
cs.createSSLEngines(); |
|
619 |
||
620 |
cs.test(); |
|
621 |
||
622 |
System.out.println("Test Passed."); |
|
623 |
} |
|
624 |
||
625 |
/* |
|
626 |
* ********************************************************** |
|
627 |
* Majority of the test case is above, below is just setup stuff |
|
628 |
* ********************************************************** |
|
629 |
*/ |
|
630 |
||
631 |
public CheckStatus() throws Exception { |
|
632 |
sslc = getSSLContext(keyFilename, trustFilename); |
|
633 |
} |
|
634 |
||
635 |
/* |
|
636 |
* Create an initialized SSLContext to use for this test. |
|
637 |
*/ |
|
638 |
private SSLContext getSSLContext(String keyFile, String trustFile) |
|
639 |
throws Exception { |
|
640 |
||
641 |
KeyStore ks = KeyStore.getInstance("JKS"); |
|
642 |
KeyStore ts = KeyStore.getInstance("JKS"); |
|
643 |
||
644 |
char[] passphrase = "passphrase".toCharArray(); |
|
645 |
||
646 |
ks.load(new FileInputStream(keyFile), passphrase); |
|
647 |
ts.load(new FileInputStream(trustFile), passphrase); |
|
648 |
||
649 |
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); |
|
650 |
kmf.init(ks, passphrase); |
|
651 |
||
652 |
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509"); |
|
653 |
tmf.init(ts); |
|
654 |
||
655 |
SSLContext sslCtx = SSLContext.getInstance("TLS"); |
|
656 |
||
657 |
sslCtx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); |
|
658 |
||
659 |
return sslCtx; |
|
660 |
} |
|
661 |
||
662 |
private void createBuffers() { |
|
663 |
// Size the buffers as appropriate. |
|
664 |
||
665 |
SSLSession session = ssle1.getSession(); |
|
666 |
int appBufferMax = session.getApplicationBufferSize(); |
|
667 |
int netBufferMax = session.getPacketBufferSize(); |
|
668 |
||
669 |
appIn1 = ByteBuffer.allocateDirect(appBufferMax + 50); |
|
670 |
appIn2 = ByteBuffer.allocateDirect(appBufferMax + 50); |
|
671 |
||
672 |
oneToTwo = ByteBuffer.allocateDirect(netBufferMax); |
|
673 |
twoToOne = ByteBuffer.allocateDirect(netBufferMax); |
|
674 |
||
675 |
appOut1 = ByteBuffer.wrap("Hi Engine2, I'm SSLEngine1".getBytes()); |
|
676 |
appOut2 = ByteBuffer.wrap("Hello Engine1, I'm SSLEngine2".getBytes()); |
|
677 |
||
678 |
log("AppOut1 = " + appOut1); |
|
679 |
log("AppOut2 = " + appOut2); |
|
680 |
log(""); |
|
681 |
} |
|
682 |
||
683 |
private static void runDelegatedTasks(SSLEngine engine) throws Exception { |
|
684 |
||
685 |
Runnable runnable; |
|
686 |
while ((runnable = engine.getDelegatedTask()) != null) { |
|
687 |
log("running delegated task..."); |
|
688 |
runnable.run(); |
|
689 |
} |
|
690 |
} |
|
691 |
||
692 |
private static void checkTransfer(ByteBuffer a, ByteBuffer b) |
|
693 |
throws Exception { |
|
694 |
a.flip(); |
|
695 |
b.flip(); |
|
696 |
||
697 |
if (!a.equals(b)) { |
|
698 |
throw new Exception("Data didn't transfer cleanly"); |
|
699 |
} else { |
|
700 |
log("Data transferred cleanly"); |
|
701 |
} |
|
702 |
||
703 |
a.position(a.limit()); |
|
704 |
b.position(b.limit()); |
|
705 |
a.limit(a.capacity()); |
|
706 |
b.limit(b.capacity()); |
|
707 |
} |
|
708 |
||
709 |
private static void log(String str) { |
|
710 |
if (debug) { |
|
711 |
System.out.println(str); |
|
712 |
} |
|
713 |
} |
|
714 |
} |