CK_ATTRIBUTE_PTR pTemplate,
CK_ULONG ulCount )
{
P11Session_t * pxSession = prvSessionPointerFromHandle( hSession );
CK_RV xResult = prvCheckValidSessionAndModule( pxSession );
CK_BYTE * pxFindObjectLabel = NULL;
uint32_t ulIndex;
CK_ATTRIBUTE xAttribute;
if( NULL == pTemplate )
{
xResult = CKR_ARGUMENTS_BAD;
}
if( ( ulCount != 1UL ) && ( ulCount != 2UL ) )
{
xResult = CKR_ARGUMENTS_BAD;
LogError( ( "Failed to initialize find object operation. Find objects "
"does not support searching by %lu attributes. Expected to "
"search with either 1 or 2 attributes.", ( unsigned long int ) ulCount ) );
}
if( xResult == CKR_OK )
{
if( prvOperationActive( pxSession ) == ( CK_BBOOL ) CK_TRUE )
{
xResult = CKR_OPERATION_ACTIVE;
LogError( ( "Failed to initialize find object operation. Find "
"object operation was already in progress." ) );
}
}
if( xResult == CKR_OK )
{
xResult = CKR_TEMPLATE_INCOMPLETE;
for( ulIndex = 0; ulIndex < ulCount; ulIndex++ )
{
xAttribute = pTemplate[ ulIndex ];
if( ( xAttribute.type == CKA_LABEL ) && ( xAttribute.ulValueLen <= pkcs11configMAX_LABEL_LENGTH ) )
{
pxFindObjectLabel = mbedtls_calloc( 1, xAttribute.ulValueLen + 1UL );
if( pxFindObjectLabel != NULL )
{
pxSession->xFindObjectLabelLen = xAttribute.ulValueLen;
pxSession->pxFindObjectLabel = pxFindObjectLabel;
( void ) memcpy( pxSession->pxFindObjectLabel, xAttribute.pValue, xAttribute.ulValueLen );
xResult = CKR_OK;
}
else
{
LogError( ( "Failed to initialize find object operation. Failed to "
"allocate %lu bytes.", ( unsigned long int ) xAttribute.ulValueLen + 1UL ) );
xResult = CKR_HOST_MEMORY;
}
}
else
{
LogDebug( ( "Search parameters other than label are ignored." ) );
}
}
}
if( ( pxSession != NULL ) && ( xResult != CKR_OK ) && ( xResult != CKR_OPERATION_ACTIVE ) )
{
mbedtls_free( pxFindObjectLabel );
pxSession->pxFindObjectLabel = NULL;
pxSession->xFindObjectLabelLen = 0;
}
return xResult;
}