Skip to content

Commit 9741a47

Browse files
committed
Only get PKCS11_OBJECT references for private keys
1 parent 56f80c3 commit 9741a47

File tree

4 files changed

+21
-4
lines changed

4 files changed

+21
-4
lines changed

src/libp11-int.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,9 +355,15 @@ extern int pkcs11_private_decrypt(
355355
/* Retrieve PKCS11_KEY from an RSA key */
356356
extern PKCS11_OBJECT_private *pkcs11_get_ex_data_rsa(const RSA *rsa);
357357

358+
/* Set PKCS11_KEY for an RSA key */
359+
void pkcs11_set_ex_data_rsa(RSA *rsa, PKCS11_OBJECT_private *key);
360+
358361
/* Retrieve PKCS11_KEY from an EC_KEY */
359362
extern PKCS11_OBJECT_private *pkcs11_get_ex_data_ec(const EC_KEY *ec);
360363

364+
/* Set PKCS11_KEY for an EC_KEY */
365+
extern void pkcs11_set_ex_data_ec(EC_KEY *ec, PKCS11_OBJECT_private *key);
366+
361367
/* Free the global RSA_METHOD */
362368
extern void pkcs11_rsa_method_free(void);
363369

src/p11_ec.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ PKCS11_OBJECT_private *pkcs11_get_ex_data_ec(const EC_KEY *ec)
351351
#endif
352352
}
353353

354-
static void pkcs11_set_ex_data_ec(EC_KEY *ec, PKCS11_OBJECT_private *key)
354+
void pkcs11_set_ex_data_ec(EC_KEY *ec, PKCS11_OBJECT_private *key)
355355
{
356356
#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
357357
EC_KEY_set_ex_data(ec, ec_ex_index, key);

src/p11_key.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,10 @@ EVP_PKEY *pkcs11_get_key(PKCS11_OBJECT_private *key0, CK_OBJECT_CLASS object_cla
481481
EVP_PKEY_free(ret);
482482
goto err;
483483
}
484-
pkcs11_object_ref(key);
484+
if (key->object_class == CKO_PRIVATE_KEY)
485+
pkcs11_object_ref(key);
486+
else /* Public key -> detach PKCS11_OBJECT */
487+
pkcs11_set_ex_data_rsa(rsa, NULL);
485488
break;
486489
case EVP_PKEY_EC:
487490
#if OPENSSL_VERSION_NUMBER < 0x30000000L || defined(LIBRESSL_VERSION_NUMBER)
@@ -498,9 +501,17 @@ EVP_PKEY *pkcs11_get_key(PKCS11_OBJECT_private *key0, CK_OBJECT_CLASS object_cla
498501
EVP_PKEY_free(ret);
499502
goto err;
500503
}
501-
pkcs11_object_ref(key);
504+
if (key->object_class == CKO_PRIVATE_KEY)
505+
pkcs11_object_ref(key);
506+
else /* Public key -> detach PKCS11_OBJECT */
507+
pkcs11_set_ex_data_ec(ec_key, NULL);
502508
#else
503509
ret = EVP_PKEY_dup(key->evp_key);
510+
if (key->object_class != CKO_PRIVATE_KEY) {
511+
/* Public key -> detach and free PKCS11_OBJECT */
512+
pkcs11_set_ex_data_ec((EC_KEY *)EVP_PKEY_get0_EC_KEY(ret), NULL);
513+
pkcs11_object_free(key);
514+
}
504515
#endif
505516
break;
506517
default:

src/p11_rsa.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ PKCS11_OBJECT_private *pkcs11_get_ex_data_rsa(const RSA *rsa)
280280
return RSA_get_ex_data(rsa, rsa_ex_index);
281281
}
282282

283-
static void pkcs11_set_ex_data_rsa(RSA *rsa, PKCS11_OBJECT_private *key)
283+
void pkcs11_set_ex_data_rsa(RSA *rsa, PKCS11_OBJECT_private *key)
284284
{
285285
RSA_set_ex_data(rsa, rsa_ex_index, key);
286286
}

0 commit comments

Comments
 (0)