Initializes a signature operation.
CK_MECHANISM_PTR pMechanism,
CK_OBJECT_HANDLE hKey )
{
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;
CK_BYTE_PTR pulKeyData = NULL;
CK_ULONG ulKeyDataLength = 0;
int32_t lMbedTLSResult = 0;
if( NULL == pMechanism )
{
PKCS11_PRINT( (
"ERROR: Null signing mechanism provided. \r\n" ) );
xResult = CKR_ARGUMENTS_BAD;
}
if( ( xResult == CKR_OK ) && (
prvOperationActive( pxSession ) == ( CK_BBOOL ) CK_TRUE ) )
{
xResult = CKR_OPERATION_ACTIVE;
}
if( xResult == CKR_OK )
{
&xPalHandle,
&pxLabel,
&xLabelLength );
if( xPalHandle != CK_INVALID_HANDLE )
{
if( xResult != CKR_OK )
{
PKCS11_PRINT( (
"ERROR: Unable to retrieve value of private key for signing %d. \r\n", xResult ) );
xResult = CKR_KEY_HANDLE_INVALID;
}
}
}
if( xResult == CKR_OK )
{
if( xIsPrivate != ( CK_BBOOL ) CK_TRUE )
{
PKCS11_PRINT( (
"ERROR: Sign operation attempted with public key. \r\n" ) );
xResult = CKR_KEY_TYPE_INCONSISTENT;
}
}
if( xResult == CKR_OK )
{
if( pdTRUE == xSemaphoreTake( pxSession->
xSignMutex, portMAX_DELAY ) )
{
mbedtls_pk_free( &pxSession->
xSignKey );
mbedtls_pk_init( &pxSession->
xSignKey );
lMbedTLSResult = mbedtls_pk_parse_key( &pxSession->
xSignKey, pulKeyData, ulKeyDataLength, NULL, 0 );
if( lMbedTLSResult != 0 )
{
PKCS11_PRINT( (
"mbedTLS unable to parse private key for signing. %s : ",
xResult = CKR_KEY_HANDLE_INVALID;
}
}
else
{
xResult = CKR_CANT_LOCK;
}
}
if( xResult == CKR_OK )
{
xKeyType = mbedtls_pk_get_type( &pxSession->
xSignKey );
if( pMechanism->mechanism == CKM_RSA_PKCS )
{
if( xKeyType != MBEDTLS_PK_RSA )
{
PKCS11_PRINT( (
"ERROR: Signing 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: Signing 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 )
{
}
return xResult;
}