jdk/test/javax/net/ssl/TLS/JSSEClient.java
author ihse
Tue, 09 May 2017 12:57:30 +0200
changeset 45028 b0ea3c0bfb81
parent 39140 86f21a96d0ab
permissions -rw-r--r--
8179889: Fix typographic errors in copyright headers Reviewed-by: erikj, dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
39140
86f21a96d0ab 8152745: javax/net/ssl/TLS/TestJSSE.java fails intermittently: Unsupported or unrecognized SSL message
asmotrak
parents: 26337
diff changeset
     1
/*
86f21a96d0ab 8152745: javax/net/ssl/TLS/TestJSSE.java fails intermittently: Unsupported or unrecognized SSL message
asmotrak
parents: 26337
diff changeset
     2
 * Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved.
26337
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
     4
 *
45028
b0ea3c0bfb81 8179889: Fix typographic errors in copyright headers
ihse
parents: 39140
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
b0ea3c0bfb81 8179889: Fix typographic errors in copyright headers
ihse
parents: 39140
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
b0ea3c0bfb81 8179889: Fix typographic errors in copyright headers
ihse
parents: 39140
diff changeset
     7
 * published by the Free Software Foundation.
26337
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
     8
 *
45028
b0ea3c0bfb81 8179889: Fix typographic errors in copyright headers
ihse
parents: 39140
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
b0ea3c0bfb81 8179889: Fix typographic errors in copyright headers
ihse
parents: 39140
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b0ea3c0bfb81 8179889: Fix typographic errors in copyright headers
ihse
parents: 39140
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
b0ea3c0bfb81 8179889: Fix typographic errors in copyright headers
ihse
parents: 39140
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
b0ea3c0bfb81 8179889: Fix typographic errors in copyright headers
ihse
parents: 39140
diff changeset
    13
 * accompanied this code).
26337
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    14
 *
45028
b0ea3c0bfb81 8179889: Fix typographic errors in copyright headers
ihse
parents: 39140
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
b0ea3c0bfb81 8179889: Fix typographic errors in copyright headers
ihse
parents: 39140
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
b0ea3c0bfb81 8179889: Fix typographic errors in copyright headers
ihse
parents: 39140
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
26337
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    18
 *
45028
b0ea3c0bfb81 8179889: Fix typographic errors in copyright headers
ihse
parents: 39140
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b0ea3c0bfb81 8179889: Fix typographic errors in copyright headers
ihse
parents: 39140
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
26337
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    21
 * questions.
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    22
 */
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    23
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    24
import java.io.InputStream;
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    25
import java.io.OutputStream;
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    26
import java.security.cert.Certificate;
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    27
import javax.net.ssl.KeyManager;
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    28
import javax.net.ssl.SSLContext;
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    29
import javax.net.ssl.SSLSession;
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    30
import javax.net.ssl.SSLSocket;
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    31
import javax.net.ssl.SSLSocketFactory;
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    32
import javax.net.ssl.TrustManager;
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    33
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    34
class JSSEClient extends CipherTestUtils.Client {
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    35
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    36
    private static final String DEFAULT = "DEFAULT";
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    37
    private static final String TLS = "TLS";
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    38
39140
86f21a96d0ab 8152745: javax/net/ssl/TLS/TestJSSE.java fails intermittently: Unsupported or unrecognized SSL message
asmotrak
parents: 26337
diff changeset
    39
    private final SSLContext context;
26337
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    40
    private final MyX509KeyManager keyManager;
39140
86f21a96d0ab 8152745: javax/net/ssl/TLS/TestJSSE.java fails intermittently: Unsupported or unrecognized SSL message
asmotrak
parents: 26337
diff changeset
    41
    private final int port;
86f21a96d0ab 8152745: javax/net/ssl/TLS/TestJSSE.java fails intermittently: Unsupported or unrecognized SSL message
asmotrak
parents: 26337
diff changeset
    42
    private final String host;
86f21a96d0ab 8152745: javax/net/ssl/TLS/TestJSSE.java fails intermittently: Unsupported or unrecognized SSL message
asmotrak
parents: 26337
diff changeset
    43
    private final String protocol;
26337
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    44
39140
86f21a96d0ab 8152745: javax/net/ssl/TLS/TestJSSE.java fails intermittently: Unsupported or unrecognized SSL message
asmotrak
parents: 26337
diff changeset
    45
    JSSEClient(CipherTestUtils cipherTest, String host, int port,
86f21a96d0ab 8152745: javax/net/ssl/TLS/TestJSSE.java fails intermittently: Unsupported or unrecognized SSL message
asmotrak
parents: 26337
diff changeset
    46
            String protocols, String ciphersuite) throws Exception {
86f21a96d0ab 8152745: javax/net/ssl/TLS/TestJSSE.java fails intermittently: Unsupported or unrecognized SSL message
asmotrak
parents: 26337
diff changeset
    47
        super(cipherTest, ciphersuite);
86f21a96d0ab 8152745: javax/net/ssl/TLS/TestJSSE.java fails intermittently: Unsupported or unrecognized SSL message
asmotrak
parents: 26337
diff changeset
    48
        this.host = host;
86f21a96d0ab 8152745: javax/net/ssl/TLS/TestJSSE.java fails intermittently: Unsupported or unrecognized SSL message
asmotrak
parents: 26337
diff changeset
    49
        this.port = port;
86f21a96d0ab 8152745: javax/net/ssl/TLS/TestJSSE.java fails intermittently: Unsupported or unrecognized SSL message
asmotrak
parents: 26337
diff changeset
    50
        this.protocol = protocols;
86f21a96d0ab 8152745: javax/net/ssl/TLS/TestJSSE.java fails intermittently: Unsupported or unrecognized SSL message
asmotrak
parents: 26337
diff changeset
    51
        this.keyManager = new MyX509KeyManager(
86f21a96d0ab 8152745: javax/net/ssl/TLS/TestJSSE.java fails intermittently: Unsupported or unrecognized SSL message
asmotrak
parents: 26337
diff changeset
    52
                                    cipherTest.getClientKeyManager());
86f21a96d0ab 8152745: javax/net/ssl/TLS/TestJSSE.java fails intermittently: Unsupported or unrecognized SSL message
asmotrak
parents: 26337
diff changeset
    53
        context = SSLContext.getInstance(TLS);
26337
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    54
    }
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    55
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    56
    @Override
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    57
    void runTest(CipherTestUtils.TestParameters params) throws Exception {
39140
86f21a96d0ab 8152745: javax/net/ssl/TLS/TestJSSE.java fails intermittently: Unsupported or unrecognized SSL message
asmotrak
parents: 26337
diff changeset
    58
        keyManager.setAuthType(params.clientAuth);
86f21a96d0ab 8152745: javax/net/ssl/TLS/TestJSSE.java fails intermittently: Unsupported or unrecognized SSL message
asmotrak
parents: 26337
diff changeset
    59
        context.init(
86f21a96d0ab 8152745: javax/net/ssl/TLS/TestJSSE.java fails intermittently: Unsupported or unrecognized SSL message
asmotrak
parents: 26337
diff changeset
    60
                new KeyManager[]{ keyManager },
86f21a96d0ab 8152745: javax/net/ssl/TLS/TestJSSE.java fails intermittently: Unsupported or unrecognized SSL message
asmotrak
parents: 26337
diff changeset
    61
                new TrustManager[]{ cipherTest.getClientTrustManager() },
86f21a96d0ab 8152745: javax/net/ssl/TLS/TestJSSE.java fails intermittently: Unsupported or unrecognized SSL message
asmotrak
parents: 26337
diff changeset
    62
                CipherTestUtils.secureRandom);
86f21a96d0ab 8152745: javax/net/ssl/TLS/TestJSSE.java fails intermittently: Unsupported or unrecognized SSL message
asmotrak
parents: 26337
diff changeset
    63
        SSLSocketFactory factory = (SSLSocketFactory)context.getSocketFactory();
86f21a96d0ab 8152745: javax/net/ssl/TLS/TestJSSE.java fails intermittently: Unsupported or unrecognized SSL message
asmotrak
parents: 26337
diff changeset
    64
86f21a96d0ab 8152745: javax/net/ssl/TLS/TestJSSE.java fails intermittently: Unsupported or unrecognized SSL message
asmotrak
parents: 26337
diff changeset
    65
        System.out.println("Connecting to server...");
86f21a96d0ab 8152745: javax/net/ssl/TLS/TestJSSE.java fails intermittently: Unsupported or unrecognized SSL message
asmotrak
parents: 26337
diff changeset
    66
        try (SSLSocket socket = (SSLSocket) factory.createSocket(host, port)) {
26337
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    67
            socket.setSoTimeout(CipherTestUtils.TIMEOUT);
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    68
            socket.setEnabledCipherSuites(params.cipherSuite.split(","));
39140
86f21a96d0ab 8152745: javax/net/ssl/TLS/TestJSSE.java fails intermittently: Unsupported or unrecognized SSL message
asmotrak
parents: 26337
diff changeset
    69
            if (params.protocol != null && !params.protocol.trim().isEmpty()
26337
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    70
                    && !params.protocol.trim().equals(DEFAULT)) {
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    71
                socket.setEnabledProtocols(params.protocol.split(","));
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    72
            }
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    73
            CipherTestUtils.printInfo(socket);
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    74
            InputStream in = socket.getInputStream();
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    75
            OutputStream out = socket.getOutputStream();
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    76
            sendRequest(in, out);
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    77
            SSLSession session = socket.getSession();
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    78
            session.invalidate();
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    79
            String cipherSuite = session.getCipherSuite();
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    80
            if (params.cipherSuite.equals(cipherSuite) == false) {
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    81
                throw new RuntimeException("Negotiated ciphersuite mismatch: "
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    82
                        + cipherSuite + " != " + params.cipherSuite);
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    83
            }
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    84
            String protocol = session.getProtocol();
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    85
            if (!DEFAULT.equals(params.protocol)
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    86
                    && !params.protocol.contains(protocol)) {
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    87
                throw new RuntimeException("Negotiated protocol mismatch: "
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    88
                        + protocol + " != " + params.protocol);
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    89
            }
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    90
            if (!cipherSuite.contains("DH_anon")) {
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    91
                session.getPeerCertificates();
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    92
            }
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    93
            Certificate[] certificates = session.getLocalCertificates();
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    94
            if (params.clientAuth == null) {
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    95
                if (certificates != null) {
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    96
                    throw new RuntimeException("Local certificates "
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    97
                            + "should be null");
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    98
                }
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    99
            } else {
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
   100
                if ((certificates == null) || (certificates.length == 0)) {
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
   101
                    throw new RuntimeException("Certificates missing");
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
   102
                }
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
   103
                String keyAlg = certificates[0].getPublicKey().getAlgorithm();
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
   104
                if ("EC".equals(keyAlg)) {
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
   105
                    keyAlg = "ECDSA";
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
   106
                }
39140
86f21a96d0ab 8152745: javax/net/ssl/TLS/TestJSSE.java fails intermittently: Unsupported or unrecognized SSL message
asmotrak
parents: 26337
diff changeset
   107
                if (!params.clientAuth.equals(keyAlg)) {
26337
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
   108
                    throw new RuntimeException("Certificate type mismatch: "
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
   109
                            + keyAlg + " != " + params.clientAuth);
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
   110
                }
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
   111
            }
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
   112
        }
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
   113
    }
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
   114
}