From 2eed4095774f4d55c1536c3d53efbf15b0e72eb3 Mon Sep 17 00:00:00 2001 From: Carl Lundin <53273776+lundinc2@users.noreply.github.com> Date: Sun, 18 Oct 2020 16:57:47 -0700 Subject: [PATCH] Configure maximum fragment length extension. (#350) --- .../freertos/transport/src/tls_freertos.c | 21 +++++++++++++++++++ .../transport/src/tls_freertos_pkcs11.c | 21 +++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/FreeRTOS-Plus/Source/Application-Protocols/platform/freertos/transport/src/tls_freertos.c b/FreeRTOS-Plus/Source/Application-Protocols/platform/freertos/transport/src/tls_freertos.c index 14cf4165c2..cb50e9f2a6 100644 --- a/FreeRTOS-Plus/Source/Application-Protocols/platform/freertos/transport/src/tls_freertos.c +++ b/FreeRTOS-Plus/Source/Application-Protocols/platform/freertos/transport/src/tls_freertos.c @@ -402,6 +402,27 @@ static void setOptionalConfigurations( SSLContext_t * pSslContext, mbedtlsLowLevelCodeOrDefault( mbedtlsError ) ) ); } } + + /* Set Maximum Fragment Length if enabled. */ + #ifdef MBEDTLS_SSL_MAX_FRAGMENT_LENGTH + if( 0 == mbedtlsError ) + { + /* Enable the max fragment extension. 4096 bytes is currently the largest fragment size permitted. + * See RFC 8449 https://tools.ietf.org/html/rfc8449 for more information. + * + * Smaller values can be found in "mbedtls/include/ssl.h". + */ + mbedtlsError = mbedtls_ssl_conf_max_frag_len( &( pSslContext->config ), MBEDTLS_SSL_MAX_FRAG_LEN_4096 ); + + if( mbedtlsError != 0 ) + { + LogError( ( "Failed to maximum fragment length extension: mbedTLSError= %s : %s.", + mbedtlsHighLevelCodeOrDefault( mbedtlsError ), + mbedtlsLowLevelCodeOrDefault( mbedtlsError ) ) ); + } + } + #endif + } /*-----------------------------------------------------------*/ diff --git a/FreeRTOS-Plus/Source/Application-Protocols/platform/freertos/transport/src/tls_freertos_pkcs11.c b/FreeRTOS-Plus/Source/Application-Protocols/platform/freertos/transport/src/tls_freertos_pkcs11.c index 18e5f89593..20917c8faf 100644 --- a/FreeRTOS-Plus/Source/Application-Protocols/platform/freertos/transport/src/tls_freertos_pkcs11.c +++ b/FreeRTOS-Plus/Source/Application-Protocols/platform/freertos/transport/src/tls_freertos_pkcs11.c @@ -393,6 +393,27 @@ static TlsTransportStatus_t tlsSetup( NetworkContext_t * pNetworkContext, } } + /* Set Maximum Fragment Length if enabled. */ + #ifdef MBEDTLS_SSL_MAX_FRAGMENT_LENGTH + if( returnStatus == TLS_TRANSPORT_SUCCESS ) + { + /* Enable the max fragment extension. 4096 bytes is currently the largest fragment size permitted. + * See RFC 8449 https://tools.ietf.org/html/rfc8449 for more information. + * + * Smaller values can be found in "mbedtls/include/ssl.h". + */ + mbedtlsError = mbedtls_ssl_conf_max_frag_len( &( pNetworkContext->sslContext.config ), MBEDTLS_SSL_MAX_FRAG_LEN_4096 ); + + if( mbedtlsError != 0 ) + { + LogError( ( "Failed to maximum fragment length extension: mbedTLSError= %s : %s.", + mbedtlsHighLevelCodeOrDefault( mbedtlsError ), + mbedtlsLowLevelCodeOrDefault( mbedtlsError ) ) ); + returnStatus = TLS_TRANSPORT_INTERNAL_ERROR; + } + } + #endif + if( returnStatus == TLS_TRANSPORT_SUCCESS ) { /* Perform the TLS handshake. */