Path: blob/master/test/jdk/sun/security/ssl/SSLContextImpl/CustomizedDefaultProtocols.java
41152 views
/*1* Copyright (c) 2013, 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*/2223// SunJSSE does not support dynamic system properties, no way to re-use24// system properties in samevm/agentvm mode.2526/*27* @test28* @bug 7093640 819049229* @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[] {"TLSv1"}),57TLS_CV_04("TLSv1",58new String[] {"TLSv1"}),59TLS_CV_05("TLSv1.1",60new String[] {"TLSv1", "TLSv1.1"}),61TLS_CV_06("TLSv1.2",62new String[] {"TLSv1", "TLSv1.1", "TLSv1.2"}),63TLS_CV_07("TLSv1.3",64new String[] {"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"};72final static String[] serverDefaultProtocols = new String[] {73"TLSv1", "TLSv1.1", "TLSv1.2", "TLSv1.3"};7475ContextVersion(String contextVersion, String[] enabledProtocols) {76this.contextVersion = contextVersion;77this.enabledProtocols = enabledProtocols;78}79}8081private static boolean checkProtocols(String[] target, String[] expected) {82boolean success = true;83if (target.length == 0) {84System.out.println("\t\t\t*** Error: No protocols");85success = false;86}8788if (!protocolEquals(target, expected)) {89System.out.println("\t\t\t*** Error: Expected to get protocols " +90Arrays.toString(expected));91success = false;92}93System.out.println("\t\t\t Protocols found " + Arrays.toString(target));94System.out.println("\t\t\t--> Protocol check passed!!");9596return success;97}9899private static boolean protocolEquals(100String[] actualProtocols,101String[] expectedProtocols) {102if (actualProtocols.length != expectedProtocols.length) {103return false;104}105106Set<String> set = new HashSet<>(Arrays.asList(expectedProtocols));107for (String actual : actualProtocols) {108if (set.add(actual)) {109return false;110}111}112113return true;114}115116private static boolean checkCipherSuites(String[] target) {117boolean success = true;118if (target.length == 0) {119System.out.println("\t\t\t*** Error: No cipher suites");120success = false;121}122123System.out.println("\t\t\t--> Cipher check passed!!");124return success;125}126127public static void main(String[] args) throws Exception {128// reset the security property to make sure that the algorithms129// and keys used in this test are not disabled.130Security.setProperty("jdk.tls.disabledAlgorithms", "");131132boolean failed = false;133for (ContextVersion cv : ContextVersion.values()) {134System.out.println("\n\nChecking SSLContext of " + cv.contextVersion);135System.out.println("============================");136SSLContext context = SSLContext.getInstance(cv.contextVersion);137138// Default SSLContext is initialized automatically.139if (!cv.contextVersion.equals("Default")) {140// Use default TK, KM and random.141context.init((KeyManager[])null, (TrustManager[])null, null);142}143144//145// Check SSLContext146//147// Check default SSLParameters of SSLContext148System.out.println("\tChecking default SSLParameters");149System.out.println("\t\tChecking SSLContext.getDefaultSSLParameters().getProtocols");150SSLParameters parameters = context.getDefaultSSLParameters();151152String[] protocols = parameters.getProtocols();153failed |= !checkProtocols(protocols, cv.enabledProtocols);154155String[] ciphers = parameters.getCipherSuites();156failed |= !checkCipherSuites(ciphers);157158// Check supported SSLParameters of SSLContext159System.out.println("\t\tChecking supported SSLParameters");160parameters = context.getSupportedSSLParameters();161162protocols = parameters.getProtocols();163failed |= !checkProtocols(protocols, cv.supportedProtocols);164165ciphers = parameters.getCipherSuites();166failed |= !checkCipherSuites(ciphers);167168//169// Check SSLEngine170//171// Check SSLParameters of SSLEngine172System.out.println();173System.out.println("\tChecking SSLEngine of this SSLContext");174System.out.println("\t\tChecking SSLEngine.getSSLParameters()");175SSLEngine engine = context.createSSLEngine();176engine.setUseClientMode(true);177parameters = engine.getSSLParameters();178179protocols = parameters.getProtocols();180failed |= !checkProtocols(protocols, cv.enabledProtocols);181182ciphers = parameters.getCipherSuites();183failed |= !checkCipherSuites(ciphers);184185System.out.println("\t\tChecking SSLEngine.getEnabledProtocols()");186protocols = engine.getEnabledProtocols();187failed |= !checkProtocols(protocols, cv.enabledProtocols);188189System.out.println("\t\tChecking SSLEngine.getEnabledCipherSuites()");190ciphers = engine.getEnabledCipherSuites();191failed |= !checkCipherSuites(ciphers);192193System.out.println("\t\tChecking SSLEngine.getSupportedProtocols()");194protocols = engine.getSupportedProtocols();195failed |= !checkProtocols(protocols, cv.supportedProtocols);196197System.out.println(198"\t\tChecking SSLEngine.getSupportedCipherSuites()");199ciphers = engine.getSupportedCipherSuites();200failed |= !checkCipherSuites(ciphers);201202//203// Check SSLSocket204//205// Check SSLParameters of SSLSocket206System.out.println();207System.out.println("\tChecking SSLSocket of this SSLContext");208System.out.println("\t\tChecking SSLSocket.getSSLParameters()");209SocketFactory fac = context.getSocketFactory();210SSLSocket socket = (SSLSocket)fac.createSocket();211parameters = socket.getSSLParameters();212213protocols = parameters.getProtocols();214failed |= !checkProtocols(protocols, cv.enabledProtocols);215216ciphers = parameters.getCipherSuites();217failed |= !checkCipherSuites(ciphers);218219System.out.println("\t\tChecking SSLEngine.getEnabledProtocols()");220protocols = socket.getEnabledProtocols();221failed |= !checkProtocols(protocols, cv.enabledProtocols);222223System.out.println("\t\tChecking SSLEngine.getEnabledCipherSuites()");224ciphers = socket.getEnabledCipherSuites();225failed |= !checkCipherSuites(ciphers);226227System.out.println("\t\tChecking SSLEngine.getSupportedProtocols()");228protocols = socket.getSupportedProtocols();229failed |= !checkProtocols(protocols, cv.supportedProtocols);230231System.out.println(232"\t\tChecking SSLEngine.getSupportedCipherSuites()");233ciphers = socket.getSupportedCipherSuites();234failed |= !checkCipherSuites(ciphers);235236//237// Check SSLServerSocket238//239// Check SSLParameters of SSLServerSocket240System.out.println();241System.out.println("\tChecking SSLServerSocket of this SSLContext");242System.out.println("\t\tChecking SSLServerSocket.getSSLParameters()");243SSLServerSocketFactory sf = context.getServerSocketFactory();244SSLServerSocket ssocket = (SSLServerSocket)sf.createServerSocket();245parameters = ssocket.getSSLParameters();246247protocols = parameters.getProtocols();248failed |= !checkProtocols(protocols, cv.serverDefaultProtocols);249250ciphers = parameters.getCipherSuites();251failed |= !checkCipherSuites(ciphers);252253System.out.println("\t\tChecking SSLEngine.getEnabledProtocols()");254protocols = ssocket.getEnabledProtocols();255failed |= !checkProtocols(protocols, cv.serverDefaultProtocols);256257System.out.println("\t\tChecking SSLEngine.getEnabledCipherSuites()");258ciphers = ssocket.getEnabledCipherSuites();259failed |= !checkCipherSuites(ciphers);260261System.out.println("\t\tChecking SSLEngine.getSupportedProtocols()");262protocols = ssocket.getSupportedProtocols();263failed |= !checkProtocols(protocols, cv.supportedProtocols);264265System.out.println(266"\t\tChecking SSLEngine.getSupportedCipherSuites()");267ciphers = ssocket.getSupportedCipherSuites();268failed |= !checkCipherSuites(ciphers);269}270271if (failed) {272throw new Exception("Run into problems, see log for more details");273}274}275}276277278