Skip to content

Commit 97d9bfc

Browse files
authored
Merge pull request wolfSSL#9601 from rizlik/early_data_client_side_fixes
check that we are resuming in write_early_data + minor fixes
2 parents 71f3bd4 + 50b39c9 commit 97d9bfc

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed

doc/dox_comments/header_files/ssl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14430,6 +14430,8 @@ int wolfSSL_set_max_early_data(WOLFSSL* ssl, unsigned int sz);
1443014430
\return BAD_FUNC_ARG if a pointer parameter is NULL, sz is less than 0 or
1443114431
not using TLSv1.3.
1443214432
\return SIDE_ERROR if called with a server.
14433+
\return BAD_STATE_E if invoked without a valid session or without a valid
14434+
PSK cb
1443314435
\return WOLFSSL_FATAL_ERROR if the connection is not made.
1443414436
\return the amount of early data written in bytes if successful.
1443514437

src/tls13.c

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4322,6 +4322,26 @@ typedef struct Sch13Args {
43224322
#endif
43234323
} Sch13Args;
43244324

4325+
#ifdef WOLFSSL_EARLY_DATA
4326+
/* Check if early data can potentially be sent.
4327+
* Returns 1 if early data is possible, 0 otherwise.
4328+
*/
4329+
static int EarlyDataPossible(WOLFSSL* ssl)
4330+
{
4331+
/* Need session resumption OR PSK callback configured */
4332+
if (ssl->options.resuming) {
4333+
return 1;
4334+
}
4335+
#ifndef NO_PSK
4336+
if (ssl->options.client_psk_tls13_cb != NULL ||
4337+
ssl->options.client_psk_cb != NULL) {
4338+
return 1;
4339+
}
4340+
#endif
4341+
return 0;
4342+
}
4343+
#endif /* WOLFSSL_EARLY_DATA */
4344+
43254345
int SendTls13ClientHello(WOLFSSL* ssl)
43264346
{
43274347
int ret;
@@ -4461,14 +4481,8 @@ int SendTls13ClientHello(WOLFSSL* ssl)
44614481
case TLS_ASYNC_FINALIZE:
44624482
{
44634483
#ifdef WOLFSSL_EARLY_DATA
4464-
#ifndef NO_PSK
4465-
if (!ssl->options.resuming &&
4466-
ssl->options.client_psk_tls13_cb == NULL &&
4467-
ssl->options.client_psk_cb == NULL)
4468-
#else
4469-
if (!ssl->options.resuming)
4470-
#endif
4471-
ssl->earlyData = no_early_data;
4484+
if (!EarlyDataPossible(ssl))
4485+
ssl->earlyData = no_early_data;
44724486
if (ssl->options.serverState == SERVER_HELLO_RETRY_REQUEST_COMPLETE)
44734487
ssl->earlyData = no_early_data;
44744488
if (ssl->earlyData == no_early_data)
@@ -5744,15 +5758,13 @@ static int DoTls13EncryptedExtensions(WOLFSSL* ssl, const byte* input,
57445758
if (ext == NULL || !ext->val)
57455759
ssl->earlyData = no_early_data;
57465760
}
5747-
#endif
57485761

5749-
#ifdef WOLFSSL_EARLY_DATA
57505762
if (ssl->earlyData == no_early_data) {
57515763
ret = SetKeysSide(ssl, ENCRYPT_SIDE_ONLY);
57525764
if (ret != 0)
57535765
return ret;
57545766
}
5755-
#endif
5767+
#endif /* WOLFSSL_EARLY_DATA */
57565768

57575769
ssl->options.serverState = SERVER_ENCRYPTED_EXTENSIONS_COMPLETE;
57585770

@@ -14978,8 +14990,9 @@ int wolfSSL_get_max_early_data(WOLFSSL* ssl)
1497814990
* sz The size of the early data in bytes.
1497914991
* outSz The number of early data bytes written.
1498014992
* returns BAD_FUNC_ARG when: ssl, data or outSz is NULL; sz is negative;
14981-
* or not using TLS v1.3. SIDE ERROR when not a server. Otherwise the number of
14982-
* early data bytes written.
14993+
* or not using TLS v1.3. SIDE ERROR when not a server. BAD_STATE_E if invoked
14994+
* without a valid session or without a valid PSK CB.
14995+
* Otherwise the number of early data bytes written.
1498314996
*/
1498414997
int wolfSSL_write_early_data(WOLFSSL* ssl, const void* data, int sz, int* outSz)
1498514998
{
@@ -14996,8 +15009,15 @@ int wolfSSL_write_early_data(WOLFSSL* ssl, const void* data, int sz, int* outSz)
1499615009
if (ssl->options.side == WOLFSSL_SERVER_END)
1499715010
return SIDE_ERROR;
1499815011

15012+
/* Early data requires PSK or session resumption */
15013+
if (!EarlyDataPossible(ssl)) {
15014+
return BAD_STATE_E;
15015+
}
15016+
1499915017
if (ssl->options.handShakeState == NULL_STATE) {
15000-
if (ssl->error != WC_NO_ERR_TRACE(WC_PENDING_E))
15018+
/* avoid re-setting ssl->earlyData if we re-enter the function because
15019+
* of WC_PENDING_E, WANT_WRITE or WANT_READ */
15020+
if (ssl->error == 0)
1500115021
ssl->earlyData = expecting_early_data;
1500215022
ret = wolfSSL_connect_TLSv13(ssl);
1500315023
if (ret != WOLFSSL_SUCCESS)

tests/api/test_tls13.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -764,8 +764,9 @@ int test_tls13_apis(void)
764764
ExpectIntEQ(wolfSSL_write_early_data(clientTls12Ssl, earlyData,
765765
sizeof(earlyData), &outSz), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
766766
#endif
767+
/* invoking without session or psk cbs */
767768
ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
768-
sizeof(earlyData), &outSz), WC_NO_ERR_TRACE(WOLFSSL_FATAL_ERROR));
769+
sizeof(earlyData), &outSz), WC_NO_ERR_TRACE(BAD_STATE_E));
769770
#endif
770771

771772
ExpectIntEQ(wolfSSL_read_early_data(NULL, earlyDataBuffer,

0 commit comments

Comments
 (0)