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