Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/ssl/SSLContextImpl/CustomizedDefaultProtocols.java
38853 views
/*1* Copyright (c) 2013, 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 709364029* @summary Enable TLS 1.1 and TLS 1.2 by default in client side of SunJSSE30* @run main/othervm -Djdk.tls.client.protocols="SSLv3,TLSv1,TLSv1.1"31* CustomizedDefaultProtocols32*/3334import java.security.Security;35import java.util.Arrays;36import java.util.HashSet;37import java.util.Set;3839import javax.net.SocketFactory;40import javax.net.ssl.KeyManager;41import javax.net.ssl.SSLContext;42import javax.net.ssl.SSLEngine;43import javax.net.ssl.SSLParameters;44import javax.net.ssl.SSLServerSocket;45import javax.net.ssl.SSLServerSocketFactory;46import javax.net.ssl.SSLSocket;47import javax.net.ssl.TrustManager;4849public class CustomizedDefaultProtocols {50enum ContextVersion {51TLS_CV_01("SSL",52new String[] {"SSLv3", "TLSv1", "TLSv1.1"}),53TLS_CV_02("TLS",54new String[] {"SSLv3", "TLSv1", "TLSv1.1"}),55TLS_CV_03("SSLv3",56new String[] {"SSLv3", "TLSv1"}),57TLS_CV_04("TLSv1",58new String[] {"SSLv3", "TLSv1"}),59TLS_CV_05("TLSv1.1",60new String[] {"SSLv3", "TLSv1", "TLSv1.1"}),61TLS_CV_06("TLSv1.2",62new String[] {"SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2"}),63TLS_CV_07("TLSv1.3",64new String[] {"SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2", "TLSv1.3"}),65TLS_CV_08("Default",66new String[] {"SSLv3", "TLSv1", "TLSv1.1"});6768final String contextVersion;69final String[] enabledProtocols;70final static String[] supportedProtocols = new String[] {71"SSLv2Hello", "SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2", "TLSv1.3"};7273ContextVersion(String contextVersion, String[] enabledProtocols) {74this.contextVersion = contextVersion;75this.enabledProtocols = enabledProtocols;76}77}7879private static boolean checkProtocols(String[] target, String[] expected) {80boolean success = true;81if (target.length == 0) {82System.out.println("\tError: No protocols");83success = false;84}8586if (!protocolEquals(target, expected)) {87System.out.println("\tError: Expected to get protocols " +88Arrays.toString(expected));89success = false;90}91System.out.println("\t Protocols found " + Arrays.toString(target));9293return success;94}9596private static boolean protocolEquals(97String[] actualProtocols,98String[] expectedProtocols) {99if (actualProtocols.length != expectedProtocols.length) {100return false;101}102103Set<String> set = new HashSet<>(Arrays.asList(expectedProtocols));104for (String actual : actualProtocols) {105if (set.add(actual)) {106return false;107}108}109110return true;111}112113private static boolean checkCipherSuites(String[] target) {114boolean success = true;115if (target.length == 0) {116System.out.println("\tError: No cipher suites");117success = false;118}119120return success;121}122123public static void main(String[] args) throws Exception {124// reset the security property to make sure that the algorithms125// and keys used in this test are not disabled.126Security.setProperty("jdk.tls.disabledAlgorithms", "");127128boolean failed = false;129for (ContextVersion cv : ContextVersion.values()) {130System.out.println("Checking SSLContext of " + cv.contextVersion);131SSLContext context = SSLContext.getInstance(cv.contextVersion);132133// Default SSLContext is initialized automatically.134if (!cv.contextVersion.equals("Default")) {135// Use default TK, KM and random.136context.init((KeyManager[])null, (TrustManager[])null, null);137}138139//140// Check SSLContext141//142// Check default SSLParameters of SSLContext143System.out.println("\tChecking default SSLParameters");144SSLParameters parameters = context.getDefaultSSLParameters();145146String[] protocols = parameters.getProtocols();147failed |= !checkProtocols(protocols, cv.enabledProtocols);148149String[] ciphers = parameters.getCipherSuites();150failed |= !checkCipherSuites(ciphers);151152// Check supported SSLParameters of SSLContext153System.out.println("\tChecking supported SSLParameters");154parameters = context.getSupportedSSLParameters();155156protocols = parameters.getProtocols();157failed |= !checkProtocols(protocols, cv.supportedProtocols);158159ciphers = parameters.getCipherSuites();160failed |= !checkCipherSuites(ciphers);161162//163// Check SSLEngine164//165// Check SSLParameters of SSLEngine166System.out.println();167System.out.println("\tChecking SSLEngine of this SSLContext");168System.out.println("\tChecking SSLEngine.getSSLParameters()");169SSLEngine engine = context.createSSLEngine();170engine.setUseClientMode(true);171parameters = engine.getSSLParameters();172173protocols = parameters.getProtocols();174failed |= !checkProtocols(protocols, cv.enabledProtocols);175176ciphers = parameters.getCipherSuites();177failed |= !checkCipherSuites(ciphers);178179System.out.println("\tChecking SSLEngine.getEnabledProtocols()");180protocols = engine.getEnabledProtocols();181failed |= !checkProtocols(protocols, cv.enabledProtocols);182183System.out.println("\tChecking SSLEngine.getEnabledCipherSuites()");184ciphers = engine.getEnabledCipherSuites();185failed |= !checkCipherSuites(ciphers);186187System.out.println("\tChecking SSLEngine.getSupportedProtocols()");188protocols = engine.getSupportedProtocols();189failed |= !checkProtocols(protocols, cv.supportedProtocols);190191System.out.println(192"\tChecking SSLEngine.getSupportedCipherSuites()");193ciphers = engine.getSupportedCipherSuites();194failed |= !checkCipherSuites(ciphers);195196//197// Check SSLSocket198//199// Check SSLParameters of SSLSocket200System.out.println();201System.out.println("\tChecking SSLSocket of this SSLContext");202System.out.println("\tChecking SSLSocket.getSSLParameters()");203SocketFactory fac = context.getSocketFactory();204SSLSocket socket = (SSLSocket)fac.createSocket();205parameters = socket.getSSLParameters();206207protocols = parameters.getProtocols();208failed |= !checkProtocols(protocols, cv.enabledProtocols);209210ciphers = parameters.getCipherSuites();211failed |= !checkCipherSuites(ciphers);212213System.out.println("\tChecking SSLEngine.getEnabledProtocols()");214protocols = socket.getEnabledProtocols();215failed |= !checkProtocols(protocols, cv.enabledProtocols);216217System.out.println("\tChecking SSLEngine.getEnabledCipherSuites()");218ciphers = socket.getEnabledCipherSuites();219failed |= !checkCipherSuites(ciphers);220221System.out.println("\tChecking SSLEngine.getSupportedProtocols()");222protocols = socket.getSupportedProtocols();223failed |= !checkProtocols(protocols, cv.supportedProtocols);224225System.out.println(226"\tChecking SSLEngine.getSupportedCipherSuites()");227ciphers = socket.getSupportedCipherSuites();228failed |= !checkCipherSuites(ciphers);229230//231// Check SSLServerSocket232//233// Check SSLParameters of SSLServerSocket234System.out.println();235System.out.println("\tChecking SSLServerSocket of this SSLContext");236System.out.println("\tChecking SSLServerSocket.getSSLParameters()");237SSLServerSocketFactory sf = context.getServerSocketFactory();238SSLServerSocket ssocket = (SSLServerSocket)sf.createServerSocket();239parameters = ssocket.getSSLParameters();240241protocols = parameters.getProtocols();242failed |= !checkProtocols(protocols, cv.supportedProtocols);243244ciphers = parameters.getCipherSuites();245failed |= !checkCipherSuites(ciphers);246247System.out.println("\tChecking SSLEngine.getEnabledProtocols()");248protocols = ssocket.getEnabledProtocols();249failed |= !checkProtocols(protocols, cv.supportedProtocols);250251System.out.println("\tChecking SSLEngine.getEnabledCipherSuites()");252ciphers = ssocket.getEnabledCipherSuites();253failed |= !checkCipherSuites(ciphers);254255System.out.println("\tChecking SSLEngine.getSupportedProtocols()");256protocols = ssocket.getSupportedProtocols();257failed |= !checkProtocols(protocols, cv.supportedProtocols);258259System.out.println(260"\tChecking SSLEngine.getSupportedCipherSuites()");261ciphers = ssocket.getSupportedCipherSuites();262failed |= !checkCipherSuites(ciphers);263}264265if (failed) {266throw new Exception("Run into problems, see log for more details");267} else {268System.out.println("\t... Success");269}270}271}272273274