diff --git a/FreeRTOS-Plus/Source/Application-Protocols/network_transport/sockets_wrapper/cellular/sockets_wrapper.c b/FreeRTOS-Plus/Source/Application-Protocols/network_transport/sockets_wrapper/cellular/sockets_wrapper.c index d1028f482..42a5a94fe 100644 --- a/FreeRTOS-Plus/Source/Application-Protocols/network_transport/sockets_wrapper/cellular/sockets_wrapper.c +++ b/FreeRTOS-Plus/Source/Application-Protocols/network_transport/sockets_wrapper/cellular/sockets_wrapper.c @@ -89,9 +89,6 @@ extern uint8_t CellularSocketPdnContextId; #define CELLULAR_SOCKET_OPEN_TIMEOUT_TICKS ( portMAX_DELAY ) #define CELLULAR_SOCKET_CLOSE_TIMEOUT_TICKS ( pdMS_TO_TICKS( 10000U ) ) -/* Cellular socket AT command timeout. */ -#define CELLULAR_SOCKET_RECV_TIMEOUT_MS ( 1000UL ) - /* Time conversion constants. */ #define _MILLISECONDS_PER_SECOND ( 1000 ) /**< @brief Milliseconds per second. */ #define _MILLISECONDS_PER_TICK ( _MILLISECONDS_PER_SECOND / configTICK_RATE_HZ ) /**< Milliseconds per FreeRTOS tick. */ @@ -447,7 +444,6 @@ static BaseType_t prvSetupSocketRecvTimeout( cellularSocketWrapper_t * pCellular static BaseType_t prvSetupSocketSendTimeout( cellularSocketWrapper_t * pCellularSocketContext, TickType_t sendTimeout ) { - CellularError_t socketStatus = CELLULAR_SUCCESS; BaseType_t retSetSockOpt = SOCKETS_ERROR_NONE; uint32_t sendTimeoutMs = 0; CellularSocketHandle_t cellularSocketHandle = NULL; @@ -478,18 +474,6 @@ static BaseType_t prvSetupSocketSendTimeout( cellularSocketWrapper_t * pCellular pCellularSocketContext->sendTimeout = sendTimeout; sendTimeoutMs = TICKS_TO_MS( sendTimeout ); } - - socketStatus = Cellular_SocketSetSockOpt( CellularHandle, - cellularSocketHandle, - CELLULAR_SOCKET_OPTION_LEVEL_TRANSPORT, - CELLULAR_SOCKET_OPTION_SEND_TIMEOUT, - ( const uint8_t * ) &sendTimeoutMs, - sizeof( uint32_t ) ); - - if( socketStatus != CELLULAR_SUCCESS ) - { - retSetSockOpt = SOCKETS_EINVAL; - } } return retSetSockOpt; @@ -599,7 +583,6 @@ BaseType_t Sockets_Connect( Socket_t * pTcpSocket, CellularSocketAddress_t serverAddress = { 0 }; EventBits_t waitEventBits = 0; BaseType_t retConnect = SOCKETS_ERROR_NONE; - const uint32_t defaultReceiveTimeoutMs = CELLULAR_SOCKET_RECV_TIMEOUT_MS; /* Create a new TCP socket. */ cellularSocketStatus = Cellular_CreateSocket( CellularHandle, @@ -660,23 +643,6 @@ BaseType_t Sockets_Connect( Socket_t * pTcpSocket, retConnect = prvCellularSocketRegisterCallback( cellularSocketHandle, pCellularSocketContext ); } - /* Setup cellular socket recv AT command default timeout. */ - if( retConnect == SOCKETS_ERROR_NONE ) - { - cellularSocketStatus = Cellular_SocketSetSockOpt( CellularHandle, - cellularSocketHandle, - CELLULAR_SOCKET_OPTION_LEVEL_TRANSPORT, - CELLULAR_SOCKET_OPTION_RECV_TIMEOUT, - ( const uint8_t * ) &defaultReceiveTimeoutMs, - sizeof( uint32_t ) ); - - if( cellularSocketStatus != CELLULAR_SUCCESS ) - { - IotLogError( "Failed to setup cellular AT command receive timeout %d.", cellularSocketStatus ); - retConnect = SOCKETS_SOCKET_ERROR; - } - } - /* Setup cellular socket send/recv timeout. */ if( retConnect == SOCKETS_ERROR_NONE ) { @@ -923,7 +889,7 @@ int32_t Sockets_Send( Socket_t xSocket, } } - IotLogDebug( "Sockets_Send expect %d write %d", len, sentLength ); + IotLogDebug( "Sockets_Send expect %d write %d", xDataLength, sentLength ); } return retSendLength; diff --git a/FreeRTOS-Plus/Source/Application-Protocols/network_transport/sockets_wrapper/cellular/sockets_wrapper.h b/FreeRTOS-Plus/Source/Application-Protocols/network_transport/sockets_wrapper/cellular/sockets_wrapper.h index 9f01c6191..be84e8536 100644 --- a/FreeRTOS-Plus/Source/Application-Protocols/network_transport/sockets_wrapper/cellular/sockets_wrapper.h +++ b/FreeRTOS-Plus/Source/Application-Protocols/network_transport/sockets_wrapper/cellular/sockets_wrapper.h @@ -53,6 +53,9 @@ #define LIBRARY_LOG_LEVEL LOG_INFO #endif +extern void vLoggingPrintf( const char * pcFormatString, + ... ); + #include "logging_stack.h" /************ End of logging configuration ****************/ diff --git a/FreeRTOS-Plus/Source/Application-Protocols/network_transport/sockets_wrapper/freertos_plus_tcp/sockets_wrapper.h b/FreeRTOS-Plus/Source/Application-Protocols/network_transport/sockets_wrapper/freertos_plus_tcp/sockets_wrapper.h index b38b8220e..9dba991b3 100644 --- a/FreeRTOS-Plus/Source/Application-Protocols/network_transport/sockets_wrapper/freertos_plus_tcp/sockets_wrapper.h +++ b/FreeRTOS-Plus/Source/Application-Protocols/network_transport/sockets_wrapper/freertos_plus_tcp/sockets_wrapper.h @@ -75,6 +75,8 @@ extern void vLoggingPrintf( const char * pcFormatString, /************ End of logging configuration ****************/ +#define SOCKETS_INVALID_SOCKET ( ( Socket_t ) ~0U ) + /** * @brief Establish a connection to server. * diff --git a/FreeRTOS-Plus/Source/Application-Protocols/network_transport/using_mbedtls/using_mbedtls.c b/FreeRTOS-Plus/Source/Application-Protocols/network_transport/using_mbedtls/using_mbedtls.c index cf93cebbe..e4b6d342d 100644 --- a/FreeRTOS-Plus/Source/Application-Protocols/network_transport/using_mbedtls/using_mbedtls.c +++ b/FreeRTOS-Plus/Source/Application-Protocols/network_transport/using_mbedtls/using_mbedtls.c @@ -36,10 +36,6 @@ /* FreeRTOS includes. */ #include "FreeRTOS.h" -/* FreeRTOS+TCP includes. */ -#include "FreeRTOS_IP.h" -#include "FreeRTOS_Sockets.h" - /* TLS transport header. */ #include "using_mbedtls.h" @@ -700,9 +696,9 @@ TlsTransportStatus_t TLS_FreeRTOS_Connect( NetworkContext_t * pNetworkContext, { sslContextFree( &( pTlsTransportParams->sslContext ) ); - if( pTlsTransportParams->tcpSocket != FREERTOS_INVALID_SOCKET ) + if( pTlsTransportParams->tcpSocket != SOCKETS_INVALID_SOCKET ) { - ( void ) FreeRTOS_closesocket( pTlsTransportParams->tcpSocket ); + ( void ) Sockets_Disconnect( pTlsTransportParams->tcpSocket ); } } } diff --git a/FreeRTOS-Plus/Source/Application-Protocols/network_transport/using_mbedtls_pkcs11/using_mbedtls_pkcs11.c b/FreeRTOS-Plus/Source/Application-Protocols/network_transport/using_mbedtls_pkcs11/using_mbedtls_pkcs11.c index 6bd709d68..4aeb16d8f 100644 --- a/FreeRTOS-Plus/Source/Application-Protocols/network_transport/using_mbedtls_pkcs11/using_mbedtls_pkcs11.c +++ b/FreeRTOS-Plus/Source/Application-Protocols/network_transport/using_mbedtls_pkcs11/using_mbedtls_pkcs11.c @@ -39,10 +39,6 @@ /* FreeRTOS includes. */ #include "FreeRTOS.h" -/* FreeRTOS+TCP includes. */ -#include "FreeRTOS_IP.h" -#include "FreeRTOS_Sockets.h" - /* TLS transport header. */ #include "using_mbedtls_pkcs11.h" @@ -530,12 +526,25 @@ static CK_RV readCertificateIntoContext( SSLContext_t * pSslContext, CK_RV xResult = CKR_OK; CK_ATTRIBUTE xTemplate = { 0 }; CK_OBJECT_HANDLE xCertObj = 0; + size_t labelLength; + char * pcNullTerminator = NULL; + + /* Check for NULL character within pkcs11configMAX_LABEL_LENGTH. */ + pcNullTerminator = memchr( pcLabelName, '\0', pkcs11configMAX_LABEL_LENGTH ); + if( NULL != pcNullTerminator ) + { + labelLength = ( size_t )( pcNullTerminator - pcLabelName ); + } + else + { + /* If NULL character not found set length to pkcs11configMAX_LABEL_LENGTH. */ + labelLength = pkcs11configMAX_LABEL_LENGTH; + } /* Get the handle of the certificate. */ xResult = xFindObjectWithLabelAndClass( pSslContext->xP11Session, pcLabelName, - strnlen( pcLabelName, - pkcs11configMAX_LABEL_LENGTH ), + labelLength, xClass, &xCertObj ); @@ -648,11 +657,25 @@ static CK_RV initializeClientKeys( SSLContext_t * pxCtx, if( CKR_OK == xResult ) { + size_t labelLength; + char * pcNullTerminator = NULL; + + /* Check for NULL character within pkcs11configMAX_LABEL_LENGTH. */ + pcNullTerminator = memchr( pcLabelName, '\0', pkcs11configMAX_LABEL_LENGTH ); + if( NULL != pcNullTerminator ) + { + labelLength = ( size_t )( pcNullTerminator - pcLabelName ); + } + else + { + /* If NULL character not found set length to pkcs11configMAX_LABEL_LENGTH. */ + labelLength = pkcs11configMAX_LABEL_LENGTH; + } + /* Get the handle of the device private key. */ xResult = xFindObjectWithLabelAndClass( pxCtx->xP11Session, pcLabelName, - strnlen( pcLabelName, - pkcs11configMAX_LABEL_LENGTH ), + labelLength, CKO_PRIVATE_KEY, &pxCtx->xP11PrivateKey ); } @@ -901,9 +924,10 @@ TlsTransportStatus_t TLS_FreeRTOS_Connect( NetworkContext_t * pNetworkContext, if( returnStatus != TLS_TRANSPORT_SUCCESS ) { if( ( pNetworkContext != NULL ) && - ( pTlsTransportParams->tcpSocket != FREERTOS_INVALID_SOCKET ) ) + ( pTlsTransportParams != NULL ) && + ( pTlsTransportParams->tcpSocket != SOCKETS_INVALID_SOCKET ) ) { - ( void ) FreeRTOS_closesocket( pTlsTransportParams->tcpSocket ); + ( void ) Sockets_Disconnect( pTlsTransportParams->tcpSocket ); } } else diff --git a/FreeRTOS-Plus/Source/Application-Protocols/network_transport/using_mbedtls_pkcs11/using_mbedtls_pkcs11.h b/FreeRTOS-Plus/Source/Application-Protocols/network_transport/using_mbedtls_pkcs11/using_mbedtls_pkcs11.h index bb6e3ee0e..c58591f46 100644 --- a/FreeRTOS-Plus/Source/Application-Protocols/network_transport/using_mbedtls_pkcs11/using_mbedtls_pkcs11.h +++ b/FreeRTOS-Plus/Source/Application-Protocols/network_transport/using_mbedtls_pkcs11/using_mbedtls_pkcs11.h @@ -74,7 +74,7 @@ extern void vLoggingPrintf( const char * pcFormatString, /************ End of logging configuration ****************/ /* FreeRTOS+TCP include. */ -#include "FreeRTOS_Sockets.h" +#include "sockets_wrapper.h" /* Transport interface include. */ #include "transport_interface.h" 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 31f2f8aa6..e4ddab3ba 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 @@ -31,13 +31,16 @@ /* FreeRTOS includes. */ #include "FreeRTOS.h" -#include "FreeRTOS_Sockets.h" /* Sockets wrapper includes. */ #include "sockets_wrapper.h" /* mbed TLS includes. */ -#include "mbedtls_config.h" +#if !defined( MBEDTLS_CONFIG_FILE ) + #include "config.h" +#else + #include MBEDTLS_CONFIG_FILE +#endif #include "threading_alt.h" #include "mbedtls/entropy.h" #include "mbedtls/ssl.h" 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 960b24aed..0cfe02234 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 @@ -34,7 +34,11 @@ #include "FreeRTOS_Sockets.h" /* mbed TLS includes. */ -#include "mbedtls_config.h" +#if !defined( MBEDTLS_CONFIG_FILE ) + #include "config.h" +#else + #include MBEDTLS_CONFIG_FILE +#endif #include "threading_alt.h" #include "mbedtls/entropy.h" #include "mbedtls/ssl.h" diff --git a/FreeRTOS-Plus/Source/Utilities/mbedtls_freertos/mbedtls_freertos_port.c b/FreeRTOS-Plus/Source/Utilities/mbedtls_freertos/mbedtls_freertos_port.c index 0cf31d320..8ad515769 100644 --- a/FreeRTOS-Plus/Source/Utilities/mbedtls_freertos/mbedtls_freertos_port.c +++ b/FreeRTOS-Plus/Source/Utilities/mbedtls_freertos/mbedtls_freertos_port.c @@ -34,7 +34,11 @@ #include "FreeRTOS_Sockets.h" /* mbed TLS includes. */ -#include "mbedtls_config.h" +#if !defined( MBEDTLS_CONFIG_FILE ) + #include "config.h" +#else + #include MBEDTLS_CONFIG_FILE +#endif #include "threading_alt.h" #include "mbedtls/entropy.h" #include "mbedtls/ssl.h" @@ -180,73 +184,77 @@ int mbedtls_platform_mutex_unlock( mbedtls_threading_mutex_t * pMutex ) /*-----------------------------------------------------------*/ -/** - * @brief Function to generate a random number. - * - * @param[in] data Callback context. - * @param[out] output The address of the buffer that receives the random number. - * @param[in] len Maximum size of the random number to be generated. - * @param[out] olen The size, in bytes, of the #output buffer. - * - * @return 0 if no critical failures occurred, - * MBEDTLS_ERR_ENTROPY_SOURCE_FAILED otherwise. - */ -int mbedtls_platform_entropy_poll( void * data, - unsigned char * output, - size_t len, - size_t * olen ) -{ - int status = 0; - NTSTATUS rngStatus = 0; - - configASSERT( output != NULL ); - configASSERT( olen != NULL ); - - /* Context is not used by this function. */ - ( void ) data; - - /* TLS requires a secure random number generator; use the RNG provided - * by Windows. This function MUST be re-implemented for other platforms. */ - rngStatus = - BCryptGenRandom( NULL, output, len, BCRYPT_USE_SYSTEM_PREFERRED_RNG ); - - if( rngStatus == 0 ) +#ifdef _WIN32 + /** + * @brief Function to generate a random number. + * + * @param[in] data Callback context. + * @param[out] output The address of the buffer that receives the random number. + * @param[in] len Maximum size of the random number to be generated. + * @param[out] olen The size, in bytes, of the #output buffer. + * + * @return 0 if no critical failures occurred, + * MBEDTLS_ERR_ENTROPY_SOURCE_FAILED otherwise. + */ + int mbedtls_platform_entropy_poll( void * data, + unsigned char * output, + size_t len, + size_t * olen ) { - /* All random bytes generated. */ - *olen = len; - } - else - { - /* RNG failure. */ - *olen = 0; - status = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; - } + int status = 0; + NTSTATUS rngStatus = 0; - return status; -} + configASSERT( output != NULL ); + configASSERT( olen != NULL ); + + /* Context is not used by this function. */ + ( void ) data; + + /* TLS requires a secure random number generator; use the RNG provided + * by Windows. This function MUST be re-implemented for other platforms. */ + rngStatus = + BCryptGenRandom( NULL, output, len, BCRYPT_USE_SYSTEM_PREFERRED_RNG ); + + if( rngStatus == 0 ) + { + /* All random bytes generated. */ + *olen = len; + } + else + { + /* RNG failure. */ + *olen = 0; + status = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; + } + + return status; + } +#endif /*-----------------------------------------------------------*/ -/** - * @brief Function to generate a random number based on a hardware poll. - * - * For this FreeRTOS Windows port, this function is redirected by calling - * #mbedtls_platform_entropy_poll. - * - * @param[in] data Callback context. - * @param[out] output The address of the buffer that receives the random number. - * @param[in] len Maximum size of the random number to be generated. - * @param[out] olen The size, in bytes, of the #output buffer. - * - * @return 0 if no critical failures occurred, - * MBEDTLS_ERR_ENTROPY_SOURCE_FAILED otherwise. - */ -int mbedtls_hardware_poll( void * data, - unsigned char * output, - size_t len, - size_t * olen ) -{ - return mbedtls_platform_entropy_poll( data, output, len, olen ); -} +#ifdef _WIN32 + /** + * @brief Function to generate a random number based on a hardware poll. + * + * For this FreeRTOS Windows port, this function is redirected by calling + * #mbedtls_platform_entropy_poll. + * + * @param[in] data Callback context. + * @param[out] output The address of the buffer that receives the random number. + * @param[in] len Maximum size of the random number to be generated. + * @param[out] olen The size, in bytes, of the #output buffer. + * + * @return 0 if no critical failures occurred, + * MBEDTLS_ERR_ENTROPY_SOURCE_FAILED otherwise. + */ + int mbedtls_hardware_poll( void * data, + unsigned char * output, + size_t len, + size_t * olen ) + { + return mbedtls_platform_entropy_poll( data, output, len, olen ); + } +#endif /*-----------------------------------------------------------*/