Skip to content

feat(repeater): sign HTTPS requests with PKCS#11 client certificates - #817

Merged
aborovsky merged 2 commits into
feat/pkcs11-smart-cardfrom
feat/pkcs11-http-executor
Nov 10, 2026
Merged

aborovsky merged 2 commits into
feat/pkcs11-smart-cardfrom
feat/pkcs11-http-executor

Conversation

@aborovsky

Copy link
Copy Markdown
Contributor

What

HTTPS requests to targets matched by --pkcs11-cert now present the client certificate with the private key on a PKCS#11 token (smart card, USB token, HSM), through the OpenSSL PKCS#11 provider: libp11 pkcs11prov on Windows/macOS, pkcs11-provider on Linux. WebSocket targets are not covered.

How

  • Pre-flight (Pkcs11Preflight, via pkcs11js). Each certificate is checked once, and concurrent callers share the result. The check covers the token, the PIN, the key and the certificate.
    • There is one C_Login per token, and the session stays open.
    • Once C_Login has been called, any failure is remembered and never retried.
    • It refuses to log in when the PIN is locked or on its final try.
    • Key selection, when neither object nor id is set: the only key with a certificate, then the PIV authentication key, otherwise it fails and lists the keys.
    • An RSA key on a token without RSA-PSS limits the connection to TLS 1.2.
  • Long-lived handles (CurlHandlePool). curl loads the provider per easy handle and unloads it on close, which tears down the module state shared by the process. PKCS#11 requests therefore run on a fixed pool of handles per certificate (maxConcurrency), use curl.reset() between requests and use keep-alive connections.
  • OpenSSL config (Pkcs11OpenSslConfig). The generated OPENSSL_CONF holds paths only, in a 0700 temp dir that is removed on exit. The PIN travels in the in-memory key URI and is never logged.
    • Both providers tag their algorithms with their built-in name (provider=pkcs11prov / provider=pkcs11). Under any other name they fail with provider keymgmt failure.
    • So one provider instance runs per process, and all --pkcs11-cert entries must use the same vendor module. The parser rejects mixed modules.
  • Errors. Failures surface as PKCS11_* codes in the existing errorCode/message fields. The bridges protocol is unchanged.
  • Packaging. pkcs11js is an optional dependency: it compiles at install time, so npm i -g still works without build tools (verified in node:24-slim). It is then loaded only when PKCS#11 is used, with a clear message if it is missing.
  • Requests without a PKCS#11 match take the unchanged path.

Tests

  • Unit tests for the pre-flight, pool, config, URI, provider locator, client, and executor routing. All 485 unit tests pass.
  • tests/pkcs11/repeater.spec.ts drives the real HttpRequestExecutor against SoftHSM2. It covers:
    • 30 sequential requests on one connection with one login;
    • maxConcurrency 2 under 6 concurrent callers;
    • the certificate from a file;
    • a wrong PIN, giving PKCS11_PIN_INCORRECT on every request after a single login attempt;
    • a missing token;
    • two targets sharing one login.
  • Platforms: passes on macOS (libp11) and Linux/Docker (pkcs11-provider). CI covers Linux and Windows.
  • Stress run: 200 fresh handshakes at concurrency 8 over 4 handles gave 200/200 with 1 login, on both macOS and Linux.
  • Test fix: the low-level tls-stack test closed each handle as soon as its own worker finished, while handshakes on other handles were still running. Unloading the provider broke those handshakes in about 1 run out of 5. Handles are now closed after all workers finish.

Follow-ups

  • Map handshake failures (curl 35/58) to PKCS11_HANDSHAKE_FAILED.
  • Give waiting for a free PKCS#11 connection its own error code instead of ETIMEDOUT.
  • Remember which certificate worked when several entries match one URL.
  • Package pkcs11prov.dll, libcrypto-3-x64.dll and the pkcs11js addon with the Windows executable.

Requests to a target matched by --pkcs11-cert now use the key on the
token through the OpenSSL PKCS#11 provider (pkcs11-provider or libp11).

- A pre-flight per certificate, through pkcs11js, checks the token, the
  PIN and the key once. Concurrent callers share the result, a wrong PIN
  is never retried, and a locked PIN or one on its final try is not used.
- Requests run on a fixed pool of long-lived easy handles per
  certificate (maxConcurrency). Closing a handle unloads the provider and
  tears down the module state shared by the process.
- The generated OPENSSL_CONF holds paths only; the PIN travels in the key
  URI in memory. The providers only work under their built-in names, so
  all entries must use one vendor module.
- Failures surface as PKCS11_* error codes in the existing errorCode.
…rking

- Remember any failure once C_Login has been called; some vendors report
  a wrong PIN as a generic error.
- Reject a second entry for a token that is already logged in with a
  different PIN, since the provider would try that PIN on the card.
- Address tokens by serial as well as label, and refuse to choose
  between tokens that share a label.
- Make pkcs11js an optional dependency: it compiles at install time and
  must not break installs on machines without build tools.
- Treat a zero timeout as no limit, and report a provider that OpenSSL
  cannot load as PKCS11_MODULE_LOAD_FAILED.
@aborovsky
aborovsky merged commit 0c78aeb into feat/pkcs11-smart-card Nov 10, 2026
22 checks passed
@aborovsky
aborovsky deleted the feat/pkcs11-http-executor branch November 10, 2026 09:29
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