Skip to content

Commit 1707ace

Browse files
committed
Bind PKCS11 ECC key duration to ECC object
1 parent cd51786 commit 1707ace

3 files changed

Lines changed: 46 additions & 23 deletions

File tree

wolfcrypt/src/ecc.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7891,6 +7891,13 @@ int wc_ecc_free(ecc_key* key)
78917891
return 0;
78927892
}
78937893

7894+
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_FREE)
7895+
if (key->devId != INVALID_DEVID) {
7896+
wc_CryptoCb_Free(key->devId, WC_ALGO_TYPE_PK,
7897+
WC_PK_TYPE_EC_KEYGEN, key);
7898+
}
7899+
#endif
7900+
78947901
#if defined(WOLFSSL_ECDSA_SET_K) || defined(WOLFSSL_ECDSA_SET_K_ONE_LOOP) || \
78957902
defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || \
78967903
defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)

wolfcrypt/src/wc_pkcs11.c

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2923,8 +2923,12 @@ static int Pkcs11EcKeyGen(Pkcs11Session* session, wc_CryptoInfo* info)
29232923

29242924
if (pubKey != NULL_PTR)
29252925
session->func->C_DestroyObject(session->handle, pubKey);
2926-
if (ret != 0 && privKey != NULL_PTR)
2926+
if (ret == 0 && privKey != NULL_PTR) {
2927+
key->devCtx = (void*)(uintptr_t)privKey;
2928+
}
2929+
else if (ret != 0 && privKey != NULL_PTR) {
29272930
session->func->C_DestroyObject(session->handle, privKey);
2931+
}
29282932

29292933
return ret;
29302934
}
@@ -2994,7 +2998,6 @@ static int Pkcs11ECDH(Pkcs11Session* session, wc_CryptoInfo* info)
29942998
{
29952999
int ret = 0;
29963000
int sessionKey = 0;
2997-
int destroyPrivKey = 0;
29983001
unsigned char* point = NULL;
29993002
word32 pointLen;
30003003
CK_RV rv;
@@ -3021,7 +3024,11 @@ static int Pkcs11ECDH(Pkcs11Session* session, wc_CryptoInfo* info)
30213024
if (ret == 0) {
30223025
WOLFSSL_MSG("PKCS#11: EC Key Derivation Operation");
30233026

3024-
if ((sessionKey = !mp_iszero(
3027+
if (info->pk.ecdh.private_key->devCtx != NULL) {
3028+
privateKey = (CK_OBJECT_HANDLE)(uintptr_t)
3029+
info->pk.ecdh.private_key->devCtx;
3030+
}
3031+
else if ((sessionKey = !mp_iszero(
30253032
wc_ecc_key_get_priv(info->pk.ecdh.private_key))))
30263033
ret = Pkcs11CreateEccPrivateKey(&privateKey, session,
30273034
info->pk.ecdh.private_key, CKA_DERIVE);
@@ -3039,11 +3046,6 @@ static int Pkcs11ECDH(Pkcs11Session* session, wc_CryptoInfo* info)
30393046
else {
30403047
ret = Pkcs11FindEccKey(&privateKey, CKO_PRIVATE_KEY, session,
30413048
info->pk.ecdh.public_key, CKA_DERIVE);
3042-
if (ret == 0) {
3043-
/* Key found by public key match is likely ephemeral (e.g. from
3044-
* Pkcs11EcKeyGen for ECDHE), clean it up after use. */
3045-
destroyPrivKey = 1;
3046-
}
30473049
}
30483050
}
30493051
if (ret == 0) {
@@ -3094,20 +3096,8 @@ static int Pkcs11ECDH(Pkcs11Session* session, wc_CryptoInfo* info)
30943096
if (secret != CK_INVALID_HANDLE)
30953097
session->func->C_DestroyObject(session->handle, secret);
30963098

3097-
if (sessionKey) {
3099+
if (sessionKey && privateKey != NULL_PTR)
30983100
session->func->C_DestroyObject(session->handle, privateKey);
3099-
}
3100-
else if (destroyPrivKey && privateKey != NULL_PTR) {
3101-
/* Only destroy if the key is a non-persistent session object */
3102-
CK_BBOOL isToken = CK_FALSE;
3103-
CK_ATTRIBUTE tokenTmpl[] = {
3104-
{ CKA_TOKEN, &isToken, sizeof(isToken) },
3105-
};
3106-
if (session->func->C_GetAttributeValue(session->handle, privateKey,
3107-
tokenTmpl, 1) == CKR_OK && isToken == CK_FALSE) {
3108-
session->func->C_DestroyObject(session->handle, privateKey);
3109-
}
3110-
}
31113101

31123102
if (point != NULL)
31133103
XFREE(point, info->pk.ecdh.public_key->heap, DYNAMIC_TYPE_ECC_BUFFER);
@@ -3335,7 +3325,11 @@ static int Pkcs11ECDSA_Sign(Pkcs11Session* session, wc_CryptoInfo* info)
33353325
if (ret == 0) {
33363326
WOLFSSL_MSG("PKCS#11: EC Signing Operation");
33373327

3338-
if ((sessionKey = !mp_iszero(
3328+
if (info->pk.eccsign.key->devCtx != NULL) {
3329+
privateKey = (CK_OBJECT_HANDLE)(uintptr_t)
3330+
info->pk.eccsign.key->devCtx;
3331+
}
3332+
else if ((sessionKey = !mp_iszero(
33393333
wc_ecc_key_get_priv(info->pk.eccsign.key))))
33403334
ret = Pkcs11CreateEccPrivateKey(&privateKey, session,
33413335
info->pk.eccsign.key, CKA_SIGN);
@@ -3399,7 +3393,7 @@ static int Pkcs11ECDSA_Sign(Pkcs11Session* session, wc_CryptoInfo* info)
33993393
sz);
34003394
}
34013395

3402-
if (sessionKey)
3396+
if (sessionKey && privateKey != NULL_PTR)
34033397
session->func->C_DestroyObject(session->handle, privateKey);
34043398

34053399
return ret;
@@ -4750,6 +4744,25 @@ int wc_Pkcs11_CryptoDevCb(int devId, wc_CryptoInfo* info, void* ctx)
47504744
else
47514745
ret = NOT_COMPILED_IN;
47524746
}
4747+
#ifdef WOLF_CRYPTO_CB_FREE
4748+
else if (info->algo_type == WC_ALGO_TYPE_FREE) {
4749+
#ifdef HAVE_ECC
4750+
if (info->free.algo == WC_ALGO_TYPE_PK &&
4751+
info->free.type == WC_PK_TYPE_EC_KEYGEN) {
4752+
ecc_key* key = (ecc_key*)info->free.obj;
4753+
if (key != NULL && key->devCtx != NULL) {
4754+
if (token->handle != NULL_PTR) {
4755+
CK_OBJECT_HANDLE handle =
4756+
(CK_OBJECT_HANDLE)(uintptr_t)key->devCtx;
4757+
token->func->C_DestroyObject(token->handle, handle);
4758+
}
4759+
key->devCtx = NULL;
4760+
}
4761+
ret = 0;
4762+
}
4763+
#endif
4764+
}
4765+
#endif /* WOLF_CRYPTO_CB_FREE */
47534766

47544767
return ret;
47554768
}

wolfssl/wolfcrypt/settings.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4962,6 +4962,9 @@ extern void uITRON4_free(void *p) ;
49624962
" (WC_TEST_NO_CRYPTOCB_SW_TEST)" \
49634963
" requires WOLF_CRYPTO_CB"
49644964
#endif
4965+
#if defined(HAVE_PKCS11) && !defined(WOLF_CRYPTO_CB_FREE)
4966+
#define WOLF_CRYPTO_CB_FREE
4967+
#endif
49654968
#if (defined(WOLF_CRYPTO_CB_COPY) || defined(WOLF_CRYPTO_CB_FREE)) && \
49664969
!defined(WOLF_CRYPTO_CB)
49674970
#error "Crypto callback utilities" \

0 commit comments

Comments
 (0)