Skip to content

Commit 91b6694

Browse files
tls13: srv: Simplify mbedtls_ssl_read_early_data() API
Do not progress the handshake in the API, just read early data if some has been detected by a previous call to mbedtls_ssl_handshake(), mbedtls_ssl_handshake_step(), mbedtls_ssl_read() or mbedtls_ssl_write(). Signed-off-by: Ronald Cron <[email protected]>
1 parent 1947b6b commit 91b6694

File tree

3 files changed

+18
-72
lines changed

3 files changed

+18
-72
lines changed

include/mbedtls/ssl.h

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5132,49 +5132,25 @@ int mbedtls_ssl_close_notify(mbedtls_ssl_context *ssl);
51325132
* same warnings apply to any use of the
51335133
* early_exporter_master_secret."
51345134
*
5135-
* \note This function behaves mainly as mbedtls_ssl_read(). The
5136-
* specification of mbedtls_ssl_read() relevant to TLS 1.3
5137-
* (thus not the parts specific to (D)TLS 1.2) applies to this
5138-
* function and the present documentation is restricted to the
5139-
* differences with mbedtls_ssl_read().
5140-
*
5141-
* \note This function can be used in conjunction with
5135+
* \note This function is used in conjunction with
51425136
* mbedtls_ssl_handshake(), mbedtls_ssl_handshake_step(),
51435137
* mbedtls_ssl_read() and mbedtls_ssl_write() to read early
51445138
* data when these functions return
51455139
* #MBEDTLS_ERR_SSL_RECEIVED_EARLY_DATA.
51465140
*
5147-
* \param ssl SSL context
5141+
* \param ssl SSL context, it must have been initialized and set up.
51485142
* \param buf buffer that will hold the data
51495143
* \param len maximum number of bytes to read
51505144
*
5151-
* \note Unlike mbedtls_ssl_read(), this function does not return
5145+
* \return The (positive) number of bytes read if successful.
5146+
* \return #MBEDTLS_ERR_SSL_BAD_INPUT_DATA if input data is invalid.
5147+
* \return #MBEDTLS_ERR_SSL_CANNOT_READ_EARLY_DATA if it is not
5148+
* possible to read early data for the SSL context \p ssl. Note
5149+
* that this function is intended to be called for an SSL
5150+
* context \p ssl only after a call to mbedtls_ssl_handshake(),
5151+
* mbedtls_ssl_handshake_step(), mbedtls_ssl_read() or
5152+
* mbedtls_ssl_write() for \p ssl that has returned
51525153
* #MBEDTLS_ERR_SSL_RECEIVED_EARLY_DATA.
5153-
*
5154-
* \return One additional specific return value:
5155-
* #MBEDTLS_ERR_SSL_CANNOT_READ_EARLY_DATA.
5156-
*
5157-
* #MBEDTLS_ERR_SSL_CANNOT_READ_EARLY_DATA is returned when it
5158-
* is not possible to read early data for the SSL context
5159-
* \p ssl.
5160-
*
5161-
* It may have been possible and it is not possible
5162-
* anymore because the server received the End of Early Data
5163-
* message or the maximum number of allowed early data for the
5164-
* PSK in use has been reached.
5165-
*
5166-
* It may never have been possible and will never be possible
5167-
* for the SSL context \p ssl because the use of early data
5168-
* is disabled for that context or more generally the context
5169-
* is not suitably configured to enable early data or the
5170-
* client does not use early data or the first call to the
5171-
* function was done while the handshake was already too
5172-
* advanced to gather and accept early data.
5173-
*
5174-
* It is not possible to read early data for the SSL context
5175-
* \p ssl but this does not preclude for using it with
5176-
* mbedtls_ssl_write(), mbedtls_ssl_read() or
5177-
* mbedtls_ssl_handshake().
51785154
*/
51795155
int mbedtls_ssl_read_early_data(mbedtls_ssl_context *ssl,
51805156
unsigned char *buf, size_t len);

library/ssl_msg.c

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5865,54 +5865,20 @@ int mbedtls_ssl_read(mbedtls_ssl_context *ssl, unsigned char *buf, size_t len)
58655865
return ret;
58665866
}
58675867

5868-
58695868
#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_EARLY_DATA)
58705869
int mbedtls_ssl_read_early_data(mbedtls_ssl_context *ssl,
58715870
unsigned char *buf, size_t len)
58725871
{
5873-
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5874-
const struct mbedtls_ssl_config *conf;
5875-
unsigned char *p = buf;
5876-
5877-
if (ssl == NULL || ((conf = ssl->conf) == NULL)) {
5872+
if (ssl == NULL || (ssl->conf == NULL)) {
58785873
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
58795874
}
58805875

5881-
if ((!mbedtls_ssl_conf_is_tls13_enabled(conf)) ||
5882-
(conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
5883-
(conf->early_data_enabled != MBEDTLS_SSL_EARLY_DATA_ENABLED)) {
5884-
return MBEDTLS_ERR_SSL_CANNOT_READ_EARLY_DATA;
5885-
}
5886-
5887-
if (ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_3) {
5888-
return MBEDTLS_ERR_SSL_CANNOT_READ_EARLY_DATA;
5889-
}
5890-
5891-
if ((ssl->early_data_state.srv !=
5892-
MBEDTLS_SSL_SRV_EARLY_DATA_STATE_WAITING_CH) &&
5893-
(ssl->early_data_state.srv !=
5894-
MBEDTLS_SSL_SRV_EARLY_DATA_STATE_ACCEPTING)) {
5895-
return MBEDTLS_ERR_SSL_CANNOT_READ_EARLY_DATA;
5896-
}
5897-
5898-
ret = mbedtls_ssl_handshake(ssl);
5899-
if (ret == MBEDTLS_ERR_SSL_RECEIVED_EARLY_DATA) {
5900-
if (ssl->in_offt == NULL) {
5901-
/* Set the reading pointer */
5902-
ssl->in_offt = ssl->in_msg;
5903-
}
5904-
ret = ssl_read_application_data(ssl, p, len);
5905-
} else if (ret == 0) {
5906-
/*
5907-
* If the handshake is completed, return immediately that early data
5908-
* cannot be read anymore. This potentially saves another call to this
5909-
* API and when the function returns 0, it only means that zero byte
5910-
* of early data has been received.
5911-
*/
5876+
if ((ssl->state != MBEDTLS_SSL_END_OF_EARLY_DATA) ||
5877+
(ssl->in_offt == NULL)) {
59125878
return MBEDTLS_ERR_SSL_CANNOT_READ_EARLY_DATA;
59135879
}
59145880

5915-
return ret;
5881+
return ssl_read_application_data(ssl, buf, len);
59165882
}
59175883
#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_EARLY_DATA */
59185884

library/ssl_tls13_server.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2909,6 +2909,10 @@ static int ssl_tls13_end_of_early_data_coordinate(mbedtls_ssl_context *ssl)
29092909
*
29102910
* TODO: Add received data size check here.
29112911
*/
2912+
if (ssl->in_offt == NULL) {
2913+
/* Set the reading pointer */
2914+
ssl->in_offt = ssl->in_msg;
2915+
}
29122916
return SSL_GOT_EARLY_DATA;
29132917
}
29142918

0 commit comments

Comments
 (0)