From a0430544ce07867adc7cd42bb1ddb947b0bf49bb Mon Sep 17 00:00:00 2001 From: David Papp Date: Mon, 9 Nov 2026 13:56:06 +0100 Subject: [PATCH 1/4] feat(use-cases): TLS keys in an HSM over PKCS#11 --- README.md | 4 + use-cases/README.md | 1 + use-cases/hsm-tls-keys/Dockerfile | 21 +++ use-cases/hsm-tls-keys/README.md | 176 ++++++++++++++++++++++ use-cases/hsm-tls-keys/client.sh | 28 ++++ use-cases/hsm-tls-keys/config.yaml | 55 +++++++ use-cases/hsm-tls-keys/demo.sh | 16 ++ use-cases/hsm-tls-keys/docker-compose.yml | 52 +++++++ use-cases/hsm-tls-keys/make-token.sh | 58 +++++++ use-cases/hsm-tls-keys/softhsm2.conf | 4 + use-cases/hsm-tls-keys/upstreams.yaml | 37 +++++ 11 files changed, 452 insertions(+) create mode 100644 use-cases/hsm-tls-keys/Dockerfile create mode 100644 use-cases/hsm-tls-keys/README.md create mode 100755 use-cases/hsm-tls-keys/client.sh create mode 100644 use-cases/hsm-tls-keys/config.yaml create mode 100755 use-cases/hsm-tls-keys/demo.sh create mode 100644 use-cases/hsm-tls-keys/docker-compose.yml create mode 100755 use-cases/hsm-tls-keys/make-token.sh create mode 100644 use-cases/hsm-tls-keys/softhsm2.conf create mode 100644 use-cases/hsm-tls-keys/upstreams.yaml diff --git a/README.md b/README.md index 2e583e9..58e855a 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,10 @@ is at [docs.gen0sec.com](https://docs.gen0sec.com). `SynapseProxy` of the Synapse operator serves an Ingress with a certificate from cert-manager, and picks up a renewal without a restart. Needs Synapse operator 0.2.0 (not released yet) and Synapse 0.8.8 or later. +- [TLS keys in an HSM](./use-cases/hsm-tls-keys/): serve certificates whose + private keys live in an HSM or other PKCS#11 token, beside a software key, + with the PIN in a file. The demo runs SoftHSM in docker. Needs Synapse 0.9.0 + (not released yet). ### Being updated diff --git a/use-cases/README.md b/use-cases/README.md index e09b09b..2536279 100644 --- a/use-cases/README.md +++ b/use-cases/README.md @@ -26,6 +26,7 @@ closest to what you want and copy it. Every set uses placeholder values | [`tcp-mss`](./tcp-mss/) | proxy | no | none: the demo listens on port 8080 | Cap the MSS of the connections a listener accepts; needs Synapse 0.8.9 or later | | [`edge-headers`](./edge-headers/) | proxy | no | none: the demo listens on ports 8080 and 8443 | `X-Forwarded-For`, `X-Forwarded-Proto`, `Via`, a request ID and HSTS; needs Synapse 0.8.9 or later | | [`header-rules`](./header-rules/) | proxy | no | none: the demo listens on port 8080 | Add and remove request and response headers, with a script that checks every rule | +| [`hsm-tls-keys`](./hsm-tls-keys/) | proxy | no | none: the demo listens on ports 8085 and 8443 | TLS private keys in an HSM over PKCS#11 (RSA and EC), served beside a software key, with the PIN in a file; the demo uses SoftHSM in docker; needs Synapse 0.9.0 (not released yet) | | [`kubernetes-cert-manager`](./kubernetes-cert-manager/) | proxy | no | a privileged proxy pod, Kubernetes cluster | A `SynapseProxy` of the Synapse operator serving an Ingress with a certificate from cert-manager, and the proxy picking up a renewed certificate without a restart; needs Synapse operator 0.2.0 (not released yet) and Synapse 0.8.8 or later | "Privileges" lists what the set's own config needs; the installation guide diff --git a/use-cases/hsm-tls-keys/Dockerfile b/use-cases/hsm-tls-keys/Dockerfile new file mode 100644 index 0000000..f23d21b --- /dev/null +++ b/use-cases/hsm-tls-keys/Dockerfile @@ -0,0 +1,21 @@ +# Two images from one file: `token` provisions a SoftHSM token, `synapse` is the +# Synapse image with the SoftHSM module added. A real HSM needs neither: you +# install your vendor's PKCS#11 module instead. +ARG SYNAPSE_IMAGE=registry.gen0sec.com/gen0sec/synapse:0.9.0 + +FROM debian:13 AS token +RUN apt-get update \ + && apt-get install -y --no-install-recommends softhsm2 opensc openssl \ + && rm -rf /var/lib/apt/lists/* +ENV SOFTHSM2_CONF=/etc/synapse/softhsm2.conf + +FROM debian:13 AS softhsm +RUN apt-get update \ + && apt-get install -y --no-install-recommends softhsm2 \ + && rm -rf /var/lib/apt/lists/* + +# The Synapse image has no shell and no package manager, so the module is +# copied in. Its libraries (libcrypto, libstdc++, libz, libzstd) are already there. +FROM ${SYNAPSE_IMAGE} AS synapse +COPY --from=softhsm /usr/lib/softhsm/libsofthsm2.so /usr/lib/softhsm/libsofthsm2.so +ENV SOFTHSM2_CONF=/etc/synapse/softhsm2.conf diff --git a/use-cases/hsm-tls-keys/README.md b/use-cases/hsm-tls-keys/README.md new file mode 100644 index 0000000..daa6cb2 --- /dev/null +++ b/use-cases/hsm-tls-keys/README.md @@ -0,0 +1,176 @@ +# TLS private keys in an HSM + +Keep the private keys of your TLS certificates in a hardware security module +(HSM), or any other PKCS#11 token, and have Synapse sign handshakes with them. +The key never leaves the device. **Needs Synapse 0.9.0 (not released yet).** +Earlier releases cannot use a `pkcs11:` key. + +```yaml +proxy: + certificates: "/certs" + default_certificate: "www.example.com" + hsm: + modules: + - /usr/lib/softhsm/libsofthsm2.so + pin_file: /hsm/pin + retry_interval_secs: 30 + on_failure: fail +``` + +```yaml +hosts: + www.example.com: + tls: + terminate: + cert: www.example.com +``` + +The certificates stay on disk. Beside `www.example.com.crt` sits +`www.example.com.key`, and its one line says where the key is: + +``` +pkcs11:token=synapse;object=www;type=private +``` + +## Things to know + +- A `.key` file whose first line is a `pkcs11:` URI is an HSM key. A PEM key in + a `.key` file is a software key, as before. Both kinds can sit in one + directory, as in the demo (`static.example.com`). +- `cert:` in `upstreams.yaml` is the certificate's **name**, the file stem in + `proxy.certificates`, never a path. There is no `key:` under `terminate`: the + key is found beside the certificate. +- The URI MUST NOT carry the PIN. `pin-value` and `pin-source` are rejected. + The PIN is in `proxy.hsm.pin_file`: a regular, non-empty file, owned by the + service user or root, mode `0600`. Synapse refuses the file otherwise, and + its errors name the path, never the content. With systemd, hand the file over + with `LoadCredential=` and point `pin_file` at + `/run/credentials//`. `PROXY_HSM_PIN_FILE` overrides the key. +- `modules` is tried in order, and the first that loads and yields the key + wins. List a second module for failover. +- Synapse reaches the token through the OpenSSL `pkcs11` provider. `provider_dir` + is the directory of its `pkcs11.so`; unset, the usual distribution + directories are probed. On Debian and Ubuntu it is the `pkcs11-provider` + package, which the deb and rpm packages suggest. A container image that ships + it needs nothing more than the vendor's PKCS#11 module. +- A key in the HSM is never read from a file: there is no software fallback. +- `retry_interval_secs` is how often, in seconds, every HSM key signs a test + message. When one cannot, Synapse reloads the certificates through `modules`, + so a recovered HSM is picked up within one interval. `0` turns the probe off. +- `on_failure` is what a host gets when its HSM key cannot be loaded. `fail` + (the default) refuses the TLS handshake for its names. `fallback` serves the + default certificate and logs a warning. During an outage a reload keeps the + last working context until its certificate expires, in both modes. +- The `hsm` block is read at start-up. Changing `modules`, `pin_file` or + `retry_interval_secs` needs a restart. A replaced PIN file is picked up at + the next certificate load. +- The built-in ACME client makes software keys. For a certificate on an HSM + key, make the CSR on the device and have certbot issue from it with + `certbot certonly --csr`. This set does not build that. + +## Run + +The demo uses SoftHSM, a software PKCS#11 token, so no hardware is needed. +Docker and Compose v2 are needed. + +```sh +cd use-cases/hsm-tls-keys +docker compose up -d --build +docker compose run --rm client +``` + +Or all of it, with the log check, in one go: `./demo.sh`. + +What runs: + +- `token` (from `Dockerfile`, target `token`) runs `make-token.sh` once: it + initialises a SoftHSM token named `synapse` with a random PIN, puts an RSA + key (`www`) and an EC P-256 key (`api`) on it, and writes a certificate and a + `.key` file with the URI for each. It also writes a plain software key and + certificate (`static`), and the PIN file with mode `0600`. +- `synapse` is the Synapse image plus SoftHSM's module, built from + `Dockerfile`, target `synapse`. The Synapse image has no shell and no + package manager, so `libsofthsm2.so` is copied in from a Debian stage. With a + real HSM you install your vendor's module instead. +- `client` connects to each host with `openssl s_client`, over TLS 1.2 and + TLS 1.3, and sees only the certificates, not the token or the PIN. + +`docker-compose.yml` builds on `SYNAPSE_IMAGE`; set it to use a build of your +own. The host maps ports 8085 (HTTP) and 8443 (HTTPS). + +### What it prints + +``` +== www.example.com: key line in www.example.com.key +pkcs11:token=synapse;object=www;type=private + tls1_2: TLSv1.2, rsaEncryption, signature RSA-PSS, verification OK + tls1_3: TLSv1.3, rsaEncryption, signature RSA-PSS, verification OK + request: Host: www.example.com +== api.example.com: key line in api.example.com.key +pkcs11:token=synapse;object=api;type=private + tls1_2: TLSv1.2, id-ecPublicKey, signature ECDSA, verification OK + tls1_3: TLSv1.3, id-ecPublicKey, signature ECDSA, verification OK + request: Host: api.example.com +== static.example.com: key line in static.example.com.key +-----BEGIN PRIVATE KEY----- + tls1_2: TLSv1.2, id-ecPublicKey, signature ECDSA, verification OK + tls1_3: TLSv1.3, id-ecPublicKey, signature ECDSA, verification OK + request: Host: static.example.com +== no SNI: the default certificate +subject=CN=www.example.com +``` + +The two HSM hosts have no private key on disk, only the URI, so every +handshake for them was signed by the token. `./demo.sh` then looks for the PIN +in the logs of all services and finds none. + +To start over, remove the volumes: + +```sh +docker compose down -v +``` + +### What goes wrong + +- A `pkcs11:` key with no `proxy.hsm` block is an error at load. +- A PIN file that is group or world readable, empty, or not a regular file is + refused, and the error names the rule that failed. +- With the token unreachable, `on_failure: fail` refuses the handshake with + `unrecognized_name` for the names of that certificate. The software host is + unaffected. + +## For production + +- **The demo imports its keys.** `make-token.sh` makes each key in software and + imports it with `softhsm2-util --import`, so for a moment a copy of the + private key is on disk. That is a shortcut for the demo. A real HSM makes the + key on the device, non-extractable, and signs the certificate request there + (`pkcs11-tool --keypairgen`, or the vendor's tool). Only the certificate + comes out. +- Replace SoftHSM with your vendor's module in `proxy.hsm.modules`, and use a + certificate from your CA. +- Keep the PIN file out of the configuration directory and out of version + control, mode `0600`, and hand it to the service with `LoadCredential=`. +- Use your own host names for the `hosts:` keys, for `cert:` and for the + certificate file names. +- Set `provider_dir` if the `pkcs11.so` of the provider is not in a + distribution directory. + +## Not checked here + +The set was run against SoftHSM only, with RSA and EC keys over TLS 1.2 and +TLS 1.3, using a build of Synapse that has HSM keys. It was not run against a +hardware HSM, nor against a published image: no release has HSM keys yet. The +`make test` check (`synapse --check-config`) needs a release that has both +`--check-config` and `proxy.hsm`; until then it reports `proxy.hsm` as an +unused option. + +## Files + +- `config.yaml`, `upstreams.yaml`: the Synapse set. +- `softhsm2.conf`: where SoftHSM keeps its token. Not needed with a real HSM. +- `docker-compose.yml`, `Dockerfile`: the token provisioner, Synapse with the + SoftHSM module, a test back-end and the client. +- `make-token.sh`: makes the token, keys, certificates and PIN file. +- `client.sh`: what the `client` service prints. +- `demo.sh`: the demo end to end, with the check that the PIN is in no log. diff --git a/use-cases/hsm-tls-keys/client.sh b/use-cases/hsm-tls-keys/client.sh new file mode 100755 index 0000000..d3ec725 --- /dev/null +++ b/use-cases/hsm-tls-keys/client.sh @@ -0,0 +1,28 @@ +#!/bin/sh +# Runs in the client container: what Synapse serves for each host, over +# TLS 1.2 and TLS 1.3. The certificates are self-signed, so each host's own +# certificate file is the trust anchor. +get() { printf 'GET / HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n\r\n' "$1"; } +fail=0 + +for host in www.example.com api.example.com static.example.com; do + echo "== $host: key line in $host.key" + head -1 "/certs/$host.key" | cut -c1-60 + for v in tls1_2 tls1_3; do + out=$(openssl s_client -connect "$host:443" -servername "$host" -"$v" \ + -CAfile "/certs/$host.crt" -verify_hostname "$host" &1) + proto=$(echo "$out" | sed -n 's/^New, \(TLSv[0-9.]*\),.*/\1/p' | head -1) + sig=$(echo "$out" | sed -n 's/^Peer signature type: *//p' | head -1) + kind=$(echo "$out" | openssl x509 -noout -text 2>/dev/null | sed -n 's/^ *Public Key Algorithm: *//p') + ver=$(echo "$out" | sed -n 's/^Verification: *//p' | head -1) + echo " $v: ${proto:-no handshake}, $kind, signature ${sig:-?}, verification ${ver:-?}" + [ "$ver" = OK ] && [ -n "$proto" ] || fail=1 + done + echo " request: $(get "$host" | openssl s_client -quiet -connect "$host:443" \ + -servername "$host" -CAfile "/certs/$host.crt" 2>/dev/null | grep -m1 '^Host:')" +done + +echo "== no SNI: the default certificate" +openssl s_client -connect www.example.com:443 /dev/null \ + | openssl x509 -noout -subject +exit $fail diff --git a/use-cases/hsm-tls-keys/config.yaml b/use-cases/hsm-tls-keys/config.yaml new file mode 100644 index 0000000..94fb892 --- /dev/null +++ b/use-cases/hsm-tls-keys/config.yaml @@ -0,0 +1,55 @@ +# Proxy mode, no firewall or capture, so the container needs no privileges. +mode: proxy + +firewall: + mode: none +capture: + enabled: false + +proxy: + listeners: + - { name: http, address: "0.0.0.0:80", mode: http } + - { name: https, address: "0.0.0.0:443", mode: tls } + upstream: + conf: "/etc/synapse/upstreams.yaml" + + # Certificates stay on disk. Each .crt has a .key beside it. A + # .key file whose first line is a pkcs11: URI is a key in the HSM; a PEM .key + # file is a software key. Both can sit in the same directory: + # + # www.example.com.crt certificate + # www.example.com.key pkcs11:token=synapse;object=www;type=private + # api.example.com.crt + # api.example.com.key pkcs11:token=synapse;object=api;type=private + # static.example.com.crt + # static.example.com.key an ordinary PEM private key + # + # The URI MUST NOT carry the PIN: pin-value and pin-source are rejected. + certificates: "/certs" + + # Served to a client that sends no SNI, or a name no certificate covers. + default_certificate: "www.example.com" + + hsm: + # PKCS#11 modules, tried in order; list a second one for failover. This is + # SoftHSM, a software token for demos. Use your vendor's module. + modules: + - /usr/lib/softhsm/libsofthsm2.so + # The token PIN. A regular, non-empty file owned by the service user or + # root, mode 0600. With systemd, hand it over with LoadCredential= and + # point this at /run/credentials//. PROXY_HSM_PIN_FILE + # overrides it. + pin_file: /hsm/pin + # How often, in seconds, every HSM key signs a test message. A failure + # reloads the certificates through `modules`, so a recovered HSM is picked + # up within one interval. 0 turns the probe off. + retry_interval_secs: 30 + # What a host whose HSM key cannot be loaded gets: + # fail the TLS handshake for its names is refused (default) + # fallback the default certificate is served, with a warning + # During an outage a reload keeps serving the last working context until + # its certificate expires, in both modes. + on_failure: fail + +logging: + level: info diff --git a/use-cases/hsm-tls-keys/demo.sh b/use-cases/hsm-tls-keys/demo.sh new file mode 100755 index 0000000..28d69cd --- /dev/null +++ b/use-cases/hsm-tls-keys/demo.sh @@ -0,0 +1,16 @@ +#!/bin/sh +# The whole demo, from the host: start, check each host, and check that the +# PIN reached no log. Needs Docker with Compose v2. Leaves the stack running. +set -eu +cd "$(dirname "$0")" + +docker compose up -d --build +docker compose run --rm client + +pin=$(docker compose run --rm --no-deps -T --entrypoint cat token /hsm/pin) +[ -n "$pin" ] || { echo "no PIN read"; exit 1; } +if docker compose logs --no-color 2>&1 | grep -qF "$pin"; then + echo "FAIL: the PIN appears in a log" + exit 1 +fi +echo "== the PIN appears in no log" diff --git a/use-cases/hsm-tls-keys/docker-compose.yml b/use-cases/hsm-tls-keys/docker-compose.yml new file mode 100644 index 0000000..4b0624d --- /dev/null +++ b/use-cases/hsm-tls-keys/docker-compose.yml @@ -0,0 +1,52 @@ +# Run: docker compose up -d +# Synapse with the SoftHSM module added, built from the image in SYNAPSE_IMAGE. +# The default is the release that has HSM keys; to use a build of your own, set +# SYNAPSE_IMAGE (for example SYNAPSE_IMAGE=synapse:local docker compose up -d). +services: + # Makes the token, the certificates and the PIN file once, into volumes. + token: + build: { context: ., target: token } + entrypoint: ["/etc/synapse/make-token.sh"] + volumes: + - ".:/etc/synapse:ro" + - "hsm:/hsm" + - "certs:/certs" + synapse: + build: + context: . + target: synapse + args: + SYNAPSE_IMAGE: ${SYNAPSE_IMAGE:-registry.gen0sec.com/gen0sec/synapse:0.9.0} + image: hsm-tls-keys-synapse:local + command: ["-c", "/etc/synapse/config.yaml"] + ports: ["8085:80", "8443:443"] + volumes: + - ".:/etc/synapse:ro" + - "hsm:/hsm:ro" + - "certs:/certs:ro" + networks: + hsm: + aliases: [www.example.com, api.example.com, static.example.com] + depends_on: + token: { condition: service_completed_successfully } + whoami: { condition: service_started } + whoami: + image: traefik/whoami:v1.10.3 + hostname: whoami + networks: [hsm] + client: + image: alpine/openssl:3.3.2 + profiles: ["demo"] + entrypoint: ["/bin/sh", "/demo/client.sh"] + volumes: + - "./client.sh:/demo/client.sh:ro" + # The certificates only: not the token or the PIN. + - "certs:/certs:ro" + networks: [hsm] + +volumes: + hsm: + certs: + +networks: + hsm: diff --git a/use-cases/hsm-tls-keys/make-token.sh b/use-cases/hsm-tls-keys/make-token.sh new file mode 100755 index 0000000..2691d1c --- /dev/null +++ b/use-cases/hsm-tls-keys/make-token.sh @@ -0,0 +1,58 @@ +#!/bin/sh +# Runs in the `token` container. Makes a SoftHSM token, puts an RSA key and an +# EC key in it, makes a certificate for each, and writes what Synapse reads: +# +# /certs/.crt certificate +# /certs/.key a pkcs11: URI (HSM keys) or a PEM key (the software key) +# /hsm/pin the token PIN, mode 0600 +# /hsm/tokens the token itself +# +# DEMO SHORTCUT: the keys are made in software and then imported into the token +# with `softhsm2-util --import`. That puts a copy of each private key on disk +# for the moment it takes to import it; it is deleted at the end. A real HSM +# makes the key on the device (`pkcs11-tool --keypairgen`, or the vendor's tool) +# and never lets it out. Issue the certificate from a CSR signed on the device. +set -eu + +[ -f /hsm/pin ] && { echo "token already provisioned"; exit 0; } + +work=$(mktemp -d) +trap 'rm -rf "$work"' EXIT +mkdir -p /hsm/tokens /certs +module=/usr/lib/softhsm/libsofthsm2.so +pin=$(openssl rand -hex 6) +sopin=$(openssl rand -hex 6) + +softhsm2-util --init-token --free --label synapse --pin "$pin" --so-pin "$sopin" >/dev/null + +cert() { # name keyfile + openssl req -x509 -new -key "$2" -days 30 -subj "/CN=$1" \ + -addext "subjectAltName=DNS:$1" -out "/certs/$1.crt" +} + +# www: RSA key, in the HSM. +openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out "$work/www.pem" 2>/dev/null +cert www.example.com "$work/www.pem" +softhsm2-util --import "$work/www.pem" --token synapse --label www --id 01 --pin "$pin" >/dev/null +echo 'pkcs11:token=synapse;object=www;type=private' > /certs/www.example.com.key + +# api: EC P-256 key, in the HSM. +openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256 -out "$work/api.pem" 2>/dev/null +cert api.example.com "$work/api.pem" +softhsm2-util --import "$work/api.pem" --token synapse --label api --id 02 --pin "$pin" >/dev/null +echo 'pkcs11:token=synapse;object=api;type=private' > /certs/api.example.com.key + +# static: a software key, to show both kinds side by side. +openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256 -out /certs/static.example.com.key 2>/dev/null +cert static.example.com /certs/static.example.com.key +chmod 600 /certs/static.example.com.key + +# The PIN file: regular, non-empty, mode 0600, owned by root (Synapse runs as +# root in the image; otherwise the service user). +umask 077 +printf '%s\n' "$pin" > /hsm/pin +chmod 600 /hsm/pin + +echo "objects on the token:" +pkcs11-tool --module "$module" --token-label synapse --login --pin "$pin" --list-objects 2>/dev/null \ + | grep -E '^(Private Key Object|Public Key Object|Certificate Object)|label:' | head -20 diff --git a/use-cases/hsm-tls-keys/softhsm2.conf b/use-cases/hsm-tls-keys/softhsm2.conf new file mode 100644 index 0000000..a8d6f00 --- /dev/null +++ b/use-cases/hsm-tls-keys/softhsm2.conf @@ -0,0 +1,4 @@ +# SoftHSM's own settings, read by the module through SOFTHSM2_CONF. +directories.tokendir = /hsm/tokens +objectstore.backend = file +log.level = ERROR diff --git a/use-cases/hsm-tls-keys/upstreams.yaml b/use-cases/hsm-tls-keys/upstreams.yaml new file mode 100644 index 0000000..c551538 --- /dev/null +++ b/use-cases/hsm-tls-keys/upstreams.yaml @@ -0,0 +1,37 @@ +# Three hosts, three keys: an RSA key and an EC key in the HSM, and a software +# key. `cert:` is the certificate's NAME, the file stem in proxy.certificates +# (www.example.com for www.example.com.crt), never a path. There is no `key:`: +# the key is found beside the certificate, and whether it is in the HSM is +# decided by the first line of that file. +version: 2 + +hosts: + # RSA key in the HSM. + www.example.com: + tls: + terminate: + cert: www.example.com + paths: + "/": + upstream: whoami:80 + force_https: true + + # EC (P-256) key in the HSM. + api.example.com: + tls: + terminate: + cert: api.example.com + paths: + "/": + upstream: whoami:80 + force_https: true + + # Software key (PEM), served beside the HSM hosts. + static.example.com: + tls: + terminate: + cert: static.example.com + paths: + "/": + upstream: whoami:80 + force_https: true From 9b303c09aefb17ab317254426e4ab6395f91d045 Mon Sep 17 00:00:00 2001 From: David Papp Date: Mon, 9 Nov 2026 14:16:51 +0100 Subject: [PATCH 2/4] docs(hsm-tls-keys): target Synapse 0.8.9 --- README.md | 4 ++-- use-cases/README.md | 2 +- use-cases/hsm-tls-keys/Dockerfile | 2 +- use-cases/hsm-tls-keys/README.md | 7 ++----- use-cases/hsm-tls-keys/docker-compose.yml | 2 +- 5 files changed, 7 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 58e855a..df73c4a 100644 --- a/README.md +++ b/README.md @@ -84,8 +84,8 @@ is at [docs.gen0sec.com](https://docs.gen0sec.com). operator 0.2.0 (not released yet) and Synapse 0.8.8 or later. - [TLS keys in an HSM](./use-cases/hsm-tls-keys/): serve certificates whose private keys live in an HSM or other PKCS#11 token, beside a software key, - with the PIN in a file. The demo runs SoftHSM in docker. Needs Synapse 0.9.0 - (not released yet). + with the PIN in a file. The demo runs SoftHSM in docker. Needs Synapse 0.8.9 + or later. ### Being updated diff --git a/use-cases/README.md b/use-cases/README.md index 2536279..4516f34 100644 --- a/use-cases/README.md +++ b/use-cases/README.md @@ -26,7 +26,7 @@ closest to what you want and copy it. Every set uses placeholder values | [`tcp-mss`](./tcp-mss/) | proxy | no | none: the demo listens on port 8080 | Cap the MSS of the connections a listener accepts; needs Synapse 0.8.9 or later | | [`edge-headers`](./edge-headers/) | proxy | no | none: the demo listens on ports 8080 and 8443 | `X-Forwarded-For`, `X-Forwarded-Proto`, `Via`, a request ID and HSTS; needs Synapse 0.8.9 or later | | [`header-rules`](./header-rules/) | proxy | no | none: the demo listens on port 8080 | Add and remove request and response headers, with a script that checks every rule | -| [`hsm-tls-keys`](./hsm-tls-keys/) | proxy | no | none: the demo listens on ports 8085 and 8443 | TLS private keys in an HSM over PKCS#11 (RSA and EC), served beside a software key, with the PIN in a file; the demo uses SoftHSM in docker; needs Synapse 0.9.0 (not released yet) | +| [`hsm-tls-keys`](./hsm-tls-keys/) | proxy | no | none: the demo listens on ports 8085 and 8443 | TLS private keys in an HSM over PKCS#11 (RSA and EC), served beside a software key, with the PIN in a file; the demo uses SoftHSM in docker; needs Synapse 0.8.9 or later | | [`kubernetes-cert-manager`](./kubernetes-cert-manager/) | proxy | no | a privileged proxy pod, Kubernetes cluster | A `SynapseProxy` of the Synapse operator serving an Ingress with a certificate from cert-manager, and the proxy picking up a renewed certificate without a restart; needs Synapse operator 0.2.0 (not released yet) and Synapse 0.8.8 or later | "Privileges" lists what the set's own config needs; the installation guide diff --git a/use-cases/hsm-tls-keys/Dockerfile b/use-cases/hsm-tls-keys/Dockerfile index f23d21b..198e3f9 100644 --- a/use-cases/hsm-tls-keys/Dockerfile +++ b/use-cases/hsm-tls-keys/Dockerfile @@ -1,7 +1,7 @@ # Two images from one file: `token` provisions a SoftHSM token, `synapse` is the # Synapse image with the SoftHSM module added. A real HSM needs neither: you # install your vendor's PKCS#11 module instead. -ARG SYNAPSE_IMAGE=registry.gen0sec.com/gen0sec/synapse:0.9.0 +ARG SYNAPSE_IMAGE=registry.gen0sec.com/gen0sec/synapse:0.8.9 FROM debian:13 AS token RUN apt-get update \ diff --git a/use-cases/hsm-tls-keys/README.md b/use-cases/hsm-tls-keys/README.md index daa6cb2..7c82659 100644 --- a/use-cases/hsm-tls-keys/README.md +++ b/use-cases/hsm-tls-keys/README.md @@ -2,7 +2,7 @@ Keep the private keys of your TLS certificates in a hardware security module (HSM), or any other PKCS#11 token, and have Synapse sign handshakes with them. -The key never leaves the device. **Needs Synapse 0.9.0 (not released yet).** +The key never leaves the device. **Needs Synapse 0.8.9 or later.** Earlier releases cannot use a `pkcs11:` key. ```yaml @@ -160,10 +160,7 @@ docker compose down -v The set was run against SoftHSM only, with RSA and EC keys over TLS 1.2 and TLS 1.3, using a build of Synapse that has HSM keys. It was not run against a -hardware HSM, nor against a published image: no release has HSM keys yet. The -`make test` check (`synapse --check-config`) needs a release that has both -`--check-config` and `proxy.hsm`; until then it reports `proxy.hsm` as an -unused option. +hardware HSM, nor against a published image. ## Files diff --git a/use-cases/hsm-tls-keys/docker-compose.yml b/use-cases/hsm-tls-keys/docker-compose.yml index 4b0624d..64c332b 100644 --- a/use-cases/hsm-tls-keys/docker-compose.yml +++ b/use-cases/hsm-tls-keys/docker-compose.yml @@ -16,7 +16,7 @@ services: context: . target: synapse args: - SYNAPSE_IMAGE: ${SYNAPSE_IMAGE:-registry.gen0sec.com/gen0sec/synapse:0.9.0} + SYNAPSE_IMAGE: ${SYNAPSE_IMAGE:-registry.gen0sec.com/gen0sec/synapse:0.8.9} image: hsm-tls-keys-synapse:local command: ["-c", "/etc/synapse/config.yaml"] ports: ["8085:80", "8443:443"] From 78207b44941822c57133df076aaaa202420ec9c4 Mon Sep 17 00:00:00 2001 From: David Papp Date: Tue, 10 Nov 2026 19:30:44 +0100 Subject: [PATCH 3/4] test(check): give a set with an hsm block a test PIN file --- scripts/check.sh | 22 +++++++++++++++++++++- scripts/fixtures/hsm-pin/config.yaml | 10 ++++++++++ scripts/test-check.sh | 10 ++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 scripts/fixtures/hsm-pin/config.yaml diff --git a/scripts/check.sh b/scripts/check.sh index 46326f7..77c7d48 100755 --- a/scripts/check.sh +++ b/scripts/check.sh @@ -91,6 +91,17 @@ fi failed=$orphans skipped=0 + +# make_test_pin: make a 0600 test PIN file at "$pin_dir/pin", once per run; +# it is removed on exit. The binary checks only the file, never a token. +pin_dir= +make_test_pin() { + [ -n "$pin_dir" ] && return 0 + pin_dir=$(mktemp -d) || return 1 + trap 'rm -rf -- "$pin_dir"' EXIT + (umask 077 && printf '1234\n' >"$pin_dir/pin") +} + for dir in "${sets[@]}"; do args=(--check-config --config config.yaml) [ -f "$dir/upstreams.yaml" ] && args+=(--upstream-conf upstreams.yaml) @@ -106,7 +117,16 @@ for dir in "${sets[@]}"; do "$dir/make-certs.sh" >/dev/null 2>&1 || { echo "FAIL $dir (make-certs.sh failed)"; failed=$((failed + 1)); continue; } fi - out=$(cd "$dir" && "$bin" "${args[@]}" 2>&1) + # A set with an hsm block names a PIN file that exists only where it is + # deployed; it gets the test PIN instead. No other set sees a PIN env, so a + # caller's PROXY_HSM_PIN_FILE cannot change a result. + env_args=(-u PROXY_HSM_PIN_FILE -u AX_PROXY_HSM_PIN_FILE) + if grep -Eq '^[[:space:]]+hsm:' "$dir/config.yaml"; then + make_test_pin || { echo "FAIL $dir (cannot make a test PIN file)"; failed=$((failed + 1)); continue; } + env_args+=("PROXY_HSM_PIN_FILE=$pin_dir/pin") + fi + + out=$(cd "$dir" && env "${env_args[@]}" "$bin" "${args[@]}" 2>&1) rc=$? # Warnings that fail the check (all but the absent-rules-file notice). diff --git a/scripts/fixtures/hsm-pin/config.yaml b/scripts/fixtures/hsm-pin/config.yaml new file mode 100644 index 0000000..8f9b4dd --- /dev/null +++ b/scripts/fixtures/hsm-pin/config.yaml @@ -0,0 +1,10 @@ +# Control fixture: an HSM block whose PIN file does not exist here. check.sh +# MUST pass it by pointing PROXY_HSM_PIN_FILE at a test PIN file. +mode: agent +network: + iface: lo +proxy: + hsm: + modules: + - /usr/lib/softhsm/libsofthsm2.so + pin_file: /nonexistent/hsm/pin diff --git a/scripts/test-check.sh b/scripts/test-check.sh index a0826db..8d12cdd 100755 --- a/scripts/test-check.sh +++ b/scripts/test-check.sh @@ -9,6 +9,8 @@ # typo in upstreams.yaml / security_rules.yaml -> must fail # orphan upstreams.yaml (no config.yaml beside it) -> must fail # relative SYNAPSE_BIN -> must still work +# HSM set (hsm-pin) -> fails without a PIN file, passes through check.sh; +# a caller's PROXY_HSM_PIN_FILE does not reach a set without hsm # ML keys (ml-keys, ml-keys-typo, ml-inner-typo) -> depends on the build, # which this script detects: a default build skips ml-keys, fails # ml-keys-typo and fails ml-keys under SYNAPSE_REQUIRE_ML=1; a classifier @@ -81,6 +83,14 @@ expect fail "typo in upstreams.yaml fails" "upstreamz" "$check" "$fx/typo-upstre expect fail "typo in security_rules.yaml fails" "ips" "$check" "$fx/typo-rules" expect fail "orphan upstreams.yaml fails" "lonely" "$check" "$fx/orphan-root" +# An HSM set names a PIN file that exists only where it is deployed. check.sh +# hands it a test PIN through PROXY_HSM_PIN_FILE; without that it fails. +expect fail "HSM set fails without a test PIN" "proxy.hsm.pin_file" \ + bash -c 'cd "$1" && env -u PROXY_HSM_PIN_FILE -u AX_PROXY_HSM_PIN_FILE "$2" --check-config --config config.yaml' _ "$fx/hsm-pin" "$bin" +expect pass "HSM set passes with check.sh's test PIN" "ok " "$check" "$fx/hsm-pin" +expect pass "a caller's PIN env does not reach a set without hsm" "ok " \ + env PROXY_HSM_PIN_FILE=/nonexistent/pin "$check" "$fx/valid" + # A relative SYNAPSE_BIN must work: the check runs from each set's directory. abs_bin=$(command -v "${SYNAPSE_BIN:-synapse}" 2>/dev/null || true) if [ -n "$abs_bin" ]; then From 6bf835b2161f130a98fe015bc854a153640e6a50 Mon Sep 17 00:00:00 2001 From: David Papp Date: Tue, 10 Nov 2026 19:33:20 +0100 Subject: [PATCH 4/4] fix(hsm-tls-keys): stop a half-made token, note the demo PINs, inject a test PIN only for a named pin_file --- scripts/check.sh | 11 +++++++---- use-cases/hsm-tls-keys/README.md | 4 ++++ use-cases/hsm-tls-keys/make-token.sh | 6 ++++++ 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/scripts/check.sh b/scripts/check.sh index 77c7d48..b6fc602 100755 --- a/scripts/check.sh +++ b/scripts/check.sh @@ -117,11 +117,14 @@ for dir in "${sets[@]}"; do "$dir/make-certs.sh" >/dev/null 2>&1 || { echo "FAIL $dir (make-certs.sh failed)"; failed=$((failed + 1)); continue; } fi - # A set with an hsm block names a PIN file that exists only where it is - # deployed; it gets the test PIN instead. No other set sees a PIN env, so a - # caller's PROXY_HSM_PIN_FILE cannot change a result. + # A set whose hsm block names a pin_file names a path that exists only where + # it is deployed; it gets the test PIN instead. No other set sees a PIN env, + # so a caller's PROXY_HSM_PIN_FILE cannot change a result. Limit: the binary + # does not require pin_file, so a set that omits it passes here and fails + # only when a key loads; every set MUST name one. env_args=(-u PROXY_HSM_PIN_FILE -u AX_PROXY_HSM_PIN_FILE) - if grep -Eq '^[[:space:]]+hsm:' "$dir/config.yaml"; then + if grep -Eq '^[[:space:]]+hsm:' "$dir/config.yaml" && + grep -Eq '^[[:space:]]+pin_file:' "$dir/config.yaml"; then make_test_pin || { echo "FAIL $dir (cannot make a test PIN file)"; failed=$((failed + 1)); continue; } env_args+=("PROXY_HSM_PIN_FILE=$pin_dir/pin") fi diff --git a/use-cases/hsm-tls-keys/README.md b/use-cases/hsm-tls-keys/README.md index 7c82659..8ffc008 100644 --- a/use-cases/hsm-tls-keys/README.md +++ b/use-cases/hsm-tls-keys/README.md @@ -151,6 +151,10 @@ docker compose down -v certificate from your CA. - Keep the PIN file out of the configuration directory and out of version control, mode `0600`, and hand it to the service with `LoadCredential=`. +- **The demo passes the PINs on the command line.** `make-token.sh` gives + `softhsm2-util` and `pkcs11-tool` the PIN and SO PIN as arguments, which + other processes on the host can read. Set up a real token with the vendor's + tool, typing the PINs at its prompt. - Use your own host names for the `hosts:` keys, for `cert:` and for the certificate file names. - Set `provider_dir` if the `pkcs11.so` of the provider is not in a diff --git a/use-cases/hsm-tls-keys/make-token.sh b/use-cases/hsm-tls-keys/make-token.sh index 2691d1c..44aff85 100755 --- a/use-cases/hsm-tls-keys/make-token.sh +++ b/use-cases/hsm-tls-keys/make-token.sh @@ -15,6 +15,12 @@ set -eu [ -f /hsm/pin ] && { echo "token already provisioned"; exit 0; } +# The PIN file is written last, so a token without it is a run that stopped +# half way. Starting again would add a second token. +if [ -n "$(ls -A /hsm/tokens 2>/dev/null)" ]; then + echo "a previous run stopped half way; start again with: docker compose down -v" >&2 + exit 1 +fi work=$(mktemp -d) trap 'rm -rf "$work"' EXIT