| 1 | package sigverify |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/base64" |
| 6 | "errors" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | "the-valley/sigverify/internal/skforge" |
| 12 | ) |
| 13 | |
| 14 | var ( |
| 15 | approver = skforge.Ed25519SK("sigverify test approver") |
| 16 | ecdsaSK = skforge.ECDSASK("sigverify test ecdsa approver") |
| 17 | software = skforge.Ed25519("sigverify test software key") |
| 18 | stranger = skforge.Ed25519SK("sigverify test stranger") |
| 19 | |
| 20 | // tkey stands in for a Tillitis TKey. A TKey's signature is an ordinary |
| 21 | // ssh-ed25519 SSHSIG, byte for byte what this software key makes. What |
| 22 | // makes it trustworthy is in the device: the signer app demands a touch |
| 23 | // for every signature, and the key is derived from the app's hash, so a |
| 24 | // no-touch app has a different key. None of that is visible in the |
| 25 | // bytes, so a verifier can only do what these tests check: accept the |
| 26 | // pinned key, on a tkey-signer line, when the call opts in to the class. |
| 27 | tkey = skforge.Ed25519("sigverify test tkey") |
| 28 | |
| 29 | message = []byte("object 0123\ntype commit\ntag v1\n\nrelease v1\n") |
| 30 | when = time.Date(2026, 10, 4, 12, 0, 0, 0, time.UTC) |
| 31 | ) |
| 32 | |
| 33 | const ( |
| 34 | release = "release@cosmo.invalid" |
| 35 | root = "root@valley.invalid" |
| 36 | ) |
| 37 | |
| 38 | // signers is the allowed-signers file most cases verify against. |
| 39 | func signers(t *testing.T) *AllowedSigners { |
| 40 | t.Helper() |
| 41 | return parse(t, strings.Join([]string{ |
| 42 | "# comments and blank lines are skipped", |
| 43 | "", |
| 44 | release + ` namespaces="git,file" ` + approver.PublicLine() + " the approver", |
| 45 | `ecdsa@cosmo.invalid ` + ecdsaSK.PublicLine(), |
| 46 | `ci@cosmo.invalid ` + software.PublicLine(), |
| 47 | root + ` tkey-signer="1.0.0" ` + tkey.PublicLine(), |
| 48 | }, "\n")) |
| 49 | } |
| 50 | |
| 51 | func parse(t *testing.T, text string) *AllowedSigners { |
| 52 | t.Helper() |
| 53 | a, err := ParseAllowedSigners("allowed_signers", []byte(text)) |
| 54 | if err != nil { |
| 55 | t.Fatal(err) |
| 56 | } |
| 57 | return a |
| 58 | } |
| 59 | |
| 60 | // as is the options for verifying in the git namespace as principal. |
| 61 | func as(principal string, p Policy) Options { |
| 62 | return Options{Namespace: "git", Principal: principal, Policy: p, Time: when} |
| 63 | } |
| 64 | |
| 65 | func TestVerify(t *testing.T) { |
| 66 | none := Policy{} |
| 67 | tkeyOnly := Policy{Classes: []Class{ClassTKey}} |
| 68 | |
| 69 | cases := []struct { |
| 70 | name string |
| 71 | signers string // replaces the default file when set |
| 72 | sig []byte |
| 73 | message []byte |
| 74 | opts Options |
| 75 | want Reason // empty: accepted |
| 76 | class Class // checked when accepted |
| 77 | }{ |
| 78 | {name: "a touched sk-ed25519 signature", sig: approver.Sign(message, "git", 0x01), opts: as(release, none), class: ClassFIDO}, |
| 79 | {name: "a touched sk-ecdsa signature", sig: ecdsaSK.Sign(message, "git", 0x01), opts: as("ecdsa@cosmo.invalid", none), class: ClassFIDO}, |
| 80 | {name: "touch and PIN, UV required", sig: approver.Sign(message, "git", 0x05), opts: as(release, Policy{RequireUV: true}), class: ClassFIDO}, |
| 81 | {name: "a software key the policy admits", sig: software.Sign(message, "git", 0), opts: as("ci@cosmo.invalid", Policy{AllowNonSK: true}), class: ClassSoftware}, |
| 82 | {name: "a TKey, with its class allowed", sig: tkey.Sign(message, "git", 0), opts: as(root, tkeyOnly), class: ClassTKey}, |
| 83 | {name: "a security key, with both classes allowed", sig: approver.Sign(message, "git", 0x01), opts: as(release, Policy{Classes: []Class{ClassFIDO, ClassTKey}}), class: ClassFIDO}, |
| 84 | |
| 85 | {name: "untouched: UP clear", sig: approver.Sign(message, "git", 0x00), opts: as(release, none), want: NoUserPresence}, |
| 86 | {name: "untouched sk-ecdsa", sig: ecdsaSK.Sign(message, "git", 0x00), opts: as("ecdsa@cosmo.invalid", none), want: NoUserPresence}, |
| 87 | {name: "a PIN is no touch: UV without UP", sig: approver.Sign(message, "git", 0x04), opts: as(release, none), want: NoUserPresence}, |
| 88 | {name: "UP alone when UV is required", sig: approver.Sign(message, "git", 0x01), opts: as(release, Policy{RequireUV: true}), want: NoUserVerification}, |
| 89 | {name: "a software key cannot prove UV", sig: software.Sign(message, "git", 0), opts: as("ci@cosmo.invalid", Policy{AllowNonSK: true, RequireUV: true}), want: NoUserVerification}, |
| 90 | {name: "wrong namespace", sig: approver.Sign(message, "file", 0x01), opts: as(release, none), want: WrongNamespace}, |
| 91 | {name: "a namespace the line does not allow", sig: approver.Sign(message, "ssh-approval", 0x01), opts: Options{Namespace: "ssh-approval", Principal: release, Time: when}, want: NamespaceNotAllowed}, |
| 92 | {name: "signer not in allowed signers", sig: stranger.Sign(message, "git", 0x01), opts: as(release, none), want: UnknownKey}, |
| 93 | {name: "a principal the line does not list", sig: approver.Sign(message, "git", 0x01), opts: as("someone@else.invalid", none), want: PrincipalNotAllowed}, |
| 94 | {name: "tampered message", sig: approver.Sign(message, "git", 0x01), message: []byte("object 0123\ntype commit\ntag v2\n\nrelease v1\n"), opts: as(release, none), want: BadSignature}, |
| 95 | {name: "a software key when only security keys are accepted", sig: software.Sign(message, "git", 0), opts: as("ci@cosmo.invalid", none), want: ClassNotAllowed}, |
| 96 | {name: "not a signature at all", sig: []byte("-----BEGIN PGP SIGNATURE-----\n"), opts: as(release, none), want: Malformed}, |
| 97 | |
| 98 | // The TKey class is opt-in, and opting in to it admits nothing else. |
| 99 | {name: "a TKey by default", sig: tkey.Sign(message, "git", 0), opts: as(root, none), want: ClassNotAllowed}, |
| 100 | {name: "a TKey with software keys allowed", sig: tkey.Sign(message, "git", 0), opts: as(root, Policy{AllowNonSK: true}), want: ClassNotAllowed}, |
| 101 | {name: "a TKey when UV is required", sig: tkey.Sign(message, "git", 0), opts: as(root, Policy{Classes: []Class{ClassTKey}, RequireUV: true}), want: NoUserVerification}, |
| 102 | {name: "a software key where only a TKey will do", sig: software.Sign(message, "git", 0), opts: as("ci@cosmo.invalid", tkeyOnly), want: ClassNotAllowed}, |
| 103 | {name: "a security key where only a TKey will do", sig: approver.Sign(message, "git", 0x05), opts: as(release, tkeyOnly), want: ClassNotAllowed}, |
| 104 | { |
| 105 | name: "a TKey's key on an unmarked line is a software key", |
| 106 | signers: root + " " + tkey.PublicLine(), |
| 107 | sig: tkey.Sign(message, "git", 0), opts: as(root, tkeyOnly), want: ClassNotAllowed, |
| 108 | }, |
| 109 | |
| 110 | { |
| 111 | name: "expired signer", |
| 112 | signers: release + ` valid-before="20260101Z" ` + approver.PublicLine(), |
| 113 | sig: approver.Sign(message, "git", 0x01), opts: as(release, none), want: Expired, |
| 114 | }, |
| 115 | { |
| 116 | name: "not-yet-valid signer", |
| 117 | signers: release + ` valid-after="20270101Z" ` + approver.PublicLine(), |
| 118 | sig: approver.Sign(message, "git", 0x01), opts: as(release, none), want: NotYetValid, |
| 119 | }, |
| 120 | { |
| 121 | name: "inside the validity window", |
| 122 | signers: release + ` valid-after="20260101Z",valid-before="20270101Z" ` + approver.PublicLine(), |
| 123 | sig: approver.Sign(message, "git", 0x01), opts: as(release, none), class: ClassFIDO, |
| 124 | }, |
| 125 | { |
| 126 | name: "verify-required on the signer's line", |
| 127 | signers: release + ` verify-required ` + approver.PublicLine(), |
| 128 | sig: approver.Sign(message, "git", 0x01), opts: as(release, none), want: NoUserVerification, |
| 129 | }, |
| 130 | { |
| 131 | name: "verify-required satisfied", |
| 132 | signers: release + ` verify-required ` + approver.PublicLine(), |
| 133 | sig: approver.Sign(message, "git", 0x05), opts: as(release, none), class: ClassFIDO, |
| 134 | }, |
| 135 | { |
| 136 | // Both lines grant this request. The first alone would not |
| 137 | // demand UV, but the second does, and the stricter one holds. |
| 138 | name: "verify-required on any matching line", |
| 139 | signers: release + ` ` + approver.PublicLine() + "\n" + |
| 140 | `*@cosmo.invalid verify-required ` + approver.PublicLine(), |
| 141 | sig: approver.Sign(message, "git", 0x01), opts: as(release, none), want: NoUserVerification, |
| 142 | }, |
| 143 | { |
| 144 | name: "a certificate authority line is not the key itself", |
| 145 | signers: release + ` cert-authority ` + approver.PublicLine(), |
| 146 | sig: approver.Sign(message, "git", 0x01), opts: as(release, none), want: UnknownKey, |
| 147 | }, |
| 148 | |
| 149 | // The review's two cases. A "*" in the pattern is a wildcard even |
| 150 | // where the principal holds a "*", so "a*b" matches "a*xb". |
| 151 | { |
| 152 | name: "a wildcard principal cannot skip a verify-required line", |
| 153 | signers: `* ` + approver.PublicLine() + "\n" + |
| 154 | `a*b verify-required ` + approver.PublicLine(), |
| 155 | sig: approver.Sign(message, "git", 0x01), opts: as("a*xb", none), want: NoUserVerification, |
| 156 | }, |
| 157 | { |
| 158 | name: "a wildcard principal cannot skip an exclusion", |
| 159 | signers: `*,!a*b ` + approver.PublicLine(), |
| 160 | sig: approver.Sign(message, "git", 0x01), opts: as("a*xb", none), want: PrincipalNotAllowed, |
| 161 | }, |
| 162 | { |
| 163 | name: "a line that excludes every principal", |
| 164 | signers: `!* ` + approver.PublicLine(), |
| 165 | sig: approver.Sign(message, "git", 0x01), opts: as(release, none), want: PrincipalNotAllowed, |
| 166 | }, |
| 167 | { |
| 168 | // With AnyPrincipal the principals field is not consulted at |
| 169 | // all, as with ssh-keygen -Y find-principals. That is why it |
| 170 | // is opt-in. |
| 171 | name: "any principal ignores the principals field", |
| 172 | signers: `!* ` + approver.PublicLine(), |
| 173 | sig: approver.Sign(message, "git", 0x01), opts: Options{Namespace: "git", AnyPrincipal: true, Time: when}, class: ClassFIDO, |
| 174 | }, |
| 175 | } |
| 176 | for _, c := range cases { |
| 177 | t.Run(c.name, func(t *testing.T) { |
| 178 | a := signers(t) |
| 179 | if c.signers != "" { |
| 180 | a = parse(t, c.signers) |
| 181 | } |
| 182 | msg := message |
| 183 | if c.message != nil { |
| 184 | msg = c.message |
| 185 | } |
| 186 | result, err := Verify(a, msg, c.sig, c.opts) |
| 187 | if c.want == "" { |
| 188 | if err != nil { |
| 189 | t.Fatalf("refused: %v", err) |
| 190 | } |
| 191 | if result.Class != c.class { |
| 192 | t.Errorf("class %s, want %s", result.Class, c.class) |
| 193 | } |
| 194 | return |
| 195 | } |
| 196 | var refusal *Refusal |
| 197 | if !errors.As(err, &refusal) { |
| 198 | t.Fatalf("got %+v, %v; want a %s refusal", result, err, c.want) |
| 199 | } |
| 200 | if refusal.Reason != c.want { |
| 201 | t.Fatalf("refused for %s (%s), want %s", refusal.Reason, refusal.Detail, c.want) |
| 202 | } |
| 203 | }) |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | func TestVerifyReports(t *testing.T) { |
| 208 | r, err := Verify(signers(t), message, tkey.Sign(message, "git", 0), as(root, Policy{Classes: []Class{ClassTKey}})) |
| 209 | if err != nil { |
| 210 | t.Fatal(err) |
| 211 | } |
| 212 | if r.Principal != root || r.SignerApp != "1.0.0" || r.SecurityKey || r.Flags != 0 { |
| 213 | t.Errorf("TKey result %+v", r) |
| 214 | } |
| 215 | _, err = Verify(signers(t), message, tkey.Sign(message, "git", 0), as(root, Policy{Classes: []Class{ClassTKey}, RequireUV: true})) |
| 216 | if err == nil || !strings.Contains(err.Error(), "a TKey signature cannot prove it") { |
| 217 | t.Errorf("a TKey under RequireUV: %v", err) |
| 218 | } |
| 219 | |
| 220 | r, err = Verify(signers(t), message, approver.Sign(message, "git", 0x05), Options{Namespace: "git", AnyPrincipal: true, Time: when}) |
| 221 | if err != nil { |
| 222 | t.Fatal(err) |
| 223 | } |
| 224 | if r.Principal != release || r.Flags != 0x05 || r.Counter != skforge.Counter || !r.UserPresent() || !r.UserVerified() { |
| 225 | t.Errorf("result %+v", r) |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | // A principal is required. Leaving it out is a caller's mistake, not a |
| 230 | // verdict, so it is an error and not a refusal. |
| 231 | func TestVerifyRequiresAPrincipal(t *testing.T) { |
| 232 | sig := approver.Sign(message, "git", 0x01) |
| 233 | for name, opts := range map[string]Options{ |
| 234 | "none": {Namespace: "git"}, |
| 235 | "a NUL": {Namespace: "git", Principal: "release\x00x"}, |
| 236 | "both": {Namespace: "git", Principal: release, AnyPrincipal: true}, |
| 237 | "no class": {Namespace: "git", Principal: release, Policy: Policy{Classes: []Class{"yubikey"}}}, |
| 238 | } { |
| 239 | _, err := Verify(signers(t), message, sig, opts) |
| 240 | var refusal *Refusal |
| 241 | if err == nil || errors.As(err, &refusal) { |
| 242 | t.Errorf("%s: got %v, want an error", name, err) |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | // The refusal for an untouched signature names the key, because a genuine |
| 248 | // signature without a touch means something is using that key. |
| 249 | func TestNoUserPresenceNamesTheKey(t *testing.T) { |
| 250 | _, err := Verify(signers(t), message, approver.Sign(message, "git", 0), as(release, Policy{})) |
| 251 | var refusal *Refusal |
| 252 | if !errors.As(err, &refusal) || refusal.Signer == nil { |
| 253 | t.Fatalf("got %v", err) |
| 254 | } |
| 255 | if !refusal.Signer.SecurityKey || refusal.Signer.Flags != 0 || refusal.Signer.Counter != skforge.Counter { |
| 256 | t.Errorf("signer %+v", *refusal.Signer) |
| 257 | } |
| 258 | if !strings.Contains(refusal.Detail, refusal.Signer.Fingerprint) { |
| 259 | t.Errorf("the detail does not name the key: %s", refusal.Detail) |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | // Signatures altered after they were made. Each one is a signature that |
| 264 | // once verified, with one thing changed that a signer did not sign or a |
| 265 | // parser must not let through. |
| 266 | func TestAlteredSignatures(t *testing.T) { |
| 267 | touched := approver.Sign(message, "git", 0x01) |
| 268 | env := func() skforge.Envelope { return skforge.ParseEnvelope(touched) } |
| 269 | // The flags byte and the counter are the last five bytes of a security |
| 270 | // key's signature. |
| 271 | flags := func(e skforge.Envelope) int { return len(e.Signature) - 5 } |
| 272 | |
| 273 | otherApp := approver.WithApplication("ssh:elsewhere") |
| 274 | cases := []struct { |
| 275 | name string |
| 276 | signers string |
| 277 | sig func() []byte |
| 278 | opts Options |
| 279 | want Reason // empty: accepted |
| 280 | }{ |
| 281 | {name: "the original, armored", sig: func() []byte { return touched }}, |
| 282 | {name: "the original, raw", sig: func() []byte { return env().Blob() }}, |
| 283 | {name: "whitespace after the footer", sig: func() []byte { return append(touched, "\n \t\n"...) }}, |
| 284 | { |
| 285 | // ssh-keygen ignores the reserved field, and it is never |
| 286 | // signed: the signed bytes always hold an empty one. |
| 287 | name: "a non-empty reserved field", |
| 288 | sig: func() []byte { e := env(); e.Reserved = []byte("anything"); return e.Armored() }, |
| 289 | }, |
| 290 | |
| 291 | { |
| 292 | name: "UV added after signing", opts: as(release, Policy{RequireUV: true}), want: BadSignature, |
| 293 | sig: func() []byte { e := env(); e.Signature[flags(e)] |= FlagUserVerified; return e.Armored() }, |
| 294 | }, |
| 295 | { |
| 296 | name: "UP cleared after signing", want: BadSignature, |
| 297 | sig: func() []byte { e := env(); e.Signature[flags(e)] = 0; return e.Armored() }, |
| 298 | }, |
| 299 | { |
| 300 | name: "the counter changed", want: BadSignature, |
| 301 | sig: func() []byte { e := env(); e.Signature[len(e.Signature)-1]++; return e.Armored() }, |
| 302 | }, |
| 303 | { |
| 304 | // The same key enrolled under another application is listed; |
| 305 | // the signature was made under "ssh:", and the application is |
| 306 | // hashed into what the authenticator signed. |
| 307 | name: "the application substituted", |
| 308 | signers: release + " " + otherApp.PublicLine(), |
| 309 | want: BadSignature, |
| 310 | sig: func() []byte { e := env(); e.PublicKey = otherApp.Blob(); return e.Armored() }, |
| 311 | }, |
| 312 | { |
| 313 | name: "an ssh-ed25519 key carrying a security-key signature", want: Malformed, |
| 314 | sig: func() []byte { e := env(); e.PublicKey = software.Blob(); return e.Armored() }, |
| 315 | }, |
| 316 | { |
| 317 | name: "a security key carrying an ssh-ed25519 signature type", want: Malformed, |
| 318 | sig: func() []byte { |
| 319 | e := env() |
| 320 | r := skforge.Reader{Rest: e.Signature} |
| 321 | r.String() |
| 322 | e.Signature = append(skforge.String([]byte(TypeEd25519)), r.Rest...) |
| 323 | return e.Armored() |
| 324 | }, |
| 325 | }, |
| 326 | { |
| 327 | name: "an ECDSA integer with a redundant leading zero", opts: as("ecdsa@cosmo.invalid", Policy{}), want: Malformed, |
| 328 | sig: func() []byte { |
| 329 | e := skforge.ParseEnvelope(ecdsaSK.Sign(message, "git", 0x01)) |
| 330 | r := skforge.Reader{Rest: e.Signature} |
| 331 | typ, inner := r.String(), r.String() |
| 332 | ints := skforge.Reader{Rest: inner} |
| 333 | sr, ss := ints.String(), ints.String() |
| 334 | padded := append(skforge.String(append([]byte{0}, sr...)), skforge.String(ss)...) |
| 335 | e.Signature = append(append(skforge.String(typ), skforge.String(padded)...), r.Rest...) |
| 336 | return e.Armored() |
| 337 | }, |
| 338 | }, |
| 339 | {name: "version 0", want: Malformed, sig: func() []byte { e := env(); e.Version = 0; return e.Armored() }}, |
| 340 | {name: "version 2", want: Malformed, sig: func() []byte { e := env(); e.Version = 2; return e.Armored() }}, |
| 341 | {name: "hash sha1", want: Malformed, sig: func() []byte { e := env(); e.HashAlg = "sha1"; return e.Armored() }}, |
| 342 | {name: "hash SHA512", want: Malformed, sig: func() []byte { e := env(); e.HashAlg = "SHA512"; return e.Armored() }}, |
| 343 | {name: "trailing binary data", want: Malformed, sig: func() []byte { e := env(); e.Trailing = []byte{0}; return e.Armored() }}, |
| 344 | {name: "a truncated blob", want: Malformed, sig: func() []byte { b := env().Blob(); return b[:len(b)-1] }}, |
| 345 | { |
| 346 | // The signature is the last field, so its length prefix sits |
| 347 | // just before it. One more than the bytes that remain. |
| 348 | name: "a length past the end", want: Malformed, |
| 349 | sig: func() []byte { e := env(); b := e.Blob(); b[len(b)-len(e.Signature)-1]++; return b }, |
| 350 | }, |
| 351 | {name: "text after the footer", want: Malformed, sig: func() []byte { return append(touched, "release v9\n"...) }}, |
| 352 | {name: "a space inside the armor", want: Malformed, sig: func() []byte { return bytes.Replace(touched, []byte("\n"), []byte("\n "), 2) }}, |
| 353 | {name: "base64 with stray padding bits", want: Malformed, sig: func() []byte { return strayPaddingBits(t, env()) }}, |
| 354 | {name: "text before the header", want: Malformed, sig: func() []byte { return append([]byte("\n"), touched...) }}, |
| 355 | } |
| 356 | for _, c := range cases { |
| 357 | t.Run(c.name, func(t *testing.T) { |
| 358 | a := signers(t) |
| 359 | if c.signers != "" { |
| 360 | a = parse(t, c.signers) |
| 361 | } |
| 362 | opts := c.opts |
| 363 | if opts.Namespace == "" { |
| 364 | opts = as(release, Policy{}) |
| 365 | } |
| 366 | _, err := Verify(a, message, c.sig(), opts) |
| 367 | if c.want == "" { |
| 368 | if err != nil { |
| 369 | t.Fatalf("refused: %v", err) |
| 370 | } |
| 371 | return |
| 372 | } |
| 373 | var refusal *Refusal |
| 374 | if !errors.As(err, &refusal) || refusal.Reason != c.want { |
| 375 | t.Fatalf("got %v, want %s", err, c.want) |
| 376 | } |
| 377 | }) |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | // strayPaddingBits armors an envelope with base64 whose padding bits are |
| 382 | // not zero. A lenient decoder reads the same bytes from it, so it is a |
| 383 | // second encoding of the same signature. |
| 384 | func strayPaddingBits(t *testing.T, e skforge.Envelope) []byte { |
| 385 | t.Helper() |
| 386 | // Grow the ignored reserved field until the blob needs padding. |
| 387 | for len(e.Blob())%3 == 0 { |
| 388 | e.Reserved = append(e.Reserved, 'x') |
| 389 | } |
| 390 | enc := strayBits(t, base64.StdEncoding.EncodeToString(e.Blob())) |
| 391 | armored := skforge.Armor(nil) |
| 392 | return bytes.Replace(armored, []byte("\n\n"), []byte("\n"+wrap(enc)+"\n"), 1) |
| 393 | } |
| 394 | |
| 395 | // strayBits sets a padding bit in padded base64, which a lenient decoder |
| 396 | // ignores. |
| 397 | func strayBits(t *testing.T, enc string) string { |
| 398 | t.Helper() |
| 399 | const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" |
| 400 | last := strings.TrimRight(enc, "=") |
| 401 | if last == enc { |
| 402 | t.Fatal("the base64 has no padding") |
| 403 | } |
| 404 | i := len(last) - 1 |
| 405 | stray := last[:i] + string(alphabet[strings.IndexByte(alphabet, last[i])+1]) + enc[len(last):] |
| 406 | want, _ := base64.StdEncoding.DecodeString(enc) |
| 407 | if got, err := base64.StdEncoding.DecodeString(stray); err != nil || !bytes.Equal(got, want) { |
| 408 | t.Fatalf("a lenient decoder does not read the same bytes: %v", err) |
| 409 | } |
| 410 | return stray |
| 411 | } |
| 412 | |
| 413 | func wrap(s string) string { |
| 414 | var lines []string |
| 415 | for len(s) > 70 { |
| 416 | lines, s = append(lines, s[:70]), s[70:] |
| 417 | } |
| 418 | return strings.Join(append(lines, s), "\n") |
| 419 | } |
| 420 | |
| 421 | func TestParseAllowedSignersRefuses(t *testing.T) { |
| 422 | key := approver.PublicLine() |
| 423 | cases := map[string]string{ |
| 424 | "no-touch-required": `a@b no-touch-required ` + key, |
| 425 | "an unknown option": `a@b permit-everything ` + key, |
| 426 | "an unquoted value": `a@b namespaces=git ` + key, |
| 427 | "an unclosed quote": `a@b namespaces="git ` + key, |
| 428 | "a repeated option": `a@b namespaces="git",namespaces="file" ` + key, |
| 429 | "a trailing comma": `a@b namespaces="git", ` + key, |
| 430 | "an inverted window": `a@b valid-after="20270101",valid-before="20260101" ` + key, |
| 431 | "a bad time": `a@b valid-after="2026-01-01" ` + key, |
| 432 | "no key": `a@b namespaces="git"`, |
| 433 | "principals alone": `a@b`, |
| 434 | "a mislabelled key": `a@b ssh-ed25519 ` + strings.Fields(key)[1], |
| 435 | "an unsupported type": `a@b ssh-dss AAAAB3NzaC1kc3MAAACBAP`, |
| 436 | "a key that is not b64": `a@b ` + approver.Type + ` !!!`, |
| 437 | "a NUL in the principals": "*,!release\x00x " + key, |
| 438 | "a NUL in a comment": "# a comment\x00", |
| 439 | "a key with stray b64 bits": `a@b ` + approver.Type + " " + strayBits(t, strings.Fields(key)[1]), |
| 440 | "tkey-signer on a security key": `a@b tkey-signer ` + key, |
| 441 | "tkey-signer on sk-ecdsa": `a@b tkey-signer ` + ecdsaSK.PublicLine(), |
| 442 | "tkey-signer, verify-required": `a@b tkey-signer,verify-required ` + tkey.PublicLine(), |
| 443 | "tkey-signer as a CA": `a@b tkey-signer,cert-authority ` + tkey.PublicLine(), |
| 444 | "an empty tkey-signer version": `a@b tkey-signer="" ` + tkey.PublicLine(), |
| 445 | "a tkey-signer version with a space": `a@b tkey-signer="1 0" ` + tkey.PublicLine(), |
| 446 | "an unquoted tkey-signer version": `a@b tkey-signer=1.0 ` + tkey.PublicLine(), |
| 447 | } |
| 448 | for name, line := range cases { |
| 449 | t.Run(name, func(t *testing.T) { |
| 450 | if _, err := ParseAllowedSigners("f", []byte(line+"\n")); err == nil { |
| 451 | t.Fatalf("accepted %q", line) |
| 452 | } else if !strings.HasPrefix(err.Error(), "f:1: ") { |
| 453 | t.Errorf("error does not name the line: %v", err) |
| 454 | } |
| 455 | }) |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | func TestParseAllowedSignersAccepts(t *testing.T) { |
| 460 | key := approver.PublicLine() |
| 461 | for _, line := range []string{ |
| 462 | `a@b ` + key, |
| 463 | `a@b ` + key + ` a comment with spaces`, |
| 464 | ` a@b,c@d ` + key, |
| 465 | `"a@b,c d" ` + key, |
| 466 | `a@b NAMESPACES="git",Valid-After="202601011200Z" ` + key, |
| 467 | `a@b namespaces="g\"it" ` + key, |
| 468 | `a@b cert-authority,valid-before="20990101010101" ` + key, |
| 469 | "a@b\t" + key, |
| 470 | `a@b tkey-signer ` + tkey.PublicLine(), |
| 471 | `a@b TKEY-SIGNER="v1.0.0+tillitis:2" ` + tkey.PublicLine(), |
| 472 | `a@b namespaces="git",tkey-signer="1" ` + tkey.PublicLine(), |
| 473 | } { |
| 474 | if _, err := ParseAllowedSigners("f", []byte(line)); err != nil { |
| 475 | t.Errorf("%q: %v", line, err) |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | // matchPatternList is held to OpenSSH's match.c. The cmd/sigverify tests |
| 481 | // also compare it with ssh-keygen -Y match-principals. |
| 482 | func TestMatchPatternList(t *testing.T) { |
| 483 | cases := []struct { |
| 484 | s, list string |
| 485 | want int |
| 486 | }{ |
| 487 | {"a@b", "a@b", 1}, |
| 488 | {"a@b", "x@y,a@b", 1}, |
| 489 | {"a@b", "*@b", 1}, |
| 490 | {"a@b", "?@b", 1}, |
| 491 | {"aa@b", "?@b", 0}, |
| 492 | {"a@b", "*", 1}, |
| 493 | {"a@b", "*@b,!a@b", -1}, |
| 494 | {"c@b", "*@b,!a@b", 1}, |
| 495 | {"a@b", "!a@b", -1}, |
| 496 | {"c@b", "!a@b", 0}, |
| 497 | {"A@b", "a@b", 0}, |
| 498 | {"a@b.c", "a@*.c", 1}, |
| 499 | {"a@bxc", "a@*.c", 0}, |
| 500 | {"abcbd", "a*b*d", 1}, |
| 501 | {"abcbe", "a*b*d", 0}, |
| 502 | {"", "", 0}, |
| 503 | // A "*" or "?" in the pattern is a wildcard, whatever the |
| 504 | // principal holds at that position. |
| 505 | {"a*xb", "a*b", 1}, |
| 506 | {"a*xb", "*,!a*b", -1}, |
| 507 | {"a*b", "a*b", 1}, |
| 508 | {"a?c", "a?c", 1}, |
| 509 | {"abc", "a?c", 1}, |
| 510 | {"ac", "a?c", 0}, |
| 511 | {"*", "?", 1}, |
| 512 | {"**", "?", 0}, |
| 513 | {"a", "a**", 1}, |
| 514 | {"xa", "**a", 1}, |
| 515 | {"aa", "*a*a*", 1}, |
| 516 | {"a", "*a*a*", 0}, |
| 517 | {"b", "a,,b", 1}, |
| 518 | {`a\b`, `a\*`, 1}, |
| 519 | {"!a", "!!a", -1}, |
| 520 | // A pattern too long for OpenSSH's buffer voids the whole list, |
| 521 | // even after an earlier match. |
| 522 | {"x", "x," + strings.Repeat("y", 1023), 0}, |
| 523 | {"x", "x," + strings.Repeat("y", 1022), 1}, |
| 524 | } |
| 525 | for _, c := range cases { |
| 526 | if got := matchPatternList(c.s, c.list); got != c.want { |
| 527 | t.Errorf("matchPatternList(%q, %.40q) = %d, want %d", c.s, c.list, got, c.want) |
| 528 | } |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | func TestSplitTag(t *testing.T) { |
| 533 | sig := "-----BEGIN SSH SIGNATURE-----\nU1NIU0lH\n-----END SSH SIGNATURE-----\n" |
| 534 | header := "object 0123456789012345678901234567890123456789\ntype commit\ntag v1\ntagger T <t@t> 1 +0000\n" |
| 535 | // A line that merely looks like a signature, earlier in the message, |
| 536 | // is part of the payload: git splits at the last one. |
| 537 | body := "\nrelease\n-----BEGIN SSH SIGNATURE-----\nnot really\n" |
| 538 | payload, signature := SplitTag([]byte(header + body + sig)) |
| 539 | if string(payload) != header+body || string(signature) != sig { |
| 540 | t.Errorf("payload %q\nsignature %q", payload, signature) |
| 541 | } |
| 542 | |
| 543 | // A signature for the other hash algorithm rides in a gpgsig-sha256 |
| 544 | // header, and git takes it out of the payload. |
| 545 | other := "gpgsig-sha256 -----BEGIN SSH SIGNATURE-----\n U1NIU0lH\n -----END SSH SIGNATURE-----\n" |
| 546 | payload, _ = SplitTag([]byte(header + other + body + sig)) |
| 547 | if string(payload) != header+body { |
| 548 | t.Errorf("payload %q", payload) |
| 549 | } |
| 550 | |
| 551 | if _, signature := SplitTag([]byte(header + "\nunsigned\n")); signature != nil { |
| 552 | t.Errorf("an unsigned tag has signature %q", signature) |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | func TestVerifyTag(t *testing.T) { |
| 557 | a := signers(t) |
| 558 | const commit = "0123456789abcdef0123456789abcdef01234567" |
| 559 | tagText := func(object, kind, name string) string { |
| 560 | return "object " + object + "\ntype " + kind + "\ntag " + name + "\ntagger T <t@t> 1 +0000\n\nrelease\n" |
| 561 | } |
| 562 | signed := func(text string, flags byte) []byte { |
| 563 | return []byte(text + string(approver.Sign([]byte(text), "git", flags))) |
| 564 | } |
| 565 | text := tagText(commit, "commit", "v1.2.0") |
| 566 | object := signed(text, 0x01) |
| 567 | |
| 568 | result, tag, err := VerifyTag(a, object, "v1.2.0", as(release, Policy{})) |
| 569 | if err != nil { |
| 570 | t.Fatal(err) |
| 571 | } |
| 572 | if result.Principal != release || *tag != (Tag{Name: "v1.2.0", Commit: commit}) { |
| 573 | t.Errorf("result %+v, tag %+v", result, tag) |
| 574 | } |
| 575 | |
| 576 | for name, c := range map[string]struct { |
| 577 | object []byte |
| 578 | tag string |
| 579 | want Reason |
| 580 | }{ |
| 581 | "republished under another name": {object, "v1.3.0", TagNameMismatch}, |
| 582 | "unsigned": {[]byte(text), "v1.2.0", Unsigned}, |
| 583 | "signed in the wrong namespace": {[]byte(text + string(approver.Sign([]byte(text), "file", 0x01))), "v1.2.0", WrongNamespace}, |
| 584 | "untouched": {signed(text, 0x00), "v1.2.0", NoUserPresence}, |
| 585 | "a target changed after signing": {bytes.Replace(object, []byte(commit), []byte(strings.Repeat("f", 40)), 1), "v1.2.0", BadSignature}, |
| 586 | "pointing at a tag": {signed(tagText(commit, "tag", "v1.2.0"), 0x01), "v1.2.0", TargetNotCommit}, |
| 587 | "pointing at a tree": {signed(tagText(commit, "tree", "v1.2.0"), 0x01), "v1.2.0", TargetNotCommit}, |
| 588 | "pointing at no object id": {signed(tagText("HEAD", "commit", "v1.2.0"), 0x01), "v1.2.0", Malformed}, |
| 589 | } { |
| 590 | t.Run(name, func(t *testing.T) { |
| 591 | _, tag, err := VerifyTag(a, c.object, c.tag, as(release, Policy{})) |
| 592 | var refusal *Refusal |
| 593 | if !errors.As(err, &refusal) || refusal.Reason != c.want { |
| 594 | t.Fatalf("got %v, want %s", err, c.want) |
| 595 | } |
| 596 | if tag != nil { |
| 597 | t.Errorf("a refusal reported the tag %+v", tag) |
| 598 | } |
| 599 | }) |
| 600 | } |
| 601 | } |