88import org .bouncycastle .tls .CipherSuite ;
99import org .bouncycastle .tls .ContentType ;
1010import org .bouncycastle .tls .DTLSClientProtocol ;
11+ import org .bouncycastle .tls .DTLSRequest ;
1112import org .bouncycastle .tls .DTLSServerProtocol ;
1213import org .bouncycastle .tls .DTLSTransport ;
14+ import org .bouncycastle .tls .DTLSVerifier ;
1315import org .bouncycastle .tls .DatagramTransport ;
1416import org .bouncycastle .tls .ExtensionType ;
1517import org .bouncycastle .tls .HandshakeType ;
1921import org .bouncycastle .tls .TlsFatalAlert ;
2022import org .bouncycastle .tls .TlsFatalAlertReceived ;
2123import org .bouncycastle .tls .TlsServer ;
24+ import org .bouncycastle .tls .crypto .TlsCrypto ;
2225import org .bouncycastle .util .Arrays ;
26+ import org .bouncycastle .util .Strings ;
2327
2428import 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