mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-07-28 00:21:48 +03:00
Merge pull request #7455 from KloolK/record-size-limit/comply-with-limit
Comply with the received Record Size Limit extension
This commit is contained in:
@ -2455,6 +2455,7 @@ mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
|
||||
* uint8 ticket_flags;
|
||||
* opaque resumption_key<0..255>;
|
||||
* uint32 max_early_data_size;
|
||||
* uint16 record_size_limit;
|
||||
* select ( endpoint ) {
|
||||
* case client: ClientOnlyData;
|
||||
* case server: uint64 ticket_creation_time;
|
||||
@ -2490,6 +2491,9 @@ static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
|
||||
#if defined(MBEDTLS_SSL_EARLY_DATA)
|
||||
needed += 4; /* max_early_data_size */
|
||||
#endif
|
||||
#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
|
||||
needed += 2; /* record_size_limit */
|
||||
#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
|
||||
|
||||
#if defined(MBEDTLS_HAVE_TIME)
|
||||
needed += 8; /* ticket_creation_time or ticket_reception_time */
|
||||
@ -2534,6 +2538,10 @@ static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
|
||||
MBEDTLS_PUT_UINT32_BE(session->max_early_data_size, p, 0);
|
||||
p += 4;
|
||||
#endif
|
||||
#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
|
||||
MBEDTLS_PUT_UINT16_BE(session->record_size_limit, p, 0);
|
||||
p += 2;
|
||||
#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
|
||||
|
||||
#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
|
||||
if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
|
||||
@ -2610,6 +2618,13 @@ static int ssl_tls13_session_load(mbedtls_ssl_session *session,
|
||||
session->max_early_data_size = MBEDTLS_GET_UINT32_BE(p, 0);
|
||||
p += 4;
|
||||
#endif
|
||||
#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
|
||||
if (end - p < 2) {
|
||||
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
||||
}
|
||||
session->record_size_limit = MBEDTLS_GET_UINT16_BE(p, 0);
|
||||
p += 2;
|
||||
#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
|
||||
|
||||
#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
|
||||
if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
|
||||
@ -3372,6 +3387,31 @@ const char *mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl)
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
|
||||
|
||||
size_t mbedtls_ssl_get_output_record_size_limit(const mbedtls_ssl_context *ssl)
|
||||
{
|
||||
const size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
|
||||
size_t record_size_limit = max_len;
|
||||
|
||||
if (ssl->session != NULL &&
|
||||
ssl->session->record_size_limit >= MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN &&
|
||||
ssl->session->record_size_limit < max_len) {
|
||||
record_size_limit = ssl->session->record_size_limit;
|
||||
}
|
||||
|
||||
// TODO: this is currently untested
|
||||
/* During a handshake, use the value being negotiated */
|
||||
if (ssl->session_negotiate != NULL &&
|
||||
ssl->session_negotiate->record_size_limit >= MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN &&
|
||||
ssl->session_negotiate->record_size_limit < max_len) {
|
||||
record_size_limit = ssl->session_negotiate->record_size_limit;
|
||||
}
|
||||
|
||||
return record_size_limit;
|
||||
}
|
||||
#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
|
||||
|
||||
#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
|
||||
size_t mbedtls_ssl_get_input_max_frag_len(const mbedtls_ssl_context *ssl)
|
||||
{
|
||||
@ -3458,6 +3498,7 @@ int mbedtls_ssl_get_max_out_record_payload(const mbedtls_ssl_context *ssl)
|
||||
size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
|
||||
|
||||
#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
|
||||
!defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT) && \
|
||||
!defined(MBEDTLS_SSL_PROTO_DTLS)
|
||||
(void) ssl;
|
||||
#endif
|
||||
@ -3470,6 +3511,30 @@ int mbedtls_ssl_get_max_out_record_payload(const mbedtls_ssl_context *ssl)
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
|
||||
const size_t record_size_limit = mbedtls_ssl_get_output_record_size_limit(ssl);
|
||||
|
||||
if (max_len > record_size_limit) {
|
||||
max_len = record_size_limit;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (ssl->transform_out != NULL &&
|
||||
ssl->transform_out->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
|
||||
/* RFC 8449, section 4:
|
||||
*
|
||||
* This value [record_size_limit] is the length of the plaintext
|
||||
* of a protected record.
|
||||
* The value includes the content type and padding added in TLS 1.3
|
||||
* (that is, the complete length of TLSInnerPlaintext).
|
||||
*
|
||||
* Thus, round down to a multiple of MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY
|
||||
* and subtract 1 (for the content type that will be added later)
|
||||
*/
|
||||
max_len = ((max_len / MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY) *
|
||||
MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY) - 1;
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
||||
if (mbedtls_ssl_get_current_mtu(ssl) != 0) {
|
||||
const size_t mtu = mbedtls_ssl_get_current_mtu(ssl);
|
||||
@ -3492,7 +3557,8 @@ int mbedtls_ssl_get_max_out_record_payload(const mbedtls_ssl_context *ssl)
|
||||
#endif /* MBEDTLS_SSL_PROTO_DTLS */
|
||||
|
||||
#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
|
||||
!defined(MBEDTLS_SSL_PROTO_DTLS)
|
||||
!defined(MBEDTLS_SSL_PROTO_DTLS) && \
|
||||
!defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
|
||||
((void) ssl);
|
||||
#endif
|
||||
|
||||
|
Reference in New Issue
Block a user