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