Skip to content

Handle CKR_USER_ALREADY_LOGGED_IN in PKCS#11 session open#30

Merged
jdoss merged 1 commit intomasterfrom
fix/pkcs11-already-logged-in
Apr 16, 2026
Merged

Handle CKR_USER_ALREADY_LOGGED_IN in PKCS#11 session open#30
jdoss merged 1 commit intomasterfrom
fix/pkcs11-already-logged-in

Conversation

@jdoss
Copy link
Copy Markdown
Contributor

@jdoss jdoss commented Apr 16, 2026

Summary

When both the Nitrokey HSM provider and the HSM cache backend open PKCS#11 sessions on the same slot, the second C_Login call fails with CKR_USER_ALREADY_LOGGED_IN because the slot only allows one login. The cache backend's open() raised ProviderError and fell back to live provider lookups, silently disabling the cache on every serve startup.

This also happens across process restarts when serve is killed before its finally block runs close()/logout() — the next process inherits a stale login.

Fix: treat CKR_USER_ALREADY_LOGGED_IN as success — the slot is already authenticated and the session is usable.

Discovered while testing the cache key fix (PR #29) on the test server: the cache had the right keys but never loaded because the HSM backend couldn't open its session.

Test plan

  • pytest — 335 tests pass (HSM tests are integration-only, no hardware in CI).
  • ruff check / ty check — clean.
  • Deploy to test server, restart psi-secrets, confirm cache loads (no HSM login failed warning), stop Infisical, verify lookups return cached values.

When both the Nitrokey HSM provider and the HSM cache backend open
PKCS#11 sessions on the same slot, the second C_Login call fails with
CKR_USER_ALREADY_LOGGED_IN because the slot only allows one login.
The cache backend's open() raised ProviderError and fell back to live
provider lookups, silently disabling the entire cache.

This also happens across process restarts: if psi serve is killed
before its finally block runs close()/logout(), the next serve
process inherits a stale login on the slot.

Treat CKR_USER_ALREADY_LOGGED_IN as success — the slot is already
authenticated and the session is usable.
@jdoss jdoss merged commit cad5a99 into master Apr 16, 2026
1 of 2 checks passed
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.

1 participant