test/jdk/javax/net/ssl/sanity/interop/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
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    24
import java.io.InputStream;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    25
import java.io.OutputStream;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
import java.security.cert.Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    28
import javax.net.ssl.KeyManager;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    29
import javax.net.ssl.SSLContext;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    30
import javax.net.ssl.SSLSession;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    31
import javax.net.ssl.SSLSocket;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    32
import javax.net.ssl.SSLSocketFactory;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    33
import javax.net.ssl.TrustManager;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
class JSSEClient extends CipherTest.Client {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    private final SSLContext sslContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private final MyX509KeyManager keyManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    JSSEClient(CipherTest cipherTest) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        super(cipherTest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        this.keyManager = new MyX509KeyManager(CipherTest.keyManager);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        sslContext = SSLContext.getInstance("TLS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    void runTest(CipherTest.TestParameters params) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        SSLSocket socket = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            keyManager.setAuthType(params.clientAuth);
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    50
            sslContext.init(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    51
                    new KeyManager[] { keyManager },
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    52
                    new TrustManager[] { CipherTest.trustManager },
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    53
                    CipherTest.secureRandom);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            SSLSocketFactory factory = (SSLSocketFactory)sslContext.getSocketFactory();
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    55
            socket = (SSLSocket)factory.createSocket("127.0.0.1", CipherTest.serverPort);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    56
            socket.setSoTimeout(CipherTest.TIMEOUT);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    57
            socket.setEnabledCipherSuites(new String[] { params.cipherSuite.name() });
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    58
            socket.setEnabledProtocols(new String[] { params.protocol.name });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            InputStream in = socket.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            OutputStream out = socket.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            sendRequest(in, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            socket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            SSLSession session = socket.getSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            session.invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            String cipherSuite = session.getCipherSuite();
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    66
            if (!params.cipherSuite.name().equals(cipherSuite)) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    67
                throw new Exception("Negotiated ciphersuite mismatch: "
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    68
                        + cipherSuite + " != " + params.cipherSuite);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            String protocol = session.getProtocol();
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    71
            if (!params.protocol.name.equals(protocol)) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    72
                throw new Exception("Negotiated protocol mismatch: " + protocol
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    73
                        + " != " + params.protocol);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            if (cipherSuite.indexOf("DH_anon") == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                session.getPeerCertificates();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            Certificate[] certificates = session.getLocalCertificates();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            if (params.clientAuth == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                if (certificates != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                    throw new Exception("Local certificates should be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                if ((certificates == null) || (certificates.length == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                    throw new Exception("Certificates missing");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                String keyAlg = certificates[0].getPublicKey().getAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                if (params.clientAuth != keyAlg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                    throw new Exception("Certificate type mismatch: " + keyAlg + " != " + params.clientAuth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            if (socket != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                socket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
}