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

Add more unit test to cover code_pkcs11.c #195

Merged
merged 4 commits into from
May 16, 2024
Merged
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
4 changes: 2 additions & 2 deletions docs/doxygen/include/size_table.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<tr>
<td>core_pkcs11.c</td>
<td><center>0.8K</center></td>
<td><center>0.8K</center></td>
<td><center>0.7K</center></td>
</tr>
<tr>
<td>core_pki_utils.c</td>
Expand All @@ -25,6 +25,6 @@
<tr>
<td><b>Total estimates</b></td>
<td><b><center>10.3K</center></b></td>
<td><b><center>8.6K</center></b></td>
<td><b><center>8.5K</center></b></td>
</tr>
</table>
81 changes: 36 additions & 45 deletions source/core_pkcs11.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,35 +41,6 @@

/*-----------------------------------------------------------*/

/** @brief Open a PKCS #11 Session.
*
* \param[out] pxSession Pointer to the session handle to be created.
* \param[out] xSlotId Slot ID to be used for the session.
*
* \return CKR_OK or PKCS #11 error code. (PKCS #11 error codes are positive).
*/
static CK_RV prvOpenSession( CK_SESSION_HANDLE * pxSession,
CK_SLOT_ID xSlotId )
{
CK_RV xResult;
CK_FUNCTION_LIST_PTR pxFunctionList;

xResult = C_GetFunctionList( &pxFunctionList );

if( ( xResult == CKR_OK ) && ( pxFunctionList != NULL ) && ( pxFunctionList->C_OpenSession != NULL ) )
{
xResult = pxFunctionList->C_OpenSession( xSlotId,
CKF_SERIAL_SESSION | CKF_RW_SESSION,
NULL, /* Application defined pointer. */
NULL, /* Callback function. */
pxSession );
}

return xResult;
}

/*-----------------------------------------------------------*/

CK_RV xGetSlotList( CK_SLOT_ID ** ppxSlotId,
CK_ULONG * pxSlotCount )
{
Expand Down Expand Up @@ -193,26 +164,27 @@ 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 );
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move C_GetFunctionList call here for CBMC check.


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 ) &&
( NULL != pxFunctionList->C_GetTokenInfo ) &&
( NULL != pxFunctionList->C_InitToken ) )
if( xResult == CKR_OK )
{
/* Check if the token requires further initialization. */
/* MISRA Ref 11.5.1 [Void pointer assignment] */
Expand Down Expand Up @@ -270,13 +242,21 @@ CK_RV xInitializePkcs11Session( CK_SESSION_HANDLE * pxSession )
CK_FUNCTION_LIST_PTR pxFunctionList = NULL;
CK_ULONG xSlotCount = 0;

xResult = C_GetFunctionList( &pxFunctionList );

if( pxSession == NULL )
{
xResult = CKR_ARGUMENTS_BAD;
}

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

if( ( xResult == CKR_OK ) && ( pxFunctionList == NULL ) )
{
xResult = CKR_FUNCTION_FAILED;
}
}

/* Initialize the module. */
if( xResult == CKR_OK )
{
Expand All @@ -295,19 +275,30 @@ CK_RV xInitializePkcs11Session( CK_SESSION_HANDLE * pxSession )
}

/* Open a PKCS #11 session. */
if( ( xResult == CKR_OK ) && ( pxSlotId != NULL ) && ( xSlotCount >= 1UL ) )
if( ( xResult == CKR_OK ) && ( xSlotCount >= 1UL ) )
Copy link
Member Author

@chinglee-iot chinglee-iot May 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

( pxSlotId != NULL ) is a redundant check since the value xResult ensure this won't be NULL.

{
/* We will take the first slot available.
* If your application has multiple slots, insert logic
* for selecting an appropriate slot here.
*/
xResult = prvOpenSession( pxSession, pxSlotId[ 0 ] );
if( pxFunctionList->C_OpenSession != NULL )
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unwind prvOpenSession here to reuse the pxFunctionList

{
xResult = pxFunctionList->C_OpenSession( pxSlotId[ 0 ],
CKF_SERIAL_SESSION | CKF_RW_SESSION,
NULL, /* Application defined pointer. */
NULL, /* Callback function. */
pxSession );
}
else
{
xResult = CKR_FUNCTION_FAILED;
}

/* Free the memory allocated by xGetSlotList. */
pkcs11configPKCS11_FREE( pxSlotId );
}

if( ( xResult == CKR_OK ) && ( pxFunctionList != NULL ) && ( pxFunctionList->C_Login != NULL ) )
if( ( xResult == CKR_OK ) && ( pxFunctionList->C_Login != NULL ) )
{
xResult = pxFunctionList->C_Login( *pxSession,
CKU_USER,
Expand Down
Loading
Loading