Skip to content

Commit

Permalink
Fix for CBMC
Browse files Browse the repository at this point in the history
  • Loading branch information
chinglee-iot committed May 16, 2024
1 parent b0e2fbf commit da9e3a8
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 9 deletions.
19 changes: 11 additions & 8 deletions source/core_pkcs11.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,21 +164,24 @@ CK_RV xInitializePkcs11Token( void )
CK_FLAGS xTokenFlags = 0;
CK_TOKEN_INFO_PTR pxTokenInfo = NULL;

xResult = C_GetFunctionList( &pxFunctionList );
xResult = xInitializePKCS11();

if( ( pxFunctionList == NULL ) || ( pxFunctionList->C_GetTokenInfo == NULL ) || ( pxFunctionList->C_InitToken == NULL ) )
if( ( xResult == CKR_OK ) || ( xResult == CKR_CRYPTOKI_ALREADY_INITIALIZED ) )
{
xResult = CKR_FUNCTION_FAILED;
xResult = xGetSlotList( &pxSlotId, &xSlotCount );
}

if( xResult == CKR_OK )
{
xResult = xInitializePKCS11();
}
xResult = C_GetFunctionList( &pxFunctionList );

if( ( xResult == CKR_OK ) || ( xResult == CKR_CRYPTOKI_ALREADY_INITIALIZED ) )
{
xResult = xGetSlotList( &pxSlotId, &xSlotCount );
if( xResult == CKR_OK )
{
if( ( pxFunctionList == NULL ) || ( pxFunctionList->C_GetTokenInfo == NULL ) || ( pxFunctionList->C_InitToken == NULL ) )
{
xResult = CKR_FUNCTION_FAILED;
}
}
}

if( xResult == CKR_OK )
Expand Down
97 changes: 96 additions & 1 deletion test/wrapper_utest/core_pkcs11_utest.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,58 @@ static CK_RV prvSetFunctionList( CK_FUNCTION_LIST_PTR_PTR ppxPtr )
return CKR_OK;
}

/*!
* @brief Create a stub for the PKCS #11 function list.
*
* Fails on the fourth call in order to create coverage for a nested branch.
*
*/
static CK_RV prvSetFunctionList2( CK_FUNCTION_LIST_PTR_PTR ppxPtr )
{
static uint32_t ulCalls = 0;
CK_RV xResult = CKR_OK;

ulCalls++;

if( ulCalls == 3 )
{
xResult = CKR_ARGUMENTS_BAD;
*ppxPtr = NULL;
}
else
{
*ppxPtr = &prvP11FunctionList;
}

return xResult;
}

/*!
* @brief Create a stub for the PKCS #11 function list.
*
* Fails on the fourth call in order to create coverage for a nested branch.
*
*/
static CK_RV prvSetFunctionList3( CK_FUNCTION_LIST_PTR_PTR ppxPtr )
{
static uint32_t ulCalls = 0;
CK_RV xResult = CKR_OK;

ulCalls++;

if( ulCalls == 3 )
{
xResult = CKR_OK;
*ppxPtr = NULL;
}
else
{
*ppxPtr = &prvP11FunctionList;
}

return xResult;
}

/*!
* @brief Return empty function list
*
Expand Down Expand Up @@ -552,6 +604,23 @@ void test_IotPkcs11_xInitializePkcs11TokenAlreadyInit( void )
TEST_ASSERT_EQUAL( CKR_OK, xResult );
}

/*!
* @brief xInitializePkcs11Token xInitializePKCS11 return error.
*
*/
void test_IotPkcs11_xInitializePkcs11TokenInitFailed( void )
{
CK_RV xResult = CKR_OK;

C_GetFunctionList_IgnoreAndReturn( CKR_OK );
C_GetFunctionList_Stub( ( void * ) &prvSetFunctionList );
C_Initialize_IgnoreAndReturn( CKR_GENERAL_ERROR );

xResult = xInitializePkcs11Token();

TEST_ASSERT_EQUAL( CKR_GENERAL_ERROR, xResult );
}

/*!
* @brief xInitializePkcs11Token C_GetTokenInfo failure due to memory constraint.
*
Expand Down Expand Up @@ -617,7 +686,33 @@ void test_IotPkcs11_xInitializePkcs11TokenBadFunctionList( void )
{
CK_RV xResult = CKR_OK;

C_GetFunctionList_IgnoreAndReturn( CKR_ARGUMENTS_BAD );
C_GetFunctionList_IgnoreAndReturn( CKR_OK );
C_GetFunctionList_Stub( ( void * ) &prvSetFunctionList2 );
C_Initialize_IgnoreAndReturn( CKR_OK );
pvPkcs11Malloc_Stub( pvPkcs11MallocCb );
vPkcs11Free_Stub( vPkcs11FreeCb );
C_GetSlotList_Stub( ( void * ) xGet1Item );

xResult = xInitializePkcs11Token();

TEST_ASSERT_EQUAL( CKR_ARGUMENTS_BAD, xResult );
}

/*!
* @brief xInitializePkcs11Token failure due to bad C_GetFunctionList.
*
*/
void test_IotPkcs11_xInitializePkcs11TokenEmptyFunctionList( void )
{
CK_RV xResult = CKR_OK;

C_GetFunctionList_IgnoreAndReturn( CKR_OK );
C_GetFunctionList_Stub( ( void * ) &prvSetFunctionList3 );
C_Initialize_IgnoreAndReturn( CKR_OK );
pvPkcs11Malloc_Stub( pvPkcs11MallocCb );
vPkcs11Free_Stub( vPkcs11FreeCb );
C_GetSlotList_Stub( ( void * ) xGet1Item );

xResult = xInitializePkcs11Token();

TEST_ASSERT_EQUAL( CKR_FUNCTION_FAILED, xResult );
Expand Down

0 comments on commit da9e3a8

Please sign in to comment.