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