Debian Codegraph @

plumbing/object/signature.go

raw
1 package object
2
3 import (
4 "bytes"
5 "io"
6
7 "github.com/go-git/go-git/v5/plumbing"
8 "github.com/go-git/go-git/v5/utils/ioutil"
9 "github.com/go-git/go-git/v5/utils/sync"
10 )
11
12 const (
13 signatureTypeUnknown signatureType = iota
14 signatureTypeOpenPGP
15 signatureTypeX509
16 signatureTypeSSH
17 )
18
19 var (
20 // openPGPSignatureFormat is the format of an OpenPGP signature.
21 openPGPSignatureFormat = signatureFormat{
22 []byte("-----BEGIN PGP SIGNATURE-----"),
23 []byte("-----BEGIN PGP MESSAGE-----"),
24 }
25 // x509SignatureFormat is the format of an X509 signature, which is
26 // a PKCS#7 (S/MIME) signature.
27 x509SignatureFormat = signatureFormat{
28 []byte("-----BEGIN CERTIFICATE-----"),
29 []byte("-----BEGIN SIGNED MESSAGE-----"),
30 }
31
32 // sshSignatureFormat is the format of an SSH signature.
33 sshSignatureFormat = signatureFormat{
34 []byte("-----BEGIN SSH SIGNATURE-----"),
35 }
36 )
37
38 var (
39 // knownSignatureFormats is a map of known signature formats, indexed by
40 // their signatureType.
41 knownSignatureFormats = map[signatureType]signatureFormat{
42 signatureTypeOpenPGP: openPGPSignatureFormat,
43 signatureTypeX509: x509SignatureFormat,
44 signatureTypeSSH: sshSignatureFormat,
45 }
46 )
47
48 // signatureType represents the type of the signature.
49 type signatureType int8
50
51 // signatureFormat represents the beginning of a signature.
52 type signatureFormat [][]byte
53
54 // typeForSignature returns the type of the signature based on its format.
55 func typeForSignature(b []byte) signatureType {
56 for t, i := range knownSignatureFormats {
57 for _, begin := range i {
58 if bytes.HasPrefix(b, begin) {
59 return t
60 }
61 }
62 }
63 return signatureTypeUnknown
64 }
65
66 // parseSignedBytes returns the position of the last signature block found in
67 // the given bytes. If no signature block is found, it returns -1.
68 //
69 // When multiple signature blocks are found, the position of the last one is
70 // returned. Any tailing bytes after this signature block start should be
71 // considered part of the signature.
72 //
73 // Given this, it would be safe to use the returned position to split the bytes
74 // into two parts: the first part containing the message, the second part
75 // containing the signature.
76 //
77 // Example:
78 //
79 // message := []byte(`Message with signature
80 //
81 // -----BEGIN SSH SIGNATURE-----
82 // ...`)
83 //
84 // var signature string
85 // if pos, _ := parseSignedBytes(message); pos != -1 {
86 // signature = string(message[pos:])
87 // message = message[:pos]
88 // }
89 //
90 // This logic is on par with git's gpg-interface.c:parse_signed_buffer().
91 // https://github.com/git/git/blob/7c2ef319c52c4997256f5807564523dfd4acdfc7/gpg-interface.c#L668
92 func parseSignedBytes(b []byte) (int, signatureType) {
93 var n, match = 0, -1
94 var t signatureType
95 for n < len(b) {
96 var i = b[n:]
97 if st := typeForSignature(i); st != signatureTypeUnknown {
98 match = n
99 t = st
100 }
101 if eol := bytes.IndexByte(i, '\n'); eol >= 0 {
102 n += eol + 1
103 continue
104 }
105 // If we reach this point, we've reached the end.
106 break
107 }
108 return match, t
109 }
110
111 // countSignatureBlocks reports how many distinct armored signature blocks
112 // start at a line boundary in b. Used by verification paths to reject
113 // multi-signature payloads, matching upstream's check in gpg-interface.c
114 // where parse_gpg_output bails out the first time it sees a second
115 // exclusive status line (a second GOODSIG/BADSIG/etc.).
116 func countSignatureBlocks(b []byte) int {
117 n, count := 0, 0
118 for n < len(b) {
119 i := b[n:]
120 if typeForSignature(i) != signatureTypeUnknown {
121 count++
122 }
123 if eol := bytes.IndexByte(i, '\n'); eol >= 0 {
124 n += eol + 1
125 continue
126 }
127 break
128 }
129 return count
130 }
131
132 // isSignatureHeader reports whether line is a canonical "gpgsig "/
133 // "gpgsig-sha256 " header line. Other "gpgsig"-prefixed extra headers
134 // are intentionally not matched.
135 func isSignatureHeader(line []byte) bool {
136 return bytes.HasPrefix(line, []byte(headerpgp+" ")) ||
137 bytes.HasPrefix(line, []byte(headerpgp256+" "))
138 }
139
140 // stripObjectSignatures streams src into dst, producing the byte sequence
141 // over which a PGP/GPG signature is computed:
142 //
143 // - Canonical "gpgsig" and "gpgsig-sha256" headers (and their
144 // continuation lines) are dropped, mirroring upstream's
145 // remove_signature in commit.c.
146 // - For tag objects, the inline trailing PGP signature is additionally
147 // truncated, mirroring upstream's parse_signature in gpg-interface.c
148 // used by gpg_verify_tag.
149 //
150 // The returned object's type is set to objType. Used by both
151 // Commit.EncodeWithoutSignature and Tag.EncodeWithoutSignature to
152 // reproduce the exact bytes the signature was computed over.
153 func stripObjectSignatures(dst, src plumbing.EncodedObject, objType plumbing.ObjectType) (err error) {
154 dst.SetType(objType)
155
156 r, err := src.Reader()
157 if err != nil {
158 return err
159 }
160 defer ioutil.CheckClose(r, &err)
161
162 var input io.Reader = r
163 if objType == plumbing.TagObject {
164 raw, err := io.ReadAll(r)
165 if err != nil {
166 return err
167 }
168 if sm, _ := parseSignedBytes(raw); sm >= 0 {
169 raw = raw[:sm]
170 }
171 input = bytes.NewReader(raw)
172 }
173
174 w, err := dst.Writer()
175 if err != nil {
176 return err
177 }
178 defer ioutil.CheckClose(w, &err)
179
180 return stripHeaderSignatures(w, input)
181 }
182
183 // stripHeaderSignatures copies r to w, dropping canonical signature header
184 // lines (gpgsig and gpgsig-sha256) and their continuation lines. Lines
185 // past the blank line that closes the header block are copied verbatim.
186 func stripHeaderSignatures(w io.Writer, r io.Reader) error {
187 br := sync.GetBufioReader(r)
188 defer sync.PutBufioReader(br)
189
190 var inBody, skipping bool
191 for {
192 line, rerr := br.ReadBytes('\n')
193 if rerr != nil && rerr != io.EOF {
194 return rerr
195 }
196
197 write := true
198 if !inBody {
199 switch {
200 case skipping && len(line) > 0 && line[0] == ' ':
201 write = false
202 case isSignatureHeader(line):
203 skipping = true
204 write = false
205 case len(line) == 1 && line[0] == '\n':
206 skipping = false
207 inBody = true
208 default:
209 skipping = false
210 }
211 }
212
213 if write && len(line) > 0 {
214 if _, werr := w.Write(line); werr != nil {
215 return werr
216 }
217 }
218 if rerr == io.EOF {
219 return nil
220 }
221 }
222 }