Path: blob/master/test/jdk/javax/net/ssl/sanity/ciphersuites/TLSCipherSuitesOrder.java
41155 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;2526import jdk.test.lib.security.SecurityUtils;2728/*29* @test30* @bug 823472831* @library /javax/net/ssl/templates32* /javax/net/ssl/TLSCommon33* /test/lib34* @summary Test TLS ciphersuites order.35* Parameter order: <protocol> <client cipher order> <server cipher order>36* @run main/othervm TLSCipherSuitesOrder TLSv13 ORDERED default37* @run main/othervm TLSCipherSuitesOrder TLSv13 UNORDERED default38* @run main/othervm TLSCipherSuitesOrder TLSv13 UNORDERED UNORDERED39* @run main/othervm TLSCipherSuitesOrder TLSv13 ORDERED ORDERED40* @run main/othervm TLSCipherSuitesOrder TLSv12 ORDERED default41* @run main/othervm TLSCipherSuitesOrder TLSv12 UNORDERED default42* @run main/othervm TLSCipherSuitesOrder TLSv12 UNORDERED UNORDERED43* @run main/othervm TLSCipherSuitesOrder TLSv12 ORDERED ORDERED44* @run main/othervm TLSCipherSuitesOrder TLSv11 ORDERED default45* @run main/othervm TLSCipherSuitesOrder TLSv11 UNORDERED default46* @run main/othervm TLSCipherSuitesOrder TLSv11 UNORDERED UNORDERED47* @run main/othervm TLSCipherSuitesOrder TLSv11 ORDERED ORDERED48* @run main/othervm TLSCipherSuitesOrder TLSv1 ORDERED default49* @run main/othervm TLSCipherSuitesOrder TLSv1 UNORDERED default50* @run main/othervm TLSCipherSuitesOrder TLSv1 UNORDERED UNORDERED51* @run main/othervm TLSCipherSuitesOrder TLSv1 ORDERED ORDERED52*/53public class TLSCipherSuitesOrder extends SSLSocketTemplate {5455private final String protocol;56private final String[] servercipherSuites;57private final String[] clientcipherSuites;5859public static void main(String[] args) {60PROTOCOL protocol = PROTOCOL.valueOf(args[0]);61try {62new TLSCipherSuitesOrder(protocol.getProtocol(),63protocol.getCipherSuite(args[1]),64protocol.getCipherSuite(args[2])).run();65} catch (Exception e) {66throw new RuntimeException(e);67}68}6970private TLSCipherSuitesOrder(String protocol, String[] clientcipherSuites,71String[] servercipherSuites) {72// Re-enable protocol if it is disabled.73if (protocol.equals("TLSv1") || protocol.equals("TLSv1.1")) {74SecurityUtils.removeFromDisabledTlsAlgs(protocol);75}76this.protocol = protocol;77this.clientcipherSuites = clientcipherSuites;78this.servercipherSuites = servercipherSuites;79}8081// Servers are configured before clients, increment test case after.82@Override83protected void configureClientSocket(SSLSocket socket) {84socket.setEnabledProtocols(new String[]{protocol});85if (clientcipherSuites != null) {86socket.setEnabledCipherSuites(clientcipherSuites);87}88}8990@Override91protected void configureServerSocket(SSLServerSocket serverSocket) {92serverSocket.setEnabledProtocols(new String[]{protocol});93if (servercipherSuites != null) {94serverSocket.setEnabledCipherSuites(servercipherSuites);95}96}9798protected void runServerApplication(SSLSocket socket) throws Exception {99if (servercipherSuites != null) {100System.out.printf("SERVER: setEnabledCipherSuites:%s - "101+ "getEnabledCipherSuites:%s%n",102Arrays.deepToString(servercipherSuites),103Arrays.deepToString(socket.getEnabledCipherSuites()));104}105if (servercipherSuites != null && !Arrays.equals(servercipherSuites,106socket.getEnabledCipherSuites())) {107throw new RuntimeException("Unmatched server side CipherSuite order");108}109super.runServerApplication(socket);110}111112protected void runClientApplication(SSLSocket socket) throws Exception {113if (clientcipherSuites != null) {114System.out.printf("CLIENT: setEnabledCipherSuites:%s - "115+ "getEnabledCipherSuites:%s%n",116Arrays.deepToString(clientcipherSuites),117Arrays.deepToString(socket.getEnabledCipherSuites()));118}119if (clientcipherSuites != null && !Arrays.equals(120clientcipherSuites, socket.getEnabledCipherSuites())) {121throw new RuntimeException("Unmatched client side CipherSuite order");122}123super.runClientApplication(socket);124}125126enum PROTOCOL {127TLSv13("TLSv1.3",128new String[]{129"TLS_AES_256_GCM_SHA384",130"TLS_AES_128_GCM_SHA256",131"TLS_CHACHA20_POLY1305_SHA256"},132new String[]{"TLS_CHACHA20_POLY1305_SHA256",133"TLS_AES_128_GCM_SHA256",134"TLS_AES_256_GCM_SHA384"}),135TLSv12("TLSv1.2",136new String[]{137"TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384",138"TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256"},139new String[]{140"TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256",141"TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384"}),142TLSv11("TLSv1.1",143new String[]{144"TLS_RSA_WITH_AES_256_CBC_SHA",145"TLS_RSA_WITH_AES_128_CBC_SHA"},146new String[]{147"TLS_RSA_WITH_AES_128_CBC_SHA",148"TLS_RSA_WITH_AES_256_CBC_SHA"}),149TLSv1("TLSv1",150new String[]{151"TLS_RSA_WITH_AES_256_CBC_SHA",152"TLS_RSA_WITH_AES_128_CBC_SHA"},153new String[]{154"TLS_RSA_WITH_AES_128_CBC_SHA",155"TLS_RSA_WITH_AES_256_CBC_SHA"});156157String protocol;158String[] orderedCiphers;159String[] unOrderedCiphers;160161private PROTOCOL(String protocol, String[] orderedCiphers,162String[] unOrderedCiphers) {163this.protocol = protocol;164this.orderedCiphers = orderedCiphers;165this.unOrderedCiphers = unOrderedCiphers;166}167168public String getProtocol() {169return protocol;170}171172public String[] getOrderedCiphers() {173return orderedCiphers;174}175176public String[] getUnOrderedCiphers() {177return unOrderedCiphers;178}179180public String[] getCipherSuite(String order) {181switch (order) {182case "ORDERED":183return getOrderedCiphers();184case "UNORDERED":185return getUnOrderedCiphers();186default:187return null;188}189}190}191}192193194