Replace global RSA methods with per-context callbacks - #675
Merged
Conversation
mtrojnar
force-pushed
the
fix-rsa-method-scope-672
branch
3 times, most recently
from
August 12, 2026 19:01
f520426 to
9ab98f8
Compare
mtrojnar
force-pushed
the
fix-rsa-method-scope-672
branch
2 times, most recently
from
August 13, 2026 07:39
71408f4 to
76c05a2
Compare
Open
3 tasks
mtrojnar
force-pushed
the
fix-rsa-method-scope-672
branch
from
August 17, 2026 18:26
23b5e86 to
ed5c1fa
Compare
mtrojnar
force-pushed
the
fix-rsa-method-scope-672
branch
3 times, most recently
from
August 18, 2026 13:08
ba0efd5 to
83855fb
Compare
Stop registering libp11's RSA EVP_PKEY_METHOD process-wide when a private key is created. Global registration changes method selection for unrelated software and provider-backed RSA keys. Add a per-context callback for returned private EVP_PKEY objects and preserve it when UTIL_CTX recreates its libp11 context. Have the ENGINE use this hook to attach its method dispatch only to keys loaded by that ENGINE, keeping ENGINE integration out of the public libp11 implementation.
mtrojnar
force-pushed
the
fix-rsa-method-scope-672
branch
from
August 19, 2026 08:11
83855fb to
08112e9
Compare
Collaborator
|
Looks good to me. |
Verify that a key returned directly by PKCS11_get_private_key() signs with PKCS#1 v1.5 through CKM_RSA_PKCS when CKM_RSA_X_509 is disabled. This ensures that removing process-wide PKEY registration retains basic RSA_METHOD dispatch. Verify that loading the PKCS#11 key leaves existing and subsequently generated provider-backed software RSA keys unchanged. Exercise ENGINE signing without passing an ENGINE to EVP_PKEY_CTX_new(), so success depends on the key-loading callback. Run it against both raw-RSA-only and native-PSS-only SoftHSM configurations with explicit digest, MGF1, and salt-length parameters.
mtrojnar
force-pushed
the
fix-rsa-method-scope-672
branch
from
August 19, 2026 10:53
08112e9 to
427ac2a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pull Request Type
Related Issue
Fixes #672
Current Behavior
On OpenSSL 3.x,
PKCS11_get_private_key()registers libp11's custom RSAEVP_PKEY_METHODprocess-wide withEVP_PKEY_meth_add0().That registration changes method selection for unrelated software RSA keys.
Provider-backed keys created before the PKCS#11 key can subsequently fail in
operations such as
X509_sign()with:Software RSA keys created afterward can also silently fall back to legacy
implementations instead of remaining backed by the default provider.
The registration also crosses an architecture boundary: the libp11 library
initializes deprecated OpenSSL method integration that belongs to the ENGINE
component.
New Behavior
PKCS11_get_private_key()no longer registers an RSAEVP_PKEY_METHODglobally. Loading a PKCS#11 key therefore does not alter method selection for
unrelated keys.
A new per-context callback API lets integrations customize each returned key:
The callback receives borrowed
PKCS11_KEYandEVP_PKEYobjects and maycustomize the returned key in place. The callback must not free either
object; an error aborts key retrieval and libp11 releases the
EVP_PKEY.The ENGINE registers this callback and attaches its method dispatch only to
private keys loaded by that ENGINE. This preserves native RSA-PSS and
software-PSS-over-raw-RSA behavior without changing process-wide method
selection.
Provider key loading and dispatch remain unchanged; the provider does not
need the callback or an additional keydata handoff.
Scope of Changes
PKCS11_CTX_set_pkey_callback()and thePKCS11_PKEY_CALLBACK_GET_PRIVATE_KEYevent.UTIL_CTXcreates or reloads itsinternal libp11 context.
EVP_PKEY_METHODregistration and its global statefrom the libp11 library.
eng_*code.callback-selected ENGINE dispatch, and ENGINE signing on both raw-RSA-only
and native-PSS-only tokens.
This adds a public libp11 API symbol and callback type. No existing public
symbol or function signature is removed.
Testing
Built with OpenSSL 3.6.3 using strict configuration and ran:
Result: 51 total, 46 passed, 5 skipped due to unavailable ML-DSA/ML-KEM
features, 0 failed.
The software-key regression test verifies both an existing and a newly
created default-provider RSA key after
PKCS11_get_private_key()loads atoken key.
The ENGINE scenario does not pass an ENGINE to
EVP_PKEY_CTX_new(), sosuccess depends on the key-loading callback. It runs against raw-RSA-only and
native-PSS-only SoftHSM configurations with explicit SHA-256, MGF1, and
salt-length parameters.
Additional Notes
ENGINE-specific APIs remain confined to
eng_*sources. No provider-specificcallback or keydata handoff is introduced, preserving the provider-only
OpenSSL 4 architecture.
License Declaration