Repository navigation
Reject CKA_VALUE in templates of keys an operation computes - #320
Conversation
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.
There was a problem hiding this comment.
🟡 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
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
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
2 comments inline, nothing blocking.
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.

Summary
C_GenerateKey,C_UnwrapKey,C_EncapsulateKeyandC_DecapsulateKeyaccepted a template withCKA_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 returnCKR_TEMPLATE_INCONSISTENT, which is whatC_DeriveKeyalready returns.CheckSecretKeyTemplate, which derive, encapsulate and decapsulate all call. Derive behaves as before.C_GenerateKeychecks forCKA_VALUEin its existing loop over the template, so a duplicate later in the template is caught as well.C_UnwrapKeychecks after the class and key type, before the wrapped key is decrypted.C_GenerateKey,C_EncapsulateKeyandC_DecapsulateKeywalk their template with anintindex. They now returnCKR_ARGUMENTS_BADfor an attribute count aboveINT_MAX, asC_UnwrapKeyandC_DeriveKeyalready 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_GenerateKeyPairis not changed. Its HSS, XMSS, ML-DSA and ML-KEM generators already refuseCKA_VALUE. Its EC, RSA and DH generators still accept aCKA_VALUEand overwrite it; that is left for a follow-up.Testing
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_keyandtest_aes_wrap_unwrap_keyhave a new case that passesCKA_VALUE.C_GenerateKeyare refused with an attribute count ofINT_MAX + 1.make checkpasses with wolfSSL 5.9.2 in three configs:--enable-pkcs11v32 --enable-mlkem --enable-mldsa, the default./configure, and--enable-nss --enable-pkcs11v32 --enable-mlkem.src/crypto.c.