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