jdk/test/javax/net/ssl/SSLEngine/EngineCloseOnAlert.java
author jnimeh
Thu, 03 Nov 2016 15:18:16 -0700
changeset 41910 1383904abbd0
permissions -rw-r--r--
8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts Summary: Upon receiving a fatal alert, make sure the input and output queues are closed and the engine is in the NOT_HANDSHAKING state. Reviewed-by: xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41910
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
     1
/*
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
     2
 * Copyright (c) 2004, 2016, Oracle and/or its affiliates. All rights reserved.
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
     4
 *
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
     7
 * published by the Free Software Foundation.
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
     8
 *
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    13
 * accompanied this code).
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    14
 *
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    18
 *
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    21
 * questions.
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    22
 */
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    23
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    24
/*
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    25
 * @test
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    26
 * @bug 8133632
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    27
 * @summary javax.net.ssl.SSLEngine does not properly handle received
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    28
 * SSL fatal alerts
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    29
 * @run main/othervm EngineCloseOnAlert
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    30
 */
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    31
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    32
import java.io.FileInputStream;
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    33
import java.io.IOException;
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    34
import javax.net.ssl.*;
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    35
import java.nio.ByteBuffer;
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    36
import java.util.*;
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    37
import java.security.*;
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    38
import static javax.net.ssl.SSLEngineResult.HandshakeStatus.*;
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    39
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    40
public class EngineCloseOnAlert {
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    41
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    42
    private static final String pathToStores = "../etc";
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    43
    private static final String keyStoreFile = "keystore";
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    44
    private static final String trustStoreFile = "truststore";
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    45
    private static final String passwd = "passphrase";
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    46
    private static final String keyFilename =
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    47
            System.getProperty("test.src", ".") + "/" + pathToStores +
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    48
                "/" + keyStoreFile;
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    49
    private static final String trustFilename =
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    50
            System.getProperty("test.src", ".") + "/" + pathToStores +
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    51
                "/" + trustStoreFile;
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    52
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    53
    private static KeyManagerFactory KMF;
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    54
    private static TrustManagerFactory TMF;
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    55
    private static TrustManagerFactory EMPTY_TMF;
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    56
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    57
    private static final String[] TLS10ONLY = { "TLSv1" };
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    58
    private static final String[] TLS12ONLY = { "TLSv1.2" };
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    59
    private static final String[] ONECIPHER =
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    60
            { "TLS_RSA_WITH_AES_128_CBC_SHA" };
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    61
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    62
    public interface TestCase {
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    63
        public void runTest() throws Exception;
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    64
    }
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    65
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    66
    public static void main(String[] args) throws Exception {
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    67
        int failed = 0;
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    68
        List<TestCase> testMatrix = new LinkedList<TestCase>() {{
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    69
            add(clientReceivesAlert);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    70
            add(serverReceivesAlert);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    71
        }};
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    72
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    73
        // Create the various key/trust manager factories we'll need
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    74
        createManagerFactories();
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    75
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    76
        for (TestCase test : testMatrix) {
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    77
            try {
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    78
                test.runTest();
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    79
            } catch (Exception e) {
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    80
                System.out.println("Exception in test:\n" + e);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    81
                e.printStackTrace(System.out);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    82
                failed++;
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    83
            }
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    84
        }
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    85
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    86
        System.out.println("Total tests: " + testMatrix.size() + ", passed: " +
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    87
                (testMatrix.size() - failed) + ", failed: " + failed);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    88
        if (failed > 0) {
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    89
            throw new RuntimeException("One or more tests failed.");
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    90
        }
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    91
    }
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    92
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    93
    private static final TestCase clientReceivesAlert = new TestCase() {
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    94
        @Override
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    95
        public void runTest() throws Exception {
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    96
            System.out.println("");
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    97
            System.out.println("=======================================");
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    98
            System.out.println("Test: Client receives alert from server");
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
    99
            System.out.println("=======================================");
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   100
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   101
            // For this test, we won't initialize any keystore so the
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   102
            // server will throw an exception because it has no key/cert to
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   103
            // match the requested ciphers offered by the client.  This
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   104
            // will generate an alert from the server to the client.
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   105
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   106
            SSLContext context = SSLContext.getDefault();
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   107
            SSLEngine client = context.createSSLEngine();
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   108
            SSLEngine server = context.createSSLEngine();
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   109
            client.setUseClientMode(true);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   110
            server.setUseClientMode(false);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   111
            SSLEngineResult clientResult;
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   112
            SSLEngineResult serverResult;
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   113
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   114
            ByteBuffer raw = ByteBuffer.allocate(32768);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   115
            ByteBuffer plain = ByteBuffer.allocate(32768);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   116
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   117
            // Generate the client hello and have the server unwrap it
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   118
            client.wrap(plain, raw);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   119
            checkEngineState(client, NEED_UNWRAP, false, false);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   120
            raw.flip();
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   121
            System.out.println("Client-to-Server:\n-----------------\n" +
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   122
                    dumpHexBytes(raw, 16, "\n", ":"));
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   123
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   124
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   125
            // The server should need to run a delegated task while processing
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   126
            // the client hello data.
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   127
            serverResult = server.unwrap(raw, plain);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   128
            checkEngineState(server, NEED_TASK, false, false);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   129
            System.out.println("Server result: " + serverResult);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   130
            runDelegatedTasks(serverResult, server);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   131
            checkEngineState(server, NEED_WRAP, true, false);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   132
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   133
            try {
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   134
                raw.clear();
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   135
                serverResult = server.wrap(plain, raw);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   136
                System.out.println("Server result: " + serverResult);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   137
                runDelegatedTasks(serverResult, server);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   138
            } catch (SSLException e) {
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   139
                // This is the expected code path
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   140
                System.out.println("Server throws exception: " + e);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   141
                System.out.println("Server engine state: " +
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   142
                        "isInboundDone = "+ server.isInboundDone() +
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   143
                        ", isOutboundDone = " + server.isOutboundDone() +
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   144
                        ", handshake status = " + server.getHandshakeStatus());
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   145
                checkEngineState(server, NEED_WRAP, true, false);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   146
            }
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   147
            raw.clear();
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   148
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   149
            // The above should show that isInboundDone returns true, and
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   150
            // handshake status is NEED_WRAP. That is the correct behavior,
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   151
            // wrap will put a fatal alert message in the buffer.
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   152
            serverResult = server.wrap(plain, raw);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   153
            System.out.println("Server result (wrap after exception): " +
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   154
                    serverResult);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   155
            System.out.println("Server engine closure state: isInboundDone="
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   156
                    + server.isInboundDone() + ", isOutboundDone="
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   157
                    + server.isOutboundDone());
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   158
            checkEngineState(server, NEED_UNWRAP, true, true);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   159
            raw.flip();
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   160
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   161
            System.out.println("Server-to-Client:\n-----------------\n" +
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   162
                    dumpHexBytes(raw, 16, "\n", ":"));
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   163
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   164
            // Client side will read the fatal alert and throw exception.
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   165
            try {
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   166
                clientResult = client.unwrap(raw, plain);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   167
                System.out.println("Client result (unwrap alert): " +
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   168
                    clientResult);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   169
            } catch (SSLException e) {
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   170
                System.out.println("Client throws exception: " + e);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   171
                System.out.println("Engine closure status: isInboundDone="
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   172
                        + client.isInboundDone() + ", isOutboundDone="
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   173
                        + client.isOutboundDone() + ", handshake status="
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   174
                        + client.getHandshakeStatus());
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   175
                checkEngineState(client, NOT_HANDSHAKING, true, true);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   176
            }
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   177
            raw.clear();
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   178
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   179
            // Last test, we try to unwrap
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   180
            clientResult = client.unwrap(raw, plain);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   181
            checkEngineState(client, NOT_HANDSHAKING, true, true);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   182
            System.out.println("Client result (wrap after exception): " +
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   183
                    clientResult);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   184
        }
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   185
    };
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   186
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   187
    private static final TestCase serverReceivesAlert = new TestCase() {
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   188
        @Override
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   189
        public void runTest() throws Exception {
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   190
            SSLContext cliContext = SSLContext.getDefault();
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   191
            SSLContext servContext = SSLContext.getInstance("TLS");
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   192
            servContext.init(KMF.getKeyManagers(), TMF.getTrustManagers(),
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   193
                    null);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   194
            SSLEngine client = cliContext.createSSLEngine();
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   195
            SSLEngine server = servContext.createSSLEngine();
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   196
            client.setUseClientMode(true);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   197
            client.setEnabledProtocols(TLS12ONLY);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   198
            client.setEnabledCipherSuites(ONECIPHER);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   199
            server.setUseClientMode(false);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   200
            server.setEnabledProtocols(TLS10ONLY);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   201
            SSLEngineResult clientResult;
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   202
            SSLEngineResult serverResult;
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   203
            ByteBuffer raw = ByteBuffer.allocate(32768);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   204
            ByteBuffer plain = ByteBuffer.allocate(32768);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   205
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   206
            System.out.println("");
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   207
            System.out.println("=======================================");
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   208
            System.out.println("Test: Server receives alert from client");
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   209
            System.out.println("=======================================");
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   210
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   211
            // Generate the client hello and have the server unwrap it
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   212
            checkEngineState(client, NOT_HANDSHAKING, false, false);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   213
            client.wrap(plain, raw);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   214
            checkEngineState(client, NEED_UNWRAP, false, false);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   215
            raw.flip();
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   216
            System.out.println("Client-to-Server:\n-----------------\n" +
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   217
                    dumpHexBytes(raw, 16, "\n", ":"));
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   218
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   219
            // The server should need to run a delegated task while processing
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   220
            // the client hello data.
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   221
            serverResult = server.unwrap(raw, plain);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   222
            checkEngineState(server, NEED_TASK, false, false);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   223
            runDelegatedTasks(serverResult, server);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   224
            checkEngineState(server, NEED_WRAP, false, false);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   225
            raw.compact();
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   226
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   227
            // The server should now wrap the response back to the client
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   228
            server.wrap(plain, raw);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   229
            checkEngineState(server, NEED_UNWRAP, false, false);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   230
            raw.flip();
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   231
            System.out.println("Server-to-Client:\n-----------------\n" +
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   232
                    dumpHexBytes(raw, 16, "\n", ":"));
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   233
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   234
            // The client should parse this and throw an exception because
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   235
            // It is unwiling to do TLS 1.0
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   236
            clientResult = client.unwrap(raw, plain);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   237
            checkEngineState(client, NEED_TASK, false, false);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   238
            runDelegatedTasks(clientResult, client);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   239
            checkEngineState(client, NEED_UNWRAP, false, false);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   240
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   241
            try {
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   242
                client.unwrap(raw, plain);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   243
            } catch (SSLException e) {
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   244
                System.out.println("Client throws exception: " + e);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   245
                System.out.println("Engine closure status: isInboundDone="
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   246
                        + client.isInboundDone() + ", isOutboundDone="
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   247
                        + client.isOutboundDone() + ", handshake status="
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   248
                        + client.getHandshakeStatus());
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   249
                checkEngineState(client, NEED_WRAP, true, false);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   250
            }
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   251
            raw.clear();
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   252
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   253
            // Now the client should wrap the exception
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   254
            client.wrap(plain, raw);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   255
            checkEngineState(client, NEED_UNWRAP, true, true);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   256
            raw.flip();
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   257
            System.out.println("Client-to-Server:\n-----------------\n" +
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   258
                    dumpHexBytes(raw, 16, "\n", ":"));
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   259
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   260
            try {
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   261
                server.unwrap(raw, plain);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   262
                checkEngineState(server, NEED_UNWRAP, false, false);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   263
            } catch (SSLException e) {
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   264
                System.out.println("Server throws exception: " + e);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   265
                System.out.println("Engine closure status: isInboundDone="
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   266
                        + server.isInboundDone() + ", isOutboundDone="
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   267
                        + server.isOutboundDone() + ", handshake status="
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   268
                        + server.getHandshakeStatus());
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   269
                checkEngineState(server, NOT_HANDSHAKING, true, true);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   270
            }
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   271
            raw.clear();
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   272
        }
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   273
    };
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   274
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   275
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   276
    /*
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   277
     * If the result indicates that we have outstanding tasks to do,
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   278
     * go ahead and run them in this thread.
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   279
     */
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   280
    private static void runDelegatedTasks(SSLEngineResult result,
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   281
            SSLEngine engine) throws Exception {
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   282
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   283
        if (result.getHandshakeStatus() ==
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   284
                SSLEngineResult.HandshakeStatus.NEED_TASK) {
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   285
            Runnable runnable;
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   286
            while ((runnable = engine.getDelegatedTask()) != null) {
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   287
                System.out.println("\trunning delegated task...");
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   288
                runnable.run();
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   289
            }
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   290
            SSLEngineResult.HandshakeStatus hsStatus =
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   291
                    engine.getHandshakeStatus();
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   292
            if (hsStatus == SSLEngineResult.HandshakeStatus.NEED_TASK) {
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   293
                throw new Exception(
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   294
                    "handshake shouldn't need additional tasks");
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   295
            }
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   296
            System.out.println("\tnew HandshakeStatus: " + hsStatus);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   297
        }
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   298
    }
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   299
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   300
    /**
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   301
     *
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   302
     * @param data The array of bytes to dump to stdout.
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   303
     * @param itemsPerLine The number of bytes to display per line
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   304
     * if the {@code lineDelim} character is blank then all bytes will be
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   305
     * printed on a single line.
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   306
     * @param lineDelim The delimiter between lines
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   307
     * @param itemDelim The delimiter between bytes
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   308
     *
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   309
     * @return The hexdump of the byte array
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   310
     */
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   311
    private static String dumpHexBytes(ByteBuffer data, int itemsPerLine,
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   312
            String lineDelim, String itemDelim) {
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   313
        StringBuilder sb = new StringBuilder();
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   314
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   315
        if (data != null) {
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   316
            data.mark();
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   317
            for (int i = 0; i < data.limit(); i++) {
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   318
                if (i % itemsPerLine == 0 && i != 0) {
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   319
                    sb.append(lineDelim);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   320
                }
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   321
                sb.append(String.format("%02X", data.get(i)));
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   322
                if (i % itemsPerLine != (itemsPerLine - 1) &&
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   323
                        i != (data.limit() -1)) {
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   324
                    sb.append(itemDelim);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   325
                }
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   326
            }
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   327
            data.reset();
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   328
        }
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   329
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   330
        return sb.toString();
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   331
    }
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   332
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   333
    private static void createManagerFactories()
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   334
            throws GeneralSecurityException, IOException {
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   335
        KeyStore keystore = KeyStore.getInstance("PKCS12");
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   336
        KeyStore truststore = KeyStore.getInstance("PKCS12");
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   337
        KeyStore empty_ts = KeyStore.getInstance("PKCS12");
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   338
        char[] passphrase = passwd.toCharArray();
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   339
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   340
        keystore.load(new FileInputStream(keyFilename), passphrase);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   341
        truststore.load(new FileInputStream(trustFilename), passphrase);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   342
        empty_ts.load(null, "".toCharArray());
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   343
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   344
        KMF = KeyManagerFactory.getInstance("PKIX");
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   345
        KMF.init(keystore, passphrase);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   346
        TMF = TrustManagerFactory.getInstance("PKIX");
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   347
        TMF.init(truststore);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   348
        EMPTY_TMF = TrustManagerFactory.getInstance("PKIX");
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   349
        EMPTY_TMF.init(truststore);
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   350
    }
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   351
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   352
    private static void checkEngineState(SSLEngine engine,
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   353
            SSLEngineResult.HandshakeStatus expectedHSStat,
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   354
            boolean expectedInboundDone, boolean expectedOutboundDone) {
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   355
        if (engine.getHandshakeStatus() != expectedHSStat ||
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   356
                engine.isInboundDone() != expectedInboundDone ||
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   357
                engine.isOutboundDone() != expectedOutboundDone) {
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   358
            throw new RuntimeException("Error: engine not in expected state\n" +
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   359
                    "Expected: state = " + expectedHSStat +
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   360
                    ", inDone = " + expectedInboundDone +
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   361
                    ", outDone = " + expectedOutboundDone + "\n" +
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   362
                    "Actual: state = " + engine.getHandshakeStatus() +
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   363
                    ", inDone = " + engine.isInboundDone() +
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   364
                    ", outDone = " + engine.isOutboundDone());
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   365
        } else {
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   366
            System.out.println((engine.getUseClientMode() ?
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   367
                    "Client" : "Server") + " handshake status: " +
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   368
                    engine.getHandshakeStatus() + ", inDone = " +
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   369
                    engine.isInboundDone() + ", outDone = " +
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   370
                    engine.isOutboundDone());
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   371
        }
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   372
    }
1383904abbd0 8133632: javax.net.ssl.SSLEngine does not properly handle received SSL fatal alerts
jnimeh
parents:
diff changeset
   373
}