You've already forked godot
mirror of
https://github.com/godotengine/godot.git
synced 2025-11-24 15:26:15 +00:00
[mbedTLS] Integrate TLS handshake defragmentation PR
Upstream PR GH-9981
This commit is contained in:
120
thirdparty/mbedtls/library/ssl_msg.c
vendored
120
thirdparty/mbedtls/library/ssl_msg.c
vendored
@@ -25,6 +25,7 @@
|
||||
#include "constant_time_internal.h"
|
||||
#include "mbedtls/constant_time.h"
|
||||
|
||||
#include <limits.h>
|
||||
#include <string.h>
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
@@ -3220,13 +3221,17 @@ static uint32_t ssl_get_hs_total_len(mbedtls_ssl_context const *ssl)
|
||||
|
||||
int mbedtls_ssl_prepare_handshake_record(mbedtls_ssl_context *ssl)
|
||||
{
|
||||
if (ssl->in_msglen < mbedtls_ssl_hs_hdr_len(ssl)) {
|
||||
/* First handshake fragment must at least include the header. */
|
||||
if (ssl->in_msglen < mbedtls_ssl_hs_hdr_len(ssl) && ssl->in_hslen == 0) {
|
||||
MBEDTLS_SSL_DEBUG_MSG(1, ("handshake message too short: %" MBEDTLS_PRINTF_SIZET,
|
||||
ssl->in_msglen));
|
||||
return MBEDTLS_ERR_SSL_INVALID_RECORD;
|
||||
}
|
||||
|
||||
ssl->in_hslen = mbedtls_ssl_hs_hdr_len(ssl) + ssl_get_hs_total_len(ssl);
|
||||
if (ssl->in_hslen == 0) {
|
||||
ssl->in_hslen = mbedtls_ssl_hs_hdr_len(ssl) + ssl_get_hs_total_len(ssl);
|
||||
ssl->badmac_seen_or_in_hsfraglen = 0;
|
||||
}
|
||||
|
||||
MBEDTLS_SSL_DEBUG_MSG(3, ("handshake message: msglen ="
|
||||
" %" MBEDTLS_PRINTF_SIZET ", type = %u, hslen = %"
|
||||
@@ -3292,10 +3297,67 @@ int mbedtls_ssl_prepare_handshake_record(mbedtls_ssl_context *ssl)
|
||||
}
|
||||
} else
|
||||
#endif /* MBEDTLS_SSL_PROTO_DTLS */
|
||||
/* With TLS we don't handle fragmentation (for now) */
|
||||
if (ssl->in_msglen < ssl->in_hslen) {
|
||||
MBEDTLS_SSL_DEBUG_MSG(1, ("TLS handshake fragmentation not supported"));
|
||||
return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
|
||||
if (ssl->badmac_seen_or_in_hsfraglen <= ssl->in_hslen) {
|
||||
int ret;
|
||||
const size_t hs_remain = ssl->in_hslen - ssl->badmac_seen_or_in_hsfraglen;
|
||||
MBEDTLS_SSL_DEBUG_MSG(3,
|
||||
("handshake fragment: %u .. %"
|
||||
MBEDTLS_PRINTF_SIZET " of %"
|
||||
MBEDTLS_PRINTF_SIZET " msglen %" MBEDTLS_PRINTF_SIZET,
|
||||
ssl->badmac_seen_or_in_hsfraglen,
|
||||
(size_t) ssl->badmac_seen_or_in_hsfraglen +
|
||||
(hs_remain <= ssl->in_msglen ? hs_remain : ssl->in_msglen),
|
||||
ssl->in_hslen, ssl->in_msglen));
|
||||
if (ssl->in_msglen < hs_remain) {
|
||||
/* ssl->in_msglen is a 25-bit value since it is the sum of the
|
||||
* header length plus the payload length, the header length is 4
|
||||
* and the payload length was received on the wire encoded as
|
||||
* 3 octets. We don't support 16-bit platforms; more specifically,
|
||||
* we assume that both unsigned and size_t are at least 32 bits.
|
||||
* Therefore there is no possible integer overflow here.
|
||||
*/
|
||||
ssl->badmac_seen_or_in_hsfraglen += (unsigned) ssl->in_msglen;
|
||||
ssl->in_hdr = ssl->in_msg + ssl->in_msglen;
|
||||
ssl->in_msglen = 0;
|
||||
mbedtls_ssl_update_in_pointers(ssl);
|
||||
return MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
|
||||
}
|
||||
if (ssl->badmac_seen_or_in_hsfraglen > 0) {
|
||||
/*
|
||||
* At in_first_hdr we have a sequence of records that cover the next handshake
|
||||
* record, each with its own record header that we need to remove.
|
||||
* Note that the reassembled record size may not equal the size of the message,
|
||||
* there may be more messages after it, complete or partial.
|
||||
*/
|
||||
unsigned char *in_first_hdr = ssl->in_buf + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
|
||||
unsigned char *p = in_first_hdr, *q = NULL;
|
||||
size_t merged_rec_len = 0;
|
||||
do {
|
||||
mbedtls_record rec;
|
||||
ret = ssl_parse_record_header(ssl, p, mbedtls_ssl_in_hdr_len(ssl), &rec);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
merged_rec_len += rec.data_len;
|
||||
p = rec.buf + rec.buf_len;
|
||||
if (q != NULL) {
|
||||
memmove(q, rec.buf + rec.data_offset, rec.data_len);
|
||||
q += rec.data_len;
|
||||
} else {
|
||||
q = p;
|
||||
}
|
||||
} while (merged_rec_len < ssl->in_hslen);
|
||||
ssl->in_hdr = in_first_hdr;
|
||||
mbedtls_ssl_update_in_pointers(ssl);
|
||||
ssl->in_msglen = merged_rec_len;
|
||||
/* Adjust message length. */
|
||||
MBEDTLS_PUT_UINT16_BE(merged_rec_len, ssl->in_len, 0);
|
||||
ssl->badmac_seen_or_in_hsfraglen = 0;
|
||||
MBEDTLS_SSL_DEBUG_BUF(4, "reassembled record",
|
||||
ssl->in_hdr, mbedtls_ssl_in_hdr_len(ssl) + merged_rec_len);
|
||||
}
|
||||
} else {
|
||||
return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
}
|
||||
|
||||
return 0;
|
||||
@@ -4640,6 +4702,16 @@ static int ssl_consume_current_message(mbedtls_ssl_context *ssl)
|
||||
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
||||
}
|
||||
|
||||
if (ssl->badmac_seen_or_in_hsfraglen != 0) {
|
||||
/* Not all handshake fragments have arrived, do not consume. */
|
||||
MBEDTLS_SSL_DEBUG_MSG(3,
|
||||
("waiting for more fragments (%u of %"
|
||||
MBEDTLS_PRINTF_SIZET ", %" MBEDTLS_PRINTF_SIZET " left)",
|
||||
ssl->badmac_seen_or_in_hsfraglen, ssl->in_hslen,
|
||||
ssl->in_hslen - ssl->badmac_seen_or_in_hsfraglen));
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Get next Handshake message in the current record
|
||||
*/
|
||||
@@ -4665,6 +4737,7 @@ static int ssl_consume_current_message(mbedtls_ssl_context *ssl)
|
||||
ssl->in_msglen -= ssl->in_hslen;
|
||||
memmove(ssl->in_msg, ssl->in_msg + ssl->in_hslen,
|
||||
ssl->in_msglen);
|
||||
MBEDTLS_PUT_UINT16_BE(ssl->in_msglen, ssl->in_len, 0);
|
||||
|
||||
MBEDTLS_SSL_DEBUG_BUF(4, "remaining content in record",
|
||||
ssl->in_msg, ssl->in_msglen);
|
||||
@@ -4967,10 +5040,12 @@ static int ssl_get_next_record(mbedtls_ssl_context *ssl)
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (ssl->conf->badmac_limit != 0 &&
|
||||
++ssl->badmac_seen >= ssl->conf->badmac_limit) {
|
||||
MBEDTLS_SSL_DEBUG_MSG(1, ("too many records with bad MAC"));
|
||||
return MBEDTLS_ERR_SSL_INVALID_MAC;
|
||||
if (ssl->conf->badmac_limit != 0) {
|
||||
++ssl->badmac_seen_or_in_hsfraglen;
|
||||
if (ssl->badmac_seen_or_in_hsfraglen >= ssl->conf->badmac_limit) {
|
||||
MBEDTLS_SSL_DEBUG_MSG(1, ("too many records with bad MAC"));
|
||||
return MBEDTLS_ERR_SSL_INVALID_MAC;
|
||||
}
|
||||
}
|
||||
|
||||
/* As above, invalid records cause
|
||||
@@ -5345,7 +5420,7 @@ void mbedtls_ssl_update_in_pointers(mbedtls_ssl_context *ssl)
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
ssl->in_ctr = ssl->in_hdr - MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
|
||||
ssl->in_ctr = ssl->in_buf;
|
||||
ssl->in_len = ssl->in_hdr + 3;
|
||||
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
|
||||
ssl->in_cid = ssl->in_len;
|
||||
@@ -5361,24 +5436,35 @@ void mbedtls_ssl_update_in_pointers(mbedtls_ssl_context *ssl)
|
||||
* Setup an SSL context
|
||||
*/
|
||||
|
||||
void mbedtls_ssl_reset_in_out_pointers(mbedtls_ssl_context *ssl)
|
||||
void mbedtls_ssl_reset_in_pointers(mbedtls_ssl_context *ssl)
|
||||
{
|
||||
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
||||
if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
|
||||
ssl->in_hdr = ssl->in_buf;
|
||||
} else
|
||||
#endif /* MBEDTLS_SSL_PROTO_DTLS */
|
||||
{
|
||||
ssl->in_hdr = ssl->in_buf + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
|
||||
}
|
||||
|
||||
/* Derive other internal pointers. */
|
||||
mbedtls_ssl_update_in_pointers(ssl);
|
||||
}
|
||||
|
||||
void mbedtls_ssl_reset_out_pointers(mbedtls_ssl_context *ssl)
|
||||
{
|
||||
/* Set the incoming and outgoing record pointers. */
|
||||
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
||||
if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
|
||||
ssl->out_hdr = ssl->out_buf;
|
||||
ssl->in_hdr = ssl->in_buf;
|
||||
} else
|
||||
#endif /* MBEDTLS_SSL_PROTO_DTLS */
|
||||
{
|
||||
ssl->out_ctr = ssl->out_buf;
|
||||
ssl->out_hdr = ssl->out_buf + 8;
|
||||
ssl->in_hdr = ssl->in_buf + 8;
|
||||
ssl->out_hdr = ssl->out_buf + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
|
||||
}
|
||||
|
||||
/* Derive other internal pointers. */
|
||||
mbedtls_ssl_update_out_pointers(ssl, NULL /* no transform enabled */);
|
||||
mbedtls_ssl_update_in_pointers(ssl);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user