Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Secure Sockets Transport Implementation #146

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions source/cellular_common_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) ) )
Expand Down
36 changes: 36 additions & 0 deletions source/include/cellular_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
7 changes: 5 additions & 2 deletions source/include/cellular_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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;

Expand Down
11 changes: 11 additions & 0 deletions source/include/common/cellular_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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. */
Expand Down
144 changes: 144 additions & 0 deletions test/unit-test/cellular_common_api_utest.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down