Path: blob/master/test/jdk/sun/security/ssl/SSLContextImpl/SSLContextVersion.java
41152 views
/*1* Copyright (c) 2011, 2020, 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 6976117 823472529* @summary SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets30* without TLSv1.1 enabled31* @library /test/lib32* @run main/othervm SSLContextVersion33*/3435import javax.net.ssl.*;3637import jdk.test.lib.security.SecurityUtils;3839public class SSLContextVersion {40static enum ContextVersion {41TLS_CV_01("SSL", "TLSv1.2", "TLSv1.2"),42TLS_CV_02("TLS", "TLSv1.2", "TLSv1.2"),43TLS_CV_03("SSLv3", "TLSv1", "TLSv1.2"),44TLS_CV_04("TLSv1", "TLSv1", "TLSv1.2"),45TLS_CV_05("TLSv1.1", "TLSv1.1", "TLSv1.2"),46TLS_CV_06("TLSv1.2", "TLSv1.2", "TLSv1.2"),47TLS_CV_07("Default", "TLSv1.2", "TLSv1.2"),48TLS_CV_08("Default", "TLSv1.3", "TLSv1.3"),49TLS_CV_09("TLS", "TLSv1.3", "TLSv1.3"),50TLS_CV_10("TLSv1.3", "TLSv1.3", "TLSv1.3");5152final String contextVersion;53final String defaultProtocolVersion;54final String supportedProtocolVersion;5556ContextVersion(String contextVersion, String defaultProtocolVersion,57String supportedProtocolVersion) {58this.contextVersion = contextVersion;59this.defaultProtocolVersion = defaultProtocolVersion;60this.supportedProtocolVersion = supportedProtocolVersion;61}62}6364public static void main(String[] args) throws Exception {65// Re-enable TLSv1 and TLSv1.1 since test depends on them.66SecurityUtils.removeFromDisabledTlsAlgs("TLSv1", "TLSv1.1");6768for (ContextVersion cv : ContextVersion.values()) {69System.out.println("Checking SSLContext of " + cv.contextVersion);70SSLContext context = SSLContext.getInstance(cv.contextVersion);7172// Default SSLContext is initialized automatically.73if (!cv.contextVersion.equals("Default")) {74// Use default TK, KM and random.75context.init((KeyManager[])null, (TrustManager[])null, null);76}7778SSLParameters parameters = context.getDefaultSSLParameters();7980String[] protocols = parameters.getProtocols();81String[] ciphers = parameters.getCipherSuites();8283if (protocols.length == 0 || ciphers.length == 0) {84throw new Exception("No default protocols or cipher suites");85}8687boolean isMatch = false;88for (String protocol : protocols) {89System.out.println("\tdefault protocol version " + protocol);90if (protocol.equals(cv.defaultProtocolVersion)) {91isMatch = true;92break;93}94}9596if (!isMatch) {97throw new Exception("No matched default protocol");98}99100parameters = context.getSupportedSSLParameters();101102protocols = parameters.getProtocols();103ciphers = parameters.getCipherSuites();104105if (protocols.length == 0 || ciphers.length == 0) {106throw new Exception("No supported protocols or cipher suites");107}108109isMatch = false;110for (String protocol : protocols) {111System.out.println("\tsupported protocol version " + protocol);112if (protocol.equals(cv.supportedProtocolVersion)) {113isMatch = true;114break;115}116}117118if (!isMatch) {119throw new Exception("No matched supported protocol");120}121System.out.println("\t... Success");122}123}124}125126127