Path: blob/master/test/jdk/sun/security/ssl/DHKeyExchange/LegacyDHEKeyExchange.java
66645 views
/*1* Copyright (c) 2016, 2021, 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// SunJSSE does not support dynamic system properties, no way to re-use24// system properties in samevm/agentvm mode.2526/*27* @test28* @bug 814810829* @summary Disable Diffie-Hellman keys less than 1024 bits30* @library /javax/net/ssl/templates31* @run main/othervm -Djdk.tls.ephemeralDHKeySize=legacy LegacyDHEKeyExchange32*/3334import javax.net.ssl.SSLException;35import javax.net.ssl.SSLHandshakeException;36import javax.net.ssl.SSLSocket;37import java.net.SocketException;38import java.util.concurrent.CountDownLatch;3940public class LegacyDHEKeyExchange extends SSLSocketTemplate{4142private final CountDownLatch connDoneLatch = new CountDownLatch(2);4344private static final int LINGER_TIMEOUT = 30; // in seconds4546@Override47protected void runServerApplication(SSLSocket socket) throws Exception {48try {49super.runServerApplication(socket);50throw new Exception("Legacy DH keys (< 1024) should be restricted");51} catch (SSLHandshakeException she) {52String expectedExMsg = "Received fatal alert: insufficient_security";53if (!expectedExMsg.equals(she.getMessage())) {54throw she;55}56System.out.println("Expected exception thrown in server");57} catch (SSLException | SocketException se) {58// The client side may have closed the socket.59System.out.println("Server exception:");60se.printStackTrace(System.out);61} finally {62connDoneLatch.countDown();63connDoneLatch.await();64}65}6667@Override68protected void runClientApplication(SSLSocket socket) throws Exception {69String[] suites = new String [] {"TLS_DHE_RSA_WITH_AES_128_CBC_SHA"};70socket.setEnabledCipherSuites(suites);71socket.setSoLinger(true, LINGER_TIMEOUT);7273try {74super.runClientApplication(socket);75throw new Exception("Legacy DH keys (< 1024) should be restricted");76} catch (SSLHandshakeException she) {77String expectedExMsg = "DH ServerKeyExchange does not comply to" +78" algorithm constraints";79if (!expectedExMsg.equals(she.getMessage())) {80throw she;81}82System.out.println("Expected exception thrown in client");83} catch (SSLException | SocketException se) {84// The server side may have closed the socket.85System.out.println("Client exception:");86se.printStackTrace(System.out);87} finally {88connDoneLatch.countDown();89connDoneLatch.await();90}91}9293public static void main(String[] args) throws Exception {94new LegacyDHEKeyExchange().run();95}96}979899