@@ -66,6 +66,14 @@ public class DTLS13ProtocolTest
6666 private static final int UNIFIED_FLAG_LENGTH = 0x04 ;
6767 private static final int UNIFIED_EPOCH_BITS_MASK = 0x03 ;
6868
69+ /**
70+ * The MTU the handshakes here run at, and a smaller one at which the client's authenticated flight no
71+ * longer fits its records and has to be fragmented (RFC 9147 5.5). Both are above the 576-byte floor
72+ * RFC 9147 4.2 assumes for a path MTU estimate only in the first case: 512 is deliberately small.
73+ */
74+ private static final int DEFAULT_MTU = 1500 ;
75+ private static final int FRAGMENTING_MTU = 512 ;
76+
6977 /** RFC 9147 5.2. The DTLS handshake message header, which the wire keeps and the transcript omits. */
7078 private static final int MESSAGE_HEADER_LENGTH = 12 ;
7179
@@ -144,7 +152,8 @@ public void testClientServer() throws Exception
144152 * No CertificateRequest was sent, so the client was never asked for credentials and the server never
145153 * saw a Certificate message of any kind - not even the empty one a declining client would send.
146154 */
147- assertEquals ("the client was asked for a certificate" , 0 , harness .clientCertificateRequestsSeen );
155+ assertEquals ("CertificateRequests the client was asked to answer" , 0 ,
156+ harness .clientCertificateRequestsSeen );
148157 assertEquals ("the client sent a certificate of its own" , -1 , harness .clientLocalCertChainLength );
149158 assertEquals ("the server received a client Certificate message" , -1 , harness .serverPeerCertChainLength );
150159
@@ -395,20 +404,47 @@ public void testClientServerWithPacketLoss() throws Exception
395404 }
396405
397406 /**
398- * The authenticated client flight over a lossy path. It is the largest flight either peer sends - a real
399- * certificate chain plus a CertificateVerify over it - so it is the one whose retransmission can span
400- * several records, and the per-record epoch bookkeeping and the flight boundaries both have more to get
401- * wrong here than for a bare Finished. Loss in both directions is what makes any of that run.
407+ * The authenticated client flight, fragmented across several records and then lost. It is the largest
408+ * flight either peer sends - a real certificate chain plus a CertificateVerify over it - so it is the
409+ * only one the record layer has to fragment, and the per-fragment epoch bookkeeping and the flight
410+ * boundaries both have more to get wrong here than for a bare Finished.
411+ * <p>
412+ * The loss is deterministic rather than seeded-random: the datagram carrying the start of the flight is
413+ * dropped outright, the way {@link #testClientServerWithClientFinishedLost} drops the bare Finished, so
414+ * that the flight under test is certainly the thing that was lost and the drop is counted where the
415+ * test can see it. The same handshake is also run first at the normal MTU, purely to count how many
416+ * records the flight needs when nothing has to be fragmented: the two runs send the identical sequence
417+ * of handshake messages, so the extra records at the smaller MTU can only be fragmentation - which is
418+ * how fragmentation is established here without decrypting anything.
419+ * </p>
402420 */
403- public void testClientServerWithClientAuthenticationAndPacketLoss () throws Exception
421+ public void testClientServerWithClientAuthenticationFlightLost () throws Exception
404422 {
423+ Harness unfragmented = new Harness ();
424+ unfragmented .clientAuth = TlsTestConfig .CLIENT_AUTH_VALID ;
425+ unfragmented .serverCertReq = TlsTestConfig .SERVER_CERT_REQ_MANDATORY ;
426+
427+ unfragmented .run (16 );
428+
429+ assertNotNull ("no application data echoed back" , unfragmented .echo );
430+
431+ String unfragmentedEpochs = epochSequence (unfragmented .clientRecords ());
432+ int unfragmentedFlightRecords = countEpoch2BeforeFirstEpoch3 (unfragmentedEpochs );
433+
434+ assertTrue ("the authenticated flight was not sent at the handshake epoch (epochs: "
435+ + unfragmentedEpochs + ")" , unfragmentedFlightRecords > 0 );
436+
405437 Harness harness = new Harness ();
406438 harness .clientAuth = TlsTestConfig .CLIENT_AUTH_VALID ;
407439 harness .serverCertReq = TlsTestConfig .SERVER_CERT_REQ_MANDATORY ;
408- harness .loss = new UnreliableDatagramTransportFactory (new Random (0x2ADF1347L ), 10 , 10 );
440+ harness .networkMtu = FRAGMENTING_MTU ;
441+ harness .dropFirstClientEpoch2Datagram = true ;
409442
410443 harness .run (16 );
411444
445+ // The drop really fired, so nothing below can pass on a path that turned out to be lossless
446+ assertTrue ("the client's authenticated flight was not dropped" , harness .dropped > 0 );
447+
412448 assertEquals ("client negotiated version" , ProtocolVersion .DTLSv13 , harness .clientVersion );
413449 assertEquals ("server negotiated version" , ProtocolVersion .DTLSv13 , harness .serverVersion );
414450
@@ -428,9 +464,37 @@ public void testClientServerWithClientAuthenticationAndPacketLoss() throws Excep
428464 assertNotNull ("no application data echoed back" , harness .echo );
429465 assertTrue ("echoed application data differs" , Arrays .areEqual (harness .request , harness .echo ));
430466
467+ String clientEpochs = epochSequence (harness .clientRecords ());
468+ int flightRecords = countEpoch2BeforeFirstEpoch3 (clientEpochs );
469+
470+ /*
471+ * RFC 9147 5.5. The same messages took more records at the smaller MTU, so at least one of them was
472+ * fragmented - and the flight therefore spans several records, with a fragment_offset to get right
473+ * on each of them, which is the specific risk this test exists for.
474+ */
475+ assertTrue ("the reduced MTU did not fragment the authenticated flight (" + flightRecords
476+ + " records at MTU " + FRAGMENTING_MTU + " against " + unfragmentedFlightRecords + " at MTU "
477+ + DEFAULT_MTU + ")" , flightRecords > unfragmentedFlightRecords );
478+
479+ /*
480+ * And the whole of that fragmented flight came back after the client had moved on to the application
481+ * epoch: retransmission is at the epoch a fragment was first sent under, not at the current one.
482+ */
483+ assertTrue ("the client did not retransmit its whole authenticated flight after reaching the"
484+ + " application epoch (epochs: " + clientEpochs + ")" ,
485+ countEpoch2AfterFirstEpoch3 (clientEpochs ) >= flightRecords );
486+
431487 checkUnifiedHeadersAfterHello (harness .clientRecords (), "client" );
432488
433- checkEpochProgression (harness .clientRecords (), "client" );
489+ assertEquals ("the client's first protected record was not at the handshake epoch" , 2 ,
490+ firstProtectedEpoch (harness .clientRecords ()));
491+ assertEquals ("the client did not end at the application epoch" , 3 ,
492+ lastProtectedEpoch (harness .clientRecords ()));
493+
494+ /*
495+ * The server never winds its own write epoch back: it has not seen the client's Finished when it
496+ * retransmits, so its whole run is monotonic even though the client's is not.
497+ */
434498 checkEpochProgression (harness .serverRecords (), "server" );
435499 }
436500
@@ -1518,6 +1582,27 @@ private static int countEpoch2AfterFirstEpoch3(String epochs)
15181582 return count ;
15191583 }
15201584
1585+ /**
1586+ * How many epoch-2 records a side sent before the first epoch-3 one: the size, in records, of its
1587+ * original final flight.
1588+ */
1589+ private static int countEpoch2BeforeFirstEpoch3 (String epochs )
1590+ {
1591+ int epoch3 = epochs .indexOf ('3' );
1592+ int end = epoch3 < 0 ? epochs .length () : epoch3 ;
1593+
1594+ int count = 0 ;
1595+ for (int i = 0 ; i < end ; ++i )
1596+ {
1597+ if ('2' == epochs .charAt (i ))
1598+ {
1599+ ++count ;
1600+ }
1601+ }
1602+
1603+ return count ;
1604+ }
1605+
15211606 private static boolean isAllZeroes (byte [] bs )
15221607 {
15231608 for (int i = 0 ; i < bs .length ; ++i )
@@ -1595,6 +1680,7 @@ static class Harness
15951680 * longer read, so giving up is the only way out.
15961681 */
15971682 int serverHandshakeTimeoutMillis = HANDSHAKE_TIMEOUT_MILLIS ;
1683+ int networkMtu = DEFAULT_MTU ;
15981684 UnreliableDatagramTransportFactory loss = null ;
15991685
16001686 ProtocolVersion clientVersion = null ;
@@ -1986,7 +2072,7 @@ public void notifyHandshakeComplete() throws IOException
19862072 }
19872073 };
19882074
1989- MockDatagramAssociation network = new MockDatagramAssociation (1500 );
2075+ MockDatagramAssociation network = new MockDatagramAssociation (networkMtu );
19902076
19912077 DTLSServerProtocol serverProtocol = new DTLSServerProtocol ();
19922078
0 commit comments