]> git.ipfire.org Git - thirdparty/git.git/blob - gpg-interface.c
Merge branch 'fc/remove-header-workarounds-for-asciidoc' into fc/doc-use-datestamp...
[thirdparty/git.git] / gpg-interface.c
1 #include "git-compat-util.h"
2 #include "commit.h"
3 #include "config.h"
4 #include "gettext.h"
5 #include "run-command.h"
6 #include "strbuf.h"
7 #include "dir.h"
8 #include "ident.h"
9 #include "gpg-interface.h"
10 #include "sigchain.h"
11 #include "tempfile.h"
12 #include "alias.h"
13 #include "wrapper.h"
14
15 static int git_gpg_config(const char *, const char *, void *);
16
17 static void gpg_interface_lazy_init(void)
18 {
19 static int done;
20
21 if (done)
22 return;
23 done = 1;
24 git_config(git_gpg_config, NULL);
25 }
26
27 static char *configured_signing_key;
28 static const char *ssh_default_key_command, *ssh_allowed_signers, *ssh_revocation_file;
29 static enum signature_trust_level configured_min_trust_level = TRUST_UNDEFINED;
30
31 struct gpg_format {
32 const char *name;
33 const char *program;
34 const char **verify_args;
35 const char **sigs;
36 int (*verify_signed_buffer)(struct signature_check *sigc,
37 struct gpg_format *fmt,
38 const char *signature,
39 size_t signature_size);
40 int (*sign_buffer)(struct strbuf *buffer, struct strbuf *signature,
41 const char *signing_key);
42 const char *(*get_default_key)(void);
43 const char *(*get_key_id)(void);
44 };
45
46 static const char *openpgp_verify_args[] = {
47 "--keyid-format=long",
48 NULL
49 };
50 static const char *openpgp_sigs[] = {
51 "-----BEGIN PGP SIGNATURE-----",
52 "-----BEGIN PGP MESSAGE-----",
53 NULL
54 };
55
56 static const char *x509_verify_args[] = {
57 NULL
58 };
59 static const char *x509_sigs[] = {
60 "-----BEGIN SIGNED MESSAGE-----",
61 NULL
62 };
63
64 static const char *ssh_verify_args[] = { NULL };
65 static const char *ssh_sigs[] = {
66 "-----BEGIN SSH SIGNATURE-----",
67 NULL
68 };
69
70 static int verify_gpg_signed_buffer(struct signature_check *sigc,
71 struct gpg_format *fmt,
72 const char *signature,
73 size_t signature_size);
74 static int verify_ssh_signed_buffer(struct signature_check *sigc,
75 struct gpg_format *fmt,
76 const char *signature,
77 size_t signature_size);
78 static int sign_buffer_gpg(struct strbuf *buffer, struct strbuf *signature,
79 const char *signing_key);
80 static int sign_buffer_ssh(struct strbuf *buffer, struct strbuf *signature,
81 const char *signing_key);
82
83 static const char *get_default_ssh_signing_key(void);
84
85 static const char *get_ssh_key_id(void);
86
87 static struct gpg_format gpg_format[] = {
88 {
89 .name = "openpgp",
90 .program = "gpg",
91 .verify_args = openpgp_verify_args,
92 .sigs = openpgp_sigs,
93 .verify_signed_buffer = verify_gpg_signed_buffer,
94 .sign_buffer = sign_buffer_gpg,
95 .get_default_key = NULL,
96 .get_key_id = NULL,
97 },
98 {
99 .name = "x509",
100 .program = "gpgsm",
101 .verify_args = x509_verify_args,
102 .sigs = x509_sigs,
103 .verify_signed_buffer = verify_gpg_signed_buffer,
104 .sign_buffer = sign_buffer_gpg,
105 .get_default_key = NULL,
106 .get_key_id = NULL,
107 },
108 {
109 .name = "ssh",
110 .program = "ssh-keygen",
111 .verify_args = ssh_verify_args,
112 .sigs = ssh_sigs,
113 .verify_signed_buffer = verify_ssh_signed_buffer,
114 .sign_buffer = sign_buffer_ssh,
115 .get_default_key = get_default_ssh_signing_key,
116 .get_key_id = get_ssh_key_id,
117 },
118 };
119
120 static struct gpg_format *use_format = &gpg_format[0];
121
122 static struct gpg_format *get_format_by_name(const char *str)
123 {
124 int i;
125
126 for (i = 0; i < ARRAY_SIZE(gpg_format); i++)
127 if (!strcmp(gpg_format[i].name, str))
128 return gpg_format + i;
129 return NULL;
130 }
131
132 static struct gpg_format *get_format_by_sig(const char *sig)
133 {
134 int i, j;
135
136 for (i = 0; i < ARRAY_SIZE(gpg_format); i++)
137 for (j = 0; gpg_format[i].sigs[j]; j++)
138 if (starts_with(sig, gpg_format[i].sigs[j]))
139 return gpg_format + i;
140 return NULL;
141 }
142
143 void signature_check_clear(struct signature_check *sigc)
144 {
145 FREE_AND_NULL(sigc->payload);
146 FREE_AND_NULL(sigc->output);
147 FREE_AND_NULL(sigc->gpg_status);
148 FREE_AND_NULL(sigc->signer);
149 FREE_AND_NULL(sigc->key);
150 FREE_AND_NULL(sigc->fingerprint);
151 FREE_AND_NULL(sigc->primary_key_fingerprint);
152 }
153
154 /* An exclusive status -- only one of them can appear in output */
155 #define GPG_STATUS_EXCLUSIVE (1<<0)
156 /* The status includes key identifier */
157 #define GPG_STATUS_KEYID (1<<1)
158 /* The status includes user identifier */
159 #define GPG_STATUS_UID (1<<2)
160 /* The status includes key fingerprints */
161 #define GPG_STATUS_FINGERPRINT (1<<3)
162 /* The status includes trust level */
163 #define GPG_STATUS_TRUST_LEVEL (1<<4)
164
165 /* Short-hand for standard exclusive *SIG status with keyid & UID */
166 #define GPG_STATUS_STDSIG (GPG_STATUS_EXCLUSIVE|GPG_STATUS_KEYID|GPG_STATUS_UID)
167
168 static struct {
169 char result;
170 const char *check;
171 unsigned int flags;
172 } sigcheck_gpg_status[] = {
173 { 'G', "GOODSIG ", GPG_STATUS_STDSIG },
174 { 'B', "BADSIG ", GPG_STATUS_STDSIG },
175 { 'E', "ERRSIG ", GPG_STATUS_EXCLUSIVE|GPG_STATUS_KEYID },
176 { 'X', "EXPSIG ", GPG_STATUS_STDSIG },
177 { 'Y', "EXPKEYSIG ", GPG_STATUS_STDSIG },
178 { 'R', "REVKEYSIG ", GPG_STATUS_STDSIG },
179 { 0, "VALIDSIG ", GPG_STATUS_FINGERPRINT },
180 { 0, "TRUST_", GPG_STATUS_TRUST_LEVEL },
181 };
182
183 /* Keep the order same as enum signature_trust_level */
184 static struct sigcheck_gpg_trust_level {
185 const char *key;
186 const char *display_key;
187 enum signature_trust_level value;
188 } sigcheck_gpg_trust_level[] = {
189 { "UNDEFINED", "undefined", TRUST_UNDEFINED },
190 { "NEVER", "never", TRUST_NEVER },
191 { "MARGINAL", "marginal", TRUST_MARGINAL },
192 { "FULLY", "fully", TRUST_FULLY },
193 { "ULTIMATE", "ultimate", TRUST_ULTIMATE },
194 };
195
196 static void replace_cstring(char **field, const char *line, const char *next)
197 {
198 free(*field);
199
200 if (line && next)
201 *field = xmemdupz(line, next - line);
202 else
203 *field = NULL;
204 }
205
206 static int parse_gpg_trust_level(const char *level,
207 enum signature_trust_level *res)
208 {
209 size_t i;
210
211 for (i = 0; i < ARRAY_SIZE(sigcheck_gpg_trust_level); i++) {
212 if (!strcmp(sigcheck_gpg_trust_level[i].key, level)) {
213 *res = sigcheck_gpg_trust_level[i].value;
214 return 0;
215 }
216 }
217 return 1;
218 }
219
220 static void parse_gpg_output(struct signature_check *sigc)
221 {
222 const char *buf = sigc->gpg_status;
223 const char *line, *next;
224 int i, j;
225 int seen_exclusive_status = 0;
226
227 /* Iterate over all lines */
228 for (line = buf; *line; line = strchrnul(line+1, '\n')) {
229 while (*line == '\n')
230 line++;
231 if (!*line)
232 break;
233
234 /* Skip lines that don't start with GNUPG status */
235 if (!skip_prefix(line, "[GNUPG:] ", &line))
236 continue;
237
238 /* Iterate over all search strings */
239 for (i = 0; i < ARRAY_SIZE(sigcheck_gpg_status); i++) {
240 if (skip_prefix(line, sigcheck_gpg_status[i].check, &line)) {
241 /*
242 * GOODSIG, BADSIG etc. can occur only once for
243 * each signature. Therefore, if we had more
244 * than one then we're dealing with multiple
245 * signatures. We don't support them
246 * currently, and they're rather hard to
247 * create, so something is likely fishy and we
248 * should reject them altogether.
249 */
250 if (sigcheck_gpg_status[i].flags & GPG_STATUS_EXCLUSIVE) {
251 if (seen_exclusive_status++)
252 goto error;
253 }
254
255 if (sigcheck_gpg_status[i].result)
256 sigc->result = sigcheck_gpg_status[i].result;
257 /* Do we have key information? */
258 if (sigcheck_gpg_status[i].flags & GPG_STATUS_KEYID) {
259 next = strchrnul(line, ' ');
260 replace_cstring(&sigc->key, line, next);
261 /* Do we have signer information? */
262 if (*next && (sigcheck_gpg_status[i].flags & GPG_STATUS_UID)) {
263 line = next + 1;
264 next = strchrnul(line, '\n');
265 replace_cstring(&sigc->signer, line, next);
266 }
267 }
268
269 /* Do we have trust level? */
270 if (sigcheck_gpg_status[i].flags & GPG_STATUS_TRUST_LEVEL) {
271 /*
272 * GPG v1 and v2 differs in how the
273 * TRUST_ lines are written. Some
274 * trust lines contain no additional
275 * space-separated information for v1.
276 */
277 size_t trust_size = strcspn(line, " \n");
278 char *trust = xmemdupz(line, trust_size);
279
280 if (parse_gpg_trust_level(trust, &sigc->trust_level)) {
281 free(trust);
282 goto error;
283 }
284 free(trust);
285 }
286
287 /* Do we have fingerprint? */
288 if (sigcheck_gpg_status[i].flags & GPG_STATUS_FINGERPRINT) {
289 const char *limit;
290 char **field;
291
292 next = strchrnul(line, ' ');
293 replace_cstring(&sigc->fingerprint, line, next);
294
295 /*
296 * Skip interim fields. The search is
297 * limited to the same line since only
298 * OpenPGP signatures has a field with
299 * the primary fingerprint.
300 */
301 limit = strchrnul(line, '\n');
302 for (j = 9; j > 0; j--) {
303 if (!*next || limit <= next)
304 break;
305 line = next + 1;
306 next = strchrnul(line, ' ');
307 }
308
309 field = &sigc->primary_key_fingerprint;
310 if (!j) {
311 next = strchrnul(line, '\n');
312 replace_cstring(field, line, next);
313 } else {
314 replace_cstring(field, NULL, NULL);
315 }
316 }
317
318 break;
319 }
320 }
321 }
322 return;
323
324 error:
325 sigc->result = 'E';
326 /* Clear partial data to avoid confusion */
327 FREE_AND_NULL(sigc->primary_key_fingerprint);
328 FREE_AND_NULL(sigc->fingerprint);
329 FREE_AND_NULL(sigc->signer);
330 FREE_AND_NULL(sigc->key);
331 }
332
333 static int verify_gpg_signed_buffer(struct signature_check *sigc,
334 struct gpg_format *fmt,
335 const char *signature,
336 size_t signature_size)
337 {
338 struct child_process gpg = CHILD_PROCESS_INIT;
339 struct tempfile *temp;
340 int ret;
341 struct strbuf gpg_stdout = STRBUF_INIT;
342 struct strbuf gpg_stderr = STRBUF_INIT;
343
344 temp = mks_tempfile_t(".git_vtag_tmpXXXXXX");
345 if (!temp)
346 return error_errno(_("could not create temporary file"));
347 if (write_in_full(temp->fd, signature, signature_size) < 0 ||
348 close_tempfile_gently(temp) < 0) {
349 error_errno(_("failed writing detached signature to '%s'"),
350 temp->filename.buf);
351 delete_tempfile(&temp);
352 return -1;
353 }
354
355 strvec_push(&gpg.args, fmt->program);
356 strvec_pushv(&gpg.args, fmt->verify_args);
357 strvec_pushl(&gpg.args,
358 "--status-fd=1",
359 "--verify", temp->filename.buf, "-",
360 NULL);
361
362 sigchain_push(SIGPIPE, SIG_IGN);
363 ret = pipe_command(&gpg, sigc->payload, sigc->payload_len, &gpg_stdout, 0,
364 &gpg_stderr, 0);
365 sigchain_pop(SIGPIPE);
366
367 delete_tempfile(&temp);
368
369 ret |= !strstr(gpg_stdout.buf, "\n[GNUPG:] GOODSIG ");
370 sigc->output = strbuf_detach(&gpg_stderr, NULL);
371 sigc->gpg_status = strbuf_detach(&gpg_stdout, NULL);
372
373 parse_gpg_output(sigc);
374
375 strbuf_release(&gpg_stdout);
376 strbuf_release(&gpg_stderr);
377
378 return ret;
379 }
380
381 static void parse_ssh_output(struct signature_check *sigc)
382 {
383 const char *line, *principal, *search;
384 char *to_free;
385 char *key = NULL;
386
387 /*
388 * ssh-keygen output should be:
389 * Good "git" signature for PRINCIPAL with RSA key SHA256:FINGERPRINT
390 *
391 * or for valid but unknown keys:
392 * Good "git" signature with RSA key SHA256:FINGERPRINT
393 *
394 * Note that "PRINCIPAL" can contain whitespace, "RSA" and
395 * "SHA256" part could be a different token that names of
396 * the algorithms used, and "FINGERPRINT" is a hexadecimal
397 * string. By finding the last occurence of " with ", we can
398 * reliably parse out the PRINCIPAL.
399 */
400 sigc->result = 'B';
401 sigc->trust_level = TRUST_NEVER;
402
403 line = to_free = xmemdupz(sigc->output, strcspn(sigc->output, "\n"));
404
405 if (skip_prefix(line, "Good \"git\" signature for ", &line)) {
406 /* Search for the last "with" to get the full principal */
407 principal = line;
408 do {
409 search = strstr(line, " with ");
410 if (search)
411 line = search + 1;
412 } while (search != NULL);
413 if (line == principal)
414 goto cleanup;
415
416 /* Valid signature and known principal */
417 sigc->result = 'G';
418 sigc->trust_level = TRUST_FULLY;
419 sigc->signer = xmemdupz(principal, line - principal - 1);
420 } else if (skip_prefix(line, "Good \"git\" signature with ", &line)) {
421 /* Valid signature, but key unknown */
422 sigc->result = 'G';
423 sigc->trust_level = TRUST_UNDEFINED;
424 } else {
425 goto cleanup;
426 }
427
428 key = strstr(line, "key ");
429 if (key) {
430 sigc->fingerprint = xstrdup(strstr(line, "key ") + 4);
431 sigc->key = xstrdup(sigc->fingerprint);
432 } else {
433 /*
434 * Output did not match what we expected
435 * Treat the signature as bad
436 */
437 sigc->result = 'B';
438 }
439
440 cleanup:
441 free(to_free);
442 }
443
444 static int verify_ssh_signed_buffer(struct signature_check *sigc,
445 struct gpg_format *fmt,
446 const char *signature,
447 size_t signature_size)
448 {
449 struct child_process ssh_keygen = CHILD_PROCESS_INIT;
450 struct tempfile *buffer_file;
451 int ret = -1;
452 const char *line;
453 char *principal;
454 struct strbuf ssh_principals_out = STRBUF_INIT;
455 struct strbuf ssh_principals_err = STRBUF_INIT;
456 struct strbuf ssh_keygen_out = STRBUF_INIT;
457 struct strbuf ssh_keygen_err = STRBUF_INIT;
458 struct strbuf verify_time = STRBUF_INIT;
459 const struct date_mode verify_date_mode = {
460 .type = DATE_STRFTIME,
461 .strftime_fmt = "%Y%m%d%H%M%S",
462 /* SSH signing key validity has no timezone information - Use the local timezone */
463 .local = 1,
464 };
465
466 if (!ssh_allowed_signers) {
467 error(_("gpg.ssh.allowedSignersFile needs to be configured and exist for ssh signature verification"));
468 return -1;
469 }
470
471 buffer_file = mks_tempfile_t(".git_vtag_tmpXXXXXX");
472 if (!buffer_file)
473 return error_errno(_("could not create temporary file"));
474 if (write_in_full(buffer_file->fd, signature, signature_size) < 0 ||
475 close_tempfile_gently(buffer_file) < 0) {
476 error_errno(_("failed writing detached signature to '%s'"),
477 buffer_file->filename.buf);
478 delete_tempfile(&buffer_file);
479 return -1;
480 }
481
482 if (sigc->payload_timestamp)
483 strbuf_addf(&verify_time, "-Overify-time=%s",
484 show_date(sigc->payload_timestamp, 0, &verify_date_mode));
485
486 /* Find the principal from the signers */
487 strvec_pushl(&ssh_keygen.args, fmt->program,
488 "-Y", "find-principals",
489 "-f", ssh_allowed_signers,
490 "-s", buffer_file->filename.buf,
491 verify_time.buf,
492 NULL);
493 ret = pipe_command(&ssh_keygen, NULL, 0, &ssh_principals_out, 0,
494 &ssh_principals_err, 0);
495 if (ret && strstr(ssh_principals_err.buf, "usage:")) {
496 error(_("ssh-keygen -Y find-principals/verify is needed for ssh signature verification (available in openssh version 8.2p1+)"));
497 goto out;
498 }
499 if (ret || !ssh_principals_out.len) {
500 /*
501 * We did not find a matching principal in the allowedSigners
502 * Check without validation
503 */
504 child_process_init(&ssh_keygen);
505 strvec_pushl(&ssh_keygen.args, fmt->program,
506 "-Y", "check-novalidate",
507 "-n", "git",
508 "-s", buffer_file->filename.buf,
509 verify_time.buf,
510 NULL);
511 pipe_command(&ssh_keygen, sigc->payload, sigc->payload_len,
512 &ssh_keygen_out, 0, &ssh_keygen_err, 0);
513
514 /*
515 * Fail on unknown keys
516 * we still call check-novalidate to display the signature info
517 */
518 ret = -1;
519 } else {
520 /* Check every principal we found (one per line) */
521 const char *next;
522 for (line = ssh_principals_out.buf;
523 *line;
524 line = next) {
525 const char *end_of_text;
526
527 next = end_of_text = strchrnul(line, '\n');
528
529 /* Did we find a LF, and did we have CR before it? */
530 if (*end_of_text &&
531 line < end_of_text &&
532 end_of_text[-1] == '\r')
533 end_of_text--;
534
535 /* Unless we hit NUL, skip over the LF we found */
536 if (*next)
537 next++;
538
539 /* Not all lines are data. Skip empty ones */
540 if (line == end_of_text)
541 continue;
542
543 /* We now know we have an non-empty line. Process it */
544 principal = xmemdupz(line, end_of_text - line);
545
546 child_process_init(&ssh_keygen);
547 strbuf_release(&ssh_keygen_out);
548 strbuf_release(&ssh_keygen_err);
549 strvec_push(&ssh_keygen.args, fmt->program);
550 /*
551 * We found principals
552 * Try with each until we find a match
553 */
554 strvec_pushl(&ssh_keygen.args, "-Y", "verify",
555 "-n", "git",
556 "-f", ssh_allowed_signers,
557 "-I", principal,
558 "-s", buffer_file->filename.buf,
559 verify_time.buf,
560 NULL);
561
562 if (ssh_revocation_file) {
563 if (file_exists(ssh_revocation_file)) {
564 strvec_pushl(&ssh_keygen.args, "-r",
565 ssh_revocation_file, NULL);
566 } else {
567 warning(_("ssh signing revocation file configured but not found: %s"),
568 ssh_revocation_file);
569 }
570 }
571
572 sigchain_push(SIGPIPE, SIG_IGN);
573 ret = pipe_command(&ssh_keygen, sigc->payload, sigc->payload_len,
574 &ssh_keygen_out, 0, &ssh_keygen_err, 0);
575 sigchain_pop(SIGPIPE);
576
577 FREE_AND_NULL(principal);
578
579 if (!ret)
580 ret = !starts_with(ssh_keygen_out.buf, "Good");
581
582 if (!ret)
583 break;
584 }
585 }
586
587 strbuf_stripspace(&ssh_keygen_out, 0);
588 strbuf_stripspace(&ssh_keygen_err, 0);
589 /* Add stderr outputs to show the user actual ssh-keygen errors */
590 strbuf_add(&ssh_keygen_out, ssh_principals_err.buf, ssh_principals_err.len);
591 strbuf_add(&ssh_keygen_out, ssh_keygen_err.buf, ssh_keygen_err.len);
592 sigc->output = strbuf_detach(&ssh_keygen_out, NULL);
593 sigc->gpg_status = xstrdup(sigc->output);
594
595 parse_ssh_output(sigc);
596
597 out:
598 if (buffer_file)
599 delete_tempfile(&buffer_file);
600 strbuf_release(&ssh_principals_out);
601 strbuf_release(&ssh_principals_err);
602 strbuf_release(&ssh_keygen_out);
603 strbuf_release(&ssh_keygen_err);
604 strbuf_release(&verify_time);
605
606 return ret;
607 }
608
609 static int parse_payload_metadata(struct signature_check *sigc)
610 {
611 const char *ident_line = NULL;
612 size_t ident_len;
613 struct ident_split ident;
614 const char *signer_header;
615
616 switch (sigc->payload_type) {
617 case SIGNATURE_PAYLOAD_COMMIT:
618 signer_header = "committer";
619 break;
620 case SIGNATURE_PAYLOAD_TAG:
621 signer_header = "tagger";
622 break;
623 case SIGNATURE_PAYLOAD_UNDEFINED:
624 case SIGNATURE_PAYLOAD_PUSH_CERT:
625 /* Ignore payloads we don't want to parse */
626 return 0;
627 default:
628 BUG("invalid value for sigc->payload_type");
629 }
630
631 ident_line = find_commit_header(sigc->payload, signer_header, &ident_len);
632 if (!ident_line || !ident_len)
633 return 1;
634
635 if (split_ident_line(&ident, ident_line, ident_len))
636 return 1;
637
638 if (!sigc->payload_timestamp && ident.date_begin && ident.date_end)
639 sigc->payload_timestamp = parse_timestamp(ident.date_begin, NULL, 10);
640
641 return 0;
642 }
643
644 int check_signature(struct signature_check *sigc,
645 const char *signature, size_t slen)
646 {
647 struct gpg_format *fmt;
648 int status;
649
650 gpg_interface_lazy_init();
651
652 sigc->result = 'N';
653 sigc->trust_level = -1;
654
655 fmt = get_format_by_sig(signature);
656 if (!fmt)
657 die(_("bad/incompatible signature '%s'"), signature);
658
659 if (parse_payload_metadata(sigc))
660 return 1;
661
662 status = fmt->verify_signed_buffer(sigc, fmt, signature, slen);
663
664 if (status && !sigc->output)
665 return !!status;
666
667 status |= sigc->result != 'G';
668 status |= sigc->trust_level < configured_min_trust_level;
669
670 return !!status;
671 }
672
673 void print_signature_buffer(const struct signature_check *sigc, unsigned flags)
674 {
675 const char *output = flags & GPG_VERIFY_RAW ? sigc->gpg_status :
676 sigc->output;
677
678 if (flags & GPG_VERIFY_VERBOSE && sigc->payload)
679 fwrite(sigc->payload, 1, sigc->payload_len, stdout);
680
681 if (output)
682 fputs(output, stderr);
683 }
684
685 size_t parse_signed_buffer(const char *buf, size_t size)
686 {
687 size_t len = 0;
688 size_t match = size;
689 while (len < size) {
690 const char *eol;
691
692 if (get_format_by_sig(buf + len))
693 match = len;
694
695 eol = memchr(buf + len, '\n', size - len);
696 len += eol ? eol - (buf + len) + 1 : size - len;
697 }
698 return match;
699 }
700
701 int parse_signature(const char *buf, size_t size, struct strbuf *payload, struct strbuf *signature)
702 {
703 size_t match = parse_signed_buffer(buf, size);
704 if (match != size) {
705 strbuf_add(payload, buf, match);
706 remove_signature(payload);
707 strbuf_add(signature, buf + match, size - match);
708 return 1;
709 }
710 return 0;
711 }
712
713 void set_signing_key(const char *key)
714 {
715 gpg_interface_lazy_init();
716
717 free(configured_signing_key);
718 configured_signing_key = xstrdup(key);
719 }
720
721 static int git_gpg_config(const char *var, const char *value, void *cb UNUSED)
722 {
723 struct gpg_format *fmt = NULL;
724 char *fmtname = NULL;
725 char *trust;
726 int ret;
727
728 if (!strcmp(var, "user.signingkey")) {
729 if (!value)
730 return config_error_nonbool(var);
731 set_signing_key(value);
732 return 0;
733 }
734
735 if (!strcmp(var, "gpg.format")) {
736 if (!value)
737 return config_error_nonbool(var);
738 fmt = get_format_by_name(value);
739 if (!fmt)
740 return error(_("invalid value for '%s': '%s'"),
741 var, value);
742 use_format = fmt;
743 return 0;
744 }
745
746 if (!strcmp(var, "gpg.mintrustlevel")) {
747 if (!value)
748 return config_error_nonbool(var);
749
750 trust = xstrdup_toupper(value);
751 ret = parse_gpg_trust_level(trust, &configured_min_trust_level);
752 free(trust);
753
754 if (ret)
755 return error(_("invalid value for '%s': '%s'"),
756 var, value);
757 return 0;
758 }
759
760 if (!strcmp(var, "gpg.ssh.defaultkeycommand")) {
761 if (!value)
762 return config_error_nonbool(var);
763 return git_config_string(&ssh_default_key_command, var, value);
764 }
765
766 if (!strcmp(var, "gpg.ssh.allowedsignersfile")) {
767 if (!value)
768 return config_error_nonbool(var);
769 return git_config_pathname(&ssh_allowed_signers, var, value);
770 }
771
772 if (!strcmp(var, "gpg.ssh.revocationfile")) {
773 if (!value)
774 return config_error_nonbool(var);
775 return git_config_pathname(&ssh_revocation_file, var, value);
776 }
777
778 if (!strcmp(var, "gpg.program") || !strcmp(var, "gpg.openpgp.program"))
779 fmtname = "openpgp";
780
781 if (!strcmp(var, "gpg.x509.program"))
782 fmtname = "x509";
783
784 if (!strcmp(var, "gpg.ssh.program"))
785 fmtname = "ssh";
786
787 if (fmtname) {
788 fmt = get_format_by_name(fmtname);
789 return git_config_string(&fmt->program, var, value);
790 }
791
792 return 0;
793 }
794
795 /*
796 * Returns 1 if `string` contains a literal ssh key, 0 otherwise
797 * `key` will be set to the start of the actual key if a prefix is present.
798 */
799 static int is_literal_ssh_key(const char *string, const char **key)
800 {
801 if (skip_prefix(string, "key::", key))
802 return 1;
803 if (starts_with(string, "ssh-")) {
804 *key = string;
805 return 1;
806 }
807 return 0;
808 }
809
810 static char *get_ssh_key_fingerprint(const char *signing_key)
811 {
812 struct child_process ssh_keygen = CHILD_PROCESS_INIT;
813 int ret = -1;
814 struct strbuf fingerprint_stdout = STRBUF_INIT;
815 struct strbuf **fingerprint;
816 char *fingerprint_ret;
817 const char *literal_key = NULL;
818
819 /*
820 * With SSH Signing this can contain a filename or a public key
821 * For textual representation we usually want a fingerprint
822 */
823 if (is_literal_ssh_key(signing_key, &literal_key)) {
824 strvec_pushl(&ssh_keygen.args, "ssh-keygen", "-lf", "-", NULL);
825 ret = pipe_command(&ssh_keygen, literal_key,
826 strlen(literal_key), &fingerprint_stdout, 0,
827 NULL, 0);
828 } else {
829 strvec_pushl(&ssh_keygen.args, "ssh-keygen", "-lf",
830 configured_signing_key, NULL);
831 ret = pipe_command(&ssh_keygen, NULL, 0, &fingerprint_stdout, 0,
832 NULL, 0);
833 }
834
835 if (!!ret)
836 die_errno(_("failed to get the ssh fingerprint for key '%s'"),
837 signing_key);
838
839 fingerprint = strbuf_split_max(&fingerprint_stdout, ' ', 3);
840 if (!fingerprint[1])
841 die_errno(_("failed to get the ssh fingerprint for key '%s'"),
842 signing_key);
843
844 fingerprint_ret = strbuf_detach(fingerprint[1], NULL);
845 strbuf_list_free(fingerprint);
846 strbuf_release(&fingerprint_stdout);
847 return fingerprint_ret;
848 }
849
850 /* Returns the first public key from an ssh-agent to use for signing */
851 static const char *get_default_ssh_signing_key(void)
852 {
853 struct child_process ssh_default_key = CHILD_PROCESS_INIT;
854 int ret = -1;
855 struct strbuf key_stdout = STRBUF_INIT, key_stderr = STRBUF_INIT;
856 struct strbuf **keys;
857 char *key_command = NULL;
858 const char **argv;
859 int n;
860 char *default_key = NULL;
861 const char *literal_key = NULL;
862
863 if (!ssh_default_key_command)
864 die(_("either user.signingkey or gpg.ssh.defaultKeyCommand needs to be configured"));
865
866 key_command = xstrdup(ssh_default_key_command);
867 n = split_cmdline(key_command, &argv);
868
869 if (n < 0)
870 die("malformed build-time gpg.ssh.defaultKeyCommand: %s",
871 split_cmdline_strerror(n));
872
873 strvec_pushv(&ssh_default_key.args, argv);
874 ret = pipe_command(&ssh_default_key, NULL, 0, &key_stdout, 0,
875 &key_stderr, 0);
876
877 if (!ret) {
878 keys = strbuf_split_max(&key_stdout, '\n', 2);
879 if (keys[0] && is_literal_ssh_key(keys[0]->buf, &literal_key)) {
880 /*
881 * We only use `is_literal_ssh_key` here to check validity
882 * The prefix will be stripped when the key is used.
883 */
884 default_key = strbuf_detach(keys[0], NULL);
885 } else {
886 warning(_("gpg.ssh.defaultKeyCommand succeeded but returned no keys: %s %s"),
887 key_stderr.buf, key_stdout.buf);
888 }
889
890 strbuf_list_free(keys);
891 } else {
892 warning(_("gpg.ssh.defaultKeyCommand failed: %s %s"),
893 key_stderr.buf, key_stdout.buf);
894 }
895
896 free(key_command);
897 free(argv);
898 strbuf_release(&key_stdout);
899
900 return default_key;
901 }
902
903 static const char *get_ssh_key_id(void) {
904 return get_ssh_key_fingerprint(get_signing_key());
905 }
906
907 /* Returns a textual but unique representation of the signing key */
908 const char *get_signing_key_id(void)
909 {
910 gpg_interface_lazy_init();
911
912 if (use_format->get_key_id) {
913 return use_format->get_key_id();
914 }
915
916 /* GPG/GPGSM only store a key id on this variable */
917 return get_signing_key();
918 }
919
920 const char *get_signing_key(void)
921 {
922 gpg_interface_lazy_init();
923
924 if (configured_signing_key)
925 return configured_signing_key;
926 if (use_format->get_default_key) {
927 return use_format->get_default_key();
928 }
929
930 return git_committer_info(IDENT_STRICT | IDENT_NO_DATE);
931 }
932
933 const char *gpg_trust_level_to_str(enum signature_trust_level level)
934 {
935 struct sigcheck_gpg_trust_level *trust;
936
937 if (level < 0 || level >= ARRAY_SIZE(sigcheck_gpg_trust_level))
938 BUG("invalid trust level requested %d", level);
939
940 trust = &sigcheck_gpg_trust_level[level];
941 if (trust->value != level)
942 BUG("sigcheck_gpg_trust_level[] unsorted");
943
944 return sigcheck_gpg_trust_level[level].display_key;
945 }
946
947 int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *signing_key)
948 {
949 gpg_interface_lazy_init();
950
951 return use_format->sign_buffer(buffer, signature, signing_key);
952 }
953
954 /*
955 * Strip CR from the line endings, in case we are on Windows.
956 * NEEDSWORK: make it trim only CRs before LFs and rename
957 */
958 static void remove_cr_after(struct strbuf *buffer, size_t offset)
959 {
960 size_t i, j;
961
962 for (i = j = offset; i < buffer->len; i++) {
963 if (buffer->buf[i] != '\r') {
964 if (i != j)
965 buffer->buf[j] = buffer->buf[i];
966 j++;
967 }
968 }
969 strbuf_setlen(buffer, j);
970 }
971
972 static int sign_buffer_gpg(struct strbuf *buffer, struct strbuf *signature,
973 const char *signing_key)
974 {
975 struct child_process gpg = CHILD_PROCESS_INIT;
976 int ret;
977 size_t bottom;
978 const char *cp;
979 struct strbuf gpg_status = STRBUF_INIT;
980
981 strvec_pushl(&gpg.args,
982 use_format->program,
983 "--status-fd=2",
984 "-bsau", signing_key,
985 NULL);
986
987 bottom = signature->len;
988
989 /*
990 * When the username signingkey is bad, program could be terminated
991 * because gpg exits without reading and then write gets SIGPIPE.
992 */
993 sigchain_push(SIGPIPE, SIG_IGN);
994 ret = pipe_command(&gpg, buffer->buf, buffer->len,
995 signature, 1024, &gpg_status, 0);
996 sigchain_pop(SIGPIPE);
997
998 for (cp = gpg_status.buf;
999 cp && (cp = strstr(cp, "[GNUPG:] SIG_CREATED "));
1000 cp++) {
1001 if (cp == gpg_status.buf || cp[-1] == '\n')
1002 break; /* found */
1003 }
1004 ret |= !cp;
1005 if (ret) {
1006 error(_("gpg failed to sign the data:\n%s"),
1007 gpg_status.len ? gpg_status.buf : "(no gpg output)");
1008 strbuf_release(&gpg_status);
1009 return -1;
1010 }
1011 strbuf_release(&gpg_status);
1012
1013 /* Strip CR from the line endings, in case we are on Windows. */
1014 remove_cr_after(signature, bottom);
1015
1016 return 0;
1017 }
1018
1019 static int sign_buffer_ssh(struct strbuf *buffer, struct strbuf *signature,
1020 const char *signing_key)
1021 {
1022 struct child_process signer = CHILD_PROCESS_INIT;
1023 int ret = -1;
1024 size_t bottom, keylen;
1025 struct strbuf signer_stderr = STRBUF_INIT;
1026 struct tempfile *key_file = NULL, *buffer_file = NULL;
1027 char *ssh_signing_key_file = NULL;
1028 struct strbuf ssh_signature_filename = STRBUF_INIT;
1029 const char *literal_key = NULL;
1030 int literal_ssh_key = 0;
1031
1032 if (!signing_key || signing_key[0] == '\0')
1033 return error(
1034 _("user.signingKey needs to be set for ssh signing"));
1035
1036 if (is_literal_ssh_key(signing_key, &literal_key)) {
1037 /* A literal ssh key */
1038 literal_ssh_key = 1;
1039 key_file = mks_tempfile_t(".git_signing_key_tmpXXXXXX");
1040 if (!key_file)
1041 return error_errno(
1042 _("could not create temporary file"));
1043 keylen = strlen(literal_key);
1044 if (write_in_full(key_file->fd, literal_key, keylen) < 0 ||
1045 close_tempfile_gently(key_file) < 0) {
1046 error_errno(_("failed writing ssh signing key to '%s'"),
1047 key_file->filename.buf);
1048 goto out;
1049 }
1050 ssh_signing_key_file = strbuf_detach(&key_file->filename, NULL);
1051 } else {
1052 /* We assume a file */
1053 ssh_signing_key_file = interpolate_path(signing_key, 1);
1054 }
1055
1056 buffer_file = mks_tempfile_t(".git_signing_buffer_tmpXXXXXX");
1057 if (!buffer_file) {
1058 error_errno(_("could not create temporary file"));
1059 goto out;
1060 }
1061
1062 if (write_in_full(buffer_file->fd, buffer->buf, buffer->len) < 0 ||
1063 close_tempfile_gently(buffer_file) < 0) {
1064 error_errno(_("failed writing ssh signing key buffer to '%s'"),
1065 buffer_file->filename.buf);
1066 goto out;
1067 }
1068
1069 strvec_pushl(&signer.args, use_format->program,
1070 "-Y", "sign",
1071 "-n", "git",
1072 "-f", ssh_signing_key_file,
1073 NULL);
1074 if (literal_ssh_key)
1075 strvec_push(&signer.args, "-U");
1076 strvec_push(&signer.args, buffer_file->filename.buf);
1077
1078 sigchain_push(SIGPIPE, SIG_IGN);
1079 ret = pipe_command(&signer, NULL, 0, NULL, 0, &signer_stderr, 0);
1080 sigchain_pop(SIGPIPE);
1081
1082 if (ret) {
1083 if (strstr(signer_stderr.buf, "usage:"))
1084 error(_("ssh-keygen -Y sign is needed for ssh signing (available in openssh version 8.2p1+)"));
1085
1086 error("%s", signer_stderr.buf);
1087 goto out;
1088 }
1089
1090 bottom = signature->len;
1091
1092 strbuf_addbuf(&ssh_signature_filename, &buffer_file->filename);
1093 strbuf_addstr(&ssh_signature_filename, ".sig");
1094 if (strbuf_read_file(signature, ssh_signature_filename.buf, 0) < 0) {
1095 ret = error_errno(
1096 _("failed reading ssh signing data buffer from '%s'"),
1097 ssh_signature_filename.buf);
1098 goto out;
1099 }
1100 /* Strip CR from the line endings, in case we are on Windows. */
1101 remove_cr_after(signature, bottom);
1102
1103 out:
1104 if (key_file)
1105 delete_tempfile(&key_file);
1106 if (buffer_file)
1107 delete_tempfile(&buffer_file);
1108 if (ssh_signature_filename.len)
1109 unlink_or_warn(ssh_signature_filename.buf);
1110 strbuf_release(&signer_stderr);
1111 strbuf_release(&ssh_signature_filename);
1112 FREE_AND_NULL(ssh_signing_key_file);
1113 return ret;
1114 }