Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/ssl/SSLContextImpl/NoOldVersionContext.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="TLSv1,TLSv1.1,TLSv1.2"31* NoOldVersionContext32*/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 NoOldVersionContext {50static enum ContextVersion {51TLS_CV_01("SSL",52new String[] {"TLSv1", "TLSv1.1", "TLSv1.2"}),53TLS_CV_02("TLS",54new String[] {"TLSv1", "TLSv1.1", "TLSv1.2"}),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[] {"TLSv1", "TLSv1.1", "TLSv1.2"});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));89System.out.println("\tError: The actual protocols " +90Arrays.toString(target));91success = false;92}9394return success;95}9697private static boolean protocolEquals(98String[] actualProtocols,99String[] expectedProtocols) {100if (actualProtocols.length != expectedProtocols.length) {101return false;102}103104Set<String> set = new HashSet<>(Arrays.asList(expectedProtocols));105for (String actual : actualProtocols) {106if (set.add(actual)) {107return false;108}109}110111return true;112}113114private static boolean checkCipherSuites(String[] target) {115boolean success = true;116if (target.length == 0) {117System.out.println("\tError: No cipher suites");118success = false;119}120121return success;122}123124public static void main(String[] args) throws Exception {125// reset the security property to make sure that the algorithms126// and keys used in this test are not disabled.127Security.setProperty("jdk.tls.disabledAlgorithms", "");128129boolean failed = false;130for (ContextVersion cv : ContextVersion.values()) {131System.out.println("Checking SSLContext of " + cv.contextVersion);132SSLContext context = SSLContext.getInstance(cv.contextVersion);133134// Default SSLContext is initialized automatically.135if (!cv.contextVersion.equals("Default")) {136// Use default TK, KM and random.137context.init((KeyManager[])null, (TrustManager[])null, null);138}139140//141// Check SSLContext142//143// Check default SSLParameters of SSLContext144System.out.println("\tChecking default SSLParameters");145SSLParameters parameters = context.getDefaultSSLParameters();146147String[] protocols = parameters.getProtocols();148failed |= !checkProtocols(protocols, cv.enabledProtocols);149150String[] ciphers = parameters.getCipherSuites();151failed |= !checkCipherSuites(ciphers);152153// Check supported SSLParameters of SSLContext154System.out.println("\tChecking supported SSLParameters");155parameters = context.getSupportedSSLParameters();156157protocols = parameters.getProtocols();158failed |= !checkProtocols(protocols, cv.supportedProtocols);159160ciphers = parameters.getCipherSuites();161failed |= !checkCipherSuites(ciphers);162163//164// Check SSLEngine165//166// Check SSLParameters of SSLEngine167System.out.println();168System.out.println("\tChecking SSLEngine of this SSLContext");169System.out.println("\tChecking SSLEngine.getSSLParameters()");170SSLEngine engine = context.createSSLEngine();171engine.setUseClientMode(true);172parameters = engine.getSSLParameters();173174protocols = parameters.getProtocols();175failed |= !checkProtocols(protocols, cv.enabledProtocols);176177ciphers = parameters.getCipherSuites();178failed |= !checkCipherSuites(ciphers);179180System.out.println("\tChecking SSLEngine.getEnabledProtocols()");181protocols = engine.getEnabledProtocols();182failed |= !checkProtocols(protocols, cv.enabledProtocols);183184System.out.println("\tChecking SSLEngine.getEnabledCipherSuites()");185ciphers = engine.getEnabledCipherSuites();186failed |= !checkCipherSuites(ciphers);187188System.out.println("\tChecking SSLEngine.getSupportedProtocols()");189protocols = engine.getSupportedProtocols();190failed |= !checkProtocols(protocols, cv.supportedProtocols);191192System.out.println(193"\tChecking SSLEngine.getSupportedCipherSuites()");194ciphers = engine.getSupportedCipherSuites();195failed |= !checkCipherSuites(ciphers);196197//198// Check SSLSocket199//200// Check SSLParameters of SSLSocket201System.out.println();202System.out.println("\tChecking SSLSocket of this SSLContext");203System.out.println("\tChecking SSLSocket.getSSLParameters()");204SocketFactory fac = context.getSocketFactory();205SSLSocket socket = (SSLSocket)fac.createSocket();206parameters = socket.getSSLParameters();207208protocols = parameters.getProtocols();209failed |= !checkProtocols(protocols, cv.enabledProtocols);210211ciphers = parameters.getCipherSuites();212failed |= !checkCipherSuites(ciphers);213214System.out.println("\tChecking SSLEngine.getEnabledProtocols()");215protocols = socket.getEnabledProtocols();216failed |= !checkProtocols(protocols, cv.enabledProtocols);217218System.out.println("\tChecking SSLEngine.getEnabledCipherSuites()");219ciphers = socket.getEnabledCipherSuites();220failed |= !checkCipherSuites(ciphers);221222System.out.println("\tChecking SSLEngine.getSupportedProtocols()");223protocols = socket.getSupportedProtocols();224failed |= !checkProtocols(protocols, cv.supportedProtocols);225226System.out.println(227"\tChecking SSLEngine.getSupportedCipherSuites()");228ciphers = socket.getSupportedCipherSuites();229failed |= !checkCipherSuites(ciphers);230231//232// Check SSLServerSocket233//234// Check SSLParameters of SSLServerSocket235System.out.println();236System.out.println("\tChecking SSLServerSocket of this SSLContext");237System.out.println("\tChecking SSLServerSocket.getSSLParameters()");238SSLServerSocketFactory sf = context.getServerSocketFactory();239SSLServerSocket ssocket = (SSLServerSocket)sf.createServerSocket();240parameters = ssocket.getSSLParameters();241242protocols = parameters.getProtocols();243failed |= !checkProtocols(protocols, cv.supportedProtocols);244245ciphers = parameters.getCipherSuites();246failed |= !checkCipherSuites(ciphers);247248System.out.println("\tChecking SSLEngine.getEnabledProtocols()");249protocols = ssocket.getEnabledProtocols();250failed |= !checkProtocols(protocols, cv.supportedProtocols);251252System.out.println("\tChecking SSLEngine.getEnabledCipherSuites()");253ciphers = ssocket.getEnabledCipherSuites();254failed |= !checkCipherSuites(ciphers);255256System.out.println("\tChecking SSLEngine.getSupportedProtocols()");257protocols = ssocket.getSupportedProtocols();258failed |= !checkProtocols(protocols, cv.supportedProtocols);259260System.out.println(261"\tChecking SSLEngine.getSupportedCipherSuites()");262ciphers = ssocket.getSupportedCipherSuites();263failed |= !checkCipherSuites(ciphers);264}265266if (failed) {267throw new Exception("Run into problems, see log for more details");268} else {269System.out.println("\t... Success");270}271}272}273274275