corePKCS11  V3.0.0
PKCS #11 Cryptoki Library
C_SignInit
CK_DECLARE_FUNCTION( CK_RV, C_SignInit )( 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;
CK_OBJECT_HANDLE xPalHandle;
CK_BYTE_PTR pxLabel = NULL;
CK_ULONG xLabelLength = 0;
mbedtls_pk_type_t xKeyType;
P11Session_t * pxSession = prvSessionPointerFromHandle( hSession );
CK_RV xResult = prvCheckValidSessionAndModule( pxSession );
CK_BYTE_PTR pulKeyData = NULL;
CK_ULONG ulKeyDataLength = 0;
int32_t lMbedTLSResult = 0;
if( NULL == pMechanism )
{
LogError( ( "Failed to initialize sign operation. NULL pointer to "
"signing mechanism provided." ) );
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 ) )
{
LogError( ( "Failed to initialize sign operation. Operation already active." ) );
xResult = CKR_OPERATION_ACTIVE;
}
/* Retrieve key value from storage. */
if( xResult == CKR_OK )
{
prvFindObjectInListByHandle( hKey,
&xPalHandle,
&pxLabel,
&xLabelLength );
if( xPalHandle != CK_INVALID_HANDLE )
{
xResult = PKCS11_PAL_GetObjectValue( xPalHandle, &pulKeyData, &ulKeyDataLength, &xIsPrivate );
if( xResult != CKR_OK )
{
LogError( ( "Failed to initialize sign operation. Unable to "
"retrieve value of private key for signing 0x%0lX.", ( unsigned long int ) xResult ) );
xResult = CKR_KEY_HANDLE_INVALID;
}
}
else
{
LogDebug( ( "Could not find PKCS #11 PAL Handle." ) );
xResult = CKR_KEY_HANDLE_INVALID;
}
}
/* Check that a private 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_TRUE )
{
LogError( ( "Failed to initialize sign operation. Sign operation "
"attempted with public key." ) );
xResult = CKR_KEY_TYPE_INCONSISTENT;
}
}
/* Convert the private key from storage format to mbedTLS usable format. */
if( xResult == CKR_OK )
{
/* Grab the sign mutex. This ensures that no signing operation
* is underway on another thread where modification of key would lead to hard fault.*/
if( 0 == mbedtls_mutex_lock( &pxSession->xSignMutex ) )
{
if( ( pxSession->xSignKeyHandle == CK_INVALID_HANDLE ) || ( pxSession->xSignKeyHandle != hKey ) )
{
pxSession->xSignKeyHandle = CK_INVALID_HANDLE;
mbedtls_pk_free( &pxSession->xSignKey );
mbedtls_pk_init( &pxSession->xSignKey );
lMbedTLSResult = mbedtls_pk_parse_key( &pxSession->xSignKey, pulKeyData, ulKeyDataLength, NULL, 0 );
if( lMbedTLSResult != 0 )
{
LogError( ( "Failed to initialize sign operation. "
"mbedtls_pk_parse_key failed: mbed TLS error = %s : %s.",
mbedtlsHighLevelCodeOrDefault( lMbedTLSResult ),
mbedtlsLowLevelCodeOrDefault( lMbedTLSResult ) ) );
xResult = CKR_KEY_HANDLE_INVALID;
}
else
{
pxSession->xSignKeyHandle = hKey;
}
}
( void ) mbedtls_mutex_unlock( &pxSession->xSignMutex );
/* Key has been parsed into mbedTLS pk structure.
* Free the memory allocated to copy the key out of flash. */
PKCS11_PAL_GetObjectValueCleanup( pulKeyData, ulKeyDataLength );
}
else
{
LogError( ( "Failed to initialize sign operation. Could not "
"take xSignMutex." ) );
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_PKCS )
{
if( xKeyType != MBEDTLS_PK_RSA )
{
LogError( ( "Failed to initialize sign operation. Signing key "
"type (0x%0lX) does not match RSA mechanism.", ( unsigned long int ) xKeyType ) );
xResult = CKR_KEY_TYPE_INCONSISTENT;
}
}
else if( pMechanism->mechanism == CKM_ECDSA )
{
if( ( xKeyType != MBEDTLS_PK_ECDSA ) && ( xKeyType != MBEDTLS_PK_ECKEY ) )
{
LogError( ( "Failed to initialize sign operation. Signing key "
"type (0x%0lX) does not match ECDSA mechanism.", ( unsigned long int ) xKeyType ) );
xResult = CKR_KEY_TYPE_INCONSISTENT;
}
}
else
{
LogError( ( "Failed to initialize sign operation. Unsupported "
"mechanism type (0x%0lX).", ( unsigned long int ) pMechanism->mechanism ) );
xResult = CKR_MECHANISM_INVALID;
}
}
if( xResult == CKR_OK )
{
pxSession->xOperationSignMechanism = pMechanism->mechanism;
LogDebug( ( "Successfully started sign operation." ) );
}
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.
CK_DECLARE_FUNCTION
#define CK_DECLARE_FUNCTION(returnType, name)
Macro for defining a PKCS #11 functions.
Definition: core_pkcs11.h:72