| 1 | package sigverify |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "the-valley/sigverify/internal/skforge" |
| 10 | ) |
| 11 | |
| 12 | var ( |
| 13 | approver = skforge.Ed25519SK("sigverify test approver") |
| 14 | ecdsaSK = skforge.ECDSASK("sigverify test ecdsa approver") |
| 15 | software = skforge.Ed25519("sigverify test software key") |
| 16 | stranger = skforge.Ed25519SK("sigverify test stranger") |
| 17 | |
| 18 | message = []byte("object 0123\ntype commit\ntag v1\n\nrelease v1\n") |
| 19 | when = time.Date(2026, 10, 4, 12, 0, 0, 0, time.UTC) |
| 20 | ) |
| 21 | |
| 22 | // signers is the allowed-signers file every case verifies against. |
| 23 | func signers(t *testing.T) *AllowedSigners { |
| 24 | t.Helper() |
| 25 | text := strings.Join([]string{ |
| 26 | "# comments and blank lines are skipped", |
| 27 | "", |
| 28 | `release@cosmo.invalid namespaces="git,file" ` + approver.PublicLine() + " the approver", |
| 29 | `ecdsa@cosmo.invalid ` + ecdsaSK.PublicLine(), |
| 30 | `ci@cosmo.invalid ` + software.PublicLine(), |
| 31 | }, "\n") |
| 32 | a, err := ParseAllowedSigners("allowed_signers", []byte(text)) |
| 33 | if err != nil { |
| 34 | t.Fatal(err) |
| 35 | } |
| 36 | return a |
| 37 | } |
| 38 | |
| 39 | func TestVerify(t *testing.T) { |
| 40 | gitOpts := Options{Namespace: "git", Time: when} |
| 41 | withPolicy := func(p Policy) Options { o := gitOpts; o.Policy = p; return o } |
| 42 | withPrincipal := func(p string) Options { o := gitOpts; o.Principal = p; return o } |
| 43 | |
| 44 | cases := []struct { |
| 45 | name string |
| 46 | signers string // replaces the default file when set |
| 47 | sig []byte |
| 48 | message []byte |
| 49 | opts Options |
| 50 | want Reason // empty: accepted |
| 51 | principal string |
| 52 | }{ |
| 53 | {name: "a touched sk-ed25519 signature", sig: approver.Sign(message, "git", 0x01), opts: gitOpts, principal: "release@cosmo.invalid"}, |
| 54 | {name: "a touched sk-ecdsa signature", sig: ecdsaSK.Sign(message, "git", 0x01), opts: gitOpts, principal: "ecdsa@cosmo.invalid"}, |
| 55 | {name: "touch and PIN, UV required", sig: approver.Sign(message, "git", 0x05), opts: withPolicy(Policy{RequireUV: true})}, |
| 56 | {name: "a named principal the line lists", sig: approver.Sign(message, "git", 0x01), opts: withPrincipal("release@cosmo.invalid"), principal: "release@cosmo.invalid"}, |
| 57 | {name: "a software key the policy admits", sig: software.Sign(message, "git", 0), opts: withPolicy(Policy{AllowNonSK: true}), principal: "ci@cosmo.invalid"}, |
| 58 | |
| 59 | {name: "untouched: UP clear", sig: approver.Sign(message, "git", 0x00), opts: gitOpts, want: NoUserPresence}, |
| 60 | {name: "untouched sk-ecdsa", sig: ecdsaSK.Sign(message, "git", 0x00), opts: gitOpts, want: NoUserPresence}, |
| 61 | {name: "a PIN is no touch: UV without UP", sig: approver.Sign(message, "git", 0x04), opts: gitOpts, want: NoUserPresence}, |
| 62 | {name: "UP alone when UV is required", sig: approver.Sign(message, "git", 0x01), opts: withPolicy(Policy{RequireUV: true}), want: NoUserVerification}, |
| 63 | {name: "a software key cannot prove UV", sig: software.Sign(message, "git", 0), opts: withPolicy(Policy{AllowNonSK: true, RequireUV: true}), want: NoUserVerification}, |
| 64 | {name: "wrong namespace", sig: approver.Sign(message, "file", 0x01), opts: gitOpts, want: WrongNamespace}, |
| 65 | {name: "a namespace the line does not allow", sig: approver.Sign(message, "ssh-approval", 0x01), opts: Options{Namespace: "ssh-approval", Time: when}, want: NamespaceNotAllowed}, |
| 66 | {name: "signer not in allowed signers", sig: stranger.Sign(message, "git", 0x01), opts: gitOpts, want: UnknownKey}, |
| 67 | {name: "a principal the line does not list", sig: approver.Sign(message, "git", 0x01), opts: withPrincipal("someone@else.invalid"), want: PrincipalNotAllowed}, |
| 68 | {name: "tampered message", sig: approver.Sign(message, "git", 0x01), message: []byte("object 0123\ntype commit\ntag v2\n\nrelease v1\n"), opts: gitOpts, want: BadSignature}, |
| 69 | {name: "software key when sk is required", sig: software.Sign(message, "git", 0), opts: gitOpts, want: NotSecurityKey}, |
| 70 | {name: "not a signature at all", sig: []byte("-----BEGIN PGP SIGNATURE-----\n"), opts: gitOpts, want: Malformed}, |
| 71 | |
| 72 | { |
| 73 | name: "expired signer", |
| 74 | signers: `release@cosmo.invalid valid-before="20260101Z" ` + approver.PublicLine(), |
| 75 | sig: approver.Sign(message, "git", 0x01), opts: gitOpts, want: Expired, |
| 76 | }, |
| 77 | { |
| 78 | name: "not-yet-valid signer", |
| 79 | signers: `release@cosmo.invalid valid-after="20270101Z" ` + approver.PublicLine(), |
| 80 | sig: approver.Sign(message, "git", 0x01), opts: gitOpts, want: NotYetValid, |
| 81 | }, |
| 82 | { |
| 83 | name: "inside the validity window", |
| 84 | signers: `release@cosmo.invalid valid-after="20260101Z",valid-before="20270101Z" ` + approver.PublicLine(), |
| 85 | sig: approver.Sign(message, "git", 0x01), opts: gitOpts, principal: "release@cosmo.invalid", |
| 86 | }, |
| 87 | { |
| 88 | name: "verify-required on the signer's line", |
| 89 | signers: `release@cosmo.invalid verify-required ` + approver.PublicLine(), |
| 90 | sig: approver.Sign(message, "git", 0x01), opts: gitOpts, want: NoUserVerification, |
| 91 | }, |
| 92 | { |
| 93 | name: "verify-required satisfied", |
| 94 | signers: `release@cosmo.invalid verify-required ` + approver.PublicLine(), |
| 95 | sig: approver.Sign(message, "git", 0x05), opts: gitOpts, principal: "release@cosmo.invalid", |
| 96 | }, |
| 97 | { |
| 98 | // Both lines grant this request. The first alone would not |
| 99 | // demand UV, but the second does, and the stricter one holds. |
| 100 | name: "verify-required on any matching line", |
| 101 | signers: `release@cosmo.invalid ` + approver.PublicLine() + "\n" + |
| 102 | `*@cosmo.invalid verify-required ` + approver.PublicLine(), |
| 103 | sig: approver.Sign(message, "git", 0x01), opts: gitOpts, want: NoUserVerification, |
| 104 | }, |
| 105 | { |
| 106 | name: "a certificate authority line is not the key itself", |
| 107 | signers: `release@cosmo.invalid cert-authority ` + approver.PublicLine(), |
| 108 | sig: approver.Sign(message, "git", 0x01), opts: gitOpts, want: UnknownKey, |
| 109 | }, |
| 110 | } |
| 111 | for _, c := range cases { |
| 112 | t.Run(c.name, func(t *testing.T) { |
| 113 | a := signers(t) |
| 114 | if c.signers != "" { |
| 115 | var err error |
| 116 | if a, err = ParseAllowedSigners("allowed_signers", []byte(c.signers)); err != nil { |
| 117 | t.Fatal(err) |
| 118 | } |
| 119 | } |
| 120 | msg := message |
| 121 | if c.message != nil { |
| 122 | msg = c.message |
| 123 | } |
| 124 | result, err := Verify(a, msg, c.sig, c.opts) |
| 125 | if c.want == "" { |
| 126 | if err != nil { |
| 127 | t.Fatalf("refused: %v", err) |
| 128 | } |
| 129 | if c.principal != "" && result.Principal != c.principal { |
| 130 | t.Errorf("principal %q, want %q", result.Principal, c.principal) |
| 131 | } |
| 132 | return |
| 133 | } |
| 134 | var refusal *Refusal |
| 135 | if !errors.As(err, &refusal) { |
| 136 | t.Fatalf("got %v, %v; want a %s refusal", result, err, c.want) |
| 137 | } |
| 138 | if refusal.Reason != c.want { |
| 139 | t.Fatalf("refused for %s (%s), want %s", refusal.Reason, refusal.Detail, c.want) |
| 140 | } |
| 141 | }) |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | // The refusal for an untouched signature names the key, because a genuine |
| 146 | // signature without a touch means something is using that key. |
| 147 | func TestNoUserPresenceNamesTheKey(t *testing.T) { |
| 148 | _, err := Verify(signers(t), message, approver.Sign(message, "git", 0), Options{Namespace: "git"}) |
| 149 | var refusal *Refusal |
| 150 | if !errors.As(err, &refusal) || refusal.Signer == nil { |
| 151 | t.Fatalf("got %v", err) |
| 152 | } |
| 153 | if !refusal.Signer.SecurityKey || refusal.Signer.Flags != 0 || refusal.Signer.Counter != skforge.Counter { |
| 154 | t.Errorf("signer %+v", *refusal.Signer) |
| 155 | } |
| 156 | if !strings.Contains(refusal.Detail, refusal.Signer.Fingerprint) { |
| 157 | t.Errorf("the detail does not name the key: %s", refusal.Detail) |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | func TestParseAllowedSignersRefuses(t *testing.T) { |
| 162 | key := approver.PublicLine() |
| 163 | cases := map[string]string{ |
| 164 | "no-touch-required": `a@b no-touch-required ` + key, |
| 165 | "an unknown option": `a@b permit-everything ` + key, |
| 166 | "an unquoted value": `a@b namespaces=git ` + key, |
| 167 | "an unclosed quote": `a@b namespaces="git ` + key, |
| 168 | "a repeated option": `a@b namespaces="git",namespaces="file" ` + key, |
| 169 | "a trailing comma": `a@b namespaces="git", ` + key, |
| 170 | "an inverted window": `a@b valid-after="20270101",valid-before="20260101" ` + key, |
| 171 | "a bad time": `a@b valid-after="2026-01-01" ` + key, |
| 172 | "no key": `a@b namespaces="git"`, |
| 173 | "principals alone": `a@b`, |
| 174 | "a mislabelled key": `a@b ssh-ed25519 ` + strings.Fields(key)[1], |
| 175 | "an unsupported type": `a@b ssh-dss AAAAB3NzaC1kc3MAAACBAP`, |
| 176 | "a key that is not b64": `a@b ` + approver.Type + ` !!!`, |
| 177 | } |
| 178 | for name, line := range cases { |
| 179 | t.Run(name, func(t *testing.T) { |
| 180 | if _, err := ParseAllowedSigners("f", []byte(line+"\n")); err == nil { |
| 181 | t.Fatalf("accepted %q", line) |
| 182 | } else if !strings.HasPrefix(err.Error(), "f:1: ") { |
| 183 | t.Errorf("error does not name the line: %v", err) |
| 184 | } |
| 185 | }) |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | func TestParseAllowedSignersAccepts(t *testing.T) { |
| 190 | key := approver.PublicLine() |
| 191 | for _, line := range []string{ |
| 192 | `a@b ` + key, |
| 193 | `a@b ` + key + ` a comment with spaces`, |
| 194 | ` a@b,c@d ` + key, |
| 195 | `"a@b,c d" ` + key, |
| 196 | `a@b NAMESPACES="git",Valid-After="202601011200Z" ` + key, |
| 197 | `a@b namespaces="g\"it" ` + key, |
| 198 | `a@b cert-authority,valid-before="20990101010101" ` + key, |
| 199 | "a@b\t" + key, |
| 200 | } { |
| 201 | if _, err := ParseAllowedSigners("f", []byte(line)); err != nil { |
| 202 | t.Errorf("%q: %v", line, err) |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | func TestMatchPatternList(t *testing.T) { |
| 208 | cases := []struct { |
| 209 | s, list string |
| 210 | want int |
| 211 | }{ |
| 212 | {"a@b", "a@b", 1}, |
| 213 | {"a@b", "x@y,a@b", 1}, |
| 214 | {"a@b", "*@b", 1}, |
| 215 | {"a@b", "?@b", 1}, |
| 216 | {"aa@b", "?@b", 0}, |
| 217 | {"a@b", "*", 1}, |
| 218 | {"a@b", "*@b,!a@b", -1}, |
| 219 | {"c@b", "*@b,!a@b", 1}, |
| 220 | {"a@b", "!a@b", -1}, |
| 221 | {"c@b", "!a@b", 0}, |
| 222 | {"A@b", "a@b", 0}, |
| 223 | {"a@b.c", "a@*.c", 1}, |
| 224 | {"a@bxc", "a@*.c", 0}, |
| 225 | {"abcbd", "a*b*d", 1}, |
| 226 | {"abcbe", "a*b*d", 0}, |
| 227 | {"", "", 0}, |
| 228 | } |
| 229 | for _, c := range cases { |
| 230 | if got := matchPatternList(c.s, c.list); got != c.want { |
| 231 | t.Errorf("matchPatternList(%q, %q) = %d, want %d", c.s, c.list, got, c.want) |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | func TestSplitTag(t *testing.T) { |
| 237 | sig := "-----BEGIN SSH SIGNATURE-----\nU1NIU0lH\n-----END SSH SIGNATURE-----\n" |
| 238 | header := "object 0123456789012345678901234567890123456789\ntype commit\ntag v1\ntagger T <t@t> 1 +0000\n" |
| 239 | // A line that merely looks like a signature, earlier in the message, |
| 240 | // is part of the payload: git splits at the last one. |
| 241 | body := "\nrelease\n-----BEGIN SSH SIGNATURE-----\nnot really\n" |
| 242 | payload, signature := SplitTag([]byte(header + body + sig)) |
| 243 | if string(payload) != header+body || string(signature) != sig { |
| 244 | t.Errorf("payload %q\nsignature %q", payload, signature) |
| 245 | } |
| 246 | |
| 247 | // A signature for the other hash algorithm rides in a gpgsig-sha256 |
| 248 | // header, and git takes it out of the payload. |
| 249 | other := "gpgsig-sha256 -----BEGIN SSH SIGNATURE-----\n U1NIU0lH\n -----END SSH SIGNATURE-----\n" |
| 250 | payload, _ = SplitTag([]byte(header + other + body + sig)) |
| 251 | if string(payload) != header+body { |
| 252 | t.Errorf("payload %q", payload) |
| 253 | } |
| 254 | |
| 255 | if _, signature := SplitTag([]byte(header + "\nunsigned\n")); signature != nil { |
| 256 | t.Errorf("an unsigned tag has signature %q", signature) |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | func TestVerifyTag(t *testing.T) { |
| 261 | a := signers(t) |
| 262 | tag := "object 0123456789012345678901234567890123456789\ntype commit\ntag v1.2.0\ntagger T <t@t> 1 +0000\n\nrelease\n" |
| 263 | object := []byte(tag + string(approver.Sign([]byte(tag), "git", 0x01))) |
| 264 | |
| 265 | result, header, err := VerifyTag(a, object, "v1.2.0", Options{Time: when}) |
| 266 | if err != nil { |
| 267 | t.Fatal(err) |
| 268 | } |
| 269 | if result.Principal != "release@cosmo.invalid" || header.Object != "0123456789012345678901234567890123456789" || header.ObjectType != "commit" { |
| 270 | t.Errorf("result %+v, tag %+v", result, header) |
| 271 | } |
| 272 | |
| 273 | for name, c := range map[string]struct { |
| 274 | object []byte |
| 275 | tag string |
| 276 | want Reason |
| 277 | }{ |
| 278 | "republished under another name": {object, "v1.3.0", TagNameMismatch}, |
| 279 | "unsigned": {[]byte(tag), "v1.2.0", Unsigned}, |
| 280 | "signed in the wrong namespace": {[]byte(tag + string(approver.Sign([]byte(tag), "file", 0x01))), "v1.2.0", WrongNamespace}, |
| 281 | "untouched": {[]byte(tag + string(approver.Sign([]byte(tag), "git", 0x00))), "v1.2.0", NoUserPresence}, |
| 282 | } { |
| 283 | t.Run(name, func(t *testing.T) { |
| 284 | _, _, err := VerifyTag(a, c.object, c.tag, Options{Time: when}) |
| 285 | var refusal *Refusal |
| 286 | if !errors.As(err, &refusal) || refusal.Reason != c.want { |
| 287 | t.Fatalf("got %v, want %s", err, c.want) |
| 288 | } |
| 289 | }) |
| 290 | } |
| 291 | } |