gunk-dev/the-valley

gittag.go

sourcefe4ab5bf3cabmain's published tip history
5.0 KiB
1package sigverify
2
3import (
4 "bytes"
5 "encoding/hex"
6 "errors"
7 "fmt"
8)
9
10// GitNamespace is the namespace git signs and verifies objects in.
11const GitNamespace = "git"
12
13// signatureHeaders are the lines that open a signature in a git object, for
14// every signature format git knows (gpg-interface.c). git splits an object
15// at the last line that opens with any of them, whatever format it is
16// configured to use, so this does too.
17var signatureHeaders = [][]byte{
18 []byte("-----BEGIN PGP SIGNATURE-----"),
19 []byte("-----BEGIN PGP MESSAGE-----"),
20 []byte("-----BEGIN SIGNED MESSAGE-----"),
21 []byte(armorBegin),
22}
23
24// SplitTag separates a raw git tag object into the payload its signature
25// covers and the signature, the way git's parse_signature does. The
26// signature starts at the last line that opens with a signature header and
27// runs to the end of the object. The payload is everything before it, less
28// any gpgsig or gpgsig-sha256 header, which carries a signature made for
29// the repository's other hash algorithm. signature is nil when the object
30// is unsigned.
31func SplitTag(object []byte) (payload, signature []byte) {
32 match := len(object)
33 for start := 0; start < len(object); {
34 line := object[start:]
35 for _, h := range signatureHeaders {
36 if bytes.HasPrefix(line, h) {
37 match = start
38 }
39 }
40 next := bytes.IndexByte(line, '\n')
41 if next < 0 {
42 break
43 }
44 start += next + 1
45 }
46 if match == len(object) {
47 return object, nil
48 }
49 return removeSignatureHeaders(object[:match]), object[match:]
50}
51
52// removeSignatureHeaders drops gpgsig and gpgsig-sha256 headers, and
53// their space-indented continuation lines, from the header block of a git
54// object, as git's remove_signature does.
55func removeSignatureHeaders(object []byte) []byte {
56 var out []byte
57 inSignature := false
58 for start := 0; start < len(object); {
59 end := bytes.IndexByte(object[start:], '\n')
60 if end < 0 {
61 end = len(object)
62 } else {
63 end += start + 1
64 }
65 line := object[start:end]
66 switch {
67 case inSignature && bytes.HasPrefix(line, []byte(" ")):
68 case bytes.HasPrefix(line, []byte("gpgsig ")) || bytes.HasPrefix(line, []byte("gpgsig-sha256 ")):
69 inSignature = true
70 case bytes.Equal(line, []byte("\n")):
71 // The header block ends here, and the message is kept whole.
72 return append(out, object[start:]...)
73 default:
74 inSignature = false
75 out = append(out, line...)
76 }
77 start = end
78 }
79 return out
80}
81
82// Tag is the header of a signed git tag object.
83type Tag struct {
84 Name string // the name the tag was signed with
85 Object string // the id of the object it points to
86 ObjectType string // that object's type, usually "commit"
87}
88
89// VerifyTag verifies the SSH signature on a raw git tag object, as
90// `git verify-tag` would, and then checks that the tag was signed with the
91// name it is being verified under. A signature covers the tag's name, but a
92// ref can point at any tag object: without this check, a signed tag for an
93// old release could be republished under a new release's name.
94//
95// The namespace is always "git", whatever opts says. The verify time is
96// opts.Time, or now, and never the tagger date. git verifies at the tagger
97// date, but the signer chooses that date, so a key past its valid-before
98// could backdate a tag.
99func VerifyTag(signers *AllowedSigners, object []byte, name string, opts Options) (*Result, *Tag, error) {
100 if name == "" {
101 return nil, nil, errors.New("sigverify: no tag name given")
102 }
103 payload, signature := SplitTag(object)
104 if signature == nil {
105 return nil, nil, &Refusal{Reason: Unsigned, Detail: "the tag object carries no signature"}
106 }
107 tag, err := parseTagHeader(payload)
108 if err != nil {
109 return nil, nil, &Refusal{Reason: Malformed, Detail: "the tag object " + err.Error()}
110 }
111 opts.Namespace = GitNamespace
112 result, err := Verify(signers, payload, signature, opts)
113 if err != nil {
114 return nil, tag, err
115 }
116 if tag.Name != name {
117 return nil, tag, &Refusal{
118 Reason: TagNameMismatch,
119 Detail: fmt.Sprintf("the tag was signed as %q and is being verified as %q", tag.Name, name),
120 Signer: &result.Signer,
121 }
122 }
123 return result, tag, nil
124}
125
126// parseTagHeader reads the object, type and tag lines that open a tag
127// object, in the order git writes and requires them.
128func parseTagHeader(payload []byte) (*Tag, error) {
129 header, _, _ := bytes.Cut(payload, []byte("\n\n"))
130 lines := bytes.Split(header, []byte("\n"))
131 field := func(i int, name string) (string, error) {
132 if i >= len(lines) || !bytes.HasPrefix(lines[i], []byte(name+" ")) {
133 return "", fmt.Errorf("has no %q header where git puts one", name)
134 }
135 return string(lines[i][len(name)+1:]), nil
136 }
137 tag := &Tag{}
138 var err error
139 if tag.Object, err = field(0, "object"); err != nil {
140 return nil, err
141 }
142 if _, err := hex.DecodeString(tag.Object); err != nil || (len(tag.Object) != 40 && len(tag.Object) != 64) {
143 return nil, fmt.Errorf("points at %q, which is not an object id", tag.Object)
144 }
145 if tag.ObjectType, err = field(1, "type"); err != nil {
146 return nil, err
147 }
148 if tag.Name, err = field(2, "tag"); err != nil {
149 return nil, err
150 }
151 return tag, nil
152}