corePKCS11  V3.0.0
PKCS #11 Cryptoki Library
C_FindObjects
CK_DECLARE_FUNCTION( CK_RV, C_FindObjects )( CK_SESSION_HANDLE hSession,
CK_OBJECT_HANDLE_PTR phObject,
CK_ULONG ulMaxObjectCount,
CK_ULONG_PTR pulObjectCount )
{
P11Session_t * pxSession = prvSessionPointerFromHandle( hSession );
CK_RV xResult = prvCheckValidSessionAndModule( pxSession );
CK_BYTE_PTR pucObjectValue = NULL;
CK_ULONG xObjectLength = 0;
/* See explanation in prvCheckValidSessionAndModule for this exception. */
/* coverity[misra_c_2012_rule_10_5_violation] */
CK_BBOOL xIsPrivate = ( CK_BBOOL ) CK_TRUE;
CK_BYTE xByte = 0;
CK_OBJECT_HANDLE xPalHandle = CK_INVALID_HANDLE;
CK_ULONG ulIndex;
/*
* Check parameters.
*/
if( ( NULL == phObject ) ||
( NULL == pulObjectCount ) )
{
LogError( ( "Failed to find objects. The object handle or the object "
"count pointer was NULL." ) );
xResult = CKR_ARGUMENTS_BAD;
}
if( xResult == CKR_OK )
{
if( pxSession->pxFindObjectLabel == NULL )
{
LogError( ( "Failed to find objects. The PKCS #11 stack must be "
"initialized before any operations." ) );
xResult = CKR_OPERATION_NOT_INITIALIZED;
}
if( 1u != ulMaxObjectCount )
{
xResult = CKR_ARGUMENTS_BAD;
LogError( ( "Failed to find objects. Searching for anything other "
"than 1 object at a time is not supported." ) );
}
}
if( xResult == CKR_OK )
{
/* Try to find the object in module's list first. */
prvFindObjectInListByLabel( pxSession->pxFindObjectLabel, pxSession->xFindObjectLabelLen, &xPalHandle, phObject );
/* Check with the PAL if the object was previously stored. */
if( *phObject == CK_INVALID_HANDLE )
{
LogDebug( ( "Could not find the object handle in the list. "
"Trying to search PKCS #11 PAL for object." ) );
xPalHandle = PKCS11_PAL_FindObject( pxSession->pxFindObjectLabel, pxSession->xFindObjectLabelLen );
}
if( xPalHandle != CK_INVALID_HANDLE )
{
xResult = PKCS11_PAL_GetObjectValue( xPalHandle, &pucObjectValue, &xObjectLength, &xIsPrivate );
if( xResult == CKR_OK )
{
for( ulIndex = 0; ulIndex < xObjectLength; ulIndex++ )
{
xByte = pucObjectValue[ ulIndex ];
if( xByte != 0UL )
{
break;
}
}
if( xByte == 0UL ) /* Deleted objects are overwritten completely w/ zero. */
{
LogDebug( ( "Found an overwritten object." ) );
*phObject = CK_INVALID_HANDLE;
}
else
{
LogDebug( ( "Found object in PAL. Adding object handle to list." ) );
xResult = prvAddObjectToList( xPalHandle, phObject, pxSession->pxFindObjectLabel, pxSession->xFindObjectLabelLen );
*pulObjectCount = 1;
}
PKCS11_PAL_GetObjectValueCleanup( pucObjectValue, xObjectLength );
}
}
else
{
/* Note: Objects living in header files are not destroyed. */
/* According to the PKCS #11 standard, not finding an object results in a CKR_OK return value with an object count of 0. */
*pulObjectCount = 0;
}
}
/* Clean up memory if there was an error finding the object. */
if( xResult != CKR_OK )
{
if( pxSession != NULL )
{
mbedtls_free( pxSession->pxFindObjectLabel );
pxSession->pxFindObjectLabel = NULL;
pxSession->xFindObjectLabelLen = 0;
}
}
return xResult;
}
PKCS11_PAL_GetObjectValueCleanup
void PKCS11_PAL_GetObjectValueCleanup(CK_BYTE_PTR pucData, CK_ULONG ulDataSize)
Cleanup after PKCS11_GetObjectValue().
PKCS11_PAL_GetObjectValue
CK_RV PKCS11_PAL_GetObjectValue(CK_OBJECT_HANDLE xHandle, CK_BYTE_PTR *ppucData, CK_ULONG_PTR pulDataSize, CK_BBOOL *pIsPrivate)
Gets the value of an object in storage, by handle.
PKCS11_PAL_FindObject
CK_OBJECT_HANDLE PKCS11_PAL_FindObject(CK_BYTE_PTR pxLabel, CK_ULONG usLength)
Translates a PKCS #11 label into an object handle.
CK_DECLARE_FUNCTION
#define CK_DECLARE_FUNCTION(returnType, name)
Macro for defining a PKCS #11 functions.
Definition: core_pkcs11.h:72