Skip to content

Add PKCS#11 APIs to store, persist and delete token keys - #12112

Open
Frauschi wants to merge 5 commits into
wolfSSL:masterfrom
Frauschi:pkcs11-persist
Open

Frauschi wants to merge 5 commits into
wolfSSL:masterfrom
Frauschi:pkcs11-persist

Conversation

@Frauschi

Copy link
Copy Markdown
Member

Description

This PR adds a way to keep keys on a PKCS#11 token across sessions. Until now every key wolfSSL imported or generated was a session object that disappeared with its session.

  • wc_Pkcs11StoreKey_ex(..., persistent) imports a key as token objects. Private and secret keys get CKA_PRIVATE and CKA_SENSITIVE and lose CKA_EXTRACTABLE; public keys clear CKA_PRIVATE.
  • A persistent key needs an id or label that no token key of its type uses, so lookups stay unambiguous.
  • wc_Pkcs11PersistKey copies a session key, from keygen or wc_Pkcs11StoreKey, to the token and points devCtx at the copy.
  • wc_Pkcs11DeleteKey removes a key's token objects by id and label.
  • wc_Pkcs11Token_SetKeyGenPersistent makes keygen create named keys as token objects; nameless keys such as TLS key shares stay session or software keys.
  • Covers RSA, ECC, ML-DSA, ML-KEM, AES and HMAC.

Testing

  • New tests against the mock token: test_wc_Pkcs11_StoreKeyPersistent, test_wc_Pkcs11_PersistKey, test_wc_Pkcs11_DeleteKey, test_wc_Pkcs11_KeyGenPersistent, test_wc_Pkcs11_KeyNames, test_wc_Pkcs11_SecretKeyPersist, test_wc_Pkcs11_RsaPersistDelete, test_wc_Pkcs11_PqcPersistDelete, test_wc_Pkcs11_PersistPaths, test_wc_Pkcs11_PqcKeyGen.
  • The mock now defaults objects to CKA_PRIVATE true, as SoftHSM2 does, and checks the protecting attributes of every token key.

Merging

Adds tests next to #11876 and #12111 in tests/api/test_pkcs11.c, and changes the same pkcs11_dump_template guard as #11876; whichever merges later needs a small rebase there.

@Frauschi Frauschi self-assigned this Nov 12, 2026
Copilot AI balanced review requested due to automatic review settings November 12, 2026 12:31
@github-actions

github-actions Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

zephyr-frdm_rw612

  • FLASH: datas +16 B, log_const_area -8 B, rodata +108 B, text +13,864 B (+4.9%, 296,444 B / 67,108,864 B, total: 0% used)
  • RAM: bss +49 B, datas +16 B (+0.1%, 58,423 B / 983,040 B, total: 6% used)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

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

Unresolved PKCS#11 persistence and lookup defects can break key validation or signing, while missing test guards cause build failures when ECC is disabled.

3 open issues
What changed in this PR

Extends wolfCrypt’s PKCS#11 integration to retain named keys across sessions and explicitly delete them, while keeping unnamed keys such as TLS key shares temporary.

Changes:

  • Adds persistent import, session-key persistence, and token-key deletion APIs.
  • Adds opt-in persistent key generation, name-collision checks, and protective key attributes.
  • Documents the new APIs and expands mock-token tests for key lifecycles and failure paths.
File Description
wolfssl/​wolfcrypt/​wc_pkcs11.h Declares persistence APIs and token/session flags.
wolfssl/​wolfcrypt/​pkcs11.h Adds PKCS#11 session-state constants.
wolfcrypt/​src/​wc_pkcs11.c Implements persistent key management and key-generation behavior.
tests/​api/​test_pkcs11.h Registers the new PKCS#11 tests.
tests/​api/​test_pkcs11.c Extends the mock token and tests persistence, deletion, and key naming.
doc/​dox_comments/​header_files/​pkcs11.h Documents API requirements, behavior, and usage.

Review effort: Balanced

Comment thread tests/api/test_pkcs11.c Resolved
Comment thread tests/api/test_pkcs11.c Resolved
Comment thread wolfcrypt/src/wc_pkcs11.c Resolved
@Frauschi

Copy link
Copy Markdown
Member Author

@claude review

@claude claude 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.

Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.

Comment thread tests/api/test_pkcs11.c Resolved

@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 #12112

Scan targets checked: wolfcrypt-src, wolfcrypt-bugs, wolfssl-src, wolfssl-bugs
Not reviewed (no scan target covers them): doc/dox_comments/header_files/pkcs11.h, tests/api/test_pkcs11.h
Coverage: 4 of 4 in-scope changed file(s) opened by the reviewer

Findings: 1
1 finding(s) posted as inline comments (see file-level comments below)

This review was generated automatically by Fenrir. Reported findings require changes before merge.

Review tier: Max

Comment thread tests/api/test_pkcs11.c Resolved
@Frauschi

Copy link
Copy Markdown
Member Author

Jenkins retest this please

@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 #12112

Scan targets checked: wolfssl-src, wolfssl-bugs
Unchanged since last review (not re-run): wolfcrypt-src, wolfcrypt-bugs
Not reviewed (no scan target covers them): doc/dox_comments/header_files/pkcs11.h, tests/api/test_pkcs11.h
Coverage: 1 of 1 in-scope changed file(s) opened by the reviewer

Findings: 1
1 finding(s) posted as inline comments (see file-level comments below)

This review was generated automatically by Fenrir. Reported findings require changes before merge.

Review tier: Lite

Comment thread tests/api/test_pkcs11.c Resolved
@Frauschi

Copy link
Copy Markdown
Member Author

Jenkins retest this please

@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 #12112

Scan targets checked: wolfssl-src, wolfssl-bugs
Unchanged since last review (not re-run): wolfcrypt-src, wolfcrypt-bugs
Not reviewed (no scan target covers them): doc/dox_comments/header_files/pkcs11.h, tests/api/test_pkcs11.h
Coverage: 1 of 1 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: Lite

@wolfSSL-Fenrir-bot
wolfSSL-Fenrir-bot dismissed stale reviews from themself November 13, 2026 16:25

Fenrir's latest completed scan found no issues; clearing the prior automated change request.

@Frauschi

Copy link
Copy Markdown
Member Author

Jenkins retest this please

@Frauschi Frauschi removed their assignment Nov 24, 2026
@Frauschi
Frauschi requested a review from SparkiDev November 24, 2026 16:46
@SparkiDev

Copy link
Copy Markdown
Contributor

Looks good.
Claude had some comments:

  1. Login is checked only in delete.
    • wc_Pkcs11DeleteKey returns BAD_STATE_E when the user isn't logged in.
      StoreKey_ex(persistent), PersistKey and keygen-on-token don't check.
    • Without a login, Pkcs11CheckNewTokenKey can't see CKA_PRIVATE token objects, so the
      uniqueness check passes against an incomplete view. The create then fails with
      CKR_USER_NOT_LOGGED_IN, reported as a generic WC_HW_E.
    • Calling Pkcs11CheckUserSession in those paths too would give a consistent
      BAD_STATE_E, and would make the uniqueness check meaningful before anything is
      created.
  2. Uniqueness matches id or label, but delete matches id and label.
    • If the token holds a foreign key with the same label but a different id, StoreKey_ex
      refuses to create the new one, and DeleteKey with that wolfSSL key won't remove the
      foreign key either.
    • A key with only a label deletes every token key of that type with that label,
      including keys other applications made.
    • Both are defensible choices; the doxygen should say so explicitly.
  3. ECC persist without CKM_ECDSA. If the public key is known in software but the token has
    no ECDSA, no token public key is created, and the session public objects are still
    destroyed. That matches what StoreKey does, but the persisted key is then private-only.
    Worth a line in the doc.
  4. PersistKey isn't atomic across private and public keys. Failures are cleaned up as far
    as I traced. But if destroying the session object fails after a successful copy, both
    objects remain: rv is logged and ignored, and the function returns 0. That's harmless,
    as the session object goes when the session closes, but a comment would make it look
    less like an oversight.

wc_Pkcs11StoreKey creates session objects only, so an imported key is
gone once the session closes. wc_Pkcs11StoreKey_ex takes a persistent
flag that creates the objects with CKA_TOKEN set instead, so they stay
on the token and can be used by id or label later. Persistent objects
need a read/write session and a logged in user.

A persistent private or secret key also gets CKA_PRIVATE and
CKA_SENSITIVE set and CKA_EXTRACTABLE cleared, so it can be neither read
without a login nor exported while it stays on the token. wolfSSL never
reads the value of such a key back, only public attributes such as the
RSA modulus. Public key objects get CKA_TOKEN set and CKA_PRIVATE
cleared, as the default for CKA_PRIVATE is up to the token and a private
public key would be hidden from sessions that are not logged in. A
persistent RSA key gets a public key object on the token too, which
public key operations on a key loaded by name look up.

A persistent key needs an id or label, or it could not be found again,
and neither may be in use by a token key of the same type: wolfSSL finds
keys by either one, and two keys with one name make lookups ambiguous.
The call fails with BAD_FUNC_ARG or BAD_STATE_E otherwise. An HMAC key
is checked under its HMAC key type and as the generic secret that
wc_Pkcs11StoreKey falls back to. Objects of other types, such as the
certificate that usually shares the key's id, do not clash, since
lookups check the key type.

The create helpers take the setting from a new tokenObjects bit in
Pkcs11Session, which Pkcs11OpenSession clears, so sessions used for
crypto operations keep creating their temporary keys as session objects.
wc_Pkcs11StoreKey calls the new function with persistent set to 0, and a
NULL token or key now fails with BAD_FUNC_ARG.

A persistent store does not need a session from wc_Pkcs11Token_Open,
as its objects outlive the session the call opens and closes.
wc_Pkcs11StoreKey still returns BAD_STATE_E without one.
Keys generated through the PKCS#11 crypto callback, and keys imported
with wc_Pkcs11StoreKey, are session objects. Key generation cannot know
whether the caller wants to keep the key, so an application decides
after the fact, for example once a certificate for the key has been
issued.

wc_Pkcs11PersistKey copies the session object to a token object with
C_CopyObject, which PKCS#11 allows for keys that are not extractable,
sets the protecting attributes wc_Pkcs11StoreKey_ex uses and the key's
id and label, and destroys the session original. The session object is
the handle in devCtx when key generation left one there, otherwise the
session object with the key's label and id. RSA, ECC, ML-DSA, ML-KEM,
AES and HMAC keys are supported. An HMAC key needs its hash type and is
found under its HMAC key type or as a generic secret.

For RSA, ECC, ML-DSA and ML-KEM keys that hold their public key, a token
public key object is created too, because RSA public key operations, the
check-private-key paths and ML-KEM encapsulation look the public key up
by id or label for a key loaded by name. As wc_Pkcs11StoreKey_ex does,
an ECC public key object is created only when the token supports
CKM_ECDSA, and a failure to create it is ignored when the token's
private key object cannot sign. For ECC, ML-DSA and ML-KEM, session
public key objects with the same names are destroyed, so such a lookup
cannot find a stale one. A key without its public part, such as one
initialized by id or label only, gets a token copy of the session public
key object with its names instead, and the session object is destroyed
only after the copy succeeds. An RSA session public key never has a
name, since neither wc_Pkcs11StoreKey nor session key generation gives
it one. RSA, ECC, ML-DSA and ML-KEM keys then refer to the new private
object through devCtx, which the free callback leaves alone.

The key needs an id or label that no token key of its type has, and
BAD_STATE_E is returned when devCtx already refers to a token object.
On failure the token objects created so far are destroyed and the key
is left as it was.
Freeing a key leaves its token objects alone, so an application that
rotates a persistent key needs a way to remove the old one.
wc_Pkcs11DeleteKey destroys every token object of the key type with the
key's id and label, which covers the private key and the public key
object wc_Pkcs11PersistKey or wc_Pkcs11StoreKey_ex created. Labels need
not be unique, so an object must match every name the key has. For RSA,
ECC, ML-DSA and ML-KEM keys a session object in devCtx is destroyed too
and devCtx is cleared. If a C_DestroyObject fails part way, devCtx is
cleared when its object is already gone, so the key is not left with a
dead handle. A delete refused before destroying anything keeps devCtx.
An HMAC key is removed under its HMAC key type and as a generic secret,
and needs its hash type. Nothing to remove is not an error.

Private keys that wolfSSL stores on the token carry CKA_PRIVATE, so a
session without a user login does not find them, and a delete would
remove only the public object and report success. The session state is
checked with C_GetSessionInfo first, and BAD_STATE_E is returned unless
the user is logged in. The session state values are added to pkcs11.h.

The new tests also cover persisting and deleting RSA, ECC, ML-DSA, ML-
KEM, AES and HMAC keys, the naming rules across the three functions, and
failures of C_CopyObject, C_GetSessionInfo and C_DestroyObject.
wc_Pkcs11PersistKey makes a generated session key persistent after the
fact, but some tokens cannot generate a usable session key at all. NXP's
SMW module on the EdgeLock Enclave reports the public point only for
token objects and returns an empty CKA_EC_POINT for session keys, and
the ELE cannot import a plaintext key, so wolfSSL's ECC key generation
failed there.

wc_Pkcs11Token_SetKeyGenPersistent sets a keyGenOnToken bit in
Pkcs11Token. While it is set, RSA, ECC, ML-DSA and ML-KEM key generation
through the crypto callback opens a read/write session and creates a key
that has an id or label as a token object, with the protecting
attributes of wc_Pkcs11StoreKey_ex. A key without a name, such as a TLS
ECDHE ephemeral key, stays a session object. The public key is kept as a
token object with the key's names, as wc_Pkcs11PersistKey leaves it. Key
generation fails with BAD_STATE_E when a token key of the type already
has the id or label.

A token key outlives a session that closes after key generation, so key
generation without an open token session is allowed for it. The handle
from the closed session is not kept, and the key is found later by its
id or label.
wc_Pkcs11DeleteKey already returned BAD_STATE_E when the user is not
logged in, but wc_Pkcs11StoreKey_ex with persistent set,
wc_Pkcs11PersistKey and key generation with
wc_Pkcs11Token_SetKeyGenPersistent did not check. Without a login the
uniqueness search cannot see CKA_PRIVATE token objects, so it passed
against an incomplete view, and the create then failed with
CKR_USER_NOT_LOGGED_IN, reported as WC_HW_E. Pkcs11CheckNewTokenKey now
calls Pkcs11CheckUserSession first, which covers all three paths and
returns BAD_STATE_E before anything is created. This includes a
persistent store of an ML-DSA or ML-KEM public key on its own, which
used to work without a login although its uniqueness search could miss
a private key with the same id or label, and which wc_Pkcs11DeleteKey
could not remove without a login anyway.

The documentation now states that a persistent store needs a login
even for a public key on its own, that the uniqueness check rejects a
token key matching only the id or only the label, that deleting a key
with only a label or only an id removes every token key of the type
with that name, and that persisting an ECC key that holds its public
key on a token without ECDSA leaves only the private key. A comment
notes that a session object whose destroy fails after the copy stays
until the token session closes.
@Frauschi

Copy link
Copy Markdown
Member Author

Addressed in cfc5083: persistent store, PersistKey and token keygen now check the login first and return BAD_STATE_E. Points 2-3 are documented, 4 has a comment.

@Frauschi

Copy link
Copy Markdown
Member Author

Jenkins retest this please

1 similar comment
@Frauschi

Copy link
Copy Markdown
Member Author

Jenkins retest this please

@Frauschi Frauschi removed their assignment Nov 26, 2026
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