test/jdk/javax/net/ssl/sanity/interop/JSSEClient.java
changeset 50768 68fa3d4026ea
parent 47216 71c04702a3d5
equal deleted inserted replaced
50767:356eaea05bf0 50768:68fa3d4026ea
     1 /*
     1 /*
     2  * Copyright (c) 2002, 2005, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2002, 2018, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 
    24 import java.io.InputStream;
    25 import java.io.*;
    25 import java.io.OutputStream;
    26 import java.net.*;
       
    27 import java.util.*;
       
    28 
       
    29 import java.security.*;
       
    30 import java.security.cert.*;
       
    31 import java.security.cert.Certificate;
    26 import java.security.cert.Certificate;
    32 
    27 
    33 import javax.net.ssl.*;
    28 import javax.net.ssl.KeyManager;
       
    29 import javax.net.ssl.SSLContext;
       
    30 import javax.net.ssl.SSLSession;
       
    31 import javax.net.ssl.SSLSocket;
       
    32 import javax.net.ssl.SSLSocketFactory;
       
    33 import javax.net.ssl.TrustManager;
    34 
    34 
    35 class JSSEClient extends CipherTest.Client {
    35 class JSSEClient extends CipherTest.Client {
    36 
    36 
    37     private final SSLContext sslContext;
    37     private final SSLContext sslContext;
    38     private final MyX509KeyManager keyManager;
    38     private final MyX509KeyManager keyManager;
    45 
    45 
    46     void runTest(CipherTest.TestParameters params) throws Exception {
    46     void runTest(CipherTest.TestParameters params) throws Exception {
    47         SSLSocket socket = null;
    47         SSLSocket socket = null;
    48         try {
    48         try {
    49             keyManager.setAuthType(params.clientAuth);
    49             keyManager.setAuthType(params.clientAuth);
    50             sslContext.init(new KeyManager[] {keyManager}, new TrustManager[] {cipherTest.trustManager}, cipherTest.secureRandom);
    50             sslContext.init(
       
    51                     new KeyManager[] { keyManager },
       
    52                     new TrustManager[] { CipherTest.trustManager },
       
    53                     CipherTest.secureRandom);
    51             SSLSocketFactory factory = (SSLSocketFactory)sslContext.getSocketFactory();
    54             SSLSocketFactory factory = (SSLSocketFactory)sslContext.getSocketFactory();
    52             socket = (SSLSocket)factory.createSocket("127.0.0.1", cipherTest.serverPort);
    55             socket = (SSLSocket)factory.createSocket("127.0.0.1", CipherTest.serverPort);
    53             socket.setSoTimeout(cipherTest.TIMEOUT);
    56             socket.setSoTimeout(CipherTest.TIMEOUT);
    54             socket.setEnabledCipherSuites(new String[] {params.cipherSuite});
    57             socket.setEnabledCipherSuites(new String[] { params.cipherSuite.name() });
    55             socket.setEnabledProtocols(new String[] {params.protocol});
    58             socket.setEnabledProtocols(new String[] { params.protocol.name });
    56             InputStream in = socket.getInputStream();
    59             InputStream in = socket.getInputStream();
    57             OutputStream out = socket.getOutputStream();
    60             OutputStream out = socket.getOutputStream();
    58             sendRequest(in, out);
    61             sendRequest(in, out);
    59             socket.close();
    62             socket.close();
    60             SSLSession session = socket.getSession();
    63             SSLSession session = socket.getSession();
    61             session.invalidate();
    64             session.invalidate();
    62             String cipherSuite = session.getCipherSuite();
    65             String cipherSuite = session.getCipherSuite();
    63             if (params.cipherSuite.equals(cipherSuite) == false) {
    66             if (!params.cipherSuite.name().equals(cipherSuite)) {
    64                 throw new Exception("Negotiated ciphersuite mismatch: " + cipherSuite + " != " + params.cipherSuite);
    67                 throw new Exception("Negotiated ciphersuite mismatch: "
       
    68                         + cipherSuite + " != " + params.cipherSuite);
    65             }
    69             }
    66             String protocol = session.getProtocol();
    70             String protocol = session.getProtocol();
    67             if (params.protocol.equals(protocol) == false) {
    71             if (!params.protocol.name.equals(protocol)) {
    68                 throw new Exception("Negotiated protocol mismatch: " + protocol + " != " + params.protocol);
    72                 throw new Exception("Negotiated protocol mismatch: " + protocol
       
    73                         + " != " + params.protocol);
    69             }
    74             }
    70             if (cipherSuite.indexOf("DH_anon") == -1) {
    75             if (cipherSuite.indexOf("DH_anon") == -1) {
    71                 session.getPeerCertificates();
    76                 session.getPeerCertificates();
    72             }
    77             }
    73             Certificate[] certificates = session.getLocalCertificates();
    78             Certificate[] certificates = session.getLocalCertificates();