Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/net/ssl/sanity/ciphersuites/TLSCipherSuitesOrder.java
38861 views
/*1* Copyright (c) 2019, 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*/22import java.util.Arrays;23import javax.net.ssl.SSLServerSocket;24import javax.net.ssl.SSLSocket;2526/*27* @test28* @bug 823472829* @library /javax/net/ssl/templates30* /javax/net/ssl/TLSCommon31* /lib/security32* @summary Test TLS ciphersuites order.33* Parameter order: <protocol> <client cipher order> <server cipher order>34* @run main/othervm TLSCipherSuitesOrder TLSv13 ORDERED default35* @run main/othervm TLSCipherSuitesOrder TLSv13 UNORDERED default36* @run main/othervm TLSCipherSuitesOrder TLSv13 UNORDERED UNORDERED37* @run main/othervm TLSCipherSuitesOrder TLSv13 ORDERED ORDERED38* @run main/othervm TLSCipherSuitesOrder TLSv12 ORDERED default39* @run main/othervm TLSCipherSuitesOrder TLSv12 UNORDERED default40* @run main/othervm TLSCipherSuitesOrder TLSv12 UNORDERED UNORDERED41* @run main/othervm TLSCipherSuitesOrder TLSv12 ORDERED ORDERED42* @run main/othervm TLSCipherSuitesOrder TLSv11 ORDERED default43* @run main/othervm TLSCipherSuitesOrder TLSv11 UNORDERED default44* @run main/othervm TLSCipherSuitesOrder TLSv11 UNORDERED UNORDERED45* @run main/othervm TLSCipherSuitesOrder TLSv11 ORDERED ORDERED46* @run main/othervm TLSCipherSuitesOrder TLSv1 ORDERED default47* @run main/othervm TLSCipherSuitesOrder TLSv1 UNORDERED default48* @run main/othervm TLSCipherSuitesOrder TLSv1 UNORDERED UNORDERED49* @run main/othervm TLSCipherSuitesOrder TLSv1 ORDERED ORDERED50*/51public class TLSCipherSuitesOrder extends SSLSocketTemplate {5253private final String protocol;54private final String[] servercipherSuites;55private final String[] clientcipherSuites;5657public static void main(String[] args) {58PROTOCOL protocol = PROTOCOL.valueOf(args[0]);59try {60new TLSCipherSuitesOrder(protocol.getProtocol(),61protocol.getCipherSuite(args[1]),62protocol.getCipherSuite(args[2])).run();63} catch (Exception e) {64throw new RuntimeException(e);65}66}6768private TLSCipherSuitesOrder(String protocol, String[] clientcipherSuites,69String[] servercipherSuites) {70// Re-enable protocol if it is disabled.71if (protocol.equals("TLSv1") || protocol.equals("TLSv1.1")) {72SecurityUtils.removeFromDisabledTlsAlgs(protocol);73}74this.protocol = protocol;75this.clientcipherSuites = clientcipherSuites;76this.servercipherSuites = servercipherSuites;77}7879// Servers are configured before clients, increment test case after.80@Override81protected void configureClientSocket(SSLSocket socket) {82socket.setEnabledProtocols(new String[]{protocol});83if (clientcipherSuites != null) {84socket.setEnabledCipherSuites(clientcipherSuites);85}86}8788@Override89protected void configureServerSocket(SSLServerSocket serverSocket) {90serverSocket.setEnabledProtocols(new String[]{protocol});91if (servercipherSuites != null) {92serverSocket.setEnabledCipherSuites(servercipherSuites);93}94}9596protected void runServerApplication(SSLSocket socket) throws Exception {97if (servercipherSuites != null) {98System.out.printf("SERVER: setEnabledCipherSuites:%s - "99+ "getEnabledCipherSuites:%s%n",100Arrays.deepToString(servercipherSuites),101Arrays.deepToString(socket.getEnabledCipherSuites()));102}103if (servercipherSuites != null && !Arrays.equals(servercipherSuites,104socket.getEnabledCipherSuites())) {105throw new RuntimeException("Unmatched server side CipherSuite order");106}107super.runServerApplication(socket);108}109110protected void runClientApplication(SSLSocket socket) throws Exception {111if (clientcipherSuites != null) {112System.out.printf("CLIENT: setEnabledCipherSuites:%s - "113+ "getEnabledCipherSuites:%s%n",114Arrays.deepToString(clientcipherSuites),115Arrays.deepToString(socket.getEnabledCipherSuites()));116}117if (clientcipherSuites != null && !Arrays.equals(118clientcipherSuites, socket.getEnabledCipherSuites())) {119throw new RuntimeException("Unmatched client side CipherSuite order");120}121super.runClientApplication(socket);122}123124enum PROTOCOL {125TLSv13("TLSv1.3",126new String[]{127"TLS_AES_256_GCM_SHA384",128"TLS_AES_128_GCM_SHA256"},129new String[]{130"TLS_AES_128_GCM_SHA256",131"TLS_AES_256_GCM_SHA384"}),132TLSv12("TLSv1.2",133new String[]{134"TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384",135"TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256"},136new String[]{137"TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256",138"TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384"}),139TLSv11("TLSv1.1",140new String[]{141"TLS_RSA_WITH_AES_256_CBC_SHA",142"TLS_RSA_WITH_AES_128_CBC_SHA"},143new String[]{144"TLS_RSA_WITH_AES_128_CBC_SHA",145"TLS_RSA_WITH_AES_256_CBC_SHA"}),146TLSv1("TLSv1",147new String[]{148"TLS_RSA_WITH_AES_256_CBC_SHA",149"TLS_RSA_WITH_AES_128_CBC_SHA"},150new String[]{151"TLS_RSA_WITH_AES_128_CBC_SHA",152"TLS_RSA_WITH_AES_256_CBC_SHA"});153154String protocol;155String[] orderedCiphers;156String[] unOrderedCiphers;157158private PROTOCOL(String protocol, String[] orderedCiphers,159String[] unOrderedCiphers) {160this.protocol = protocol;161this.orderedCiphers = orderedCiphers;162this.unOrderedCiphers = unOrderedCiphers;163}164165public String getProtocol() {166return protocol;167}168169public String[] getOrderedCiphers() {170return orderedCiphers;171}172173public String[] getUnOrderedCiphers() {174return unOrderedCiphers;175}176177public String[] getCipherSuite(String order) {178switch (order) {179case "ORDERED":180return getOrderedCiphers();181case "UNORDERED":182return getUnOrderedCiphers();183default:184return null;185}186}187}188}189190191