Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
NEWS for Libp11 -- History of user visible changes

New in 0.4.22; unreleased
* Fixed a crash when signing with a retained RSA key after refreshing slots
* Fixed a leak when freeing token-backed EC keys with OpenSSL 1.0.2

New in 0.4.21; 2026-09-07; Michał Trojnara
* Added PKCS#11 provider support for `openssl genpkey` (Małgorzata Olszówka)
Expand Down
12 changes: 6 additions & 6 deletions src/libp11-int.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ struct pkcs11_object_private {
size_t id_len;
char *label;
PKCS11_OBJECT_ops *ops;
/* Both fields are protected by the key cache lock. */
EVP_PKEY *evp_key;
int in_key_cache;
X509 *x509;
unsigned int forkid;
int refcnt;
Expand Down Expand Up @@ -226,6 +228,10 @@ extern int check_fork(PKCS11_CTX_private *ctx);
extern int check_slot_fork(PKCS11_SLOT_private *slot);
extern int check_object_fork(PKCS11_OBJECT_private *key);

/* EVP_PKEY cache synchronization (also used by the fork handlers) */
extern void pkcs11_key_cache_lock(void);
extern void pkcs11_key_cache_unlock(void);

/* Other internal functions */
extern void *C_LoadModule(const char *name, CK_FUNCTION_LIST_PTR_PTR,
CK_FUNCTION_LIST_3_2_PTR_PTR);
Expand Down Expand Up @@ -577,15 +583,9 @@ extern int pkcs11_private_decrypt(
/* Retrieve PKCS11_OBJECT_private from an RSA key */
extern PKCS11_OBJECT_private *pkcs11_get_ex_data_rsa(const RSA *rsa);

/* Set PKCS11_OBJECT_private for an RSA key */
void pkcs11_set_ex_data_rsa(RSA *rsa, PKCS11_OBJECT_private *key);

#ifndef OPENSSL_NO_EC
/* Retrieve PKCS11_OBJECT_private from an EC_KEY */
extern PKCS11_OBJECT_private *pkcs11_get_ex_data_ec(const EC_KEY *ec);

/* Set PKCS11_OBJECT_private for an EC_KEY */
extern void pkcs11_set_ex_data_ec(EC_KEY *ec, PKCS11_OBJECT_private *key);
#endif /* OPENSSL_NO_EC */

#if OPENSSL_VERSION_NUMBER >= 0x30000000L
Expand Down
30 changes: 29 additions & 1 deletion src/p11_atfork.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,30 @@

#include "libp11-int.h"

#ifdef HAVE_PTHREAD
static pthread_mutex_t key_cache_lock = PTHREAD_MUTEX_INITIALIZER;
#else
static SRWLOCK key_cache_lock = SRWLOCK_INIT;
#endif

void pkcs11_key_cache_lock(void)
{
#ifdef HAVE_PTHREAD
pthread_mutex_lock(&key_cache_lock);
#else
AcquireSRWLockExclusive(&key_cache_lock);
#endif
}

void pkcs11_key_cache_unlock(void)
{
#ifdef HAVE_PTHREAD
pthread_mutex_unlock(&key_cache_lock);
#else
ReleaseSRWLockExclusive(&key_cache_lock);
#endif
}

#ifndef _WIN32

static unsigned int P11_forkid = 0;
Expand All @@ -33,12 +57,16 @@ static unsigned int P11_forkid = 0;
static void _P11_atfork_child(void)
{
P11_forkid++;
pkcs11_key_cache_unlock();
}

__attribute__((constructor))
int _P11_register_fork_handler(void)
{
if (pthread_atfork(0, 0, _P11_atfork_child) != 0)
/* Drain cache operations before forking so the caller owns the lock
* and can release it in both processes. */
if (pthread_atfork(pkcs11_key_cache_lock, pkcs11_key_cache_unlock,
_P11_atfork_child) != 0)
return -1;
return 0;
}
Expand Down
54 changes: 40 additions & 14 deletions src/p11_ec.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ static void (*ossl_ec_finish)(EC_KEY *);
static int (*ossl_ec_copy)(EC_KEY *, const EC_KEY *);
#endif /* OPENSSL_VERSION_NUMBER */

static int ec_ex_index = 0;

/********** Missing ECDSA_METHOD functions for OpenSSL < 1.1.0 */

typedef ECDSA_SIG *(*sign_sig_fn)(const unsigned char *, int,
Expand Down Expand Up @@ -174,6 +172,27 @@ void ECDH_METHOD_set_compute_key(ECDH_METHOD *m, compute_key_fn f)

/********** Manage EC ex_data */

#if OPENSSL_VERSION_NUMBER < 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
#define P11_LEGACY_EC_KEY_DATA

/* OpenSSL 1.0.2 has no EC_KEY_METHOD finish hook. Keep the lifetime
* callbacks on the key, not in a process-wide ex_data index. */
static void *pkcs11_ec_dup_object(void *ptr)
{
return pkcs11_object_ref(ptr);
}

static void pkcs11_ec_free_object(void *ptr)
{
pkcs11_object_free(ptr);
}

/* Per-key callbacks do not need an ex_data index. */
static void alloc_ec_ex_index(void) {}
static void free_ec_ex_index(void) {}
#else
static int ec_ex_index = 0;

/* NOTE: ECDH also uses ECDSA ex_data and *not* ECDH ex_data */
static void alloc_ec_ex_index(void)
{
Expand Down Expand Up @@ -201,6 +220,7 @@ static void free_ec_ex_index(void)
ec_ex_index = 0;
}
}
#endif

/********** EVP_PKEY retrieval */

Expand Down Expand Up @@ -354,19 +374,27 @@ static EC_KEY *pkcs11_get_ec(PKCS11_OBJECT_private *key)

PKCS11_OBJECT_private *pkcs11_get_ex_data_ec(const EC_KEY *ec)
{
#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
return EC_KEY_get_ex_data(ec, ec_ex_index);
#else
#if defined(P11_LEGACY_EC_KEY_DATA)
return EC_KEY_get_key_method_data((EC_KEY *)ec, pkcs11_ec_dup_object,
pkcs11_ec_free_object, pkcs11_ec_free_object);
#elif defined(LIBRESSL_VERSION_NUMBER)
return ECDSA_get_ex_data((EC_KEY *)ec, ec_ex_index);
#else
return EC_KEY_get_ex_data(ec, ec_ex_index);
#endif
}

void pkcs11_set_ex_data_ec(EC_KEY *ec, PKCS11_OBJECT_private *key)
static int pkcs11_set_ex_data_ec(EC_KEY *ec, PKCS11_OBJECT_private *key)
{
#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
EC_KEY_set_ex_data(ec, ec_ex_index, key);
#if defined(P11_LEGACY_EC_KEY_DATA)
/* Insertion returns NULL both on success and on allocation failure. */
EC_KEY_insert_key_method_data(ec, key, pkcs11_ec_dup_object,
pkcs11_ec_free_object, pkcs11_ec_free_object);
return pkcs11_get_ex_data_ec(ec) == key;
#elif defined(LIBRESSL_VERSION_NUMBER)
return ECDSA_set_ex_data(ec, ec_ex_index, key);
#else
ECDSA_set_ex_data(ec, ec_ex_index, key);
return EC_KEY_set_ex_data(ec, ec_ex_index, key);
#endif
}

Expand Down Expand Up @@ -399,11 +427,9 @@ static EVP_PKEY *pkcs11_get_evp_key_ec(PKCS11_OBJECT_private *key)
ECDSA_set_method(ec, PKCS11_get_ecdsa_method());
ECDH_set_method(ec, PKCS11_get_ecdh_method());
#endif
/* Keep the PKCS11_OBJECT_private alive while referenced from EC ex_data.
* With EC_KEY_METHOD, pkcs11_ec_finish() releases this reference;
* legacy ECDSA/ECDH methods have no equivalent finish hook. */
key = pkcs11_object_ref(key);
pkcs11_set_ex_data_ec(ec, key);
if (!pkcs11_set_ex_data_ec(ec, key))
goto error;
pkcs11_object_ref(key);
}
/* TODO: Retrieve the ECDSA private key object attributes instead,
* unless the key has the "sensitive" attribute set */
Expand Down
Loading
Loading