FreeRTOS: PKCS11
PKCS11 Cryptoki Library
Return to main page ↑
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;
PKCS11_PRINT( ( "ERROR: Find objects does not support searching by %d attributes. \r\n", 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;
PKCS11_PRINT( ( "ERROR: Find object operation already in progress. \r\n" ) );
}
}
/* Malloc space to save template information. */
if( xResult == CKR_OK )
{
/* Plus one to leave room for a NULL terminator. */
pxFindObjectLabel = pvPortMalloc( pTemplate->ulValueLen + 1UL );
pxSession->xFindObjectLabelLen = pTemplate->ulValueLen;
pxSession->pxFindObjectLabel = pxFindObjectLabel;
if( pxFindObjectLabel != NULL )
{
/* Plus one so buffer is guaranteed to end with a NULL terminator. */
( void ) memset( pxFindObjectLabel, 0, pTemplate->ulValueLen + 1UL );
}
else
{
xResult = CKR_HOST_MEMORY;
}
}
/* 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++ ) /* TODO: Re-evaluate the need for this for loop... we are making bad assumptions if 2 objects have the same label anyhow! */
{
xAttribute = pTemplate[ ulIndex ];
if( xAttribute.type == CKA_LABEL )
{
( void ) memcpy( pxSession->pxFindObjectLabel, xAttribute.pValue, xAttribute.ulValueLen );
xResult = CKR_OK;
}
else
{
PKCS11_WARNING_PRINT( ( "WARNING: Search parameters other than label are ignored.\r\n" ) );
}
}
}
/* Clean up memory if there was an error parsing the template. */
if( ( pxSession != NULL ) && ( xResult != CKR_OK ) )
{
vPortFree( 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. Else, see PKCS #11 specification for more information.
CK_DECLARE_FUNCTION
#define CK_DECLARE_FUNCTION(returnType, name)
Macro for defining a PKCS #11 functions.
Definition: iot_pkcs11.h:66
prvOperationActive
static CK_BBOOL prvOperationActive(const P11Session_t *pxSession)
Determines if an operation is in progress.
Definition: iot_pkcs11_mbedtls.c:303
PKCS11_PRINT
#define PKCS11_PRINT(X)
Macro for logging in PKCS #11.
Definition: iot_pkcs11_mbedtls.c:103
CK_ATTRIBUTE
Definition: pkcs11t.h:565
prvSessionPointerFromHandle
static P11Session_t * prvSessionPointerFromHandle(CK_SESSION_HANDLE xSession)
Maps an opaque caller session handle into its internal state structure.
Definition: iot_pkcs11_mbedtls.c:287
P11Session_t::xFindObjectLabelLen
CK_ULONG xFindObjectLabelLen
Size of current search label.
Definition: iot_pkcs11_mbedtls.c:222
prvCheckValidSessionAndModule
static CK_RV prvCheckValidSessionAndModule(const P11Session_t *pxSession)
Helper to check if the current session is initialized and valid.
Definition: iot_pkcs11_mbedtls.c:248
P11Session_t
Session structure.
Definition: iot_pkcs11_mbedtls.c:217
PKCS11_WARNING_PRINT
#define PKCS11_WARNING_PRINT(X)
Macro for logging warnings in PKCS #11.
Definition: iot_pkcs11_mbedtls.c:110
P11Session_t::pxFindObjectLabel
CK_BYTE * pxFindObjectLabel
Pointer to the label for the search in progress. Should be NULL if no search in progress.
Definition: iot_pkcs11_mbedtls.c:221
C_FindObjectsInit
CK_RV C_FindObjectsInit(CK_SESSION_HANDLE hSession, CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount)
Initializes an object search operation.
Definition: iot_pkcs11_mbedtls.c:2857