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