@@ -6065,6 +6065,7 @@ int mbedtls_ssl_write_early_data(mbedtls_ssl_context *ssl,
60656065 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ;
60666066 const struct mbedtls_ssl_config * conf ;
60676067 int written_data_len = 0 ;
6068+ uint32_t remaining ;
60686069
60696070 MBEDTLS_SSL_DEBUG_MSG (2 , ("=> write early_data" ));
60706071
@@ -6114,31 +6115,46 @@ int mbedtls_ssl_write_early_data(mbedtls_ssl_context *ssl,
61146115 return ret ;
61156116 }
61166117 }
6118+ remaining = ssl -> session_negotiate -> max_early_data_size ;
61176119 } else {
61186120 /*
6119- * If we are past the point where we can send early data, return
6120- * immediatly. Otherwise, progress the handshake as much as possible to
6121- * not delay it too much. If we reach a point where we can still send
6122- * early data, then we will send some.
6121+ * If we are past the point where we can send early data or we have
6122+ * already reached the maximum early data size, return immediatly.
6123+ * Otherwise, progress the handshake as much as possible to not delay
6124+ * it too much. If we reach a point where we can still send early data,
6125+ * then we will send some.
61236126 */
61246127 if ((ssl -> early_data_status != MBEDTLS_SSL_EARLY_DATA_STATUS_CAN_WRITE ) &&
61256128 (ssl -> early_data_status != MBEDTLS_SSL_EARLY_DATA_STATUS_ACCEPTED )) {
61266129 return MBEDTLS_ERR_SSL_CANNOT_WRITE_EARLY_DATA ;
61276130 }
61286131
6132+ remaining = ssl -> session_negotiate -> max_early_data_size -
6133+ ssl -> early_data_count ;
6134+
6135+ if (remaining == 0 ) {
6136+ return MBEDTLS_ERR_SSL_CANNOT_WRITE_EARLY_DATA ;
6137+ }
6138+
61296139 ret = mbedtls_ssl_handshake (ssl );
61306140 if ((ret != 0 ) && (ret != MBEDTLS_ERR_SSL_WANT_READ )) {
61316141 MBEDTLS_SSL_DEBUG_RET (1 , "mbedtls_ssl_handshake" , ret );
61326142 return ret ;
61336143 }
61346144 }
61356145
6136- if ((ssl -> early_data_status != MBEDTLS_SSL_EARLY_DATA_STATUS_CAN_WRITE ) &&
6137- (ssl -> early_data_status != MBEDTLS_SSL_EARLY_DATA_STATUS_ACCEPTED )) {
6146+ if (((ssl -> early_data_status != MBEDTLS_SSL_EARLY_DATA_STATUS_CAN_WRITE ) &&
6147+ (ssl -> early_data_status != MBEDTLS_SSL_EARLY_DATA_STATUS_ACCEPTED ))
6148+ || (remaining == 0 )) {
61386149 return MBEDTLS_ERR_SSL_CANNOT_WRITE_EARLY_DATA ;
61396150 }
61406151
6152+ if (len > remaining ) {
6153+ len = remaining ;
6154+ }
6155+
61416156 written_data_len = ssl_write_real (ssl , buf , len );
6157+ ssl -> early_data_count += written_data_len ;
61426158
61436159 MBEDTLS_SSL_DEBUG_MSG (2 , ("<= write early_data, len=%d" , written_data_len ));
61446160
0 commit comments