@@ -115,9 +115,16 @@ public class DTLS13ProtocolTest
115115 private static final int MANGLE_COOKIE = 1 ;
116116 private static final int MANGLE_RANDOM = 2 ;
117117
118+ /**
119+ * The plain baseline: a certificate-based DTLS 1.3 handshake with no CertificateRequest at all, which is
120+ * the shortest flight shape the protocol has and the one that leaves 'state.certificateRequest' null on
121+ * both peers. Every other test here has the server ask for a certificate, optionally or otherwise, so
122+ * without this one that branch would go unexercised.
123+ */
118124 public void testClientServer () throws Exception
119125 {
120126 Harness harness = new Harness ();
127+ harness .serverCertReq = TlsTestConfig .SERVER_CERT_REQ_NONE ;
121128
122129 harness .run (16 );
123130
@@ -133,6 +140,14 @@ public void testClientServer() throws Exception
133140 assertNotNull ("no application data echoed back" , harness .echo );
134141 assertTrue ("echoed application data differs" , Arrays .areEqual (harness .request , harness .echo ));
135142
143+ /*
144+ * No CertificateRequest was sent, so the client was never asked for credentials and the server never
145+ * saw a Certificate message of any kind - not even the empty one a declining client would send.
146+ */
147+ assertEquals ("the client was asked for a certificate" , 0 , harness .clientCertificateRequestsSeen );
148+ assertEquals ("the client sent a certificate of its own" , -1 , harness .clientLocalCertChainLength );
149+ assertEquals ("the server received a client Certificate message" , -1 , harness .serverPeerCertChainLength );
150+
136151 checkClientHello (harness .clientRecords ());
137152 checkServerHello (harness .serverRecords ());
138153
@@ -292,9 +307,15 @@ public void testClientServerWithClientFinishedLost() throws Exception
292307
293308 assertTrue ("the client's final flight was not dropped" , harness .dropped > 0 );
294309
295- int clientEpoch2Records = countRecordsAtEpoch (harness .clientRecords (), 2 );
296- assertTrue ("the client did not retransmit its final flight (epoch 2 records: " + clientEpoch2Records
297- + ")" , clientEpoch2Records >= 2 );
310+ String clientEpochs = epochSequence (harness .clientRecords ());
311+
312+ /*
313+ * The flight is a Certificate and a Finished, so two epoch-2 records, and both of them must come back:
314+ * counting epoch-2 records from the first epoch-3 record onwards is what distinguishes a
315+ * retransmission from the original flight, which is already two epoch-2 records of its own.
316+ */
317+ assertTrue ("the client did not retransmit its whole final flight after reaching the application epoch"
318+ + " (epochs: " + clientEpochs + ")" , countEpoch2AfterFirstEpoch3 (clientEpochs ) >= 2 );
298319
299320 checkUnifiedHeadersAfterHello (harness .clientRecords (), "client" );
300321
@@ -307,8 +328,6 @@ public void testClientServerWithClientFinishedLost() throws Exception
307328 assertEquals ("the client's first protected record was not at the handshake epoch" , 2 ,
308329 firstProtectedEpoch (harness .clientRecords ()));
309330
310- String clientEpochs = epochSequence (harness .clientRecords ());
311-
312331 assertTrue ("the client did not retransmit at the handshake epoch after reaching epoch 3 (epochs: "
313332 + clientEpochs + ")" , lastProtectedEpoch (harness .clientRecords ()) == 3
314333 && retransmittedAtHandshakeEpochAfterApplicationEpoch (clientEpochs ));
@@ -375,6 +394,46 @@ public void testClientServerWithPacketLoss() throws Exception
375394 assertTrue ("echoed application data differs" , Arrays .areEqual (harness .request , harness .echo ));
376395 }
377396
397+ /**
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.
402+ */
403+ public void testClientServerWithClientAuthenticationAndPacketLoss () throws Exception
404+ {
405+ Harness harness = new Harness ();
406+ harness .clientAuth = TlsTestConfig .CLIENT_AUTH_VALID ;
407+ harness .serverCertReq = TlsTestConfig .SERVER_CERT_REQ_MANDATORY ;
408+ harness .loss = new UnreliableDatagramTransportFactory (new Random (0x2ADF1347L ), 10 , 10 );
409+
410+ harness .run (16 );
411+
412+ assertEquals ("client negotiated version" , ProtocolVersion .DTLSv13 , harness .clientVersion );
413+ assertEquals ("server negotiated version" , ProtocolVersion .DTLSv13 , harness .serverVersion );
414+
415+ assertTrue ("the client did not send a certificate of its own" , harness .clientLocalCertChainLength > 0 );
416+ assertEquals ("the chain the server received is not the one the client sent" ,
417+ harness .clientLocalCertChainLength , harness .serverPeerCertChainLength );
418+
419+ /*
420+ * The transcripts agreed all the way through the client's Certificate and CertificateVerify despite
421+ * the loss: a flight reassembled wrongly, or digested at the wrong boundary, would fail here.
422+ */
423+ assertTrue ("the server did not verify the client's Finished over the same transcript" ,
424+ Arrays .areEqual (harness .clientLocalVerifyData , harness .serverPeerVerifyData ));
425+ assertTrue ("the client did not verify the server's Finished over the same transcript" ,
426+ Arrays .areEqual (harness .serverLocalVerifyData , harness .clientPeerVerifyData ));
427+
428+ assertNotNull ("no application data echoed back" , harness .echo );
429+ assertTrue ("echoed application data differs" , Arrays .areEqual (harness .request , harness .echo ));
430+
431+ checkUnifiedHeadersAfterHello (harness .clientRecords (), "client" );
432+
433+ checkEpochProgression (harness .clientRecords (), "client" );
434+ checkEpochProgression (harness .serverRecords (), "server" );
435+ }
436+
378437 /**
379438 * RFC 8446 4.4.1. A HelloRetryRequest changes how the transcript is computed: the first ClientHello is
380439 * replaced by a synthetic "message_hash" message. The handshake must complete after the retry, and the
@@ -1434,6 +1493,31 @@ private static int lastProtectedEpoch(Vector records)
14341493 return epochs .length () < 1 ? -1 : epochs .charAt (epochs .length () - 1 ) - '0' ;
14351494 }
14361495
1496+ /**
1497+ * How many epoch-2 records appear after the first epoch-3 one. A side's original final flight is already
1498+ * several epoch-2 records, so only the ones after it has moved on to the application epoch can be a
1499+ * retransmission of that flight, and the count says how much of the flight came back.
1500+ */
1501+ private static int countEpoch2AfterFirstEpoch3 (String epochs )
1502+ {
1503+ int epoch3 = epochs .indexOf ('3' );
1504+ if (epoch3 < 0 )
1505+ {
1506+ return 0 ;
1507+ }
1508+
1509+ int count = 0 ;
1510+ for (int i = epoch3 + 1 ; i < epochs .length (); ++i )
1511+ {
1512+ if ('2' == epochs .charAt (i ))
1513+ {
1514+ ++count ;
1515+ }
1516+ }
1517+
1518+ return count ;
1519+ }
1520+
14371521 private static boolean isAllZeroes (byte [] bs )
14381522 {
14391523 for (int i = 0 ; i < bs .length ; ++i )
0 commit comments