test/jdk/javax/net/ssl/templates/SSLEngineTemplate.java
author wetmore
Fri, 11 May 2018 15:53:12 -0700
branchJDK-8145252-TLS13-branch
changeset 56542 56aaa6cb3693
parent 47216 71c04702a3d5
child 51574 ed52ea83f830
permissions -rw-r--r--
Initial TLSv1.3 Implementation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
34380
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
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: 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
18784
7f517475eeb4 8019341: Update CookieHttpsClientTest to use the newer framework.
wetmore
parents: 14342
diff changeset
    24
// SunJSSE does not support dynamic system properties, no way to re-use
7f517475eeb4 8019341: Update CookieHttpsClientTest to use the newer framework.
wetmore
parents: 14342
diff changeset
    25
// system properties in samevm/agentvm mode.
7f517475eeb4 8019341: Update CookieHttpsClientTest to use the newer framework.
wetmore
parents: 14342
diff changeset
    26
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @bug 1234567
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @summary SSLEngine has not yet caused Solaris kernel to panic
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    31
 * @run main/othervm SSLEngineTemplate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * A SSLEngine usage example which simplifies the presentation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * by removing the I/O and multi-threading concerns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * The test creates two SSLEngines, simulating a client and server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * The "transport" layer consists two byte buffers:  think of them
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * as directly connected pipes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * Note, this is a *very* simple example: real code will be much more
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * involved.  For example, different threading and I/O models could be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * used, transport mechanisms could close unexpectedly, and so on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * When this application runs, notice that several messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * (wrap/unwrap) pass before any application data is consumed or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * produced.  (For more information, please see the SSL/TLS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * specifications.)  There may several steps for a successful handshake,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * so it's typical to see the following series of operations:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *      client          server          message
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *      ======          ======          =======
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *      wrap()          ...             ClientHello
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *      ...             unwrap()        ClientHello
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *      ...             wrap()          ServerHello/Certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *      unwrap()        ...             ServerHello/Certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *      wrap()          ...             ClientKeyExchange
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *      wrap()          ...             ChangeCipherSpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *      wrap()          ...             Finished
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *      ...             unwrap()        ClientKeyExchange
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *      ...             unwrap()        ChangeCipherSpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *      ...             unwrap()        Finished
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *      ...             wrap()          ChangeCipherSpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *      ...             wrap()          Finished
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *      unwrap()        ...             ChangeCipherSpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *      unwrap()        ...             Finished
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
import javax.net.ssl.SSLEngineResult.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
public class SSLEngineTemplate {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Enables logging of the SSLEngine operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
34380
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents: 23052
diff changeset
    81
    private static final boolean logging = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Enables the JSSE system debugging system property:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *     -Djavax.net.debug=all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * This gives a lot of low-level information about operations underway,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * including specific handshake messages, and might be best examined
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * after gaining some familiarity with this application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
34380
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents: 23052
diff changeset
    92
    private static final boolean debug = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
34380
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents: 23052
diff changeset
    94
    private final SSLContext sslc;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private SSLEngine clientEngine;     // client Engine
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private ByteBuffer clientOut;       // write side of clientEngine
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private ByteBuffer clientIn;        // read side of clientEngine
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private SSLEngine serverEngine;     // server Engine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private ByteBuffer serverOut;       // write side of serverEngine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private ByteBuffer serverIn;        // read side of serverEngine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * For data transport, this example uses local ByteBuffers.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * isn't really useful, but the purpose of this example is to show
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * SSLEngine concepts, not how to do network transport.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private ByteBuffer cTOs;            // "reliable" transport client->server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private ByteBuffer sTOc;            // "reliable" transport server->client
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * The following is to set up the keystores.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
34380
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents: 23052
diff changeset
   115
    private static final String pathToStores = "../etc";
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents: 23052
diff changeset
   116
    private static final String keyStoreFile = "keystore";
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents: 23052
diff changeset
   117
    private static final String trustStoreFile = "truststore";
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents: 23052
diff changeset
   118
    private static final String passwd = "passphrase";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
34380
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents: 23052
diff changeset
   120
    private static final String keyFilename =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            System.getProperty("test.src", ".") + "/" + pathToStores +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                "/" + keyStoreFile;
34380
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents: 23052
diff changeset
   123
    private static final String trustFilename =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            System.getProperty("test.src", ".") + "/" + pathToStores +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                "/" + trustStoreFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Main entry point for this test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            System.setProperty("javax.net.debug", "all");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        SSLEngineTemplate test = new SSLEngineTemplate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        test.runTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        System.out.println("Test Passed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * Create an initialized SSLContext to use for these tests.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    public SSLEngineTemplate() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        KeyStore ks = KeyStore.getInstance("JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        KeyStore ts = KeyStore.getInstance("JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        char[] passphrase = "passphrase".toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        ks.load(new FileInputStream(keyFilename), passphrase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        ts.load(new FileInputStream(trustFilename), passphrase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        kmf.init(ks, passphrase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        tmf.init(ts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        SSLContext sslCtx = SSLContext.getInstance("TLS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        sslCtx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        sslc = sslCtx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * Run the test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * Sit in a tight loop, both engines calling wrap/unwrap regardless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * of whether data is available or not.  We do this until both engines
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * report back they are closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * The main loop handles all of the I/O phases of the SSLEngine's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * lifetime:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *     initial handshaking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *     application data transfer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *     engine closing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * One could easily separate these phases into separate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * sections of code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    private void runTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        boolean dataDone = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        createSSLEngines();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        createBuffers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        SSLEngineResult clientResult;   // results from client's last operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        SSLEngineResult serverResult;   // results from server's last operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
         * Examining the SSLEngineResults could be much more involved,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
         * and may alter the overall flow of the application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
         * For example, if we received a BUFFER_OVERFLOW when trying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
         * to write to the output pipe, we could reallocate a larger
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
         * pipe, but instead we wait for the peer to drain it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        while (!isEngineClosed(clientEngine) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                !isEngineClosed(serverEngine)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            log("================");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            clientResult = clientEngine.wrap(clientOut, cTOs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            log("client wrap: ", clientResult);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            runDelegatedTasks(clientResult, clientEngine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            serverResult = serverEngine.wrap(serverOut, sTOc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            log("server wrap: ", serverResult);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            runDelegatedTasks(serverResult, serverEngine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            cTOs.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            sTOc.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            log("----");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            clientResult = clientEngine.unwrap(sTOc, clientIn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            log("client unwrap: ", clientResult);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            runDelegatedTasks(clientResult, clientEngine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            serverResult = serverEngine.unwrap(cTOs, serverIn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            log("server unwrap: ", serverResult);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            runDelegatedTasks(serverResult, serverEngine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            cTOs.compact();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            sTOc.compact();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
             * After we've transfered all application data between the client
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
             * and server, we close the clientEngine's outbound stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
             * This generates a close_notify handshake message, which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
             * server engine receives and responds by closing itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            if (!dataDone && (clientOut.limit() == serverIn.position()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                    (serverOut.limit() == clientIn.position())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                 * A sanity check to ensure we got what was sent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                checkTransfer(serverOut, clientIn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                checkTransfer(clientOut, serverIn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                log("\tClosing clientEngine's *OUTBOUND*...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                clientEngine.closeOutbound();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                dataDone = true;
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   248
                log("\tClosing serverEngine's *OUTBOUND*...");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   249
                serverEngine.closeOutbound();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * Using the SSLContext created during object creation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * create/configure the SSLEngines we'll use for this test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    private void createSSLEngines() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
         * Configure the serverEngine to act as a server in the SSL/TLS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
         * handshake.  Also, require SSL client authentication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        serverEngine = sslc.createSSLEngine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        serverEngine.setUseClientMode(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        serverEngine.setNeedClientAuth(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
         * Similar to above, but using client mode instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        clientEngine = sslc.createSSLEngine("client", 80);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        clientEngine.setUseClientMode(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * Create and size the buffers appropriately.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    private void createBuffers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
         * We'll assume the buffer sizes are the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
         * between client and server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        SSLSession session = clientEngine.getSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        int appBufferMax = session.getApplicationBufferSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        int netBufferMax = session.getPacketBufferSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
         * We'll make the input buffers a bit bigger than the max needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
         * size, so that unwrap()s following a successful data transfer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
         * won't generate BUFFER_OVERFLOWS.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
         * We'll use a mix of direct and indirect ByteBuffers for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
         * tutorial purposes only.  In reality, only use direct
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
         * ByteBuffers when they give a clear performance enhancement.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        clientIn = ByteBuffer.allocate(appBufferMax + 50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        serverIn = ByteBuffer.allocate(appBufferMax + 50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        cTOs = ByteBuffer.allocateDirect(netBufferMax);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        sTOc = ByteBuffer.allocateDirect(netBufferMax);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        clientOut = ByteBuffer.wrap("Hi Server, I'm Client".getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        serverOut = ByteBuffer.wrap("Hello Client, I'm Server".getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * If the result indicates that we have outstanding tasks to do,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * go ahead and run them in this thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    private static void runDelegatedTasks(SSLEngineResult result,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            SSLEngine engine) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        if (result.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            Runnable runnable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            while ((runnable = engine.getDelegatedTask()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                log("\trunning delegated task...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                runnable.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            HandshakeStatus hsStatus = engine.getHandshakeStatus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            if (hsStatus == HandshakeStatus.NEED_TASK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                throw new Exception(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                    "handshake shouldn't need additional tasks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            log("\tnew HandshakeStatus: " + hsStatus);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    private static boolean isEngineClosed(SSLEngine engine) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        return (engine.isOutboundDone() && engine.isInboundDone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * Simple check to make sure everything came across as expected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    private static void checkTransfer(ByteBuffer a, ByteBuffer b)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        a.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        b.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        if (!a.equals(b)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            throw new Exception("Data didn't transfer cleanly");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            log("\tData transferred cleanly");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        a.position(a.limit());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        b.position(b.limit());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        a.limit(a.capacity());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        b.limit(b.capacity());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * Logging code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    private static boolean resultOnce = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    private static void log(String str, SSLEngineResult result) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        if (!logging) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        if (resultOnce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            resultOnce = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            System.out.println("The format of the SSLEngineResult is: \n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                "\t\"getStatus() / getHandshakeStatus()\" +\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                "\t\"bytesConsumed() / bytesProduced()\"\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        HandshakeStatus hsStatus = result.getHandshakeStatus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        log(str +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            result.getStatus() + "/" + hsStatus + ", " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            result.bytesConsumed() + "/" + result.bytesProduced() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            " bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        if (hsStatus == HandshakeStatus.FINISHED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            log("\t...ready for application data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    private static void log(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        if (logging) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            System.out.println(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
}