Path: blob/master/test/jdk/sun/security/ssl/CipherSuite/LegacyConstraints.java
41152 views
/*1* Copyright (c) 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*/2223/*24* @test25* @bug 823856026* @library /javax/net/ssl/templates27* @summary Make sure that legacy suites are not selected if stronger choices28* are available29* @run main/othervm LegacyConstraints30*/3132import java.io.InputStream;33import java.io.OutputStream;34import java.security.Security;35import java.util.Arrays;36import java.util.ArrayList;37import java.util.List;38import javax.net.ssl.SSLSocket;3940public class LegacyConstraints extends SSLSocketTemplate {4142// none of the legacy suites are supported in TLS 1.3, so don't43// test TLS 1.344private final static List<String> TLS_PROTOCOLS = List.of(45"TLSv1", "TLSv1.1", "TLSv1.2");4647// cipher suites that contain legacy algorithms48private final static List<String> LEGACY_SUITES = List.of(49"TLS_RSA_WITH_NULL_SHA256",50"TLS_DH_anon_WITH_AES_128_CBC_SHA",51"TLS_ECDH_anon_WITH_AES_128_CBC_SHA",52"TLS_ECDHE_ECDSA_WITH_RC4_128_SHA",53"TLS_RSA_WITH_DES_CBC_SHA",54"TLS_RSA_WITH_3DES_EDE_CBC_SHA");5556private static String protocol;5758public static void main(String[] args) throws Exception {59// Clear disabled algorithms so that it doesn't interfere60// with legacy suites61Security.setProperty("jdk.tls.disabledAlgorithms", "");62for (String tlsProtocol : TLS_PROTOCOLS) {63protocol = tlsProtocol;64System.out.println("Testing " + protocol);65new LegacyConstraints().run();66}67}6869/**70* Prepends legacy suites to the array of enabled suites.71*/72private static void configureSocket(SSLSocket socket) {73socket.setEnabledProtocols(new String[] {protocol});74List<String> newSuites = new ArrayList(LEGACY_SUITES);75Arrays.stream(socket.getEnabledCipherSuites())76.forEachOrdered(suite -> newSuites.add(suite));77socket.setEnabledCipherSuites(newSuites.toArray(new String[0]));78}7980@Override81protected void runServerApplication(SSLSocket socket) throws Exception {82configureSocket(socket);8384InputStream sslIS = socket.getInputStream();85OutputStream sslOS = socket.getOutputStream();8687sslIS.read();88sslOS.write(85);89sslOS.flush();9091String negotiatedSuite = socket.getSession().getCipherSuite();92if (LEGACY_SUITES.contains(negotiatedSuite)) {93throw new Exception("Test FAILED: the negotiated suite " +94negotiatedSuite + " is a legacy suite");95}96}9798@Override99protected void runClientApplication(SSLSocket socket) throws Exception {100configureSocket(socket);101102InputStream sslIS = socket.getInputStream();103OutputStream sslOS = socket.getOutputStream();104105sslOS.write(280);106sslOS.flush();107sslIS.read();108}109}110111112