Skip to content

Commit 28f8e20

Browse files
authored
Merge pull request #9872 from rojer/tls_hs_defrag_in
Defragment incoming TLS handshake messages
2 parents 03e7040 + dd14c0a commit 28f8e20

File tree

6 files changed

+110
-41
lines changed

6 files changed

+110
-41
lines changed

ChangeLog.d/tls-hs-defrag-in.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Bugfix
2+
* Support re-assembly of fragmented handshake messages in TLS, as mandated
3+
by the spec. Lack of support was causing handshake failures with some
4+
servers, especially with TLS 1.3 in practice (though both protocol
5+
version could be affected in principle, and both are fixed now).

include/mbedtls/ssl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1795,6 +1795,8 @@ struct mbedtls_ssl_context {
17951795

17961796
size_t MBEDTLS_PRIVATE(in_hslen); /*!< current handshake message length,
17971797
including the handshake header */
1798+
size_t MBEDTLS_PRIVATE(in_hsfraglen); /*!< accumulated length of hs fragments
1799+
(up to in_hslen) */
17981800
int MBEDTLS_PRIVATE(nb_zero); /*!< # of 0-length encrypted messages */
17991801

18001802
int MBEDTLS_PRIVATE(keep_current_message); /*!< drop or reuse current message

library/ssl_misc.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1756,10 +1756,11 @@ void mbedtls_ssl_set_timer(mbedtls_ssl_context *ssl, uint32_t millisecs);
17561756
MBEDTLS_CHECK_RETURN_CRITICAL
17571757
int mbedtls_ssl_check_timer(mbedtls_ssl_context *ssl);
17581758

1759-
void mbedtls_ssl_reset_in_out_pointers(mbedtls_ssl_context *ssl);
1759+
void mbedtls_ssl_reset_in_pointers(mbedtls_ssl_context *ssl);
1760+
void mbedtls_ssl_update_in_pointers(mbedtls_ssl_context *ssl);
1761+
void mbedtls_ssl_reset_out_pointers(mbedtls_ssl_context *ssl);
17601762
void mbedtls_ssl_update_out_pointers(mbedtls_ssl_context *ssl,
17611763
mbedtls_ssl_transform *transform);
1762-
void mbedtls_ssl_update_in_pointers(mbedtls_ssl_context *ssl);
17631764

17641765
MBEDTLS_CHECK_RETURN_CRITICAL
17651766
int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial);

library/ssl_msg.c

Lines changed: 89 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2962,13 +2962,17 @@ static uint32_t ssl_get_hs_total_len(mbedtls_ssl_context const *ssl)
29622962

29632963
int mbedtls_ssl_prepare_handshake_record(mbedtls_ssl_context *ssl)
29642964
{
2965-
if (ssl->in_msglen < mbedtls_ssl_hs_hdr_len(ssl)) {
2965+
/* First handshake fragment must at least include the header. */
2966+
if (ssl->in_msglen < mbedtls_ssl_hs_hdr_len(ssl) && ssl->in_hslen == 0) {
29662967
MBEDTLS_SSL_DEBUG_MSG(1, ("handshake message too short: %" MBEDTLS_PRINTF_SIZET,
29672968
ssl->in_msglen));
29682969
return MBEDTLS_ERR_SSL_INVALID_RECORD;
29692970
}
29702971

2971-
ssl->in_hslen = mbedtls_ssl_hs_hdr_len(ssl) + ssl_get_hs_total_len(ssl);
2972+
if (ssl->in_hslen == 0) {
2973+
ssl->in_hslen = mbedtls_ssl_hs_hdr_len(ssl) + ssl_get_hs_total_len(ssl);
2974+
ssl->in_hsfraglen = 0;
2975+
}
29722976

29732977
MBEDTLS_SSL_DEBUG_MSG(3, ("handshake message: msglen ="
29742978
" %" MBEDTLS_PRINTF_SIZET ", type = %u, hslen = %"
@@ -3034,10 +3038,60 @@ int mbedtls_ssl_prepare_handshake_record(mbedtls_ssl_context *ssl)
30343038
}
30353039
} else
30363040
#endif /* MBEDTLS_SSL_PROTO_DTLS */
3037-
/* With TLS we don't handle fragmentation (for now) */
3038-
if (ssl->in_msglen < ssl->in_hslen) {
3039-
MBEDTLS_SSL_DEBUG_MSG(1, ("TLS handshake fragmentation not supported"));
3040-
return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3041+
if (ssl->in_hsfraglen <= ssl->in_hslen) {
3042+
int ret;
3043+
const size_t hs_remain = ssl->in_hslen - ssl->in_hsfraglen;
3044+
MBEDTLS_SSL_DEBUG_MSG(3,
3045+
("handshake fragment: %" MBEDTLS_PRINTF_SIZET " .. %"
3046+
MBEDTLS_PRINTF_SIZET " of %"
3047+
MBEDTLS_PRINTF_SIZET " msglen %" MBEDTLS_PRINTF_SIZET,
3048+
ssl->in_hsfraglen,
3049+
ssl->in_hsfraglen +
3050+
(hs_remain <= ssl->in_msglen ? hs_remain : ssl->in_msglen),
3051+
ssl->in_hslen, ssl->in_msglen));
3052+
if (ssl->in_msglen < hs_remain) {
3053+
ssl->in_hsfraglen += ssl->in_msglen;
3054+
ssl->in_hdr = ssl->in_msg + ssl->in_msglen;
3055+
ssl->in_msglen = 0;
3056+
mbedtls_ssl_update_in_pointers(ssl);
3057+
return MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
3058+
}
3059+
if (ssl->in_hsfraglen > 0) {
3060+
/*
3061+
* At in_first_hdr we have a sequence of records that cover the next handshake
3062+
* record, each with its own record header that we need to remove.
3063+
* Note that the reassembled record size may not equal the size of the message,
3064+
* there may be more messages after it, complete or partial.
3065+
*/
3066+
unsigned char *in_first_hdr = ssl->in_buf + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
3067+
unsigned char *p = in_first_hdr, *q = NULL;
3068+
size_t merged_rec_len = 0;
3069+
do {
3070+
mbedtls_record rec;
3071+
ret = ssl_parse_record_header(ssl, p, mbedtls_ssl_in_hdr_len(ssl), &rec);
3072+
if (ret != 0) {
3073+
return ret;
3074+
}
3075+
merged_rec_len += rec.data_len;
3076+
p = rec.buf + rec.buf_len;
3077+
if (q != NULL) {
3078+
memmove(q, rec.buf + rec.data_offset, rec.data_len);
3079+
q += rec.data_len;
3080+
} else {
3081+
q = p;
3082+
}
3083+
} while (merged_rec_len < ssl->in_hslen);
3084+
ssl->in_hdr = in_first_hdr;
3085+
mbedtls_ssl_update_in_pointers(ssl);
3086+
ssl->in_msglen = merged_rec_len;
3087+
/* Adjust message length. */
3088+
MBEDTLS_PUT_UINT16_BE(merged_rec_len, ssl->in_len, 0);
3089+
ssl->in_hsfraglen = 0;
3090+
MBEDTLS_SSL_DEBUG_BUF(4, "reassembled record",
3091+
ssl->in_hdr, mbedtls_ssl_in_hdr_len(ssl) + merged_rec_len);
3092+
}
3093+
} else {
3094+
return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
30413095
}
30423096

30433097
return 0;
@@ -4382,6 +4436,16 @@ static int ssl_consume_current_message(mbedtls_ssl_context *ssl)
43824436
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
43834437
}
43844438

4439+
if (ssl->in_hsfraglen != 0) {
4440+
/* Not all handshake fragments have arrived, do not consume. */
4441+
MBEDTLS_SSL_DEBUG_MSG(3,
4442+
("waiting for more fragments (%" MBEDTLS_PRINTF_SIZET " of %"
4443+
MBEDTLS_PRINTF_SIZET ", %" MBEDTLS_PRINTF_SIZET " left)",
4444+
ssl->in_hsfraglen, ssl->in_hslen,
4445+
ssl->in_hslen - ssl->in_hsfraglen));
4446+
return 0;
4447+
}
4448+
43854449
/*
43864450
* Get next Handshake message in the current record
43874451
*/
@@ -4407,6 +4471,7 @@ static int ssl_consume_current_message(mbedtls_ssl_context *ssl)
44074471
ssl->in_msglen -= ssl->in_hslen;
44084472
memmove(ssl->in_msg, ssl->in_msg + ssl->in_hslen,
44094473
ssl->in_msglen);
4474+
MBEDTLS_PUT_UINT16_BE(ssl->in_msglen, ssl->in_len, 0);
44104475

44114476
MBEDTLS_SSL_DEBUG_BUF(4, "remaining content in record",
44124477
ssl->in_msg, ssl->in_msglen);
@@ -5081,7 +5146,7 @@ void mbedtls_ssl_update_in_pointers(mbedtls_ssl_context *ssl)
50815146
} else
50825147
#endif
50835148
{
5084-
ssl->in_ctr = ssl->in_hdr - MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
5149+
ssl->in_ctr = ssl->in_buf;
50855150
ssl->in_len = ssl->in_hdr + 3;
50865151
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
50875152
ssl->in_cid = ssl->in_len;
@@ -5097,24 +5162,35 @@ void mbedtls_ssl_update_in_pointers(mbedtls_ssl_context *ssl)
50975162
* Setup an SSL context
50985163
*/
50995164

5100-
void mbedtls_ssl_reset_in_out_pointers(mbedtls_ssl_context *ssl)
5165+
void mbedtls_ssl_reset_in_pointers(mbedtls_ssl_context *ssl)
5166+
{
5167+
#if defined(MBEDTLS_SSL_PROTO_DTLS)
5168+
if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
5169+
ssl->in_hdr = ssl->in_buf;
5170+
} else
5171+
#endif /* MBEDTLS_SSL_PROTO_DTLS */
5172+
{
5173+
ssl->in_hdr = ssl->in_buf + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
5174+
}
5175+
5176+
/* Derive other internal pointers. */
5177+
mbedtls_ssl_update_in_pointers(ssl);
5178+
}
5179+
5180+
void mbedtls_ssl_reset_out_pointers(mbedtls_ssl_context *ssl)
51015181
{
51025182
/* Set the incoming and outgoing record pointers. */
51035183
#if defined(MBEDTLS_SSL_PROTO_DTLS)
51045184
if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
51055185
ssl->out_hdr = ssl->out_buf;
5106-
ssl->in_hdr = ssl->in_buf;
51075186
} else
51085187
#endif /* MBEDTLS_SSL_PROTO_DTLS */
51095188
{
51105189
ssl->out_ctr = ssl->out_buf;
5111-
ssl->out_hdr = ssl->out_buf + 8;
5112-
ssl->in_hdr = ssl->in_buf + 8;
5190+
ssl->out_hdr = ssl->out_buf + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
51135191
}
5114-
51155192
/* Derive other internal pointers. */
51165193
mbedtls_ssl_update_out_pointers(ssl, NULL /* no transform enabled */);
5117-
mbedtls_ssl_update_in_pointers(ssl);
51185194
}
51195195

51205196
/*

library/ssl_tls.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -339,12 +339,13 @@ static void handle_buffer_resizing(mbedtls_ssl_context *ssl, int downsizing,
339339
size_t out_buf_new_len)
340340
{
341341
int modified = 0;
342-
size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
342+
size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0, hdr_in = 0;
343343
size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
344344
if (ssl->in_buf != NULL) {
345345
written_in = ssl->in_msg - ssl->in_buf;
346346
iv_offset_in = ssl->in_iv - ssl->in_buf;
347347
len_offset_in = ssl->in_len - ssl->in_buf;
348+
hdr_in = ssl->in_hdr - ssl->in_buf;
348349
if (downsizing ?
349350
ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
350351
ssl->in_buf_len < in_buf_new_len) {
@@ -376,7 +377,10 @@ static void handle_buffer_resizing(mbedtls_ssl_context *ssl, int downsizing,
376377
}
377378
if (modified) {
378379
/* Update pointers here to avoid doing it twice. */
379-
mbedtls_ssl_reset_in_out_pointers(ssl);
380+
ssl->in_hdr = ssl->in_buf + hdr_in;
381+
mbedtls_ssl_update_in_pointers(ssl);
382+
mbedtls_ssl_reset_out_pointers(ssl);
383+
380384
/* Fields below might not be properly updated with record
381385
* splitting or with CID, so they are manually updated here. */
382386
ssl->out_msg = ssl->out_buf + written_out;
@@ -1277,7 +1281,8 @@ int mbedtls_ssl_setup(mbedtls_ssl_context *ssl,
12771281
goto error;
12781282
}
12791283

1280-
mbedtls_ssl_reset_in_out_pointers(ssl);
1284+
mbedtls_ssl_reset_in_pointers(ssl);
1285+
mbedtls_ssl_reset_out_pointers(ssl);
12811286

12821287
#if defined(MBEDTLS_SSL_DTLS_SRTP)
12831288
memset(&ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info));
@@ -1342,14 +1347,16 @@ void mbedtls_ssl_session_reset_msg_layer(mbedtls_ssl_context *ssl,
13421347
/* Cancel any possibly running timer */
13431348
mbedtls_ssl_set_timer(ssl, 0);
13441349

1345-
mbedtls_ssl_reset_in_out_pointers(ssl);
1350+
mbedtls_ssl_reset_in_pointers(ssl);
1351+
mbedtls_ssl_reset_out_pointers(ssl);
13461352

13471353
/* Reset incoming message parsing */
13481354
ssl->in_offt = NULL;
13491355
ssl->nb_zero = 0;
13501356
ssl->in_msgtype = 0;
13511357
ssl->in_msglen = 0;
13521358
ssl->in_hslen = 0;
1359+
ssl->in_hsfraglen = 0;
13531360
ssl->keep_current_message = 0;
13541361
ssl->transform_in = NULL;
13551362

library/ssl_tls12_server.c

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,28 +1015,6 @@ static int ssl_parse_client_hello(mbedtls_ssl_context *ssl)
10151015
MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
10161016
return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
10171017
}
1018-
{
1019-
size_t handshake_len = MBEDTLS_GET_UINT24_BE(buf, 1);
1020-
MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v3, handshake len.: %u",
1021-
(unsigned) handshake_len));
1022-
1023-
/* The record layer has a record size limit of 2^14 - 1 and
1024-
* fragmentation is not supported, so buf[1] should be zero. */
1025-
if (buf[1] != 0) {
1026-
MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message: %u != 0",
1027-
(unsigned) buf[1]));
1028-
return MBEDTLS_ERR_SSL_DECODE_ERROR;
1029-
}
1030-
1031-
/* We don't support fragmentation of ClientHello (yet?) */
1032-
if (msg_len != mbedtls_ssl_hs_hdr_len(ssl) + handshake_len) {
1033-
MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message: %u != %u + %u",
1034-
(unsigned) msg_len,
1035-
(unsigned) mbedtls_ssl_hs_hdr_len(ssl),
1036-
(unsigned) handshake_len));
1037-
return MBEDTLS_ERR_SSL_DECODE_ERROR;
1038-
}
1039-
}
10401018

10411019
#if defined(MBEDTLS_SSL_PROTO_DTLS)
10421020
if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {

0 commit comments

Comments
 (0)