Skip to content

Fix #937: check the second key of CKM_CONCATENATE_BASE_AND_KEY - #942

Open
AlexLaroche wants to merge 1 commit into
softhsm:mainfrom
AlexLaroche:fix/concatenate-second-key-checks
Open

AlexLaroche wants to merge 1 commit into
softhsm:mainfrom
AlexLaroche:fix/concatenate-second-key-checks

Conversation

@AlexLaroche

@AlexLaroche AlexLaroche commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #937.

Problem

For CKM_CONCATENATE_BASE_AND_KEY, C_DeriveKey checks the base key (read access, CKA_DERIVE, CKA_ALLOWED_MECHANISMS), but deriveSymmetric reads the CKA_VALUE of the second key, named in the mechanism parameter, with no checks at all. A sensitive, unextractable key with CKA_DERIVE = FALSE, or a private key (EC, EdDSA, ML-DSA, ML-KEM, SLH-DSA), could therefore be concatenated into a new key and, combined with CKA_VALUE_LEN truncation, recovered. See #937 for the reproduction and the full scope.

Change

In deriveSymmetric, apply to the second key the same checks C_DeriveKey applies to the base key, before its value is read:

Check Error
Session read access (haveRead) as for the base key
Class is CKO_SECRET_KEY (PKCS#11 v3.2 §6.43.4: concatenation of two secret keys) CKR_KEY_TYPE_INCONSISTENT
CKA_DERIVE = CK_TRUE CKR_KEY_FUNCTION_NOT_PERMITTED
CKA_ALLOWED_MECHANISMS permits CKM_CONCATENATE_BASE_AND_KEY CKR_MECHANISM_INVALID

These are the error codes SoftHSM already returns for the same conditions on the base key. This is the patch proposed in #937, as suggested there, plus unit tests.

Requiring CKA_DERIVE on the second key is a policy choice the spec does not spell out; it is the check that protects keys with no CKA_ALLOWED_MECHANISMS list (the default), so I kept it, as recommended in the issue.

Tests

  • DeriveTests::testMiscDerivations used a derived key with the default CKA_DERIVE = FALSE as the second key. It now checks that this is refused, then sets CKA_DERIVE and continues as before.
  • New DeriveTests::testConcatenateBaseAndKeyChecks:
    • second key without CKA_DERIVE → CKR_KEY_FUNCTION_NOT_PERMITTED, no object created;
    • second key whose CKA_ALLOWED_MECHANISMS is { CKM_AES_GCM } → CKR_MECHANISM_INVALID, no object created;
    • EC P-256 private key with CKA_DERIVE = TRUE as second key → CKR_KEY_TYPE_INCONSISTENT, no object created (WITH_ECC);
    • second key with CKA_DERIVE and CKA_ALLOWED_MECHANISMS = { CKM_CONCATENATE_BASE_AND_KEY }, and one with no list, still derive, and the value is base || second.

Both tests fail on current main. I also removed each of the three new checks in turn: each removal makes testConcatenateBaseAndKeyChecks fail at the matching case, so every check is covered. p11test passes with OpenSSL 3.5.5 (CMake, -Werror -Wall -Wextra, 97 tests) and Botan 2 (autotools, -Werror, 86 tests).

Related

#905 changes the same function, in the attribute block further down (CKA_NEVER_EXTRACTABLE). The hunks do not overlap.

Summary by CodeRabbit

  • Bug Fixes
    • Symmetric key derivation now checks that the second key is authorized for reading, is a suitable secret key, permits derivation, and allows the requested mechanism.
    • Invalid second keys now cause derivation to fail with an error appropriate to the issue, rather than proceeding with the operation.
    • Added coverage for valid and invalid second-key settings, including successful derivation when no mechanism restrictions are configured.

deriveSymmetric read the CKA_VALUE of the key named in the
CKM_CONCATENATE_BASE_AND_KEY parameter without any of the checks
C_DeriveKey applies to the base key. Its class, CKA_DERIVE and
CKA_ALLOWED_MECHANISMS were ignored, so a sensitive, unextractable key
with CKA_DERIVE = FALSE, or a private key, could be concatenated into
a new key and, with CKA_VALUE_LEN truncation, recovered.

Apply the base key checks to the second key before reading it: read
access for the session, CKO_SECRET_KEY (CKR_KEY_TYPE_INCONSISTENT),
CKA_DERIVE (CKR_KEY_FUNCTION_NOT_PERMITTED) and CKA_ALLOWED_MECHANISMS
(CKR_MECHANISM_INVALID).

Tests:
- testMiscDerivations used a second key with the default CKA_DERIVE =
  FALSE; set CKA_DERIVE on it and check the call is refused without it.
- New testConcatenateBaseAndKeyChecks: a second key without CKA_DERIVE,
  one whose CKA_ALLOWED_MECHANISMS leaves the mechanism out, and an EC
  private key are refused and create no object; a second key with
  CKA_DERIVE (with or without an allowing CKA_ALLOWED_MECHANISMS) still
  derives base || second.
@coderabbitai

coderabbitai Bot commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The concatenation derivation path now validates the second key before reading its value. Regression tests cover authorization and attribute failures, successful concatenation, and the required derive permission.

Changes: Concatenation second-key validation

Layer / File(s) Summary
Validate the second key
src/lib/SoftHSM.cpp
For CKM_CONCATENATE_BASE_AND_KEY, checks now verify read authorization, secret-key type, CKA_DERIVE, and CKA_ALLOWED_MECHANISMS before the second key value is read.
Test second-key validation
src/lib/test/DeriveTests.cpp, src/lib/test/DeriveTests.h
Tests cover the validation error codes, successful concatenation with and without an allowed-mechanisms list, and the failure when CKA_DERIVE is unset.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix · Severity of issue fixed: High

Suggested reviewers: bukka

Merge Risk: Minimal · up to 954b5

This change closes the gap where the second key in a concatenation derivation was read without the checks applied to the base key. Keys that are ineligible are now refused, and eligible keys still derive. No remaining merge risk was identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "Fix #937: check the second key of CKM_CONCATENATE_BASE_AND_KEY" names the main change. The changeset adds checks on the second key in SoftHSM::deriveSymmetric (read authorization, CKO_SECRE…
Linked Issues check Passed [#937] The issue requires the second key of CKM_CONCATENATE_BASE_AND_KEY to be validated before its value is read. The whole-PR diff of src/lib/SoftHSM.cpp (merge base to head) adds, inside deriveSymm…
Out of Scope Changes check Passed All changes relate to #937. The production change is limited to the second-key validation in deriveSymmetric (+15 lines in src/lib/SoftHSM.cpp). The DeriveTests.cpp and DeriveTests.h changes add the r…
Full details: Title check

Explanation

The title "Fix #937: check the second key of CKM_CONCATENATE_BASE_AND_KEY" names the main change. The changeset adds checks on the second key in SoftHSM::deriveSymmetric (read authorization, CKO_SECRET_KEY type, CKA_DERIVE, and CKA_ALLOWED_MECHANISMS) before its value is read, and adds tests for these checks. The title also references the linked issue #937, which matches the stated objectives. It is concise and specific enough for a teammate scanning history.

Full details: Linked Issues check

Explanation

[#937] The issue requires the second key of CKM_CONCATENATE_BASE_AND_KEY to be validated before its value is read. The whole-PR diff of src/lib/SoftHSM.cpp (merge base to head) adds, inside deriveSymmetric after the handle check and before the CKA_PRIVATE/token->decrypt read of CKA_VALUE, these checks: (1) haveRead() session read access, returning its error; (2) CKA_CLASS must be CKO_SECRET_KEY, else CKR_KEY_TYPE_INCONSISTENT; (3) CKA_DERIVE must be CK_TRUE, else CKR_KEY_FUNCTION_NOT_PERMITTED; (4) isMechanismPermitted(otherKey, CKM_CONCATENATE_BASE_AND_KEY), else CKR_MECHANISM_INVALID. Each rejection returns before any object is created, so no derived object is produced. Eligible keys continue to the existing derivation path, so successful derivation is retained. The issue's regression-test requirement is addressed by the new DeriveTests::testConcatenateBaseAndKeyChecks (rejection cases and successful derivation with an eligible key, with and without an allowed-mechanisms list), and by the adjusted testMiscDerivations. I did not execute the tests; the test assertions are taken from the PR description and the test summary, and were not independently read in source.

Full details: Out of Scope Changes check

Explanation

All changes relate to #937. The production change is limited to the second-key validation in deriveSymmetric (+15 lines in src/lib/SoftHSM.cpp). The DeriveTests.cpp and DeriveTests.h changes add the regression test, a test helper, and the CppUnit registration, which support the required tests. The modification to testMiscDerivations follows from the new CKA_DERIVE check on the second key. No unrelated functional changes appear in the reviewed hunk. The reference to #905 concerns a separate, non-overlapping hunk and is not in this PR's code changes beyond the described context.

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: 2bc69146-c894-4cda-9946-bd314d3500ed
Commits

Reviewing files that changed from the base of the PR and between 8791ac1 and 954b5ae.

Files selected for processing: 3

  • src/lib/SoftHSM.cpp
  • src/lib/test/DeriveTests.cpp
  • src/lib/test/DeriveTests.h

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

@antoinelochet

Copy link
Copy Markdown
Contributor

LGTM

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.

C_DeriveKey(CKM_CONCATENATE_BASE_AND_KEY) does not check the second key: any sensitive key, CKA_DERIVE=FALSE included, can be extracted byte by byte

2 participants