Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/ssl/SSLContextImpl/IllegalProtocolProperty.java
38853 views
/*1* Copyright (c) 2013, 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 709364029* @summary Enable TLS 1.1 and TLS 1.2 by default in client side of SunJSSE30* @library /lib/security31* @run main/othervm -Djdk.tls.client.protocols="XSLv3,TLSv1"32* IllegalProtocolProperty33*/3435import javax.net.ssl.*;36import java.security.NoSuchAlgorithmException;3738public class IllegalProtocolProperty {39static enum ContextVersion {40TLS_CV_01("SSL", "TLSv1", "TLSv1.2", true),41TLS_CV_02("TLS", "TLSv1", "TLSv1.2", true),42TLS_CV_03("SSLv3", "TLSv1", "TLSv1.2", false),43TLS_CV_04("TLSv1", "TLSv1", "TLSv1.2", false),44TLS_CV_05("TLSv1.1", "TLSv1.1", "TLSv1.2", false),45TLS_CV_06("TLSv1.2", "TLSv1.2", "TLSv1.2", false),46TLS_CV_07("Default", "TLSv1", "TLSv1.2", true);4748final String contextVersion;49final String defaultProtocolVersion;50final String supportedProtocolVersion;51final boolean impacted;5253ContextVersion(String contextVersion, String defaultProtocolVersion,54String supportedProtocolVersion, boolean impacted) {55this.contextVersion = contextVersion;56this.defaultProtocolVersion = defaultProtocolVersion;57this.supportedProtocolVersion = supportedProtocolVersion;58this.impacted = impacted;59}60}6162public static void main(String[] args) throws Exception {63// Re-enable TLSv1 and TLSv1.1 since test depends on them.64SecurityUtils.removeFromDisabledTlsAlgs("TLSv1", "TLSv1.1");6566for (ContextVersion cv : ContextVersion.values()) {67System.out.println("Checking SSLContext of " + cv.contextVersion);6869SSLContext context;70try {71context = SSLContext.getInstance(cv.contextVersion);72if (cv.impacted) {73throw new Exception(74"illegal system property jdk.tls.client.protocols: " +75System.getProperty("jdk.tls.client.protocols"));76}77} catch (NoSuchAlgorithmException nsae) {78if (cv.impacted) {79System.out.println(80"\tIgnore: illegal system property " +81"jdk.tls.client.protocols=" +82System.getProperty("jdk.tls.client.protocols"));83continue;84} else {85throw nsae;86}87}8889// Default SSLContext is initialized automatically.90if (!cv.contextVersion.equals("Default")) {91// Use default TK, KM and random.92context.init((KeyManager[])null, (TrustManager[])null, null);93}9495SSLParameters parameters = context.getDefaultSSLParameters();9697String[] protocols = parameters.getProtocols();98String[] ciphers = parameters.getCipherSuites();99100if (protocols.length == 0 || ciphers.length == 0) {101throw new Exception("No default protocols or cipher suites");102}103104boolean isMatch = false;105for (String protocol : protocols) {106System.out.println("\tdefault protocol version " + protocol);107if (protocol.equals(cv.defaultProtocolVersion)) {108isMatch = true;109break;110}111}112113if (!isMatch) {114throw new Exception("No matched default protocol");115}116117parameters = context.getSupportedSSLParameters();118119protocols = parameters.getProtocols();120ciphers = parameters.getCipherSuites();121122if (protocols.length == 0 || ciphers.length == 0) {123throw new Exception("No supported protocols or cipher suites");124}125126isMatch = false;127for (String protocol : protocols) {128System.out.println("\tsupported protocol version " + protocol);129if (protocol.equals(cv.supportedProtocolVersion)) {130isMatch = true;131break;132}133}134135if (!isMatch) {136throw new Exception("No matched supported protocol");137}138System.out.println("\t... Success");139}140}141}142143144