From 4c829241c5671eaf5204167fb03e221ff5d07904 Mon Sep 17 00:00:00 2001 From: Andy Sun Date: Tue, 9 Nov 2021 02:05:28 +0800 Subject: Make changes from the github comment. --- .../using_mbedtls/using_mbedtls/using_mbedtls.c | 12 ++++++---- .../mbedtls_bio_freertos_cellular.c | 27 +++++++++++++--------- .../mbedtls_bio_freertos_plus_tcp.c | 10 ++------ 3 files changed, 26 insertions(+), 23 deletions(-) diff --git a/FreeRTOS-Plus/Source/Application-Protocols/network_transport/using_mbedtls/using_mbedtls/using_mbedtls.c b/FreeRTOS-Plus/Source/Application-Protocols/network_transport/using_mbedtls/using_mbedtls/using_mbedtls.c index bb76dea6d..fff337e2a 100644 --- a/FreeRTOS-Plus/Source/Application-Protocols/network_transport/using_mbedtls/using_mbedtls/using_mbedtls.c +++ b/FreeRTOS-Plus/Source/Application-Protocols/network_transport/using_mbedtls/using_mbedtls/using_mbedtls.c @@ -48,8 +48,8 @@ /*-----------------------------------------------------------*/ -/** - * @brief Each compilation unit that consumes the NetworkContext must define it. +/** + * @brief Each compilation unit that consumes the NetworkContext must define it. * It should contain a single pointer as seen below whenever the header file * of this transport implementation is included to your project. * @@ -79,7 +79,7 @@ static const char * pNoLowLevelMbedTlsCodeStr = ""; * @brief Utility for converting the high-level code in an mbedTLS error to string, * if the code-contains a high-level code; otherwise, using a default string. */ -#define mbedtlsHighLevelCodeOrDefault( mbedTlsCode ) \ +#define mbedtlsHighLevelCodeOrDefault( mbedTlsCode ) \ ( mbedtls_high_level_strerr( mbedTlsCode ) != NULL ) ? \ mbedtls_high_level_strerr( mbedTlsCode ) : pNoHighLevelMbedTlsCodeStr @@ -87,7 +87,7 @@ static const char * pNoLowLevelMbedTlsCodeStr = ""; * @brief Utility for converting the level-level code in an mbedTLS error to string, * if the code-contains a level-level code; otherwise, using a default string. */ -#define mbedtlsLowLevelCodeOrDefault( mbedTlsCode ) \ +#define mbedtlsLowLevelCodeOrDefault( mbedTlsCode ) \ ( mbedtls_low_level_strerr( mbedTlsCode ) != NULL ) ? \ mbedtls_low_level_strerr( mbedTlsCode ) : pNoLowLevelMbedTlsCodeStr @@ -525,6 +525,10 @@ static TlsTransportStatus_t tlsHandshake( NetworkContext_t * pNetworkContext, * #mbedtls_ssl_set_bio requires the second parameter as void *. */ /* coverity[misra_c_2012_rule_11_2_violation] */ + + /* These two macros MBEDTLS_SSL_SEND and MBEDTLS_SSL_RECV need to be + * defined in mbedtls_config.h according to which implementation you use. + */ mbedtls_ssl_set_bio( &( pTlsTransportParams->sslContext.context ), ( void * ) pTlsTransportParams->tcpSocket, MBEDTLS_SSL_SEND, diff --git a/FreeRTOS-Plus/Source/Utilities/mbedtls_freertos/mbedtls_bio_freertos_cellular.c b/FreeRTOS-Plus/Source/Utilities/mbedtls_freertos/mbedtls_bio_freertos_cellular.c index 8e37a071e..7c4d916aa 100644 --- a/FreeRTOS-Plus/Source/Utilities/mbedtls_freertos/mbedtls_bio_freertos_cellular.c +++ b/FreeRTOS-Plus/Source/Utilities/mbedtls_freertos/mbedtls_bio_freertos_cellular.c @@ -42,7 +42,7 @@ /*-----------------------------------------------------------*/ /** - * @brief Sends data over FreeRTOS+TCP sockets. + * @brief Sends data over cellular sockets. * * @param[in] ctx The network context containing the socket handle. * @param[in] buf Buffer containing the bytes to send. @@ -54,20 +54,16 @@ int mbedtls_cellular_send( void * ctx, const unsigned char * buf, size_t len ) { - Socket_t socket; - configASSERT( ctx != NULL ); configASSERT( buf != NULL ); - socket = ( Socket_t ) ctx; - - return Sockets_Send( socket, buf, len ); + return Sockets_Send( ( Socket_t ) ctx, buf, len ); } /*-----------------------------------------------------------*/ /** - * @brief Receives data from FreeRTOS+TCP socket. + * @brief Receives data from cellular socket. * * @param[in] ctx The network context containing the socket handle. * @param[out] buf Buffer to receive bytes into. @@ -79,13 +75,22 @@ int mbedtls_cellular_recv( void * ctx, unsigned char * buf, size_t len ) { - Socket_t socket; + int recvStatus = 0; + int returnStatus = -1; configASSERT( ctx != NULL ); configASSERT( buf != NULL ); - socket = ( Socket_t ) ctx; + recvStatus = Sockets_Recv( ( Socket_t ) ctx, buf, len ); - return ( int ) Sockets_Recv( socket, buf, len ); -} + if( recvStatus < 0 ) + { + returnStatus = MBEDTLS_ERR_SSL_INTERNAL_ERROR; + } + else + { + returnStatus = recvStatus; + } + return returnStatus; +} diff --git a/FreeRTOS-Plus/Source/Utilities/mbedtls_freertos/mbedtls_bio_freertos_plus_tcp.c b/FreeRTOS-Plus/Source/Utilities/mbedtls_freertos/mbedtls_bio_freertos_plus_tcp.c index f26f3cfbe..4cc1e3cb3 100644 --- a/FreeRTOS-Plus/Source/Utilities/mbedtls_freertos/mbedtls_bio_freertos_plus_tcp.c +++ b/FreeRTOS-Plus/Source/Utilities/mbedtls_freertos/mbedtls_bio_freertos_plus_tcp.c @@ -54,15 +54,13 @@ int mbedtls_platform_send( void * ctx, const unsigned char * buf, size_t len ) { - Socket_t socket; BaseType_t sendStatus = 0; int returnStatus = -1; configASSERT( ctx != NULL ); configASSERT( buf != NULL ); - socket = ( Socket_t ) ctx; - sendStatus = FreeRTOS_send( socket, buf, len, 0 ); + sendStatus = FreeRTOS_send( ( Socket_t ) ctx, buf, len, 0 ); switch( sendStatus ) { @@ -105,16 +103,13 @@ int mbedtls_platform_recv( void * ctx, unsigned char * buf, size_t len ) { - Socket_t socket; BaseType_t recvStatus = 0; int returnStatus = -1; configASSERT( ctx != NULL ); configASSERT( buf != NULL ); - socket = ( Socket_t ) ctx; - - recvStatus = FreeRTOS_recv( socket, buf, len, 0 ); + recvStatus = FreeRTOS_recv( ( Socket_t ) ctx, buf, len, 0 ); switch( recvStatus ) { @@ -139,4 +134,3 @@ int mbedtls_platform_recv( void * ctx, return returnStatus; } - -- cgit v1.2.1