Path: blob/master/test/jdk/sun/security/ssl/SSLContextImpl/CustomizedDTLSServerDefaultProtocols.java
41152 views
/*1* Copyright (c) 2018, 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 823747429* @summary Test jdk.tls.server.protocols with DTLS30* @run main/othervm -Djdk.tls.server.protocols="DTLSv1.0"31* CustomizedDTLSServerDefaultProtocols32*/3334import java.lang.UnsupportedOperationException;35import java.security.NoSuchAlgorithmException;36import java.security.Security;37import java.util.Arrays;38import java.util.HashSet;39import java.util.Set;4041import javax.net.SocketFactory;42import javax.net.ssl.SSLContext;43import javax.net.ssl.SSLEngine;44import javax.net.ssl.SSLParameters;45import javax.net.ssl.SSLServerSocket;46import javax.net.ssl.SSLServerSocketFactory;47import javax.net.ssl.SSLSocket;4849public class CustomizedDTLSServerDefaultProtocols {5051final static String[] supportedProtocols = new String[]{52"DTLSv1.0", "DTLSv1.2"};5354enum ContextVersion {55TLS_CV_01("DTLS",56new String[]{"DTLSv1.0"},57supportedProtocols),58TLS_CV_02("DTLSv1.0",59supportedProtocols,60new String[]{"DTLSv1.0"}),61TLS_CV_03("DTLS1.2",62supportedProtocols,63supportedProtocols);6465final String contextVersion;66final String[] serverEnabledProtocols;67final String[] clientEnabledProtocols;6869ContextVersion(String contextVersion, String[] serverEnabledProtocols,70String[] clientEnabledProtocols) {71this.contextVersion = contextVersion;72this.serverEnabledProtocols = serverEnabledProtocols;73this.clientEnabledProtocols = clientEnabledProtocols;74}75}7677private static boolean checkProtocols(String[] target, String[] expected) {78boolean success = true;79if (target.length == 0) {80System.out.println("\tError: No protocols");81success = false;82}8384if (!protocolEquals(target, expected)) {85System.out.println("\tError: Expected to get protocols " +86Arrays.toString(expected));87success = false;88}89System.out.println("\t Protocols found " + Arrays.toString(target));90return success;91}9293private static boolean protocolEquals(94String[] actualProtocols,95String[] expectedProtocols) {96if (actualProtocols.length != expectedProtocols.length) {97return false;98}99100Set<String> set = new HashSet<>(Arrays.asList(expectedProtocols));101for (String actual : actualProtocols) {102if (set.add(actual)) {103return false;104}105}106107return true;108}109110private static boolean checkCipherSuites(String[] target) {111boolean success = true;112if (target.length == 0) {113System.out.println("\tError: No cipher suites");114success = false;115}116117return success;118}119120public static void main(String[] args) throws Exception {121// reset the security property to make sure that the algorithms122// and keys used in this test are not disabled.123Security.setProperty("jdk.tls.disabledAlgorithms", "");124System.out.println("jdk.tls.client.protocols = " +125System.getProperty("jdk.tls.client.protocols"));126System.out.println("jdk.tls.server.protocols = "+127System.getProperty("jdk.tls.server.protocols"));128Test();129}130131static void Test() throws Exception {132boolean failed = false;133134SSLContext context;135for (ContextVersion cv : ContextVersion.values()) {136System.out.println("Checking SSLContext of " + cv.contextVersion);137try {138context = SSLContext.getInstance(cv.contextVersion);139} catch (NoSuchAlgorithmException e) {140if (cv.contextVersion.compareToIgnoreCase("DTLS1.2") == 0) {141System.out.println("Exception expected: " + e.getMessage());142continue;143}144throw e;145}146// Default SSLContext is initialized automatically.147if (!cv.contextVersion.equals("Default")) {148// Use default TK, KM and random.149context.init(null, null, null);150}151152//153// Check SSLContext154//155// Check default SSLParameters of SSLContext156System.out.println("\tChecking default SSLParameters");157SSLParameters parameters = context.getDefaultSSLParameters();158159String[] protocols = parameters.getProtocols();160failed |= !checkProtocols(protocols, cv.clientEnabledProtocols);161162String[] ciphers = parameters.getCipherSuites();163failed |= !checkCipherSuites(ciphers);164165// Check supported SSLParameters of SSLContext166System.out.println("\tChecking supported SSLParameters");167parameters = context.getSupportedSSLParameters();168169protocols = parameters.getProtocols();170failed |= !checkProtocols(protocols, supportedProtocols);171172ciphers = parameters.getCipherSuites();173failed |= !checkCipherSuites(ciphers);174175//176// Check SSLEngine177//178// Check SSLParameters of SSLEngine179System.out.println();180System.out.println("\tChecking SSLEngine of this SSLContext");181System.out.println("\tChecking SSLEngine.getSSLParameters()");182SSLEngine engine = context.createSSLEngine();183engine.setUseClientMode(true);184parameters = engine.getSSLParameters();185186protocols = parameters.getProtocols();187failed |= !checkProtocols(protocols, cv.clientEnabledProtocols);188189ciphers = parameters.getCipherSuites();190failed |= !checkCipherSuites(ciphers);191192System.out.println("\tChecking SSLEngine.getEnabledProtocols()");193protocols = engine.getEnabledProtocols();194failed |= !checkProtocols(protocols, cv.clientEnabledProtocols);195196System.out.println("\tChecking SSLEngine.getEnabledCipherSuites()");197ciphers = engine.getEnabledCipherSuites();198failed |= !checkCipherSuites(ciphers);199200System.out.println("\tChecking SSLEngine.getSupportedProtocols()");201protocols = engine.getSupportedProtocols();202failed |= !checkProtocols(protocols, supportedProtocols);203204System.out.println(205"\tChecking SSLEngine.getSupportedCipherSuites()");206ciphers = engine.getSupportedCipherSuites();207failed |= !checkCipherSuites(ciphers);208209//210// Check SSLSocket211//212// Check SSLParameters of SSLSocket213System.out.println();214System.out.println("\tChecking SSLSocket of this SSLContext");215try {216context.getSocketFactory();217failed = true;218System.out.println("SSLSocket returned a socket for DTLS");219} catch (UnsupportedOperationException e) {220System.out.println("\t " + e.getMessage());221}222223//224// Check SSLServerSocket225//226// Check SSLParameters of SSLServerSocket227System.out.println();228System.out.println("\tChecking SSLServerSocket of this SSLContext");229try {230context.getServerSocketFactory();231failed = true;232System.out.println("SSLServerSocket returned a socket for DTLS");233} catch (UnsupportedOperationException e) {234System.out.println("\t " + e.getMessage());235}236237if (failed) {238throw new Exception("Run into problems, see log for more details");239} else {240System.out.println("\t... Success");241}242}243}244}245246247