Files
ianitor/docs/pki.md
T

113 lines
6.6 KiB
Markdown

# The Domi CA
ROADMAP §1.1, §1.2, §1.8, §3. Code: `crates/core/src/pki.rs` (the CA, pure),
`crates/control/src/pki.rs` (state, requests, devices), `crates/edge/src/tls.rs` (device
certificates in the handshake).
```text
Domi PKI Root ~10 years, pathLen 1 offline; never on the server
└ Ianitor Domi Intermediate 1 year, pathLen 0 on the server, key encrypted at rest
├ <hostname> 90 days, serverAuth tls = "domi" hosts, renewed by control
└ <device name> 1 year, clientAuth mTLS for hosts with device_certs = true
```
All keys are ECDSA P-256.
## Lifetimes, and why
| Certificate | Lifetime | Why |
|:--|:--|:--|
| Root | 3653 days | ROADMAP §3 ("~10 years"). It signs once a year, offline. |
| Intermediate | 365 days | ROADMAP §3 ("~1 year"); renewed through the banner, signed by the root. |
| Server | 90 days, renewed at 30 days left | Control renews them by itself (hourly check), so a short life costs nothing and bounds a key read from the certificate directory; 30 days of slack survive a long control outage. |
| Device | 365 days, "renew soon" at 30 days | Installing is a deliberate manual step (§4 accepts its 4th click), so not more than once a year; a lost device is revoked (allow list) rather than waited out. Mark, 2026-11-07 (Q7). |
No certificate is ever issued past its issuer's `notAfter`.
## What is where on the server
`/var/lib/ianitor/control/pki/` (mode 0700, owner ianitor-control):
| File | What |
|:--|:--|
| `intermediate.crt`, `previous.crt` | the intermediate, and the one before while it is valid |
| `intermediate.key` | its key, `IANITOR ENCRYPTED PRIVATE KEY` (PBKDF2-HMAC-SHA256 600,000 rounds + AES-256-GCM), password from the `pki-password` credential |
| `pending-<id>.key` | the new key of an open renewal, encrypted the same way |
| `devices.json`, `devices/<id>.key` | issued device certificates; their keys, encrypted, kept to make an install file again until revoked |
| `requests.json`, `approvals/` | approval requests, and the manual approvals the CLI writes |
`/var/lib/ianitor/certs/`: `<host>.crt` (leaf + intermediate) and `<host>.key` (0640, group
ianitor), `domi-devices-ca.pem` (the intermediates that issue device certificates) and
`domi-devices.pem` (the allow list: every issued, unrevoked, unexpired device certificate). Edge
and Caddy read these files; a presented device certificate must chain to the bundle **and** be
on the list, or the handshake fails.
The root key is never here. Control refuses to start when a file named like a root key is in
its state directory (tested: `control_refuses_to_start_with_a_root_key_in_its_state_dir`).
## Until Custos can sign: the manual offline step
Custos has no CSR-signing operation yet (ROADMAP §8.6, [custos-ticket.md](custos-ticket.md)).
Mark accepted this as a §4 exception (2026-11-07, Q6): **the offline machine runs openssl.**
### Once: the root, on the offline machine
```sh
openssl req -x509 -new -newkey ec -pkeyopt ec_paramgen_curve:P-256 \
-keyout domi-root.key -out domi-root.crt -days 3653 \
-subj "/CN=Domi PKI Root/O=Fabrica Domi" \
-addext "basicConstraints=critical,CA:TRUE,pathlen:1" \
-addext "keyUsage=critical,keyCertSign,cRLSign"
# asks for a passphrase: domi-root.key is stored encrypted
openssl x509 -in domi-root.crt -noout -fingerprint -sha256
```
(`ianitor-control ca init --out <dir>` makes the same root with Ianitor's own encrypted key
format, on a machine that has Ianitor.) Copy **only `domi-root.crt`** to the server's
configuration (`services.ianitor.pki.rootCertificate`). `domi-root.key` and its passphrase stay
offline (and go into Custos once it can hold them).
### Each year (and the first time): the intermediate
1. In the UI: the banner's **Renew** (or **Set up**) makes a new key on the server and a CSR, and
shows a match code. **Sign it by hand** downloads `domi-intermediate-<date>.csr` and
`domi-intermediate-<date>.ext` (the signing profile: CA, pathLen 0, keyCertSign + cRLSign). By
API: `POST /certs/intermediate/renewal`, `GET /pki-requests/{id}/csr`, `GET /pki-requests/{id}/ext`.
2. On the offline machine, check the request, then sign:
```sh
openssl req -in domi-intermediate-<date>.csr -noout -subject # CN=Ianitor Domi Intermediate <yyyy-mm>
openssl x509 -req -in domi-intermediate-<date>.csr -CA domi-root.crt -CAkey domi-root.key \
-days 365 -sha256 -extfile domi-intermediate-<date>.ext -out domi-intermediate-<date>.crt
```
(`ianitor-control ca sign --root <dir> --csr … --out …` does the same and asks for the match
code before it signs.)
3. Back on the server: upload the `.crt` in the UI (step 3), or
`sudo -u ianitor-control ianitor-control pki install domi-intermediate-<date>.crt --config /etc/ianitor/control.toml`.
Control checks that the certificate is signed by the configured root, is a CA with path length 0,
is valid now, and carries the pending key; then it swaps the intermediate, reissues every
`tls = "domi"` certificate and reloads the engine that serves. Tested with real openssl
(`an_intermediate_signed_with_openssl_is_installed`).
## Device certificates
ROADMAP §4: Certificates → + Device → Issue (3 clicks), then install (the accepted 4th).
1. `POST /certs/devices {name, kind}` opens a request with a six-digit match code, for 10 minutes.
2. Until Custos can approve (Mark, Q5): approve it on the server, comparing the code with the UI:
`sudo -u ianitor-control ianitor-control pki approve 47 19 83 --config /etc/ianitor/control.toml`
(`pki deny <code>` turns it down; `pki status` lists open requests). Control issues the
certificate within two seconds and puts it on the allow list; edge re-reads the list at once.
3. Install: `POST /certs/devices/{id}/bundle {format: "qr"|"p12"}` gives a one-time link (10
minutes, one download) to a PKCS#12 file (AES-256, SHA-256 MAC) and a one-time password. When
`[ui] hostname` is set the link goes through edge on the admin host, as a share link when that
host has share links on, so a device without a sign-in can fetch it; the token is the secret.
4. Revoke: `DELETE /certs/devices/{id}`. It is off the allow list at once (edge:
`/v1/device-certs/reload`; Caddy: control restarts caddy-fallback, which reads the list at start).
A host asks for device certificates with `device_certs = true` in its file. Only such hosts send a
certificate request in the TLS handshake (other hosts never prompt a browser). Without a
certificate, or with one not on the list, the handshake fails on both engines; a request whose
`Host` is another name than the connection's SNI is answered 421 once any host needs device
certificates (as Caddy does).