Generates a public-key/private-key pair.
CK_MECHANISM_PTR pMechanism,
CK_ATTRIBUTE_PTR pPublicKeyTemplate,
CK_ULONG ulPublicKeyAttributeCount,
CK_ATTRIBUTE_PTR pPrivateKeyTemplate,
CK_ULONG ulPrivateKeyAttributeCount,
CK_OBJECT_HANDLE_PTR phPublicKey,
CK_OBJECT_HANDLE_PTR phPrivateKey )
{
int32_t lMbedTLSResult = 0;
uint32_t ulIndex = 0;
mbedtls_pk_context xCtx = { 0 };
CK_ATTRIBUTE_PTR pxPrivateLabel = NULL;
CK_ATTRIBUTE_PTR pxPublicLabel = NULL;
CK_OBJECT_HANDLE xPalPublic = CK_INVALID_HANDLE;
CK_OBJECT_HANDLE xPalPrivate = CK_INVALID_HANDLE;
uint32_t xAttributeMap = 0;
CK_RV xAddObjectListResult = CKR_OK;
#ifdef pkcs11configSUPPRESS_ECDSA_MECHANISM
if( xResult == CKR_OK )
{
LogDebug( (
"ECDSA Mechanism is suppressed on this port." ) );
xResult = CKR_MECHANISM_INVALID;
}
#endif
if( xResult == CKR_OK )
{
if( ( pPublicKeyTemplate == NULL ) ||
( pPrivateKeyTemplate == NULL ) ||
( phPublicKey == NULL ) ||
( phPrivateKey == NULL ) ||
( pMechanism == NULL ) )
{
LogError( (
"Failed generating a key pair. One of the arguments "
"was NULL." ) );
xResult = CKR_ARGUMENTS_BAD;
}
}
if( xResult == CKR_OK )
{
if( pucDerFile == NULL )
{
LogError( (
"Failed generating a key pair. Could not allocated a "
xResult = CKR_HOST_MEMORY;
}
}
if( xResult == CKR_OK )
{
if( CKM_EC_KEY_PAIR_GEN != pMechanism->mechanism )
{
LogError( (
"Failed generating a key pair. CKM_EC_KEY_PAIR_GEN is "
"the only valid key generation mechanism currently." ) );
xResult = CKR_MECHANISM_INVALID;
}
}
if( xResult == CKR_OK )
{
for( ulIndex = 0; ulIndex < ulPrivateKeyAttributeCount; ++ulIndex )
{
&pPrivateKeyTemplate[ ulIndex ],
&xAttributeMap );
if( xResult != CKR_OK )
{
break;
}
}
if( ( xResult == CKR_OK ) && ( ( xAttributeMap & xPrivateRequiredAttributeMap ) != xPrivateRequiredAttributeMap ) )
{
LogError( (
"Failed generating a key pair. Attributes were missing "
"in the private key template." ) );
xResult = CKR_TEMPLATE_INCOMPLETE;
}
}
if( xResult == CKR_OK )
{
xAttributeMap = 0;
for( ulIndex = 0; ulIndex < ulPublicKeyAttributeCount; ++ulIndex )
{
&pPublicKeyTemplate[ ulIndex ],
&xAttributeMap );
if( xResult != CKR_OK )
{
break;
}
}
if( ( xResult == CKR_OK ) && ( ( xAttributeMap & xPublicRequiredAttributeMap ) != xPublicRequiredAttributeMap ) )
{
LogError( (
"Failed generating a key pair. Attributes were missing "
"in the public key template." ) );
xResult = CKR_TEMPLATE_INCOMPLETE;
}
}
if( xResult == CKR_OK )
{
mbedtls_pk_init( &xCtx );
lMbedTLSResult = mbedtls_pk_setup( &xCtx, mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY ) );
if( lMbedTLSResult != 0 )
{
LogError( (
"Failed generating a key pair. mbedtls_pk_setup failed: "
"mbed TLS error = %s : %s.",
xResult = CKR_FUNCTION_FAILED;
}
else
{
LogDebug( (
"mbedtls_pk_setup was successful." ) );
}
}
if( xResult == CKR_OK )
{
lMbedTLSResult = mbedtls_ecp_gen_key( MBEDTLS_ECP_DP_SECP256R1,
mbedtls_pk_ec( xCtx ),
mbedtls_ctr_drbg_random,
if( 0 != lMbedTLSResult )
{
LogError( (
"Failed generating a key pair. mbedtls_ecp_gen_key "
"failed: mbed TLS error = %s : %s.",
xResult = CKR_FUNCTION_FAILED;
}
}
if( xResult == CKR_OK )
{
if( ( lMbedTLSResult > 0 ) &&
{
LogDebug( (
"PKCS11_PAL_SaveObject returned a %lu PAL handle value "
"for the public key.", ( unsigned long int ) xPalPublic ) );
}
else
{
LogError( (
"Failed generating a key pair. "
"mbedtls_pk_write_pubkey_der failed: mbed TLS error = %s : %s.",
xResult = CKR_GENERAL_ERROR;
}
}
if( xResult == CKR_OK )
{
if( ( lMbedTLSResult > 0 ) &&
{
LogDebug( (
"PKCS11_PAL_SaveObject returned a %lu PAL handle value "
"for the private key.", ( unsigned long int ) xPalPrivate ) );
}
else
{
LogError( (
"Failed generating a key pair. mbedtls_pk_write_key_der "
"failed: mbed TLS error = %s : %s.",
xResult = CKR_GENERAL_ERROR;
}
}
if( ( xPalPublic != CK_INVALID_HANDLE ) && ( xPalPrivate != CK_INVALID_HANDLE ) )
{
xAddObjectListResult =
prvAddObjectToList( xPalPrivate, phPrivateKey, pxPrivateLabel->pValue, pxPrivateLabel->ulValueLen );
if( xAddObjectListResult == CKR_OK )
{
xAddObjectListResult =
prvAddObjectToList( xPalPublic, phPublicKey, pxPublicLabel->pValue, pxPublicLabel->ulValueLen );
}
if( xAddObjectListResult != CKR_OK )
{
LogError( (
"Could not add private key to object list failed with (0x%0lX). Cleaning up PAL objects.", xResult ) );
if( xResult != CKR_OK )
{
LogError( (
"Could not clean up private key. PKCS11_PAL_DestroyObject failed with (0x%0lX).", xResult ) );
}
if( xResult != CKR_OK )
{
LogError( (
"Could not remove private key object from internal list. Failed with (0x%0lX).", xResult ) );
}
if( xResult != CKR_OK )
{
LogError( (
"Could not clean up public key. PKCS11_PAL_DestroyObject failed with (0x%0lX).", xResult ) );
}
if( xResult != CKR_OK )
{
LogError( (
"Could not remove private key object from internal list. Failed with (0x%0lX).", xResult ) );
}
if( xResult == CKR_OK )
{
xResult = xAddObjectListResult;
}
}
}
mbedtls_free( pucDerFile );
mbedtls_pk_free( &xCtx );
return xResult;
}
#define CK_DECLARE_FUNCTION(returnType, name)
Macro for defining a PKCS #11 functions.
Definition: core_pkcs11.h:77
#define LogError(message)
Macro that is called in the corePKCS11 library for logging "Error" level messages.
Definition: core_pkcs11_config_defaults.h:317
#define LogDebug(message)
Macro that is called in the corePKCS11 library for logging "Debug" level messages.
Definition: core_pkcs11_config_defaults.h:377
#define mbedtlsLowLevelCodeOrDefault(mbedTlsCode)
Utility for converting the level-level code in an mbedTLS error to string, if the code-contains a lev...
Definition: core_pkcs11_mbedtls.c:96
#define PRIVATE_IN_TEMPLATE
Definition: core_pkcs11_mbedtls.c:215
#define SIGN_IN_TEMPLATE
Definition: core_pkcs11_mbedtls.c:216
#define VERIFY_IN_TEMPLATE
Definition: core_pkcs11_mbedtls.c:218
static P11Struct_t xP11Context
The global PKCS #11 module object. Entropy/randomness and object lists are shared across PKCS #11 ses...
Definition: core_pkcs11_mbedtls.c:326
#define EC_PARAMS_IN_TEMPLATE
Definition: core_pkcs11_mbedtls.c:217
static P11Session_t * prvSessionPointerFromHandle(CK_SESSION_HANDLE xSession)
Maps an opaque caller session handle into its internal state structure.
Definition: core_pkcs11_mbedtls.c:382
CK_RV C_GenerateKeyPair(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism, CK_ATTRIBUTE_PTR pPublicKeyTemplate, CK_ULONG ulPublicKeyAttributeCount, CK_ATTRIBUTE_PTR pPrivateKeyTemplate, CK_ULONG ulPrivateKeyAttributeCount, CK_OBJECT_HANDLE_PTR phPublicKey, CK_OBJECT_HANDLE_PTR phPrivateKey)
Generates a public-key/private-key pair.
Definition: core_pkcs11_mbedtls.c:5507
#define mbedtlsHighLevelCodeOrDefault(mbedTlsCode)
Utility for converting the high-level code in an mbedTLS error to string, if the code-contains a high...
Definition: core_pkcs11_mbedtls.c:88
static CK_RV prvCheckGenerateKeyPairPublicTemplate(CK_ATTRIBUTE **ppxLabel, CK_ATTRIBUTE *pxAttribute, uint32_t *pulAttributeMap)
Checks that the public key template provided for C_GenerateKeyPair contains all necessary attributes,...
Definition: core_pkcs11_mbedtls.c:5352
static CK_RV prvCheckValidSessionAndModule(const P11Session_t *pxSession)
Helper to check if the current session is initialized and valid.
Definition: core_pkcs11_mbedtls.c:336
static CK_RV prvAddObjectToList(CK_OBJECT_HANDLE xPalHandle, CK_OBJECT_HANDLE_PTR pxAppHandle, const CK_BYTE *pcLabel, CK_ULONG xLabelLength)
Add an object that exists in NVM to the application object array.
Definition: core_pkcs11_mbedtls.c:1183
static CK_RV prvDeleteObjectFromList(CK_OBJECT_HANDLE xPalHandle)
Removes an object from the module object list (xP11Context.xObjectList)
Definition: core_pkcs11_mbedtls.c:1142
static CK_RV prvCheckGenerateKeyPairPrivateTemplate(CK_ATTRIBUTE **ppxLabel, CK_ATTRIBUTE *pxAttribute, uint32_t *pulAttributeMap)
Checks that the private key template provided for C_GenerateKeyPair contains all necessary attributes...
Definition: core_pkcs11_mbedtls.c:5234
CK_RV PKCS11_PAL_DestroyObject(CK_OBJECT_HANDLE xHandle)
Delete an object from NVM.
CK_OBJECT_HANDLE PKCS11_PAL_SaveObject(CK_ATTRIBUTE_PTR pxLabel, CK_BYTE_PTR pucData, CK_ULONG ulDataSize)
Saves an object in non-volatile storage.
#define LABEL_IN_TEMPLATE
Private defines for checking that attribute templates are complete.
Definition: core_pkcs11_mbedtls.c:214
#define pkcs11KEY_GEN_MAX_DER_SIZE
The size of the buffer malloc'ed for the exported public key in C_GenerateKeyPair.
Definition: core_pkcs11_mbedtls.c:200
Session structure.
Definition: core_pkcs11_mbedtls.c:299
mbedtls_ctr_drbg_context xMbedDrbgCtx
CTR-DRBG context for PKCS #11 module - used to generate pseudo-random numbers.
Definition: core_pkcs11_mbedtls.c:286
This port only supports generating elliptic curve P-256 key pairs.