]> git.ipfire.org Git - thirdparty/git.git/blame - gpg-interface.c
branch: don't mix --edit-description
[thirdparty/git.git] / gpg-interface.c
CommitLineData
2f47eae2 1#include "cache.h"
b2141fc1 2#include "config.h"
2f47eae2
JH
3#include "run-command.h"
4#include "strbuf.h"
5#include "gpg-interface.h"
6#include "sigchain.h"
4322353b 7#include "tempfile.h"
2f47eae2
JH
8
9static char *configured_signing_key;
54887b46
HJI
10static enum signature_trust_level configured_min_trust_level = TRUST_UNDEFINED;
11
58af57e1
HS
12struct gpg_format {
13 const char *name;
14 const char *program;
15 const char **verify_args;
16 const char **sigs;
17};
18
19static const char *openpgp_verify_args[] = {
20 "--keyid-format=long",
21 NULL
22};
23static const char *openpgp_sigs[] = {
24 "-----BEGIN PGP SIGNATURE-----",
25 "-----BEGIN PGP MESSAGE-----",
26 NULL
27};
28
1e7adb97
HS
29static const char *x509_verify_args[] = {
30 NULL
31};
32static const char *x509_sigs[] = {
33 "-----BEGIN SIGNED MESSAGE-----",
34 NULL
35};
36
58af57e1
HS
37static struct gpg_format gpg_format[] = {
38 { .name = "openpgp", .program = "gpg",
39 .verify_args = openpgp_verify_args,
40 .sigs = openpgp_sigs
41 },
1e7adb97
HS
42 { .name = "x509", .program = "gpgsm",
43 .verify_args = x509_verify_args,
44 .sigs = x509_sigs
45 },
58af57e1
HS
46};
47
48static struct gpg_format *use_format = &gpg_format[0];
2f47eae2 49
58af57e1
HS
50static struct gpg_format *get_format_by_name(const char *str)
51{
52 int i;
53
54 for (i = 0; i < ARRAY_SIZE(gpg_format); i++)
55 if (!strcmp(gpg_format[i].name, str))
56 return gpg_format + i;
57 return NULL;
58}
59
60static struct gpg_format *get_format_by_sig(const char *sig)
61{
62 int i, j;
63
64 for (i = 0; i < ARRAY_SIZE(gpg_format); i++)
65 for (j = 0; gpg_format[i].sigs[j]; j++)
66 if (starts_with(sig, gpg_format[i].sigs[j]))
67 return gpg_format + i;
68 return NULL;
69}
d7c67668 70
01e57b5d
MG
71void signature_check_clear(struct signature_check *sigc)
72{
88ce3ef6
ÆAB
73 FREE_AND_NULL(sigc->payload);
74 FREE_AND_NULL(sigc->gpg_output);
75 FREE_AND_NULL(sigc->gpg_status);
76 FREE_AND_NULL(sigc->signer);
77 FREE_AND_NULL(sigc->key);
3daaaabe 78 FREE_AND_NULL(sigc->fingerprint);
4de9394d 79 FREE_AND_NULL(sigc->primary_key_fingerprint);
01e57b5d
MG
80}
81
da6cf1b3
MG
82/* An exclusive status -- only one of them can appear in output */
83#define GPG_STATUS_EXCLUSIVE (1<<0)
0b11a84e
MG
84/* The status includes key identifier */
85#define GPG_STATUS_KEYID (1<<1)
86/* The status includes user identifier */
87#define GPG_STATUS_UID (1<<2)
3daaaabe
MG
88/* The status includes key fingerprints */
89#define GPG_STATUS_FINGERPRINT (1<<3)
54887b46
HJI
90/* The status includes trust level */
91#define GPG_STATUS_TRUST_LEVEL (1<<4)
0b11a84e
MG
92
93/* Short-hand for standard exclusive *SIG status with keyid & UID */
94#define GPG_STATUS_STDSIG (GPG_STATUS_EXCLUSIVE|GPG_STATUS_KEYID|GPG_STATUS_UID)
da6cf1b3 95
a50e7ca3
JH
96static struct {
97 char result;
98 const char *check;
da6cf1b3 99 unsigned int flags;
a50e7ca3 100} sigcheck_gpg_status[] = {
0b11a84e
MG
101 { 'G', "GOODSIG ", GPG_STATUS_STDSIG },
102 { 'B', "BADSIG ", GPG_STATUS_STDSIG },
0b11a84e
MG
103 { 'E', "ERRSIG ", GPG_STATUS_EXCLUSIVE|GPG_STATUS_KEYID },
104 { 'X', "EXPSIG ", GPG_STATUS_STDSIG },
105 { 'Y', "EXPKEYSIG ", GPG_STATUS_STDSIG },
106 { 'R', "REVKEYSIG ", GPG_STATUS_STDSIG },
3daaaabe 107 { 0, "VALIDSIG ", GPG_STATUS_FINGERPRINT },
54887b46
HJI
108 { 0, "TRUST_", GPG_STATUS_TRUST_LEVEL },
109};
110
111static struct {
112 const char *key;
113 enum signature_trust_level value;
114} sigcheck_gpg_trust_level[] = {
115 { "UNDEFINED", TRUST_UNDEFINED },
116 { "NEVER", TRUST_NEVER },
117 { "MARGINAL", TRUST_MARGINAL },
118 { "FULLY", TRUST_FULLY },
119 { "ULTIMATE", TRUST_ULTIMATE },
a50e7ca3
JH
120};
121
392b862e
HJI
122static void replace_cstring(char **field, const char *line, const char *next)
123{
124 free(*field);
125
126 if (line && next)
127 *field = xmemdupz(line, next - line);
128 else
129 *field = NULL;
130}
131
54887b46
HJI
132static int parse_gpg_trust_level(const char *level,
133 enum signature_trust_level *res)
134{
135 size_t i;
136
137 for (i = 0; i < ARRAY_SIZE(sigcheck_gpg_trust_level); i++) {
138 if (!strcmp(sigcheck_gpg_trust_level[i].key, level)) {
139 *res = sigcheck_gpg_trust_level[i].value;
140 return 0;
141 }
142 }
143 return 1;
144}
145
fbd0f166 146static void parse_gpg_output(struct signature_check *sigc)
a50e7ca3
JH
147{
148 const char *buf = sigc->gpg_status;
da6cf1b3 149 const char *line, *next;
4de9394d 150 int i, j;
da6cf1b3
MG
151 int seen_exclusive_status = 0;
152
153 /* Iterate over all lines */
154 for (line = buf; *line; line = strchrnul(line+1, '\n')) {
155 while (*line == '\n')
156 line++;
64c45dc7
SR
157 if (!*line)
158 break;
159
da6cf1b3
MG
160 /* Skip lines that don't start with GNUPG status */
161 if (!skip_prefix(line, "[GNUPG:] ", &line))
162 continue;
163
164 /* Iterate over all search strings */
165 for (i = 0; i < ARRAY_SIZE(sigcheck_gpg_status); i++) {
166 if (skip_prefix(line, sigcheck_gpg_status[i].check, &line)) {
54887b46
HJI
167 /*
168 * GOODSIG, BADSIG etc. can occur only once for
169 * each signature. Therefore, if we had more
170 * than one then we're dealing with multiple
171 * signatures. We don't support them
172 * currently, and they're rather hard to
173 * create, so something is likely fishy and we
174 * should reject them altogether.
175 */
da6cf1b3 176 if (sigcheck_gpg_status[i].flags & GPG_STATUS_EXCLUSIVE) {
02561896 177 if (seen_exclusive_status++)
54887b46 178 goto error;
da6cf1b3
MG
179 }
180
3daaaabe
MG
181 if (sigcheck_gpg_status[i].result)
182 sigc->result = sigcheck_gpg_status[i].result;
0b11a84e
MG
183 /* Do we have key information? */
184 if (sigcheck_gpg_status[i].flags & GPG_STATUS_KEYID) {
da6cf1b3 185 next = strchrnul(line, ' ');
392b862e 186 replace_cstring(&sigc->key, line, next);
0b11a84e
MG
187 /* Do we have signer information? */
188 if (*next && (sigcheck_gpg_status[i].flags & GPG_STATUS_UID)) {
da6cf1b3
MG
189 line = next + 1;
190 next = strchrnul(line, '\n');
392b862e 191 replace_cstring(&sigc->signer, line, next);
da6cf1b3
MG
192 }
193 }
54887b46
HJI
194
195 /* Do we have trust level? */
196 if (sigcheck_gpg_status[i].flags & GPG_STATUS_TRUST_LEVEL) {
197 /*
198 * GPG v1 and v2 differs in how the
199 * TRUST_ lines are written. Some
200 * trust lines contain no additional
201 * space-separated information for v1.
202 */
203 size_t trust_size = strcspn(line, " \n");
204 char *trust = xmemdupz(line, trust_size);
205
206 if (parse_gpg_trust_level(trust, &sigc->trust_level)) {
207 free(trust);
208 goto error;
209 }
210 free(trust);
211 }
212
3daaaabe
MG
213 /* Do we have fingerprint? */
214 if (sigcheck_gpg_status[i].flags & GPG_STATUS_FINGERPRINT) {
67a6ea63
HJI
215 const char *limit;
216 char **field;
217
3daaaabe 218 next = strchrnul(line, ' ');
392b862e 219 replace_cstring(&sigc->fingerprint, line, next);
4de9394d 220
67a6ea63
HJI
221 /*
222 * Skip interim fields. The search is
223 * limited to the same line since only
224 * OpenPGP signatures has a field with
225 * the primary fingerprint.
226 */
227 limit = strchrnul(line, '\n');
4de9394d 228 for (j = 9; j > 0; j--) {
67a6ea63 229 if (!*next || limit <= next)
4de9394d
MG
230 break;
231 line = next + 1;
232 next = strchrnul(line, ' ');
233 }
234
67a6ea63
HJI
235 field = &sigc->primary_key_fingerprint;
236 if (!j) {
237 next = strchrnul(line, '\n');
238 replace_cstring(field, line, next);
239 } else {
240 replace_cstring(field, NULL, NULL);
241 }
3daaaabe 242 }
da6cf1b3
MG
243
244 break;
661a1806 245 }
a50e7ca3
JH
246 }
247 }
da6cf1b3
MG
248 return;
249
54887b46 250error:
da6cf1b3
MG
251 sigc->result = 'E';
252 /* Clear partial data to avoid confusion */
4de9394d 253 FREE_AND_NULL(sigc->primary_key_fingerprint);
3daaaabe 254 FREE_AND_NULL(sigc->fingerprint);
da6cf1b3
MG
255 FREE_AND_NULL(sigc->signer);
256 FREE_AND_NULL(sigc->key);
a50e7ca3
JH
257}
258
434060ec 259int check_signature(const char *payload, size_t plen, const char *signature,
a4cc18f2 260 size_t slen, struct signature_check *sigc)
261{
262 struct strbuf gpg_output = STRBUF_INIT;
263 struct strbuf gpg_status = STRBUF_INIT;
264 int status;
265
266 sigc->result = 'N';
54887b46 267 sigc->trust_level = -1;
a4cc18f2 268
269 status = verify_signed_buffer(payload, plen, signature, slen,
270 &gpg_output, &gpg_status);
271 if (status && !gpg_output.len)
272 goto out;
273 sigc->payload = xmemdupz(payload, plen);
274 sigc->gpg_output = strbuf_detach(&gpg_output, NULL);
275 sigc->gpg_status = strbuf_detach(&gpg_status, NULL);
276 parse_gpg_output(sigc);
54887b46
HJI
277 status |= sigc->result != 'G';
278 status |= sigc->trust_level < configured_min_trust_level;
a4cc18f2 279
280 out:
281 strbuf_release(&gpg_status);
282 strbuf_release(&gpg_output);
434060ec 283
4e5dc9ca 284 return !!status;
a4cc18f2 285}
286
ca194d50 287void print_signature_buffer(const struct signature_check *sigc, unsigned flags)
288{
aeff29dd 289 const char *output = flags & GPG_VERIFY_RAW ?
290 sigc->gpg_status : sigc->gpg_output;
291
ca194d50 292 if (flags & GPG_VERIFY_VERBOSE && sigc->payload)
293 fputs(sigc->payload, stdout);
294
aeff29dd 295 if (output)
296 fputs(output, stderr);
ca194d50 297}
298
e6fa6cde 299size_t parse_signature(const char *buf, size_t size)
d7c67668 300{
d7c67668 301 size_t len = 0;
8b44b2be
JK
302 size_t match = size;
303 while (len < size) {
304 const char *eol;
305
58af57e1 306 if (get_format_by_sig(buf + len))
8b44b2be
JK
307 match = len;
308
309 eol = memchr(buf + len, '\n', size - len);
d7c67668
JH
310 len += eol ? eol - (buf + len) + 1 : size - len;
311 }
8b44b2be 312 return match;
d7c67668
JH
313}
314
2f47eae2
JH
315void set_signing_key(const char *key)
316{
317 free(configured_signing_key);
318 configured_signing_key = xstrdup(key);
319}
320
321int git_gpg_config(const char *var, const char *value, void *cb)
322{
58af57e1
HS
323 struct gpg_format *fmt = NULL;
324 char *fmtname = NULL;
54887b46
HJI
325 char *trust;
326 int ret;
58af57e1 327
2f47eae2 328 if (!strcmp(var, "user.signingkey")) {
1b0eeec3
JK
329 if (!value)
330 return config_error_nonbool(var);
0c5e70f0 331 set_signing_key(value);
1b0eeec3 332 return 0;
0c5e70f0 333 }
1b0eeec3 334
57a8dd75
HS
335 if (!strcmp(var, "gpg.format")) {
336 if (!value)
337 return config_error_nonbool(var);
58af57e1
HS
338 fmt = get_format_by_name(value);
339 if (!fmt)
57a8dd75
HS
340 return error("unsupported value for %s: %s",
341 var, value);
58af57e1
HS
342 use_format = fmt;
343 return 0;
57a8dd75
HS
344 }
345
54887b46
HJI
346 if (!strcmp(var, "gpg.mintrustlevel")) {
347 if (!value)
348 return config_error_nonbool(var);
349
350 trust = xstrdup_toupper(value);
351 ret = parse_gpg_trust_level(trust, &configured_min_trust_level);
352 free(trust);
353
354 if (ret)
355 return error("unsupported value for %s: %s", var,
356 value);
357 return 0;
358 }
359
b02f51b1 360 if (!strcmp(var, "gpg.program") || !strcmp(var, "gpg.openpgp.program"))
58af57e1
HS
361 fmtname = "openpgp";
362
1e7adb97
HS
363 if (!strcmp(var, "gpg.x509.program"))
364 fmtname = "x509";
365
58af57e1
HS
366 if (fmtname) {
367 fmt = get_format_by_name(fmtname);
368 return git_config_string(&fmt->program, var, value);
2f47eae2 369 }
1b0eeec3 370
2f47eae2
JH
371 return 0;
372}
373
374const char *get_signing_key(void)
375{
376 if (configured_signing_key)
377 return configured_signing_key;
f9bc573f 378 return git_committer_info(IDENT_STRICT|IDENT_NO_DATE);
2f47eae2
JH
379}
380
2f47eae2
JH
381int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *signing_key)
382{
d3180279 383 struct child_process gpg = CHILD_PROCESS_INIT;
0581b546 384 int ret;
2f47eae2 385 size_t i, j, bottom;
efee9553 386 struct strbuf gpg_status = STRBUF_INIT;
2f47eae2 387
aedb5dc3 388 argv_array_pushl(&gpg.args,
58af57e1 389 use_format->program,
efee9553 390 "--status-fd=2",
aedb5dc3
JK
391 "-bsau", signing_key,
392 NULL);
2f47eae2 393
0581b546 394 bottom = signature->len;
2f47eae2
JH
395
396 /*
397 * When the username signingkey is bad, program could be terminated
398 * because gpg exits without reading and then write gets SIGPIPE.
399 */
400 sigchain_push(SIGPIPE, SIG_IGN);
0581b546 401 ret = pipe_command(&gpg, buffer->buf, buffer->len,
efee9553 402 signature, 1024, &gpg_status, 0);
2f47eae2
JH
403 sigchain_pop(SIGPIPE);
404
efee9553
MG
405 ret |= !strstr(gpg_status.buf, "\n[GNUPG:] SIG_CREATED ");
406 strbuf_release(&gpg_status);
407 if (ret)
2f47eae2
JH
408 return error(_("gpg failed to sign the data"));
409
410 /* Strip CR from the line endings, in case we are on Windows. */
411 for (i = j = bottom; i < signature->len; i++)
412 if (signature->buf[i] != '\r') {
413 if (i != j)
414 signature->buf[j] = signature->buf[i];
415 j++;
416 }
417 strbuf_setlen(signature, j);
418
419 return 0;
420}
0106b1d4
JH
421
422int verify_signed_buffer(const char *payload, size_t payload_size,
423 const char *signature, size_t signature_size,
424 struct strbuf *gpg_output, struct strbuf *gpg_status)
425{
426 struct child_process gpg = CHILD_PROCESS_INIT;
427 struct gpg_format *fmt;
428 struct tempfile *temp;
429 int ret;
430 struct strbuf buf = STRBUF_INIT;
431
432 temp = mks_tempfile_t(".git_vtag_tmpXXXXXX");
433 if (!temp)
434 return error_errno(_("could not create temporary file"));
435 if (write_in_full(temp->fd, signature, signature_size) < 0 ||
436 close_tempfile_gently(temp) < 0) {
437 error_errno(_("failed writing detached signature to '%s'"),
438 temp->filename.buf);
439 delete_tempfile(&temp);
440 return -1;
441 }
442
443 fmt = get_format_by_sig(signature);
444 if (!fmt)
445 BUG("bad signature '%s'", signature);
446
447 argv_array_push(&gpg.args, fmt->program);
448 argv_array_pushv(&gpg.args, fmt->verify_args);
449 argv_array_pushl(&gpg.args,
450 "--status-fd=1",
451 "--verify", temp->filename.buf, "-",
452 NULL);
453
454 if (!gpg_status)
455 gpg_status = &buf;
456
457 sigchain_push(SIGPIPE, SIG_IGN);
458 ret = pipe_command(&gpg, payload, payload_size,
459 gpg_status, 0, gpg_output, 0);
460 sigchain_pop(SIGPIPE);
461
462 delete_tempfile(&temp);
463
464 ret |= !strstr(gpg_status->buf, "\n[GNUPG:] GOODSIG ");
465 strbuf_release(&buf); /* no matter it was used or not */
466
467 return ret;
468}