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