FreeRTOS: PKCS11
PKCS11 Cryptoki Library
Return to main page ↑
C_VerifyInit

Initializes a verification operation.

CK_DECLARE_FUNCTION( CK_RV, C_VerifyInit )( CK_SESSION_HANDLE hSession,
CK_MECHANISM_PTR pMechanism,
CK_OBJECT_HANDLE hKey )
{
/* See explanation in prvCheckValidSessionAndModule for this exception. */
/* coverity[misra_c_2012_rule_10_5_violation] */
CK_BBOOL xIsPrivate = ( CK_BBOOL ) CK_TRUE;
P11Session_t * pxSession;
CK_BYTE_PTR pucKeyData = NULL;
CK_ULONG ulKeyDataLength = 0;
mbedtls_pk_type_t xKeyType;
CK_OBJECT_HANDLE xPalHandle = CK_INVALID_HANDLE;
CK_BYTE_PTR pxLabel = NULL;
CK_ULONG xLabelLength = 0;
pxSession = prvSessionPointerFromHandle( hSession );
CK_RV xResult = prvCheckValidSessionAndModule( pxSession );
if( NULL == pMechanism )
{
PKCS11_PRINT( ( "ERROR: Null verification mechanism provided. \r\n" ) );
xResult = CKR_ARGUMENTS_BAD;
}
/* See explanation in prvCheckValidSessionAndModule for this exception. */
/* coverity[misra_c_2012_rule_10_5_violation] */
if( ( xResult == CKR_OK ) && ( prvOperationActive( pxSession ) == ( CK_BBOOL ) CK_TRUE ) )
{
xResult = CKR_OPERATION_ACTIVE;
}
/* Retrieve key value from storage. */
if( xResult == CKR_OK )
{
&xPalHandle,
&pxLabel,
&xLabelLength );
if( xPalHandle != CK_INVALID_HANDLE )
{
xResult = PKCS11_PAL_GetObjectValue( xPalHandle, &pucKeyData, &ulKeyDataLength, &xIsPrivate );
if( xResult != CKR_OK )
{
PKCS11_PRINT( ( "ERROR: Unable to retrieve value of private key for signing %d. \r\n", xResult ) );
}
}
else
{
xResult = CKR_KEY_HANDLE_INVALID;
}
}
/* Check that a public key was retrieved. */
if( xResult == CKR_OK )
{
/* See explanation in prvCheckValidSessionAndModule for this exception. */
/* coverity[misra_c_2012_rule_10_5_violation] */
if( xIsPrivate != ( CK_BBOOL ) CK_FALSE )
{
PKCS11_PRINT( ( "ERROR: Verify operation attempted with private key. \r\n" ) );
xResult = CKR_KEY_TYPE_INCONSISTENT;
}
}
if( xResult == CKR_OK )
{
if( pdTRUE == xSemaphoreTake( pxSession->xVerifyMutex, portMAX_DELAY ) )
{
/* Free the public key context if it exists.
* TODO: Check if the key is the same as used by last verify operation. */
mbedtls_pk_free( &pxSession->xVerifyKey );
mbedtls_pk_init( &pxSession->xVerifyKey );
if( 0 != mbedtls_pk_parse_public_key( &pxSession->xVerifyKey, pucKeyData, ulKeyDataLength ) )
{
if( 0 != mbedtls_pk_parse_key( &pxSession->xVerifyKey, pucKeyData, ulKeyDataLength, NULL, 0 ) )
{
PKCS11_PRINT( ( "ERROR: Unable to parse public key for verification. \r\n" ) );
xResult = CKR_KEY_HANDLE_INVALID;
}
}
( void ) xSemaphoreGive( pxSession->xVerifyMutex );
PKCS11_PAL_GetObjectValueCleanup( pucKeyData, ulKeyDataLength );
}
else
{
xResult = CKR_CANT_LOCK;
}
}
/* Check that the mechanism and key type are compatible, supported. */
if( xResult == CKR_OK )
{
xKeyType = mbedtls_pk_get_type( &pxSession->xSignKey );
if( pMechanism->mechanism == CKM_RSA_X_509 )
{
if( xKeyType != MBEDTLS_PK_RSA )
{
PKCS11_PRINT( ( "ERROR: Verification key type (%d) does not match RSA mechanism \r\n", xKeyType ) );
xResult = CKR_KEY_TYPE_INCONSISTENT;
}
}
else if( pMechanism->mechanism == CKM_ECDSA )
{
if( ( xKeyType != MBEDTLS_PK_ECDSA ) && ( xKeyType != MBEDTLS_PK_ECKEY ) )
{
PKCS11_PRINT( ( "ERROR: Verification key type (%d) does not match ECDSA mechanism \r\n", xKeyType ) );
xResult = CKR_KEY_TYPE_INCONSISTENT;
}
}
else
{
PKCS11_PRINT( ( "ERROR: Unsupported mechanism type %d \r\n", pMechanism->mechanism ) );
xResult = CKR_MECHANISM_INVALID;
}
}
if( xResult == CKR_OK )
{
pxSession->xOperationVerifyMechanism = pMechanism->mechanism;
}
return xResult;
}
See also
C_Verify() completes verifications initiated by C_VerifyInit().
Note
C_Verify() parameters are shared by a session. Calling C_VerifyInit() & C_Verify() with the same session across different tasks may lead to unexpected results.
Parameters
[in]hSessionHandle of a valid PKCS #11 session.
[in]pMechanismMechanism used to verify signature. This port supports the following mechanisms:
  • CKM_RSA_X_509 for RSA verifications
  • CKM_ECDSA for elliptic curve verifications
[in]hKeyThe handle of the public key to be used for verification. Key must be compatible with the mechanism chosen by pxMechanism.
Returns
CKR_OK if successful. Else, see PKCS #11 specification for more information.
P11Session_t::xOperationVerifyMechanism
CK_MECHANISM_TYPE xOperationVerifyMechanism
The mechanism of verify operation in progress. Set during C_VerifyInit.
Definition: iot_pkcs11_mbedtls.c:223
CK_DECLARE_FUNCTION
#define CK_DECLARE_FUNCTION(returnType, name)
Macro for defining a PKCS #11 functions.
Definition: iot_pkcs11.h:66
P11Session_t::xSignKey
mbedtls_pk_context xSignKey
Signing key. Set during C_SignInit.
Definition: iot_pkcs11_mbedtls.c:228
P11Session_t::xVerifyMutex
SemaphoreHandle_t xVerifyMutex
Protects the verification key from being modified while in use.
Definition: iot_pkcs11_mbedtls.c:224
prvOperationActive
static CK_BBOOL prvOperationActive(const P11Session_t *pxSession)
Determines if an operation is in progress.
Definition: iot_pkcs11_mbedtls.c:303
prvFindObjectInListByHandle
static void prvFindObjectInListByHandle(CK_OBJECT_HANDLE xAppHandle, CK_OBJECT_HANDLE_PTR pxPalHandle, CK_BYTE_PTR *ppcLabel, CK_ULONG_PTR pxLabelLength)
Looks up a PKCS #11 object's label and PAL handle given an application handle.
Definition: iot_pkcs11_mbedtls.c:956
PKCS11_PRINT
#define PKCS11_PRINT(X)
Macro for logging in PKCS #11.
Definition: iot_pkcs11_mbedtls.c:103
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
PKCS11_PAL_GetObjectValueCleanup
void PKCS11_PAL_GetObjectValueCleanup(CK_BYTE_PTR pucData, CK_ULONG ulDataSize)
Cleanup after PKCS11_GetObjectValue().
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::xVerifyKey
mbedtls_pk_context xVerifyKey
Verification key. Set during C_VerifyInit.
Definition: iot_pkcs11_mbedtls.c:225
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.
P11Session_t
Session structure.
Definition: iot_pkcs11_mbedtls.c:217
C_VerifyInit
CK_RV C_VerifyInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism, CK_OBJECT_HANDLE hKey)
Initializes a verification operation.
Definition: iot_pkcs11_mbedtls.c:3690