Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/ssl/SSLContextImpl/DefaultEnabledProtocols.java
38854 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 DefaultEnabledProtocols31*/3233import java.security.Security;34import java.util.Arrays;35import java.util.HashSet;36import java.util.Set;3738import javax.net.SocketFactory;39import javax.net.ssl.KeyManager;40import javax.net.ssl.SSLContext;41import javax.net.ssl.SSLEngine;42import javax.net.ssl.SSLParameters;43import javax.net.ssl.SSLServerSocket;44import javax.net.ssl.SSLServerSocketFactory;45import javax.net.ssl.SSLSocket;46import javax.net.ssl.TrustManager;4748public class DefaultEnabledProtocols {49enum ContextVersion {50TLS_CV_01("SSL",51new String[] {"SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2"}),52TLS_CV_02("TLS",53new String[] {"SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2"}),54TLS_CV_03("SSLv3",55new String[] {"SSLv3", "TLSv1"}),56TLS_CV_04("TLSv1",57new String[] {"SSLv3", "TLSv1"}),58TLS_CV_05("TLSv1.1",59new String[] {"SSLv3", "TLSv1", "TLSv1.1"}),60TLS_CV_06("TLSv1.2",61new String[] {"SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2"}),62TLS_CV_07("TLSv1.3",63new String[] {"SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2", "TLSv1.3"}),64TLS_CV_08("Default",65new String[] {"SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2"});6667final String contextVersion;68final String[] enabledProtocols;69final static String[] supportedProtocols = new String[] {70"SSLv2Hello", "SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2", "TLSv1.3"};7172ContextVersion(String contextVersion, String[] enabledProtocols) {73this.contextVersion = contextVersion;74this.enabledProtocols = enabledProtocols;75}76}7778private static boolean checkProtocols(String[] target, String[] expected) {79boolean success = true;80if (target.length == 0) {81System.out.println("\tError: No protocols");82success = false;83}8485if (!protocolEquals(target, expected)) {86System.out.println("\tError: Expected to get protocols " +87Arrays.toString(expected));88success = false;89}90System.out.println("\t Protocols found " + Arrays.toString(target));9192return success;93}9495private static boolean protocolEquals(96String[] actualProtocols,97String[] expectedProtocols) {98if (actualProtocols.length != expectedProtocols.length) {99return false;100}101102Set<String> set = new HashSet<>(Arrays.asList(expectedProtocols));103for (String actual : actualProtocols) {104if (set.add(actual)) {105return false;106}107}108109return true;110}111112private static boolean checkCipherSuites(String[] target) {113boolean success = true;114if (target.length == 0) {115System.out.println("\tError: No cipher suites");116success = false;117}118119return success;120}121122public static void main(String[] args) throws Exception {123// reset the security property to make sure that the algorithms124// and keys used in this test are not disabled.125Security.setProperty("jdk.tls.disabledAlgorithms", "");126127boolean failed = false;128for (ContextVersion cv : ContextVersion.values()) {129System.out.println("Checking SSLContext of " + cv.contextVersion);130SSLContext context = SSLContext.getInstance(cv.contextVersion);131132// Default SSLContext is initialized automatically.133if (!cv.contextVersion.equals("Default")) {134// Use default TK, KM and random.135context.init((KeyManager[])null, (TrustManager[])null, null);136}137138//139// Check SSLContext140//141// Check default SSLParameters of SSLContext142System.out.println("\tChecking default SSLParameters");143SSLParameters parameters = context.getDefaultSSLParameters();144145String[] protocols = parameters.getProtocols();146failed |= !checkProtocols(protocols, cv.enabledProtocols);147148String[] ciphers = parameters.getCipherSuites();149failed |= !checkCipherSuites(ciphers);150151// Check supported SSLParameters of SSLContext152System.out.println("\tChecking supported SSLParameters");153parameters = context.getSupportedSSLParameters();154155protocols = parameters.getProtocols();156failed |= !checkProtocols(protocols, cv.supportedProtocols);157158ciphers = parameters.getCipherSuites();159failed |= !checkCipherSuites(ciphers);160161//162// Check SSLEngine163//164// Check SSLParameters of SSLEngine165System.out.println();166System.out.println("\tChecking SSLEngine of this SSLContext");167System.out.println("\tChecking SSLEngine.getSSLParameters()");168SSLEngine engine = context.createSSLEngine();169engine.setUseClientMode(true);170parameters = engine.getSSLParameters();171172protocols = parameters.getProtocols();173failed |= !checkProtocols(protocols, cv.enabledProtocols);174175ciphers = parameters.getCipherSuites();176failed |= !checkCipherSuites(ciphers);177178System.out.println("\tChecking SSLEngine.getEnabledProtocols()");179protocols = engine.getEnabledProtocols();180failed |= !checkProtocols(protocols, cv.enabledProtocols);181182System.out.println("\tChecking SSLEngine.getEnabledCipherSuites()");183ciphers = engine.getEnabledCipherSuites();184failed |= !checkCipherSuites(ciphers);185186System.out.println("\tChecking SSLEngine.getSupportedProtocols()");187protocols = engine.getSupportedProtocols();188failed |= !checkProtocols(protocols, cv.supportedProtocols);189190System.out.println(191"\tChecking SSLEngine.getSupportedCipherSuites()");192ciphers = engine.getSupportedCipherSuites();193failed |= !checkCipherSuites(ciphers);194195//196// Check SSLSocket197//198// Check SSLParameters of SSLSocket199System.out.println();200System.out.println("\tChecking SSLSocket of this SSLContext");201System.out.println("\tChecking SSLSocket.getSSLParameters()");202SocketFactory fac = context.getSocketFactory();203SSLSocket socket = (SSLSocket)fac.createSocket();204parameters = socket.getSSLParameters();205206protocols = parameters.getProtocols();207failed |= !checkProtocols(protocols, cv.enabledProtocols);208209ciphers = parameters.getCipherSuites();210failed |= !checkCipherSuites(ciphers);211212System.out.println("\tChecking SSLEngine.getEnabledProtocols()");213protocols = socket.getEnabledProtocols();214failed |= !checkProtocols(protocols, cv.enabledProtocols);215216System.out.println("\tChecking SSLEngine.getEnabledCipherSuites()");217ciphers = socket.getEnabledCipherSuites();218failed |= !checkCipherSuites(ciphers);219220System.out.println("\tChecking SSLEngine.getSupportedProtocols()");221protocols = socket.getSupportedProtocols();222failed |= !checkProtocols(protocols, cv.supportedProtocols);223224System.out.println(225"\tChecking SSLEngine.getSupportedCipherSuites()");226ciphers = socket.getSupportedCipherSuites();227failed |= !checkCipherSuites(ciphers);228229//230// Check SSLServerSocket231//232// Check SSLParameters of SSLServerSocket233System.out.println();234System.out.println("\tChecking SSLServerSocket of this SSLContext");235System.out.println("\tChecking SSLServerSocket.getSSLParameters()");236SSLServerSocketFactory sf = context.getServerSocketFactory();237SSLServerSocket ssocket = (SSLServerSocket)sf.createServerSocket();238parameters = ssocket.getSSLParameters();239240protocols = parameters.getProtocols();241failed |= !checkProtocols(protocols, cv.supportedProtocols);242243ciphers = parameters.getCipherSuites();244failed |= !checkCipherSuites(ciphers);245246System.out.println("\tChecking SSLEngine.getEnabledProtocols()");247protocols = ssocket.getEnabledProtocols();248failed |= !checkProtocols(protocols, cv.supportedProtocols);249250System.out.println("\tChecking SSLEngine.getEnabledCipherSuites()");251ciphers = ssocket.getEnabledCipherSuites();252failed |= !checkCipherSuites(ciphers);253254System.out.println("\tChecking SSLEngine.getSupportedProtocols()");255protocols = ssocket.getSupportedProtocols();256failed |= !checkProtocols(protocols, cv.supportedProtocols);257258System.out.println(259"\tChecking SSLEngine.getSupportedCipherSuites()");260ciphers = ssocket.getSupportedCipherSuites();261failed |= !checkCipherSuites(ciphers);262}263264if (failed) {265throw new Exception("Run into problems, see log for more details");266} else {267System.out.println("\t... Success");268}269}270}271272273