corePKCS11  v3.2.0
PKCS #11 Cryptoki Library
C_FindObjectsInit

Initializes an object search operation.

CK_DECLARE_FUNCTION( CK_RV, C_FindObjectsInit )( CK_SESSION_HANDLE hSession,
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 )
{
/* See explanation in prvCheckValidSessionAndModule for this exception. */
/* coverity[misra_c_2012_rule_10_5_violation] */
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." ) );
}
}
/* Search template for label.
* NOTE: This port only supports looking up objects by CKA_LABEL and all
* other search attributes are ignored. */
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 ) )
{
/* Plus one to leave room for a NULL terminator. */
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." ) );
}
}
}
/* Clean up memory if there was an error parsing the template. */
if( ( pxSession != NULL ) && ( xResult != CKR_OK ) && ( xResult != CKR_OPERATION_ACTIVE ) )
{
mbedtls_free( pxFindObjectLabel );
pxSession->pxFindObjectLabel = NULL;
pxSession->xFindObjectLabelLen = 0;
}
return xResult;
}
See also
C_FindObjects() and C_FindObjectsFinal() which must be called after C_FindObjectsInit().
Note
FindObjects parameters are shared by a session. Calling C_FindObjectsInit(), C_FindObjects(), and C_FindObjectsFinal() with the same session across different tasks may lead to unexpected results.
Parameters
[in]hSessionHandle of a valid PKCS #11 session.
[in]pTemplatePointer to a template which specifies the object attributes to match. In this port, the only searchable attribute is object label. All other attributes will be ignored.
[in]ulCountThe number of attributes in pTemplate.
Returns
CKR_OK if successful.
LogDebug
#define LogDebug(message)
Macro that is called in the corePKCS11 library for logging "Debug" level messages.
Definition: core_pkcs11_config_defaults.h:375
C_FindObjectsInit
CK_RV C_FindObjectsInit(CK_SESSION_HANDLE hSession, CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount)
Initializes an object search operation.
Definition: core_pkcs11_mbedtls.c:3285
P11Session_t::xFindObjectLabelLen
CK_ULONG xFindObjectLabelLen
Size of current search label.
Definition: core_pkcs11_mbedtls.c:291
prvOperationActive
static CK_BBOOL prvOperationActive(const P11Session_t *pxSession)
Determines if an operation is in progress.
Definition: core_pkcs11_mbedtls.c:385
prvCheckValidSessionAndModule
static CK_RV prvCheckValidSessionAndModule(const P11Session_t *pxSession)
Helper to check if the current session is initialized and valid.
Definition: core_pkcs11_mbedtls.c:323
CK_DECLARE_FUNCTION
#define CK_DECLARE_FUNCTION(returnType, name)
Macro for defining a PKCS #11 functions.
Definition: core_pkcs11.h:75
prvSessionPointerFromHandle
static P11Session_t * prvSessionPointerFromHandle(CK_SESSION_HANDLE xSession)
Maps an opaque caller session handle into its internal state structure.
Definition: core_pkcs11_mbedtls.c:365
P11Session_t
Session structure.
Definition: core_pkcs11_mbedtls.c:286
P11Session_t::pxFindObjectLabel
CK_BYTE * pxFindObjectLabel
Pointer to the label for the search in progress. Should be NULL if no search in progress.
Definition: core_pkcs11_mbedtls.c:290
LogError
#define LogError(message)
Macro that is called in the corePKCS11 library for logging "Error" level messages.
Definition: core_pkcs11_config_defaults.h:315
pkcs11configMAX_LABEL_LENGTH
#define pkcs11configMAX_LABEL_LENGTH
Maximum length (in characters) for a PKCS #11 CKA_LABEL attribute.
Definition: core_pkcs11_config_defaults.h:104