< prev index next >

test/jdk/javax/net/ssl/DTLS/DTLSWontNegotiateV10.java

Print this page

  1 /*
  2  * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 

 24 import jdk.test.lib.security.SecurityUtils;
 25 
 26 import javax.net.ssl.*;
 27 import java.io.IOException;
 28 import java.net.*;
 29 import java.nio.ByteBuffer;
 30 import java.nio.file.Path;
 31 import java.util.ArrayList;
 32 import java.util.List;
 33 import java.util.concurrent.atomic.AtomicInteger;
 34 
 35 /*
 36  * @test
 37  * @bug 8301381
 38  * @library /test/lib /javax/net/ssl/templates
 39  * @summary DTLSv10 is now disabled. This test verifies that the server will
 40  *     not negotiate a connection if the client asks for it.
 41  * @run main/othervm DTLSWontNegotiateV10 DTLS
 42  * @run main/othervm DTLSWontNegotiateV10 DTLSv1.0
 43  */
 44 public class DTLSWontNegotiateV10 {
 45 
 46     private static final int MTU = 1024;
 47     private static final String DTLSV_1_0 = "DTLSv1.0";
 48     private static final String DTLS = "DTLS";
 49     private static final String DTLSV_1_2 = "DTLSv1.2";
 50 


 51     public static void main(String[] args) throws Exception {
 52         if (args[0].equals(DTLSV_1_0)) {
 53             SecurityUtils.removeFromDisabledTlsAlgs(DTLSV_1_0);
 54         }
 55 
 56         if (args.length > 1) {
 57             // running in client child process
 58             // args: protocol server-port
 59             try (DTLSClient client = new DTLSClient(args[0], Integer.parseInt(args[1]))) {
 60                 client.run();
 61             }
 62 
 63         } else {
 64             // server process
 65             // args: protocol
 66             try (DTLSServer server = new DTLSServer(args[0])) {
 67                 List<String> command = List.of(
 68                         Path.of(System.getProperty("java.home"), "bin", "java").toString(),
 69                         "DTLSWontNegotiateV10",
 70                         // if server is "DTLS" then the client should be v1.0 and vice versa
 71                         args[0].equals(DTLS) ? DTLSV_1_0 : DTLS,
 72                         Integer.toString(server.getListeningPortNumber())
 73                 );
 74 
 75                 ProcessBuilder builder = new ProcessBuilder(command);
 76                 Process p = builder.inheritIO().start();
 77                 server.run();
 78                 p.destroy();
 79                 System.out.println("Success: DTLSv1.0 connection was not established.");






















 80             }
 81         }
 82     }
 83 
 84     private static class DTLSClient extends DTLSEndpoint {
 85         private final int remotePort;
 86 
 87         private final DatagramSocket socket = new DatagramSocket();
 88 
 89         public DTLSClient(String protocol, int portNumber) throws Exception {
 90             super(true, protocol);
 91             remotePort = portNumber;



 92             log("Enabled protocols: " + String.join(" ", engine.getEnabledProtocols()));
 93         }
 94 
 95         @Override
 96         public void run() throws Exception {
 97             doHandshake(socket);
 98             log("Client done handshaking. Protocol: " + engine.getSession().getProtocol());
 99         }
100 
101         @Override
102         void setRemotePortNumber(int portNumber) {
103             // don't do anything; we're using the one we already know
104         }
105 
106         @Override
107         int getRemotePortNumber() {
108             return remotePort;
109         }
110 
111         @Override

270                 runDelegatedTasks();
271                 oNet.clear();
272             }
273 
274             log("Generated " + packets.size() + " packets.");
275             return packets;
276         }
277 
278         protected void log(String msg) {
279             System.out.println(tag + ": " + msg);
280         }
281     }
282 
283     private static class DTLSServer extends DTLSEndpoint implements AutoCloseable {
284 
285         private final AtomicInteger portNumber = new AtomicInteger(0);
286         private final DatagramSocket socket = new DatagramSocket(0);
287 
288         public DTLSServer(String protocol) throws Exception {
289             super(false, protocol);


290             log("Enabled protocols: " + String.join(" ", engine.getEnabledProtocols()));
291         }
292 
293         @Override
294         public void run() throws Exception {
295             doHandshake(socket);
296             if (!engine.getSession().getProtocol().equals("NONE")) {
297                 throw new RuntimeException("Negotiated protocol: "
298                         + engine.getSession().getProtocol() +
299                         ". No protocol should be negotated.");
300             }
301         }
302 
303         public int getListeningPortNumber() {
304             return socket.getLocalPort();
305         }
306 
307         void setRemotePortNumber(int portNumber) {
308             this.portNumber.compareAndSet(0, portNumber);
309         }

  1 /*
  2  * Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 import jdk.test.lib.process.ProcessTools;
 25 import jdk.test.lib.security.SecurityUtils;
 26 
 27 import javax.net.ssl.*;
 28 import java.io.IOException;
 29 import java.net.*;
 30 import java.nio.ByteBuffer;

 31 import java.util.ArrayList;
 32 import java.util.List;
 33 import java.util.concurrent.atomic.AtomicInteger;
 34 
 35 /*
 36  * @test
 37  * @bug 8301381
 38  * @library /test/lib /javax/net/ssl/templates
 39  * @summary DTLSv10 is now disabled. This test verifies that the server will
 40  *     not negotiate a connection if the client asks for it.
 41  * @run main/othervm DTLSWontNegotiateV10 DTLS
 42  * @run main/othervm DTLSWontNegotiateV10 DTLSv1.0
 43  */
 44 public class DTLSWontNegotiateV10 {
 45 
 46     private static final int MTU = 1024;
 47     private static final String DTLSV_1_0 = "DTLSv1.0";
 48     private static final String DTLS = "DTLS";
 49     private static final String DTLSV_1_2 = "DTLSv1.2";
 50 
 51     private static final int READ_TIMEOUT_SECS = Integer.getInteger("readtimeout", 30);
 52 
 53     public static void main(String[] args) throws Exception {
 54         if (args[0].equals(DTLSV_1_0)) {
 55             SecurityUtils.removeFromDisabledTlsAlgs(DTLSV_1_0);
 56         }
 57 
 58         if (args.length > 1) {
 59             // running in client child process
 60             // args: protocol server-port
 61             try (DTLSClient client = new DTLSClient(args[0], Integer.parseInt(args[1]))) {
 62                 client.run();
 63             }
 64 
 65         } else {
 66             // server process
 67             // args: protocol
 68             final int totalAttempts = 5;
 69             int tries;
 70             for (tries = 0 ; tries < totalAttempts ; ++tries) {
 71                 try {
 72                     System.out.printf("Starting server %d/%d attempts%n", tries+1, totalAttempts);
 73                     runServer(args[0]);
 74                     break;
 75                 } catch (SocketTimeoutException exc) {
 76                     System.out.println("The server timed-out waiting for packets from the client.");
 77                 }
 78             }
 79             if (tries == totalAttempts) {
 80                 throw new RuntimeException("The server/client communications timed-out after " + totalAttempts + " tries.");
 81             }
 82         }
 83     }
 84 
 85     private static void runServer(String protocol) throws Exception {
 86         // args: protocol
 87         Process clientProcess = null;
 88         try (DTLSServer server = new DTLSServer(protocol)) {
 89             List<String> command = List.of(
 90                     "DTLSWontNegotiateV10",
 91                     // if server is "DTLS" then the client should be v1.0 and vice versa
 92                     protocol.equals(DTLS) ? DTLSV_1_0 : DTLS,
 93                     Integer.toString(server.getListeningPortNumber())
 94             );
 95 
 96             ProcessBuilder builder = ProcessTools.createTestJavaProcessBuilder(command);
 97             clientProcess = builder.inheritIO().start();
 98             server.run();
 99             System.out.println("Success: DTLSv1.0 connection was not established.");
100 
101         } finally {
102             if (clientProcess != null) {
103                 clientProcess.destroy();
104             }
105         }
106     }
107 
108     private static class DTLSClient extends DTLSEndpoint {
109         private final int remotePort;
110 
111         private final DatagramSocket socket = new DatagramSocket();
112 
113         public DTLSClient(String protocol, int portNumber) throws Exception {
114             super(true, protocol);
115             remotePort = portNumber;
116             socket.setSoTimeout(READ_TIMEOUT_SECS * 1000);
117             log("Client listening on port " + socket.getLocalPort()
118                     + ". Sending data to server port " + remotePort);
119             log("Enabled protocols: " + String.join(" ", engine.getEnabledProtocols()));
120         }
121 
122         @Override
123         public void run() throws Exception {
124             doHandshake(socket);
125             log("Client done handshaking. Protocol: " + engine.getSession().getProtocol());
126         }
127 
128         @Override
129         void setRemotePortNumber(int portNumber) {
130             // don't do anything; we're using the one we already know
131         }
132 
133         @Override
134         int getRemotePortNumber() {
135             return remotePort;
136         }
137 
138         @Override

297                 runDelegatedTasks();
298                 oNet.clear();
299             }
300 
301             log("Generated " + packets.size() + " packets.");
302             return packets;
303         }
304 
305         protected void log(String msg) {
306             System.out.println(tag + ": " + msg);
307         }
308     }
309 
310     private static class DTLSServer extends DTLSEndpoint implements AutoCloseable {
311 
312         private final AtomicInteger portNumber = new AtomicInteger(0);
313         private final DatagramSocket socket = new DatagramSocket(0);
314 
315         public DTLSServer(String protocol) throws Exception {
316             super(false, protocol);
317             socket.setSoTimeout(READ_TIMEOUT_SECS * 1000);
318             log("Server listening on port: " + socket.getLocalPort());
319             log("Enabled protocols: " + String.join(" ", engine.getEnabledProtocols()));
320         }
321 
322         @Override
323         public void run() throws Exception {
324             doHandshake(socket);
325             if (!engine.getSession().getProtocol().equals("NONE")) {
326                 throw new RuntimeException("Negotiated protocol: "
327                         + engine.getSession().getProtocol() +
328                         ". No protocol should be negotated.");
329             }
330         }
331 
332         public int getListeningPortNumber() {
333             return socket.getLocalPort();
334         }
335 
336         void setRemotePortNumber(int portNumber) {
337             this.portNumber.compareAndSet(0, portNumber);
338         }
< prev index next >