Repository navigation
Fix #937: check the second key of CKM_CONCATENATE_BASE_AND_KEY - #942
AlexLaroche wants to merge 1 commit into
Conversation
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.
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
|
LGTM |
Fixes #937.
Problem
For
CKM_CONCATENATE_BASE_AND_KEY,C_DeriveKeychecks the base key (read access,CKA_DERIVE,CKA_ALLOWED_MECHANISMS), butderiveSymmetricreads theCKA_VALUEof the second key, named in the mechanism parameter, with no checks at all. A sensitive, unextractable key withCKA_DERIVE = FALSE, or a private key (EC, EdDSA, ML-DSA, ML-KEM, SLH-DSA), could therefore be concatenated into a new key and, combined withCKA_VALUE_LENtruncation, recovered. See #937 for the reproduction and the full scope.Change
In
deriveSymmetric, apply to the second key the same checksC_DeriveKeyapplies to the base key, before its value is read:haveRead)CKO_SECRET_KEY(PKCS#11 v3.2 §6.43.4: concatenation of two secret keys)CKR_KEY_TYPE_INCONSISTENTCKA_DERIVE = CK_TRUECKR_KEY_FUNCTION_NOT_PERMITTEDCKA_ALLOWED_MECHANISMSpermitsCKM_CONCATENATE_BASE_AND_KEYCKR_MECHANISM_INVALIDThese 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_DERIVEon the second key is a policy choice the spec does not spell out; it is the check that protects keys with noCKA_ALLOWED_MECHANISMSlist (the default), so I kept it, as recommended in the issue.Tests
DeriveTests::testMiscDerivationsused a derived key with the defaultCKA_DERIVE = FALSEas the second key. It now checks that this is refused, then setsCKA_DERIVEand continues as before.DeriveTests::testConcatenateBaseAndKeyChecks:CKA_DERIVE→CKR_KEY_FUNCTION_NOT_PERMITTED, no object created;CKA_ALLOWED_MECHANISMSis{ CKM_AES_GCM }→CKR_MECHANISM_INVALID, no object created;CKA_DERIVE = TRUEas second key →CKR_KEY_TYPE_INCONSISTENT, no object created (WITH_ECC);CKA_DERIVEandCKA_ALLOWED_MECHANISMS = { CKM_CONCATENATE_BASE_AND_KEY }, and one with no list, still derive, and the value isbase || second.Both tests fail on current main. I also removed each of the three new checks in turn: each removal makes
testConcatenateBaseAndKeyChecksfail 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