Path: blob/master/test/jdk/sun/security/ssl/CipherSuite/NamedGroupsWithCipherSuite.java
41152 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*/2223import javax.net.ssl.SSLContext;24import javax.net.ssl.SSLServerSocket;25import javax.net.ssl.SSLSocket;2627import jdk.test.lib.security.SecurityUtils;2829/*30* @test31* @bug 8224650 824292932* @library /javax/net/ssl/templates33* /javax/net/ssl/TLSCommon34* /test/lib35* @summary Test TLS ciphersuite with each individual supported group36* @run main/othervm NamedGroupsWithCipherSuite x2551937* @run main/othervm NamedGroupsWithCipherSuite X44838* @run main/othervm NamedGroupsWithCipherSuite secp256r139* @run main/othervm NamedGroupsWithCipherSuite secP384r140* @run main/othervm NamedGroupsWithCipherSuite SECP521R141* @run main/othervm NamedGroupsWithCipherSuite ffDhe204842* @run main/othervm NamedGroupsWithCipherSuite FFDHE307243* @run main/othervm NamedGroupsWithCipherSuite ffdhe409644* @run main/othervm NamedGroupsWithCipherSuite ffdhe614445* @run main/othervm NamedGroupsWithCipherSuite ffdhe819246*/47public class NamedGroupsWithCipherSuite extends SSLSocketTemplate {4849private static final Protocol[] PROTOCOLS = new Protocol[] {50Protocol.TLSV1_3,51Protocol.TLSV1_2,52Protocol.TLSV1_1,53Protocol.TLSV154};5556private static final CipherSuite[] CIPHER_SUITES = new CipherSuite[] {57CipherSuite.TLS_AES_128_GCM_SHA256,58CipherSuite.TLS_AES_256_GCM_SHA384,59CipherSuite.TLS_CHACHA20_POLY1305_SHA256,6061CipherSuite.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,62CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,63CipherSuite.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,64CipherSuite.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,6566CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,67CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,68CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384,69CipherSuite.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,7071CipherSuite.TLS_DHE_DSS_WITH_AES_128_CBC_SHA,72CipherSuite.TLS_DHE_DSS_WITH_AES_256_CBC_SHA256,7374CipherSuite.TLS_DHE_RSA_WITH_AES_128_CBC_SHA,75CipherSuite.TLS_DHE_RSA_WITH_AES_256_CBC_SHA256,76CipherSuite.TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA25677};7879private String protocol;80private String cipher;8182private SSLSocketTemplate.Cert[] trustedCerts = TRUSTED_CERTS;83private SSLSocketTemplate.Cert[] endEntityCerts = END_ENTITY_CERTS;8485NamedGroupsWithCipherSuite(86Protocol protocol,87CipherSuite cipher,88String namedGroup) {89this.protocol = protocol.name;90this.cipher = cipher.name();9192if (cipher.keyExAlgorithm == KeyExAlgorithm.ECDHE_ECDSA) {93switch (namedGroup) {94case "secp256r1":95trustedCerts = new SSLSocketTemplate.Cert[] {96SSLSocketTemplate.Cert.CA_ECDSA_SECP256R1 };97endEntityCerts = new SSLSocketTemplate.Cert[] {98SSLSocketTemplate.Cert.EE_ECDSA_SECP256R1 };99break;100case "secp384r1":101trustedCerts = new SSLSocketTemplate.Cert[] {102SSLSocketTemplate.Cert.CA_ECDSA_SECP384R1 };103endEntityCerts = new SSLSocketTemplate.Cert[] {104SSLSocketTemplate.Cert.EE_ECDSA_SECP384R1 };105break;106case "secp521r1":107trustedCerts = new SSLSocketTemplate.Cert[] {108SSLSocketTemplate.Cert.CA_ECDSA_SECP521R1 };109endEntityCerts = new SSLSocketTemplate.Cert[] {110SSLSocketTemplate.Cert.EE_ECDSA_SECP521R1 };111}112} else if (protocol.id < Protocol.TLSV1_2.id113&& cipher.keyExAlgorithm == KeyExAlgorithm.DHE_DSS) {114trustedCerts = new SSLSocketTemplate.Cert[] {115SSLSocketTemplate.Cert.CA_DSA_1024 };116endEntityCerts = new SSLSocketTemplate.Cert[] {117SSLSocketTemplate.Cert.EE_DSA_1024 };118}119}120121protected SSLContext createClientSSLContext() throws Exception {122return createSSLContext(trustedCerts, endEntityCerts,123getClientContextParameters());124}125126protected SSLContext createServerSSLContext() throws Exception {127return createSSLContext(trustedCerts, endEntityCerts,128getServerContextParameters());129}130131// Servers are configured before clients, increment test case after.132@Override133protected void configureClientSocket(SSLSocket socket) {134socket.setEnabledProtocols(new String[] { protocol });135socket.setEnabledCipherSuites(new String[] { cipher });136}137138@Override139protected void configureServerSocket(SSLServerSocket serverSocket) {140serverSocket.setEnabledProtocols(new String[] { protocol });141serverSocket.setEnabledCipherSuites(new String[] { cipher });142}143144public static void main(String[] args) throws Exception {145String namedGroup = args[0];146// Named group is set as per run argument with no change in it's alphabet147System.setProperty("jdk.tls.namedGroups", namedGroup);148System.out.println("NamedGroup: " + namedGroup);149150// Re-enable TLSv1 and TLSv1.1 since test depends on it.151SecurityUtils.removeFromDisabledTlsAlgs("TLSv1", "TLSv1.1");152153for (Protocol protocol : PROTOCOLS) {154for (CipherSuite cipherSuite : CIPHER_SUITES) {155// Named group converted to lower case just156// to satisfy Test condition157if (cipherSuite.supportedByProtocol(protocol)158&& groupSupportdByCipher(namedGroup.toLowerCase(),159cipherSuite)) {160System.out.printf("Protocol: %s, cipher suite: %s%n",161protocol, cipherSuite);162// Named group converted to lower case just163// to satisfy Test condition164new NamedGroupsWithCipherSuite(protocol,165cipherSuite, namedGroup.toLowerCase()).run();166}167}168}169}170171private static boolean groupSupportdByCipher(String group,172CipherSuite cipherSuite) {173return (group.startsWith("x")174&& xdhGroupSupportdByCipher(cipherSuite))175|| (group.startsWith("secp")176&& ecdhGroupSupportdByCipher(cipherSuite))177|| (group.startsWith("ffdhe")178&& ffdhGroupSupportdByCipher(cipherSuite));179}180181private static boolean xdhGroupSupportdByCipher(182CipherSuite cipherSuite) {183return cipherSuite.keyExAlgorithm == null184|| cipherSuite.keyExAlgorithm == KeyExAlgorithm.ECDHE_RSA;185}186187private static boolean ecdhGroupSupportdByCipher(188CipherSuite cipherSuite) {189return cipherSuite.keyExAlgorithm == null190|| cipherSuite.keyExAlgorithm == KeyExAlgorithm.ECDHE_RSA191|| cipherSuite.keyExAlgorithm == KeyExAlgorithm.ECDHE_ECDSA;192}193194private static boolean ffdhGroupSupportdByCipher(195CipherSuite cipherSuite) {196return cipherSuite.keyExAlgorithm == null197|| cipherSuite.keyExAlgorithm == KeyExAlgorithm.DHE_DSS198|| cipherSuite.keyExAlgorithm == KeyExAlgorithm.DHE_RSA;199}200}201202203