Skip to content

Commit dd14c0a

Browse files
committed
Remove in_hshdr
The first fragment of a fragmented handshake message always starts at the beginning of the buffer so there's no need to store it. Signed-off-by: Deomid rojer Ryabkov <[email protected]>
1 parent cf4e6a1 commit dd14c0a

File tree

3 files changed

+12
-22
lines changed

3 files changed

+12
-22
lines changed

include/mbedtls/ssl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1808,8 +1808,8 @@ struct mbedtls_ssl_context {
18081808

18091809
size_t MBEDTLS_PRIVATE(in_hslen); /*!< current handshake message length,
18101810
including the handshake header */
1811-
unsigned char *MBEDTLS_PRIVATE(in_hshdr); /*!< original handshake header start */
1812-
size_t MBEDTLS_PRIVATE(in_hsfraglen); /*!< accumulated hs fragments length */
1811+
size_t MBEDTLS_PRIVATE(in_hsfraglen); /*!< accumulated length of hs fragments
1812+
(up to in_hslen) */
18131813
int MBEDTLS_PRIVATE(nb_zero); /*!< # of 0-length encrypted messages */
18141814

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

library/ssl_msg.c

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3229,7 +3229,6 @@ int mbedtls_ssl_prepare_handshake_record(mbedtls_ssl_context *ssl)
32293229
if (ssl->in_hslen == 0) {
32303230
ssl->in_hslen = mbedtls_ssl_hs_hdr_len(ssl) + ssl_get_hs_total_len(ssl);
32313231
ssl->in_hsfraglen = 0;
3232-
ssl->in_hshdr = ssl->in_hdr;
32333232
}
32343233

32353234
MBEDTLS_SSL_DEBUG_MSG(3, ("handshake message: msglen ="
@@ -3296,10 +3295,7 @@ int mbedtls_ssl_prepare_handshake_record(mbedtls_ssl_context *ssl)
32963295
}
32973296
} else
32983297
#endif /* MBEDTLS_SSL_PROTO_DTLS */
3299-
{
3300-
if (ssl->in_hsfraglen > ssl->in_hslen) {
3301-
return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3302-
}
3298+
if (ssl->in_hsfraglen <= ssl->in_hslen) {
33033299
int ret;
33043300
const size_t hs_remain = ssl->in_hslen - ssl->in_hsfraglen;
33053301
MBEDTLS_SSL_DEBUG_MSG(3,
@@ -3317,15 +3313,16 @@ int mbedtls_ssl_prepare_handshake_record(mbedtls_ssl_context *ssl)
33173313
mbedtls_ssl_update_in_pointers(ssl);
33183314
return MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
33193315
}
3320-
if (ssl->in_hshdr != ssl->in_hdr) {
3316+
if (ssl->in_hsfraglen > 0) {
33213317
/*
3322-
* At ssl->in_hshdr we have a sequence of records that cover the next handshake
3318+
* At in_first_hdr we have a sequence of records that cover the next handshake
33233319
* record, each with its own record header that we need to remove.
33243320
* Note that the reassembled record size may not equal the size of the message,
3325-
* there maybe bytes from the next message following it.
3321+
* there may be more messages after it, complete or partial.
33263322
*/
3323+
unsigned char *in_first_hdr = ssl->in_buf + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
3324+
unsigned char *p = in_first_hdr, *q = NULL;
33273325
size_t merged_rec_len = 0;
3328-
unsigned char *p = ssl->in_hshdr, *q = NULL;
33293326
do {
33303327
mbedtls_record rec;
33313328
ret = ssl_parse_record_header(ssl, p, mbedtls_ssl_in_hdr_len(ssl), &rec);
@@ -3341,16 +3338,17 @@ int mbedtls_ssl_prepare_handshake_record(mbedtls_ssl_context *ssl)
33413338
q = p;
33423339
}
33433340
} while (merged_rec_len < ssl->in_hslen);
3344-
ssl->in_hdr = ssl->in_hshdr;
3341+
ssl->in_hdr = in_first_hdr;
33453342
mbedtls_ssl_update_in_pointers(ssl);
33463343
ssl->in_msglen = merged_rec_len;
33473344
/* Adjust message length. */
33483345
MBEDTLS_PUT_UINT16_BE(merged_rec_len, ssl->in_len, 0);
33493346
ssl->in_hsfraglen = 0;
3350-
ssl->in_hshdr = NULL;
33513347
MBEDTLS_SSL_DEBUG_BUF(4, "reassembled record",
33523348
ssl->in_hdr, mbedtls_ssl_in_hdr_len(ssl) + merged_rec_len);
33533349
}
3350+
} else {
3351+
return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
33543352
}
33553353

33563354
return 0;

library/ssl_tls.c

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -345,15 +345,11 @@ static void handle_buffer_resizing(mbedtls_ssl_context *ssl, int downsizing,
345345
int modified = 0;
346346
size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0, hdr_in = 0;
347347
size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
348-
size_t hshdr_in = 0;
349348
if (ssl->in_buf != NULL) {
350349
written_in = ssl->in_msg - ssl->in_buf;
351350
iv_offset_in = ssl->in_iv - ssl->in_buf;
352351
len_offset_in = ssl->in_len - ssl->in_buf;
353352
hdr_in = ssl->in_hdr - ssl->in_buf;
354-
if (ssl->in_hshdr != NULL) {
355-
hshdr_in = ssl->in_hshdr - ssl->in_buf;
356-
}
357353
if (downsizing ?
358354
ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
359355
ssl->in_buf_len < in_buf_new_len) {
@@ -398,9 +394,6 @@ static void handle_buffer_resizing(mbedtls_ssl_context *ssl, int downsizing,
398394
ssl->in_msg = ssl->in_buf + written_in;
399395
ssl->in_len = ssl->in_buf + len_offset_in;
400396
ssl->in_iv = ssl->in_buf + iv_offset_in;
401-
if (ssl->in_hshdr != NULL) {
402-
ssl->in_hshdr = ssl->in_buf + hshdr_in;
403-
}
404397
}
405398
}
406399
#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
@@ -1494,10 +1487,9 @@ void mbedtls_ssl_session_reset_msg_layer(mbedtls_ssl_context *ssl,
14941487
ssl->in_msgtype = 0;
14951488
ssl->in_msglen = 0;
14961489
ssl->in_hslen = 0;
1490+
ssl->in_hsfraglen = 0;
14971491
ssl->keep_current_message = 0;
14981492
ssl->transform_in = NULL;
1499-
ssl->in_hshdr = NULL;
1500-
ssl->in_hsfraglen = 0;
15011493

15021494
#if defined(MBEDTLS_SSL_PROTO_DTLS)
15031495
ssl->next_record_offset = 0;

0 commit comments

Comments
 (0)