Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/ssl/SSLContextImpl/SSLContextVersion.java
38854 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 697611729* @summary SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets30* without TLSv1.1 enabled31* @library /lib/security32* @run main/othervm SSLContextVersion33*/3435import javax.net.ssl.*;3637public class SSLContextVersion {38static enum ContextVersion {39TLS_CV_01("SSL", "TLSv1.2", "TLSv1.2"),40TLS_CV_02("TLS", "TLSv1.2", "TLSv1.2"),41TLS_CV_03("SSLv3", "TLSv1", "TLSv1.2"),42TLS_CV_04("TLSv1", "TLSv1", "TLSv1.2"),43TLS_CV_05("TLSv1.1", "TLSv1.1", "TLSv1.2"),44TLS_CV_06("TLSv1.2", "TLSv1.2", "TLSv1.2"),45TLS_CV_07("Default", "TLSv1.2", "TLSv1.2");4647final String contextVersion;48final String defaultProtocolVersion;49final String supportedProtocolVersion;5051ContextVersion(String contextVersion, String defaultProtocolVersion,52String supportedProtocolVersion) {53this.contextVersion = contextVersion;54this.defaultProtocolVersion = defaultProtocolVersion;55this.supportedProtocolVersion = supportedProtocolVersion;56}57}5859public static void main(String[] args) throws Exception {60// Re-enable TLSv1 and TLSv1.1 since test depends on them.61SecurityUtils.removeFromDisabledTlsAlgs("TLSv1", "TLSv1.1");6263for (ContextVersion cv : ContextVersion.values()) {64System.out.println("Checking SSLContext of " + cv.contextVersion);65SSLContext context = SSLContext.getInstance(cv.contextVersion);6667// Default SSLContext is initialized automatically.68if (!cv.contextVersion.equals("Default")) {69// Use default TK, KM and random.70context.init((KeyManager[])null, (TrustManager[])null, null);71}7273SSLParameters parameters = context.getDefaultSSLParameters();7475String[] protocols = parameters.getProtocols();76String[] ciphers = parameters.getCipherSuites();7778if (protocols.length == 0 || ciphers.length == 0) {79throw new Exception("No default protocols or cipher suites");80}8182boolean isMatch = false;83for (String protocol : protocols) {84System.out.println("\tdefault protocol version " + protocol);85if (protocol.equals(cv.defaultProtocolVersion)) {86isMatch = true;87break;88}89}9091if (!isMatch) {92throw new Exception("No matched default protocol");93}9495parameters = context.getSupportedSSLParameters();9697protocols = parameters.getProtocols();98ciphers = parameters.getCipherSuites();99100if (protocols.length == 0 || ciphers.length == 0) {101throw new Exception("No supported protocols or cipher suites");102}103104isMatch = false;105for (String protocol : protocols) {106System.out.println("\tsupported protocol version " + protocol);107if (protocol.equals(cv.supportedProtocolVersion)) {108isMatch = true;109break;110}111}112113if (!isMatch) {114throw new Exception("No matched supported protocol");115}116System.out.println("\t... Success");117}118}119}120121122