@@ -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+
43254345int 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 */
1498414997int 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)
0 commit comments