Skip to content

C_UnwrapKey: set CKA_VALUE_LEN on the unwrapped secret key - #925

Open
Princemachiavelli wants to merge 1 commit into
softhsm:mainfrom
Princemachiavelli:jhoffer/fix_unwrap_CKA_VALUE_LEN
Open

Princemachiavelli wants to merge 1 commit into
softhsm:mainfrom
Princemachiavelli:jhoffer/fix_unwrap_CKA_VALUE_LEN

Conversation

@Princemachiavelli

@Princemachiavelli Princemachiavelli commented Nov 14, 2026 •

Copy link
Copy Markdown

C_UnwrapKey stored the recovered CKA_VALUE of a secret key but left CKA_VALUE_LEN at its default of 0, so an unwrapped 256-bit AES key reported CKA_VALUE_LEN 0. A template cannot supply it: the attribute is refused with CKR_ATTRIBUTE_READ_ONLY, as PKCS#11 (v2.40 4.1.2, footnote 6) says it must not be specified when unwrapping, and the mechanism contributes it with CKA_VALUE.

Set CKA_VALUE_LEN to the length of the recovered value for every secret key type that has the attribute. The tests check it after an AES unwrap of a generic secret key and an RSA unwrap of an AES key, and that a Triple DES key, which has no CKA_VALUE_LEN, still unwraps to its value without one.

Fixes #628

Summary by CodeRabbit

  • Bug Fixes
    • Unwrapped secret keys now report their value length accurately when that attribute is supported. This improves consistency when checking the length of recovered keys after a wrap-and-unwrap operation. Key types that do not support the value-length attribute remain unchanged.

@Princemachiavelli
Princemachiavelli requested a review from a team as a code owner November 14, 2026 05:51
@coderabbitai

coderabbitai Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

C_UnwrapKey now sets CKA_VALUE_LEN to the unwrapped value’s byte length when the attribute is present. Tests check the reported length for AES and generic keys, and confirm that DES3 keys do not have this attribute.

Changes: Unwrapped key length

Layer / File(s) Summary
Set unwrapped key length
src/lib/SoftHSM.cpp
C_UnwrapKey sets CKA_VALUE_LEN to the unwrapped value’s byte size when the object has that attribute.
Verify unwrapped key length
src/lib/test/AsymWrapUnwrapTests.cpp, src/lib/test/SymmetricAlgorithmTests.cpp, src/lib/test/SymmetricAlgorithmTests.h
AES and generic-key tests check CKA_VALUE_LEN. A non-FIPS DES3 test checks the recovered value and confirms that CKA_VALUE_LEN is absent.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Low

Suggested reviewers: bukka, jschlyter

Merge Risk: Minimal · up to a2082

This change makes unwrapped secret keys report the correct CKA_VALUE_LEN, as requested in issue #628. The change is small and its tests are reasonable. No merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: C_UnwrapKey now sets CKA_VALUE_LEN on the unwrapped secret key. The raw summary confirms this change in src/lib/SoftHSM.cpp, with matching test updates. The objectives…
Linked Issues check Passed Issue #628 [direct] requires that an unwrapped AES key report CKA_VALUE_LEN that matches its recovered value. The whole-PR diff of src/lib/SoftHSM.cpp (merge base to head) adds, in C_UnwrapKey after s…
Out of Scope Changes check Passed All changes relate to #628. The SoftHSM.cpp change sets CKA_VALUE_LEN during C_UnwrapKey, as the issue requires. The SymmetricAlgorithmTests.cpp change adds the CKA_VALUE_LEN check to the AES wrap and…
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Full details: Title check

Explanation

The title names the main change: C_UnwrapKey now sets CKA_VALUE_LEN on the unwrapped secret key. The raw summary confirms this change in src/lib/SoftHSM.cpp, with matching test updates. The objectives describe the same intent, which addresses issue #628.

Full details: Linked Issues check

Explanation

Issue #628 [direct] requires that an unwrapped AES key report CKA_VALUE_LEN that matches its recovered value. The whole-PR diff of src/lib/SoftHSM.cpp (merge base to head) adds, in C_UnwrapKey after setAttribute(CKA_VALUE, value), a call that sets CKA_VALUE_LEN to keydata.size() when osobject->attributeExists(CKA_VALUE_LEN) is true. Key types without the attribute (DES, DES3) are skipped. The test changes in src/lib/test/SymmetricAlgorithmTests.cpp (aesWrapUnwrapGeneric) and src/lib/test/AsymWrapUnwrapTests.cpp (rsaWrapUnwrap) read CKA_VALUE_LEN after unwrap and compare it with the original key length. Residual uncertainty: the hunk does not show the code that sets value, so equality of value.size() and keydata.size() for every secret type is inferred. For AES, the diff context (value = keydata in the else branch) supports equality. Non-AES secret types were not fully traced.

Full details: Out of Scope Changes check

Explanation

All changes relate to #628. The SoftHSM.cpp change sets CKA_VALUE_LEN during C_UnwrapKey, as the issue requires. The SymmetricAlgorithmTests.cpp change adds the CKA_VALUE_LEN check to the AES wrap and unwrap test and a C_DestroyObject cleanup for the test-created object. The new aesWrapUnwrapDes3 test checks that CKA_VALUE_LEN is absent for DES3, which matches the code skip for key types without the attribute. The AsymWrapUnwrapTests.cpp change adds the same length check on the RSA-wrapped AES path named in the issue. The SymmetricAlgorithmTests.h change only declares the new test under the non-FIPS guard. No unrelated functional change appears in the summaries or the SoftHSM.cpp diff.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: f1eafaea-b2d9-4bd3-b446-2c3c58f9620d
Commits

Reviewing files that changed from the base of the PR and between d7b5f8e and a208256.

Files selected for processing: 3

  • src/lib/test/AsymWrapUnwrapTests.cpp
  • src/lib/test/SymmetricAlgorithmTests.cpp
  • src/lib/test/SymmetricAlgorithmTests.h

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

@Princemachiavelli

Copy link
Copy Markdown
Author

Compare to older fix #650 . I think we should only compute the CKA_VALUE_LEN for keys that have the attribute.

C_UnwrapKey stored the recovered CKA_VALUE of a secret key but left
CKA_VALUE_LEN at its default of 0, so an unwrapped 256-bit AES key
reported CKA_VALUE_LEN 0. A template cannot supply it: the attribute
is refused with CKR_ATTRIBUTE_READ_ONLY, as PKCS#11 (v2.40 4.1.2,
footnote 6) says it must not be specified when unwrapping, and the
mechanism contributes it with CKA_VALUE.

Set CKA_VALUE_LEN to the length of the recovered value for every
secret key type that has the attribute. The tests check it after an
AES unwrap of a generic secret key and an RSA unwrap of an AES key,
and that a Triple DES key, which has no CKA_VALUE_LEN, still unwraps
to its value without one.

Fixes softhsm#628

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Josh Hoffer <jhoffer@sansorgan.es>
@Princemachiavelli
Princemachiavelli force-pushed the jhoffer/fix_unwrap_CKA_VALUE_LEN branch from d7b5f8e to a208256 Compare November 15, 2026 22:16
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.

Unwrapped AES keys have VALUE_LEN set to 0

1 participant