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