Skip to content

Reject template values that C_GenerateKeyPair computes - #324

Open
LinuxJedi wants to merge 2 commits into
wolfSSL:masterfrom
LinuxJedi:reject-keypair-template-value
Open

LinuxJedi wants to merge 2 commits into
wolfSSL:masterfrom
LinuxJedi:reject-keypair-template-value

Conversation

@LinuxJedi

Copy link
Copy Markdown
Member

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_GenerateKeyPair returns CKR_TEMPLATE_INCONSISTENT, before any key object is built, when a template gives:

    • CKA_VALUE on either template, for any key pair mechanism. HSS, XMSS, ML-DSA and ML-KEM already refused it.
    • CKA_EC_POINT on an EC public key template.
    • CKA_MODULUS, CKA_PRIVATE_EXPONENT, CKA_PRIME_1, CKA_PRIME_2, CKA_EXPONENT_1, CKA_EXPONENT_2 or CKA_COEFFICIENT on either RSA template.

    The check is in CheckGenPairAttrs, which already walks every attribute of both templates for the class and key type checks.

  • C_GenerateKeyPair returns CKR_ARGUMENTS_BAD when either attribute count is above INT_MAX, as C_GenerateKey, C_UnwrapKey and the KEM calls do. The templates are walked with an int index, so a larger count went negative and the templates were never checked.

  • The C_GenerateKeyPair doxygen block sat above CheckGenPairAttrs. It is now on wp11_C_GenerateKeyPair and lists both codes.

Behaviour before this change

Each attribute was added on its own to an otherwise valid template:

Key Template Attribute Before
RSA public CKA_MODULUS, CKA_PRIVATE_EXPONENT CKR_OK: modulus overwritten, private exponent kept in the public key object
RSA private CKA_MODULUS, CKA_PRIVATE_EXPONENT, CRT values CKR_OK, overwritten
RSA either CKA_VALUE CKR_ATTRIBUTE_VALUE_INVALID
EC public CKA_EC_POINT CKR_OK, overwritten
EC public CKA_VALUE CKR_OK, private value set on the public key object
EC private CKA_VALUE CKR_TEMPLATE_INCOMPLETE
DH either CKA_VALUE CKR_OK, overwritten
any - count of INT_MAX + 1 template not read

Left as is

  • CKA_EC_POINT on an EC private key template. PKCS#11 gives that key no CKA_EC_POINT, and NSS sends an empty one on some private keys.
  • A private template's RSA CKA_PUBLIC_EXPONENT, EC CKA_EC_PARAMS, and DH CKA_PRIME and CKA_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.
  • The CKA_VALUE checks in the HSS, XMSS, ML-DSA and ML-KEM paths now duplicate the common one.

Testing

  • test_generate_key_pair tries an INT_MAX + 1 public count and private count.
  • New test_rsa_gen_keys_computed, test_ecc_gen_keys_computed and test_dh_gen_keys_computed add each computed attribute to the end of a valid template. They expect CKR_TEMPLATE_INCONSISTENT and no key handles, then check that the templates without it make a key pair. They all fail without the fix.
  • make check passes with default options, with --enable-nss, and with --enable-pkcs11v32 --enable-mlkem --enable-mldsa (wolfSSL 5.9.2, -Werror).
  • The HSS and XMSS tests were not run locally. They expect CKR_TEMPLATE_INCONSISTENT for CKA_VALUE, which is still the result.

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.
Copilot AI balanced review requested due to automatic review settings December 3, 2026 15:41

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants