Skip to content

Commit 74b9e36

Browse files
committed
Refuse a DTLS 1.2 HelloVerifyRequest on a DTLS 1.3 handshake, relates to github #1468.
1 parent 3d00eff commit 74b9e36

2 files changed

Lines changed: 180 additions & 8 deletions

File tree

‎tls/src/main/java/org/bouncycastle/tls/DTLSClientProtocol.java‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,20 @@ protected DTLSTransport clientHandshake(ClientHandshakeState state)
116116
// if (serverMessage.getType() == HandshakeType.hello_verify_request)
117117
while (serverMessage.getType() == HandshakeType.hello_verify_request)
118118
{
119+
/*
120+
* RFC 9147 5.1. HelloVerifyRequest has no place in DTLS 1.3 at all; its denial-of-service
121+
* countermeasure is a HelloRetryRequest cookie instead. A client that offered nothing earlier
122+
* than DTLS 1.3 therefore has no use for one, and accepting it would let an attacker drive the
123+
* 1.2 cookie exchange ahead of a 1.3 handshake.
124+
*/
125+
if (!state.offeringDTLSv12Minus)
126+
{
127+
throw new TlsFatalAlert(AlertDescription.unexpected_message,
128+
"HelloVerifyRequest received, but only DTLS 1.3 was offered");
129+
}
130+
131+
state.helloVerifyRequested = true;
132+
119133
byte[] cookie = processHelloVerifyRequest(state, serverMessage.getBody());
120134
byte[] patched = patchClientHelloWithCookie(clientHelloBody, cookie);
121135

@@ -127,6 +141,16 @@ protected DTLSTransport clientHandshake(ClientHandshakeState state)
127141

128142
if (serverMessage.getType() == HandshakeType.server_hello && isHelloRetryRequest(serverMessage.getBody()))
129143
{
144+
/*
145+
* RFC 9147 5.1. A HelloRetryRequest means DTLS 1.3, which does not have HelloVerifyRequest, so
146+
* the two cannot both have happened.
147+
*/
148+
if (state.helloVerifyRequested)
149+
{
150+
throw new TlsFatalAlert(AlertDescription.illegal_parameter,
151+
"HelloRetryRequest received after a HelloVerifyRequest");
152+
}
153+
130154
process13HelloRetryRequest(state, serverMessage.getBody());
131155

132156
/*
@@ -187,6 +211,17 @@ protected DTLSTransport clientHandshake(ClientHandshakeState state)
187211

188212
boolean isDTLSv13 = ProtocolVersion.DTLSv13.isEqualOrEarlierVersionOf(server_version);
189213

214+
/*
215+
* RFC 9147 5.1. DTLS 1.3 has no HelloVerifyRequest, so a server that sent one cannot then select
216+
* DTLS 1.3: either it is confused, or the cookie exchange was driven by something in the middle.
217+
* Without this a 1.3 handshake could be reached through the 1.2 countermeasure.
218+
*/
219+
if (isDTLSv13 && state.helloVerifyRequested)
220+
{
221+
throw new TlsFatalAlert(AlertDescription.illegal_parameter,
222+
"Server selected DTLS 1.3 after sending a HelloVerifyRequest");
223+
}
224+
190225
if (!isDTLSv13)
191226
{
192227
reportServerVersion(state, recordLayerVersion);
@@ -619,6 +654,9 @@ protected byte[] generateClientHello(ClientHandshakeState state)
619654
boolean offeringDTLSv12Minus = ProtocolVersion.DTLSv12.isEqualOrLaterVersionOf(earliestVersion);
620655
boolean offeringDTLSv13Plus = ProtocolVersion.DTLSv13.isEqualOrEarlierVersionOf(latestVersion);
621656

657+
// NOTE: Whether a HelloVerifyRequest is a legal answer at all - see clientHandshake
658+
state.offeringDTLSv12Minus = offeringDTLSv12Minus;
659+
622660
{
623661
boolean useGMTUnixTime = !offeringDTLSv13Plus && client.shouldUseGMTUnixTime();
624662

@@ -1964,6 +2002,8 @@ protected static class ClientHandshakeState
19642002
boolean expectSessionTicket = false;
19652003
Hashtable clientAgreements = null;
19662004
OfferedPsks.BindersConfig clientBinders = null;
2005+
boolean offeringDTLSv12Minus = false;
2006+
boolean helloVerifyRequested = false;
19672007
boolean afterHelloRetryRequest = false;
19682008
byte[] retryCookie = null;
19692009
int retryGroup = -1;

‎tls/src/test/java/org/bouncycastle/tls/test/DTLS13ProtocolTest.java‎

Lines changed: 140 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
import org.bouncycastle.tls.CipherSuite;
99
import org.bouncycastle.tls.ContentType;
1010
import org.bouncycastle.tls.DTLSClientProtocol;
11+
import org.bouncycastle.tls.DTLSRequest;
1112
import org.bouncycastle.tls.DTLSServerProtocol;
1213
import org.bouncycastle.tls.DTLSTransport;
14+
import org.bouncycastle.tls.DTLSVerifier;
1315
import org.bouncycastle.tls.DatagramTransport;
1416
import org.bouncycastle.tls.ExtensionType;
1517
import org.bouncycastle.tls.HandshakeType;
@@ -19,7 +21,9 @@
1921
import org.bouncycastle.tls.TlsFatalAlert;
2022
import org.bouncycastle.tls.TlsFatalAlertReceived;
2123
import org.bouncycastle.tls.TlsServer;
24+
import org.bouncycastle.tls.crypto.TlsCrypto;
2225
import org.bouncycastle.util.Arrays;
26+
import org.bouncycastle.util.Strings;
2327

2428
import junit.framework.TestCase;
2529

@@ -63,6 +67,9 @@ public class DTLS13ProtocolTest
6367
(byte)0x07, (byte)0x9E, (byte)0x09, (byte)0xE2, (byte)0xC8, (byte)0xA8, (byte)0x33, (byte)0x9C
6468
};
6569

70+
// NOTE: Test value only - would typically be the client's IP address
71+
private static final byte[] CLIENT_ID = Strings.toUTF8ByteArray("MockDtlsClient");
72+
6673
/** What, if anything, to corrupt in the second ClientHello on its way to the server. */
6774
private static final int MANGLE_NONE = 0;
6875
private static final int MANGLE_COOKIE = 1;
@@ -442,6 +449,79 @@ public void testSecondHelloRetryRequestRejected() throws Exception
442449
assertTrue("the second HelloRetryRequest was never played back", harness.replayed > 0);
443450
}
444451

452+
/**
453+
* RFC 9147 5.1. DTLS 1.3 replaces the HelloVerifyRequest with a HelloRetryRequest cookie and has no
454+
* HelloVerifyRequest at all, so a client that offered nothing earlier than DTLS 1.3 must refuse one. The
455+
* DTLS 1.2 countermeasure front end is put in front of the server here precisely because it is the thing
456+
* that would otherwise send one.
457+
*/
458+
public void testHelloVerifyRequestRefusedWhenOnlyDTLSv13Offered() throws Exception
459+
{
460+
Harness harness = new Harness();
461+
harness.helloVerifyRequestFrontEnd = true;
462+
harness.serverHandshakeTimeoutMillis = 4000;
463+
464+
try
465+
{
466+
harness.run(16);
467+
468+
fail("expected the client to refuse a HelloVerifyRequest");
469+
}
470+
catch (TlsFatalAlert fatalAlert)
471+
{
472+
assertEquals("alert for a HelloVerifyRequest with only DTLS 1.3 offered",
473+
AlertDescription.unexpected_message, fatalAlert.getAlertDescription());
474+
}
475+
}
476+
477+
/**
478+
* RFC 9147 5.1. The other half of the same rule, and the one a version-straddling client reaches: a client
479+
* that also offers DTLS 1.2 must accept a HelloVerifyRequest, since the server may be a 1.2 server - but
480+
* having answered one it must not then let the server select DTLS 1.3, because that handshake would have
481+
* been reached through a countermeasure DTLS 1.3 does not have. Without this check the cookie exchange is
482+
* a way in to a 1.3 handshake.
483+
*/
484+
public void testDTLSv13RefusedAfterAHelloVerifyRequest() throws Exception
485+
{
486+
Harness harness = new Harness();
487+
harness.helloVerifyRequestFrontEnd = true;
488+
harness.clientVersions = ProtocolVersion.DTLSv13.downTo(ProtocolVersion.DTLSv12);
489+
harness.serverHandshakeTimeoutMillis = 4000;
490+
491+
try
492+
{
493+
harness.run(16);
494+
495+
fail("expected the client to refuse DTLS 1.3 after a HelloVerifyRequest");
496+
}
497+
catch (TlsFatalAlert fatalAlert)
498+
{
499+
assertEquals("alert for DTLS 1.3 selected after a HelloVerifyRequest",
500+
AlertDescription.illegal_parameter, fatalAlert.getAlertDescription());
501+
}
502+
}
503+
504+
/**
505+
* The counterpart of the two above: the HelloVerifyRequest path that DTLS 1.2 depends on is untouched. A
506+
* client and server that both also speak DTLS 1.2 complete a 1.2 handshake through the cookie exchange,
507+
* exactly as the DTLS 1.2 suites do.
508+
*/
509+
public void testHelloVerifyRequestStillWorksForDTLSv12() throws Exception
510+
{
511+
Harness harness = new Harness();
512+
harness.helloVerifyRequestFrontEnd = true;
513+
harness.clientVersions = ProtocolVersion.DTLSv13.downTo(ProtocolVersion.DTLSv12);
514+
harness.serverVersions = ProtocolVersion.DTLSv12.only();
515+
516+
harness.run(16);
517+
518+
assertEquals("client negotiated version", ProtocolVersion.DTLSv12, harness.clientVersion);
519+
assertEquals("server negotiated version", ProtocolVersion.DTLSv12, harness.serverVersion);
520+
521+
assertNotNull("no application data echoed back", harness.echo);
522+
assertTrue("echoed application data differs", Arrays.areEqual(harness.request, harness.echo));
523+
}
524+
445525
/**
446526
* RFC 8446 4.2.2. The second ClientHello must echo the cookie exactly, and a server that accepted anything
447527
* else would have no way to tell its own HelloRetryRequest's answer from an unrelated ClientHello. One byte
@@ -885,6 +965,17 @@ static class Harness
885965
boolean forceHelloRetryRequest = false;
886966
boolean replaySecondHelloRetryRequest = false;
887967
int mangleSecondClientHello = MANGLE_NONE;
968+
boolean helloVerifyRequestFrontEnd = false;
969+
ProtocolVersion[] clientVersions = ProtocolVersion.DTLSv13.only();
970+
ProtocolVersion[] serverVersions = ProtocolVersion.DTLSv13.only();
971+
972+
/*
973+
* Only worth lowering for a test where the client aborts partway: the server then has no peer left to
974+
* hear from, and shutting the harness down waits for its handshake to give up. Where the client aborts
975+
* before installing the handshake traffic keys, its alert is an epoch-0 record the server can no
976+
* longer read, so giving up is the only way out.
977+
*/
978+
int serverHandshakeTimeoutMillis = HANDSHAKE_TIMEOUT_MILLIS;
888979
UnreliableDatagramTransportFactory loss = null;
889980

890981
ProtocolVersion clientVersion = null;
@@ -926,7 +1017,7 @@ void run(int dataLength) throws Exception
9261017
{
9271018
protected ProtocolVersion[] getSupportedVersions()
9281019
{
929-
return ProtocolVersion.DTLSv13.only();
1020+
return clientVersions;
9301021
}
9311022

9321023
public void notifyServerVersion(ProtocolVersion version) throws IOException
@@ -953,12 +1044,12 @@ public void notifyHandshakeComplete() throws IOException
9531044
{
9541045
protected ProtocolVersion[] getSupportedVersions()
9551046
{
956-
return ProtocolVersion.DTLSv13.only();
1047+
return serverVersions;
9571048
}
9581049

9591050
public int getHandshakeTimeoutMillis()
9601051
{
961-
return HANDSHAKE_TIMEOUT_MILLIS;
1052+
return serverHandshakeTimeoutMillis;
9621053
}
9631054

9641055
public int[] getSupportedGroups() throws IOException
@@ -1002,7 +1093,8 @@ public void notifyHandshakeComplete() throws IOException
10021093

10031094
DTLSServerProtocol serverProtocol = new DTLSServerProtocol();
10041095

1005-
ServerThread serverThread = new ServerThread(serverProtocol, server, network.getServer());
1096+
ServerThread serverThread = new ServerThread(serverProtocol, server, network.getServer(),
1097+
helloVerifyRequestFrontEnd);
10061098
serverThread.setDaemon(true);
10071099
serverThread.start();
10081100

@@ -1153,24 +1245,31 @@ static class ServerThread
11531245
private final DTLSServerProtocol serverProtocol;
11541246
private final TlsServer server;
11551247
private final DatagramTransport serverTransport;
1248+
private final boolean helloVerifyRequestFrontEnd;
11561249
private volatile boolean isShutdown = false;
11571250

1158-
ServerThread(DTLSServerProtocol serverProtocol, TlsServer server, DatagramTransport serverTransport)
1251+
ServerThread(DTLSServerProtocol serverProtocol, TlsServer server, DatagramTransport serverTransport,
1252+
boolean helloVerifyRequestFrontEnd)
11591253
{
11601254
this.serverProtocol = serverProtocol;
11611255
this.server = server;
11621256
this.serverTransport = serverTransport;
1257+
this.helloVerifyRequestFrontEnd = helloVerifyRequestFrontEnd;
11631258
}
11641259

11651260
public void run()
11661261
{
11671262
try
11681263
{
11691264
/*
1170-
* NOTE: Not the DTLSVerifier harness the DTLS 1.2 tests use: that issues a DTLS 1.2
1171-
* HelloVerifyRequest, which RFC 9147 5.1 has no place for in DTLS 1.3.
1265+
* NOTE: By default not the DTLSVerifier harness the DTLS 1.2 tests use: that issues a DTLS
1266+
* 1.2 HelloVerifyRequest, where RFC 9147 5.1 uses a HelloRetryRequest cookie instead. The
1267+
* tests of that rule turn it on deliberately, to check that a DTLS 1.3 handshake cannot be
1268+
* reached through it.
11721269
*/
1173-
DTLSTransport dtlsTransport = serverProtocol.accept(server, serverTransport);
1270+
DTLSRequest request = helloVerifyRequestFrontEnd ? verifyRequest() : null;
1271+
1272+
DTLSTransport dtlsTransport = serverProtocol.accept(server, serverTransport, request);
11741273

11751274
byte[] buf = new byte[dtlsTransport.getReceiveLimit()];
11761275
while (!isShutdown)
@@ -1189,6 +1288,39 @@ public void run()
11891288
}
11901289
}
11911290

1291+
/**
1292+
* The DTLS 1.2 denial-of-service countermeasure as a front end, exactly as DTLSProtocolTest drives
1293+
* it: datagrams are read until one carries a ClientHello with a valid cookie, and anything else is
1294+
* answered with a HelloVerifyRequest.
1295+
*/
1296+
private DTLSRequest verifyRequest() throws IOException
1297+
{
1298+
TlsCrypto serverCrypto = server.getCrypto();
1299+
1300+
DTLSVerifier verifier = new DTLSVerifier(serverCrypto);
1301+
1302+
int receiveLimit = serverTransport.getReceiveLimit();
1303+
byte[] buf = new byte[receiveLimit];
1304+
1305+
for (;;)
1306+
{
1307+
if (isShutdown)
1308+
{
1309+
return null;
1310+
}
1311+
1312+
int length = serverTransport.receive(buf, 0, receiveLimit, 100);
1313+
if (length > 0)
1314+
{
1315+
DTLSRequest request = verifier.verifyRequest(CLIENT_ID, buf, 0, length, serverTransport);
1316+
if (null != request)
1317+
{
1318+
return request;
1319+
}
1320+
}
1321+
}
1322+
}
1323+
11921324
void shutdown()
11931325
throws InterruptedException
11941326
{

0 commit comments

Comments
 (0)