Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/net/ssl/SSLSession/TestEnabledProtocols.java
38854 views
/*1* Copyright (c) 2001, 2018, 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.security.Security;44import java.util.Arrays;4546import javax.net.ssl.SSLException;47import javax.net.ssl.SSLHandshakeException;48import javax.net.ssl.SSLServerSocket;49import javax.net.ssl.SSLSocket;5051public class TestEnabledProtocols extends SSLSocketTemplate {5253private final String[] serverProtocols;54private final String[] clientProtocols;55private final boolean exceptionExpected;56private final String selectedProtocol;5758public TestEnabledProtocols(String[] serverProtocols,59String[] clientProtocols, boolean exceptionExpected,60String selectedProtocol) {61this.serverProtocols = serverProtocols;62this.clientProtocols = clientProtocols;63this.exceptionExpected = exceptionExpected;64this.selectedProtocol = selectedProtocol;65}6667@Override68protected void configureServerSocket(SSLServerSocket sslServerSocket) {69sslServerSocket.setEnabledProtocols(serverProtocols);70}7172@Override73protected void runServerApplication(SSLSocket socket) throws Exception {74try {75socket.startHandshake();7677InputStream in = socket.getInputStream();78OutputStream out = socket.getOutputStream();79out.write(280);80in.read();81} catch (SSLHandshakeException se) {82// ignore it; this is part of the testing83// log it for debugging84System.out.println("Server SSLHandshakeException:");85se.printStackTrace(System.out);86} catch (InterruptedIOException ioe) {87// must have been interrupted, no harm88} catch (SSLException ssle) {89// The client side may have closed the socket.90System.out.println("Server SSLException:");91ssle.printStackTrace(System.out);92} catch (Exception e) {93System.out.println("Server exception:");94e.printStackTrace(System.out);95throw new RuntimeException(e);96}97}9899@Override100protected void runClientApplication(SSLSocket sslSocket) throws Exception {101try {102System.out.println("=== Starting new test run ===");103showProtocols("server", serverProtocols);104showProtocols("client", clientProtocols);105106sslSocket.setEnabledProtocols(clientProtocols);107sslSocket.startHandshake();108109String protocolName = sslSocket.getSession().getProtocol();110System.out.println("Protocol name after getSession is " +111protocolName);112113if (protocolName.equals(selectedProtocol)) {114System.out.println("** Success **");115} else {116System.out.println("** FAILURE ** ");117throw new RuntimeException118("expected protocol " + selectedProtocol +119" but using " + protocolName);120}121122InputStream in = sslSocket.getInputStream();123OutputStream out = sslSocket.getOutputStream();124in.read();125out.write(280);126} catch (SSLHandshakeException e) {127if (!exceptionExpected) {128System.out.println(129"Client got UNEXPECTED SSLHandshakeException:");130e.printStackTrace(System.out);131System.out.println("** FAILURE **");132throw new RuntimeException(e);133} else {134System.out.println(135"Client got expected SSLHandshakeException:");136e.printStackTrace(System.out);137System.out.println("** Success **");138}139} catch (Exception e) {140System.out.println("Client got UNEXPECTED Exception:");141e.printStackTrace(System.out);142System.out.println("** FAILURE **");143throw new RuntimeException(e);144}145}146147public static void main(String[] args) throws Exception {148Security.setProperty("jdk.tls.disabledAlgorithms", "");149150runCase(new String[] { "TLSv1" },151new String[] { "TLSv1" },152false, "TLSv1");153runCase(new String[] { "TLSv1" },154new String[] { "TLSv1", "SSLv2Hello" },155true, null);156runCase(new String[] { "TLSv1" },157new String[] { "TLSv1", "SSLv3" },158false, "TLSv1");159runCase(new String[] { "TLSv1" },160new String[] { "SSLv3", "SSLv2Hello" },161true, null);162runCase(new String[] { "TLSv1" },163new String[] { "SSLv3" },164true, null);165runCase(new String[] { "TLSv1" },166new String[] { "TLSv1", "SSLv3", "SSLv2Hello" },167true, null);168169runCase(new String[] { "TLSv1", "SSLv2Hello" },170new String[] { "TLSv1" },171false, "TLSv1");172runCase(new String[] { "TLSv1", "SSLv2Hello" },173new String[] { "TLSv1", "SSLv2Hello" },174false, "TLSv1");175runCase(new String[] { "TLSv1", "SSLv2Hello" },176new String[] { "TLSv1", "SSLv3" },177false, "TLSv1");178runCase(new String[] { "TLSv1", "SSLv2Hello" },179new String[] { "SSLv3", "SSLv2Hello" },180true, null);181runCase(new String[] { "TLSv1", "SSLv2Hello" },182new String[] { "SSLv3" },183true, null);184runCase(new String[] { "TLSv1", "SSLv2Hello" },185new String[] { "TLSv1", "SSLv3", "SSLv2Hello" },186false, "TLSv1");187188runCase(new String[] { "TLSv1", "SSLv3" },189new String[] { "TLSv1" },190false, "TLSv1");191runCase(new String[] { "TLSv1", "SSLv3" },192new String[] { "TLSv1", "SSLv2Hello" },193true, null);194runCase(new String[] { "TLSv1", "SSLv3" },195new String[] { "TLSv1", "SSLv3" },196false, "TLSv1");197runCase(new String[] { "TLSv1", "SSLv3" },198new String[] { "SSLv3", "SSLv2Hello" },199true, null);200runCase(new String[] { "TLSv1", "SSLv3" },201new String[] { "SSLv3" },202false, "SSLv3");203runCase(new String[] { "TLSv1", "SSLv3" },204new String[] { "TLSv1", "SSLv3", "SSLv2Hello" },205true, null);206207runCase(new String[] { "SSLv3", "SSLv2Hello" },208new String[] { "TLSv1" },209true, null);210runCase(new String[] { "SSLv3", "SSLv2Hello" },211new String[] { "TLSv1", "SSLv2Hello" },212true, null);213runCase(new String[] { "SSLv3", "SSLv2Hello" },214new String[] { "TLSv1", "SSLv3" },215false, "SSLv3");216runCase(new String[] { "SSLv3", "SSLv2Hello" },217new String[] { "SSLv3", "SSLv2Hello" },218false, "SSLv3");219runCase(new String[] { "SSLv3", "SSLv2Hello" },220new String[] { "SSLv3" },221false, "SSLv3");222runCase(new String[] { "SSLv3", "SSLv2Hello" },223new String[] { "TLSv1", "SSLv3", "SSLv2Hello" },224false, "SSLv3");225226runCase(new String[] { "SSLv3" },227new String[] { "TLSv1" },228true, null);229runCase(new String[] { "SSLv3" },230new String[] { "TLSv1", "SSLv2Hello" },231true, null);232runCase(new String[] { "SSLv3" },233new String[] { "TLSv1", "SSLv3" },234false, "SSLv3");235runCase(new String[] { "SSLv3" },236new String[] { "SSLv3", "SSLv2Hello" },237true, null);238runCase(new String[] { "SSLv3" },239new String[] { "SSLv3" },240false, "SSLv3");241runCase(new String[] { "SSLv3" },242new String[] { "TLSv1", "SSLv3", "SSLv2Hello" },243true, null);244245runCase(new String[] { "TLSv1", "SSLv3", "SSLv2Hello" },246new String[] { "TLSv1" },247false, "TLSv1");248runCase(new String[] { "TLSv1", "SSLv3", "SSLv2Hello" },249new String[] { "TLSv1", "SSLv2Hello" },250false, "TLSv1");251runCase(new String[] { "TLSv1", "SSLv3", "SSLv2Hello" },252new String[] { "TLSv1", "SSLv3" },253false, "TLSv1");254runCase(new String[] { "TLSv1", "SSLv3", "SSLv2Hello" },255new String[] { "SSLv3", "SSLv2Hello" },256false, "SSLv3");257runCase(new String[] { "TLSv1", "SSLv3", "SSLv2Hello" },258new String[] { "SSLv3" },259false, "SSLv3");260runCase(new String[] { "TLSv1", "SSLv3", "SSLv2Hello" },261new String[] { "TLSv1", "SSLv3", "SSLv2Hello" },262false, "TLSv1");263}264265private static void runCase(266String[] serverProtocols,267String[] clientProtocols,268boolean exceptionExpected,269String selectedProtocol) throws Exception {270new TestEnabledProtocols(271serverProtocols,272clientProtocols,273exceptionExpected,274selectedProtocol).run();275}276277private static void showProtocols(String name, String[] protocols) {278System.out.printf("Enabled protocols on the %s are: %s%n",279name,280Arrays.asList(protocols));281}282}283284285