Signing through opaque private-key handles
README.md

signer #

Signing through opaque private-key handles, for OCaml.

A program that signs with a key held in its own memory has to be rewritten when the key moves to an OS keychain, a PKCS#11 token, a TPM or a remote KMS, since none of them hands the private key over. PKCS#11 is the OASIS C API through which a program asks a smart card or a hardware token to sign. A TPM (Trusted Platform Module, ISO/IEC 11889) is a security chip on the machine's board that holds keys and signs on request. A KMS (key management service) is a cloud service that signs on request over HTTPS. All of them return signatures and never export the private key.

signer puts a private key behind a handle that gives the public key, the certificates that name that key, and a sign operation, and never the private key itself. Code written against the handle keeps working when the key moves, because only the backend changes.

An algorithm is a single identifier that combines key type, padding and digest, such as Ecdsa_p256_sha256, because OS key stores, PKCS#11 tokens and KMS APIs negotiate signatures in those terms.

Installation #

$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam install signer

Usage #

The software backend keeps an X509.Private_key.t in process memory:

# let key = X509.Private_key.generate ~seed:"signer-readme" `ED25519
val key : X509.Private_key.t = `ED25519 <abstr>
# let signer = Signer.Software.v key
val signer : Signer.t = <abstr>
# Signer.algorithms signer
- : Signer.Algorithm.t list = [Signer.Algorithm.Ed25519]
# (match Signer.sign signer Signer.Algorithm.Ed25519 "hello" with
   | Ok signature ->
       X509.Public_key.verify `SHA512 ~scheme:`ED25519 ~signature
         (Signer.public_key signer) (`Message "hello")
   | Error _ -> assert false)
- : (unit, [> `Msg of string ]) result = Ok ()
# Signer.sign signer Signer.Algorithm.Rsa_pkcs1_sha256 "hello"
- : (string, Signer.Error.t) result =
Error (Signer.Error.Unsupported Signer.Algorithm.Rsa_pkcs1_sha256)

Signatures come in the encoding X509.Public_key.verify expects: the 64-octet r || s form for Ed25519 (RFC 8032), a DER-encoded ECDSA-Sig-Value for ECDSA (RFC 5480), and an octet string as long as the modulus for RSA (RFC 8017).

JWA, COSE and UCAN carry an ECDSA signature as the fixed-width r || s rather than DER. Signer.Ecdsa.sign signs through a handle and answers that form; Signer.Ecdsa.raw_of_der and der_of_raw convert either way.

Four sublibraries reach key stores, hardware and other formats. They are kept separate so that the core signer library links no transport and no platform framework.

  • signer.macos keeps keys in the macOS keychain. Signer_macos.generate creates a P-256, P-384, P-521 or RSA key with SecKeyCreateRandomKey, either permanently in the default keychain or for the current process only. Signer_macos.get looks up a keychain key by its label. Signing goes through SecKeyCreateSignature, so the private key never enters the process. With ~secure_enclave:true the key is a P-256 key made inside the Secure Enclave, which it never leaves; storing one permanently needs a binary signed with a keychain-access-groups entitlement. Signer_macos.available tells whether the build links against Security.framework, which lets a portable program choose a backend at run time. A program that links it loads Security and CoreFoundation, and the closure they link, as it starts.
  • signer.tpm holds keys in a TPM. Signer_tpm.of_handle wraps a loaded NIST P-256, P-384 or P-521 signing key and signs through TPM2_Sign over a tpm-eio transport; Signer_tpm.primary creates the key. The bus is the wire between the CPU and the TPM (SPI or LPC on a board, reached through the kernel's /dev/tpmrm0 device), and a hostile host or a probe can sit on it. Without ?salt, signing uses a password session, which sends the key's authorisation in clear and binds nothing, so it is safe only over a trusted transport. ?salt is the handle of an ECC P-256 decryption key already loaded in the TPM. With it, the session key is derived by ephemeral ECDH to that key and never crosses the bus. The command HMAC under the session key covers the digest and the key's name, and the response HMAC is checked before the signature is trusted, so something sitting on the bus can neither swap the digest being signed nor forge the signature that comes back. The caller is responsible for the public half of the salt key being authentic, for example an endorsement key checked against its certificate.
  • signer.pkcs11 holds keys on a PKCS#11 token. Signer_pkcs11.Make signs with C_Sign, for EC and Ed25519 keys.
  • signer.cose adapts a signer to COSE (CBOR Object Signing and Encryption, RFC 9052), the CBOR counterpart of JOSE. A COSE_Sign1 is the COSE structure for a single signer: a CBOR array of a protected header, an unprotected header, the payload, and one signature over a canonical encoding of the protected header and the payload. signer.cose lets any backend sign one.

A custom backend implements Signer.BACKEND and is turned into a signer by Signer.Make. A backend that talks to an OS key store can block on the store's IPC and on authorisation prompts, so it should run away from any event loop that needs low latency.

Errors #

Signer.Error.t has the cases Missing, Unsupported of Algorithm.t, Locked, Interaction_required, Denied, Unavailable and Backend of string. A caller can act on the case, by prompting, picking another algorithm or falling back, without parsing message strings.

  • Java's provider-backed KeyStore and Apple's SecKey have the same shape: the application asks the key store to sign and never receives the bytes of the private key.
  • Apple's Secure Enclave guidance takes the model furthest: the key cannot leave the hardware, and every operation goes through the handle.
  • The keyring package is the companion for secrets that can be read out, such as tokens and passphrases; signer is for keys that should never leave their store.

Licence #

ISC. See LICENSE.md.