Path: blob/master/test/jdk/sun/security/ssl/DHKeyExchange/LegacyDHEKeyExchange.java
41152 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.SSLHandshakeException;35import javax.net.ssl.SSLSocket;36import java.util.concurrent.CountDownLatch;3738public class LegacyDHEKeyExchange extends SSLSocketTemplate{3940private final CountDownLatch connDoneLatch = new CountDownLatch(2);4142private static final int LINGER_TIMEOUT = 30; // in seconds4344@Override45protected void runServerApplication(SSLSocket socket) throws Exception {46try {47super.runServerApplication(socket);48throw new Exception("Legacy DH keys (< 1024) should be restricted");49} catch (SSLHandshakeException she) {50String expectedExMsg = "Received fatal alert: insufficient_security";51if (!expectedExMsg.equals(she.getMessage())) {52throw she;53}54System.out.println("Expected exception thrown in server");55} finally {56connDoneLatch.countDown();57connDoneLatch.await();58}59}6061@Override62protected void runClientApplication(SSLSocket socket) throws Exception {63String[] suites = new String [] {"TLS_DHE_RSA_WITH_AES_128_CBC_SHA"};64socket.setEnabledCipherSuites(suites);65socket.setSoLinger(true, LINGER_TIMEOUT);6667try {68super.runClientApplication(socket);69throw new Exception("Legacy DH keys (< 1024) should be restricted");70} catch (SSLHandshakeException she) {71String expectedExMsg = "DH ServerKeyExchange does not comply to" +72" algorithm constraints";73if (!expectedExMsg.equals(she.getMessage())) {74throw she;75}76System.out.println("Expected exception thrown in client");77} finally {78connDoneLatch.countDown();79connDoneLatch.await();80}81}8283public static void main(String[] args) throws Exception {84new LegacyDHEKeyExchange().run();85}86}878889