diff --git a/source/cellular_common_api.c b/source/cellular_common_api.c index 43a67bf0..378d9ebb 100644 --- a/source/cellular_common_api.c +++ b/source/cellular_common_api.c @@ -112,6 +112,32 @@ static CellularError_t _socketSetSockOptLevelTransport( CellularSocketOption_t o cellularStatus = CELLULAR_INTERNAL_FAILURE; } } + else if( option == CELLULAR_SOCKET_OPTION_SSL_CONTEXT_ID ) + { + if( ( socketHandle->socketState == SOCKETSTATE_ALLOCATED ) && ( optionValueLength == sizeof( uint8_t ) ) ) + { + socketHandle->sslConfig.sslContextId = *pOptionValue; + } + else + { + LogError( ( "Cellular_SocketSetSockOpt: Cannot change the sslContextID in this state %d or length %d is invalid.", + socketHandle->socketState, optionValueLength ) ); + cellularStatus = CELLULAR_INTERNAL_FAILURE; + } + } + else if( option == CELLULAR_SOCKET_OPTION_SSL_USAGE ) + { + if( ( socketHandle->socketState == SOCKETSTATE_ALLOCATED ) && ( optionValueLength == sizeof( uint8_t ) ) ) + { + socketHandle->sslConfig.useSsl = *pOptionValue; + } + else + { + LogError( ( "Cellular_SocketSetSockOpt: Cannot change the useSsl in this state %d or length %d is invalid.", + socketHandle->socketState, optionValueLength ) ); + cellularStatus = CELLULAR_INTERNAL_FAILURE; + } + } else if( option == CELLULAR_SOCKET_OPTION_SET_LOCAL_PORT ) { if( ( socketHandle->socketState == SOCKETSTATE_ALLOCATED ) && ( optionValueLength == sizeof( uint16_t ) ) ) diff --git a/source/include/cellular_api.h b/source/include/cellular_api.h index ddd3c17c..20daf4ae 100644 --- a/source/include/cellular_api.h +++ b/source/include/cellular_api.h @@ -639,6 +639,42 @@ CellularError_t Cellular_SocketRegisterClosedCallback( CellularHandle_t cellular CellularSocketClosedCallback_t closedCallback, void * pCallbackContext ); +/** + * @brief Configure parameters of an SSL Context. + * + * @param[in] cellularHandle The opaque cellular context pointer created by Cellular_Init. + * @param[in] sslContextId The SSL context ID. + * @param[in] sslConfigurationParameter The SSL parameter to be configured. + * @param[in] inputArg The value to be passed to the SSL parameter. + * + * @return CELLULAR_SUCCESS if the operation is successful, otherwise an error + * code indicating the cause of the error. + */ +CellularError_t Cellular_ConfigureSSLContext( CellularHandle_t cellularHandle, + uint8_t sslContextId, + const char * sslConfigurationParameter, + const char * inputArg ); + +/** + * @brief Upload a File into the Storage. + * + * @param[in] cellularHandle The opaque cellular context pointer created by Cellular_Init. + * @param[in] filename Name of the file to be uploaded. + * @param[in] fileContent Content of the file to be uploaded. + * @param[in] fileSize Size of the file to be uploaded. + * @param[out] pSentDataLength Out parameter to provide the length of the actual + * data sent. Note that it may be less than the dataLength in case complete data + * could not be sent. + * + * @return CELLULAR_SUCCESS if the operation is successful, otherwise an error + * code indicating the cause of the error. + */ +CellularError_t Cellular_UploadFileToStorage( CellularHandle_t cellularHandle, + const char * filename, + const char * fileContent, + uint32_t fileSize, + uint32_t * pSentDataLength ); + /* *INDENT-OFF* */ #ifdef __cplusplus } diff --git a/source/include/cellular_types.h b/source/include/cellular_types.h index c8e04c09..860bef52 100644 --- a/source/include/cellular_types.h +++ b/source/include/cellular_types.h @@ -313,8 +313,9 @@ typedef enum CellularIPAddressType */ typedef enum CellularSocketOptionLevel { - CELLULAR_SOCKET_OPTION_LEVEL_IP, /**< IP layer options. */ - CELLULAR_SOCKET_OPTION_LEVEL_TRANSPORT /**< Transport (TCP/UDP) layer options. */ + CELLULAR_SOCKET_OPTION_LEVEL_IP, /**< IP layer options. */ + CELLULAR_SOCKET_OPTION_LEVEL_TRANSPORT, /**< Transport (TCP/UDP) layer options. */ + CELLULAR_SOCKET_OPTION_LEVEL_SECURE /**< Secured socket connection. */ } CellularSocketOptionLevel_t; /** @@ -327,6 +328,8 @@ typedef enum CellularSocketOption CELLULAR_SOCKET_OPTION_SEND_TIMEOUT, /**< Set send timeout (in milliseconds). */ CELLULAR_SOCKET_OPTION_RECV_TIMEOUT, /**< Set receive timeout (in milliseconds). */ CELLULAR_SOCKET_OPTION_PDN_CONTEXT_ID, /**< Set PDN Context ID to use for the socket. */ + CELLULAR_SOCKET_OPTION_SSL_CONTEXT_ID, /**< Set SSL Context ID to use for the socket. */ + CELLULAR_SOCKET_OPTION_SSL_USAGE, /**< Set SSL or non SSL to use for the socket. */ CELLULAR_SOCKET_OPTION_SET_LOCAL_PORT /**< Set local port. */ } CellularSocketOption_t; diff --git a/source/include/common/cellular_common.h b/source/include/common/cellular_common.h index 3bd8ac49..2611ef74 100644 --- a/source/include/common/cellular_common.h +++ b/source/include/common/cellular_common.h @@ -115,6 +115,16 @@ typedef enum CellularSocketState SOCKETSTATE_DISCONNECTED /**< Socket is disconnected by remote peer or due to network error. */ } CellularSocketState_t; +/** + * @ingroup cellular_common_datatypes_paramstructs + * @brief the ssl mapping structure. + */ +typedef struct CellularSocketSslConfig +{ + uint8_t useSsl; + uint8_t sslContextId; +} CellularSocketSslConfig_t; + /** * @ingroup cellular_common_datatypes_paramstructs * @brief Parameters involved in sending/receiving data through sockets. @@ -123,6 +133,7 @@ typedef struct CellularSocketContext { uint8_t contextId; /**< PDN context ID on which this socket exists. */ uint32_t socketId; /**< Socket ID of this socket. */ + CellularSocketSslConfig_t sslConfig; /**< SSL context ID on which this socket exists. */ CellularSocketState_t socketState; /**< State of the socket, Allocated, Free etc. */ CellularSocketType_t socketType; /**< Type of socket, DGRAM or STREAM. */ CellularSocketDomain_t socketDomain; /**< Socket domain, IPV4 or V6. */ diff --git a/test/unit-test/cellular_common_api_utest.c b/test/unit-test/cellular_common_api_utest.c index 787afa06..4361a916 100644 --- a/test/unit-test/cellular_common_api_utest.c +++ b/test/unit-test/cellular_common_api_utest.c @@ -841,6 +841,150 @@ void test_Cellular_CommonSocketSetSockOpt_Option_PdnContextId_WrongSize_Failure_ TEST_ASSERT_EQUAL( CELLULAR_INTERNAL_FAILURE, cellularStatus ); } +/** + * @brief Test that option ssl context id happy path case for Cellular_CommonSocketSetSockOpt. + */ +void test_Cellular_CommonSocketSetSockOpt_Option_SslContextId_Happy_Path( void ) +{ + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularContext_t context; + + memset( &context, 0, sizeof( CellularContext_t ) ); + struct CellularSocketContext socketHandle; + uint32_t optionValue = 0; + + socketHandle.socketState = SOCKETSTATE_ALLOCATED; + + _Cellular_CheckLibraryStatus_IgnoreAndReturn( CELLULAR_SUCCESS ); + + cellularStatus = Cellular_CommonSocketSetSockOpt( &context, &socketHandle, + CELLULAR_SOCKET_OPTION_LEVEL_SECURE, + CELLULAR_SOCKET_OPTION_SSL_CONTEXT_ID, + ( const uint8_t * ) &optionValue, sizeof( uint8_t ) ); + + TEST_ASSERT_EQUAL( CELLULAR_SUCCESS, cellularStatus ); +} + +/** + * @brief Test that option ssl context id failure path case for Cellular_CommonSocketSetSockOpt. + */ +void test_Cellular_CommonSocketSetSockOpt_Option_SslContextId_Failure_Path( void ) +{ + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularContext_t context; + + memset( &context, 0, sizeof( CellularContext_t ) ); + struct CellularSocketContext socketHandle; + uint32_t optionValue = 0; + + socketHandle.socketState = SOCKETSTATE_CONNECTING; + + _Cellular_CheckLibraryStatus_IgnoreAndReturn( CELLULAR_SUCCESS ); + + cellularStatus = Cellular_CommonSocketSetSockOpt( &context, &socketHandle, + CELLULAR_SOCKET_OPTION_LEVEL_SECURE, + CELLULAR_SOCKET_OPTION_SSL_CONTEXT_ID, + ( const uint8_t * ) &optionValue, sizeof( uint8_t ) ); + + TEST_ASSERT_EQUAL( CELLULAR_INTERNAL_FAILURE, cellularStatus ); +} + +/** + * @brief Test that option ssl context id failure path case with wrong size for Cellular_CommonSocketSetSockOpt. + */ +void test_Cellular_CommonSocketSetSockOpt_Option_SslContextId_WrongSize_Failure_Path( void ) +{ + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularContext_t context; + + memset( &context, 0, sizeof( CellularContext_t ) ); + struct CellularSocketContext socketHandle; + uint32_t optionValue = 0; + + socketHandle.socketState = SOCKETSTATE_ALLOCATED; + + _Cellular_CheckLibraryStatus_IgnoreAndReturn( CELLULAR_SUCCESS ); + + cellularStatus = Cellular_CommonSocketSetSockOpt( &context, &socketHandle, + CELLULAR_SOCKET_OPTION_LEVEL_SECURE, + CELLULAR_SOCKET_OPTION_SSL_CONTEXT_ID, + ( const uint8_t * ) &optionValue, sizeof( uint32_t ) ); + + TEST_ASSERT_EQUAL( CELLULAR_INTERNAL_FAILURE, cellularStatus ); +} + +/** + * @brief Test that option ssl context id happy path case for Cellular_CommonSocketSetSockOpt. + */ +void test_Cellular_CommonSocketSetSockOpt_Option_UseSsl_Happy_Path( void ) +{ + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularContext_t context; + + memset( &context, 0, sizeof( CellularContext_t ) ); + struct CellularSocketContext socketHandle; + uint32_t optionValue = 0; + + socketHandle.socketState = SOCKETSTATE_ALLOCATED; + + _Cellular_CheckLibraryStatus_IgnoreAndReturn( CELLULAR_SUCCESS ); + + cellularStatus = Cellular_CommonSocketSetSockOpt( &context, &socketHandle, + CELLULAR_SOCKET_OPTION_LEVEL_SECURE, + CELLULAR_SOCKET_OPTION_SSL_USAGE, + ( const uint8_t * ) &optionValue, sizeof( uint8_t ) ); + + TEST_ASSERT_EQUAL( CELLULAR_SUCCESS, cellularStatus ); +} + +/** + * @brief Test that option ssl context id failure path case for Cellular_CommonSocketSetSockOpt. + */ +void test_Cellular_CommonSocketSetSockOpt_Option_UseSsl_Failure_Path( void ) +{ + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularContext_t context; + + memset( &context, 0, sizeof( CellularContext_t ) ); + struct CellularSocketContext socketHandle; + uint32_t optionValue = 0; + + socketHandle.socketState = SOCKETSTATE_CONNECTING; + + _Cellular_CheckLibraryStatus_IgnoreAndReturn( CELLULAR_SUCCESS ); + + cellularStatus = Cellular_CommonSocketSetSockOpt( &context, &socketHandle, + CELLULAR_SOCKET_OPTION_LEVEL_SECURE, + CELLULAR_SOCKET_OPTION_SSL_USAGE, + ( const uint8_t * ) &optionValue, sizeof( uint8_t ) ); + + TEST_ASSERT_EQUAL( CELLULAR_INTERNAL_FAILURE, cellularStatus ); +} + +/** + * @brief Test that option ssl context id failure path case with wrong size for Cellular_CommonSocketSetSockOpt. + */ +void test_Cellular_CommonSocketSetSockOpt_Option_UseSsl_WrongSize_Failure_Path( void ) +{ + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularContext_t context; + + memset( &context, 0, sizeof( CellularContext_t ) ); + struct CellularSocketContext socketHandle; + uint32_t optionValue = 0; + + socketHandle.socketState = SOCKETSTATE_ALLOCATED; + + _Cellular_CheckLibraryStatus_IgnoreAndReturn( CELLULAR_SUCCESS ); + + cellularStatus = Cellular_CommonSocketSetSockOpt( &context, &socketHandle, + CELLULAR_SOCKET_OPTION_LEVEL_SECURE, + CELLULAR_SOCKET_OPTION_SSL_USAGE, + ( const uint8_t * ) &optionValue, sizeof( uint32_t ) ); + + TEST_ASSERT_EQUAL( CELLULAR_INTERNAL_FAILURE, cellularStatus ); +} + /** * @brief Test that option pdn context id failure path case for Cellular_CommonSocketSetSockOpt. */