| 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. |
| 31 | package sigverify |
| 32 | |
| 33 | import ( |
| 34 | "errors" |
| 35 | "fmt" |
| 36 | "slices" |
| 37 | "strings" |
| 38 | "time" |
| 39 | ) |
| 40 | |
| 41 | // The authenticator-data flag bits, from the WebAuthn specification. |
| 42 | const ( |
| 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. |
| 49 | type Class string |
| 50 | |
| 51 | const ( |
| 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. |
| 70 | type Reason string |
| 71 | |
| 72 | const ( |
| 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. |
| 121 | type 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 | |
| 129 | func (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. |
| 136 | type 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 | |
| 151 | func (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 | |
| 165 | func 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. |
| 175 | func 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. |
| 184 | type 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. |
| 203 | type 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. |
| 214 | func (s Signer) UserPresent() bool { return s.SecurityKey && s.Flags&FlagUserPresent != 0 } |
| 215 | |
| 216 | // UserVerified reports whether the signature carries the UV bit. |
| 217 | func (s Signer) UserVerified() bool { return s.SecurityKey && s.Flags&FlagUserVerified != 0 } |
| 218 | |
| 219 | // Result is a verified signature. |
| 220 | type 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. |
| 234 | func 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 | } |
| 245 | for _, c := range opts.Policy.Classes { |
| 246 | if _, err := ParseClass(string(c)); err != nil { |
| 247 | return nil, fmt.Errorf("sigverify: %w", err) |
| 248 | } |
| 249 | } |
| 250 | at := opts.Time |
| 251 | if at.IsZero() { |
| 252 | at = time.Now() |
| 253 | } |
| 254 | |
| 255 | env, err := parseSignature(signature) |
| 256 | if err != nil { |
| 257 | return nil, &Refusal{Reason: Malformed, Detail: "the signature is not an SSHSIG: " + err.Error()} |
| 258 | } |
| 259 | key, err := parsePublicKey(env.publicKey) |
| 260 | if err != nil { |
| 261 | var unsupported unsupportedKeyError |
| 262 | if errors.As(err, &unsupported) { |
| 263 | return nil, &Refusal{Reason: UnsupportedKey, Detail: err.Error()} |
| 264 | } |
| 265 | return nil, &Refusal{Reason: Malformed, Detail: err.Error()} |
| 266 | } |
| 267 | signer := &Signer{KeyType: key.typ, Fingerprint: key.fingerprint(), SecurityKey: key.securityKey()} |
| 268 | refuse := func(reason Reason, format string, args ...any) (*Result, error) { |
| 269 | return nil, &Refusal{Reason: reason, Detail: fmt.Sprintf(format, args...), Signer: signer} |
| 270 | } |
| 271 | |
| 272 | if env.namespace != opts.Namespace { |
| 273 | return refuse(WrongNamespace, "the signature is for namespace %q, not %q", env.namespace, opts.Namespace) |
| 274 | } |
| 275 | data, err := signedData(env.namespace, env.hashAlg, message) |
| 276 | if err != nil { |
| 277 | return refuse(Malformed, "%s", err) |
| 278 | } |
| 279 | auth, ok, err := key.verify(data, env.signature) |
| 280 | if err != nil { |
| 281 | return refuse(Malformed, "%s", err) |
| 282 | } |
| 283 | if !ok { |
| 284 | return refuse(BadSignature, "the signature by %s does not verify over this message", signer.Fingerprint) |
| 285 | } |
| 286 | signer.Flags, signer.Counter = auth.flags, auth.counter |
| 287 | |
| 288 | m, refusal := signers.lookup(request{ |
| 289 | key: key.blob, |
| 290 | principal: opts.Principal, |
| 291 | namespace: opts.Namespace, |
| 292 | at: at, |
| 293 | classes: opts.Policy.classes(), |
| 294 | }) |
| 295 | if refusal != nil { |
| 296 | refusal.Signer = signer |
| 297 | return nil, refusal |
| 298 | } |
| 299 | |
| 300 | if m.class == ClassFIDO && !signer.UserPresent() { |
| 301 | 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) |
| 302 | } |
| 303 | if opts.Policy.RequireUV || m.uvRequired { |
| 304 | why := "the policy requires it" |
| 305 | if !opts.Policy.RequireUV { |
| 306 | why = "the signer's line carries verify-required" |
| 307 | } |
| 308 | switch { |
| 309 | case m.class == ClassTKey: |
| 310 | return refuse(NoUserVerification, "user verification is required (%s), and a TKey signature cannot prove it", why) |
| 311 | case m.class != ClassFIDO: |
| 312 | return refuse(NoUserVerification, "user verification is required (%s), and a %s software key cannot prove it", why, key.typ) |
| 313 | case !signer.UserVerified(): |
| 314 | return refuse(NoUserVerification, "user verification is required (%s), and the authenticator flags are 0x%02x", why, signer.Flags) |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | return &Result{ |
| 319 | Signer: *signer, |
| 320 | Namespace: env.namespace, |
| 321 | Principal: m.principal, |
| 322 | Class: m.class, |
| 323 | SignerApp: m.signerApp, |
| 324 | }, nil |
| 325 | } |