Path: blob/master/test/jdk/sun/security/ssl/SSLContextImpl/DefaultCipherSuitePreference.java
41152 views
/*1* Copyright (c) 2019, 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 816826129* @summary Use server cipher suites preference by default30* @run main/othervm DefaultCipherSuitePreference31*/3233import javax.net.SocketFactory;34import javax.net.ssl.KeyManager;35import javax.net.ssl.SSLContext;36import javax.net.ssl.SSLEngine;37import javax.net.ssl.SSLParameters;38import javax.net.ssl.SSLServerSocket;39import javax.net.ssl.SSLServerSocketFactory;40import javax.net.ssl.SSLSocket;41import javax.net.ssl.TrustManager;4243public class DefaultCipherSuitePreference {44private static final String[] contextAlgorithms = {45"Default", "SSL", "TLS", "SSLv3", "TLSv1",46"TLSv1.1", "TLSv1.2", "TLSv1.3"47};4849public static void main(String[] args) throws Exception {50for (String algorithm : contextAlgorithms) {51System.out.println("Checking SSLContext of " + algorithm);52SSLContext sslContext = SSLContext.getInstance(algorithm);5354// Default SSLContext is initialized automatically.55if (!algorithm.equals("Default")) {56// Use default TK, KM and random.57sslContext.init((KeyManager[])null, (TrustManager[])null, null);58}5960//61// Check SSLContext62//63// Check default SSLParameters of SSLContext64checkDefaultCipherSuitePreference(65sslContext.getDefaultSSLParameters(),66"SSLContext.getDefaultSSLParameters()");6768// Check supported SSLParameters of SSLContext69checkDefaultCipherSuitePreference(70sslContext.getSupportedSSLParameters(),71"SSLContext.getSupportedSSLParameters()");7273//74// Check SSLEngine75//76// Check SSLParameters of SSLEngine77SSLEngine engine = sslContext.createSSLEngine();78engine.setUseClientMode(true);79checkDefaultCipherSuitePreference(80engine.getSSLParameters(),81"client mode SSLEngine.getSSLParameters()");8283engine.setUseClientMode(false);84checkDefaultCipherSuitePreference(85engine.getSSLParameters(),86"server mode SSLEngine.getSSLParameters()");8788//89// Check SSLSocket90//91// Check SSLParameters of SSLSocket92SocketFactory fac = sslContext.getSocketFactory();93SSLSocket socket = (SSLSocket)fac.createSocket();94checkDefaultCipherSuitePreference(95socket.getSSLParameters(),96"SSLSocket.getSSLParameters()");9798//99// Check SSLServerSocket100//101// Check SSLParameters of SSLServerSocket102SSLServerSocketFactory sf = sslContext.getServerSocketFactory();103SSLServerSocket ssocket = (SSLServerSocket)sf.createServerSocket();104checkDefaultCipherSuitePreference(105ssocket.getSSLParameters(),106"SSLServerSocket.getSSLParameters()");107}108}109110private static void checkDefaultCipherSuitePreference(111SSLParameters parameters, String context) throws Exception {112if (!parameters.getUseCipherSuitesOrder()) {113throw new Exception(114"The local cipher suite preference is not honored " +115"in the connection populated SSLParameters object (" +116context + ")");117}118}119}120121122