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