0
0
Fork 0
forked from freeipa/synta
synta/docs/api-reference.md
Alexander Bokovoy b2c52f158b docs: document PKCS#11 URI key loading in guides
- api-reference.md: add PKCS#11 URI Key Loading section with full API
  reference for from_pkcs11_uri(), pkcs11_info(), Pkcs11Uri, and
  Pkcs11UriAttributes including per-backend mandatory attribute table
- system-architecture.md: add PKCS#11 Key Loading Architecture section
  with OpenSSL (OSSL_STORE) and NSS (PK11_ListPrivKeysInSlot) Mermaid
  sequence diagrams and design decision table
- testing-guide.md: add PKCS#11/HSM integration test section documenting
  the kryoptic test, build-time library discovery, --test-threads=1
  requirement, and graceful skip behaviour
- deployment-guide.md: add HSM/PKCS#11 Key Storage section with step-by-step
  OpenSSL provider and NSS secmod setup, kryoptic usage example, and
  no-key-extraction guarantee
- codebase-summary.md: note new synta-certificate modules, foreign-types
  optional dependency, and PKCS#11 URI support in Python API surface

Signed-off-by: Alexander Bokovoy <abokovoy@redhat.com>
2026-04-15 11:55:52 +03:00

3.6 KiB

API Reference for Rust Crates

If the frame above is empty, build the documentation first:

bash contrib/ci/local-ci.sh doc

PKCS#11 URI Key Loading

The functions and types described below are part of synta-certificate and allow loading private keys directly from PKCS#11 hardware or software tokens without ever extracting key material into process memory.

BackendPrivateKey::from_pkcs11_uri

pub fn from_pkcs11_uri(uri: &str) -> Result<Self, PrivateKeyError>

Loads a private key from a PKCS#11 token identified by the given RFC 7512 pkcs11: URI. The URI must begin with the pkcs11: scheme.

The key handle (or, for the NSS backend, the slot reference) is stored inside the returned BackendPrivateKey. Key material is never extracted; all signing operations are delegated to the token.

When to use it: Use from_pkcs11_uri instead of from_pem or from_pkcs8_der whenever the private key resides in an HSM, smart card, or software token and must not leave the token boundary.

Backend-specific requirements:

Backend Mandatory URI attributes Prerequisites
OpenSSL (openssl feature) None pkcs11-provider configured via OPENSSL_CONF before the call
NSS (nss feature) token= and object= PKCS#11 module registered via modutil before the call

See deployment-guide.md for step-by-step setup instructions for each backend.

BackendPrivateKey::pkcs11_info

pub fn pkcs11_info(&self) -> Option<&Pkcs11Uri>

Returns the parsed PKCS#11 URI for keys loaded via from_pkcs11_uri, or None for software keys loaded from PEM/DER.

Useful for logging, diagnostics, or conditional logic that must behave differently for HSM-backed keys.

Pkcs11Uri

pub struct Pkcs11Uri {
    pub raw: String,              // verbatim URI string as supplied to from_pkcs11_uri
    pub attrs: Pkcs11UriAttributes,
}

Parsed representation of an RFC 7512 pkcs11: URI. The raw field preserves the original string exactly; attrs contains the pre-decoded path and query attributes.

Pkcs11UriAttributes

pub struct Pkcs11UriAttributes {
    pub token:     Option<String>,   // token= path attribute (slot/token label)
    pub object:    Option<String>,   // object= path attribute (key nickname/label)
    pub id:        Option<Vec<u8>>,  // id= CKA_ID, percent-decoded raw bytes
    pub pin_value: Option<String>,   // ?pin-value= query attribute (token PIN)
}

Pre-decoded path and query attributes extracted from a pkcs11: URI.

URI format example

pkcs11:token=MyHSM;object=cakey;type=private?pin-value=1234
  • token=MyHSM — the token label (mandatory for NSS, optional for OpenSSL)
  • object=cakey — the key label / nickname (mandatory for NSS, optional for OpenSSL)
  • type=private — selects the private-key object class (recommended)
  • pin-value=1234 — PIN supplied inline; omit if the token is already authenticated

Note on key material

BackendPrivateKey::pkcs8_der is always empty for HSM-backed keys. The token handle is stored internally and used for every signing operation. Callers should never attempt to serialise an HSM key via pkcs8_der.