Skip to content

Commit 94f5948

Browse files
Merge pull request #8858 from rizlik/dtls13_set_epoch_fix
dtls13: move Dtls13NewEpoch into DeriveTls13Keys
2 parents 81852e7 + 3e6703e commit 94f5948

5 files changed

Lines changed: 111 additions & 53 deletions

File tree

src/dtls13.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2618,19 +2618,16 @@ static int Dtls13RtxIsTrackedByRn(const Dtls13RtxRecord* r, w64wrapper epoch,
26182618
static int Dtls13KeyUpdateAckReceived(WOLFSSL* ssl)
26192619
{
26202620
int ret;
2621-
w64Increment(&ssl->dtls13Epoch);
2622-
2623-
/* Epoch wrapped up */
2624-
if (w64IsZero(ssl->dtls13Epoch))
2625-
return BAD_STATE_E;
26262621

26272622
ret = DeriveTls13Keys(ssl, update_traffic_key, ENCRYPT_SIDE_ONLY, 1);
26282623
if (ret != 0)
26292624
return ret;
26302625

2631-
ret = Dtls13NewEpoch(ssl, ssl->dtls13Epoch, ENCRYPT_SIDE_ONLY);
2632-
if (ret != 0)
2633-
return ret;
2626+
w64Increment(&ssl->dtls13Epoch);
2627+
2628+
/* Epoch wrapped up */
2629+
if (w64IsZero(ssl->dtls13Epoch))
2630+
return BAD_STATE_E;
26342631

26352632
return Dtls13SetEpochKeys(ssl, ssl->dtls13Epoch, ENCRYPT_SIDE_ONLY);
26362633
}

src/tls13.c

Lines changed: 34 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1623,9 +1623,42 @@ int DeriveTls13Keys(WOLFSSL* ssl, int secret, int side, int store)
16231623
goto end;
16241624

16251625
if (ssl->options.dtls) {
1626+
w64wrapper epochNumber;
16261627
ret = Dtls13DeriveSnKeys(ssl, provision);
16271628
if (ret != 0)
1628-
return ret;
1629+
goto end;
1630+
1631+
switch (secret) {
1632+
case early_data_key:
1633+
epochNumber = w64From32(0, DTLS13_EPOCH_EARLYDATA);
1634+
break;
1635+
case handshake_key:
1636+
epochNumber = w64From32(0, DTLS13_EPOCH_HANDSHAKE);
1637+
break;
1638+
case traffic_key:
1639+
case no_key:
1640+
epochNumber = w64From32(0, DTLS13_EPOCH_TRAFFIC0);
1641+
break;
1642+
case update_traffic_key:
1643+
if (side == ENCRYPT_SIDE_ONLY) {
1644+
epochNumber = ssl->dtls13Epoch;
1645+
}
1646+
else if (side == DECRYPT_SIDE_ONLY) {
1647+
epochNumber = ssl->dtls13PeerEpoch;
1648+
}
1649+
else {
1650+
ret = BAD_STATE_E;
1651+
goto end;
1652+
}
1653+
w64Increment(&epochNumber);
1654+
break;
1655+
default:
1656+
ret = BAD_STATE_E;
1657+
goto end;
1658+
}
1659+
ret = Dtls13NewEpoch(ssl, epochNumber, side);
1660+
if (ret != 0)
1661+
goto end;
16291662
}
16301663

16311664
#endif /* WOLFSSL_DTLS13 */
@@ -4083,15 +4116,6 @@ static int WritePSKBinders(WOLFSSL* ssl, byte* output, word32 idx)
40834116
if ((ret = SetKeysSide(ssl, ENCRYPT_SIDE_ONLY)) != 0)
40844117
return ret;
40854118

4086-
#ifdef WOLFSSL_DTLS13
4087-
if (ssl->options.dtls) {
4088-
ret = Dtls13NewEpoch(
4089-
ssl, w64From32(0x0, DTLS13_EPOCH_EARLYDATA), ENCRYPT_SIDE_ONLY);
4090-
if (ret != 0)
4091-
return ret;
4092-
}
4093-
#endif /* WOLFSSL_DTLS13 */
4094-
40954119
}
40964120
#endif
40974121

@@ -6296,17 +6320,6 @@ static int CheckPreSharedKeys(WOLFSSL* ssl, const byte* input, word32 helloSz,
62966320
return ret;
62976321

62986322
ssl->keys.encryptionOn = 1;
6299-
6300-
#ifdef WOLFSSL_DTLS13
6301-
if (ssl->options.dtls) {
6302-
ret = Dtls13NewEpoch(ssl,
6303-
w64From32(0x0, DTLS13_EPOCH_EARLYDATA),
6304-
DECRYPT_SIDE_ONLY);
6305-
if (ret != 0)
6306-
return ret;
6307-
}
6308-
#endif /* WOLFSSL_DTLS13 */
6309-
63106323
ssl->earlyData = process_early_data;
63116324
}
63126325
else
@@ -7604,11 +7617,6 @@ static int SendTls13EncryptedExtensions(WOLFSSL* ssl)
76047617
w64wrapper epochHandshake = w64From32(0, DTLS13_EPOCH_HANDSHAKE);
76057618
ssl->dtls13Epoch = epochHandshake;
76067619

7607-
ret = Dtls13NewEpoch(
7608-
ssl, epochHandshake, ENCRYPT_AND_DECRYPT_SIDE);
7609-
if (ret != 0)
7610-
return ret;
7611-
76127620
ret = Dtls13SetEpochKeys(
76137621
ssl, epochHandshake, ENCRYPT_AND_DECRYPT_SIDE);
76147622
if (ret != 0)
@@ -11194,11 +11202,6 @@ static int SendTls13Finished(WOLFSSL* ssl)
1119411202
ssl->dtls13Epoch = epochTraffic0;
1119511203
ssl->dtls13PeerEpoch = epochTraffic0;
1119611204

11197-
ret = Dtls13NewEpoch(
11198-
ssl, epochTraffic0, ENCRYPT_AND_DECRYPT_SIDE);
11199-
if (ret != 0)
11200-
return ret;
11201-
1120211205
ret = Dtls13SetEpochKeys(
1120311206
ssl, epochTraffic0, ENCRYPT_AND_DECRYPT_SIDE);
1120411207
if (ret != 0)
@@ -11236,11 +11239,6 @@ static int SendTls13Finished(WOLFSSL* ssl)
1123611239
ssl->dtls13Epoch = epochTraffic0;
1123711240
ssl->dtls13PeerEpoch = epochTraffic0;
1123811241

11239-
ret = Dtls13NewEpoch(
11240-
ssl, epochTraffic0, ENCRYPT_AND_DECRYPT_SIDE);
11241-
if (ret != 0)
11242-
return ret;
11243-
1124411242
ret = Dtls13SetEpochKeys(
1124511243
ssl, epochTraffic0, ENCRYPT_AND_DECRYPT_SIDE);
1124611244
if (ret != 0)
@@ -11440,10 +11438,6 @@ static int DoTls13KeyUpdate(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
1144011438
if (ssl->options.dtls) {
1144111439
w64Increment(&ssl->dtls13PeerEpoch);
1144211440

11443-
ret = Dtls13NewEpoch(ssl, ssl->dtls13PeerEpoch, DECRYPT_SIDE_ONLY);
11444-
if (ret != 0)
11445-
return ret;
11446-
1144711441
ret = Dtls13SetEpochKeys(ssl, ssl->dtls13PeerEpoch, DECRYPT_SIDE_ONLY);
1144811442
if (ret != 0)
1144911443
return ret;
@@ -12859,11 +12853,6 @@ int DoTls13HandShakeMsgType(WOLFSSL* ssl, byte* input, word32* inOutIdx,
1285912853
ssl->dtls13Epoch = epochHandshake;
1286012854
ssl->dtls13PeerEpoch = epochHandshake;
1286112855

12862-
ret = Dtls13NewEpoch(
12863-
ssl, epochHandshake, ENCRYPT_AND_DECRYPT_SIDE);
12864-
if (ret != 0)
12865-
return ret;
12866-
1286712856
ret = Dtls13SetEpochKeys(
1286812857
ssl, epochHandshake, ENCRYPT_AND_DECRYPT_SIDE);
1286912858
if (ret != 0)

tests/api.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68282,6 +68282,7 @@ TEST_CASE testCases[] = {
6828268282
TEST_DECL(test_wolfSSL_inject),
6828368283
TEST_DECL(test_wolfSSL_dtls_cid_parse),
6828468284
TEST_DECL(test_dtls13_epochs),
68285+
TEST_DECL(test_dtls_rtx_across_epoch_change),
6828568286
TEST_DECL(test_dtls13_ack_order),
6828668287
TEST_DECL(test_dtls_version_checking),
6828768288
TEST_DECL(test_ocsp_status_callback),

tests/api/test_dtls.c

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,3 +1313,73 @@ int test_records_span_network_boundaries(void)
13131313
}
13141314
#endif /* defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
13151315
!defined(WOLFSSL_NO_TLS12) */
1316+
1317+
int test_dtls_rtx_across_epoch_change(void)
1318+
{
1319+
EXPECT_DECLS;
1320+
#if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
1321+
defined(WOLFSSL_DTLS13) && defined(WOLFSSL_DTLS)
1322+
WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
1323+
WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
1324+
struct test_memio_ctx test_ctx;
1325+
1326+
XMEMSET(&test_ctx, 0, sizeof(test_ctx));
1327+
1328+
/* Setup DTLS contexts */
1329+
ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
1330+
wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method),
1331+
0);
1332+
1333+
/* CH0 */
1334+
wolfSSL_SetLoggingPrefix("client:");
1335+
ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
1336+
ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), SSL_ERROR_WANT_READ);
1337+
1338+
/* HRR */
1339+
wolfSSL_SetLoggingPrefix("server:");
1340+
ExpectIntEQ(wolfSSL_accept(ssl_s), -1);
1341+
ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), SSL_ERROR_WANT_READ);
1342+
1343+
/* CH1 */
1344+
wolfSSL_SetLoggingPrefix("client:");
1345+
ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
1346+
ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), SSL_ERROR_WANT_READ);
1347+
1348+
/* SH ... FINISHED */
1349+
wolfSSL_SetLoggingPrefix("server:");
1350+
ExpectIntEQ(wolfSSL_accept(ssl_s), -1);
1351+
ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), SSL_ERROR_WANT_READ);
1352+
1353+
/* we should have now SH ... FINISHED messages in the buffer*/
1354+
ExpectIntGE(test_ctx.c_msg_count, 2);
1355+
1356+
/* drop everything but the SH */
1357+
while (test_ctx.c_msg_count > 1 && EXPECT_SUCCESS()) {
1358+
ExpectIntEQ(test_memio_drop_message(&test_ctx, 1, test_ctx.c_msg_count - 1), 0);
1359+
}
1360+
1361+
/* Read the SH */
1362+
wolfSSL_SetLoggingPrefix("client:");
1363+
ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
1364+
ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), SSL_ERROR_WANT_READ);
1365+
1366+
/* trigger client timeout */
1367+
ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
1368+
/* this should have triggered a rtx */
1369+
ExpectIntGT(test_ctx.s_msg_count, 0);
1370+
1371+
/* finish the handshake */
1372+
ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
1373+
1374+
/* Test communication works correctly */
1375+
ExpectIntEQ(test_dtls_communication(ssl_s, ssl_c), TEST_SUCCESS);
1376+
1377+
/* Cleanup */
1378+
wolfSSL_free(ssl_c);
1379+
wolfSSL_CTX_free(ctx_c);
1380+
wolfSSL_free(ssl_s);
1381+
wolfSSL_CTX_free(ctx_s);
1382+
#endif /* defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
1383+
defined(WOLFSSL_DTLS13) */
1384+
return EXPECT_RESULT();
1385+
}

tests/api/test_dtls.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,5 @@ int test_dtls13_longer_length(void);
3636
int test_dtls13_short_read(void);
3737
int test_records_span_network_boundaries(void);
3838
int test_dtls_record_cross_boundaries(void);
39+
int test_dtls_rtx_across_epoch_change(void);
3940
#endif /* TESTS_API_DTLS_H */

0 commit comments

Comments
 (0)