test/jdk/javax/net/ssl/SSLEngine/LargeBufs.java
author xuelei
Mon, 25 Jun 2018 13:41:39 -0700
changeset 50768 68fa3d4026ea
parent 47216 71c04702a3d5
permissions -rw-r--r--
8196584: TLS 1.3 Implementation Reviewed-by: ascarpino, coffeys, dfuchs, jjiang, jnimeh, mullan, rhalade, ssahoo, valeriep, weijun, wetmore, xuelei Contributed-by: Adam Petcher <adam.petcher@oracle.com>, Amanda Jiang <amanda.jiang@oracle.com>, Anthony Scarpino <anthony.scarpino@oracle.com>, Bradford Wetmore <bradford.wetmore@oracle.com>, Jamil Nimeh <jamil.j.nimeh@oracle.com>, John Jiang <sha.jiang@oracle.com>, Rajan Halade <rajan.halade@oracle.com>, Sibabrata Sahoo <sibabrata.sahoo@oracle.com>, Valerie Peng <valerie.peng@oracle.com>, Weijun Wang <weijun.wang@oracle.com>, Xuelei Fan <xuelei.fan@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
29902
dc24eacaae11 8076221: Disable RC4 cipher suites
asmotrak
parents: 23052
diff changeset
     2
 * Copyright (c) 2004, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
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 Add non-blocking SSL/TLS functionality, usable with any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *      I/O abstraction
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * This is to test larger buffer arrays, and make sure the maximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * is being passed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *
10915
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 5506
diff changeset
    33
 * @run main/othervm -Djsse.enableCBCProtection=false LargeBufs
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 5506
diff changeset
    34
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @author Brad R. Wetmore
30046
cf2c86e1819e 8078334: Mark regression tests using randomness
darcy
parents: 29902
diff changeset
    36
 * @key randomness
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.net.ssl.SSLEngineResult.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.util.Random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
public class LargeBufs {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    48
    private static boolean debug = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private SSLContext sslc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    static private SSLEngine ssle1;     // client
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    static private SSLEngine ssle2;     // server
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
23052
241885315119 8032473: Restructure JSSE regression test hierarchy in jdk test
xuelei
parents: 10915
diff changeset
    54
    private static String pathToStores = "../etc";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private static String keyStoreFile = "keystore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private static String trustStoreFile = "truststore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private static String passwd = "passphrase";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private static String keyFilename =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            System.getProperty("test.src", "./") + "/" + pathToStores +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                "/" + keyStoreFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private static String trustFilename =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            System.getProperty("test.src", "./") + "/" + pathToStores +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                "/" + trustStoreFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private ByteBuffer appOut1;         // write side of ssle1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private ByteBuffer appIn1;          // read side of ssle1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private ByteBuffer appOut2;         // write side of ssle2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private ByteBuffer appIn2;          // read side of ssle2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private ByteBuffer oneToTwo;        // "reliable" transport ssle1->ssle2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private ByteBuffer twoToOne;        // "reliable" transport ssle2->ssle1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private int appBufferMax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private int netBufferMax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private int OFFSET = 37;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Majority of the test case is here, setup is done below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private void createSSLEngines() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        ssle1 = sslc.createSSLEngine("client", 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        ssle1.setUseClientMode(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        ssle2 = sslc.createSSLEngine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        ssle2.setUseClientMode(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        ssle2.setNeedClientAuth(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private void runTest(String cipher) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        boolean dataDone = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        createSSLEngines();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        System.out.println("Using " + cipher);
31706
895170f33881 8043202: Prohibit RC4 cipher suites
asmotrak
parents: 30904
diff changeset
    96
        ssle1.setEnabledCipherSuites(new String [] { cipher });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        ssle2.setEnabledCipherSuites(new String [] { cipher });
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        createBuffers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        SSLEngineResult result1;        // ssle1's results from last operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        SSLEngineResult result2;        // ssle2's results from last operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        while (!isEngineClosed(ssle1) || !isEngineClosed(ssle2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            log("================");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            result1 = ssle1.wrap(appOut1, oneToTwo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            result2 = ssle2.wrap(appOut2, twoToOne);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            if ((result1.bytesConsumed() != 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                (result1.bytesConsumed() != appBufferMax) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                (result1.bytesConsumed() != OFFSET)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                throw new Exception("result1: " + result1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            if ((result2.bytesConsumed() != 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                (result2.bytesConsumed() != appBufferMax) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                (result2.bytesConsumed() != 2 * OFFSET)) {
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 30046
diff changeset
   120
                throw new Exception("result2: " + result2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            log("wrap1:  " + result1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            log("oneToTwo  = " + oneToTwo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            log("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            log("wrap2:  " + result2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            log("twoToOne  = " + twoToOne);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            runDelegatedTasks(result1, ssle1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            runDelegatedTasks(result2, ssle2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            oneToTwo.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            twoToOne.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            log("----");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            result1 = ssle1.unwrap(twoToOne, appIn1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            result2 = ssle2.unwrap(oneToTwo, appIn2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            if ((result1.bytesProduced() != 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                (result1.bytesProduced() != appBufferMax) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                (result1.bytesProduced() != 2 * OFFSET)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                throw new Exception("result1: " + result1);
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 ((result2.bytesProduced() != 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                (result2.bytesProduced() != appBufferMax) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                (result2.bytesProduced() != OFFSET)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                throw new Exception("result1: " + result1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            log("unwrap1: " + result1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            log("twoToOne  = " + twoToOne);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            log("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            log("unwrap2: " + result2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            log("oneToTwo  = " + oneToTwo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            runDelegatedTasks(result1, ssle1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            runDelegatedTasks(result2, ssle2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            oneToTwo.compact();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            twoToOne.compact();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
             * If we've transfered all the data between app1 and app2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
             * we try to close and see what that gets us.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            if (!dataDone && (appOut1.limit() == appIn2.position()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    (appOut2.limit() == appIn1.position())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                checkTransfer(appOut1, appIn2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                checkTransfer(appOut2, appIn1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                log("Closing ssle1's *OUTBOUND*...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                ssle1.closeOutbound();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                dataDone = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    public static void main(String args[]) throws Exception {
29902
dc24eacaae11 8076221: Disable RC4 cipher suites
asmotrak
parents: 23052
diff changeset
   184
        // reset the security property to make sure that the algorithms
dc24eacaae11 8076221: Disable RC4 cipher suites
asmotrak
parents: 23052
diff changeset
   185
        // and keys used in this test are not disabled.
dc24eacaae11 8076221: Disable RC4 cipher suites
asmotrak
parents: 23052
diff changeset
   186
        Security.setProperty("jdk.tls.disabledAlgorithms", "");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        LargeBufs test;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        test = new LargeBufs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        test.runTest("SSL_RSA_WITH_RC4_128_MD5");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        test = new LargeBufs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        test.runTest("SSL_RSA_WITH_3DES_EDE_CBC_SHA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        System.out.println("Test Passed.");
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
     * **********************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * Majority of the test case is above, below is just setup stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * **********************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    public LargeBufs() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        sslc = getSSLContext(keyFilename, trustFilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * Create an initialized SSLContext to use for this test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    private SSLContext getSSLContext(String keyFile, String trustFile)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        KeyStore ks = KeyStore.getInstance("JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        KeyStore ts = KeyStore.getInstance("JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        char[] passphrase = "passphrase".toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        ks.load(new FileInputStream(keyFile), passphrase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        ts.load(new FileInputStream(trustFile), passphrase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        kmf.init(ks, passphrase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        tmf.init(ts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        SSLContext sslCtx = SSLContext.getInstance("TLS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        sslCtx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        return sslCtx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    private void createBuffers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        // Size the buffers as appropriate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        SSLSession session = ssle1.getSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        // The maximum application buffer should calculate like
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        // appBufferMax = session.getApplicationBufferSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        // however, the getApplicationBufferSize() doesn't guarantee
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        // that the ability to concume or produce applicaton data upto
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        // the size. 16384 is the default JSSE implementation maximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        // application size that could be consumed and produced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        appBufferMax = 16384;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        netBufferMax = session.getPacketBufferSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        Random random = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        byte [] one = new byte [appBufferMax * 5 + OFFSET];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        byte [] two = new byte [appBufferMax * 5 + 2 * OFFSET];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        random.nextBytes(one);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        random.nextBytes(two);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        appOut1 = ByteBuffer.wrap(one);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        appOut2 = ByteBuffer.wrap(two);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        appIn1 = ByteBuffer.allocate(appBufferMax * 6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        appIn2 = ByteBuffer.allocate(appBufferMax * 6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        oneToTwo = ByteBuffer.allocateDirect(netBufferMax);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        twoToOne = ByteBuffer.allocateDirect(netBufferMax);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        System.out.println("Testing arrays of: " + one.length +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            " and " + two.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        log("AppOut1 = " + appOut1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        log("AppOut2 = " + appOut2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        log("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    private static void runDelegatedTasks(SSLEngineResult result,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            SSLEngine engine) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        if (result.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            Runnable runnable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            while ((runnable = engine.getDelegatedTask()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                log("running delegated task...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                runnable.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    private static boolean isEngineClosed(SSLEngine engine) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        return (engine.isOutboundDone() && engine.isInboundDone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    private static void checkTransfer(ByteBuffer a, ByteBuffer b)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        a.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        b.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        if (!a.equals(b)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            throw new Exception("Data didn't transfer cleanly");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            log("Data transferred cleanly");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        }
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   300
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   301
        a.position(a.limit());
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   302
        b.position(b.limit());
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   303
        a.limit(a.capacity());
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   304
        b.limit(b.capacity());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    private static void log(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            System.out.println(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
}