test/jdk/sun/security/pkcs11/sslecc/JSSEClient.java
author xuelei
Mon, 25 Jun 2018 13:41:39 -0700
changeset 50768 68fa3d4026ea
parent 47216 71c04702a3d5
permissions -rw-r--r--
8196584: TLS 1.3 Implementation Reviewed-by: ascarpino, coffeys, dfuchs, jjiang, jnimeh, mullan, rhalade, ssahoo, valeriep, weijun, wetmore, xuelei Contributed-by: Adam Petcher <adam.petcher@oracle.com>, Amanda Jiang <amanda.jiang@oracle.com>, Anthony Scarpino <anthony.scarpino@oracle.com>, Bradford Wetmore <bradford.wetmore@oracle.com>, Jamil Nimeh <jamil.j.nimeh@oracle.com>, John Jiang <sha.jiang@oracle.com>, Rajan Halade <rajan.halade@oracle.com>, Sibabrata Sahoo <sibabrata.sahoo@oracle.com>, Valerie Peng <valerie.peng@oracle.com>, Weijun Wang <weijun.wang@oracle.com>, Xuelei Fan <xuelei.fan@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
     2
 * Copyright (c) 2002, 2018, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.security.cert.Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
class JSSEClient extends CipherTest.Client {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
    private final SSLContext sslContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    private final MyX509KeyManager keyManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    JSSEClient(CipherTest cipherTest) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
        super(cipherTest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        this.keyManager = new MyX509KeyManager(CipherTest.keyManager);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        sslContext = SSLContext.getInstance("TLS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    void runTest(CipherTest.TestParameters params) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        SSLSocket socket = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
            keyManager.setAuthType(params.clientAuth);
43106
4a7670d316ee 8167146: sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java failed with "Remote host terminated the handshake"
fyuan
parents: 5506
diff changeset
    46
            sslContext.init(
4a7670d316ee 8167146: sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java failed with "Remote host terminated the handshake"
fyuan
parents: 5506
diff changeset
    47
                    new KeyManager[] { keyManager },
4a7670d316ee 8167146: sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java failed with "Remote host terminated the handshake"
fyuan
parents: 5506
diff changeset
    48
                    new TrustManager[] { CipherTest.trustManager },
4a7670d316ee 8167146: sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java failed with "Remote host terminated the handshake"
fyuan
parents: 5506
diff changeset
    49
                    CipherTest.secureRandom);
4a7670d316ee 8167146: sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java failed with "Remote host terminated the handshake"
fyuan
parents: 5506
diff changeset
    50
            SSLSocketFactory factory
4a7670d316ee 8167146: sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java failed with "Remote host terminated the handshake"
fyuan
parents: 5506
diff changeset
    51
                    = (SSLSocketFactory) sslContext.getSocketFactory();
4a7670d316ee 8167146: sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java failed with "Remote host terminated the handshake"
fyuan
parents: 5506
diff changeset
    52
4a7670d316ee 8167146: sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java failed with "Remote host terminated the handshake"
fyuan
parents: 5506
diff changeset
    53
            socket = (SSLSocket) factory.createSocket();
4a7670d316ee 8167146: sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java failed with "Remote host terminated the handshake"
fyuan
parents: 5506
diff changeset
    54
            try {
4a7670d316ee 8167146: sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java failed with "Remote host terminated the handshake"
fyuan
parents: 5506
diff changeset
    55
                socket.connect(new InetSocketAddress("127.0.0.1",
4a7670d316ee 8167146: sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java failed with "Remote host terminated the handshake"
fyuan
parents: 5506
diff changeset
    56
                        CipherTest.serverPort), 15000);
4a7670d316ee 8167146: sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java failed with "Remote host terminated the handshake"
fyuan
parents: 5506
diff changeset
    57
            } catch (IOException ioe) {
4a7670d316ee 8167146: sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java failed with "Remote host terminated the handshake"
fyuan
parents: 5506
diff changeset
    58
                // The server side may be impacted by naughty test cases or
4a7670d316ee 8167146: sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java failed with "Remote host terminated the handshake"
fyuan
parents: 5506
diff changeset
    59
                // third party routines, and cannot accept connections.
4a7670d316ee 8167146: sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java failed with "Remote host terminated the handshake"
fyuan
parents: 5506
diff changeset
    60
                //
4a7670d316ee 8167146: sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java failed with "Remote host terminated the handshake"
fyuan
parents: 5506
diff changeset
    61
                // Just ignore the test if the connection cannot be
4a7670d316ee 8167146: sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java failed with "Remote host terminated the handshake"
fyuan
parents: 5506
diff changeset
    62
                // established.
4a7670d316ee 8167146: sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java failed with "Remote host terminated the handshake"
fyuan
parents: 5506
diff changeset
    63
                System.out.println(
4a7670d316ee 8167146: sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java failed with "Remote host terminated the handshake"
fyuan
parents: 5506
diff changeset
    64
                        "Cannot make a connection in 15 seconds. " +
4a7670d316ee 8167146: sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java failed with "Remote host terminated the handshake"
fyuan
parents: 5506
diff changeset
    65
                        "Ignore in client side.");
4a7670d316ee 8167146: sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java failed with "Remote host terminated the handshake"
fyuan
parents: 5506
diff changeset
    66
                return;
4a7670d316ee 8167146: sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java failed with "Remote host terminated the handshake"
fyuan
parents: 5506
diff changeset
    67
            }
4a7670d316ee 8167146: sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java failed with "Remote host terminated the handshake"
fyuan
parents: 5506
diff changeset
    68
4a7670d316ee 8167146: sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java failed with "Remote host terminated the handshake"
fyuan
parents: 5506
diff changeset
    69
            socket.setSoTimeout(CipherTest.TIMEOUT);
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    70
            socket.setEnabledCipherSuites(new String[] {params.cipherSuite.name()});
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    71
            socket.setEnabledProtocols(new String[] {params.protocol.name});
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            InputStream in = socket.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            OutputStream out = socket.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            sendRequest(in, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            socket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            SSLSession session = socket.getSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            session.invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            String cipherSuite = session.getCipherSuite();
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    79
            if (!params.cipherSuite.name().equals(cipherSuite)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                throw new Exception("Negotiated ciphersuite mismatch: " + cipherSuite + " != " + params.cipherSuite);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            String protocol = session.getProtocol();
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    83
            if (!params.protocol.name.equals(protocol)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                throw new Exception("Negotiated protocol mismatch: " + protocol + " != " + params.protocol);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            if (cipherSuite.indexOf("DH_anon") == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                session.getPeerCertificates();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            Certificate[] certificates = session.getLocalCertificates();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            if (params.clientAuth == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                if (certificates != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    throw new Exception("Local certificates should be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                if ((certificates == null) || (certificates.length == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                    throw new Exception("Certificates missing");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                String keyAlg = certificates[0].getPublicKey().getAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                if (keyAlg.equals("EC")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                    keyAlg = "ECDSA";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                if (params.clientAuth != keyAlg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    throw new Exception("Certificate type mismatch: " + keyAlg + " != " + params.clientAuth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            if (socket != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                socket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
}