Skip to content

Commit 559772a

Browse files
yourslabn9wxu
andauthored
Update unused headers and NULL checks for platform wrappers (FreeRTOS#367)
- Remove unused headers in the plaintext FreeRTOS sockets wrapper - Update MFLN even though the preceding optional configuration returned an mbedTLS error - Remove an unused `NULL` check in a private method that is already checked by the public connect method - Add a `NULL` check to the public disconnect method Co-authored-by: Joseph Julicher <[email protected]>
1 parent ca9dcda commit 559772a

File tree

3 files changed

+86
-86
lines changed

3 files changed

+86
-86
lines changed

FreeRTOS-Plus/Source/Application-Protocols/platform/freertos/transport/src/plaintext_freertos.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424

2525
/* FreeRTOS includes. */
2626
#include "FreeRTOS.h"
27-
#include "atomic.h"
28-
#include "semphr.h"
2927

3028
/* FreeRTOS+TCP includes. */
3129
#include "FreeRTOS_IP.h"

FreeRTOS-Plus/Source/Application-Protocols/platform/freertos/transport/src/tls_freertos.c

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -328,12 +328,9 @@ static int32_t setCredentials( SSLContext_t * pSslContext,
328328
mbedtls_ssl_conf_cert_profile( &( pSslContext->config ),
329329
&( pSslContext->certProfile ) );
330330

331-
if( pNetworkCredentials->pRootCa != NULL )
332-
{
333-
mbedtlsError = setRootCa( pSslContext,
334-
pNetworkCredentials->pRootCa,
335-
pNetworkCredentials->rootCaSize );
336-
}
331+
mbedtlsError = setRootCa( pSslContext,
332+
pNetworkCredentials->pRootCa,
333+
pNetworkCredentials->rootCaSize );
337334

338335
if( ( pNetworkCredentials->pClientCert != NULL ) &&
339336
( pNetworkCredentials->pPrivateKey != NULL ) )
@@ -405,8 +402,7 @@ static void setOptionalConfigurations( SSLContext_t * pSslContext,
405402

406403
/* Set Maximum Fragment Length if enabled. */
407404
#ifdef MBEDTLS_SSL_MAX_FRAGMENT_LENGTH
408-
if( 0 == mbedtlsError )
409-
{
405+
410406
/* Enable the max fragment extension. 4096 bytes is currently the largest fragment size permitted.
411407
* See RFC 8449 https://tools.ietf.org/html/rfc8449 for more information.
412408
*
@@ -420,9 +416,7 @@ static void setOptionalConfigurations( SSLContext_t * pSslContext,
420416
mbedtlsHighLevelCodeOrDefault( mbedtlsError ),
421417
mbedtlsLowLevelCodeOrDefault( mbedtlsError ) ) );
422418
}
423-
}
424-
#endif
425-
419+
#endif /* ifdef MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
426420
}
427421
/*-----------------------------------------------------------*/
428422

@@ -672,12 +666,14 @@ TlsTransportStatus_t TLS_FreeRTOS_Connect( NetworkContext_t * pNetworkContext,
672666
/* Clean up on failure. */
673667
if( returnStatus != TLS_TRANSPORT_SUCCESS )
674668
{
675-
sslContextFree( &( pNetworkContext->sslContext ) );
676-
677-
if( ( pNetworkContext != NULL ) &&
678-
( pNetworkContext->tcpSocket != FREERTOS_INVALID_SOCKET ) )
669+
if( pNetworkContext != NULL )
679670
{
680-
( void ) FreeRTOS_closesocket( pNetworkContext->tcpSocket );
671+
sslContextFree( &( pNetworkContext->sslContext ) );
672+
673+
if( pNetworkContext->tcpSocket != FREERTOS_INVALID_SOCKET )
674+
{
675+
( void ) FreeRTOS_closesocket( pNetworkContext->tcpSocket );
676+
}
681677
}
682678
}
683679
else
@@ -695,40 +691,43 @@ void TLS_FreeRTOS_Disconnect( NetworkContext_t * pNetworkContext )
695691
{
696692
BaseType_t tlsStatus = 0;
697693

698-
/* Attempting to terminate TLS connection. */
699-
tlsStatus = ( BaseType_t ) mbedtls_ssl_close_notify( &( pNetworkContext->sslContext.context ) );
700-
701-
/* Ignore the WANT_READ and WANT_WRITE return values. */
702-
if( ( tlsStatus != ( BaseType_t ) MBEDTLS_ERR_SSL_WANT_READ ) &&
703-
( tlsStatus != ( BaseType_t ) MBEDTLS_ERR_SSL_WANT_WRITE ) )
694+
if( pNetworkContext != NULL )
704695
{
705-
if( tlsStatus == 0 )
696+
/* Attempting to terminate TLS connection. */
697+
tlsStatus = ( BaseType_t ) mbedtls_ssl_close_notify( &( pNetworkContext->sslContext.context ) );
698+
699+
/* Ignore the WANT_READ and WANT_WRITE return values. */
700+
if( ( tlsStatus != ( BaseType_t ) MBEDTLS_ERR_SSL_WANT_READ ) &&
701+
( tlsStatus != ( BaseType_t ) MBEDTLS_ERR_SSL_WANT_WRITE ) )
706702
{
707-
LogInfo( ( "(Network connection %p) TLS close-notify sent.",
708-
pNetworkContext ) );
703+
if( tlsStatus == 0 )
704+
{
705+
LogInfo( ( "(Network connection %p) TLS close-notify sent.",
706+
pNetworkContext ) );
707+
}
708+
else
709+
{
710+
LogError( ( "(Network connection %p) Failed to send TLS close-notify: mbedTLSError= %s : %s.",
711+
pNetworkContext,
712+
mbedtlsHighLevelCodeOrDefault( tlsStatus ),
713+
mbedtlsLowLevelCodeOrDefault( tlsStatus ) ) );
714+
}
709715
}
710716
else
711717
{
712-
LogError( ( "(Network connection %p) Failed to send TLS close-notify: mbedTLSError= %s : %s.",
713-
pNetworkContext,
714-
mbedtlsHighLevelCodeOrDefault( tlsStatus ),
715-
mbedtlsLowLevelCodeOrDefault( tlsStatus ) ) );
718+
/* WANT_READ and WANT_WRITE can be ignored. Logging for debugging purposes. */
719+
LogInfo( ( "(Network connection %p) TLS close-notify sent; ",
720+
"received %s as the TLS status can be ignored for close-notify."
721+
( tlsStatus == MBEDTLS_ERR_SSL_WANT_READ ) ? "WANT_READ" : "WANT_WRITE",
722+
pNetworkContext ) );
716723
}
717-
}
718-
else
719-
{
720-
/* WANT_READ and WANT_WRITE can be ignored. Logging for debugging purposes. */
721-
LogInfo( ( "(Network connection %p) TLS close-notify sent; ",
722-
"received %s as the TLS status can be ignored for close-notify."
723-
( tlsStatus == MBEDTLS_ERR_SSL_WANT_READ ) ? "WANT_READ" : "WANT_WRITE",
724-
pNetworkContext ) );
725-
}
726724

727-
/* Call socket shutdown function to close connection. */
728-
Sockets_Disconnect( pNetworkContext->tcpSocket );
725+
/* Call socket shutdown function to close connection. */
726+
Sockets_Disconnect( pNetworkContext->tcpSocket );
729727

730-
/* Free mbed TLS contexts. */
731-
sslContextFree( &( pNetworkContext->sslContext ) );
728+
/* Free mbed TLS contexts. */
729+
sslContextFree( &( pNetworkContext->sslContext ) );
730+
}
732731

733732
/* Clear the mutex functions for mbed TLS thread safety. */
734733
mbedtls_threading_free_alt();

FreeRTOS-Plus/Source/Application-Protocols/platform/freertos/transport/src/tls_freertos_pkcs11.c

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -395,24 +395,24 @@ static TlsTransportStatus_t tlsSetup( NetworkContext_t * pNetworkContext,
395395

396396
/* Set Maximum Fragment Length if enabled. */
397397
#ifdef MBEDTLS_SSL_MAX_FRAGMENT_LENGTH
398-
if( returnStatus == TLS_TRANSPORT_SUCCESS )
399-
{
400-
/* Enable the max fragment extension. 4096 bytes is currently the largest fragment size permitted.
401-
* See RFC 8449 https://tools.ietf.org/html/rfc8449 for more information.
402-
*
403-
* Smaller values can be found in "mbedtls/include/ssl.h".
404-
*/
405-
mbedtlsError = mbedtls_ssl_conf_max_frag_len( &( pNetworkContext->sslContext.config ), MBEDTLS_SSL_MAX_FRAG_LEN_4096 );
406-
407-
if( mbedtlsError != 0 )
398+
if( returnStatus == TLS_TRANSPORT_SUCCESS )
408399
{
409-
LogError( ( "Failed to maximum fragment length extension: mbedTLSError= %s : %s.",
410-
mbedtlsHighLevelCodeOrDefault( mbedtlsError ),
411-
mbedtlsLowLevelCodeOrDefault( mbedtlsError ) ) );
412-
returnStatus = TLS_TRANSPORT_INTERNAL_ERROR;
400+
/* Enable the max fragment extension. 4096 bytes is currently the largest fragment size permitted.
401+
* See RFC 8449 https://tools.ietf.org/html/rfc8449 for more information.
402+
*
403+
* Smaller values can be found in "mbedtls/include/ssl.h".
404+
*/
405+
mbedtlsError = mbedtls_ssl_conf_max_frag_len( &( pNetworkContext->sslContext.config ), MBEDTLS_SSL_MAX_FRAG_LEN_4096 );
406+
407+
if( mbedtlsError != 0 )
408+
{
409+
LogError( ( "Failed to maximum fragment length extension: mbedTLSError= %s : %s.",
410+
mbedtlsHighLevelCodeOrDefault( mbedtlsError ),
411+
mbedtlsLowLevelCodeOrDefault( mbedtlsError ) ) );
412+
returnStatus = TLS_TRANSPORT_INTERNAL_ERROR;
413+
}
413414
}
414-
}
415-
#endif
415+
#endif /* ifdef MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
416416

417417
if( returnStatus == TLS_TRANSPORT_SUCCESS )
418418
{
@@ -853,40 +853,43 @@ void TLS_FreeRTOS_Disconnect( NetworkContext_t * pNetworkContext )
853853
{
854854
BaseType_t tlsStatus = 0;
855855

856-
/* Attempting to terminate TLS connection. */
857-
tlsStatus = ( BaseType_t ) mbedtls_ssl_close_notify( &( pNetworkContext->sslContext.context ) );
858-
859-
/* Ignore the WANT_READ and WANT_WRITE return values. */
860-
if( ( tlsStatus != ( BaseType_t ) MBEDTLS_ERR_SSL_WANT_READ ) &&
861-
( tlsStatus != ( BaseType_t ) MBEDTLS_ERR_SSL_WANT_WRITE ) )
856+
if( pNetworkContext != NULL )
862857
{
863-
if( tlsStatus == 0 )
858+
/* Attempting to terminate TLS connection. */
859+
tlsStatus = ( BaseType_t ) mbedtls_ssl_close_notify( &( pNetworkContext->sslContext.context ) );
860+
861+
/* Ignore the WANT_READ and WANT_WRITE return values. */
862+
if( ( tlsStatus != ( BaseType_t ) MBEDTLS_ERR_SSL_WANT_READ ) &&
863+
( tlsStatus != ( BaseType_t ) MBEDTLS_ERR_SSL_WANT_WRITE ) )
864864
{
865-
LogInfo( ( "(Network connection %p) TLS close-notify sent.",
866-
pNetworkContext ) );
865+
if( tlsStatus == 0 )
866+
{
867+
LogInfo( ( "(Network connection %p) TLS close-notify sent.",
868+
pNetworkContext ) );
869+
}
870+
else
871+
{
872+
LogError( ( "(Network connection %p) Failed to send TLS close-notify: mbedTLSError= %s : %s.",
873+
pNetworkContext,
874+
mbedtlsHighLevelCodeOrDefault( tlsStatus ),
875+
mbedtlsLowLevelCodeOrDefault( tlsStatus ) ) );
876+
}
867877
}
868878
else
869879
{
870-
LogError( ( "(Network connection %p) Failed to send TLS close-notify: mbedTLSError= %s : %s.",
871-
pNetworkContext,
872-
mbedtlsHighLevelCodeOrDefault( tlsStatus ),
873-
mbedtlsLowLevelCodeOrDefault( tlsStatus ) ) );
880+
/* WANT_READ and WANT_WRITE can be ignored. Logging for debugging purposes. */
881+
LogInfo( ( "(Network connection %p) TLS close-notify sent; ",
882+
"received %s as the TLS status can be ignored for close-notify."
883+
( tlsStatus == MBEDTLS_ERR_SSL_WANT_READ ) ? "WANT_READ" : "WANT_WRITE",
884+
pNetworkContext ) );
874885
}
875-
}
876-
else
877-
{
878-
/* WANT_READ and WANT_WRITE can be ignored. Logging for debugging purposes. */
879-
LogInfo( ( "(Network connection %p) TLS close-notify sent; ",
880-
"received %s as the TLS status can be ignored for close-notify."
881-
( tlsStatus == MBEDTLS_ERR_SSL_WANT_READ ) ? "WANT_READ" : "WANT_WRITE",
882-
pNetworkContext ) );
883-
}
884886

885-
/* Call socket shutdown function to close connection. */
886-
Sockets_Disconnect( pNetworkContext->tcpSocket );
887+
/* Call socket shutdown function to close connection. */
888+
Sockets_Disconnect( pNetworkContext->tcpSocket );
887889

888-
/* Free mbed TLS contexts. */
889-
sslContextFree( &( pNetworkContext->sslContext ) );
890+
/* Free mbed TLS contexts. */
891+
sslContextFree( &( pNetworkContext->sslContext ) );
892+
}
890893

891894
/* Clear the mutex functions for mbed TLS thread safety. */
892895
mbedtls_threading_free_alt();

0 commit comments

Comments
 (0)