Path: blob/master/test/jdk/javax/net/ssl/SSLSession/TestEnabledProtocols.java
41152 views
/*1* Copyright (c) 2001, 2021, 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*/2223// SunJSSE does not support dynamic system properties, no way to re-use24// system properties in samevm/agentvm mode.2526/*27* @test28* @bug 4416068 4478803 447973629* @summary 4273544 JSSE request for function forceV3ClientHello()30* 4479736 setEnabledProtocols API does not work correctly31* 4478803 Need APIs to determine the protocol versions used in an SSL32* session33* 4701722 protocol mismatch exceptions should be consistent between34* SSLv3 and TLSv135* @library /javax/net/ssl/templates36* @run main/othervm TestEnabledProtocols37* @author Ram Marti38*/3940import java.io.InputStream;41import java.io.InterruptedIOException;42import java.io.OutputStream;43import java.net.InetAddress;44import java.net.SocketException;45import java.security.Security;46import java.util.Arrays;4748import javax.net.ssl.SSLException;49import javax.net.ssl.SSLHandshakeException;50import javax.net.ssl.SSLServerSocket;51import javax.net.ssl.SSLSocket;5253public class TestEnabledProtocols extends SSLSocketTemplate {5455private final String[] serverProtocols;56private final String[] clientProtocols;57private final boolean exceptionExpected;58private final String selectedProtocol;5960public TestEnabledProtocols(String[] serverProtocols,61String[] clientProtocols, boolean exceptionExpected,62String selectedProtocol) {63this.serverProtocols = serverProtocols;64this.clientProtocols = clientProtocols;65this.exceptionExpected = exceptionExpected;66this.selectedProtocol = selectedProtocol;67this.serverAddress = InetAddress.getLoopbackAddress();68}6970@Override71protected void configureServerSocket(SSLServerSocket sslServerSocket) {72sslServerSocket.setEnabledProtocols(serverProtocols);73}7475@Override76protected void runServerApplication(SSLSocket socket) throws Exception {77try {78socket.startHandshake();7980InputStream in = socket.getInputStream();81OutputStream out = socket.getOutputStream();82out.write(280);83in.read();84} catch (SSLHandshakeException se) {85// ignore it; this is part of the testing86// log it for debugging87System.out.println("Server SSLHandshakeException:");88se.printStackTrace(System.out);89} catch (InterruptedIOException ioe) {90// must have been interrupted, no harm91} catch (SSLException | SocketException se) {92// The client side may have closed the socket.93System.out.println("Server SSLException:");94se.printStackTrace(System.out);95} catch (Exception e) {96System.out.println("Server exception:");97e.printStackTrace(System.out);98throw new RuntimeException(e);99}100}101102@Override103protected void runClientApplication(SSLSocket sslSocket) throws Exception {104try {105System.out.println("=== Starting new test run ===");106showProtocols("server", serverProtocols);107showProtocols("client", clientProtocols);108109sslSocket.setEnabledProtocols(clientProtocols);110sslSocket.startHandshake();111112String protocolName = sslSocket.getSession().getProtocol();113System.out.println("Protocol name after getSession is " +114protocolName);115116if (protocolName.equals(selectedProtocol)) {117System.out.println("** Success **");118} else {119System.out.println("** FAILURE ** ");120throw new RuntimeException121("expected protocol " + selectedProtocol +122" but using " + protocolName);123}124125InputStream in = sslSocket.getInputStream();126OutputStream out = sslSocket.getOutputStream();127in.read();128out.write(280);129} catch (SSLHandshakeException e) {130if (!exceptionExpected) {131failTest(e, "Client got UNEXPECTED SSLHandshakeException:");132} else {133System.out.println(134"Client got expected SSLHandshakeException:");135e.printStackTrace(System.out);136System.out.println("** Success **");137}138} catch (SSLException ssle) {139// The server side may have closed the socket.140if (isConnectionReset(ssle)) {141System.out.println("Client SSLException:");142ssle.printStackTrace(System.out);143} else {144failTest(ssle, "Client got UNEXPECTED SSLException:");145}146147} catch (Exception e) {148failTest(e, "Client got UNEXPECTED Exception:");149}150}151152private boolean isConnectionReset(SSLException ssle) {153Throwable cause = ssle.getCause();154return cause instanceof SocketException155&& "Connection reset".equals(cause.getMessage());156}157158private void failTest(Exception e, String message) {159System.out.println(message);160e.printStackTrace(System.out);161System.out.println("** FAILURE **");162throw new RuntimeException(e);163}164165public static void main(String[] args) throws Exception {166Security.setProperty("jdk.tls.disabledAlgorithms", "");167168runCase(new String[] { "TLSv1" },169new String[] { "TLSv1" },170false, "TLSv1");171runCase(new String[] { "TLSv1" },172new String[] { "TLSv1", "SSLv2Hello" },173true, null);174runCase(new String[] { "TLSv1" },175new String[] { "TLSv1", "SSLv3" },176false, "TLSv1");177runCase(new String[] { "TLSv1" },178new String[] { "SSLv3", "SSLv2Hello" },179true, null);180runCase(new String[] { "TLSv1" },181new String[] { "SSLv3" },182true, null);183runCase(new String[] { "TLSv1" },184new String[] { "TLSv1", "SSLv3", "SSLv2Hello" },185true, null);186187runCase(new String[] { "TLSv1", "SSLv2Hello" },188new String[] { "TLSv1" },189false, "TLSv1");190runCase(new String[] { "TLSv1", "SSLv2Hello" },191new String[] { "TLSv1", "SSLv2Hello" },192false, "TLSv1");193runCase(new String[] { "TLSv1", "SSLv2Hello" },194new String[] { "TLSv1", "SSLv3" },195false, "TLSv1");196runCase(new String[] { "TLSv1", "SSLv2Hello" },197new String[] { "SSLv3", "SSLv2Hello" },198true, null);199runCase(new String[] { "TLSv1", "SSLv2Hello" },200new String[] { "SSLv3" },201true, null);202runCase(new String[] { "TLSv1", "SSLv2Hello" },203new String[] { "TLSv1", "SSLv3", "SSLv2Hello" },204false, "TLSv1");205206runCase(new String[] { "TLSv1", "SSLv3" },207new String[] { "TLSv1" },208false, "TLSv1");209runCase(new String[] { "TLSv1", "SSLv3" },210new String[] { "TLSv1", "SSLv2Hello" },211true, null);212runCase(new String[] { "TLSv1", "SSLv3" },213new String[] { "TLSv1", "SSLv3" },214false, "TLSv1");215runCase(new String[] { "TLSv1", "SSLv3" },216new String[] { "SSLv3", "SSLv2Hello" },217true, null);218runCase(new String[] { "TLSv1", "SSLv3" },219new String[] { "SSLv3" },220false, "SSLv3");221runCase(new String[] { "TLSv1", "SSLv3" },222new String[] { "TLSv1", "SSLv3", "SSLv2Hello" },223true, null);224225runCase(new String[] { "SSLv3", "SSLv2Hello" },226new String[] { "TLSv1" },227true, null);228runCase(new String[] { "SSLv3", "SSLv2Hello" },229new String[] { "TLSv1", "SSLv2Hello" },230true, null);231runCase(new String[] { "SSLv3", "SSLv2Hello" },232new String[] { "TLSv1", "SSLv3" },233false, "SSLv3");234runCase(new String[] { "SSLv3", "SSLv2Hello" },235new String[] { "SSLv3", "SSLv2Hello" },236false, "SSLv3");237runCase(new String[] { "SSLv3", "SSLv2Hello" },238new String[] { "SSLv3" },239false, "SSLv3");240runCase(new String[] { "SSLv3", "SSLv2Hello" },241new String[] { "TLSv1", "SSLv3", "SSLv2Hello" },242false, "SSLv3");243244runCase(new String[] { "SSLv3" },245new String[] { "TLSv1" },246true, null);247runCase(new String[] { "SSLv3" },248new String[] { "TLSv1", "SSLv2Hello" },249true, null);250runCase(new String[] { "SSLv3" },251new String[] { "TLSv1", "SSLv3" },252false, "SSLv3");253runCase(new String[] { "SSLv3" },254new String[] { "SSLv3", "SSLv2Hello" },255true, null);256runCase(new String[] { "SSLv3" },257new String[] { "SSLv3" },258false, "SSLv3");259runCase(new String[] { "SSLv3" },260new String[] { "TLSv1", "SSLv3", "SSLv2Hello" },261true, null);262263runCase(new String[] { "TLSv1", "SSLv3", "SSLv2Hello" },264new String[] { "TLSv1" },265false, "TLSv1");266runCase(new String[] { "TLSv1", "SSLv3", "SSLv2Hello" },267new String[] { "TLSv1", "SSLv2Hello" },268false, "TLSv1");269runCase(new String[] { "TLSv1", "SSLv3", "SSLv2Hello" },270new String[] { "TLSv1", "SSLv3" },271false, "TLSv1");272runCase(new String[] { "TLSv1", "SSLv3", "SSLv2Hello" },273new String[] { "SSLv3", "SSLv2Hello" },274false, "SSLv3");275runCase(new String[] { "TLSv1", "SSLv3", "SSLv2Hello" },276new String[] { "SSLv3" },277false, "SSLv3");278runCase(new String[] { "TLSv1", "SSLv3", "SSLv2Hello" },279new String[] { "TLSv1", "SSLv3", "SSLv2Hello" },280false, "TLSv1");281}282283private static void runCase(284String[] serverProtocols,285String[] clientProtocols,286boolean exceptionExpected,287String selectedProtocol) throws Exception {288new TestEnabledProtocols(289serverProtocols,290clientProtocols,291exceptionExpected,292selectedProtocol).run();293}294295private static void showProtocols(String name, String[] protocols) {296System.out.printf("Enabled protocols on the %s are: %s%n",297name,298Arrays.asList(protocols));299}300}301302303