gunk-dev/the-valley

sigverify.go

sourcefe4ab5bf3cabmain's published tip history
8.8 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// Verify checks, in order:
19//
20// 1. The signature is a well-formed SSHSIG.
21// 2. Its namespace is the one the caller expects.
22// 3. Its key is a security key, unless the policy admits software keys.
23// 4. The signature verifies over the message.
24// 5. The allowed-signers file lists the key, for the requested principal
25// if there is one, in this namespace, at the verify time.
26// 6. A security key's signature has UP set, always.
27// 7. It has UV set, when the policy or the signer's line requires it.
28//
29// The first check that fails is the refusal, with a Reason that names it.
30package sigverify
31
32import (
33 "errors"
34 "fmt"
35 "time"
36)
37
38// The authenticator-data flag bits, from the WebAuthn specification.
39const (
40 FlagUserPresent = 0x01
41 FlagUserVerified = 0x04
42)
43
44// Reason names why a signature was refused. The values are stable, and
45// callers may match on them.
46type Reason string
47
48const (
49 // Malformed: the signature, or the git tag carrying it, cannot be read.
50 Malformed Reason = "malformed"
51 // Unsigned: a git tag carries no signature at all.
52 Unsigned Reason = "unsigned"
53 // WrongNamespace: the signature was made for a different namespace.
54 WrongNamespace Reason = "wrong-namespace"
55 // UnsupportedKey: the signing key is of a type this package does not
56 // verify, such as a certificate.
57 UnsupportedKey Reason = "unsupported-key"
58 // NotSecurityKey: the key is a software key, and the policy requires a
59 // FIDO security key.
60 NotSecurityKey Reason = "not-security-key"
61 // BadSignature: the signature does not verify over the message. Either
62 // the message changed, or the signature is not that key's.
63 BadSignature Reason = "bad-signature"
64 // UnknownKey: no allowed-signers line lists the key.
65 UnknownKey Reason = "unknown-key"
66 // PrincipalNotAllowed: the key is listed, but not for the requested
67 // principal.
68 PrincipalNotAllowed Reason = "principal-not-allowed"
69 // NamespaceNotAllowed: the key's line restricts it to other namespaces.
70 NamespaceNotAllowed Reason = "namespace-not-allowed"
71 // NotYetValid: the verify time is before the key's valid-after.
72 NotYetValid Reason = "not-yet-valid"
73 // Expired: the verify time is after the key's valid-before.
74 Expired Reason = "expired"
75 // NoUserPresence: a genuine security-key signature made without a
76 // touch. The key's owner should treat this as a sign that something
77 // running as them is using the key.
78 NoUserPresence Reason = "no-user-presence"
79 // NoUserVerification: user verification was required, and the
80 // signature does not prove it.
81 NoUserVerification Reason = "no-user-verification"
82 // TagNameMismatch: a signed git tag object is reachable under a name
83 // other than the one it was signed with.
84 TagNameMismatch Reason = "tag-name-mismatch"
85)
86
87// Refusal is the error Verify returns when it examined a signature and
88// does not accept it. Any other error means verification could not be
89// attempted.
90type Refusal struct {
91 Reason Reason
92 Detail string
93 // Signer is what is known about the signing key. It is nil when the
94 // signature could not be read far enough to name one.
95 Signer *Signer
96}
97
98func (r *Refusal) Error() string {
99 return fmt.Sprintf("refused (%s): %s", r.Reason, r.Detail)
100}
101
102// Policy is what a caller requires beyond a valid, allowed signature.
103// User presence is not in it: every security-key signature must have UP
104// set, and nothing turns that off.
105type Policy struct {
106 // RequireUV refuses any signature that does not prove user
107 // verification. A software key cannot prove it, so with RequireUV
108 // set only security keys can pass.
109 RequireUV bool
110 // AllowNonSK admits signatures by software keys, which prove nothing
111 // about presence. Leave it off for anything a human approves.
112 AllowNonSK bool
113}
114
115// Options says what to verify against.
116type Options struct {
117 // Namespace is the SSHSIG namespace the signature must carry, such as
118 // "git" for git objects. Required.
119 Namespace string
120 // Principal, if set, is the identity the signature must be valid for,
121 // as with `ssh-keygen -Y verify -I`. If empty, any principal the key
122 // is listed under will do, as with `ssh-keygen -Y find-principals`.
123 Principal string
124 Policy Policy
125 // Time is when the signer's validity window is judged. The zero value
126 // means now.
127 Time time.Time
128}
129
130// Signer describes the key that made a signature.
131type Signer struct {
132 KeyType string
133 Fingerprint string // SHA256:..., as `ssh-keygen -l` prints it
134 SecurityKey bool
135 // Flags and Counter are the authenticator's flags byte and signature
136 // counter. They are zero for a software key.
137 Flags byte
138 Counter uint32
139}
140
141// UserPresent reports whether the signature proves a touch.
142func (s Signer) UserPresent() bool { return s.SecurityKey && s.Flags&FlagUserPresent != 0 }
143
144// UserVerified reports whether the signature proves a PIN or biometric.
145func (s Signer) UserVerified() bool { return s.SecurityKey && s.Flags&FlagUserVerified != 0 }
146
147// Result is a verified signature.
148type Result struct {
149 Signer
150 Namespace string
151 // Principal is the requested principal or, if none was requested, the
152 // principals field of the first allowed-signers line that matched.
153 Principal string
154}
155
156// Verify checks signature, an armored or raw SSHSIG, over message.
157// It returns a *Refusal if the signature is not acceptable.
158func Verify(signers *AllowedSigners, message, signature []byte, opts Options) (*Result, error) {
159 if opts.Namespace == "" {
160 return nil, errors.New("sigverify: no namespace given")
161 }
162 if signers == nil {
163 return nil, errors.New("sigverify: no allowed signers given")
164 }
165 at := opts.Time
166 if at.IsZero() {
167 at = time.Now()
168 }
169
170 env, err := parseSignature(signature)
171 if err != nil {
172 return nil, &Refusal{Reason: Malformed, Detail: "the signature is not an SSHSIG: " + err.Error()}
173 }
174 key, err := parsePublicKey(env.publicKey)
175 if err != nil {
176 var unsupported unsupportedKeyError
177 if errors.As(err, &unsupported) {
178 return nil, &Refusal{Reason: UnsupportedKey, Detail: err.Error()}
179 }
180 return nil, &Refusal{Reason: Malformed, Detail: err.Error()}
181 }
182 signer := &Signer{KeyType: key.typ, Fingerprint: key.fingerprint(), SecurityKey: key.securityKey()}
183 refuse := func(reason Reason, format string, args ...any) (*Result, error) {
184 return nil, &Refusal{Reason: reason, Detail: fmt.Sprintf(format, args...), Signer: signer}
185 }
186
187 if env.namespace != opts.Namespace {
188 return refuse(WrongNamespace, "the signature is for namespace %q, not %q", env.namespace, opts.Namespace)
189 }
190 if !signer.SecurityKey && !opts.Policy.AllowNonSK {
191 return refuse(NotSecurityKey, "the signature is by a %s software key, and only FIDO security keys are accepted", key.typ)
192 }
193 data, err := signedData(env.namespace, env.hashAlg, message)
194 if err != nil {
195 return refuse(Malformed, "%s", err)
196 }
197 auth, ok, err := key.verify(data, env.signature)
198 if err != nil {
199 return refuse(Malformed, "%s", err)
200 }
201 if !ok {
202 return refuse(BadSignature, "the signature by %s does not verify over this message", signer.Fingerprint)
203 }
204 signer.Flags, signer.Counter = auth.flags, auth.counter
205
206 principal, uvRequired, refusal := signers.lookup(key.blob, opts.Principal, opts.Namespace, at)
207 if refusal != nil {
208 refusal.Signer = signer
209 return nil, refusal
210 }
211
212 if signer.SecurityKey && !signer.UserPresent() {
213 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)
214 }
215 if (opts.Policy.RequireUV || uvRequired) && !signer.UserVerified() {
216 why := "the policy requires it"
217 if !opts.Policy.RequireUV {
218 why = "the signer's line carries verify-required"
219 }
220 if !signer.SecurityKey {
221 return refuse(NoUserVerification, "user verification is required (%s), and a %s software key cannot prove it", why, key.typ)
222 }
223 return refuse(NoUserVerification, "user verification is required (%s), and the authenticator flags are 0x%02x", why, signer.Flags)
224 }
225
226 return &Result{Signer: *signer, Namespace: env.namespace, Principal: principal}, nil
227}