Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/net/ssl/compatibility/Client.java
38853 views
/*1* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.IOException;24import java.io.InputStream;25import java.io.OutputStream;26import java.net.InetSocketAddress;27import java.security.cert.X509Certificate;28import java.util.ArrayList;29import java.util.List;3031import javax.net.ssl.SSLContext;32import javax.net.ssl.SSLParameters;33import javax.net.ssl.SSLSession;34import javax.net.ssl.SSLSocket;35import javax.net.ssl.SSLSocketFactory;3637/*38* A simple SSL socket client.39*/40public class Client {4142private final SSLSocket socket;4344public Client(SSLContext context) throws Exception {45SSLSocketFactory socketFactory = context.getSocketFactory();46socket = (SSLSocket) socketFactory.createSocket();47socket.setSoTimeout(Utils.TIMEOUT);48}4950public Client(Cert... certs) throws Exception {51this(Utils.createSSLContext(certs));52}5354private SSLSession getSession() {55return socket.getSession();56}5758private void setEnabledCipherSuites(String... cipherSuites) {59socket.setEnabledCipherSuites(cipherSuites);60}6162private void setEnabledProtocols(String... protocols) {63socket.setEnabledProtocols(protocols);64}6566@SuppressWarnings(value = { "unchecked", "rawtypes" })67private void setServerName(String hostname) {68List serverNames = new ArrayList();69serverNames.add(createSNIHostName(hostname));70SSLParameters params = socket.getSSLParameters();71params.setServerNames(serverNames);72socket.setSSLParameters(params);73}7475// Create SNIHostName via reflection due to pre-8 JDK builds don't support76// SNI. Those JDK builds cannot find classes SNIServerName and SNIHostName.77private Object createSNIHostName(String hostname) {78try {79Class<?> clazz = Class.forName("javax.net.ssl.SNIHostName");80return clazz.getConstructor(String.class).newInstance(hostname);81} catch (Exception e) {82throw new RuntimeException("Creates SNIHostName failed!", e);83}84}8586private void setApplicationProtocols(String... protocols) {87SSLParameters params = socket.getSSLParameters();88params.setApplicationProtocols(protocols);89socket.setSSLParameters(params);90}9192private String getNegotiatedApplicationProtocol() {93return socket.getApplicationProtocol();94}9596private void oneTimeConnect(String host, int port) throws IOException {97socket.connect(new InetSocketAddress(host, port));9899OutputStream out = socket.getOutputStream();100out.write('C');101out.flush();102103InputStream in = socket.getInputStream();104in.read();105}106107public void close() throws IOException {108socket.close();109}110111public static void main(String[] args) throws IOException {112System.out.println("----- Client start -----");113int port = Integer.valueOf(System.getProperty(Utils.PROP_PORT));114115String protocol = System.getProperty(Utils.PROP_PROTOCOL);116String cipherSuite = System.getProperty(Utils.PROP_CIPHER_SUITE);117String serverName = System.getProperty(Utils.PROP_SERVER_NAME);118String appProtocols = System.getProperty(Utils.PROP_APP_PROTOCOLS);119boolean supportsSNIOnServer120= Boolean.getBoolean(Utils.PROP_SUPPORTS_SNI_ON_SERVER);121boolean supportsSNIOnClient122= Boolean.getBoolean(Utils.PROP_SUPPORTS_SNI_ON_CLIENT);123boolean supportsALPNOnServer124= Boolean.getBoolean(Utils.PROP_SUPPORTS_ALPN_ON_SERVER);125boolean supportsALPNOnClient126= Boolean.getBoolean(Utils.PROP_SUPPORTS_ALPN_ON_CLIENT);127boolean negativeCase128= Boolean.getBoolean(Utils.PROP_NEGATIVE_CASE_ON_CLIENT);129System.out.println(Utils.join(Utils.PARAM_DELIMITER,130"ClientJDK=" + System.getProperty(Utils.PROP_CLIENT_JDK),131"Protocol=" + protocol,132"CipherSuite=" + cipherSuite,133"ServerName=" + serverName,134"AppProtocols=" + appProtocols));135136Status status = Status.SUCCESS;137Client client = null;138try {139client = new Client(Cert.getCerts(CipherSuite.cipherSuite(cipherSuite)));140client.setEnabledProtocols(protocol);141client.setEnabledCipherSuites(cipherSuite);142143if (serverName != null) {144if (supportsSNIOnClient) {145client.setServerName(serverName);146} else {147System.out.println(148"Ignored due to client doesn't support SNI.");149}150}151152if (appProtocols != null) {153if (supportsALPNOnClient) {154client.setApplicationProtocols(155Utils.split(appProtocols, Utils.VALUE_DELIMITER));156} else {157System.out.println(158"Ignored due to client doesn't support ALPN.");159}160}161162client.oneTimeConnect("localhost", port);163164if (serverName != null && supportsSNIOnServer165&& supportsSNIOnClient) {166X509Certificate cert167= (X509Certificate) client.getSession().getPeerCertificates()[0];168String subject169= cert.getSubjectX500Principal().getName();170if (!subject.contains(serverName)) {171System.out.println("Unexpected server: " + subject);172status = Status.FAIL;173}174}175176if (appProtocols != null && supportsALPNOnServer177&& supportsALPNOnClient) {178String negoAppProtocol179= client.getNegotiatedApplicationProtocol();180String expectedNegoAppProtocol181= System.getProperty(Utils.PROP_NEGO_APP_PROTOCOL);182if (!expectedNegoAppProtocol.equals(negoAppProtocol)) {183System.out.println("Unexpected negotiated app protocol: "184+ negoAppProtocol);185status = Status.FAIL;186}187}188189if (status != Status.FAIL) {190status = negativeCase191? Status.UNEXPECTED_SUCCESS192: Status.SUCCESS;193}194} catch (Exception exception) {195status = Utils.handleException(exception, negativeCase);196} finally {197if (client != null) {198client.close();199}200}201202System.out.println("STATUS: " + status);203System.out.println("----- Client end -----");204}205}206207208