test/jdk/sun/security/ssl/SSLEngineImpl/CloseEngineException.java
author xuelei
Mon, 25 Jun 2018 08:14:11 -0700
branchJDK-8145252-TLS13-branch
changeset 56806 32a737f51e37
parent 56594 99e0f3f3f0e4
permissions -rw-r--r--
Support RSASSS-PSS for TLS 1.2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
56594
99e0f3f3f0e4 close issues introduced in TLS 1.3 implementation
xuelei
parents: 47216
diff changeset
     2
 * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
19830
9c6ba80438f9 8024444: Change to use othervm mode of tests in SSLEngineImpl
xuelei
parents: 5506
diff changeset
    24
//
9c6ba80438f9 8024444: Change to use othervm mode of tests in SSLEngineImpl
xuelei
parents: 5506
diff changeset
    25
// SunJSSE does not support dynamic system properties, no way to re-use
9c6ba80438f9 8024444: Change to use othervm mode of tests in SSLEngineImpl
xuelei
parents: 5506
diff changeset
    26
// system properties in samevm/agentvm mode.
9c6ba80438f9 8024444: Change to use othervm mode of tests in SSLEngineImpl
xuelei
parents: 5506
diff changeset
    27
//
9c6ba80438f9 8024444: Change to use othervm mode of tests in SSLEngineImpl
xuelei
parents: 5506
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 4969799
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @summary javax.net.ssl.SSLSocket.SSLSocket(InetAddress,int) shouldn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *              throw exception
19830
9c6ba80438f9 8024444: Change to use othervm mode of tests in SSLEngineImpl
xuelei
parents: 5506
diff changeset
    34
 * @run main/othervm CloseEngineException
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
19830
9c6ba80438f9 8024444: Change to use othervm mode of tests in SSLEngineImpl
xuelei
parents: 5506
diff changeset
    37
//
9c6ba80438f9 8024444: Change to use othervm mode of tests in SSLEngineImpl
xuelei
parents: 5506
diff changeset
    38
// This is making sure that starting a new handshake throws the right
9c6ba80438f9 8024444: Change to use othervm mode of tests in SSLEngineImpl
xuelei
parents: 5506
diff changeset
    39
// exception.  There is a similar test for SSLSocket.
9c6ba80438f9 8024444: Change to use othervm mode of tests in SSLEngineImpl
xuelei
parents: 5506
diff changeset
    40
//
9c6ba80438f9 8024444: Change to use othervm mode of tests in SSLEngineImpl
xuelei
parents: 5506
diff changeset
    41
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.net.ssl.SSLEngineResult.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
56594
99e0f3f3f0e4 close issues introduced in TLS 1.3 implementation
xuelei
parents: 47216
diff changeset
    48
// Note that this test case depends on JSSE provider implementation details.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
public class CloseEngineException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
56594
99e0f3f3f0e4 close issues introduced in TLS 1.3 implementation
xuelei
parents: 47216
diff changeset
    51
    private static boolean debug = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private SSLContext sslc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private SSLEngine ssle1;    // client
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private SSLEngine ssle2;    // server
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
23052
241885315119 8032473: Restructure JSSE regression test hierarchy in jdk test
xuelei
parents: 23010
diff changeset
    57
    private static String pathToStores = "../../../../javax/net/ssl/etc";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private static String keyStoreFile = "keystore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private static String trustStoreFile = "truststore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private static String passwd = "passphrase";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private static String keyFilename =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            System.getProperty("test.src", "./") + "/" + pathToStores +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                "/" + keyStoreFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private static String trustFilename =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            System.getProperty("test.src", "./") + "/" + pathToStores +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                "/" + trustStoreFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private ByteBuffer appOut1;         // write side of ssle1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private ByteBuffer appIn1;          // read side of ssle1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private ByteBuffer appOut2;         // write side of ssle2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private ByteBuffer appIn2;          // read side of ssle2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private ByteBuffer oneToTwo;        // "reliable" transport ssle1->ssle2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private ByteBuffer twoToOne;        // "reliable" transport ssle2->ssle1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Majority of the test case is here, setup is done below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private void createSSLEngines() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        ssle1 = sslc.createSSLEngine("client", 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        ssle1.setUseClientMode(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        ssle2 = sslc.createSSLEngine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        ssle2.setUseClientMode(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        ssle2.setNeedClientAuth(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
    private void runTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        boolean dataDone = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        createSSLEngines();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        createBuffers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        SSLEngineResult result1;        // ssle1's results from last operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        SSLEngineResult result2;        // ssle2's results from last operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
56594
99e0f3f3f0e4 close issues introduced in TLS 1.3 implementation
xuelei
parents: 47216
diff changeset
    98
        while (!isEngineClosed(ssle1) && !isEngineClosed(ssle2)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            log("================");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
56594
99e0f3f3f0e4 close issues introduced in TLS 1.3 implementation
xuelei
parents: 47216
diff changeset
   102
            if (!isEngineClosed(ssle1)) {
99e0f3f3f0e4 close issues introduced in TLS 1.3 implementation
xuelei
parents: 47216
diff changeset
   103
                result1 = ssle1.wrap(appOut1, oneToTwo);
99e0f3f3f0e4 close issues introduced in TLS 1.3 implementation
xuelei
parents: 47216
diff changeset
   104
                runDelegatedTasks(result1, ssle1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
56594
99e0f3f3f0e4 close issues introduced in TLS 1.3 implementation
xuelei
parents: 47216
diff changeset
   106
                log("wrap1:  " + result1);
99e0f3f3f0e4 close issues introduced in TLS 1.3 implementation
xuelei
parents: 47216
diff changeset
   107
                log("oneToTwo  = " + oneToTwo);
99e0f3f3f0e4 close issues introduced in TLS 1.3 implementation
xuelei
parents: 47216
diff changeset
   108
                log("");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
56594
99e0f3f3f0e4 close issues introduced in TLS 1.3 implementation
xuelei
parents: 47216
diff changeset
   110
                oneToTwo.flip();
99e0f3f3f0e4 close issues introduced in TLS 1.3 implementation
xuelei
parents: 47216
diff changeset
   111
            }
99e0f3f3f0e4 close issues introduced in TLS 1.3 implementation
xuelei
parents: 47216
diff changeset
   112
            if (!isEngineClosed(ssle2)) {
99e0f3f3f0e4 close issues introduced in TLS 1.3 implementation
xuelei
parents: 47216
diff changeset
   113
                result2 = ssle2.wrap(appOut2, twoToOne);
99e0f3f3f0e4 close issues introduced in TLS 1.3 implementation
xuelei
parents: 47216
diff changeset
   114
                runDelegatedTasks(result2, ssle2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
56594
99e0f3f3f0e4 close issues introduced in TLS 1.3 implementation
xuelei
parents: 47216
diff changeset
   116
                log("wrap2:  " + result2);
99e0f3f3f0e4 close issues introduced in TLS 1.3 implementation
xuelei
parents: 47216
diff changeset
   117
                log("twoToOne  = " + twoToOne);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
56594
99e0f3f3f0e4 close issues introduced in TLS 1.3 implementation
xuelei
parents: 47216
diff changeset
   119
                twoToOne.flip();
99e0f3f3f0e4 close issues introduced in TLS 1.3 implementation
xuelei
parents: 47216
diff changeset
   120
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            log("----");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
56594
99e0f3f3f0e4 close issues introduced in TLS 1.3 implementation
xuelei
parents: 47216
diff changeset
   124
            if (!isEngineClosed(ssle1) && !dataDone) {
99e0f3f3f0e4 close issues introduced in TLS 1.3 implementation
xuelei
parents: 47216
diff changeset
   125
            log("--");
99e0f3f3f0e4 close issues introduced in TLS 1.3 implementation
xuelei
parents: 47216
diff changeset
   126
                result1 = ssle1.unwrap(twoToOne, appIn1);
99e0f3f3f0e4 close issues introduced in TLS 1.3 implementation
xuelei
parents: 47216
diff changeset
   127
                runDelegatedTasks(result1, ssle1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
56594
99e0f3f3f0e4 close issues introduced in TLS 1.3 implementation
xuelei
parents: 47216
diff changeset
   129
                log("unwrap1: " + result1);
99e0f3f3f0e4 close issues introduced in TLS 1.3 implementation
xuelei
parents: 47216
diff changeset
   130
                log("twoToOne  = " + twoToOne);
99e0f3f3f0e4 close issues introduced in TLS 1.3 implementation
xuelei
parents: 47216
diff changeset
   131
                log("");
56806
32a737f51e37 Support RSASSS-PSS for TLS 1.2
xuelei
parents: 56594
diff changeset
   132
56594
99e0f3f3f0e4 close issues introduced in TLS 1.3 implementation
xuelei
parents: 47216
diff changeset
   133
                twoToOne.compact();
99e0f3f3f0e4 close issues introduced in TLS 1.3 implementation
xuelei
parents: 47216
diff changeset
   134
            }
99e0f3f3f0e4 close issues introduced in TLS 1.3 implementation
xuelei
parents: 47216
diff changeset
   135
            if (!isEngineClosed(ssle2)) {
99e0f3f3f0e4 close issues introduced in TLS 1.3 implementation
xuelei
parents: 47216
diff changeset
   136
            log("---");
99e0f3f3f0e4 close issues introduced in TLS 1.3 implementation
xuelei
parents: 47216
diff changeset
   137
                result2 = ssle2.unwrap(oneToTwo, appIn2);
99e0f3f3f0e4 close issues introduced in TLS 1.3 implementation
xuelei
parents: 47216
diff changeset
   138
                runDelegatedTasks(result2, ssle2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
56594
99e0f3f3f0e4 close issues introduced in TLS 1.3 implementation
xuelei
parents: 47216
diff changeset
   140
                log("unwrap2: " + result2);
99e0f3f3f0e4 close issues introduced in TLS 1.3 implementation
xuelei
parents: 47216
diff changeset
   141
                log("oneToTwo  = " + oneToTwo);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
56594
99e0f3f3f0e4 close issues introduced in TLS 1.3 implementation
xuelei
parents: 47216
diff changeset
   143
                oneToTwo.compact();
99e0f3f3f0e4 close issues introduced in TLS 1.3 implementation
xuelei
parents: 47216
diff changeset
   144
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
             * If we've transfered all the data between app1 and app2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
             * we try to close and see what that gets us.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            if (!dataDone && (appOut1.limit() == appIn2.position()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    (appOut2.limit() == appIn1.position())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                checkTransfer(appOut1, appIn2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                checkTransfer(appOut2, appIn1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                log("Closing ssle1's *OUTBOUND*...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                ssle1.closeOutbound();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                dataDone = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                     * Check that closed Outbound generates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    ssle1.beginHandshake();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    throw new Exception(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                        "TEST FAILED:  didn't throw Exception");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                } catch (SSLException e) {
56594
99e0f3f3f0e4 close issues introduced in TLS 1.3 implementation
xuelei
parents: 47216
diff changeset
   168
                    System.err.println("PARTIAL PASS");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
             * Check that closed Inbound generates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            ssle2.beginHandshake();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            throw new Exception(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                "TEST FAILED:  didn't throw Exception");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        } catch (SSLException e) {
56594
99e0f3f3f0e4 close issues introduced in TLS 1.3 implementation
xuelei
parents: 47216
diff changeset
   181
            System.err.println("TEST PASSED");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        CloseEngineException test;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        test = new CloseEngineException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        test.createSSLEngines();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        test.runTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
56594
99e0f3f3f0e4 close issues introduced in TLS 1.3 implementation
xuelei
parents: 47216
diff changeset
   195
        System.err.println("Test Passed.");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * **********************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * Majority of the test case is above, below is just setup stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * **********************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    public CloseEngineException() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        sslc = getSSLContext(keyFilename, trustFilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * Create an initialized SSLContext to use for this test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    private SSLContext getSSLContext(String keyFile, String trustFile)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        KeyStore ks = KeyStore.getInstance("JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        KeyStore ts = KeyStore.getInstance("JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        char[] passphrase = "passphrase".toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        ks.load(new FileInputStream(keyFile), passphrase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        ts.load(new FileInputStream(trustFile), passphrase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        kmf.init(ks, passphrase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        tmf.init(ts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        SSLContext sslCtx = SSLContext.getInstance("TLS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        sslCtx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        return sslCtx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    private void createBuffers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        // Size the buffers as appropriate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        SSLSession session = ssle1.getSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        int appBufferMax = session.getApplicationBufferSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        int netBufferMax = session.getPacketBufferSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        appIn1 = ByteBuffer.allocateDirect(appBufferMax + 50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        appIn2 = ByteBuffer.allocateDirect(appBufferMax + 50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        oneToTwo = ByteBuffer.allocateDirect(netBufferMax);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        twoToOne = ByteBuffer.allocateDirect(netBufferMax);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        appOut1 = ByteBuffer.wrap("Hi Engine2, I'm SSLEngine1".getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        appOut2 = ByteBuffer.wrap("Hello Engine1, I'm SSLEngine2".getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        log("AppOut1 = " + appOut1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        log("AppOut2 = " + appOut2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        log("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    private static void runDelegatedTasks(SSLEngineResult result,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            SSLEngine engine) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        if (result.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            Runnable runnable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            while ((runnable = engine.getDelegatedTask()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                log("running delegated task...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                runnable.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    private static boolean isEngineClosed(SSLEngine engine) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        return (engine.isOutboundDone() && engine.isInboundDone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    private static void checkTransfer(ByteBuffer a, ByteBuffer b)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        a.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        b.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        if (!a.equals(b)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            throw new Exception("Data didn't transfer cleanly");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            log("Data transferred cleanly");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        a.position(a.limit());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        b.position(b.limit());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        a.limit(a.capacity());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        b.limit(b.capacity());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    private static void log(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        if (debug) {
56594
99e0f3f3f0e4 close issues introduced in TLS 1.3 implementation
xuelei
parents: 47216
diff changeset
   291
            System.err.println(str);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
}