Path: blob/master/test/jdk/sun/security/ssl/SSLContextImpl/IllegalProtocolProperty.java
41152 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 7093640 823472529* @summary Enable TLS 1.1 and TLS 1.2 by default in client side of SunJSSE30* @library /test/lib31* @run main/othervm -Djdk.tls.client.protocols="XSLv3,TLSv1"32* IllegalProtocolProperty33*/3435import javax.net.ssl.*;36import java.security.NoSuchAlgorithmException;3738import jdk.test.lib.security.SecurityUtils;3940public class IllegalProtocolProperty {41static enum ContextVersion {42TLS_CV_01("SSL", "TLSv1", "TLSv1.2", true),43TLS_CV_02("TLS", "TLSv1", "TLSv1.2", true),44TLS_CV_03("SSLv3", "TLSv1", "TLSv1.2", false),45TLS_CV_04("TLSv1", "TLSv1", "TLSv1.2", false),46TLS_CV_05("TLSv1.1", "TLSv1.1", "TLSv1.2", false),47TLS_CV_06("TLSv1.2", "TLSv1.2", "TLSv1.2", false),48TLS_CV_07("Default", "TLSv1", "TLSv1.2", true),49TLS_CV_08("TLSv1.3", "TLSv1.3", "TLSv1.3", false);5051final String contextVersion;52final String defaultProtocolVersion;53final String supportedProtocolVersion;54final boolean impacted;5556ContextVersion(String contextVersion, String defaultProtocolVersion,57String supportedProtocolVersion, boolean impacted) {58this.contextVersion = contextVersion;59this.defaultProtocolVersion = defaultProtocolVersion;60this.supportedProtocolVersion = supportedProtocolVersion;61this.impacted = impacted;62}63}6465public static void main(String[] args) throws Exception {66// Re-enable TLSv1 and TLSv1.1 since test depends on them.67SecurityUtils.removeFromDisabledTlsAlgs("TLSv1", "TLSv1.1");6869for (ContextVersion cv : ContextVersion.values()) {70System.out.println("Checking SSLContext of " + cv.contextVersion);7172SSLContext context;73try {74context = SSLContext.getInstance(cv.contextVersion);75if (cv.impacted) {76throw new Exception(77"illegal system property jdk.tls.client.protocols: " +78System.getProperty("jdk.tls.client.protocols"));79}80} catch (NoSuchAlgorithmException nsae) {81if (cv.impacted) {82System.out.println(83"\tIgnore: illegal system property " +84"jdk.tls.client.protocols=" +85System.getProperty("jdk.tls.client.protocols"));86continue;87} else {88throw nsae;89}90}9192// Default SSLContext is initialized automatically.93if (!cv.contextVersion.equals("Default")) {94// Use default TK, KM and random.95context.init((KeyManager[])null, (TrustManager[])null, null);96}9798SSLParameters parameters = context.getDefaultSSLParameters();99100String[] protocols = parameters.getProtocols();101String[] ciphers = parameters.getCipherSuites();102103if (protocols.length == 0 || ciphers.length == 0) {104throw new Exception("No default protocols or cipher suites");105}106107boolean isMatch = false;108for (String protocol : protocols) {109System.out.println("\tdefault protocol version " + protocol);110if (protocol.equals(cv.defaultProtocolVersion)) {111isMatch = true;112break;113}114}115116if (!isMatch) {117throw new Exception("No matched default protocol");118}119120parameters = context.getSupportedSSLParameters();121122protocols = parameters.getProtocols();123ciphers = parameters.getCipherSuites();124125if (protocols.length == 0 || ciphers.length == 0) {126throw new Exception("No supported protocols or cipher suites");127}128129isMatch = false;130for (String protocol : protocols) {131System.out.println("\tsupported protocol version " + protocol);132if (protocol.equals(cv.supportedProtocolVersion)) {133isMatch = true;134break;135}136}137138if (!isMatch) {139throw new Exception("No matched supported protocol");140}141System.out.println("\t... Success");142}143}144}145146147