Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/net/ssl/sanity/ciphersuites/CipherSuitesInOrder.java
38861 views
/*1* Copyright (c) 2012, 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//24// SunJSSE does not support dynamic system properties, no way to re-use25// system properties in samevm/agentvm mode.26//2728/*29* @test30* @bug 7174244 823472831* @summary Test for ciphersuites order32* @run main/othervm CipherSuitesInOrder33*/3435import java.util.*;36import javax.net.ssl.*;3738public class CipherSuitesInOrder {3940// Supported ciphersuites41private final static List<String> supportedCipherSuites42= Arrays.<String>asList(43"TLS_AES_128_GCM_SHA256",44"TLS_AES_256_GCM_SHA384",45"TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384",46"TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256",47"TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384",48"TLS_RSA_WITH_AES_256_GCM_SHA384",49"TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384",50"TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384",51"TLS_DHE_RSA_WITH_AES_256_GCM_SHA384",52"TLS_DHE_DSS_WITH_AES_256_GCM_SHA384",53"TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",54"TLS_RSA_WITH_AES_128_GCM_SHA256",55"TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256",56"TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256",57"TLS_DHE_RSA_WITH_AES_128_GCM_SHA256",58"TLS_DHE_DSS_WITH_AES_128_GCM_SHA256",5960"TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384",61"TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384",62"TLS_RSA_WITH_AES_256_CBC_SHA256",63"TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384",64"TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384",65"TLS_DHE_RSA_WITH_AES_256_CBC_SHA256",66"TLS_DHE_DSS_WITH_AES_256_CBC_SHA256",67"TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA",68"TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA",69"TLS_RSA_WITH_AES_256_CBC_SHA",70"TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA",71"TLS_ECDH_RSA_WITH_AES_256_CBC_SHA",72"TLS_DHE_RSA_WITH_AES_256_CBC_SHA",73"TLS_DHE_DSS_WITH_AES_256_CBC_SHA",74"TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256",75"TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256",76"TLS_RSA_WITH_AES_128_CBC_SHA256",77"TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256",78"TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256",79"TLS_DHE_RSA_WITH_AES_128_CBC_SHA256",80"TLS_DHE_DSS_WITH_AES_128_CBC_SHA256",81"TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA",82"TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA",83"TLS_RSA_WITH_AES_128_CBC_SHA",84"TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA",85"TLS_ECDH_RSA_WITH_AES_128_CBC_SHA",86"TLS_DHE_RSA_WITH_AES_128_CBC_SHA",87"TLS_DHE_DSS_WITH_AES_128_CBC_SHA",8889"TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA",90"TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA",91"SSL_RSA_WITH_3DES_EDE_CBC_SHA",92"TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA",93"TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA",94"SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA",95"SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA",9697"TLS_EMPTY_RENEGOTIATION_INFO_SCSV",9899"TLS_DH_anon_WITH_AES_256_GCM_SHA384",100"TLS_DH_anon_WITH_AES_128_GCM_SHA256",101102"TLS_DH_anon_WITH_AES_256_CBC_SHA256",103"TLS_ECDH_anon_WITH_AES_256_CBC_SHA",104"TLS_DH_anon_WITH_AES_256_CBC_SHA",105"TLS_DH_anon_WITH_AES_128_CBC_SHA256",106"TLS_ECDH_anon_WITH_AES_128_CBC_SHA",107"TLS_DH_anon_WITH_AES_128_CBC_SHA",108"TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA",109"SSL_DH_anon_WITH_3DES_EDE_CBC_SHA",110111"TLS_ECDHE_ECDSA_WITH_RC4_128_SHA",112"TLS_ECDHE_RSA_WITH_RC4_128_SHA",113"SSL_RSA_WITH_RC4_128_SHA",114"TLS_ECDH_ECDSA_WITH_RC4_128_SHA",115"TLS_ECDH_RSA_WITH_RC4_128_SHA",116"SSL_RSA_WITH_RC4_128_MD5",117"TLS_ECDH_anon_WITH_RC4_128_SHA",118"SSL_DH_anon_WITH_RC4_128_MD5",119120"SSL_RSA_WITH_DES_CBC_SHA",121"SSL_DHE_RSA_WITH_DES_CBC_SHA",122"SSL_DHE_DSS_WITH_DES_CBC_SHA",123"SSL_DH_anon_WITH_DES_CBC_SHA",124"SSL_RSA_EXPORT_WITH_DES40_CBC_SHA",125"SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA",126"SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA",127"SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA",128129"SSL_RSA_EXPORT_WITH_RC4_40_MD5",130"SSL_DH_anon_EXPORT_WITH_RC4_40_MD5",131132"TLS_RSA_WITH_NULL_SHA256",133"TLS_ECDHE_ECDSA_WITH_NULL_SHA",134"TLS_ECDHE_RSA_WITH_NULL_SHA",135"SSL_RSA_WITH_NULL_SHA",136"TLS_ECDH_ECDSA_WITH_NULL_SHA",137"TLS_ECDH_RSA_WITH_NULL_SHA",138"TLS_ECDH_anon_WITH_NULL_SHA",139"SSL_RSA_WITH_NULL_MD5",140141"TLS_KRB5_WITH_3DES_EDE_CBC_SHA",142"TLS_KRB5_WITH_3DES_EDE_CBC_MD5",143"TLS_KRB5_WITH_RC4_128_SHA",144"TLS_KRB5_WITH_RC4_128_MD5",145"TLS_KRB5_WITH_DES_CBC_SHA",146"TLS_KRB5_WITH_DES_CBC_MD5",147"TLS_KRB5_EXPORT_WITH_DES_CBC_40_SHA",148"TLS_KRB5_EXPORT_WITH_DES_CBC_40_MD5",149"TLS_KRB5_EXPORT_WITH_RC4_40_SHA",150"TLS_KRB5_EXPORT_WITH_RC4_40_MD5"151);152153private final static String[] protocols = {154"", "SSL", "TLS", "SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2", "TLSv1.3"155};156157158public static void main(String[] args) throws Exception {159// show all of the supported cipher suites160showSuites(supportedCipherSuites.toArray(new String[0]),161"All supported cipher suites");162163for (String protocol : protocols) {164System.out.println("//");165System.out.println("// "166+ "Testing for SSLContext of " + protocol);167System.out.println("//");168checkForProtocols(protocol);169}170}171172public static void checkForProtocols(String protocol) throws Exception {173SSLContext context;174if (protocol.isEmpty()) {175context = SSLContext.getDefault();176} else {177context = SSLContext.getInstance(protocol);178context.init(null, null, null);179}180181// check the order of default cipher suites of SSLContext182SSLParameters parameters = context.getDefaultSSLParameters();183checkSuites(parameters.getCipherSuites(),184"Default cipher suites in SSLContext");185186// check the order of supported cipher suites of SSLContext187parameters = context.getSupportedSSLParameters();188checkSuites(parameters.getCipherSuites(),189"Supported cipher suites in SSLContext");190191//192// Check the cipher suites order of SSLEngine193//194SSLEngine engine = context.createSSLEngine();195196// check the order of endabled cipher suites197String[] ciphers = engine.getEnabledCipherSuites();198checkSuites(ciphers,199"Enabled cipher suites in SSLEngine");200201// check the order of supported cipher suites202ciphers = engine.getSupportedCipherSuites();203checkSuites(ciphers,204"Supported cipher suites in SSLEngine");205206//207// Check the cipher suites order of SSLSocket208//209SSLSocketFactory factory = context.getSocketFactory();210try (SSLSocket socket = (SSLSocket) factory.createSocket()) {211212// check the order of endabled cipher suites213ciphers = socket.getEnabledCipherSuites();214checkSuites(ciphers,215"Enabled cipher suites in SSLSocket");216217// check the order of supported cipher suites218ciphers = socket.getSupportedCipherSuites();219checkSuites(ciphers,220"Supported cipher suites in SSLSocket");221}222223//224// Check the cipher suites order of SSLServerSocket225//226SSLServerSocketFactory serverFactory = context.getServerSocketFactory();227try (SSLServerSocket serverSocket228= (SSLServerSocket) serverFactory.createServerSocket()) {229// check the order of endabled cipher suites230ciphers = serverSocket.getEnabledCipherSuites();231checkSuites(ciphers,232"Enabled cipher suites in SSLServerSocket");233234// check the order of supported cipher suites235ciphers = serverSocket.getSupportedCipherSuites();236checkSuites(ciphers,237"Supported cipher suites in SSLServerSocket");238}239}240241private static void checkSuites(String[] suites, String title) {242showSuites(suites, title);243244int loc = -1;245int index = 0;246for (String suite : suites) {247index = supportedCipherSuites.indexOf(suite);248if (index <= loc) {249throw new RuntimeException(suite + " is not in order");250}251loc = index;252}253}254255private static void showSuites(String[] suites, String title) {256System.out.println(title + "[" + suites.length + "]:");257for (String suite : suites) {258System.out.println(" " + suite);259}260}261}262263264