-
Notifications
You must be signed in to change notification settings - Fork 48
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
chinglee-iot
merged 4 commits into
FreeRTOS:main
from
chinglee-iot:update-core-pkcs11-unit-test
May 16, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 ) | ||
{ | ||
|
@@ -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 ); | ||
|
||
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] */ | ||
|
@@ -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 ) | ||
{ | ||
|
@@ -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 ) ) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.