efe2c0d3863c7b4118d08c4152dbc6e1d5dcc09f
1#include "cache.h"
2#include "config.h"
3#include "run-command.h"
4#include "strbuf.h"
5#include "gpg-interface.h"
6#include "sigchain.h"
7#include "tempfile.h"
8
9static char *configured_signing_key;
10struct gpg_format {
11 const char *name;
12 const char *program;
13 const char **verify_args;
14 const char **sigs;
15};
16
17static const char *openpgp_verify_args[] = {
18 "--keyid-format=long",
19 NULL
20};
21static const char *openpgp_sigs[] = {
22 "-----BEGIN PGP SIGNATURE-----",
23 "-----BEGIN PGP MESSAGE-----",
24 NULL
25};
26
27static const char *x509_verify_args[] = {
28 NULL
29};
30static const char *x509_sigs[] = {
31 "-----BEGIN SIGNED MESSAGE-----",
32 NULL
33};
34
35static struct gpg_format gpg_format[] = {
36 { .name = "openpgp", .program = "gpg",
37 .verify_args = openpgp_verify_args,
38 .sigs = openpgp_sigs
39 },
40 { .name = "x509", .program = "gpgsm",
41 .verify_args = x509_verify_args,
42 .sigs = x509_sigs
43 },
44};
45
46static struct gpg_format *use_format = &gpg_format[0];
47
48static struct gpg_format *get_format_by_name(const char *str)
49{
50 int i;
51
52 for (i = 0; i < ARRAY_SIZE(gpg_format); i++)
53 if (!strcmp(gpg_format[i].name, str))
54 return gpg_format + i;
55 return NULL;
56}
57
58static struct gpg_format *get_format_by_sig(const char *sig)
59{
60 int i, j;
61
62 for (i = 0; i < ARRAY_SIZE(gpg_format); i++)
63 for (j = 0; gpg_format[i].sigs[j]; j++)
64 if (starts_with(sig, gpg_format[i].sigs[j]))
65 return gpg_format + i;
66 return NULL;
67}
68
69void signature_check_clear(struct signature_check *sigc)
70{
71 FREE_AND_NULL(sigc->payload);
72 FREE_AND_NULL(sigc->gpg_output);
73 FREE_AND_NULL(sigc->gpg_status);
74 FREE_AND_NULL(sigc->signer);
75 FREE_AND_NULL(sigc->key);
76}
77
78/* An exclusive status -- only one of them can appear in output */
79#define GPG_STATUS_EXCLUSIVE (1<<0)
80
81static struct {
82 char result;
83 const char *check;
84 unsigned int flags;
85} sigcheck_gpg_status[] = {
86 { 'G', "GOODSIG ", GPG_STATUS_EXCLUSIVE },
87 { 'B', "BADSIG ", GPG_STATUS_EXCLUSIVE },
88 { 'U', "TRUST_NEVER", 0 },
89 { 'U', "TRUST_UNDEFINED", 0 },
90 { 'E', "ERRSIG ", GPG_STATUS_EXCLUSIVE },
91 { 'X', "EXPSIG ", GPG_STATUS_EXCLUSIVE },
92 { 'Y', "EXPKEYSIG ", GPG_STATUS_EXCLUSIVE },
93 { 'R', "REVKEYSIG ", GPG_STATUS_EXCLUSIVE },
94};
95
96static void parse_gpg_output(struct signature_check *sigc)
97{
98 const char *buf = sigc->gpg_status;
99 const char *line, *next;
100 int i;
101 int seen_exclusive_status = 0;
102
103 /* Iterate over all lines */
104 for (line = buf; *line; line = strchrnul(line+1, '\n')) {
105 while (*line == '\n')
106 line++;
107 /* Skip lines that don't start with GNUPG status */
108 if (!skip_prefix(line, "[GNUPG:] ", &line))
109 continue;
110
111 /* Iterate over all search strings */
112 for (i = 0; i < ARRAY_SIZE(sigcheck_gpg_status); i++) {
113 if (skip_prefix(line, sigcheck_gpg_status[i].check, &line)) {
114 if (sigcheck_gpg_status[i].flags & GPG_STATUS_EXCLUSIVE) {
115 if (++seen_exclusive_status > 1)
116 goto found_duplicate_status;
117 }
118
119 sigc->result = sigcheck_gpg_status[i].result;
120 /* The trust messages are not followed by key/signer information */
121 if (sigc->result != 'U') {
122 next = strchrnul(line, ' ');
123 free(sigc->key);
124 sigc->key = xmemdupz(line, next - line);
125 /* The ERRSIG message is not followed by signer information */
126 if (*next && sigc->result != 'E') {
127 line = next + 1;
128 next = strchrnul(line, '\n');
129 free(sigc->signer);
130 sigc->signer = xmemdupz(line, next - line);
131 }
132 }
133
134 break;
135 }
136 }
137 }
138 return;
139
140found_duplicate_status:
141 /*
142 * GOODSIG, BADSIG etc. can occur only once for each signature.
143 * Therefore, if we had more than one then we're dealing with multiple
144 * signatures. We don't support them currently, and they're rather
145 * hard to create, so something is likely fishy and we should reject
146 * them altogether.
147 */
148 sigc->result = 'E';
149 /* Clear partial data to avoid confusion */
150 FREE_AND_NULL(sigc->signer);
151 FREE_AND_NULL(sigc->key);
152}
153
154int check_signature(const char *payload, size_t plen, const char *signature,
155 size_t slen, struct signature_check *sigc)
156{
157 struct strbuf gpg_output = STRBUF_INIT;
158 struct strbuf gpg_status = STRBUF_INIT;
159 int status;
160
161 sigc->result = 'N';
162
163 status = verify_signed_buffer(payload, plen, signature, slen,
164 &gpg_output, &gpg_status);
165 if (status && !gpg_output.len)
166 goto out;
167 sigc->payload = xmemdupz(payload, plen);
168 sigc->gpg_output = strbuf_detach(&gpg_output, NULL);
169 sigc->gpg_status = strbuf_detach(&gpg_status, NULL);
170 parse_gpg_output(sigc);
171 status |= sigc->result != 'G' && sigc->result != 'U';
172
173 out:
174 strbuf_release(&gpg_status);
175 strbuf_release(&gpg_output);
176
177 return !!status;
178}
179
180void print_signature_buffer(const struct signature_check *sigc, unsigned flags)
181{
182 const char *output = flags & GPG_VERIFY_RAW ?
183 sigc->gpg_status : sigc->gpg_output;
184
185 if (flags & GPG_VERIFY_VERBOSE && sigc->payload)
186 fputs(sigc->payload, stdout);
187
188 if (output)
189 fputs(output, stderr);
190}
191
192size_t parse_signature(const char *buf, size_t size)
193{
194 size_t len = 0;
195 size_t match = size;
196 while (len < size) {
197 const char *eol;
198
199 if (get_format_by_sig(buf + len))
200 match = len;
201
202 eol = memchr(buf + len, '\n', size - len);
203 len += eol ? eol - (buf + len) + 1 : size - len;
204 }
205 return match;
206}
207
208void set_signing_key(const char *key)
209{
210 free(configured_signing_key);
211 configured_signing_key = xstrdup(key);
212}
213
214int git_gpg_config(const char *var, const char *value, void *cb)
215{
216 struct gpg_format *fmt = NULL;
217 char *fmtname = NULL;
218
219 if (!strcmp(var, "user.signingkey")) {
220 if (!value)
221 return config_error_nonbool(var);
222 set_signing_key(value);
223 return 0;
224 }
225
226 if (!strcmp(var, "gpg.format")) {
227 if (!value)
228 return config_error_nonbool(var);
229 fmt = get_format_by_name(value);
230 if (!fmt)
231 return error("unsupported value for %s: %s",
232 var, value);
233 use_format = fmt;
234 return 0;
235 }
236
237 if (!strcmp(var, "gpg.program") || !strcmp(var, "gpg.openpgp.program"))
238 fmtname = "openpgp";
239
240 if (!strcmp(var, "gpg.x509.program"))
241 fmtname = "x509";
242
243 if (fmtname) {
244 fmt = get_format_by_name(fmtname);
245 return git_config_string(&fmt->program, var, value);
246 }
247
248 return 0;
249}
250
251const char *get_signing_key(void)
252{
253 if (configured_signing_key)
254 return configured_signing_key;
255 return git_committer_info(IDENT_STRICT|IDENT_NO_DATE);
256}
257
258int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *signing_key)
259{
260 struct child_process gpg = CHILD_PROCESS_INIT;
261 int ret;
262 size_t i, j, bottom;
263 struct strbuf gpg_status = STRBUF_INIT;
264
265 argv_array_pushl(&gpg.args,
266 use_format->program,
267 "--status-fd=2",
268 "-bsau", signing_key,
269 NULL);
270
271 bottom = signature->len;
272
273 /*
274 * When the username signingkey is bad, program could be terminated
275 * because gpg exits without reading and then write gets SIGPIPE.
276 */
277 sigchain_push(SIGPIPE, SIG_IGN);
278 ret = pipe_command(&gpg, buffer->buf, buffer->len,
279 signature, 1024, &gpg_status, 0);
280 sigchain_pop(SIGPIPE);
281
282 ret |= !strstr(gpg_status.buf, "\n[GNUPG:] SIG_CREATED ");
283 strbuf_release(&gpg_status);
284 if (ret)
285 return error(_("gpg failed to sign the data"));
286
287 /* Strip CR from the line endings, in case we are on Windows. */
288 for (i = j = bottom; i < signature->len; i++)
289 if (signature->buf[i] != '\r') {
290 if (i != j)
291 signature->buf[j] = signature->buf[i];
292 j++;
293 }
294 strbuf_setlen(signature, j);
295
296 return 0;
297}
298
299int verify_signed_buffer(const char *payload, size_t payload_size,
300 const char *signature, size_t signature_size,
301 struct strbuf *gpg_output, struct strbuf *gpg_status)
302{
303 struct child_process gpg = CHILD_PROCESS_INIT;
304 struct gpg_format *fmt;
305 struct tempfile *temp;
306 int ret;
307 struct strbuf buf = STRBUF_INIT;
308
309 temp = mks_tempfile_t(".git_vtag_tmpXXXXXX");
310 if (!temp)
311 return error_errno(_("could not create temporary file"));
312 if (write_in_full(temp->fd, signature, signature_size) < 0 ||
313 close_tempfile_gently(temp) < 0) {
314 error_errno(_("failed writing detached signature to '%s'"),
315 temp->filename.buf);
316 delete_tempfile(&temp);
317 return -1;
318 }
319
320 fmt = get_format_by_sig(signature);
321 if (!fmt)
322 BUG("bad signature '%s'", signature);
323
324 argv_array_push(&gpg.args, fmt->program);
325 argv_array_pushv(&gpg.args, fmt->verify_args);
326 argv_array_pushl(&gpg.args,
327 "--status-fd=1",
328 "--verify", temp->filename.buf, "-",
329 NULL);
330
331 if (!gpg_status)
332 gpg_status = &buf;
333
334 sigchain_push(SIGPIPE, SIG_IGN);
335 ret = pipe_command(&gpg, payload, payload_size,
336 gpg_status, 0, gpg_output, 0);
337 sigchain_pop(SIGPIPE);
338
339 delete_tempfile(&temp);
340
341 ret |= !strstr(gpg_status->buf, "\n[GNUPG:] GOODSIG ");
342 strbuf_release(&buf); /* no matter it was used or not */
343
344 return ret;
345}