Skip to content

Commit c87339e

Browse files
committed
dtls13: Add support for 0.5-RTT data
1 parent 2e89e46 commit c87339e

3 files changed

Lines changed: 55 additions & 10 deletions

File tree

src/internal.c

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19597,7 +19597,8 @@ int DoApplicationData(WOLFSSL* ssl, byte* input, word32* inOutIdx, int sniff)
1959719597
return BUFFER_ERROR;
1959819598
}
1959919599
#ifdef WOLFSSL_EARLY_DATA
19600-
if (ssl->earlyData > early_data_ext) {
19600+
if (ssl->options.side == WOLFSSL_SERVER_END &&
19601+
ssl->earlyData > early_data_ext) {
1960119602
if (ssl->earlyDataSz + dataSz > ssl->options.maxEarlyDataSz) {
1960219603
if (sniff == NO_SNIFF) {
1960319604
SendAlert(ssl, alert_fatal, unexpected_message);
@@ -19637,6 +19638,15 @@ int DoApplicationData(WOLFSSL* ssl, byte* input, word32* inOutIdx, int sniff)
1963719638
#endif
1963819639

1963919640
*inOutIdx = idx;
19641+
#ifdef WOLFSSL_DTLS13
19642+
if (ssl->options.connectState == WAIT_FINISHED_ACK) {
19643+
/* Reset the processReply state since
19644+
* we finished processing this message. */
19645+
ssl->options.processReply = doProcessInit;
19646+
/* DTLS 1.3 is waiting for an ACK but we can still return app data. */
19647+
return APP_DATA_READY;
19648+
}
19649+
#endif
1964019650
#ifdef HAVE_SECURE_RENEGOTIATION
1964119651
if (IsSCR(ssl)) {
1964219652
/* Reset the processReply state since
@@ -20234,7 +20244,7 @@ int ProcessReplyEx(WOLFSSL* ssl, int allowSocketErr)
2023420244
#endif
2023520245

2023620246
if (ssl->error != 0 && ssl->error != WANT_READ && ssl->error != WANT_WRITE
20237-
#ifdef HAVE_SECURE_RENEGOTIATION
20247+
#if defined(HAVE_SECURE_RENEGOTIATION) || defined(WOLFSSL_DTLS13)
2023820248
&& ssl->error != APP_DATA_READY
2023920249
#endif
2024020250
#ifdef WOLFSSL_ASYNC_CRYPT
@@ -21258,9 +21268,18 @@ int ProcessReplyEx(WOLFSSL* ssl, int allowSocketErr)
2125821268
/* input exhausted */
2125921269
if (ssl->buffers.inputBuffer.idx >= ssl->buffers.inputBuffer.length
2126021270
#ifdef WOLFSSL_DTLS
21261-
/* If app data was processed then return now to avoid
21262-
* dropping any app data. */
21263-
|| (ssl->options.dtls && ssl->curRL.type == application_data)
21271+
|| (ssl->options.dtls &&
21272+
/* If app data was processed then return now to avoid
21273+
* dropping any app data. */
21274+
(ssl->curRL.type == application_data ||
21275+
/* client: if we processed a finished message, return to
21276+
* allow higher layers to establish the crypto
21277+
* parameters of the connection. The remaining data
21278+
* may be app data that we would drop without the
21279+
* crypto setup. */
21280+
(ssl->options.side == WOLFSSL_CLIENT_END &&
21281+
ssl->options.serverState == SERVER_FINISHED_COMPLETE &&
21282+
ssl->options.handShakeState != HANDSHAKE_DONE)))
2126421283
#endif
2126521284
) {
2126621285
/* Shrink input buffer when we successfully finish record
@@ -23586,6 +23605,12 @@ int SendData(WOLFSSL* ssl, const void* data, int sz)
2358623605
groupMsgs = 1;
2358723606
#endif
2358823607
}
23608+
else if (IsAtLeastTLSv1_3(ssl->version) &&
23609+
ssl->options.side == WOLFSSL_SERVER_END &&
23610+
ssl->options.acceptState >= TLS13_ACCEPT_FINISHED_SENT) {
23611+
/* We can send data without waiting on peer finished msg */
23612+
WOLFSSL_MSG("server sending data before receiving client finished");
23613+
}
2358923614
else
2359023615
#endif
2359123616
if (ssl->options.handShakeState != HANDSHAKE_DONE && !IsSCR(ssl)) {
@@ -23823,7 +23848,7 @@ int ReceiveData(WOLFSSL* ssl, byte* output, int sz, int peek)
2382323848
#ifdef WOLFSSL_ASYNC_CRYPT
2382423849
&& ssl->error != WC_PENDING_E
2382523850
#endif
23826-
#ifdef HAVE_SECURE_RENEGOTIATION
23851+
#if defined(HAVE_SECURE_RENEGOTIATION) || defined(WOLFSSL_DTLS13)
2382723852
&& ssl->error != APP_DATA_READY
2382823853
#endif
2382923854
) {

src/ssl.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3233,8 +3233,14 @@ int wolfSSL_write(WOLFSSL* ssl, const void* data, int sz)
32333233
}
32343234
#endif
32353235
#ifdef WOLFSSL_EARLY_DATA
3236-
if (ssl->earlyData != no_early_data && (ret = wolfSSL_negotiate(ssl)) < 0) {
3237-
ssl->error = ret;
3236+
if (IsAtLeastTLSv1_3(ssl->version) &&
3237+
ssl->options.side == WOLFSSL_SERVER_END &&
3238+
ssl->options.acceptState >= TLS13_ACCEPT_FINISHED_SENT) {
3239+
/* We can send data without waiting on peer finished msg */
3240+
WOLFSSL_MSG("server sending data before receiving client finished");
3241+
}
3242+
else if (ssl->earlyData != no_early_data &&
3243+
(ret = wolfSSL_negotiate(ssl)) < 0) {
32383244
return WOLFSSL_FATAL_ERROR;
32393245
}
32403246
ssl->earlyData = no_early_data;

tests/api.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68124,6 +68124,7 @@ static int test_dtls13_early_data(void)
6812468124
char msg[] = "This is early data";
6812568125
char msg2[] = "This is client data";
6812668126
char msg3[] = "This is server data";
68127+
char msg4[] = "This is server immediate data";
6812768128
char msgBuf[50];
6812868129

6812968130
XMEMSET(&test_ctx, 0, sizeof(test_ctx));
@@ -68151,6 +68152,7 @@ static int test_dtls13_early_data(void)
6815168152
ExpectIntEQ(wolfSSL_disable_hrr_cookie(ssl_s), WOLFSSL_SUCCESS);
6815268153
#endif
6815368154

68155+
/* Test 0-RTT data */
6815468156
ExpectIntEQ(wolfSSL_write_early_data(ssl_c, msg, sizeof(msg),
6815568157
&written), sizeof(msg));
6815668158
ExpectIntEQ(written, sizeof(msg));
@@ -68160,6 +68162,15 @@ static int test_dtls13_early_data(void)
6816068162
ExpectIntEQ(read, sizeof(msg));
6816168163
ExpectStrEQ(msg, msgBuf);
6816268164

68165+
/* Test 0.5-RTT data */
68166+
ExpectIntEQ(wolfSSL_write(ssl_s, msg4, sizeof(msg4)), sizeof(msg4));
68167+
68168+
ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
68169+
ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), APP_DATA_READY);
68170+
68171+
ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), sizeof(msg4));
68172+
ExpectStrEQ(msg4, msgBuf);
68173+
6816368174
/* Complete handshake */
6816468175
ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
6816568176
ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
@@ -68171,11 +68182,14 @@ static int test_dtls13_early_data(void)
6817168182
* parsing logic. */
6817268183
ExpectFalse(wolfSSL_is_init_finished(ssl_s));
6817368184
ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
68174-
&read), WOLFSSL_FAILURE);
68175-
ExpectTrue(wolfSSL_is_init_finished(ssl_s));
68185+
&read), -1);
68186+
ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
6817668187

6817768188
ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
6817868189

68190+
ExpectTrue(wolfSSL_is_init_finished(ssl_s));
68191+
68192+
6817968193
/* Test bi-directional write */
6818068194
ExpectIntEQ(wolfSSL_write(ssl_c, msg2, sizeof(msg2)), sizeof(msg2));
6818168195
ExpectIntEQ(wolfSSL_read(ssl_s, msgBuf, sizeof(msgBuf)), sizeof(msg2));

0 commit comments

Comments
 (0)