Path: blob/master/test/jdk/sun/security/ssl/SSLContextImpl/SSLContextDefault.java
41152 views
/*1* Copyright (c) 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//24// SunJSSE does not support dynamic system properties, no way to re-use25// system properties in samevm/agentvm mode.26//2728/*29* @test30* @bug 820234331* @summary Check that SSLv3, TLSv1 and TLSv1.1 are disabled by default32* @run main/othervm SSLContextDefault33*/3435import java.util.List;36import javax.net.ssl.*;3738public class SSLContextDefault {3940private final static String[] protocols = {41"", "SSL", "TLS", "SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2", "TLSv1.3"42};4344private final static List<String> disabledProtocols = List.<String>of(45"SSLv3", "TLSv1", "TLSv1.1"46);4748public static void main(String[] args) throws Exception {49for (String protocol : protocols) {50System.out.println("//");51System.out.println("// " + "Testing for SSLContext of " +52(protocol.isEmpty() ? "<default>" : protocol));53System.out.println("//");54checkForProtocols(protocol);55System.out.println();56}57}5859public static void checkForProtocols(String protocol) throws Exception {60SSLContext context;61if (protocol.isEmpty()) {62context = SSLContext.getDefault();63} else {64context = SSLContext.getInstance(protocol);65context.init(null, null, null);66}6768// check for the presence of supported protocols of SSLContext69SSLParameters parameters = context.getSupportedSSLParameters();70checkProtocols(parameters.getProtocols(),71"Supported protocols in SSLContext", false);727374// check for the presence of default protocols of SSLContext75parameters = context.getDefaultSSLParameters();76checkProtocols(parameters.getProtocols(),77"Enabled protocols in SSLContext", true);7879// check for the presence of supported protocols of SSLEngine80SSLEngine engine = context.createSSLEngine();81checkProtocols(engine.getSupportedProtocols(),82"Supported protocols in SSLEngine", false);8384// Check for the presence of default protocols of SSLEngine85checkProtocols(engine.getEnabledProtocols(),86"Enabled protocols in SSLEngine", true);8788SSLSocketFactory factory = context.getSocketFactory();89try (SSLSocket socket = (SSLSocket)factory.createSocket()) {90// check for the presence of supported protocols of SSLSocket91checkProtocols(socket.getSupportedProtocols(),92"Supported cipher suites in SSLSocket", false);9394// Check for the presence of default protocols of SSLSocket95checkProtocols(socket.getEnabledProtocols(),96"Enabled protocols in SSLSocket", true);97}9899SSLServerSocketFactory serverFactory = context.getServerSocketFactory();100try (SSLServerSocket serverSocket =101(SSLServerSocket)serverFactory.createServerSocket()) {102// check for the presence of supported protocols of SSLServerSocket103checkProtocols(serverSocket.getSupportedProtocols(),104"Supported cipher suites in SSLServerSocket", false);105106// Check for the presence of default protocols of SSLServerSocket107checkProtocols(serverSocket.getEnabledProtocols(),108"Enabled protocols in SSLServerSocket", true);109}110}111112private static void checkProtocols(String[] protocols,113String title, boolean disabled) throws Exception {114showProtocols(protocols, title);115116if (disabled) {117for (String protocol : protocols ) {118if (disabledProtocols.contains(protocol)) {119throw new Exception(protocol +120" should not be enabled by default");121}122}123} else {124for (String disabledProtocol : disabledProtocols) {125if (!List.of(protocols).contains(disabledProtocol)) {126throw new Exception(disabledProtocol +127" should be supported by default");128}129}130}131}132133private static void showProtocols(String[] protocols, String title) {134System.out.println(title + "[" + protocols.length + "]:");135for (String protocol : protocols) {136System.out.println(" " + protocol);137}138}139}140141142