]> git.ipfire.org Git - thirdparty/git.git/blame - gpg-interface.c
ssh signing: retrieve a default key from ssh-agent
[thirdparty/git.git] / gpg-interface.c
CommitLineData
2f47eae2 1#include "cache.h"
9b27b492 2#include "commit.h"
b2141fc1 3#include "config.h"
2f47eae2
JH
4#include "run-command.h"
5#include "strbuf.h"
6#include "gpg-interface.h"
7#include "sigchain.h"
4322353b 8#include "tempfile.h"
fd9e2267 9#include "alias.h"
2f47eae2
JH
10
11static char *configured_signing_key;
fd9e2267 12static const char *ssh_default_key_command;
54887b46
HJI
13static enum signature_trust_level configured_min_trust_level = TRUST_UNDEFINED;
14
58af57e1
HS
15struct gpg_format {
16 const char *name;
17 const char *program;
18 const char **verify_args;
19 const char **sigs;
b5726a5d
FS
20 int (*verify_signed_buffer)(struct signature_check *sigc,
21 struct gpg_format *fmt, const char *payload,
22 size_t payload_size, const char *signature,
23 size_t signature_size);
24 int (*sign_buffer)(struct strbuf *buffer, struct strbuf *signature,
25 const char *signing_key);
fd9e2267 26 const char *(*get_default_key)(void);
58af57e1
HS
27};
28
29static const char *openpgp_verify_args[] = {
30 "--keyid-format=long",
31 NULL
32};
33static const char *openpgp_sigs[] = {
34 "-----BEGIN PGP SIGNATURE-----",
35 "-----BEGIN PGP MESSAGE-----",
36 NULL
37};
38
1e7adb97
HS
39static const char *x509_verify_args[] = {
40 NULL
41};
42static const char *x509_sigs[] = {
43 "-----BEGIN SIGNED MESSAGE-----",
44 NULL
45};
46
29b31577
FS
47static const char *ssh_verify_args[] = { NULL };
48static const char *ssh_sigs[] = {
49 "-----BEGIN SSH SIGNATURE-----",
50 NULL
51};
52
b5726a5d
FS
53static int verify_gpg_signed_buffer(struct signature_check *sigc,
54 struct gpg_format *fmt, const char *payload,
55 size_t payload_size, const char *signature,
56 size_t signature_size);
57static int sign_buffer_gpg(struct strbuf *buffer, struct strbuf *signature,
58 const char *signing_key);
29b31577
FS
59static int sign_buffer_ssh(struct strbuf *buffer, struct strbuf *signature,
60 const char *signing_key);
b5726a5d 61
fd9e2267
FS
62static const char *get_default_ssh_signing_key(void);
63
58af57e1 64static struct gpg_format gpg_format[] = {
b5726a5d
FS
65 {
66 .name = "openpgp",
67 .program = "gpg",
68 .verify_args = openpgp_verify_args,
69 .sigs = openpgp_sigs,
70 .verify_signed_buffer = verify_gpg_signed_buffer,
71 .sign_buffer = sign_buffer_gpg,
fd9e2267 72 .get_default_key = NULL,
58af57e1 73 },
b5726a5d
FS
74 {
75 .name = "x509",
76 .program = "gpgsm",
77 .verify_args = x509_verify_args,
78 .sigs = x509_sigs,
79 .verify_signed_buffer = verify_gpg_signed_buffer,
80 .sign_buffer = sign_buffer_gpg,
fd9e2267 81 .get_default_key = NULL,
1e7adb97 82 },
29b31577
FS
83 {
84 .name = "ssh",
85 .program = "ssh-keygen",
86 .verify_args = ssh_verify_args,
87 .sigs = ssh_sigs,
88 .verify_signed_buffer = NULL, /* TODO */
fd9e2267
FS
89 .sign_buffer = sign_buffer_ssh,
90 .get_default_key = get_default_ssh_signing_key,
29b31577 91 },
58af57e1
HS
92};
93
94static struct gpg_format *use_format = &gpg_format[0];
2f47eae2 95
58af57e1
HS
96static struct gpg_format *get_format_by_name(const char *str)
97{
98 int i;
99
100 for (i = 0; i < ARRAY_SIZE(gpg_format); i++)
101 if (!strcmp(gpg_format[i].name, str))
102 return gpg_format + i;
103 return NULL;
104}
105
106static struct gpg_format *get_format_by_sig(const char *sig)
107{
108 int i, j;
109
110 for (i = 0; i < ARRAY_SIZE(gpg_format); i++)
111 for (j = 0; gpg_format[i].sigs[j]; j++)
112 if (starts_with(sig, gpg_format[i].sigs[j]))
113 return gpg_format + i;
114 return NULL;
115}
d7c67668 116
01e57b5d
MG
117void signature_check_clear(struct signature_check *sigc)
118{
88ce3ef6 119 FREE_AND_NULL(sigc->payload);
b5726a5d 120 FREE_AND_NULL(sigc->output);
88ce3ef6
ÆAB
121 FREE_AND_NULL(sigc->gpg_status);
122 FREE_AND_NULL(sigc->signer);
123 FREE_AND_NULL(sigc->key);
3daaaabe 124 FREE_AND_NULL(sigc->fingerprint);
4de9394d 125 FREE_AND_NULL(sigc->primary_key_fingerprint);
01e57b5d
MG
126}
127
da6cf1b3
MG
128/* An exclusive status -- only one of them can appear in output */
129#define GPG_STATUS_EXCLUSIVE (1<<0)
0b11a84e
MG
130/* The status includes key identifier */
131#define GPG_STATUS_KEYID (1<<1)
132/* The status includes user identifier */
133#define GPG_STATUS_UID (1<<2)
3daaaabe
MG
134/* The status includes key fingerprints */
135#define GPG_STATUS_FINGERPRINT (1<<3)
54887b46
HJI
136/* The status includes trust level */
137#define GPG_STATUS_TRUST_LEVEL (1<<4)
0b11a84e
MG
138
139/* Short-hand for standard exclusive *SIG status with keyid & UID */
140#define GPG_STATUS_STDSIG (GPG_STATUS_EXCLUSIVE|GPG_STATUS_KEYID|GPG_STATUS_UID)
da6cf1b3 141
a50e7ca3
JH
142static struct {
143 char result;
144 const char *check;
da6cf1b3 145 unsigned int flags;
a50e7ca3 146} sigcheck_gpg_status[] = {
0b11a84e
MG
147 { 'G', "GOODSIG ", GPG_STATUS_STDSIG },
148 { 'B', "BADSIG ", GPG_STATUS_STDSIG },
0b11a84e
MG
149 { 'E', "ERRSIG ", GPG_STATUS_EXCLUSIVE|GPG_STATUS_KEYID },
150 { 'X', "EXPSIG ", GPG_STATUS_STDSIG },
151 { 'Y', "EXPKEYSIG ", GPG_STATUS_STDSIG },
152 { 'R', "REVKEYSIG ", GPG_STATUS_STDSIG },
3daaaabe 153 { 0, "VALIDSIG ", GPG_STATUS_FINGERPRINT },
54887b46
HJI
154 { 0, "TRUST_", GPG_STATUS_TRUST_LEVEL },
155};
156
157static struct {
158 const char *key;
159 enum signature_trust_level value;
160} sigcheck_gpg_trust_level[] = {
161 { "UNDEFINED", TRUST_UNDEFINED },
162 { "NEVER", TRUST_NEVER },
163 { "MARGINAL", TRUST_MARGINAL },
164 { "FULLY", TRUST_FULLY },
165 { "ULTIMATE", TRUST_ULTIMATE },
a50e7ca3
JH
166};
167
392b862e
HJI
168static void replace_cstring(char **field, const char *line, const char *next)
169{
170 free(*field);
171
172 if (line && next)
173 *field = xmemdupz(line, next - line);
174 else
175 *field = NULL;
176}
177
54887b46
HJI
178static int parse_gpg_trust_level(const char *level,
179 enum signature_trust_level *res)
180{
181 size_t i;
182
183 for (i = 0; i < ARRAY_SIZE(sigcheck_gpg_trust_level); i++) {
184 if (!strcmp(sigcheck_gpg_trust_level[i].key, level)) {
185 *res = sigcheck_gpg_trust_level[i].value;
186 return 0;
187 }
188 }
189 return 1;
190}
191
fbd0f166 192static void parse_gpg_output(struct signature_check *sigc)
a50e7ca3
JH
193{
194 const char *buf = sigc->gpg_status;
da6cf1b3 195 const char *line, *next;
4de9394d 196 int i, j;
da6cf1b3
MG
197 int seen_exclusive_status = 0;
198
199 /* Iterate over all lines */
200 for (line = buf; *line; line = strchrnul(line+1, '\n')) {
201 while (*line == '\n')
202 line++;
64c45dc7
SR
203 if (!*line)
204 break;
205
da6cf1b3
MG
206 /* Skip lines that don't start with GNUPG status */
207 if (!skip_prefix(line, "[GNUPG:] ", &line))
208 continue;
209
210 /* Iterate over all search strings */
211 for (i = 0; i < ARRAY_SIZE(sigcheck_gpg_status); i++) {
212 if (skip_prefix(line, sigcheck_gpg_status[i].check, &line)) {
54887b46
HJI
213 /*
214 * GOODSIG, BADSIG etc. can occur only once for
215 * each signature. Therefore, if we had more
216 * than one then we're dealing with multiple
217 * signatures. We don't support them
218 * currently, and they're rather hard to
219 * create, so something is likely fishy and we
220 * should reject them altogether.
221 */
da6cf1b3 222 if (sigcheck_gpg_status[i].flags & GPG_STATUS_EXCLUSIVE) {
02561896 223 if (seen_exclusive_status++)
54887b46 224 goto error;
da6cf1b3
MG
225 }
226
3daaaabe
MG
227 if (sigcheck_gpg_status[i].result)
228 sigc->result = sigcheck_gpg_status[i].result;
0b11a84e
MG
229 /* Do we have key information? */
230 if (sigcheck_gpg_status[i].flags & GPG_STATUS_KEYID) {
da6cf1b3 231 next = strchrnul(line, ' ');
392b862e 232 replace_cstring(&sigc->key, line, next);
0b11a84e
MG
233 /* Do we have signer information? */
234 if (*next && (sigcheck_gpg_status[i].flags & GPG_STATUS_UID)) {
da6cf1b3
MG
235 line = next + 1;
236 next = strchrnul(line, '\n');
392b862e 237 replace_cstring(&sigc->signer, line, next);
da6cf1b3
MG
238 }
239 }
54887b46
HJI
240
241 /* Do we have trust level? */
242 if (sigcheck_gpg_status[i].flags & GPG_STATUS_TRUST_LEVEL) {
243 /*
244 * GPG v1 and v2 differs in how the
245 * TRUST_ lines are written. Some
246 * trust lines contain no additional
247 * space-separated information for v1.
248 */
249 size_t trust_size = strcspn(line, " \n");
250 char *trust = xmemdupz(line, trust_size);
251
252 if (parse_gpg_trust_level(trust, &sigc->trust_level)) {
253 free(trust);
254 goto error;
255 }
256 free(trust);
257 }
258
3daaaabe
MG
259 /* Do we have fingerprint? */
260 if (sigcheck_gpg_status[i].flags & GPG_STATUS_FINGERPRINT) {
67a6ea63
HJI
261 const char *limit;
262 char **field;
263
3daaaabe 264 next = strchrnul(line, ' ');
392b862e 265 replace_cstring(&sigc->fingerprint, line, next);
4de9394d 266
67a6ea63
HJI
267 /*
268 * Skip interim fields. The search is
269 * limited to the same line since only
270 * OpenPGP signatures has a field with
271 * the primary fingerprint.
272 */
273 limit = strchrnul(line, '\n');
4de9394d 274 for (j = 9; j > 0; j--) {
67a6ea63 275 if (!*next || limit <= next)
4de9394d
MG
276 break;
277 line = next + 1;
278 next = strchrnul(line, ' ');
279 }
280
67a6ea63
HJI
281 field = &sigc->primary_key_fingerprint;
282 if (!j) {
283 next = strchrnul(line, '\n');
284 replace_cstring(field, line, next);
285 } else {
286 replace_cstring(field, NULL, NULL);
287 }
3daaaabe 288 }
da6cf1b3
MG
289
290 break;
661a1806 291 }
a50e7ca3
JH
292 }
293 }
da6cf1b3
MG
294 return;
295
54887b46 296error:
da6cf1b3
MG
297 sigc->result = 'E';
298 /* Clear partial data to avoid confusion */
4de9394d 299 FREE_AND_NULL(sigc->primary_key_fingerprint);
3daaaabe 300 FREE_AND_NULL(sigc->fingerprint);
da6cf1b3
MG
301 FREE_AND_NULL(sigc->signer);
302 FREE_AND_NULL(sigc->key);
a50e7ca3
JH
303}
304
b5726a5d
FS
305static int verify_gpg_signed_buffer(struct signature_check *sigc,
306 struct gpg_format *fmt, const char *payload,
307 size_t payload_size, const char *signature,
308 size_t signature_size)
67948981
HJI
309{
310 struct child_process gpg = CHILD_PROCESS_INIT;
67948981
HJI
311 struct tempfile *temp;
312 int ret;
b5726a5d
FS
313 struct strbuf gpg_stdout = STRBUF_INIT;
314 struct strbuf gpg_stderr = STRBUF_INIT;
67948981
HJI
315
316 temp = mks_tempfile_t(".git_vtag_tmpXXXXXX");
317 if (!temp)
318 return error_errno(_("could not create temporary file"));
319 if (write_in_full(temp->fd, signature, signature_size) < 0 ||
320 close_tempfile_gently(temp) < 0) {
321 error_errno(_("failed writing detached signature to '%s'"),
322 temp->filename.buf);
323 delete_tempfile(&temp);
324 return -1;
325 }
326
ef8d7ac4
JK
327 strvec_push(&gpg.args, fmt->program);
328 strvec_pushv(&gpg.args, fmt->verify_args);
329 strvec_pushl(&gpg.args,
f6d8942b
JK
330 "--status-fd=1",
331 "--verify", temp->filename.buf, "-",
332 NULL);
67948981 333
67948981 334 sigchain_push(SIGPIPE, SIG_IGN);
b5726a5d
FS
335 ret = pipe_command(&gpg, payload, payload_size, &gpg_stdout, 0,
336 &gpg_stderr, 0);
67948981
HJI
337 sigchain_pop(SIGPIPE);
338
339 delete_tempfile(&temp);
340
b5726a5d
FS
341 ret |= !strstr(gpg_stdout.buf, "\n[GNUPG:] GOODSIG ");
342 sigc->payload = xmemdupz(payload, payload_size);
343 sigc->output = strbuf_detach(&gpg_stderr, NULL);
344 sigc->gpg_status = strbuf_detach(&gpg_stdout, NULL);
345
346 parse_gpg_output(sigc);
347
348 strbuf_release(&gpg_stdout);
349 strbuf_release(&gpg_stderr);
67948981
HJI
350
351 return ret;
352}
353
434060ec 354int check_signature(const char *payload, size_t plen, const char *signature,
a4cc18f2 355 size_t slen, struct signature_check *sigc)
356{
b5726a5d 357 struct gpg_format *fmt;
a4cc18f2 358 int status;
359
360 sigc->result = 'N';
54887b46 361 sigc->trust_level = -1;
a4cc18f2 362
b5726a5d
FS
363 fmt = get_format_by_sig(signature);
364 if (!fmt)
365 die(_("bad/incompatible signature '%s'"), signature);
366
367 status = fmt->verify_signed_buffer(sigc, fmt, payload, plen, signature,
368 slen);
369
370 if (status && !sigc->output)
371 return !!status;
372
54887b46
HJI
373 status |= sigc->result != 'G';
374 status |= sigc->trust_level < configured_min_trust_level;
a4cc18f2 375
4e5dc9ca 376 return !!status;
a4cc18f2 377}
378
ca194d50 379void print_signature_buffer(const struct signature_check *sigc, unsigned flags)
380{
b5726a5d
FS
381 const char *output = flags & GPG_VERIFY_RAW ? sigc->gpg_status :
382 sigc->output;
aeff29dd 383
ca194d50 384 if (flags & GPG_VERIFY_VERBOSE && sigc->payload)
385 fputs(sigc->payload, stdout);
386
aeff29dd 387 if (output)
388 fputs(output, stderr);
ca194d50 389}
390
482c1191 391size_t parse_signed_buffer(const char *buf, size_t size)
d7c67668 392{
d7c67668 393 size_t len = 0;
8b44b2be
JK
394 size_t match = size;
395 while (len < size) {
396 const char *eol;
397
58af57e1 398 if (get_format_by_sig(buf + len))
8b44b2be
JK
399 match = len;
400
401 eol = memchr(buf + len, '\n', size - len);
d7c67668
JH
402 len += eol ? eol - (buf + len) + 1 : size - len;
403 }
8b44b2be 404 return match;
d7c67668
JH
405}
406
482c1191 407int parse_signature(const char *buf, size_t size, struct strbuf *payload, struct strbuf *signature)
408{
409 size_t match = parse_signed_buffer(buf, size);
410 if (match != size) {
411 strbuf_add(payload, buf, match);
9b27b492 412 remove_signature(payload);
482c1191 413 strbuf_add(signature, buf + match, size - match);
414 return 1;
415 }
416 return 0;
417}
418
2f47eae2
JH
419void set_signing_key(const char *key)
420{
421 free(configured_signing_key);
422 configured_signing_key = xstrdup(key);
423}
424
425int git_gpg_config(const char *var, const char *value, void *cb)
426{
58af57e1
HS
427 struct gpg_format *fmt = NULL;
428 char *fmtname = NULL;
54887b46
HJI
429 char *trust;
430 int ret;
58af57e1 431
2f47eae2 432 if (!strcmp(var, "user.signingkey")) {
1b0eeec3
JK
433 if (!value)
434 return config_error_nonbool(var);
0c5e70f0 435 set_signing_key(value);
1b0eeec3 436 return 0;
0c5e70f0 437 }
1b0eeec3 438
57a8dd75
HS
439 if (!strcmp(var, "gpg.format")) {
440 if (!value)
441 return config_error_nonbool(var);
58af57e1
HS
442 fmt = get_format_by_name(value);
443 if (!fmt)
57a8dd75
HS
444 return error("unsupported value for %s: %s",
445 var, value);
58af57e1
HS
446 use_format = fmt;
447 return 0;
57a8dd75
HS
448 }
449
54887b46
HJI
450 if (!strcmp(var, "gpg.mintrustlevel")) {
451 if (!value)
452 return config_error_nonbool(var);
453
454 trust = xstrdup_toupper(value);
455 ret = parse_gpg_trust_level(trust, &configured_min_trust_level);
456 free(trust);
457
458 if (ret)
459 return error("unsupported value for %s: %s", var,
460 value);
461 return 0;
462 }
463
fd9e2267
FS
464 if (!strcmp(var, "gpg.ssh.defaultkeycommand")) {
465 if (!value)
466 return config_error_nonbool(var);
467 return git_config_string(&ssh_default_key_command, var, value);
468 }
469
b02f51b1 470 if (!strcmp(var, "gpg.program") || !strcmp(var, "gpg.openpgp.program"))
58af57e1
HS
471 fmtname = "openpgp";
472
1e7adb97
HS
473 if (!strcmp(var, "gpg.x509.program"))
474 fmtname = "x509";
475
29b31577
FS
476 if (!strcmp(var, "gpg.ssh.program"))
477 fmtname = "ssh";
478
58af57e1
HS
479 if (fmtname) {
480 fmt = get_format_by_name(fmtname);
481 return git_config_string(&fmt->program, var, value);
2f47eae2 482 }
1b0eeec3 483
2f47eae2
JH
484 return 0;
485}
486
fd9e2267
FS
487/* Returns the first public key from an ssh-agent to use for signing */
488static const char *get_default_ssh_signing_key(void)
489{
490 struct child_process ssh_default_key = CHILD_PROCESS_INIT;
491 int ret = -1;
492 struct strbuf key_stdout = STRBUF_INIT, key_stderr = STRBUF_INIT;
493 struct strbuf **keys;
494 char *key_command = NULL;
495 const char **argv;
496 int n;
497 char *default_key = NULL;
498
499 if (!ssh_default_key_command)
500 die(_("either user.signingkey or gpg.ssh.defaultKeyCommand needs to be configured"));
501
502 key_command = xstrdup(ssh_default_key_command);
503 n = split_cmdline(key_command, &argv);
504
505 if (n < 0)
506 die("malformed build-time gpg.ssh.defaultKeyCommand: %s",
507 split_cmdline_strerror(n));
508
509 strvec_pushv(&ssh_default_key.args, argv);
510 ret = pipe_command(&ssh_default_key, NULL, 0, &key_stdout, 0,
511 &key_stderr, 0);
512
513 if (!ret) {
514 keys = strbuf_split_max(&key_stdout, '\n', 2);
515 if (keys[0] && starts_with(keys[0]->buf, "ssh-")) {
516 default_key = strbuf_detach(keys[0], NULL);
517 } else {
518 warning(_("gpg.ssh.defaultKeycommand succeeded but returned no keys: %s %s"),
519 key_stderr.buf, key_stdout.buf);
520 }
521
522 strbuf_list_free(keys);
523 } else {
524 warning(_("gpg.ssh.defaultKeyCommand failed: %s %s"),
525 key_stderr.buf, key_stdout.buf);
526 }
527
528 free(key_command);
529 free(argv);
530 strbuf_release(&key_stdout);
531
532 return default_key;
533}
534
2f47eae2
JH
535const char *get_signing_key(void)
536{
537 if (configured_signing_key)
538 return configured_signing_key;
fd9e2267
FS
539 if (use_format->get_default_key) {
540 return use_format->get_default_key();
541 }
542
543 return git_committer_info(IDENT_STRICT | IDENT_NO_DATE);
2f47eae2
JH
544}
545
2f47eae2 546int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *signing_key)
b5726a5d
FS
547{
548 return use_format->sign_buffer(buffer, signature, signing_key);
549}
550
29b31577
FS
551/*
552 * Strip CR from the line endings, in case we are on Windows.
553 * NEEDSWORK: make it trim only CRs before LFs and rename
554 */
555static void remove_cr_after(struct strbuf *buffer, size_t offset)
556{
557 size_t i, j;
558
559 for (i = j = offset; i < buffer->len; i++) {
560 if (buffer->buf[i] != '\r') {
561 if (i != j)
562 buffer->buf[j] = buffer->buf[i];
563 j++;
564 }
565 }
566 strbuf_setlen(buffer, j);
567}
568
b5726a5d
FS
569static int sign_buffer_gpg(struct strbuf *buffer, struct strbuf *signature,
570 const char *signing_key)
2f47eae2 571{
d3180279 572 struct child_process gpg = CHILD_PROCESS_INIT;
0581b546 573 int ret;
29b31577 574 size_t bottom;
efee9553 575 struct strbuf gpg_status = STRBUF_INIT;
2f47eae2 576
ef8d7ac4 577 strvec_pushl(&gpg.args,
f6d8942b
JK
578 use_format->program,
579 "--status-fd=2",
580 "-bsau", signing_key,
581 NULL);
2f47eae2 582
0581b546 583 bottom = signature->len;
2f47eae2
JH
584
585 /*
586 * When the username signingkey is bad, program could be terminated
587 * because gpg exits without reading and then write gets SIGPIPE.
588 */
589 sigchain_push(SIGPIPE, SIG_IGN);
0581b546 590 ret = pipe_command(&gpg, buffer->buf, buffer->len,
efee9553 591 signature, 1024, &gpg_status, 0);
2f47eae2
JH
592 sigchain_pop(SIGPIPE);
593
efee9553
MG
594 ret |= !strstr(gpg_status.buf, "\n[GNUPG:] SIG_CREATED ");
595 strbuf_release(&gpg_status);
596 if (ret)
2f47eae2
JH
597 return error(_("gpg failed to sign the data"));
598
599 /* Strip CR from the line endings, in case we are on Windows. */
29b31577 600 remove_cr_after(signature, bottom);
2f47eae2
JH
601
602 return 0;
603}
29b31577
FS
604
605static int sign_buffer_ssh(struct strbuf *buffer, struct strbuf *signature,
606 const char *signing_key)
607{
608 struct child_process signer = CHILD_PROCESS_INIT;
609 int ret = -1;
610 size_t bottom, keylen;
611 struct strbuf signer_stderr = STRBUF_INIT;
612 struct tempfile *key_file = NULL, *buffer_file = NULL;
613 char *ssh_signing_key_file = NULL;
614 struct strbuf ssh_signature_filename = STRBUF_INIT;
615
616 if (!signing_key || signing_key[0] == '\0')
617 return error(
618 _("user.signingkey needs to be set for ssh signing"));
619
620 if (starts_with(signing_key, "ssh-")) {
621 /* A literal ssh key */
622 key_file = mks_tempfile_t(".git_signing_key_tmpXXXXXX");
623 if (!key_file)
624 return error_errno(
625 _("could not create temporary file"));
626 keylen = strlen(signing_key);
627 if (write_in_full(key_file->fd, signing_key, keylen) < 0 ||
628 close_tempfile_gently(key_file) < 0) {
629 error_errno(_("failed writing ssh signing key to '%s'"),
630 key_file->filename.buf);
631 goto out;
632 }
633 ssh_signing_key_file = strbuf_detach(&key_file->filename, NULL);
634 } else {
635 /* We assume a file */
636 ssh_signing_key_file = expand_user_path(signing_key, 1);
637 }
638
639 buffer_file = mks_tempfile_t(".git_signing_buffer_tmpXXXXXX");
640 if (!buffer_file) {
641 error_errno(_("could not create temporary file"));
642 goto out;
643 }
644
645 if (write_in_full(buffer_file->fd, buffer->buf, buffer->len) < 0 ||
646 close_tempfile_gently(buffer_file) < 0) {
647 error_errno(_("failed writing ssh signing key buffer to '%s'"),
648 buffer_file->filename.buf);
649 goto out;
650 }
651
652 strvec_pushl(&signer.args, use_format->program,
653 "-Y", "sign",
654 "-n", "git",
655 "-f", ssh_signing_key_file,
656 buffer_file->filename.buf,
657 NULL);
658
659 sigchain_push(SIGPIPE, SIG_IGN);
660 ret = pipe_command(&signer, NULL, 0, NULL, 0, &signer_stderr, 0);
661 sigchain_pop(SIGPIPE);
662
663 if (ret) {
664 if (strstr(signer_stderr.buf, "usage:"))
665 error(_("ssh-keygen -Y sign is needed for ssh signing (available in openssh version 8.2p1+)"));
666
667 error("%s", signer_stderr.buf);
668 goto out;
669 }
670
671 bottom = signature->len;
672
673 strbuf_addbuf(&ssh_signature_filename, &buffer_file->filename);
674 strbuf_addstr(&ssh_signature_filename, ".sig");
675 if (strbuf_read_file(signature, ssh_signature_filename.buf, 0) < 0) {
676 error_errno(
677 _("failed reading ssh signing data buffer from '%s'"),
678 ssh_signature_filename.buf);
679 }
680 unlink_or_warn(ssh_signature_filename.buf);
681
682 /* Strip CR from the line endings, in case we are on Windows. */
683 remove_cr_after(signature, bottom);
684
685out:
686 if (key_file)
687 delete_tempfile(&key_file);
688 if (buffer_file)
689 delete_tempfile(&buffer_file);
690 strbuf_release(&signer_stderr);
691 strbuf_release(&ssh_signature_filename);
692 FREE_AND_NULL(ssh_signing_key_file);
693 return ret;
694}