Skip to content

Commit 53ef26b

Browse files
Merge pull request #6392 from rizlik/dtls13-fix-ch-epoch
DTLS v1.3: fix epoch 0 check on plaintext message
2 parents ae37fee + 5773252 commit 53ef26b

2 files changed

Lines changed: 79 additions & 5 deletions

File tree

src/internal.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10421,16 +10421,19 @@ static int GetDtlsRecordHeader(WOLFSSL* ssl, word32* inOutIdx,
1042110421
ENUM_LEN + VERSION_SZ);
1042210422
*inOutIdx += ENUM_LEN + VERSION_SZ;
1042310423
ato16(ssl->buffers.inputBuffer.buffer + *inOutIdx, &ssl->keys.curEpoch);
10424+
1042410425
#ifdef WOLFSSL_DTLS13
1042510426
/* only non protected message can use the DTLSPlaintext record header */
10426-
if (ssl->options.tls1_3 && ssl->keys.curEpoch != 0)
10427+
if (IsAtLeastTLSv1_3(ssl->version)) {
10428+
if (ssl->keys.curEpoch != 0)
1042710429
return SEQUENCE_ERROR;
1042810430

10429-
w64Zero(&ssl->keys.curEpoch64);
10430-
if (!w64IsZero(ssl->dtls13DecryptEpoch->epochNumber))
10431-
Dtls13SetEpochKeys(ssl, ssl->keys.curEpoch64, DECRYPT_SIDE_ONLY);
10432-
10431+
w64Zero(&ssl->keys.curEpoch64);
10432+
if (!w64IsZero(ssl->dtls13DecryptEpoch->epochNumber))
10433+
Dtls13SetEpochKeys(ssl, ssl->keys.curEpoch64, DECRYPT_SIDE_ONLY);
10434+
}
1043310435
#endif /* WOLFSSL_DTLS13 */
10436+
1043410437
*inOutIdx += OPAQUE16_LEN;
1043510438
if (ssl->options.haveMcast) {
1043610439
#ifdef WOLFSSL_MULTICAST

tests/api.c

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65778,6 +65778,76 @@ static int test_override_alt_cert_chain(void)
6577865778
}
6577965779
#endif
6578065780

65781+
#if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
65782+
65783+
65784+
static int test_dtls13_bad_epoch_ch(void)
65785+
{
65786+
WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
65787+
WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
65788+
struct test_memio_ctx test_ctx;
65789+
const int EPOCH_OFF = 3;
65790+
int ret, err;
65791+
65792+
XMEMSET(&test_ctx, 0, sizeof(test_ctx));
65793+
ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
65794+
wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method);
65795+
if (ret != 0)
65796+
return TEST_FAIL;
65797+
65798+
/* disable hrr cookie so we can later check msgsReceived.got_client_hello
65799+
* with just one message */
65800+
ret = wolfSSL_disable_hrr_cookie(ssl_s);
65801+
if (ret != WOLFSSL_SUCCESS)
65802+
return TEST_FAIL;
65803+
65804+
ret = wolfSSL_connect(ssl_c);
65805+
err = wolfSSL_get_error(ssl_c, ret);
65806+
if (ret == WOLFSSL_SUCCESS || err != WOLFSSL_ERROR_WANT_READ)
65807+
return TEST_FAIL;
65808+
65809+
if (test_ctx.s_len < EPOCH_OFF + 2)
65810+
return TEST_FAIL;
65811+
65812+
/* first CH should use epoch 0x0 */
65813+
if (test_ctx.s_buff[EPOCH_OFF] != 0x0 ||
65814+
test_ctx.s_buff[EPOCH_OFF + 1] != 0x0)
65815+
return TEST_FAIL;
65816+
65817+
/* change epoch to 2 */
65818+
test_ctx.s_buff[EPOCH_OFF + 1] = 0x2;
65819+
65820+
ret = wolfSSL_accept(ssl_s);
65821+
err = wolfSSL_get_error(ssl_s, ret);
65822+
if (ret == WOLFSSL_SUCCESS || err != WOLFSSL_ERROR_WANT_READ)
65823+
return TEST_FAIL;
65824+
65825+
if (ssl_s->msgsReceived.got_client_hello == 1)
65826+
return TEST_FAIL;
65827+
65828+
/* resend the CH */
65829+
ret = wolfSSL_dtls_got_timeout(ssl_c);
65830+
if (ret != WOLFSSL_SUCCESS)
65831+
return TEST_FAIL;
65832+
65833+
ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
65834+
if (ret != 0)
65835+
return TEST_FAIL;
65836+
65837+
wolfSSL_free(ssl_c);
65838+
wolfSSL_CTX_free(ctx_c);
65839+
wolfSSL_free(ssl_s);
65840+
wolfSSL_CTX_free(ctx_s);
65841+
65842+
return TEST_SUCCESS;
65843+
}
65844+
#else
65845+
static int test_dtls13_bad_epoch_ch(void)
65846+
{
65847+
return TEST_SKIPPED;
65848+
}
65849+
#endif
65850+
6578165851

6578265852
/*----------------------------------------------------------------------------*
6578365853
| Main
@@ -66815,6 +66885,7 @@ TEST_CASE testCases[] = {
6681566885
TEST_DECL(test_extra_alerts_bad_psk),
6681666886
TEST_DECL(test_harden_no_secure_renegotiation),
6681766887
TEST_DECL(test_override_alt_cert_chain),
66888+
TEST_DECL(test_dtls13_bad_epoch_ch),
6681866889
/* If at some point a stub get implemented this test should fail indicating
6681966890
* a need to implement a new test case
6682066891
*/

0 commit comments

Comments
 (0)