Path: blob/master/test/jdk/javax/net/ssl/SSLSession/TestEnabledProtocols.java
66646 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.IOException;41import java.io.InputStream;42import java.io.InterruptedIOException;43import java.io.OutputStream;44import java.net.InetAddress;45import java.net.SocketException;46import java.security.Security;47import java.util.Arrays;4849import javax.net.ssl.SSLException;50import javax.net.ssl.SSLHandshakeException;51import javax.net.ssl.SSLServerSocket;52import javax.net.ssl.SSLSocket;5354public class TestEnabledProtocols extends SSLSocketTemplate {5556private final String[] serverProtocols;57private final String[] clientProtocols;58private final boolean exceptionExpected;59private final String selectedProtocol;6061public TestEnabledProtocols(String[] serverProtocols,62String[] clientProtocols, boolean exceptionExpected,63String selectedProtocol) {64this.serverProtocols = serverProtocols;65this.clientProtocols = clientProtocols;66this.exceptionExpected = exceptionExpected;67this.selectedProtocol = selectedProtocol;68this.serverAddress = InetAddress.getLoopbackAddress();69}7071@Override72protected void configureServerSocket(SSLServerSocket sslServerSocket) {73sslServerSocket.setEnabledProtocols(serverProtocols);74}7576@Override77protected void runServerApplication(SSLSocket socket) throws Exception {78try {79socket.startHandshake();8081InputStream in = socket.getInputStream();82OutputStream out = socket.getOutputStream();83out.write(280);84in.read();85} catch (SSLHandshakeException se) {86// ignore it; this is part of the testing87// log it for debugging88System.out.println("Server SSLHandshakeException:");89se.printStackTrace(System.out);90} catch (InterruptedIOException ioe) {91// must have been interrupted, no harm92} catch (SSLException | SocketException se) {93// The client side may have closed the socket.94System.out.println("Server SSLException:");95se.printStackTrace(System.out);96} catch (Exception e) {97System.out.println("Server exception:");98e.printStackTrace(System.out);99throw new RuntimeException(e);100}101}102103@Override104protected void runClientApplication(SSLSocket sslSocket) throws Exception {105try {106System.out.println("=== Starting new test run ===");107showProtocols("server", serverProtocols);108showProtocols("client", clientProtocols);109110sslSocket.setEnabledProtocols(clientProtocols);111sslSocket.startHandshake();112113String protocolName = sslSocket.getSession().getProtocol();114System.out.println("Protocol name after getSession is " +115protocolName);116117if (protocolName.equals(selectedProtocol)) {118System.out.println("** Success **");119} else {120System.out.println("** FAILURE ** ");121throw new RuntimeException122("expected protocol " + selectedProtocol +123" but using " + protocolName);124}125126InputStream in = sslSocket.getInputStream();127OutputStream out = sslSocket.getOutputStream();128in.read();129out.write(280);130} catch (SSLHandshakeException e) {131if (!exceptionExpected) {132failTest(e, "Client got UNEXPECTED SSLHandshakeException:");133} else {134System.out.println(135"Client got expected SSLHandshakeException:");136e.printStackTrace(System.out);137System.out.println("** Success **");138}139} catch (SSLException | SocketException se) {140// The server side may have closed the socket.141if (isConnectionReset(se)) {142System.out.println("Client SocketException:");143se.printStackTrace(System.out);144} else {145failTest(se, "Client got UNEXPECTED Exception:");146}147148} catch (Exception e) {149failTest(e, "Client got UNEXPECTED Exception:");150}151}152153private boolean isConnectionReset(IOException ioe) {154Throwable cause = ioe instanceof SSLException se ? se.getCause() : ioe;155return cause instanceof SocketException156&& "Connection reset".equals(cause.getMessage());157}158159private void failTest(Exception e, String message) {160System.out.println(message);161e.printStackTrace(System.out);162System.out.println("** FAILURE **");163throw new RuntimeException(e);164}165166public static void main(String[] args) throws Exception {167Security.setProperty("jdk.tls.disabledAlgorithms", "");168169runCase(new String[] { "TLSv1" },170new String[] { "TLSv1" },171false, "TLSv1");172runCase(new String[] { "TLSv1" },173new String[] { "TLSv1", "SSLv2Hello" },174true, null);175runCase(new String[] { "TLSv1" },176new String[] { "TLSv1", "SSLv3" },177false, "TLSv1");178runCase(new String[] { "TLSv1" },179new String[] { "SSLv3", "SSLv2Hello" },180true, null);181runCase(new String[] { "TLSv1" },182new String[] { "SSLv3" },183true, null);184runCase(new String[] { "TLSv1" },185new String[] { "TLSv1", "SSLv3", "SSLv2Hello" },186true, null);187188runCase(new String[] { "TLSv1", "SSLv2Hello" },189new String[] { "TLSv1" },190false, "TLSv1");191runCase(new String[] { "TLSv1", "SSLv2Hello" },192new String[] { "TLSv1", "SSLv2Hello" },193false, "TLSv1");194runCase(new String[] { "TLSv1", "SSLv2Hello" },195new String[] { "TLSv1", "SSLv3" },196false, "TLSv1");197runCase(new String[] { "TLSv1", "SSLv2Hello" },198new String[] { "SSLv3", "SSLv2Hello" },199true, null);200runCase(new String[] { "TLSv1", "SSLv2Hello" },201new String[] { "SSLv3" },202true, null);203runCase(new String[] { "TLSv1", "SSLv2Hello" },204new String[] { "TLSv1", "SSLv3", "SSLv2Hello" },205false, "TLSv1");206207runCase(new String[] { "TLSv1", "SSLv3" },208new String[] { "TLSv1" },209false, "TLSv1");210runCase(new String[] { "TLSv1", "SSLv3" },211new String[] { "TLSv1", "SSLv2Hello" },212true, null);213runCase(new String[] { "TLSv1", "SSLv3" },214new String[] { "TLSv1", "SSLv3" },215false, "TLSv1");216runCase(new String[] { "TLSv1", "SSLv3" },217new String[] { "SSLv3", "SSLv2Hello" },218true, null);219runCase(new String[] { "TLSv1", "SSLv3" },220new String[] { "SSLv3" },221false, "SSLv3");222runCase(new String[] { "TLSv1", "SSLv3" },223new String[] { "TLSv1", "SSLv3", "SSLv2Hello" },224true, null);225226runCase(new String[] { "SSLv3", "SSLv2Hello" },227new String[] { "TLSv1" },228true, null);229runCase(new String[] { "SSLv3", "SSLv2Hello" },230new String[] { "TLSv1", "SSLv2Hello" },231true, null);232runCase(new String[] { "SSLv3", "SSLv2Hello" },233new String[] { "TLSv1", "SSLv3" },234false, "SSLv3");235runCase(new String[] { "SSLv3", "SSLv2Hello" },236new String[] { "SSLv3", "SSLv2Hello" },237false, "SSLv3");238runCase(new String[] { "SSLv3", "SSLv2Hello" },239new String[] { "SSLv3" },240false, "SSLv3");241runCase(new String[] { "SSLv3", "SSLv2Hello" },242new String[] { "TLSv1", "SSLv3", "SSLv2Hello" },243false, "SSLv3");244245runCase(new String[] { "SSLv3" },246new String[] { "TLSv1" },247true, null);248runCase(new String[] { "SSLv3" },249new String[] { "TLSv1", "SSLv2Hello" },250true, null);251runCase(new String[] { "SSLv3" },252new String[] { "TLSv1", "SSLv3" },253false, "SSLv3");254runCase(new String[] { "SSLv3" },255new String[] { "SSLv3", "SSLv2Hello" },256true, null);257runCase(new String[] { "SSLv3" },258new String[] { "SSLv3" },259false, "SSLv3");260runCase(new String[] { "SSLv3" },261new String[] { "TLSv1", "SSLv3", "SSLv2Hello" },262true, null);263264runCase(new String[] { "TLSv1", "SSLv3", "SSLv2Hello" },265new String[] { "TLSv1" },266false, "TLSv1");267runCase(new String[] { "TLSv1", "SSLv3", "SSLv2Hello" },268new String[] { "TLSv1", "SSLv2Hello" },269false, "TLSv1");270runCase(new String[] { "TLSv1", "SSLv3", "SSLv2Hello" },271new String[] { "TLSv1", "SSLv3" },272false, "TLSv1");273runCase(new String[] { "TLSv1", "SSLv3", "SSLv2Hello" },274new String[] { "SSLv3", "SSLv2Hello" },275false, "SSLv3");276runCase(new String[] { "TLSv1", "SSLv3", "SSLv2Hello" },277new String[] { "SSLv3" },278false, "SSLv3");279runCase(new String[] { "TLSv1", "SSLv3", "SSLv2Hello" },280new String[] { "TLSv1", "SSLv3", "SSLv2Hello" },281false, "TLSv1");282}283284private static void runCase(285String[] serverProtocols,286String[] clientProtocols,287boolean exceptionExpected,288String selectedProtocol) throws Exception {289new TestEnabledProtocols(290serverProtocols,291clientProtocols,292exceptionExpected,293selectedProtocol).run();294}295296private static void showProtocols(String name, String[] protocols) {297System.out.printf("Enabled protocols on the %s are: %s%n",298name,299Arrays.asList(protocols));300}301}302303304