1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package tests.api.javax.net.ssl; 19 20 import java.io.IOException; 21 import java.nio.ByteBuffer; 22 import java.nio.ReadOnlyBufferException; 23 import java.nio.channels.Pipe; 24 import java.nio.channels.Pipe.SinkChannel; 25 import java.nio.channels.Pipe.SourceChannel; 26 import java.security.KeyManagementException; 27 import java.security.NoSuchAlgorithmException; 28 import java.util.Arrays; 29 import java.util.HashSet; 30 import java.util.Set; 31 import javax.net.ssl.SSLContext; 32 import javax.net.ssl.SSLEngine; 33 import javax.net.ssl.SSLEngineResult; 34 import javax.net.ssl.SSLEngineResult.Status; 35 import javax.net.ssl.SSLException; 36 import javax.net.ssl.SSLEngineResult.HandshakeStatus; 37 import junit.framework.TestCase; 38 import libcore.java.security.StandardNames; 39 40 /** 41 * Tests for SSLEngine class 42 * 43 */ 44 public class SSLEngineTest extends TestCase { 45 private static final int MAX_TLS_RECORD_SIZE = 32768; 46 47 private HandshakeHandler clientEngine; 48 private HandshakeHandler serverEngine; 49 50 @Override protected void setUp() throws Exception { 51 super.setUp(); 52 } 53 54 /** 55 * Test for <code>SSLEngine()</code> constructor Assertion: creates 56 * SSLEngine object with null host and -1 port 57 */ 58 public void test_Constructor() throws Exception { 59 SSLEngine e = getEngine(); 60 assertNull(e.getPeerHost()); 61 assertEquals(-1, e.getPeerPort()); 62 String[] suites = e.getSupportedCipherSuites(); 63 e.setEnabledCipherSuites(suites); 64 assertEquals(e.getEnabledCipherSuites().length, suites.length); 65 } 66 67 /** 68 * Test for <code>SSLEngine(String host, int port)</code> constructor 69 */ 70 public void test_ConstructorLjava_lang_StringI01() throws Exception { 71 int port = 1010; 72 SSLEngine e = getEngine(null, port); 73 assertNull(e.getPeerHost()); 74 assertEquals(e.getPeerPort(), port); 75 try { 76 e.beginHandshake(); 77 fail("should throw IllegalStateException"); 78 } catch (IllegalStateException expected) { 79 } 80 81 e = getEngine(null, port); 82 e.setUseClientMode(true); 83 e.beginHandshake(); 84 85 e = getEngine(null, port); 86 e.setUseClientMode(false); 87 e.beginHandshake(); 88 } 89 90 /** 91 * Test for <code>SSLEngine(String host, int port)</code> constructor 92 */ 93 public void test_ConstructorLjava_lang_StringI02() throws Exception { 94 String host = "new host"; 95 int port = 8080; 96 SSLEngine e = getEngine(host, port); 97 assertEquals(e.getPeerHost(), host); 98 assertEquals(e.getPeerPort(), port); 99 String[] suites = e.getSupportedCipherSuites(); 100 e.setEnabledCipherSuites(suites); 101 assertEquals(e.getEnabledCipherSuites().length, suites.length); 102 e.setUseClientMode(true); 103 assertTrue(e.getUseClientMode()); 104 } 105 106 /** 107 * Test for <code>getPeerHost()</code> method 108 */ 109 public void test_getPeerHost() throws Exception { 110 SSLEngine e = getEngine(); 111 assertNull(e.getPeerHost()); 112 e = getEngine("www.fortify.net", 80); 113 assertEquals("Incorrect host name", "www.fortify.net", e.getPeerHost()); 114 } 115 116 /** 117 * Test for <code>getPeerPort()</code> method 118 */ 119 public void test_getPeerPort() throws Exception { 120 SSLEngine e = getEngine(); 121 assertEquals("Incorrect default value of peer port", 122 -1 ,e.getPeerPort()); 123 e = getEngine("www.fortify.net", 80); 124 assertEquals("Incorrect peer port", 80, e.getPeerPort()); 125 } 126 127 public void test_getSupportedProtocols() throws Exception { 128 SSLEngine sse = getEngine(); 129 130 String[] res = sse.getSupportedProtocols(); 131 assertNotNull(res); 132 assertTrue(res.length > 0); 133 } 134 135 /** 136 * javax.net.ssl.SSLEngine#setEnabledProtocols(String[] protocols) 137 * javax.net.ssl.SSLEngine#getEnabledProtocols() 138 */ 139 public void test_EnabledProtocols() throws Exception { 140 SSLEngine sse = getEngine(); 141 String[] pr = sse.getSupportedProtocols(); 142 143 sse.setEnabledProtocols(pr); 144 String[] res = sse.getEnabledProtocols(); 145 assertNotNull("Null array was returned", res); 146 assertEquals("Incorrect array length", res.length, pr.length); 147 assertTrue("Incorrect array was returned", Arrays.equals(res, pr)); 148 149 try { 150 sse.setEnabledProtocols(null); 151 fail("IllegalArgumentException wasn't thrown"); 152 } catch (IllegalArgumentException expected) { 153 } 154 } 155 156 /** 157 * javax.net.ssl.SSLEngine#getSupportedCipherSuites() 158 */ 159 public void test_getSupportedCipherSuites() throws Exception { 160 SSLEngine sse = getEngine(); 161 162 String[] res = sse.getSupportedCipherSuites(); 163 assertNotNull(res); 164 assertTrue(res.length > 0); 165 } 166 167 /** 168 * javax.net.ssl.SSLEngine#setEnabledCipherSuites(String[] suites) 169 * javax.net.ssl.SSLEngine#getEnabledCipherSuites() 170 */ 171 public void test_EnabledCipherSuites() throws Exception { 172 SSLEngine sse = getEngine(); 173 String[] st = sse.getSupportedCipherSuites(); 174 175 sse.setEnabledCipherSuites(st); 176 String[] res = sse.getEnabledCipherSuites(); 177 assertNotNull("Null array was returned", res); 178 assertEquals("Incorrect array length", res.length, st.length); 179 assertTrue("Incorrect array was returned", Arrays.equals(res, st)); 180 181 try { 182 sse.setEnabledCipherSuites(null); 183 fail("IllegalArgumentException wasn't thrown"); 184 } catch (IllegalArgumentException expected) { 185 } 186 } 187 188 /** 189 * javax.net.ssl.SSLEngine#setEnableSessionCreation(boolean flag) 190 * javax.net.ssl.SSLEngine#getEnableSessionCreation() 191 */ 192 public void test_EnableSessionCreation() throws Exception { 193 SSLEngine sse = getEngine(); 194 try { 195 assertTrue(sse.getEnableSessionCreation()); 196 sse.setEnableSessionCreation(false); 197 assertFalse(sse.getEnableSessionCreation()); 198 sse.setEnableSessionCreation(true); 199 assertTrue(sse.getEnableSessionCreation()); 200 } catch (Exception ex) { 201 fail("Unexpected exception " + ex); 202 } 203 } 204 205 /** 206 * javax.net.ssl.SSLEngine#setNeedClientAuth(boolean need) 207 * javax.net.ssl.SSLEngine#getNeedClientAuth() 208 */ 209 public void test_NeedClientAuth() throws Exception { 210 SSLEngine sse = getEngine(); 211 try { 212 sse.setNeedClientAuth(false); 213 assertFalse(sse.getNeedClientAuth()); 214 sse.setNeedClientAuth(true); 215 assertTrue(sse.getNeedClientAuth()); 216 } catch (Exception ex) { 217 fail("Unexpected exception " + ex); 218 } 219 } 220 221 /** 222 * javax.net.ssl.SSLEngine#setWantClientAuth(boolean want) 223 * javax.net.ssl.SSLEngine#getWantClientAuth() 224 */ 225 public void test_WantClientAuth() throws Exception { 226 SSLEngine sse = getEngine(); 227 sse.setWantClientAuth(false); 228 assertFalse(sse.getWantClientAuth()); 229 sse.setWantClientAuth(true); 230 assertTrue(sse.getWantClientAuth()); 231 } 232 233 /** 234 * javax.net.ssl.SSLEngine#beginHandshake() 235 */ 236 public void test_beginHandshake() throws Exception { 237 SSLEngine sse = getEngine(); 238 try { 239 sse.beginHandshake(); 240 fail("IllegalStateException wasn't thrown"); 241 } catch (IllegalStateException expected) { 242 } 243 sse = getEngine("new host", 1080); 244 try { 245 sse.beginHandshake(); 246 fail("IllegalStateException wasn't thrown"); 247 } catch (IllegalStateException expected) { 248 } 249 sse = getEngine(); 250 sse.setUseClientMode(true); 251 sse.beginHandshake(); 252 } 253 254 /** 255 * javax.net.ssl.SSLEngine#setUseClientMode(boolean mode) 256 * javax.net.ssl.SSLEngine#getUseClientMode() 257 */ 258 public void test_UseClientMode() throws Exception { 259 SSLEngine sse = getEngine(); 260 sse.setUseClientMode(false); 261 assertFalse(sse.getUseClientMode()); 262 sse.setUseClientMode(true); 263 assertTrue(sse.getUseClientMode()); 264 265 sse = getEngine(null, 1080); 266 sse.setUseClientMode(true); 267 sse.beginHandshake(); 268 try { 269 sse.setUseClientMode(false); 270 fail("IllegalArgumentException was not thrown"); 271 } catch (IllegalArgumentException expected) { 272 } 273 } 274 275 /** 276 * javax.net.ssl.SSLEngine#getSession() 277 */ 278 public void test_getSession() throws Exception { 279 SSLEngine sse = getEngine(); 280 assertNotNull(sse.getSession()); 281 } 282 283 /** 284 * javax.net.ssl.SSLEngine#getHandshakeStatus() 285 */ 286 public void test_getHandshakeStatus() throws Exception { 287 SSLEngine sse = getEngine(); 288 assertEquals(sse.getHandshakeStatus().toString(), "NOT_HANDSHAKING"); 289 sse.setUseClientMode(true); 290 sse.beginHandshake(); 291 assertEquals(sse.getHandshakeStatus().toString(), "NEED_WRAP"); 292 } 293 294 /** 295 * javax.net.ssl.SSLEngine#getDelegatedTask() 296 */ 297 public void test_getDelegatedTask() throws Exception { 298 SSLEngine sse = getEngine(); 299 assertNull(sse.getDelegatedTask()); 300 } 301 302 /** 303 * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts, 304 * int offset, int length) 305 * Exception case: SSLException should be thrown. 306 */ 307 public void test_unwrap_01() throws Exception { 308 prepareEngines(); 309 doHandshake(); 310 311 ByteBuffer bbs = ByteBuffer.wrap(new byte[] {1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,31,2,3,1,2,3,1,2,3,1,2,3}); 312 ByteBuffer bbd = ByteBuffer.allocate(100); 313 try { 314 clientEngine.engine.unwrap(bbs, new ByteBuffer[] { bbd }, 0, 1); 315 fail("SSLException wasn't thrown"); 316 } catch (SSLException expected) { 317 } 318 } 319 320 /** 321 * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts, 322 * int offset, int length) 323 * Exception case: IndexOutOfBoundsException should be thrown. 324 */ 325 public void test_unwrap_02() throws Exception { 326 String host = "new host"; 327 int port = 8080; 328 ByteBuffer[] bbA = { ByteBuffer.allocate(100), ByteBuffer.allocate(10), ByteBuffer.allocate(100) }; 329 330 ByteBuffer bb = ByteBuffer.allocate(10); 331 SSLEngine sse = getEngine(host, port); 332 sse.setUseClientMode(true); 333 334 try { 335 sse.unwrap(bb, bbA, -1, 3); 336 fail("IndexOutOfBoundsException wasn't thrown"); 337 } catch (IndexOutOfBoundsException expected) { 338 } 339 try { 340 sse.unwrap(bb, bbA, 0, -3); 341 fail("IndexOutOfBoundsException wasn't thrown"); 342 } catch (IndexOutOfBoundsException expected) { 343 } 344 try { 345 sse.unwrap(bb, bbA, bbA.length + 1, bbA.length); 346 fail("IndexOutOfBoundsException wasn't thrown"); 347 } catch (IndexOutOfBoundsException expected) { 348 } 349 try { 350 sse.unwrap(bb, bbA, 0, bbA.length + 1); 351 fail("IndexOutOfBoundsException wasn't thrown"); 352 } catch (IndexOutOfBoundsException expected) { 353 } 354 } 355 356 /** 357 * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts, 358 * int offset, int length) 359 * Exception case: ReadOnlyBufferException should be thrown. 360 */ 361 public void test_unwrap_03() throws Exception { 362 String host = "new host"; 363 int port = 8080; 364 ByteBuffer bbR = ByteBuffer.allocate(100).asReadOnlyBuffer(); 365 ByteBuffer[] bbA = { bbR, ByteBuffer.allocate(10), ByteBuffer.allocate(100) }; 366 367 ByteBuffer bb = ByteBuffer.allocate(10); 368 SSLEngine sse = getEngine(host, port); 369 sse.setUseClientMode(true); 370 371 try { 372 sse.unwrap(bb, bbA, 0, bbA.length); 373 fail("ReadOnlyBufferException wasn't thrown"); 374 } catch (ReadOnlyBufferException expected) { 375 } 376 } 377 378 /** 379 * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts, 380 * int offset, int length) 381 * Exception case: IllegalArgumentException should be thrown. 382 */ 383 public void test_unwrap_04() throws Exception { 384 String host = "new host"; 385 int port = 8080; 386 ByteBuffer[] bbA = {ByteBuffer.allocate(100), ByteBuffer.allocate(10), ByteBuffer.allocate(100)}; 387 ByteBuffer[] bbAN = {ByteBuffer.allocate(100), null, ByteBuffer.allocate(100)}; 388 ByteBuffer[] bbN = null; 389 ByteBuffer bb = ByteBuffer.allocate(10); 390 ByteBuffer bN = null; 391 SSLEngine sse = getEngine(host, port); 392 sse.setUseClientMode(true); 393 394 try { 395 sse.unwrap(bN, bbA, 0, 3); 396 fail("IllegalArgumentException wasn't thrown"); 397 } catch (IllegalArgumentException expected) { 398 } 399 try { 400 sse.unwrap(bb, bbAN, 0, 3); 401 fail("IllegalArgumentException wasn't thrown"); 402 } catch (IllegalArgumentException expected) { 403 } 404 try { 405 sse.unwrap(bb, bbN, 0, 0); 406 fail("IllegalArgumentException wasn't thrown"); 407 } catch (IllegalArgumentException expected) { 408 } 409 try { 410 sse.unwrap(bN, bbN, 0, 0); 411 fail("IllegalArgumentException wasn't thrown"); 412 } catch (IllegalArgumentException expected) { 413 } 414 } 415 416 /** 417 * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts, 418 * int offset, int length) 419 * Exception case: IllegalStateException should be thrown. 420 */ 421 public void test_unwrap_05() throws Exception { 422 String host = "new host"; 423 int port = 8080; 424 ByteBuffer[] bbA = { ByteBuffer.allocate(100), ByteBuffer.allocate(10), ByteBuffer.allocate(100) }; 425 426 ByteBuffer bb = ByteBuffer.allocate(10); 427 SSLEngine sse = getEngine(host, port); 428 429 try { 430 SSLEngineResult result = sse.unwrap(bb, bbA, 0, bbA.length); 431 fail("IllegalStateException wasn't thrown"); 432 } catch (IllegalStateException expected) { 433 } 434 } 435 436 /** 437 * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts, 438 * int offset, int length) 439 */ 440 public void test_unwrap_06() throws Exception { 441 String host = "new host"; 442 int port = 8080; 443 ByteBuffer[] bbA = { ByteBuffer.allocate(100), ByteBuffer.allocate(10), ByteBuffer.allocate(100) }; 444 445 ByteBuffer bb = ByteBuffer.allocate(10); 446 SSLEngine sse = getEngine(host, port); 447 sse.setUseClientMode(true); 448 449 SSLEngineResult res = sse.unwrap(bb, bbA, 0, bbA.length); 450 assertEquals(0, res.bytesConsumed()); 451 assertEquals(0, res.bytesProduced()); 452 } 453 454 public void test_wrap_01() throws Exception { 455 prepareEngines(); 456 doHandshake(); 457 ByteBuffer bbs = ByteBuffer.allocate(100); 458 ByteBuffer bbd = ByteBuffer.allocate(MAX_TLS_RECORD_SIZE); 459 clientEngine.engine.wrap(new ByteBuffer[] { bbs }, 0, 1, bbd); 460 } 461 462 /** 463 * javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, int offset, 464 * int length, ByteBuffer dst) 465 * Exception case: IndexOutOfBoundsException should be thrown. 466 */ 467 public void test_wrap_02() throws Exception { 468 String host = "new host"; 469 int port = 8080; 470 ByteBuffer bb = ByteBuffer.allocate(10); 471 ByteBuffer[] bbA = {ByteBuffer.allocate(5), ByteBuffer.allocate(10), ByteBuffer.allocate(5)}; 472 SSLEngine sse = getEngine(host, port); 473 sse.setUseClientMode(true); 474 475 try { 476 sse.wrap(bbA, -1, 3, bb); 477 fail("IndexOutOfBoundsException wasn't thrown"); 478 } catch (IndexOutOfBoundsException expected) { 479 } 480 try { 481 sse.wrap(bbA, 0, -3, bb); 482 fail("IndexOutOfBoundsException wasn't thrown"); 483 } catch (IndexOutOfBoundsException expected) { 484 } 485 try { 486 sse.wrap(bbA, bbA.length + 1, bbA.length, bb); 487 fail("IndexOutOfBoundsException wasn't thrown"); 488 } catch (IndexOutOfBoundsException expected) { 489 } 490 try { 491 sse.wrap(bbA, 0, bbA.length + 1, bb); 492 fail("IndexOutOfBoundsException wasn't thrown"); 493 } catch (IndexOutOfBoundsException expected) { 494 } 495 } 496 497 /** 498 * javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, int offset, 499 * int length, ByteBuffer dst) 500 * Exception case: ReadOnlyBufferException should be thrown. 501 */ 502 public void test_wrap_03() throws Exception { 503 String host = "new host"; 504 int port = 8080; 505 ByteBuffer bb = ByteBuffer.allocate(MAX_TLS_RECORD_SIZE).asReadOnlyBuffer(); 506 ByteBuffer[] bbA = {ByteBuffer.allocate(5), ByteBuffer.allocate(10), ByteBuffer.allocate(5)}; 507 SSLEngine sse = getEngine(host, port); 508 sse.setUseClientMode(true); 509 510 try { 511 sse.wrap(bbA, 0, bbA.length, bb); 512 fail("ReadOnlyBufferException wasn't thrown"); 513 } catch (ReadOnlyBufferException expected) { 514 } 515 } 516 517 /** 518 * javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, int offset, 519 * int length, ByteBuffer dst) 520 * Exception case: IllegalArgumentException should be thrown. 521 */ 522 public void test_wrap_04() throws Exception { 523 String host = "new host"; 524 int port = 8080; 525 ByteBuffer[] bbA = {ByteBuffer.allocate(100), ByteBuffer.allocate(10), ByteBuffer.allocate(100)}; 526 ByteBuffer[] bbN = null; 527 ByteBuffer bN = null; 528 SSLEngine e = getEngine(host, port); 529 e.setUseClientMode(true); 530 531 try { 532 e.wrap(bbA, 0, 3, bN); 533 fail("IllegalArgumentException must be thrown for null srcs byte buffer array"); 534 } catch (IllegalArgumentException ex) { 535 } 536 537 try { 538 e.wrap(bbN, 0, 0, bN); 539 fail("IllegalArgumentException wasn't thrown"); 540 } catch (IllegalArgumentException ex) { 541 } 542 } 543 544 /** 545 * javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, int offset, 546 * int length, ByteBuffer dst) 547 * Exception case: IllegalStateException should be thrown. 548 */ 549 public void test_wrap_05() throws Exception { 550 String host = "new host"; 551 int port = 8080; 552 ByteBuffer bb = ByteBuffer.allocate(MAX_TLS_RECORD_SIZE); 553 ByteBuffer[] bbA = {ByteBuffer.allocate(5), ByteBuffer.allocate(10), ByteBuffer.allocate(5)}; 554 SSLEngine sse = getEngine(host, port); 555 556 try { 557 SSLEngineResult result = sse.wrap(bbA, 0, bbA.length, bb); 558 fail("Should fail since mode not set yet"); 559 } catch (IllegalStateException expected) { 560 } 561 } 562 563 /** 564 * javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, int offset, 565 * int length, ByteBuffer dst) 566 */ 567 public void test_wrap_06() throws Exception { 568 String host = "new host"; 569 int port = 8080; 570 ByteBuffer bb = ByteBuffer.allocate(MAX_TLS_RECORD_SIZE); 571 ByteBuffer[] bbA = {ByteBuffer.allocate(5), ByteBuffer.allocate(10), ByteBuffer.allocate(5)}; 572 SSLEngine sse = getEngine(host, port); 573 sse.setUseClientMode(true); 574 575 SSLEngineResult result = sse.wrap(bbA, 0, bbA.length, bb); 576 assertEquals(SSLEngineResult.Status.OK, result.getStatus()); 577 assertEquals(0, result.bytesConsumed()); 578 assertTrue(result.bytesProduced() > 0); 579 } 580 581 /** 582 * javax.net.ssl.SSLEngine#closeOutbound() 583 * javax.net.ssl.SSLEngine#isOutboundDone() 584 */ 585 public void test_closeOutbound() throws Exception { 586 SSLEngine sse = getEngine(); 587 588 assertFalse(sse.isOutboundDone()); 589 sse.closeOutbound(); 590 assertTrue(sse.isOutboundDone()); 591 } 592 593 /** 594 * javax.net.ssl.SSLEngine#closeInbound() 595 * javax.net.ssl.SSLEngine#isInboundDone() 596 */ 597 public void test_closeInbound() throws Exception { 598 SSLEngine sse = getEngine(); 599 600 assertFalse(sse.isInboundDone()); 601 sse.closeInbound(); 602 assertTrue(sse.isInboundDone()); 603 } 604 605 /** 606 * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer dst) 607 * SSLException should be thrown. 608 */ 609 public void test_unwrap_ByteBuffer_ByteBuffer_01() throws Exception { 610 prepareEngines(); 611 doHandshake(); 612 ByteBuffer bbs = ByteBuffer.allocate(100); 613 ByteBuffer bbd = ByteBuffer.allocate(100); 614 615 try { 616 SSLEngineResult unwrap = clientEngine.engine.unwrap(bbs, bbd); 617 fail("SSLException wasn't thrown"); 618 } catch (SSLException ex) { 619 //expected 620 } 621 } 622 623 /** 624 * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer dst) 625 * ReadOnlyBufferException should be thrown. 626 */ 627 public void test_unwrap_ByteBuffer_ByteBuffer_02() throws Exception { 628 String host = "new host"; 629 int port = 8080; 630 ByteBuffer bbs = ByteBuffer.allocate(10); 631 ByteBuffer bbd = ByteBuffer.allocate(100).asReadOnlyBuffer(); 632 SSLEngine sse = getEngine(host, port); 633 sse.setUseClientMode(true); 634 635 try { 636 sse.unwrap(bbs, bbd); 637 fail("ReadOnlyBufferException wasn't thrown"); 638 } catch (ReadOnlyBufferException iobe) { 639 //expected 640 } catch (Exception e) { 641 fail(e + " was thrown instead of ReadOnlyBufferException"); 642 } 643 } 644 645 /** 646 * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer dst) 647 * IllegalArgumentException should be thrown. 648 */ 649 public void test_unwrap_ByteBuffer_ByteBuffer_03() throws Exception { 650 String host = "new host"; 651 int port = 8080; 652 ByteBuffer bbsN = null; 653 ByteBuffer bbdN = null; 654 ByteBuffer bbs = ByteBuffer.allocate(10); 655 ByteBuffer bbd = ByteBuffer.allocate(100); 656 SSLEngine sse = getEngine(host, port); 657 sse.setUseClientMode(true); 658 659 try { 660 sse.unwrap(bbsN, bbd); 661 fail("IllegalArgumentException wasn't thrown"); 662 } catch (IllegalArgumentException expected) { 663 } 664 665 try { 666 sse.unwrap(bbs, bbdN); 667 fail("IllegalArgumentException wasn't thrown"); 668 } catch (IllegalArgumentException expected) { 669 } 670 671 try { 672 sse.unwrap(bbsN, bbdN); 673 fail("IllegalArgumentException wasn't thrown"); 674 } catch (IllegalArgumentException expected) { 675 } 676 } 677 678 /** 679 * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer dst) 680 * IllegalStateException should be thrown. 681 */ 682 public void test_unwrap_ByteBuffer_ByteBuffer_04() throws Exception { 683 String host = "new host"; 684 int port = 8080; 685 ByteBuffer bbs = ByteBuffer.allocate(10); 686 ByteBuffer bbd = ByteBuffer.allocate(100); 687 SSLEngine sse = getEngine(host, port); 688 689 try { 690 sse.unwrap(bbs, bbd); 691 fail("IllegalStateException wasn't thrown"); 692 } catch (IllegalStateException expected) { 693 } 694 } 695 696 /** 697 * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer dst) 698 */ 699 public void test_unwrap_ByteBuffer_ByteBuffer_05() throws Exception { 700 String host = "new host"; 701 int port = 8080; 702 ByteBuffer bbs = ByteBuffer.allocate(10); 703 ByteBuffer bbd = ByteBuffer.allocate(100); 704 SSLEngine sse = getEngine(host, port); 705 sse.setUseClientMode(true); 706 707 SSLEngineResult res = sse.unwrap(bbs, bbd); 708 assertEquals(0, res.bytesConsumed()); 709 assertEquals(0, res.bytesProduced()); 710 } 711 712 /** 713 * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts) 714 * SSLException should be thrown. 715 */ 716 public void test_unwrap_ByteBuffer$ByteBuffer_01() throws Exception { 717 prepareEngines(); 718 doHandshake(); 719 720 ByteBuffer bbs = ByteBuffer.allocate(100); 721 ByteBuffer bbd = ByteBuffer.allocate(100); 722 723 try { 724 clientEngine.engine.unwrap(bbs, new ByteBuffer[] { bbd }); 725 fail("SSLException wasn't thrown"); 726 } catch (SSLException expected) { 727 } 728 } 729 730 /** 731 * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts) 732 * ReadOnlyBufferException should be thrown. 733 */ 734 public void test_unwrap_ByteBuffer$ByteBuffer_02() throws Exception { 735 String host = "new host"; 736 int port = 8080; 737 ByteBuffer bbs = ByteBuffer.allocate(10); 738 ByteBuffer bbR = ByteBuffer.allocate(100).asReadOnlyBuffer(); 739 ByteBuffer[] bbA = { bbR, ByteBuffer.allocate(10), ByteBuffer.allocate(100) }; 740 SSLEngine sse = getEngine(host, port); 741 sse.setUseClientMode(true); 742 743 try { 744 sse.unwrap(bbs, bbA); 745 fail("ReadOnlyBufferException wasn't thrown"); 746 } catch (ReadOnlyBufferException expected) { 747 } 748 } 749 750 /** 751 * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts) 752 * IllegalArgumentException should be thrown. 753 */ 754 public void test_unwrap_ByteBuffer$ByteBuffer_03() throws Exception { 755 String host = "new host"; 756 int port = 8080; 757 ByteBuffer[] bbA = { ByteBuffer.allocate(100), ByteBuffer.allocate(10), ByteBuffer.allocate(100) }; 758 ByteBuffer[] bbN = { ByteBuffer.allocate(100), null, ByteBuffer.allocate(100) }; 759 ByteBuffer[] bbAN = null; 760 ByteBuffer bb = ByteBuffer.allocate(10); 761 ByteBuffer bN = null; 762 SSLEngine sse = getEngine(host, port); 763 sse.setUseClientMode(true); 764 765 try { 766 sse.unwrap(bN, bbA); 767 fail("IllegalArgumentException wasn't thrown"); 768 } catch (IllegalArgumentException expected) { 769 } 770 771 try { 772 sse.unwrap(bb, bbAN); 773 fail("IllegalArgumentException wasn't thrown"); 774 } catch (IllegalArgumentException expected) { 775 } 776 777 try { 778 sse.unwrap(bb, bbN); 779 fail("IllegalArgumentException wasn't thrown"); 780 } catch (IllegalArgumentException expected) { 781 } 782 783 try { 784 sse.unwrap(bN, bbAN); 785 fail("IllegalArgumentException wasn't thrown"); 786 } catch (IllegalArgumentException expected) { 787 } 788 } 789 790 /** 791 * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts) 792 * IllegalStateException should be thrown. 793 */ 794 public void test_unwrap_ByteBuffer$ByteBuffer_04() throws Exception { 795 String host = "new host"; 796 int port = 8080; 797 ByteBuffer bbs = ByteBuffer.allocate(10); 798 ByteBuffer[] bbd = {ByteBuffer.allocate(100), ByteBuffer.allocate(10), ByteBuffer.allocate(100) }; 799 SSLEngine sse = getEngine(host, port); 800 801 try { 802 sse.unwrap(bbs, bbd); 803 fail("IllegalStateException wasn't thrown"); 804 } catch (IllegalStateException expected) { 805 } 806 } 807 808 /** 809 * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts) 810 */ 811 public void test_unwrap_ByteBuffer$ByteBuffer_05() throws Exception { 812 String host = "new host"; 813 int port = 8080; 814 ByteBuffer bbs = ByteBuffer.allocate(10); 815 ByteBuffer[] bbd = {ByteBuffer.allocate(100), ByteBuffer.allocate(10), ByteBuffer.allocate(100) }; 816 SSLEngine sse = getEngine(host, port); 817 sse.setUseClientMode(true); 818 819 SSLEngineResult res = sse.unwrap(bbs, bbd); 820 assertEquals(0, res.bytesConsumed()); 821 assertEquals(0, res.bytesProduced()); 822 } 823 824 public void test_wrap_ByteBuffer_ByteBuffer_01() throws Exception { 825 prepareEngines(); 826 doHandshake(); 827 ByteBuffer bbs = ByteBuffer.allocate(20); 828 ByteBuffer bbd = ByteBuffer.allocate(20000); 829 clientEngine.engine.wrap(bbs, bbd); 830 } 831 832 /** 833 * javax.net.ssl.SSLEngine#wrap(ByteBuffer src, ByteBuffer dst) 834 * ReadOnlyBufferException should be thrown. 835 */ 836 public void test_wrap_ByteBuffer_ByteBuffer_02() throws Exception { 837 String host = "new host"; 838 int port = 8080; 839 ByteBuffer bbs = ByteBuffer.allocate(10); 840 ByteBuffer bbd = ByteBuffer.allocate(100).asReadOnlyBuffer(); 841 SSLEngine sse = getEngine(host, port); 842 sse.setUseClientMode(true); 843 844 try { 845 sse.wrap(bbs, bbd); 846 fail("ReadOnlyBufferException wasn't thrown"); 847 } catch (ReadOnlyBufferException expected) { 848 } 849 } 850 851 /** 852 * javax.net.ssl.SSLEngine#wrap(ByteBuffer src, ByteBuffer dst) 853 * IllegalArgumentException should be thrown. 854 */ 855 public void test_wrap_ByteBuffer_ByteBuffer_03() throws Exception { 856 String host = "new host"; 857 int port = 8080; 858 ByteBuffer bbsN = null; 859 ByteBuffer bbdN = null; 860 ByteBuffer bbs = ByteBuffer.allocate(10); 861 ByteBuffer bbd = ByteBuffer.allocate(100); 862 SSLEngine sse = getEngine(host, port); 863 sse.setUseClientMode(true); 864 865 try { 866 sse.wrap(bbsN, bbd); 867 fail("IllegalArgumentException wasn't thrown"); 868 } catch (IllegalArgumentException expected) { 869 } 870 871 try { 872 sse.wrap(bbs, bbdN); 873 fail("IllegalArgumentException wasn't thrown"); 874 } catch (IllegalArgumentException expected) { 875 } 876 877 try { 878 sse.wrap(bbsN, bbdN); 879 fail("IllegalArgumentException wasn't thrown"); 880 } catch (IllegalArgumentException expected) { 881 } 882 } 883 884 /** 885 * javax.net.ssl.SSLEngine#wrap(ByteBuffer src, ByteBuffer dst) 886 * IllegalStateException should be thrown. 887 */ 888 public void test_wrap_ByteBuffer_ByteBuffer_04() throws Exception { 889 String host = "new host"; 890 int port = 8080; 891 ByteBuffer bbs = ByteBuffer.allocate(10); 892 ByteBuffer bbd = ByteBuffer.allocate(10); 893 SSLEngine sse = getEngine(host, port); 894 895 try { 896 SSLEngineResult result = sse.wrap(bbs, bbd); 897 } catch (IllegalStateException expected) { 898 } 899 } 900 901 /** 902 * javax.net.ssl.SSLEngine#wrap(ByteBuffer src, ByteBuffer dst) 903 */ 904 public void test_wrap_ByteBuffer_ByteBuffer_05() throws Exception { 905 String host = "new host"; 906 int port = 8080; 907 ByteBuffer bb = ByteBuffer.allocate(10); 908 SSLEngine sse = getEngine(host, port); 909 sse.setUseClientMode(true); 910 911 SSLEngineResult res = sse.wrap(bb, ByteBuffer.allocate(10)); 912 assertEquals(Status.BUFFER_OVERFLOW, res.getStatus()); 913 assertEquals(0, res.bytesConsumed()); 914 assertEquals(0, res.bytesProduced()); 915 } 916 917 /** 918 * javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, ByteBuffer dst) 919 * SSLException should be thrown. 920 */ 921 public void test_wrap_ByteBuffer$ByteBuffer_01() throws Exception { 922 prepareEngines(); 923 doHandshake(); 924 ByteBuffer bbs = ByteBuffer.allocate(100); 925 ByteBuffer bbd = ByteBuffer.allocate(20000); 926 927 clientEngine.engine.wrap(new ByteBuffer[] { bbs }, bbd); 928 serverEngine.engine.wrap(new ByteBuffer[] { bbs }, bbd); 929 } 930 931 /** 932 * javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, ByteBuffer dst) 933 * ReadOnlyBufferException should be thrown. 934 */ 935 public void test_wrap_ByteBuffer$ByteBuffer_02() throws Exception { 936 String host = "new host"; 937 int port = 8080; 938 ByteBuffer bb = ByteBuffer.allocate(10).asReadOnlyBuffer(); 939 ByteBuffer[] bbA = {ByteBuffer.allocate(5), ByteBuffer.allocate(10), ByteBuffer.allocate(5)}; 940 SSLEngine sse = getEngine(host, port); 941 sse.setUseClientMode(true); 942 943 try { 944 sse.wrap(bbA, bb); 945 fail("ReadOnlyBufferException wasn't thrown"); 946 } catch (ReadOnlyBufferException expected) { 947 } 948 } 949 950 /** 951 * javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, ByteBuffer dst) 952 * IllegalArgumentException should be thrown. 953 */ 954 public void test_wrap_ByteBuffer$ByteBuffer_03() throws Exception { 955 String host = "new host"; 956 int port = 8080; 957 ByteBuffer[] bbA = {ByteBuffer.allocate(100), ByteBuffer.allocate(10), ByteBuffer.allocate(100)}; 958 ByteBuffer[] bbAN = null; 959 ByteBuffer bb = ByteBuffer.allocate(10); 960 ByteBuffer bN = null; 961 SSLEngine sse = getEngine(host, port); 962 sse.setUseClientMode(true); 963 964 try { 965 sse.wrap(bbA, bN); 966 fail("IllegalArgumentException wasn't thrown"); 967 } catch (IllegalArgumentException expected) { 968 } 969 970 try { 971 sse.wrap(bbAN, bb); 972 fail("IllegalArgumentException wasn't thrown"); 973 } catch (IllegalArgumentException expected) { 974 } 975 976 try { 977 sse.wrap(bbAN, bN); 978 fail("IllegalArgumentException wasn't thrown"); 979 } catch (IllegalArgumentException expected) { 980 } 981 } 982 983 /** 984 * javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, ByteBuffer dst) 985 * IllegalStateException should be thrown. 986 */ 987 public void test_wrap_ByteBuffer$ByteBuffer_04() throws Exception { 988 String host = "new host"; 989 int port = 8080; 990 ByteBuffer bb = ByteBuffer.allocate(10); 991 ByteBuffer[] bbA = { ByteBuffer.allocate(5), ByteBuffer.allocate(10), ByteBuffer.allocate(5) }; 992 SSLEngine sse = getEngine(host, port); 993 994 SSLEngineResult result = sse.wrap(bbA, bb); 995 assertEquals(Status.BUFFER_OVERFLOW, result.getStatus()); 996 } 997 998 /** 999 * javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, ByteBuffer dst) 1000 */ 1001 public void test_wrap_ByteBuffer$ByteBuffer_05() throws Exception { 1002 String host = "new host"; 1003 int port = 8080; 1004 ByteBuffer bb = ByteBuffer.allocate(2000); 1005 ByteBuffer[] bbA = { ByteBuffer.allocate(5), ByteBuffer.allocate(10), ByteBuffer.allocate(5) }; 1006 SSLEngine sse = getEngine(host, port); 1007 sse.setUseClientMode(true); 1008 1009 SSLEngineResult res = sse.wrap(bbA, bb); 1010 assertEquals(0, res.bytesConsumed()); 1011 assertEquals(0, res.bytesProduced()); 1012 } 1013 1014 private SSLEngine getEngine() throws Exception { 1015 SSLContext context = SSLContext.getInstance("TLS"); 1016 context.init(null, null, null); 1017 return context.createSSLEngine(); 1018 } 1019 1020 private SSLEngine getEngine(String host, int port) throws Exception { 1021 SSLContext context = SSLContext.getInstance("TLS"); 1022 context.init(null, null, null); 1023 return context.createSSLEngine(host, port); 1024 } 1025 1026 class HandshakeHandler implements Runnable { 1027 1028 private final SSLEngine engine; 1029 1030 private final SourceChannel in; 1031 1032 private final SinkChannel out; 1033 1034 private final ByteBuffer EMPTY = ByteBuffer.allocate(0); 1035 1036 @SuppressWarnings("unused") 1037 private final String LOGTAG; 1038 1039 private SSLEngineResult.HandshakeStatus status; 1040 1041 private ByteBuffer readBuffer; 1042 1043 private ByteBuffer writeBuffer; 1044 1045 HandshakeHandler(boolean clientMode, SourceChannel in, SinkChannel out) throws Exception { 1046 this.in = in; 1047 this.out = out; 1048 engine = getEngine(); 1049 engine.setUseClientMode(clientMode); 1050 String[] cipherSuites = engine.getSupportedCipherSuites(); 1051 Set<String> enabledSuites = new HashSet<String>(); 1052 for (String cipherSuite : cipherSuites) { 1053 if (cipherSuite.contains("anon")) { 1054 enabledSuites.add(cipherSuite); 1055 } 1056 } 1057 engine.setEnabledCipherSuites((String[]) enabledSuites.toArray( 1058 new String[enabledSuites.size()])); 1059 1060 engine.beginHandshake(); 1061 status = engine.getHandshakeStatus(); 1062 1063 if (clientMode) { 1064 LOGTAG = "CLIENT: "; 1065 } else { 1066 LOGTAG = "SERVER: "; 1067 } 1068 1069 log("CipherSuites: " + Arrays.toString(engine.getEnabledCipherSuites())); 1070 log(status); 1071 1072 readBuffer = ByteBuffer.allocate(200000); 1073 writeBuffer = ByteBuffer.allocate(20000); 1074 } 1075 1076 public SSLEngineResult.HandshakeStatus getStatus() { 1077 return status; 1078 } 1079 1080 private void log(Object o) { 1081 //System.out.print(LOGTAG); 1082 //System.out.println(o); 1083 } 1084 1085 private ByteBuffer read() throws IOException { 1086 if (readBuffer == null || readBuffer.remaining() == 0 || readBuffer.position() == 0) { 1087 readBuffer.clear(); 1088 int read = in.read(readBuffer); 1089 log("read: " + read); 1090 readBuffer.rewind(); 1091 readBuffer.limit(read); 1092 } 1093 return readBuffer; 1094 } 1095 1096 public void run() { 1097 try { 1098 while (true) { 1099 switch (status) { 1100 case FINISHED: { 1101 log(status); 1102 return; 1103 } 1104 case NEED_TASK: { 1105 log(status); 1106 Runnable task; 1107 while ((task = engine.getDelegatedTask()) != null) { 1108 task.run(); 1109 } 1110 status = engine.getHandshakeStatus(); 1111 break; 1112 } 1113 case NEED_UNWRAP: { 1114 log(status); 1115 ByteBuffer source = read(); 1116 writeBuffer.clear(); 1117 1118 while (status == HandshakeStatus.NEED_UNWRAP) { 1119 SSLEngineResult result = engine.unwrap(source, writeBuffer); 1120 status = result.getHandshakeStatus(); 1121 log(result); 1122 } 1123 break; 1124 } 1125 case NEED_WRAP: { 1126 log(status); 1127 writeBuffer.clear(); 1128 1129 int produced = 0; 1130 SSLEngineResult result = null; 1131 while (status == HandshakeStatus.NEED_WRAP) { 1132 result = engine.wrap(EMPTY, writeBuffer); 1133 status = result.getHandshakeStatus(); 1134 produced += result.bytesProduced(); 1135 log(result); 1136 } 1137 writeBuffer.rewind(); 1138 writeBuffer.limit(produced); 1139 log("write: " + produced); 1140 out.write(writeBuffer); 1141 break; 1142 } 1143 case NOT_HANDSHAKING: { 1144 log("Not Handshaking"); 1145 return; 1146 } 1147 } 1148 } 1149 } catch (IOException e) { 1150 log(e); 1151 } catch (RuntimeException e) { 1152 // ignore; 1153 } 1154 } 1155 } 1156 1157 public void testHandshake() throws Exception { 1158 1159 prepareEngines(); 1160 1161 assertTrue("handshake failed", doHandshake()); 1162 1163 System.out.println(clientEngine.engine.getSession().getCipherSuite()); 1164 1165 assertEquals("Handshake not finished", 1166 SSLEngineResult.HandshakeStatus.FINISHED, 1167 clientEngine.getStatus()); 1168 assertEquals("Handshake not finished", 1169 SSLEngineResult.HandshakeStatus.FINISHED, 1170 serverEngine.getStatus()); 1171 } 1172 1173 void prepareEngines() throws Exception { 1174 Pipe clientSendPipe = Pipe.open(); 1175 Pipe serverSendPipe = Pipe.open(); 1176 1177 SinkChannel clientSink = clientSendPipe.sink(); 1178 SourceChannel serverSource = clientSendPipe.source(); 1179 SinkChannel serverSink = serverSendPipe.sink(); 1180 SourceChannel clientSource = serverSendPipe.source(); 1181 1182 clientEngine = new HandshakeHandler(true, clientSource, clientSink); 1183 serverEngine = new HandshakeHandler(false, serverSource, serverSink); 1184 } 1185 1186 boolean doHandshake() throws InterruptedException { 1187 Thread clientThread = new Thread(clientEngine); 1188 clientThread.start(); 1189 1190 Thread serverThread = new Thread(serverEngine); 1191 serverThread.start(); 1192 1193 int i = 0; 1194 while (clientThread.isAlive() && serverThread.isAlive() && i < 20) { 1195 Thread.sleep(500); 1196 i++; 1197 } 1198 1199 if (clientThread.isAlive()) { 1200 clientThread.interrupt(); 1201 } 1202 1203 if (serverThread.isAlive()) { 1204 serverThread.interrupt(); 1205 } 1206 1207 return clientEngine.getStatus() == HandshakeStatus.FINISHED && serverEngine.getStatus() == HandshakeStatus.FINISHED; 1208 } 1209 1210 } 1211