From 29092e6d80dbd056d2415325a233d04f231ecc47 Mon Sep 17 00:00:00 2001 From: Hannes Tschofenig Date: Tue, 27 Apr 2021 15:06:29 +0200 Subject: [PATCH 1/5] Changed function signature for writing MFL extension --- library/ssl_tls13_server.c | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/library/ssl_tls13_server.c b/library/ssl_tls13_server.c index 0f5c6fcbf0ae..a3ada68c2805 100644 --- a/library/ssl_tls13_server.c +++ b/library/ssl_tls13_server.c @@ -3067,28 +3067,27 @@ static int ssl_client_hello_postprocess( mbedtls_ssl_context* ssl, #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) -static void ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl, - unsigned char *buf, - size_t *olen ) +static int ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl, + unsigned char *buf, + const unsigned char *end, + size_t *olen ) { unsigned char *p = buf; *olen = 0; - if( ( ssl->handshake->extensions_present & MAX_FRAGMENT_LENGTH_EXTENSION ) - == 0 ) - { - return( 0 ); - } - if( ssl->session_negotiate->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE ) - { + if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE ) return( 0 ); - } - MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) ); + MBEDTLS_SSL_DEBUG_MSG( 3, + ( "client hello, adding max_fragment_length extension" ) ); + + MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 ); - *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF ); - *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF ); + *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) + & 0xFF ); + *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH ) + & 0xFF ); *p++ = 0x00; *p++ = 1; @@ -3096,6 +3095,8 @@ static void ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl, *p++ = ssl->session_negotiate->mfl_code; *olen = 5; + + return( 0 ); } #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ @@ -3391,9 +3392,12 @@ static int ssl_encrypted_extensions_write( mbedtls_ssl_context* ssl, #endif /* MBEDTLS_SSL_ALPN */ #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) - ret = ssl_write_max_fragment_length_ext( ssl, p, end - p, &n ); - if( ret != 0 ) + if( ( ret = ssl_write_max_fragment_length_ext( ssl, p, + end - p, &n ) ) != 0 ) + { + MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_max_fragment_length_ext", ret ); return( ret ); + } p += n; #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ From ab8d4b7ead8e083d086ec2316c4fd6d6bd967d4c Mon Sep 17 00:00:00 2001 From: Hannes Tschofenig Date: Tue, 27 Apr 2021 15:07:41 +0200 Subject: [PATCH 2/5] Removed code in mbedtls_ssl_get_input_max_frag_len --- library/ssl_tls.c | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/library/ssl_tls.c b/library/ssl_tls.c index e77b72abcfde..8f8cdf097be1 100644 --- a/library/ssl_tls.c +++ b/library/ssl_tls.c @@ -5703,13 +5703,6 @@ size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl ) size_t max_len = MBEDTLS_SSL_MAX_CONTENT_LEN; size_t read_mfl; - /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */ - if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && - ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE ) - { - return ssl_mfl_code_to_length( ssl->conf->mfl_code ); - } - /* Check if a smaller max length was negotiated */ if( ssl->session_out != NULL ) { @@ -5720,7 +5713,7 @@ size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl ) } } - // During a handshake, use the value being negotiated + /* During a handshake, use the value being negotiated */ if( ssl->session_negotiate != NULL ) { read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); From 0e8d7e818b447e79a7511eaf9603aa02dd618da3 Mon Sep 17 00:00:00 2001 From: Hannes Tschofenig Date: Wed, 28 Apr 2021 10:43:35 +0200 Subject: [PATCH 3/5] Changes to client and server MFL extension API --- library/ssl_tls.c | 9 +++++++++ library/ssl_tls13_client.c | 10 ++++++++-- library/ssl_tls13_server.c | 20 +++++++++----------- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/library/ssl_tls.c b/library/ssl_tls.c index 8f8cdf097be1..c179a9817f8f 100644 --- a/library/ssl_tls.c +++ b/library/ssl_tls.c @@ -5703,6 +5703,15 @@ size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl ) size_t max_len = MBEDTLS_SSL_MAX_CONTENT_LEN; size_t read_mfl; +#if defined(MBEDTLS_SSL_PROTO_TLS1_2_OR_EARLIER) + /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */ + if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && + ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE ) + { + return ssl_mfl_code_to_length( ssl->conf->mfl_code ); + } +#endif /* MBEDTLS_SSL_PROTO_TLS1_2_OR_EARLIER */ + /* Check if a smaller max length was negotiated */ if( ssl->session_out != NULL ) { diff --git a/library/ssl_tls13_client.c b/library/ssl_tls13_client.c index 033803a8ed85..c4ede423839f 100644 --- a/library/ssl_tls13_client.c +++ b/library/ssl_tls13_client.c @@ -569,7 +569,7 @@ static int ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl, return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); } - MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding max_fragment_length extension" ) ); + MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding max_fragment_length extension" ) ); *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF ); *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF ); @@ -1754,7 +1754,13 @@ static int ssl_client_hello_write_partial( mbedtls_ssl_context* ssl, #endif /* MBEDTLS_SSL_ALPN */ #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) - ssl_write_max_fragment_length_ext( ssl, buf, end, &cur_ext_len ); + if( ( ret = ssl_write_max_fragment_length_ext( ssl, buf, + end - buf, &cur_ext_len ) ) != 0 ) + { + MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_max_fragment_length_ext", ret ); + return( ret ); + } + total_ext_len += cur_ext_len; buf += cur_ext_len; #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ diff --git a/library/ssl_tls13_server.c b/library/ssl_tls13_server.c index a3ada68c2805..8d9465c12c2b 100644 --- a/library/ssl_tls13_server.c +++ b/library/ssl_tls13_server.c @@ -3069,20 +3069,21 @@ static int ssl_client_hello_postprocess( mbedtls_ssl_context* ssl, #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) static int ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl, unsigned char *buf, - const unsigned char *end, + size_t buflen, size_t *olen ) { unsigned char *p = buf; - *olen = 0; - - if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE ) + if( ssl->session_negotiate->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE ) + { + *olen = 0; return( 0 ); + } MBEDTLS_SSL_DEBUG_MSG( 3, - ( "client hello, adding max_fragment_length extension" ) ); + ( "adding max_fragment_length extension" ) ); - MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 ); + MBEDTLS_SSL_CHK_BUF_PTR( p, buf + buflen, 5 ); *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF ); @@ -3392,12 +3393,9 @@ static int ssl_encrypted_extensions_write( mbedtls_ssl_context* ssl, #endif /* MBEDTLS_SSL_ALPN */ #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) - if( ( ret = ssl_write_max_fragment_length_ext( ssl, p, - end - p, &n ) ) != 0 ) - { - MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_max_fragment_length_ext", ret ); + ret = ssl_write_max_fragment_length_ext( ssl, p, end - p, &n ); + if( ret != 0 ) return( ret ); - } p += n; #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ From 24c0538dcd57def7c16346965caaafabd545503d Mon Sep 17 00:00:00 2001 From: Hannes Tschofenig Date: Mon, 21 Jun 2021 17:46:45 +0200 Subject: [PATCH 4/5] Update ssl_tls13_server.c --- library/ssl_tls13_server.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/library/ssl_tls13_server.c b/library/ssl_tls13_server.c index 8d9465c12c2b..bd2da16546a5 100644 --- a/library/ssl_tls13_server.c +++ b/library/ssl_tls13_server.c @@ -3074,9 +3074,16 @@ static int ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl, { unsigned char *p = buf; + *olen = 0; + + if( ( ssl->handshake->extensions_present & MAX_FRAGMENT_LENGTH_EXTENSION ) + == 0 ) + { + return( 0 ); + } + if( ssl->session_negotiate->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE ) { - *olen = 0; return( 0 ); } From 5e3aa857df9da29e6a39acab63a1abf8a2dda84a Mon Sep 17 00:00:00 2001 From: Hannes Tschofenig Date: Mon, 21 Jun 2021 17:47:49 +0200 Subject: [PATCH 5/5] Update library/ssl_tls13_client.c Co-authored-by: Hanno Becker --- library/ssl_tls13_client.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/library/ssl_tls13_client.c b/library/ssl_tls13_client.c index c4ede423839f..65fbab9fcbe4 100644 --- a/library/ssl_tls13_client.c +++ b/library/ssl_tls13_client.c @@ -1755,7 +1755,8 @@ static int ssl_client_hello_write_partial( mbedtls_ssl_context* ssl, #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) if( ( ret = ssl_write_max_fragment_length_ext( ssl, buf, - end - buf, &cur_ext_len ) ) != 0 ) + (size_t)( end - buf ), + &cur_ext_len ) ) != 0 ) { MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_max_fragment_length_ext", ret ); return( ret );