1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
|
/*
* Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import jdk.test.lib.process.ProcessTools;
import jdk.test.lib.security.SecurityUtils;
import javax.net.ssl.*;
import java.io.IOException;
import java.net.*;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
/*
* @test
* @bug 8301381
* @library /test/lib /javax/net/ssl/templates
* @summary DTLSv10 is now disabled. This test verifies that the server will
* not negotiate a connection if the client asks for it.
* @run main/othervm DTLSWontNegotiateV10 DTLS
* @run main/othervm DTLSWontNegotiateV10 DTLSv1.0
*/
public class DTLSWontNegotiateV10 {
private static final int MTU = 1024;
private static final String DTLSV_1_0 = "DTLSv1.0";
private static final String DTLS = "DTLS";
private static final String DTLSV_1_2 = "DTLSv1.2";
private static final int READ_TIMEOUT_SECS = Integer.getInteger("readtimeout", 30);
public static void main(String[] args) throws Exception {
if (args[0].equals(DTLSV_1_0)) {
SecurityUtils.removeFromDisabledTlsAlgs(DTLSV_1_0);
}
if (args.length > 1) {
// running in client child process
// args: protocol server-port
try (DTLSClient client = new DTLSClient(args[0], Integer.parseInt(args[1]))) {
client.run();
}
} else {
// server process
// args: protocol
final int totalAttempts = 5;
int tries;
for (tries = 0 ; tries < totalAttempts ; ++tries) {
try {
System.out.printf("Starting server %d/%d attempts%n", tries+1, totalAttempts);
runServer(args[0]);
break;
} catch (SocketTimeoutException exc) {
System.out.println("The server timed-out waiting for packets from the client.");
}
}
if (tries == totalAttempts) {
throw new RuntimeException("The server/client communications timed-out after " + totalAttempts + " tries.");
}
}
}
private static void runServer(String protocol) throws Exception {
// args: protocol
Process clientProcess = null;
try (DTLSServer server = new DTLSServer(protocol)) {
List<String> command = List.of(
"DTLSWontNegotiateV10",
// if server is "DTLS" then the client should be v1.0 and vice versa
protocol.equals(DTLS) ? DTLSV_1_0 : DTLS,
Integer.toString(server.getListeningPortNumber())
);
ProcessBuilder builder = ProcessTools.createTestJavaProcessBuilder(command);
clientProcess = builder.inheritIO().start();
server.run();
System.out.println("Success: DTLSv1.0 connection was not established.");
} finally {
if (clientProcess != null) {
clientProcess.destroy();
}
}
}
private static class DTLSClient extends DTLSEndpoint {
private final int remotePort;
private final DatagramSocket socket = new DatagramSocket();
public DTLSClient(String protocol, int portNumber) throws Exception {
super(true, protocol);
remotePort = portNumber;
socket.setSoTimeout(READ_TIMEOUT_SECS * 1000);
log("Client listening on port " + socket.getLocalPort()
+ ". Sending data to server port " + remotePort);
log("Enabled protocols: " + String.join(" ", engine.getEnabledProtocols()));
}
@Override
public void run() throws Exception {
doHandshake(socket);
log("Client done handshaking. Protocol: " + engine.getSession().getProtocol());
}
@Override
void setRemotePortNumber(int portNumber) {
// don't do anything; we're using the one we already know
}
@Override
int getRemotePortNumber() {
return remotePort;
}
@Override
public void close () {
socket.close();
}
}
private abstract static class DTLSEndpoint extends SSLContextTemplate implements AutoCloseable {
protected final SSLEngine engine;
protected final SSLContext context;
private final String protocol;
protected final InetAddress LOCALHOST;
private final String tag;
public DTLSEndpoint(boolean useClientMode, String protocol) throws Exception {
this.protocol = protocol;
if (useClientMode) {
tag = "client";
context = createClientSSLContext();
} else {
tag = "server";
context = createServerSSLContext();
}
engine = context.createSSLEngine();
engine.setUseClientMode(useClientMode);
SSLParameters params = engine.getSSLParameters();
params.setMaximumPacketSize(MTU);
engine.setSSLParameters(params);
if (protocol.equals(DTLS)) {
// make sure both versions are "enabled"; 1.0 should be
// disabled by policy now and won't be negotiated.
engine.setEnabledProtocols(new String[]{DTLSV_1_0, DTLSV_1_2});
} else {
engine.setEnabledProtocols(new String[]{DTLSV_1_0});
}
LOCALHOST = InetAddress.getByName("localhost");
}
@Override
protected ContextParameters getServerContextParameters() {
return new ContextParameters(protocol, "PKIX", "NewSunX509");
}
@Override
protected ContextParameters getClientContextParameters() {
return new ContextParameters(protocol, "PKIX", "NewSunX509");
}
abstract void setRemotePortNumber(int portNumber);
abstract int getRemotePortNumber();
abstract void run() throws Exception;
private boolean runDelegatedTasks() {
log("Running delegated tasks.");
Runnable runnable;
while ((runnable = engine.getDelegatedTask()) != null) {
runnable.run();
}
SSLEngineResult.HandshakeStatus hs = engine.getHandshakeStatus();
if (hs == SSLEngineResult.HandshakeStatus.NEED_TASK) {
throw new RuntimeException(
"Handshake shouldn't need additional tasks");
}
return true;
}
protected void doHandshake(DatagramSocket socket) throws Exception {
boolean handshaking = true;
engine.beginHandshake();
while (handshaking) {
log("Handshake status = " + engine.getHandshakeStatus());
handshaking = switch (engine.getHandshakeStatus()) {
case NEED_UNWRAP, NEED_UNWRAP_AGAIN -> readFromServer(socket);
case NEED_WRAP -> sendHandshakePackets(socket);
case NEED_TASK -> runDelegatedTasks();
case NOT_HANDSHAKING, FINISHED -> false;
};
}
}
private boolean readFromServer(DatagramSocket socket) throws IOException {
log("Reading data from remote endpoint.");
ByteBuffer iNet, iApp;
if (engine.getHandshakeStatus() == SSLEngineResult.HandshakeStatus.NEED_UNWRAP) {
byte[] buffer = new byte[MTU];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
socket.receive(packet);
setRemotePortNumber(packet.getPort());
iNet = ByteBuffer.wrap(buffer, 0, packet.getLength());
iApp = ByteBuffer.allocate(MTU);
} else {
iNet = ByteBuffer.allocate(0);
iApp = ByteBuffer.allocate(MTU);
}
SSLEngineResult engineResult;
do {
engineResult = engine.unwrap(iNet, iApp);
} while (iNet.hasRemaining());
return switch (engineResult.getStatus()) {
case CLOSED -> false;
case OK -> true;
case BUFFER_OVERFLOW -> throw new RuntimeException("Buffer overflow: "
+ "incorrect server maximum fragment size");
case BUFFER_UNDERFLOW -> throw new RuntimeException("Buffer underflow: "
+ "incorrect server maximum fragment size");
};
}
private boolean sendHandshakePackets(DatagramSocket socket) throws Exception {
List<DatagramPacket> packets = generateHandshakePackets();
log("Sending handshake packets.");
packets.forEach((p) -> {
try {
socket.send(p);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
return true;
}
private List<DatagramPacket> generateHandshakePackets() throws SSLException {
log("Generating handshake packets.");
List<DatagramPacket> packets = new ArrayList<>();
ByteBuffer oNet = ByteBuffer.allocate(engine.getSession().getPacketBufferSize());
ByteBuffer oApp = ByteBuffer.allocate(0);
while (engine.getHandshakeStatus() == SSLEngineResult.HandshakeStatus.NEED_WRAP) {
SSLEngineResult result = engine.wrap(oApp, oNet);
oNet.flip();
switch (result.getStatus()) {
case BUFFER_UNDERFLOW -> {
if (engine.getHandshakeStatus() != SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING) {
throw new RuntimeException("Buffer underflow: "
+ "incorrect server maximum fragment size");
}
}
case BUFFER_OVERFLOW -> throw new RuntimeException("Buffer overflow: "
+ "incorrect server maximum fragment size");
case CLOSED -> throw new RuntimeException("SSLEngine has closed");
}
if (oNet.hasRemaining()) {
byte[] packetBuffer = new byte[oNet.remaining()];
oNet.get(packetBuffer);
packets.add(new DatagramPacket(packetBuffer, packetBuffer.length,
LOCALHOST, getRemotePortNumber()));
}
runDelegatedTasks();
oNet.clear();
}
log("Generated " + packets.size() + " packets.");
return packets;
}
protected void log(String msg) {
System.out.println(tag + ": " + msg);
}
}
private static class DTLSServer extends DTLSEndpoint implements AutoCloseable {
private final AtomicInteger portNumber = new AtomicInteger(0);
private final DatagramSocket socket = new DatagramSocket(0);
public DTLSServer(String protocol) throws Exception {
super(false, protocol);
socket.setSoTimeout(READ_TIMEOUT_SECS * 1000);
log("Server listening on port: " + socket.getLocalPort());
log("Enabled protocols: " + String.join(" ", engine.getEnabledProtocols()));
}
@Override
public void run() throws Exception {
doHandshake(socket);
if (!engine.getSession().getProtocol().equals("NONE")) {
throw new RuntimeException("Negotiated protocol: "
+ engine.getSession().getProtocol() +
". No protocol should be negotated.");
}
}
public int getListeningPortNumber() {
return socket.getLocalPort();
}
void setRemotePortNumber(int portNumber) {
this.portNumber.compareAndSet(0, portNumber);
}
int getRemotePortNumber() {
return portNumber.get();
}
@Override
public void close() throws Exception {
socket.close();
}
}
}
|