Path: blob/master/test/jdk/sun/security/ssl/SSLContextImpl/CustomizedCipherSuites.java
41152 views
/*1* Copyright (c) 2016, 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 816236229* @summary Cannot enable previously default enabled cipher suites30* @run main/othervm31* CustomizedCipherSuites Default true32* TLS_RSA_WITH_AES_128_CBC_SHA33* TLS_ECDH_anon_WITH_AES_128_CBC_SHA34* @run main/othervm35* -Djdk.tls.client.cipherSuites="unknown"36* CustomizedCipherSuites Default true37* TLS_RSA_WITH_AES_128_CBC_SHA38* TLS_ECDH_anon_WITH_AES_128_CBC_SHA39* @run main/othervm40* -Djdk.tls.client.cipherSuites=""41* CustomizedCipherSuites Default true42* TLS_RSA_WITH_AES_128_CBC_SHA43* TLS_ECDH_anon_WITH_AES_128_CBC_SHA44* @run main/othervm45* -Djdk.tls.client.cipherSuites="TLS_ECDH_anon_WITH_AES_128_CBC_SHA"46* CustomizedCipherSuites Default true47* TLS_ECDH_anon_WITH_AES_128_CBC_SHA48* TLS_RSA_WITH_AES_128_CBC_SHA49* @run main/othervm50* -Djdk.tls.server.cipherSuites="TLS_ECDH_anon_WITH_AES_128_CBC_SHA"51* CustomizedCipherSuites Default false52* TLS_ECDH_anon_WITH_AES_128_CBC_SHA53* TLS_RSA_WITH_AES_128_CBC_SHA54* @run main/othervm55* -Djdk.tls.client.cipherSuites="TLS_RSA_WITH_AES_128_CBC_SHA,unknown,TLS_ECDH_anon_WITH_AES_128_CBC_SHA"56* CustomizedCipherSuites Default true57* TLS_ECDH_anon_WITH_AES_128_CBC_SHA58* ""59* @run main/othervm60* -Djdk.tls.server.cipherSuites="TLS_RSA_WITH_AES_128_CBC_SHA,unknown,TLS_ECDH_anon_WITH_AES_128_CBC_SHA"61* CustomizedCipherSuites Default false62* TLS_RSA_WITH_AES_128_CBC_SHA63* ""64* @run main/othervm65* -Djdk.tls.server.cipherSuites="TLS_ECDH_anon_WITH_AES_128_CBC_SHA"66* CustomizedCipherSuites Default true67* TLS_RSA_WITH_AES_128_CBC_SHA68* TLS_ECDH_anon_WITH_AES_128_CBC_SHA69* @run main/othervm70* -Djdk.tls.client.cipherSuites="TLS_ECDH_anon_WITH_AES_128_CBC_SHA"71* CustomizedCipherSuites Default false72* TLS_RSA_WITH_AES_128_CBC_SHA73* TLS_ECDH_anon_WITH_AES_128_CBC_SHA74*/7576import java.security.Security;77import javax.net.ssl.*;7879/**80* Test the customized default cipher suites.81*82* This test is based on the behavior that TLS_ECDH_anon_WITH_AES_128_CBC_SHA is83* disabled by default, and TLS_RSA_WITH_AES_128_CBC_SHA is enabled by84* default in JDK. If the behavior is changed in the future, please85* update the test cases above accordingly.86*/87public class CustomizedCipherSuites {8889private static String contextProtocol;90private static boolean isClientMode;9192private static String enabledCipherSuite;93private static String notEnabledCipherSuite;9495public static void main(String[] args) throws Exception {9697// reset the security property to make sure the cipher suites98// used in this test are not disabled99Security.setProperty("jdk.tls.disabledAlgorithms", "");100101contextProtocol = trimQuotes(args[0]);102isClientMode = Boolean.parseBoolean(args[1]);103enabledCipherSuite = trimQuotes(args[2]);104notEnabledCipherSuite = trimQuotes(args[3]);105106//107// Create instance of SSLContext with the specified protocol.108//109SSLContext context = SSLContext.getInstance(contextProtocol);110111// Default SSLContext is initialized automatically.112if (!contextProtocol.equals("Default")) {113// Use default TK, KM and random.114context.init((KeyManager[])null, (TrustManager[])null, null);115}116117// SSLContext default parameters is client mode in JDK.118if (isClientMode) {119//120// Check default parameters of the specified SSLContext protocol121//122SSLParameters parameters = context.getDefaultSSLParameters();123System.out.println("Checking SSLContext default parameters ...");124checkEnabledCiphers(parameters.getCipherSuites());125}126127//128// Check supported parameters of the specified SSLContext protocol129//130SSLParameters parameters = context.getSupportedSSLParameters();131System.out.println("Checking SSLContext suppport parameters ...");132checkSupportedCiphers(parameters.getCipherSuites());133134135//136// Check the default cipher suites of SSLEngine.137//138SSLEngine engine = context.createSSLEngine();139engine.setUseClientMode(isClientMode);140141System.out.println("Checking SSLEngine default cipher suites ...");142checkEnabledCiphers(engine.getEnabledCipherSuites());143144//145// Check the supported cipher suites of SSLEngine.146//147System.out.println("Checking SSLEngine supported cipher suites ...");148checkSupportedCiphers(engine.getSupportedCipherSuites());149150if (isClientMode) {151SSLSocketFactory factory = context.getSocketFactory();152// Use an unconnected socket.153try (SSLSocket socket = (SSLSocket)factory.createSocket()) {154//155// Check the default cipher suites of SSLSocket.156//157System.out.println(158"Checking SSLSocket default cipher suites ...");159checkEnabledCiphers(socket.getEnabledCipherSuites());160161//162// Check the supported cipher suites of SSLSocket.163//164System.out.println(165"Checking SSLSocket supported cipher suites ...");166checkSupportedCiphers(socket.getSupportedCipherSuites());167}168} else {169SSLServerSocketFactory factory = context.getServerSocketFactory();170// Use an unbound server socket.171try (SSLServerSocket socket =172(SSLServerSocket)factory.createServerSocket()) {173//174// Check the default cipher suites of SSLServerSocket.175//176System.out.println(177"Checking SSLServerSocket default cipher suites ...");178checkEnabledCiphers(socket.getEnabledCipherSuites());179180//181// Check the supported cipher suites of SSLServerSocket.182//183System.out.println(184"Checking SSLServerSocket supported cipher suites ...");185checkSupportedCiphers(socket.getSupportedCipherSuites());186}187}188189System.out.println("\t... Success");190}191192private static void checkEnabledCiphers(193String[] ciphers) throws Exception {194195if (ciphers.length == 0) {196throw new Exception("No default cipher suites");197}198199boolean isMatch = false;200if (enabledCipherSuite.isEmpty()) {201// Don't check if not specify the expected cipher suite.202isMatch = true;203}204205boolean isBroken = false;206for (String cipher : ciphers) {207System.out.println("\tdefault cipher suite " + cipher);208if (!enabledCipherSuite.isEmpty() &&209cipher.equals(enabledCipherSuite)) {210isMatch = true;211}212213if (!notEnabledCipherSuite.isEmpty() &&214cipher.equals(notEnabledCipherSuite)) {215isBroken = true;216}217}218219if (!isMatch) {220throw new Exception(221"Cipher suite " + enabledCipherSuite + " should be enabled");222}223224if (isBroken) {225throw new Exception(226"Cipher suite " + notEnabledCipherSuite + " should not be enabled");227}228}229230private static void checkSupportedCiphers(231String[] ciphers) throws Exception {232233if (ciphers.length == 0) {234throw new Exception("No supported cipher suites");235}236237boolean hasEnabledCipherSuite = enabledCipherSuite.isEmpty();238boolean hasNotEnabledCipherSuite = notEnabledCipherSuite.isEmpty();239for (String cipher : ciphers) {240System.out.println("\tsupported cipher suite " + cipher);241if (!enabledCipherSuite.isEmpty() &&242cipher.equals(enabledCipherSuite)) {243hasEnabledCipherSuite = true;244}245246if (!notEnabledCipherSuite.isEmpty() &&247cipher.equals(notEnabledCipherSuite)) {248hasNotEnabledCipherSuite = true;249}250}251252if (!hasEnabledCipherSuite) {253throw new Exception(254"Cipher suite " + enabledCipherSuite + " should be supported");255}256257if (!hasNotEnabledCipherSuite) {258throw new Exception(259"Cipher suite " + notEnabledCipherSuite + " should not be enabled");260}261}262263private static String trimQuotes(String candidate) {264if (candidate != null && candidate.length() != 0) {265// Remove double quote marks from beginning/end of the string.266if (candidate.length() > 1 && candidate.charAt(0) == '"' &&267candidate.charAt(candidate.length() - 1) == '"') {268return candidate.substring(1, candidate.length() - 1);269}270}271272return candidate;273}274}275276277