gunk-dev/the-valley

sigverify.go

sourceba94f05e889bmain's published tip history
12.2 KiB
1// Package sigverify verifies OpenSSH signatures (SSHSIG) and enforces what
2// standard OpenSSH verification does not: that a FIDO security key was
3// touched when it signed.
4//
5// A security key does not sign a message directly. It signs authenticator
6// data that holds the message's hash and a flags byte. The flags byte has a
7// user-presence bit (UP, 0x01), set only when someone touched the key, and
8// a user-verification bit (UV, 0x04), set only when the key also checked a
9// PIN or biometric. Both bits are inside the signed bytes, so a verifier
10// that reads them cannot be fooled about them.
11//
12// `ssh-keygen -Y verify`, and so `git verify-tag` with gpg.format=ssh,
13// never reads them. And a process running as the key's owner can ask the
14// key to sign without a touch: it only has to clear one flag in the key
15// stub on disk. So a check that a person was present has to happen here,
16// in the verifier.
17//
18// Every allowed signer has a class, and each call names the classes it
19// accepts (see Class). Verify checks, in order:
20//
21// 1. The signature is a well-formed SSHSIG.
22// 2. Its namespace is the one the caller expects.
23// 3. The signature verifies over the message.
24// 4. An allowed-signers line lists the key, in a class the call accepts,
25// for the requested principal, in this namespace, at the verify time.
26// 5. A security key's signature has UP set, always.
27// 6. It has UV set, when the policy or the signer's line requires it. A
28// TKey or software signature cannot prove UV, so then it is refused.
29//
30// The first check that fails is the refusal, with a Reason that names it.
31package sigverify
32
33import (
34 "errors"
35 "fmt"
36 "slices"
37 "strings"
38 "time"
39)
40
41// The authenticator-data flag bits, from the WebAuthn specification.
42const (
43 FlagUserPresent = 0x01
44 FlagUserVerified = 0x04
45)
46
47// Class is the kind of signer an allowed-signers line names. It decides
48// what a signature by that line's key can prove.
49type Class string
50
51const (
52 // ClassFIDO is a FIDO security key: sk-ssh-ed25519@openssh.com or
53 // sk-ecdsa-sha2-nistp256@openssh.com. Its signatures carry the UP and UV
54 // bits, and UP is always required.
55 ClassFIDO Class = "fido-sk"
56 // ClassTKey is a Tillitis TKey running the stock signer app, on a line
57 // marked tkey-signer. Its signatures are plain ssh-ed25519 and carry no
58 // flags. They can still be trusted to need a touch: the app requires
59 // one for every signature, and the TKey derives the key from the hash
60 // of the app it runs, so an app that skips the touch has a different
61 // key. The line pins the key, and with it the app.
62 ClassTKey Class = "tkey-signer"
63 // ClassSoftware is any other key. Its signatures prove nothing about
64 // presence.
65 ClassSoftware Class = "software"
66)
67
68// Reason names why a signature was refused. The values are stable, and
69// callers may match on them.
70type Reason string
71
72const (
73 // Malformed: the signature, or the git tag carrying it, cannot be read.
74 Malformed Reason = "malformed"
75 // Unsigned: a git tag carries no signature at all.
76 Unsigned Reason = "unsigned"
77 // WrongNamespace: the signature was made for a different namespace.
78 WrongNamespace Reason = "wrong-namespace"
79 // UnsupportedKey: the signing key is of a type this package does not
80 // verify, such as a certificate.
81 UnsupportedKey Reason = "unsupported-key"
82 // BadSignature: the signature does not verify over the message. Either
83 // the message changed, or the signature is not that key's.
84 BadSignature Reason = "bad-signature"
85 // UnknownKey: no allowed-signers line lists the key.
86 UnknownKey Reason = "unknown-key"
87 // ClassNotAllowed: the key is listed, but only in a signer class this
88 // call does not accept. A software key where a security key is
89 // required is refused for this reason.
90 ClassNotAllowed Reason = "class-not-allowed"
91 // PrincipalNotAllowed: the key is listed, but not for the requested
92 // principal.
93 PrincipalNotAllowed Reason = "principal-not-allowed"
94 // NamespaceNotAllowed: the key's line restricts it to other namespaces.
95 NamespaceNotAllowed Reason = "namespace-not-allowed"
96 // NotYetValid: the verify time is before the key's valid-after.
97 NotYetValid Reason = "not-yet-valid"
98 // Expired: the verify time is after the key's valid-before.
99 Expired Reason = "expired"
100 // NoUserPresence: a genuine security-key signature made without a
101 // touch. The key's owner should treat this as a sign that something
102 // running as them is using the key.
103 NoUserPresence Reason = "no-user-presence"
104 // NoUserVerification: user verification was required, and the
105 // signature does not prove it.
106 NoUserVerification Reason = "no-user-verification"
107 // TagNameMismatch: a signed git tag object is reachable under a name
108 // other than the one it was signed with.
109 TagNameMismatch Reason = "tag-name-mismatch"
110 // TargetNotCommit: a signed git tag points at something other than a
111 // commit, such as another tag or a tree.
112 TargetNotCommit Reason = "target-not-commit"
113 // TargetMissing: the commit a signed git tag points at is not in the
114 // repository.
115 TargetMissing Reason = "target-missing"
116)
117
118// Refusal is the error Verify returns when it examined a signature and
119// does not accept it. Any other error means verification could not be
120// attempted.
121type Refusal struct {
122 Reason Reason
123 Detail string
124 // Signer is what is known about the signing key. It is nil when the
125 // signature could not be read far enough to name one.
126 Signer *Signer
127}
128
129func (r *Refusal) Error() string {
130 return fmt.Sprintf("refused (%s): %s", r.Reason, r.Detail)
131}
132
133// Policy is what a caller requires beyond a valid, allowed signature.
134// User presence is not in it: every security-key signature must have UP
135// set, and nothing turns that off.
136type Policy struct {
137 // Classes is the signer classes the call accepts. Empty means fido-sk
138 // only. A trust-root operation that only the TKey may authorize asks
139 // for exactly ClassTKey, and then no security key satisfies it.
140 Classes []Class
141 // AllowNonSK adds ClassSoftware to Classes. Software keys prove nothing
142 // about presence: leave it off for anything a human approves. It never
143 // makes a key count as a TKey: only a tkey-signer line does that.
144 AllowNonSK bool
145 // RequireUV refuses any signature that does not prove user
146 // verification. Only a security key can prove it, so with RequireUV
147 // set, TKey and software signatures are refused.
148 RequireUV bool
149}
150
151func (p Policy) classes() map[Class]bool {
152 set := map[Class]bool{}
153 for _, c := range p.Classes {
154 set[c] = true
155 }
156 if len(set) == 0 {
157 set[ClassFIDO] = true
158 }
159 if p.AllowNonSK {
160 set[ClassSoftware] = true
161 }
162 return set
163}
164
165func classList(set map[Class]bool) string {
166 var names []string
167 for c := range set {
168 names = append(names, string(c))
169 }
170 slices.Sort(names)
171 return strings.Join(names, ", ")
172}
173
174// ParseClass reads a class name.
175func ParseClass(name string) (Class, error) {
176 switch c := Class(name); c {
177 case ClassFIDO, ClassTKey, ClassSoftware:
178 return c, nil
179 }
180 return "", fmt.Errorf("%q is not a signer class: use %s, %s or %s", name, ClassFIDO, ClassTKey, ClassSoftware)
181}
182
183// Options says what to verify against.
184type Options struct {
185 // Namespace is the SSHSIG namespace the signature must carry, such as
186 // "git" for git objects. Required.
187 Namespace string
188 // Principal is the identity the signature must be valid for, as with
189 // `ssh-keygen -Y verify -I`. Required, unless AnyPrincipal is set.
190 Principal string
191 // AnyPrincipal accepts any line that lists the key, whatever its
192 // principals field says, as `ssh-keygen -Y find-principals` does. Even
193 // a line whose principals are all negated, such as "!*", then
194 // authorizes its key. Use it only where the key alone decides.
195 AnyPrincipal bool
196 Policy Policy
197 // Time is when the signer's validity window is judged. The zero value
198 // means now.
199 Time time.Time
200}
201
202// Signer describes the key that made a signature.
203type Signer struct {
204 KeyType string
205 Fingerprint string // SHA256:..., as `ssh-keygen -l` prints it
206 SecurityKey bool
207 // Flags and Counter are the authenticator's flags byte and signature
208 // counter. They are zero for a key that is not a security key.
209 Flags byte
210 Counter uint32
211}
212
213// UserPresent reports whether the signature carries the UP bit.
214func (s Signer) UserPresent() bool { return s.SecurityKey && s.Flags&FlagUserPresent != 0 }
215
216// UserVerified reports whether the signature carries the UV bit.
217func (s Signer) UserVerified() bool { return s.SecurityKey && s.Flags&FlagUserVerified != 0 }
218
219// Result is a verified signature.
220type Result struct {
221 Signer
222 Namespace string
223 // Principal is the requested principal or, with AnyPrincipal, the
224 // principals field of the first allowed-signers line that matched.
225 Principal string
226 // Class is the matching line's signer class.
227 Class Class
228 // SignerApp is the version a tkey-signer line records, if any.
229 SignerApp string
230}
231
232// Verify checks signature, an armored or raw SSHSIG, over message.
233// It returns a *Refusal if the signature is not acceptable.
234func Verify(signers *AllowedSigners, message, signature []byte, opts Options) (*Result, error) {
235 switch {
236 case opts.Namespace == "":
237 return nil, errors.New("sigverify: no namespace given")
238 case signers == nil:
239 return nil, errors.New("sigverify: no allowed signers given")
240 case opts.Principal == "" && !opts.AnyPrincipal:
241 return nil, errors.New("sigverify: no principal given, and AnyPrincipal is not set")
242 case opts.Principal != "" && opts.AnyPrincipal:
243 return nil, errors.New("sigverify: both a principal and AnyPrincipal given")
244 case strings.IndexByte(opts.Principal, 0) >= 0 || strings.IndexByte(opts.Namespace, 0) >= 0:
245 return nil, errors.New("sigverify: a principal or namespace holds a NUL byte")
246 }
247 for _, c := range opts.Policy.Classes {
248 if _, err := ParseClass(string(c)); err != nil {
249 return nil, fmt.Errorf("sigverify: %w", err)
250 }
251 }
252 at := opts.Time
253 if at.IsZero() {
254 at = time.Now()
255 }
256
257 env, err := parseSignature(signature)
258 if err != nil {
259 return nil, &Refusal{Reason: Malformed, Detail: "the signature is not an SSHSIG: " + err.Error()}
260 }
261 key, err := parsePublicKey(env.publicKey)
262 if err != nil {
263 var unsupported unsupportedKeyError
264 if errors.As(err, &unsupported) {
265 return nil, &Refusal{Reason: UnsupportedKey, Detail: err.Error()}
266 }
267 return nil, &Refusal{Reason: Malformed, Detail: err.Error()}
268 }
269 signer := &Signer{KeyType: key.typ, Fingerprint: key.fingerprint(), SecurityKey: key.securityKey()}
270 refuse := func(reason Reason, format string, args ...any) (*Result, error) {
271 return nil, &Refusal{Reason: reason, Detail: fmt.Sprintf(format, args...), Signer: signer}
272 }
273
274 if env.namespace != opts.Namespace {
275 return refuse(WrongNamespace, "the signature is for namespace %q, not %q", env.namespace, opts.Namespace)
276 }
277 data, err := signedData(env.namespace, env.hashAlg, message)
278 if err != nil {
279 return refuse(Malformed, "%s", err)
280 }
281 auth, ok, err := key.verify(data, env.signature)
282 if err != nil {
283 return refuse(Malformed, "%s", err)
284 }
285 if !ok {
286 return refuse(BadSignature, "the signature by %s does not verify over this message", signer.Fingerprint)
287 }
288 signer.Flags, signer.Counter = auth.flags, auth.counter
289
290 m, refusal := signers.lookup(request{
291 key: key.blob,
292 principal: opts.Principal,
293 namespace: opts.Namespace,
294 at: at,
295 classes: opts.Policy.classes(),
296 })
297 if refusal != nil {
298 refusal.Signer = signer
299 return nil, refusal
300 }
301
302 if m.class == ClassFIDO && !signer.UserPresent() {
303 return refuse(NoUserPresence, "the signature by %s is genuine, but its authenticator flags are 0x%02x: the key was not touched", signer.Fingerprint, signer.Flags)
304 }
305 if opts.Policy.RequireUV || m.uvRequired {
306 why := "the policy requires it"
307 if !opts.Policy.RequireUV {
308 why = "the signer's line carries verify-required"
309 }
310 switch {
311 case m.class == ClassTKey:
312 return refuse(NoUserVerification, "user verification is required (%s), and a TKey signature cannot prove it", why)
313 case m.class != ClassFIDO:
314 return refuse(NoUserVerification, "user verification is required (%s), and a %s software key cannot prove it", why, key.typ)
315 case !signer.UserVerified():
316 return refuse(NoUserVerification, "user verification is required (%s), and the authenticator flags are 0x%02x", why, signer.Flags)
317 }
318 }
319
320 return &Result{
321 Signer: *signer,
322 Namespace: env.namespace,
323 Principal: m.principal,
324 Class: m.class,
325 SignerApp: m.signerApp,
326 }, nil
327}