# 01 · domi-pki: the Domi CA as a shared crate Phase T3a of the coherence roadmap (Fabrica `docs/32_coherence.md`, 2.3): *"The Domi CA code moved out of Ianitor core into a shared public crate, so Fabrica can issue agent certificates even without Ianitor. Root key stays in Custos."* ## Why Until now the Domi CA lived in `ianitor-core::pki`. Fabrica needs the same CA for Legatus, its agent: every agent gets a client certificate for mTLS, and Fabrica must issue and check those whether or not Ianitor is installed (roadmap decision 4: Ianitor is optional). Two copies of CA code would drift; one crate, pinned by tag, does not. ## The hierarchy ```text Domi PKI Root 10 years, pathLen 1 key in Custos, signs through `Signer` ├ Ianitor Domi Intermediate 1 year, pathLen 0 Ianitor control (key encrypted at rest) │ ├ 90 days, serverAuth tls = "domi" hosts │ └ 1 year, clientAuth mTLS devices └ Fabrica Domi Agents Intermediate 1 year, pathLen 0 the Fabrica server ├ 90 days, serverAuth the agents' endpoint └ 30 days, clientAuth one Legatus agent, URI urn:domi:agent:: ``` Every intermediate is signed from a CSR of which only the public key and the CN are used; the CN must start with the name of the intermediate being made (`IANITOR_INTERMEDIATE_CN` or `AGENTS_INTERMEDIATE_CN`). No leaf outlives its issuer. Keys are ECDSA P-256 (Ed25519 is accepted from a `Signer` for the root, not for leaves). ### Agent certificates (new) - The agent makes its own key and sends a CSR; the key never leaves the agent. `issue_agent` uses only the CSR's public key (ECDSA P-256 only) and checks the CSR's own signature (proof the agent holds the key). - Subject: CN = agent id, O = Fabrica Domi, OU = Domi agents. SAN: exactly one URI `urn:domi:agent::`. Ids are 1–40 of `a-z 0-9 -`, not starting or ending with `-`. - clientAuth only, digitalSignature only. 30 days; Legatus renews when 10 are left (`AGENT_RENEW_DAYS`), over its existing mTLS connection. A short life bounds how long a stolen agent key is useful even before revocation. ### Why agents get their own intermediate Ianitor device certificates are also clientAuth certificates under the same root. If agents shared Ianitor's intermediate, a phone's device certificate would chain to the root for client auth just as an agent's does. `verify_agent` therefore requires the chain to go through an intermediate named `AGENTS_INTERMEDIATE_CN`, and the leaf to carry the agent URI. A device certificate, a server certificate, an agent certificate of another root, or one whose chain skips the agents intermediate is *foreign* and refused. ## The signer interface (the root stays in Custos) ```rust pub trait Signer { /// SubjectPublicKeyInfo DER of the signing key (ECDSA P-256 or Ed25519). fn spki_der(&self) -> Vec; /// Sign `msg`: ECDSA P-256 with SHA-256 (ASN.1 DER signature) or Ed25519. fn sign(&self, msg: &[u8]) -> Result, SignerError>; } ``` `create_root(&dyn Signer, now)` builds the root's self-signed certificate and `sign_intermediate(root_pem, &dyn Signer, csr, prefix, now)` signs an intermediate: in both, the crate builds the to-be-signed bytes and asks the signer once. Custos implements `Signer` behind a match-code approval (its signing service, T1); the crate never sees the root's private key. `KeyPair` (a local P-256 key) also implements `Signer`: it is what intermediates use, and what tests and the offline machine use for a root (`create_root_local`). The signer is synchronous: an adapter that waits for an approval blocks the calling thread. That is fine for the rare root operations (a root once, an intermediate a year); agents are issued by the intermediate, not the root. ## Checking certificates - `verify_chain(chain_der, root_der, now, Usage)`: rustls-webpki path building and signature checks plus validity at `now`, for server or client use. Errors are typed: `Expired`, `NotYetValid`, `UnknownIssuer`, `BadSignature`, `WrongUsage`, `NotCa`, `BadCertificate`. - `verify_agent(chain_der, root_der, now)`: `verify_chain` for client use, then the Domi rules above; returns the `AgentIdentity` (team, agent, serial, fingerprint, notAfter). What it does *not* know: whether the agent is still registered and whether this serial is its current one. That registry is the caller's (Legatus protocol, Fabrica). - `inspect`, `issued_by`, `intermediate_problem(cert, root, prefix, now)`: what Ianitor already used. ## Moved from Ianitor, unchanged Profiles, lifetimes, the CSR rules, `match_code`, `INTERMEDIATE_OPENSSL_EXT` and `openssl_sign_command`, the placeholder leaf for Caddy's allow list, the test helpers. Ianitor keeps its encrypted-key format (`IANITOR ENCRYPTED PRIVATE KEY`): that is Ianitor's storage, not the CA. `ianitor-core::pki` becomes a thin layer over this crate with the same public API and the same error messages, so Ianitor's behaviour and tests do not change. ## Tests (first) - chains: server and device and agent chains validate to the root; a leaf cannot sign; pathLen 0 holds; - expiry: expired and not-yet-valid leaves and intermediates are refused; an expired issuer issues nothing; - wrong issuer: a leaf of another root with identical names, a chain missing its intermediate, a self-signed leaf; - agents: issue from a CSR, the CSR's wishes ignored, a tampered CSR refused, a non-P-256 key refused, a device or server certificate refused as an agent, another root's agent refused, bad ids refused; - the signer: a root made through a counting signer is used exactly once per operation and never exposes a key; a signer whose key does not match the root certificate is refused; - hostile input: garbage PEM/DER never panics. ## Risks and gaps - No CRL or OCSP: revocation of agents is the registry's job (Fabrica keeps the current serial per agent), of devices Ianitor's allow list. Short agent lifetimes bound the gap. - `Signer` is synchronous (see above). - The agents intermediate's key is held by the Fabrica server; how it is stored at rest is Fabrica's (T3b).