test/jdk/sun/security/ssl/SSLEngineImpl/RehandshakeFinished.java
author xuelei
Mon, 25 Jun 2018 13:41:39 -0700
changeset 50768 68fa3d4026ea
parent 47216 71c04702a3d5
permissions -rw-r--r--
8196584: TLS 1.3 Implementation Reviewed-by: ascarpino, coffeys, dfuchs, jjiang, jnimeh, mullan, rhalade, ssahoo, valeriep, weijun, wetmore, xuelei Contributed-by: Adam Petcher <adam.petcher@oracle.com>, Amanda Jiang <amanda.jiang@oracle.com>, Anthony Scarpino <anthony.scarpino@oracle.com>, Bradford Wetmore <bradford.wetmore@oracle.com>, Jamil Nimeh <jamil.j.nimeh@oracle.com>, John Jiang <sha.jiang@oracle.com>, Rajan Halade <rajan.halade@oracle.com>, Sibabrata Sahoo <sibabrata.sahoo@oracle.com>, Valerie Peng <valerie.peng@oracle.com>, Weijun Wang <weijun.wang@oracle.com>, Xuelei Fan <xuelei.fan@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 19830
diff changeset
     2
 * Copyright (c) 2004, 2013, 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: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
19830
9c6ba80438f9 8024444: Change to use othervm mode of tests in SSLEngineImpl
xuelei
parents: 14342
diff changeset
    24
//
9c6ba80438f9 8024444: Change to use othervm mode of tests in SSLEngineImpl
xuelei
parents: 14342
diff changeset
    25
// SunJSSE does not support dynamic system properties, no way to re-use
9c6ba80438f9 8024444: Change to use othervm mode of tests in SSLEngineImpl
xuelei
parents: 14342
diff changeset
    26
// system properties in samevm/agentvm mode.
9c6ba80438f9 8024444: Change to use othervm mode of tests in SSLEngineImpl
xuelei
parents: 14342
diff changeset
    27
//
9c6ba80438f9 8024444: Change to use othervm mode of tests in SSLEngineImpl
xuelei
parents: 14342
diff changeset
    28
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @bug 6207322
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @summary SSLEngine is returning a premature FINISHED message when doing
19830
9c6ba80438f9 8024444: Change to use othervm mode of tests in SSLEngineImpl
xuelei
parents: 14342
diff changeset
    33
 *     an abbreviated handshake.
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    34
 * @run main/othervm RehandshakeFinished
19830
9c6ba80438f9 8024444: Change to use othervm mode of tests in SSLEngineImpl
xuelei
parents: 14342
diff changeset
    35
 * @author Brad Wetmore
9c6ba80438f9 8024444: Change to use othervm mode of tests in SSLEngineImpl
xuelei
parents: 14342
diff changeset
    36
 */
9c6ba80438f9 8024444: Change to use othervm mode of tests in SSLEngineImpl
xuelei
parents: 14342
diff changeset
    37
9c6ba80438f9 8024444: Change to use othervm mode of tests in SSLEngineImpl
xuelei
parents: 14342
diff changeset
    38
/*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * This test may need some updating if the messages change order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Currently I'm expecting that there is a simple renegotiation, with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * each message being contained in a single SSL packet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *      ClientHello
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *                              Server Hello
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *                              CCS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *                              FINISHED
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *      CCS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *      FINISHED
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * A SSLEngine usage example which simplifies the presentation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * by removing the I/O and multi-threading concerns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * The test creates two SSLEngines, simulating a client and server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * The "transport" layer consists two byte buffers:  think of them
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * as directly connected pipes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * Note, this is a *very* simple example: real code will be much more
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * involved.  For example, different threading and I/O models could be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * used, transport mechanisms could close unexpectedly, and so on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * When this application runs, notice that several messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * (wrap/unwrap) pass before any application data is consumed or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * produced.  (For more information, please see the SSL/TLS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * specifications.)  There may several steps for a successful handshake,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * so it's typical to see the following series of operations:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *      client          server          message
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *      ======          ======          =======
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *      wrap()          ...             ClientHello
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *      ...             unwrap()        ClientHello
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *      ...             wrap()          ServerHello/Certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *      unwrap()        ...             ServerHello/Certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *      wrap()          ...             ClientKeyExchange
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *      wrap()          ...             ChangeCipherSpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *      wrap()          ...             Finished
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *      ...             unwrap()        ClientKeyExchange
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *      ...             unwrap()        ChangeCipherSpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *      ...             unwrap()        Finished
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *      ...             wrap()          ChangeCipherSpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *      ...             wrap()          Finished
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *      unwrap()        ...             ChangeCipherSpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *      unwrap()        ...             Finished
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
import javax.net.ssl.SSLEngineResult.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
public class RehandshakeFinished {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Enables logging of the SSLEngine operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private static boolean logging = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Enables the JSSE system debugging system property:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *     -Djavax.net.debug=all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * This gives a lot of low-level information about operations underway,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * including specific handshake messages, and might be best examined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * after gaining some familiarity with this application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private static boolean debug = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    static private SSLContext sslc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    private SSLEngine clientEngine;     // client Engine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private ByteBuffer clientOut;       // write side of clientEngine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private ByteBuffer clientIn;        // read side of clientEngine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    private SSLEngine serverEngine;     // server Engine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private ByteBuffer serverOut;       // write side of serverEngine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    private ByteBuffer serverIn;        // read side of serverEngine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * For data transport, this example uses local ByteBuffers.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * isn't really useful, but the purpose of this example is to show
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * SSLEngine concepts, not how to do network transport.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private ByteBuffer cTOs;            // "reliable" transport client->server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    private ByteBuffer sTOc;            // "reliable" transport server->client
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * The following is to set up the keystores.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
23052
241885315119 8032473: Restructure JSSE regression test hierarchy in jdk test
xuelei
parents: 23010
diff changeset
   132
    private static String pathToStores = "../../../../javax/net/ssl/etc";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    private static String keyStoreFile = "keystore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private static String trustStoreFile = "truststore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    private static String passwd = "passphrase";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    private static String keyFilename =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            System.getProperty("test.src", "./") + "/" + pathToStores +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                "/" + keyStoreFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    private static String trustFilename =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            System.getProperty("test.src", "./") + "/" + pathToStores +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                "/" + trustStoreFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    private static Exception loadException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            KeyStore ks = KeyStore.getInstance("JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            KeyStore ts = KeyStore.getInstance("JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            char[] passphrase = "passphrase".toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            ks.load(new FileInputStream(keyFilename), passphrase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            ts.load(new FileInputStream(trustFilename), passphrase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            kmf.init(ks, passphrase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            TrustManagerFactory tmf =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                TrustManagerFactory.getInstance("SunX509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            tmf.init(ts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   163
            SSLContext sslCtx = SSLContext.getInstance("TLSv1.2");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            sslCtx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            sslc = sslCtx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            loadException = e;
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
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * Main entry point for this test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            System.setProperty("javax.net.debug", "all");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (loadException != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            throw loadException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        // Prime the session cache with a good session
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        // Second connection should be a simple session resumption.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if ((new RehandshakeFinished().runTest()) !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                new RehandshakeFinished().runRehandshake()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            throw new Exception("Sessions not equivalent");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        System.out.println("Test Passed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    private void checkResult(SSLEngine engine, SSLEngineResult result,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            HandshakeStatus rqdHsStatus,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            boolean consumed, boolean produced) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        HandshakeStatus hsStatus = result.getHandshakeStatus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        if (hsStatus == HandshakeStatus.NEED_TASK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            Runnable runnable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            while ((runnable = engine.getDelegatedTask()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                runnable.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            hsStatus = engine.getHandshakeStatus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        if (hsStatus != rqdHsStatus) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            throw new Exception("Required " + rqdHsStatus +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                ", got " + hsStatus);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        int bc = result.bytesConsumed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        int bp = result.bytesProduced();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        if (consumed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            if (bc <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                throw new Exception("Should have consumed bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            if (bc > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                throw new Exception("Should not have consumed bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        if (produced) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            if (bp <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                throw new Exception("Should have produced bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            if (bp > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                throw new Exception("Should not have produced bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    private SSLSession runRehandshake() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        log("\n\n==============================================");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        log("Staring actual test.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        createSSLEngines();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        createBuffers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        SSLEngineResult result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        log("Client's ClientHello");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        checkResult(clientEngine,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            clientEngine.wrap(clientOut, cTOs), HandshakeStatus.NEED_UNWRAP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            false, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        cTOs.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        checkResult(serverEngine,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            serverEngine.unwrap(cTOs, serverIn), HandshakeStatus.NEED_WRAP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        cTOs.compact();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        log("Server's ServerHello/ServerHelloDone");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        checkResult(serverEngine,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            serverEngine.wrap(serverOut, sTOc), HandshakeStatus.NEED_WRAP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            false, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        sTOc.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        checkResult(clientEngine,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            clientEngine.unwrap(sTOc, clientIn), HandshakeStatus.NEED_UNWRAP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        sTOc.compact();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        log("Server's CCS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        checkResult(serverEngine,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            serverEngine.wrap(serverOut, sTOc), HandshakeStatus.NEED_WRAP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            false, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        sTOc.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        checkResult(clientEngine,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            clientEngine.unwrap(sTOc, clientIn), HandshakeStatus.NEED_UNWRAP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        sTOc.compact();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        log("Server's FINISHED");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        checkResult(serverEngine,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            serverEngine.wrap(serverOut, sTOc), HandshakeStatus.NEED_UNWRAP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            false, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        sTOc.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        checkResult(clientEngine,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            clientEngine.unwrap(sTOc, clientIn), HandshakeStatus.NEED_WRAP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        sTOc.compact();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        log("Client's CCS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        checkResult(clientEngine,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            clientEngine.wrap(clientOut, cTOs), HandshakeStatus.NEED_WRAP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            false, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        cTOs.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        checkResult(serverEngine,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            serverEngine.unwrap(cTOs, serverIn), HandshakeStatus.NEED_UNWRAP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        cTOs.compact();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        log("Client's FINISHED should trigger FINISHED messages all around.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        checkResult(clientEngine,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            clientEngine.wrap(clientOut, cTOs), HandshakeStatus.FINISHED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            false, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        cTOs.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        checkResult(serverEngine,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            serverEngine.unwrap(cTOs, serverIn), HandshakeStatus.FINISHED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        cTOs.compact();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        return clientEngine.getSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * Run the test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * Sit in a tight loop, both engines calling wrap/unwrap regardless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * of whether data is available or not.  We do this until both engines
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * report back they are closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * The main loop handles all of the I/O phases of the SSLEngine's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * lifetime:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *     initial handshaking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     *     application data transfer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     *     engine closing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * One could easily separate these phases into separate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * sections of code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    private SSLSession runTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        boolean dataDone = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        createSSLEngines();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        createBuffers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        SSLEngineResult clientResult;   // results from client's last operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        SSLEngineResult serverResult;   // results from server's last operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
         * Examining the SSLEngineResults could be much more involved,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
         * and may alter the overall flow of the application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
         * For example, if we received a BUFFER_OVERFLOW when trying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
         * to write to the output pipe, we could reallocate a larger
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
         * pipe, but instead we wait for the peer to drain it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        while (!isEngineClosed(clientEngine) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                !isEngineClosed(serverEngine)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            log("================");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            clientResult = clientEngine.wrap(clientOut, cTOs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            log("client wrap: ", clientResult);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            runDelegatedTasks(clientResult, clientEngine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            serverResult = serverEngine.wrap(serverOut, sTOc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            log("server wrap: ", serverResult);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            runDelegatedTasks(serverResult, serverEngine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            cTOs.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            sTOc.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            log("----");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            clientResult = clientEngine.unwrap(sTOc, clientIn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            log("client unwrap: ", clientResult);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            runDelegatedTasks(clientResult, clientEngine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            serverResult = serverEngine.unwrap(cTOs, serverIn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            log("server unwrap: ", serverResult);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            runDelegatedTasks(serverResult, serverEngine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            cTOs.compact();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            sTOc.compact();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
             * After we've transfered all application data between the client
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
             * and server, we close the clientEngine's outbound stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
             * This generates a close_notify handshake message, which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
             * server engine receives and responds by closing itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            if (!dataDone && (clientOut.limit() == serverIn.position()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                    (serverOut.limit() == clientIn.position())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                 * A sanity check to ensure we got what was sent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                checkTransfer(serverOut, clientIn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                checkTransfer(clientOut, serverIn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                log("\tClosing clientEngine's *OUTBOUND*...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                clientEngine.closeOutbound();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                dataDone = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        return clientEngine.getSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * Using the SSLContext created during object creation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * create/configure the SSLEngines we'll use for this test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    private void createSSLEngines() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
         * Configure the serverEngine to act as a server in the SSL/TLS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
         * handshake.  Also, require SSL client authentication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        serverEngine = sslc.createSSLEngine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        serverEngine.setUseClientMode(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        serverEngine.setNeedClientAuth(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
         * Similar to above, but using client mode instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        clientEngine = sslc.createSSLEngine("client", 80);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        clientEngine.setUseClientMode(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
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * Create and size the buffers appropriately.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    private void createBuffers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
         * We'll assume the buffer sizes are the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
         * between client and server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        SSLSession session = clientEngine.getSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        int appBufferMax = session.getApplicationBufferSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        int netBufferMax = session.getPacketBufferSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
         * We'll make the input buffers a bit bigger than the max needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
         * size, so that unwrap()s following a successful data transfer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
         * won't generate BUFFER_OVERFLOWS.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
         * We'll use a mix of direct and indirect ByteBuffers for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
         * tutorial purposes only.  In reality, only use direct
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
         * ByteBuffers when they give a clear performance enhancement.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        clientIn = ByteBuffer.allocate(appBufferMax + 50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        serverIn = ByteBuffer.allocate(appBufferMax + 50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        cTOs = ByteBuffer.allocateDirect(netBufferMax);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        sTOc = ByteBuffer.allocateDirect(netBufferMax);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        clientOut = ByteBuffer.wrap("Hi Server, I'm Client".getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        serverOut = ByteBuffer.wrap("Hello Client, I'm Server".getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * If the result indicates that we have outstanding tasks to do,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * go ahead and run them in this thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    private static void runDelegatedTasks(SSLEngineResult result,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            SSLEngine engine) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        if (result.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            Runnable runnable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            while ((runnable = engine.getDelegatedTask()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                log("\trunning delegated task...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                runnable.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            HandshakeStatus hsStatus = engine.getHandshakeStatus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            if (hsStatus == HandshakeStatus.NEED_TASK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                throw new Exception(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                    "handshake shouldn't need additional tasks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            log("\tnew HandshakeStatus: " + hsStatus);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    private static boolean isEngineClosed(SSLEngine engine) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        return (engine.isOutboundDone() && engine.isInboundDone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * Simple check to make sure everything came across as expected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    private static void checkTransfer(ByteBuffer a, ByteBuffer b)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        a.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        b.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        if (!a.equals(b)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            throw new Exception("Data didn't transfer cleanly");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            log("\tData transferred cleanly");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        a.position(a.limit());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        b.position(b.limit());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        a.limit(a.capacity());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        b.limit(b.capacity());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * Logging code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    private static boolean resultOnce = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    private static void log(String str, SSLEngineResult result) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        if (!logging) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        if (resultOnce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            resultOnce = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            System.out.println("The format of the SSLEngineResult is: \n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                "\t\"getStatus() / getHandshakeStatus()\" +\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                "\t\"bytesConsumed() / bytesProduced()\"\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        HandshakeStatus hsStatus = result.getHandshakeStatus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        log(str +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            result.getStatus() + "/" + hsStatus + ", " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            result.bytesConsumed() + "/" + result.bytesProduced() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            " bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        if (hsStatus == HandshakeStatus.FINISHED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            log("\t...ready for application data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    private static void log(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        if (logging) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            System.out.println(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
}