gunk-dev/the-valley

README.md

source16d422ffa0femain's published tip history
22.3 KiB · Rendered
1# sigverify
2
3sigverify verifies OpenSSH signatures and refuses any FIDO security-key signature made without a
4touch. It reads standard SSHSIG, the format `ssh-keygen -Y sign` writes and git uses for SSH-signed
5tags. It is a Go library (`the-valley/sigverify`) and a command (`sigverify`).
6
7sigverify judges one signature at a time and keeps no state. What a host must do around it to gate
8releases safely is in [The caller's contract](#the-callers-contract).
9
10## Why the verifier must check for a touch
11
12The operator's approvals and host releases are meant to be authorized by a touch on a hardware key.
13Agents and the interactive coordinator run as the operator's Unix user. They can do anything the
14operator's files allow, so the touch is the one step they cannot perform.
15
16A security key signs authenticator data, not the message itself. That data holds the message's hash
17and a flags byte. The user-presence bit (UP, `0x01`) is set only when someone touched the key. The
18user-verification bit (UV, `0x04`) is set only when the key also checked a PIN or biometric. Both
19bits are inside the signed bytes, so nobody can change them after signing.
20
21Whether the key asks for a touch is decided by a flag in the key stub, the private key file on disk.
22Anything running as the operator can clear that flag, either with
23`ssh-keygen -p -O no-touch-required` (OpenSSH 10.5) or by editing the stub's bytes. The plugged-in
24key then signs without a touch, and the signature records UP as clear. Clearing the stub's
25verify-required flag the same way drops the PIN, and the signature records UV as clear.
26
27Standard verification never looks at the bits:
28
29- `ssh-keygen -Y verify` logs the flags at debug level and accepts the signature.
30- `git verify-tag` with `gpg.format=ssh` calls `ssh-keygen -Y verify`, so it accepts it too.
31- An allowed-signers file cannot ask for a touch. Its only options are `cert-authority`,
32 `namespaces`, `valid-after` and `valid-before`.
33
34So the presence check has to be in the verifier. sigverify does it on every security-key signature,
35and nothing turns it off.
36
37## Signer classes
38
39Every line of the allowed-signers file names a signer of one class:
40
41- **`fido-sk`**: a FIDO security key, `sk-ssh-ed25519@openssh.com` or
42 `sk-ecdsa-sha2-nistp256@openssh.com`. Its signatures carry UP and UV, and UP is always required.
43- **`tkey-signer`**: a Tillitis TKey, on a line marked `tkey-signer`. The line's key must be
44 `ssh-ed25519`.
45- **`software`**: any other key: `ssh-ed25519`, ECDSA, or RSA of at least 2048 bits. Its signatures
46 prove nothing about presence.
47
48A TKey signature is plain `ssh-ed25519` and carries no flags. It can still be trusted to need a
49touch, for two reasons. The stock `tkey-device-signer` app requires a touch for every signature. And
50the TKey derives its ed25519 key from the hash of the app it runs, so an app built to skip the touch
51has a different key. The `tkey-signer` line pins the key, and so pins the app. The marker may carry
52the app's version for bookkeeping, as `tkey-signer="1.0.0"`. sigverify reports the version and never
53compares it with anything. Upgrading the app changes the key, so it is a planned trust-root
54rotation.
55
56A class marker is an enrollment assertion: sigverify believes it and cannot check it. A
57`tkey-signer` line holding a software key makes that software key pass a TKey-only call. A `fido-sk`
58line holding a key made in software makes it pass as a security key, with whatever flags its maker
59chose. Nothing in a signature's bytes shows any of these:
60
61- that the key lives in genuine hardware
62- which app a TKey runs, or that it is the version the line records
63- that a TKey was touched for this signature, or that its user was verified
64- that a touch was an informed approval of what was signed
65
66So every line must come from a trusted enrollment of genuine hardware. Requirement 3 of
67[the caller's contract](#the-callers-contract) says what that means.
68
69Each call names the classes it accepts. By default only `fido-sk` signers verify. A call accepts
70other classes only when it asks for them, with `--allow-class`. Giving `--allow-class` replaces the
71default. So:
72
73- A release or an approval uses the default. A TKey listed in the same file cannot sign it.
74- A trust-root change passes `--allow-class tkey-signer`. A security key cannot sign it.
75- `--allow-non-sk` adds `software`. It never makes an unmarked key count as a TKey: only a
76 `tkey-signer` line does that.
77
78A TKey cannot prove user verification, so `--require-uv` refuses TKey signatures.
79
80## What is verified
81
82A signature is accepted only if every one of these holds, checked in this order:
83
841. It is a well-formed SSHSIG, armored or raw, with hash `sha256` or `sha512`.
852. Its namespace is the one the caller names. The git-tag command always uses `git`.
863. The signature verifies over the message.
874. A line of the allowed-signers file lists the key, and all of these hold for that line:
88 - Its class is one the call accepts.
89 - Its principals match the principal the caller names. A principal is required. `--any-principal`
90 skips this check, and then even a line whose principals are all negated, such as `!*`,
91 authorizes its key.
92 - Its `namespaces`, if present, match the namespace.
93 - The verify time falls between its `valid-after` and `valid-before`.
94
95 Principal and namespace patterns use OpenSSH's own matching rules, and the tests compare them
96 with `ssh-keygen`.
97
985. A `fido-sk` signature has UP set.
996. The signature has UV set, if the caller passes `--require-uv` or any matching line carries
100 `verify-required`. Only a `fido-sk` signature can.
101
102A success reports the principal, the signer class, the key type, the key's SHA256 fingerprint and,
103for a security key, the authenticator flags and the signature counter.
104
105sigverify trusts the allowed-signers file completely. Anyone can make a software key in the
106security-key format, and its signatures would carry whatever flags its maker chose. sigverify does
107not check the enrollment attestation that would tell the two apart. So whoever can write the file
108can sign anything, and whoever enrolls a key decides what it counts as. Requirements 1 and 3 of the
109caller's contract are about this. sigverify also does not track the signature counter, so it does
110not detect a replayed signature or a cloned key.
111
112## What is refused
113
114Each refusal has a stable reason that callers can match on.
115
116| Reason | Meaning |
117| ----------------------- | --------------------------------------------------------------------- |
118| `malformed` | The signature, or the tag object holding it, cannot be read. |
119| `unsigned` | The git tag has no signature, or is a lightweight tag. |
120| `wrong-namespace` | The signature was made for another namespace. |
121| `unsupported-key` | The key type is not one listed above, such as a certificate. |
122| `bad-signature` | The signature does not verify over this message. |
123| `unknown-key` | No allowed-signers line lists the key. |
124| `class-not-allowed` | The key is listed, but only in a class the call does not accept. |
125| `principal-not-allowed` | The key is listed, but not for the requested principal. |
126| `namespace-not-allowed` | The key's line restricts it to other namespaces. |
127| `not-yet-valid` | The verify time is before the key's `valid-after`. |
128| `expired` | The verify time is after the key's `valid-before`. |
129| `no-user-presence` | A genuine security-key signature made without a touch. |
130| `no-user-verification` | UV is required and the signature does not prove it. |
131| `tag-name-mismatch` | The tag object was signed under another name than the one it has now. |
132| `target-not-commit` | The signed tag points at a tag, tree or blob, not a commit. |
133| `target-missing` | The commit the signed tag points at is not in the repository. |
134
135A `no-user-presence` refusal means the key really did sign, without a touch. Something running as
136the key's owner is using the key, and the refusal names the key's fingerprint on stderr for that
137reason.
138
139## Encodings
140
141sigverify accepts one encoding for each signature, and refuses the rest:
142
143- Armored base64 must be canonical: no stray padding bits, and nothing but line breaks inside the
144 armor.
145- Nothing but whitespace may follow the armor footer.
146- Integers must be in their minimal encoding, without redundant leading zero bytes.
147- An RSA signature must be exactly as long as the key's modulus.
148
149One field is accepted in any form. The SSHSIG envelope has a reserved field, which `ssh-keygen`
150reads and ignores, and so does sigverify. The bytes a key signs always hold an empty reserved field,
151so whatever the envelope carries there cannot change what was signed.
152
153These rules leave signature bytes malleable in ways no verifier can prevent. An ECDSA signature has
154two valid forms, and the reserved field can hold anything. None of this changes what was signed.
155
156## Differences from ssh-keygen
157
158sigverify is stricter than `ssh-keygen -Y verify` in these ways:
159
160- It enforces UP, and UV when required.
161- It requires a principal unless told otherwise.
162- It refuses an allowed-signers file with any line it cannot parse, including unknown options.
163 ssh-keygen skips such a line, so a typo could silently drop a signer's restrictions.
164- It refuses an allowed-signers file with a `no-touch-required` option on any line.
165- It refuses an allowed-signers file that holds a NUL byte anywhere. ssh-keygen reads each line as a
166 C string, which ends at the first NUL, so it rejects a line like `*,!release<NUL>x K` as having no
167 key. Read byte by byte, the same line would admit `release`.
168- It adds two allowed-signers options, `verify-required` and `tkey-signer`. `verify-required` is the
169 keyword sshd uses in `authorized_keys`. ssh-keygen does not know either one, and skips any line
170 carrying them.
171- It does not verify certificates. A `cert-authority` line never matches, so a certificate signature
172 is refused.
173- It refuses RSA keys under 2048 bits, and the encodings described above.
174
175The git-tag command also differs from `git verify-tag`:
176
177- It judges the validity window at the current time, or at `--verify-time`. git uses the tagger
178 date, which the signer chooses, so a key past its `valid-before` could sign a backdated tag.
179- It refuses a tag whose signed name differs from the ref it was found under. Without this check, a
180 signed tag for an old release could be published again under a new release's name.
181- It requires the tag to point at a commit, and that commit to be in the repository.
182- It looks the tag up only as `refs/tags/NAME`. A name git would resolve elsewhere, such as a
183 remote-tracking ref, is not a tag.
184- It runs git with replacement refs disabled. A replacement ref makes git read one object in place
185 of another, so it could otherwise substitute a different tag object or commit.
186- It runs git in an environment of its own, inheriting nothing from the caller. Variables such as
187 `GIT_DIR`, `GIT_OBJECT_DIRECTORY` or `GIT_CONFIG_PARAMETERS` could otherwise send git to other
188 objects. User and system git configuration are not read.
189- The Nix package compiles the absolute path of its git into the binary, so no `PATH` decides which
190 git runs.
191
192git refuses a repository owned by a user other than the one running it, and sigverify does not
193override that.
194
195## Using the command
196
197```
198sigverify verify --namespace NS --signature FILE --allowed-signers FILE \
199 (--principal ID | --any-principal) [--allow-class CLASS]... [--allow-non-sk] \
200 [--require-uv] [--verify-time TIME] < message
201
202sigverify git-tag --repo DIR --tag NAME --allowed-signers FILE \
203 (--principal ID | --any-principal) [--allow-class CLASS]... [--allow-non-sk] \
204 [--require-uv] [--verify-time TIME]
205```
206
207The exit status is 0 for verified, 1 for refused, and 2 for an error. An error means nothing was
208judged: bad usage, an unreadable or invalid allowed-signers file, a missing tag, a git failure, or a
209report that could not be written to stdout.
210
211The output depends on the exit status:
212
213- **0:** stdout is `verified`, followed by lines of the form `name value`. The whole report goes out
214 in one write. If that write fails, the exit status is 2, never 0.
215- **1:** stdout is exactly one line, `refused <reason>`. stderr repeats the reason and explains the
216 refusal in a sentence, as `sigverify: refused (<reason>): <sentence>`. Nothing about a refused
217 tag's target appears anywhere, because none of it is authenticated.
218- **2:** stderr says what went wrong. sigverify writes nothing to stdout on an error, with one
219 exception: a report write that fails partway leaves the part already written on stdout.
220
221So the rule for callers is plain: **discard stdout whenever the exit status is not 0.** Everything
222worth logging about a refusal or an error is on stderr.
223
224A verified release tag looks like this:
225
226```
227$ sigverify git-tag --repo /srv/git/app.git --tag v2026.10.04 \
228 --allowed-signers /etc/app/release-signers --principal release@example.org --require-uv
229verified
230namespace git
231principal release@example.org
232class fido-sk
233key-type sk-ssh-ed25519@openssh.com
234fingerprint SHA256:...
235flags 0x05
236user-presence yes
237user-verification yes
238counter 42
239tag v2026.10.04
240commit 5d1c...
241```
242
243The report's lines, and their order, are fixed:
244
2451. `verified`
2462. `namespace`, `principal`, `class`, `key-type` and `fingerprint`
2473. for a `fido-sk` signer: `flags`, `user-presence`, `user-verification` and `counter`
2484. for a `tkey-signer` line that records a version: `signer-app`
2495. for git-tag only: `tag`, then `commit`, which is always the last line
250
251Each name appears at most once. A git-tag report always holds exactly one `tag` line and one
252`commit` line. Both come from the signed tag object, read after its signature verified.
253
254The flake exposes the command as `packages.<system>.sigverify`, for `x86_64-linux` and
255`aarch64-linux`. Its checks are defined for both systems.
256
257## The caller's contract
258
259sigverify answers one question: does this signature verify against this file, under this policy?
260These requirements are for the host-side caller that turns that answer into an activated release,
261such as a host's deploy step. Each one closes a hole that sigverify cannot close by itself. The
262caller must meet all of them before a host activates anything on sigverify's word.
263
2641. **Keep every trust root out of the operator account's reach.** The operator account must not be
265 able to write any of these:
266 - the allowed-signers file
267 - every parent directory of that file, and the target of any symlink on its path
268 - the sigverify binary
269 - the invocation policy: the flags the caller passes
270 - the activation service
271 - the rollback state of requirement 8
272
273 Each must be owned by root and not writable by the operator, or be in the Nix store of the
274 running system. Never take any of them from the candidate release being verified. Whoever can
275 write the allowed-signers file can list a software key in security-key format and sign any
276 release with UP and UV set.
277
2782. **Protect the repository and the build until activation.** The operator account must not be able
279 to write any of these, from fetch through activation:
280 - the repository's object storage, including any alternates it borrows objects from
281 - the repository's local configuration and hooks
282 - the checkout of the verified commit
283 - every input of the build
284
285 sigverify reads the objects as they are when it runs. Whoever can change the object storage, the
286 checkout or the build inputs afterwards can change what gets built under the verified commit id.
287 Disabling replacement refs does not prevent that.
288
2893. **Enroll every signer through a trusted ceremony.** Every allowed-signers line is an enrollment
290 assertion, and sigverify cannot check it (see [Signer classes](#signer-classes)). So each line
291 must record a key that was enrolled from genuine hardware:
292 - a `fido-sk` line, from a security key in hand, with the public key taken from that key at
293 enrollment
294 - a `tkey-signer` line, from a TKey in hand running the intended touch-requiring signer app, with
295 the public key taken from that TKey under that app
296
297 File ownership does not establish any of this. A root-owned file can still list a key that never
298 lived in hardware.
299
3004. **Accept exactly the classes and options a release needs.** `--allow-class`, `--allow-non-sk` and
301 `--any-principal` each make sigverify accept more:
302 - `--allow-class` replaces the default class, and repeating it adds more classes.
303 - `--allow-non-sk` adds software keys, which prove nothing about presence.
304 - `--any-principal` ignores the principals field.
305
306 A release caller passes exactly what it intends. For a release, that is:
307 - `--principal` with the release principal
308 - the default class, `fido-sk`, so no `--allow-class` other than `--allow-class fido-sk`
309 - `--require-uv`, or a `verify-required` mark on every release signer in the protected file
310 - never `--allow-non-sk` or `--any-principal`
311
3125. **Use a clock the operator account cannot set.** sigverify judges `valid-after` and
313 `valid-before` at the host's current time, or at `--verify-time`. Whoever sets that time can
314 bring an expired signer back, or bring a future one forward. So the host's clock must be one the
315 operator account cannot change, or the caller passes `--verify-time` from a source it trusts.
316 Never pass the tagger date: the signer chooses it.
317
3186. **Require the complete success report.**
319 - Act only on exit status 0. Discard stdout whenever the exit status is not 0.
320 - Require the whole git-tag report: the first line is `verified`, there is exactly one `tag` line
321 and exactly one `commit` line, and `commit` is the last line (see
322 [Using the command](#using-the-command)).
323 - Require the `tag` line to name the tag the caller asked for, and the `commit` line to hold a
324 full hexadecimal commit id.
325 - Treat any other output as a failure.
326
3277. **Apply exactly what was verified.**
328 - Use only the commit id on the `commit` line. Never resolve the tag name again, because a ref
329 can move after verification.
330 - Keep replacement refs disabled through fetch, checkout and build, with
331 `GIT_NO_REPLACE_OBJECTS=1` or `git --no-replace-objects`. A replacement ref makes git produce a
332 different commit's contents under the verified commit id.
333 - Run git with a sanitized environment throughout: no inherited `GIT_*` variables, and no user or
334 system configuration.
335
3368. **Refuse rollback.**
337 - **Parse release names one way.** Derive the release stream and sequence number from the
338 verified tag name, and from nothing else. Use one strict grammar, with exactly one spelling for
339 each stream and number: for example, a decimal number with no sign, no spaces and no leading
340 zeros. Refuse a tag name that does not parse. Two different tag names must never give the same
341 stream and number.
342 - **Start from a trusted floor.** A host that has never accepted a release takes its starting
343 floor from protected provisioning, such as the Nix store of its running system. It never takes
344 it from the first release it sees.
345 - **Compare under a lock.** Under an exclusive activation lock, compare the candidate with the
346 last accepted release in durable, protected state. Reject a lower sequence. Reject an equal
347 sequence that names a different commit. Accept an equal sequence with the same commit only as
348 an explicit retry of that release.
349 - **Record before activating.** Write the accepted sequence and commit to that state crash-safely
350 before activating. Hold the lock until activation finishes, so an older activation cannot
351 complete after a newer one.
352 - **Fail closed on missing or corrupt state.** If the rollback state is missing, unreadable or
353 inconsistent, activate nothing. Never rebuild it from the candidate release or from the
354 repository. Recovery is a documented procedure in which the operator, present at the host,
355 restores the floor from a trusted record.
356 - **Roll back forward.** An authorized rollback is a newly signed release with a higher sequence
357 that points at the older commit.
358
3599. **Never identify a release by its signature bytes.** Do not use the raw signature, or the bytes
360 of the tag object, to identify a release or to detect a replay. The same signed content can be
361 encoded more than one way (see [Encodings](#encodings)). Identify a release by its verified tag
362 name and commit id.
363
364## Tests
365
366`nix flake check` runs two checks. `sigverify-unit` runs the Go tests. `sigverify-e2e` runs the
367packaged command over a real signed tag, with an empty environment and no git on `PATH`. Both are
368defined for `x86_64-linux` and `aarch64-linux`. `nix flake check` builds the checks for the
369machine's own system.
370`nix build .#checks.aarch64-linux.sigverify-unit .#checks.aarch64-linux.sigverify-e2e` runs the ARM
371ones on an aarch64 builder, or on an x86 machine with aarch64 emulation.
372
373The tests need no hardware. Security-key signatures come from two sources:
374
375- `internal/skforge` derives keys from seed strings and makes signatures with chosen flags. It
376 rebuilds the signed bytes independently of the verifier, and it can take a signature apart and
377 alter one field.
378- OpenSSH's `sk-dummy` is the software authenticator from OpenSSH's own regression suite. The checks
379 build it from the same source as the `ssh-keygen` they run. With it, real `ssh-keygen` makes
380 touchless signatures for ed25519-sk and ecdsa-sk keys: from keys generated with
381 `no-touch-required`, and from stubs whose flags are cleared after the keys were made. A cleared
382 verify-required flag gives a signature without UV.
383
384Some tests record that `ssh-keygen -Y verify` or `git verify-tag` accepts a signature sigverify
385refuses. Those expectations are in subtests named `interop: ...`. If a future OpenSSH or git starts
386refusing silent signatures, only those subtests fail. That failure is a change upstream, not a
387sigverify regression.
388
389The tests compare sigverify's verdicts with `ssh-keygen -Y verify` across namespaces and validity
390windows, and with `ssh-keygen -Y match-principals` across principal patterns and a line holding a
391NUL byte. Outside nix, `go test ./...` skips the `sk-dummy` cases unless `SIGVERIFY_SK_PROVIDER`
392names the provider.