Repository navigation
Conversation
C_GenerateKeyPair accepted a template that gave values the RSA, EC and DH generators compute, then replaced them with the generated key. PKCS#11 does not allow these on key generation, and a private part given on the public template, such as an RSA private exponent or an EC private value, was left in the public key object. The HSS, XMSS, ML-DSA and ML-KEM generators already refused CKA_VALUE with CKR_TEMPLATE_INCONSISTENT. Return CKR_TEMPLATE_INCONSISTENT, before any key object is built, for: - CKA_VALUE on either template of any key pair mechanism. - CKA_EC_POINT on an EC public key template. - CKA_MODULUS, CKA_PRIVATE_EXPONENT and the CRT values on either RSA template. CKA_EC_POINT stays allowed on the EC private template: PKCS#11 gives that key none, and NSS sends an empty one on some private keys. The domain parameters and public exponent that a private template may repeat from the public one are left alone. Move the C_GenerateKeyPair doxygen block, which sat above CheckGenPairAttrs, onto wp11_C_GenerateKeyPair and list the new code.
C_GenerateKeyPair walks its templates with an int index. On LP64 a count above INT_MAX was truncated to a negative int, so the templates were not checked, a CKA_VALUE or a mismatched CKA_CLASS among them. Return CKR_ARGUMENTS_BAD for such a count, as C_GenerateKey, C_UnwrapKey and the KEM calls already do.
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are reported for the key-pair template validation changes.
No open issues 🎉
What changed in this PR
Extends #320’s template validation to C_GenerateKeyPair, rejecting caller-supplied values that key generation computes.
Changes:
- Rejects computed attributes before creating key objects.
- Rejects template counts above
INT_MAX. - Adds regression tests and relocates and updates API documentation.
| File | Description |
|---|---|
| tests/pkcs11test.c | Adds oversized-count and RSA, EC, and DH computed-attribute tests. |
| src/crypto.c | Validates computed attributes and template counts; updates function documentation. |
Review effort: Balanced
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.
Follow-up to #320, from its review: EC, RSA and DH key pair generation still accepted a template value that the generator computes, then overwrote it with the generated key.
Changes
C_GenerateKeyPairreturnsCKR_TEMPLATE_INCONSISTENT, before any key object is built, when a template gives:CKA_VALUEon either template, for any key pair mechanism. HSS, XMSS, ML-DSA and ML-KEM already refused it.CKA_EC_POINTon an EC public key template.CKA_MODULUS,CKA_PRIVATE_EXPONENT,CKA_PRIME_1,CKA_PRIME_2,CKA_EXPONENT_1,CKA_EXPONENT_2orCKA_COEFFICIENTon either RSA template.The check is in
CheckGenPairAttrs, which already walks every attribute of both templates for the class and key type checks.C_GenerateKeyPairreturnsCKR_ARGUMENTS_BADwhen either attribute count is aboveINT_MAX, asC_GenerateKey,C_UnwrapKeyand the KEM calls do. The templates are walked with anintindex, so a larger count went negative and the templates were never checked.The
C_GenerateKeyPairdoxygen block sat aboveCheckGenPairAttrs. It is now onwp11_C_GenerateKeyPairand lists both codes.Behaviour before this change
Each attribute was added on its own to an otherwise valid template:
CKA_MODULUS,CKA_PRIVATE_EXPONENTCKR_OK: modulus overwritten, private exponent kept in the public key objectCKA_MODULUS,CKA_PRIVATE_EXPONENT, CRT valuesCKR_OK, overwrittenCKA_VALUECKR_ATTRIBUTE_VALUE_INVALIDCKA_EC_POINTCKR_OK, overwrittenCKA_VALUECKR_OK, private value set on the public key objectCKA_VALUECKR_TEMPLATE_INCOMPLETECKA_VALUECKR_OK, overwrittenINT_MAX + 1Left as is
CKA_EC_POINTon an EC private key template. PKCS#11 gives that key noCKA_EC_POINT, and NSS sends an empty one on some private keys.CKA_PUBLIC_EXPONENT, ECCKA_EC_PARAMS, and DHCKA_PRIMEandCKA_BASE. These are inputs that generation copies from the public template, and some applications repeat them on both templates. PKCS#11 does not allow them on key generation either, so they could be refused later.CKA_VALUEchecks in the HSS, XMSS, ML-DSA and ML-KEM paths now duplicate the common one.Testing
test_generate_key_pairtries anINT_MAX + 1public count and private count.test_rsa_gen_keys_computed,test_ecc_gen_keys_computedandtest_dh_gen_keys_computedadd each computed attribute to the end of a valid template. They expectCKR_TEMPLATE_INCONSISTENTand no key handles, then check that the templates without it make a key pair. They all fail without the fix.make checkpasses with default options, with--enable-nss, and with--enable-pkcs11v32 --enable-mlkem --enable-mldsa(wolfSSL 5.9.2,-Werror).CKR_TEMPLATE_INCONSISTENTforCKA_VALUE, which is still the result.