Skip to content

Reject CKA_VALUE in templates of keys an operation computes - #320

Merged
Frauschi merged 3 commits into
wolfSSL:masterfrom
LinuxJedi:reject-template-cka-value
Dec 1, 2026
Merged

Frauschi merged 3 commits into
wolfSSL:masterfrom
LinuxJedi:reject-template-cka-value

Conversation

@LinuxJedi

@LinuxJedi LinuxJedi commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

Summary

C_GenerateKey, C_UnwrapKey, C_EncapsulateKey and C_DecapsulateKey accepted a template with CKA_VALUE, then replaced that value with the generated, unwrapped or shared secret. PKCS#11 does not allow the value to be given when a key is made this way. These calls now return CKR_TEMPLATE_INCONSISTENT, which is what C_DeriveKey already returns.

  • The derive check moved into CheckSecretKeyTemplate, which derive, encapsulate and decapsulate all call. Derive behaves as before.
  • C_GenerateKey checks for CKA_VALUE in its existing loop over the template, so a duplicate later in the template is caught as well.
  • C_UnwrapKey checks after the class and key type, before the wrapped key is decrypted.
  • C_GenerateKey, C_EncapsulateKey and C_DecapsulateKey walk their template with an int index. They now return CKR_ARGUMENTS_BAD for an attribute count above INT_MAX, as C_UnwrapKey and C_DeriveKey already do. Before, such a count was truncated and later attributes went unchecked.
  • test_wrap_unwrap_key (in pkcs11test and pkcs11mtt) had an empty {CKA_VALUE, NULL, 0} entry in its unwrap template. That entry is now refused before the mechanism check the test is for, so it is removed.

C_GenerateKeyPair is not changed. Its HSS, XMSS, ML-DSA and ML-KEM generators already refuse CKA_VALUE. Its EC, RSA and DH generators still accept a CKA_VALUE and overwrite it; that is left for a follow-up.

Testing

  • New test_mlkem_secret_template_value: encapsulation (size query and full call) and decapsulation are refused with a template value, both when it matches the real secret and when it differs. Without the value, the same calls succeed.
  • test_generate_key and test_aes_wrap_unwrap_key have a new case that passes CKA_VALUE.
  • Encapsulation, decapsulation and C_GenerateKey are refused with an attribute count of INT_MAX + 1.
  • make check passes with wolfSSL 5.9.2 in three configs: --enable-pkcs11v32 --enable-mlkem --enable-mldsa, the default ./configure, and --enable-nss --enable-pkcs11v32 --enable-mlkem.
  • All three new cases fail when built against master's src/crypto.c.

C_GenerateKey, C_UnwrapKey, C_EncapsulateKey and C_DecapsulateKey
accepted a template with CKA_VALUE, then replaced that value with the
generated, unwrapped or shared secret. PKCS#11 does not allow the value
to be given when a key is made this way, and C_DeriveKey already
refused it with CKR_TEMPLATE_INCONSISTENT.

Move the derive check into CheckSecretKeyTemplate, so it also covers
encapsulation and decapsulation. Add it to the C_GenerateKey template
loop, and to C_UnwrapKey before the wrapped key is decrypted.

test_wrap_unwrap_key had an empty CKA_VALUE in its unwrap template.
That is now refused before the mechanism check the test is for, so
remove it.
Copilot AI balanced review requested due to automatic review settings November 30, 2026 08:27

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.

🟡 Changes recommended

The ML-KEM entry points lack attribute-count bounds, allowing oversized counts to truncate during template lookup and bypass validation or compute key material before rejection.

1 open issue
What changed in this PR

This PR enforces PKCS#11 template rules by rejecting caller-supplied CKA_VALUE when an operation computes the key material.

Changes:

  • Adds value rejection to generation and unwrap, and shares derive’s existing check with encapsulation and decapsulation.
  • Adds regression tests for rejected templates and successful calls without CKA_VALUE.
  • Removes empty value attributes from unwrap tests so they still reach the intended mechanism checks.
File Description
tests/​pkcs11v3test.c Adds ML-KEM template-value rejection and success cases.
tests/​pkcs11test.c Tests generation and unwrap rejection; updates an unwrap template.
tests/​pkcs11mtt.c Removes the empty value attribute from an unwrap template.
src/​crypto.c Adds and centralizes computed-key template validation.

Review effort: Balanced

Comment thread src/crypto.c Resolved
C_GenerateKey, C_EncapsulateKey and C_DecapsulateKey walk their
template with an int index. On LP64 a count above INT_MAX was
truncated, so later attributes, a CKA_VALUE among them, were not
checked. Return CKR_ARGUMENTS_BAD for such a count, as C_UnwrapKey and
C_DeriveKey already do.

@wolfSSL-Fenrir-bot wolfSSL-Fenrir-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Fenrir Automated Review — PR #320

Scan targets checked: wolfpkcs11-src, wolfpkcs11-bugs
Coverage: 4 of 4 in-scope changed file(s) opened by the reviewer

Fenrir result: Approved ✅

No new issues found in the changed files.

Advisory only — this automated result does not count as a GitHub approval.

Review tier: Max

@Frauschi Frauschi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

2 comments inline, nothing blocking.

Comment thread src/crypto.c Resolved
Comment thread src/crypto.c Resolved
List the new CKR_TEMPLATE_INCONSISTENT case for a template CKA_VALUE in
the C_GenerateKey, C_UnwrapKey and C_DeriveKey docs, and the
ulCount/ulAttributeCount INT_MAX limit in the CKR_ARGUMENTS_BAD lines of
C_GenerateKey and C_UnwrapKey.
@Frauschi
Frauschi merged commit 752107e into wolfSSL:master Dec 1, 2026
125 checks passed
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.

5 participants