4 #include "run-command.h"
7 #include "gpg-interface.h"
12 static char *configured_signing_key
;
13 static const char *ssh_default_key_command
, *ssh_allowed_signers
, *ssh_revocation_file
;
14 static enum signature_trust_level configured_min_trust_level
= TRUST_UNDEFINED
;
19 const char **verify_args
;
21 int (*verify_signed_buffer
)(struct signature_check
*sigc
,
22 struct gpg_format
*fmt
,
23 const char *signature
,
24 size_t signature_size
);
25 int (*sign_buffer
)(struct strbuf
*buffer
, struct strbuf
*signature
,
26 const char *signing_key
);
27 const char *(*get_default_key
)(void);
28 const char *(*get_key_id
)(void);
31 static const char *openpgp_verify_args
[] = {
32 "--keyid-format=long",
35 static const char *openpgp_sigs
[] = {
36 "-----BEGIN PGP SIGNATURE-----",
37 "-----BEGIN PGP MESSAGE-----",
41 static const char *x509_verify_args
[] = {
44 static const char *x509_sigs
[] = {
45 "-----BEGIN SIGNED MESSAGE-----",
49 static const char *ssh_verify_args
[] = { NULL
};
50 static const char *ssh_sigs
[] = {
51 "-----BEGIN SSH SIGNATURE-----",
55 static int verify_gpg_signed_buffer(struct signature_check
*sigc
,
56 struct gpg_format
*fmt
,
57 const char *signature
,
58 size_t signature_size
);
59 static int verify_ssh_signed_buffer(struct signature_check
*sigc
,
60 struct gpg_format
*fmt
,
61 const char *signature
,
62 size_t signature_size
);
63 static int sign_buffer_gpg(struct strbuf
*buffer
, struct strbuf
*signature
,
64 const char *signing_key
);
65 static int sign_buffer_ssh(struct strbuf
*buffer
, struct strbuf
*signature
,
66 const char *signing_key
);
68 static const char *get_default_ssh_signing_key(void);
70 static const char *get_ssh_key_id(void);
72 static struct gpg_format gpg_format
[] = {
76 .verify_args
= openpgp_verify_args
,
78 .verify_signed_buffer
= verify_gpg_signed_buffer
,
79 .sign_buffer
= sign_buffer_gpg
,
80 .get_default_key
= NULL
,
86 .verify_args
= x509_verify_args
,
88 .verify_signed_buffer
= verify_gpg_signed_buffer
,
89 .sign_buffer
= sign_buffer_gpg
,
90 .get_default_key
= NULL
,
95 .program
= "ssh-keygen",
96 .verify_args
= ssh_verify_args
,
98 .verify_signed_buffer
= verify_ssh_signed_buffer
,
99 .sign_buffer
= sign_buffer_ssh
,
100 .get_default_key
= get_default_ssh_signing_key
,
101 .get_key_id
= get_ssh_key_id
,
105 static struct gpg_format
*use_format
= &gpg_format
[0];
107 static struct gpg_format
*get_format_by_name(const char *str
)
111 for (i
= 0; i
< ARRAY_SIZE(gpg_format
); i
++)
112 if (!strcmp(gpg_format
[i
].name
, str
))
113 return gpg_format
+ i
;
117 static struct gpg_format
*get_format_by_sig(const char *sig
)
121 for (i
= 0; i
< ARRAY_SIZE(gpg_format
); i
++)
122 for (j
= 0; gpg_format
[i
].sigs
[j
]; j
++)
123 if (starts_with(sig
, gpg_format
[i
].sigs
[j
]))
124 return gpg_format
+ i
;
128 void signature_check_clear(struct signature_check
*sigc
)
130 FREE_AND_NULL(sigc
->payload
);
131 FREE_AND_NULL(sigc
->output
);
132 FREE_AND_NULL(sigc
->gpg_status
);
133 FREE_AND_NULL(sigc
->signer
);
134 FREE_AND_NULL(sigc
->key
);
135 FREE_AND_NULL(sigc
->fingerprint
);
136 FREE_AND_NULL(sigc
->primary_key_fingerprint
);
139 /* An exclusive status -- only one of them can appear in output */
140 #define GPG_STATUS_EXCLUSIVE (1<<0)
141 /* The status includes key identifier */
142 #define GPG_STATUS_KEYID (1<<1)
143 /* The status includes user identifier */
144 #define GPG_STATUS_UID (1<<2)
145 /* The status includes key fingerprints */
146 #define GPG_STATUS_FINGERPRINT (1<<3)
147 /* The status includes trust level */
148 #define GPG_STATUS_TRUST_LEVEL (1<<4)
150 /* Short-hand for standard exclusive *SIG status with keyid & UID */
151 #define GPG_STATUS_STDSIG (GPG_STATUS_EXCLUSIVE|GPG_STATUS_KEYID|GPG_STATUS_UID)
157 } sigcheck_gpg_status
[] = {
158 { 'G', "GOODSIG ", GPG_STATUS_STDSIG
},
159 { 'B', "BADSIG ", GPG_STATUS_STDSIG
},
160 { 'E', "ERRSIG ", GPG_STATUS_EXCLUSIVE
|GPG_STATUS_KEYID
},
161 { 'X', "EXPSIG ", GPG_STATUS_STDSIG
},
162 { 'Y', "EXPKEYSIG ", GPG_STATUS_STDSIG
},
163 { 'R', "REVKEYSIG ", GPG_STATUS_STDSIG
},
164 { 0, "VALIDSIG ", GPG_STATUS_FINGERPRINT
},
165 { 0, "TRUST_", GPG_STATUS_TRUST_LEVEL
},
170 enum signature_trust_level value
;
171 } sigcheck_gpg_trust_level
[] = {
172 { "UNDEFINED", TRUST_UNDEFINED
},
173 { "NEVER", TRUST_NEVER
},
174 { "MARGINAL", TRUST_MARGINAL
},
175 { "FULLY", TRUST_FULLY
},
176 { "ULTIMATE", TRUST_ULTIMATE
},
179 static void replace_cstring(char **field
, const char *line
, const char *next
)
184 *field
= xmemdupz(line
, next
- line
);
189 static int parse_gpg_trust_level(const char *level
,
190 enum signature_trust_level
*res
)
194 for (i
= 0; i
< ARRAY_SIZE(sigcheck_gpg_trust_level
); i
++) {
195 if (!strcmp(sigcheck_gpg_trust_level
[i
].key
, level
)) {
196 *res
= sigcheck_gpg_trust_level
[i
].value
;
203 static void parse_gpg_output(struct signature_check
*sigc
)
205 const char *buf
= sigc
->gpg_status
;
206 const char *line
, *next
;
208 int seen_exclusive_status
= 0;
210 /* Iterate over all lines */
211 for (line
= buf
; *line
; line
= strchrnul(line
+1, '\n')) {
212 while (*line
== '\n')
217 /* Skip lines that don't start with GNUPG status */
218 if (!skip_prefix(line
, "[GNUPG:] ", &line
))
221 /* Iterate over all search strings */
222 for (i
= 0; i
< ARRAY_SIZE(sigcheck_gpg_status
); i
++) {
223 if (skip_prefix(line
, sigcheck_gpg_status
[i
].check
, &line
)) {
225 * GOODSIG, BADSIG etc. can occur only once for
226 * each signature. Therefore, if we had more
227 * than one then we're dealing with multiple
228 * signatures. We don't support them
229 * currently, and they're rather hard to
230 * create, so something is likely fishy and we
231 * should reject them altogether.
233 if (sigcheck_gpg_status
[i
].flags
& GPG_STATUS_EXCLUSIVE
) {
234 if (seen_exclusive_status
++)
238 if (sigcheck_gpg_status
[i
].result
)
239 sigc
->result
= sigcheck_gpg_status
[i
].result
;
240 /* Do we have key information? */
241 if (sigcheck_gpg_status
[i
].flags
& GPG_STATUS_KEYID
) {
242 next
= strchrnul(line
, ' ');
243 replace_cstring(&sigc
->key
, line
, next
);
244 /* Do we have signer information? */
245 if (*next
&& (sigcheck_gpg_status
[i
].flags
& GPG_STATUS_UID
)) {
247 next
= strchrnul(line
, '\n');
248 replace_cstring(&sigc
->signer
, line
, next
);
252 /* Do we have trust level? */
253 if (sigcheck_gpg_status
[i
].flags
& GPG_STATUS_TRUST_LEVEL
) {
255 * GPG v1 and v2 differs in how the
256 * TRUST_ lines are written. Some
257 * trust lines contain no additional
258 * space-separated information for v1.
260 size_t trust_size
= strcspn(line
, " \n");
261 char *trust
= xmemdupz(line
, trust_size
);
263 if (parse_gpg_trust_level(trust
, &sigc
->trust_level
)) {
270 /* Do we have fingerprint? */
271 if (sigcheck_gpg_status
[i
].flags
& GPG_STATUS_FINGERPRINT
) {
275 next
= strchrnul(line
, ' ');
276 replace_cstring(&sigc
->fingerprint
, line
, next
);
279 * Skip interim fields. The search is
280 * limited to the same line since only
281 * OpenPGP signatures has a field with
282 * the primary fingerprint.
284 limit
= strchrnul(line
, '\n');
285 for (j
= 9; j
> 0; j
--) {
286 if (!*next
|| limit
<= next
)
289 next
= strchrnul(line
, ' ');
292 field
= &sigc
->primary_key_fingerprint
;
294 next
= strchrnul(line
, '\n');
295 replace_cstring(field
, line
, next
);
297 replace_cstring(field
, NULL
, NULL
);
309 /* Clear partial data to avoid confusion */
310 FREE_AND_NULL(sigc
->primary_key_fingerprint
);
311 FREE_AND_NULL(sigc
->fingerprint
);
312 FREE_AND_NULL(sigc
->signer
);
313 FREE_AND_NULL(sigc
->key
);
316 static int verify_gpg_signed_buffer(struct signature_check
*sigc
,
317 struct gpg_format
*fmt
,
318 const char *signature
,
319 size_t signature_size
)
321 struct child_process gpg
= CHILD_PROCESS_INIT
;
322 struct tempfile
*temp
;
324 struct strbuf gpg_stdout
= STRBUF_INIT
;
325 struct strbuf gpg_stderr
= STRBUF_INIT
;
327 temp
= mks_tempfile_t(".git_vtag_tmpXXXXXX");
329 return error_errno(_("could not create temporary file"));
330 if (write_in_full(temp
->fd
, signature
, signature_size
) < 0 ||
331 close_tempfile_gently(temp
) < 0) {
332 error_errno(_("failed writing detached signature to '%s'"),
334 delete_tempfile(&temp
);
338 strvec_push(&gpg
.args
, fmt
->program
);
339 strvec_pushv(&gpg
.args
, fmt
->verify_args
);
340 strvec_pushl(&gpg
.args
,
342 "--verify", temp
->filename
.buf
, "-",
345 sigchain_push(SIGPIPE
, SIG_IGN
);
346 ret
= pipe_command(&gpg
, sigc
->payload
, sigc
->payload_len
, &gpg_stdout
, 0,
348 sigchain_pop(SIGPIPE
);
350 delete_tempfile(&temp
);
352 ret
|= !strstr(gpg_stdout
.buf
, "\n[GNUPG:] GOODSIG ");
353 sigc
->output
= strbuf_detach(&gpg_stderr
, NULL
);
354 sigc
->gpg_status
= strbuf_detach(&gpg_stdout
, NULL
);
356 parse_gpg_output(sigc
);
358 strbuf_release(&gpg_stdout
);
359 strbuf_release(&gpg_stderr
);
364 static void parse_ssh_output(struct signature_check
*sigc
)
366 const char *line
, *principal
, *search
;
371 * ssh-keygen output should be:
372 * Good "git" signature for PRINCIPAL with RSA key SHA256:FINGERPRINT
374 * or for valid but unknown keys:
375 * Good "git" signature with RSA key SHA256:FINGERPRINT
377 * Note that "PRINCIPAL" can contain whitespace, "RSA" and
378 * "SHA256" part could be a different token that names of
379 * the algorithms used, and "FINGERPRINT" is a hexadecimal
380 * string. By finding the last occurence of " with ", we can
381 * reliably parse out the PRINCIPAL.
384 sigc
->trust_level
= TRUST_NEVER
;
386 line
= to_free
= xmemdupz(sigc
->output
, strcspn(sigc
->output
, "\n"));
388 if (skip_prefix(line
, "Good \"git\" signature for ", &line
)) {
389 /* Search for the last "with" to get the full principal */
392 search
= strstr(line
, " with ");
395 } while (search
!= NULL
);
396 if (line
== principal
)
399 /* Valid signature and known principal */
401 sigc
->trust_level
= TRUST_FULLY
;
402 sigc
->signer
= xmemdupz(principal
, line
- principal
- 1);
403 } else if (skip_prefix(line
, "Good \"git\" signature with ", &line
)) {
404 /* Valid signature, but key unknown */
406 sigc
->trust_level
= TRUST_UNDEFINED
;
411 key
= strstr(line
, "key ");
413 sigc
->fingerprint
= xstrdup(strstr(line
, "key ") + 4);
414 sigc
->key
= xstrdup(sigc
->fingerprint
);
417 * Output did not match what we expected
418 * Treat the signature as bad
427 static int verify_ssh_signed_buffer(struct signature_check
*sigc
,
428 struct gpg_format
*fmt
,
429 const char *signature
,
430 size_t signature_size
)
432 struct child_process ssh_keygen
= CHILD_PROCESS_INIT
;
433 struct tempfile
*buffer_file
;
438 struct strbuf ssh_principals_out
= STRBUF_INIT
;
439 struct strbuf ssh_principals_err
= STRBUF_INIT
;
440 struct strbuf ssh_keygen_out
= STRBUF_INIT
;
441 struct strbuf ssh_keygen_err
= STRBUF_INIT
;
442 struct strbuf verify_time
= STRBUF_INIT
;
443 const struct date_mode verify_date_mode
= {
444 .type
= DATE_STRFTIME
,
445 .strftime_fmt
= "%Y%m%d%H%M%S",
446 /* SSH signing key validity has no timezone information - Use the local timezone */
450 if (!ssh_allowed_signers
) {
451 error(_("gpg.ssh.allowedSignersFile needs to be configured and exist for ssh signature verification"));
455 buffer_file
= mks_tempfile_t(".git_vtag_tmpXXXXXX");
457 return error_errno(_("could not create temporary file"));
458 if (write_in_full(buffer_file
->fd
, signature
, signature_size
) < 0 ||
459 close_tempfile_gently(buffer_file
) < 0) {
460 error_errno(_("failed writing detached signature to '%s'"),
461 buffer_file
->filename
.buf
);
462 delete_tempfile(&buffer_file
);
466 if (sigc
->payload_timestamp
)
467 strbuf_addf(&verify_time
, "-Overify-time=%s",
468 show_date(sigc
->payload_timestamp
, 0, &verify_date_mode
));
470 /* Find the principal from the signers */
471 strvec_pushl(&ssh_keygen
.args
, fmt
->program
,
472 "-Y", "find-principals",
473 "-f", ssh_allowed_signers
,
474 "-s", buffer_file
->filename
.buf
,
477 ret
= pipe_command(&ssh_keygen
, NULL
, 0, &ssh_principals_out
, 0,
478 &ssh_principals_err
, 0);
479 if (ret
&& strstr(ssh_principals_err
.buf
, "usage:")) {
480 error(_("ssh-keygen -Y find-principals/verify is needed for ssh signature verification (available in openssh version 8.2p1+)"));
483 if (ret
|| !ssh_principals_out
.len
) {
485 * We did not find a matching principal in the allowedSigners
486 * Check without validation
488 child_process_init(&ssh_keygen
);
489 strvec_pushl(&ssh_keygen
.args
, fmt
->program
,
490 "-Y", "check-novalidate",
492 "-s", buffer_file
->filename
.buf
,
495 pipe_command(&ssh_keygen
, sigc
->payload
, sigc
->payload_len
,
496 &ssh_keygen_out
, 0, &ssh_keygen_err
, 0);
499 * Fail on unknown keys
500 * we still call check-novalidate to display the signature info
504 /* Check every principal we found (one per line) */
505 for (line
= ssh_principals_out
.buf
; *line
;
506 line
= strchrnul(line
+ 1, '\n')) {
507 while (*line
== '\n')
512 trust_size
= strcspn(line
, "\n");
513 principal
= xmemdupz(line
, trust_size
);
515 child_process_init(&ssh_keygen
);
516 strbuf_release(&ssh_keygen_out
);
517 strbuf_release(&ssh_keygen_err
);
518 strvec_push(&ssh_keygen
.args
, fmt
->program
);
520 * We found principals
521 * Try with each until we find a match
523 strvec_pushl(&ssh_keygen
.args
, "-Y", "verify",
525 "-f", ssh_allowed_signers
,
527 "-s", buffer_file
->filename
.buf
,
531 if (ssh_revocation_file
) {
532 if (file_exists(ssh_revocation_file
)) {
533 strvec_pushl(&ssh_keygen
.args
, "-r",
534 ssh_revocation_file
, NULL
);
536 warning(_("ssh signing revocation file configured but not found: %s"),
537 ssh_revocation_file
);
541 sigchain_push(SIGPIPE
, SIG_IGN
);
542 ret
= pipe_command(&ssh_keygen
, sigc
->payload
, sigc
->payload_len
,
543 &ssh_keygen_out
, 0, &ssh_keygen_err
, 0);
544 sigchain_pop(SIGPIPE
);
546 FREE_AND_NULL(principal
);
549 ret
= !starts_with(ssh_keygen_out
.buf
, "Good");
556 strbuf_stripspace(&ssh_keygen_out
, 0);
557 strbuf_stripspace(&ssh_keygen_err
, 0);
558 /* Add stderr outputs to show the user actual ssh-keygen errors */
559 strbuf_add(&ssh_keygen_out
, ssh_principals_err
.buf
, ssh_principals_err
.len
);
560 strbuf_add(&ssh_keygen_out
, ssh_keygen_err
.buf
, ssh_keygen_err
.len
);
561 sigc
->output
= strbuf_detach(&ssh_keygen_out
, NULL
);
562 sigc
->gpg_status
= xstrdup(sigc
->output
);
564 parse_ssh_output(sigc
);
568 delete_tempfile(&buffer_file
);
569 strbuf_release(&ssh_principals_out
);
570 strbuf_release(&ssh_principals_err
);
571 strbuf_release(&ssh_keygen_out
);
572 strbuf_release(&ssh_keygen_err
);
573 strbuf_release(&verify_time
);
578 static int parse_payload_metadata(struct signature_check
*sigc
)
580 const char *ident_line
= NULL
;
582 struct ident_split ident
;
583 const char *signer_header
;
585 switch (sigc
->payload_type
) {
586 case SIGNATURE_PAYLOAD_COMMIT
:
587 signer_header
= "committer";
589 case SIGNATURE_PAYLOAD_TAG
:
590 signer_header
= "tagger";
592 case SIGNATURE_PAYLOAD_UNDEFINED
:
593 case SIGNATURE_PAYLOAD_PUSH_CERT
:
594 /* Ignore payloads we don't want to parse */
597 BUG("invalid value for sigc->payload_type");
600 ident_line
= find_commit_header(sigc
->payload
, signer_header
, &ident_len
);
601 if (!ident_line
|| !ident_len
)
604 if (split_ident_line(&ident
, ident_line
, ident_len
))
607 if (!sigc
->payload_timestamp
&& ident
.date_begin
&& ident
.date_end
)
608 sigc
->payload_timestamp
= parse_timestamp(ident
.date_begin
, NULL
, 10);
613 int check_signature(struct signature_check
*sigc
,
614 const char *signature
, size_t slen
)
616 struct gpg_format
*fmt
;
620 sigc
->trust_level
= -1;
622 fmt
= get_format_by_sig(signature
);
624 die(_("bad/incompatible signature '%s'"), signature
);
626 if (parse_payload_metadata(sigc
))
629 status
= fmt
->verify_signed_buffer(sigc
, fmt
, signature
, slen
);
631 if (status
&& !sigc
->output
)
634 status
|= sigc
->result
!= 'G';
635 status
|= sigc
->trust_level
< configured_min_trust_level
;
640 void print_signature_buffer(const struct signature_check
*sigc
, unsigned flags
)
642 const char *output
= flags
& GPG_VERIFY_RAW
? sigc
->gpg_status
:
645 if (flags
& GPG_VERIFY_VERBOSE
&& sigc
->payload
)
646 fwrite(sigc
->payload
, 1, sigc
->payload_len
, stdout
);
649 fputs(output
, stderr
);
652 size_t parse_signed_buffer(const char *buf
, size_t size
)
659 if (get_format_by_sig(buf
+ len
))
662 eol
= memchr(buf
+ len
, '\n', size
- len
);
663 len
+= eol
? eol
- (buf
+ len
) + 1 : size
- len
;
668 int parse_signature(const char *buf
, size_t size
, struct strbuf
*payload
, struct strbuf
*signature
)
670 size_t match
= parse_signed_buffer(buf
, size
);
672 strbuf_add(payload
, buf
, match
);
673 remove_signature(payload
);
674 strbuf_add(signature
, buf
+ match
, size
- match
);
680 void set_signing_key(const char *key
)
682 free(configured_signing_key
);
683 configured_signing_key
= xstrdup(key
);
686 int git_gpg_config(const char *var
, const char *value
, void *cb
)
688 struct gpg_format
*fmt
= NULL
;
689 char *fmtname
= NULL
;
693 if (!strcmp(var
, "user.signingkey")) {
695 return config_error_nonbool(var
);
696 set_signing_key(value
);
700 if (!strcmp(var
, "gpg.format")) {
702 return config_error_nonbool(var
);
703 fmt
= get_format_by_name(value
);
705 return error("unsupported value for %s: %s",
711 if (!strcmp(var
, "gpg.mintrustlevel")) {
713 return config_error_nonbool(var
);
715 trust
= xstrdup_toupper(value
);
716 ret
= parse_gpg_trust_level(trust
, &configured_min_trust_level
);
720 return error("unsupported value for %s: %s", var
,
725 if (!strcmp(var
, "gpg.ssh.defaultkeycommand")) {
727 return config_error_nonbool(var
);
728 return git_config_string(&ssh_default_key_command
, var
, value
);
731 if (!strcmp(var
, "gpg.ssh.allowedsignersfile")) {
733 return config_error_nonbool(var
);
734 return git_config_pathname(&ssh_allowed_signers
, var
, value
);
737 if (!strcmp(var
, "gpg.ssh.revocationfile")) {
739 return config_error_nonbool(var
);
740 return git_config_pathname(&ssh_revocation_file
, var
, value
);
743 if (!strcmp(var
, "gpg.program") || !strcmp(var
, "gpg.openpgp.program"))
746 if (!strcmp(var
, "gpg.x509.program"))
749 if (!strcmp(var
, "gpg.ssh.program"))
753 fmt
= get_format_by_name(fmtname
);
754 return git_config_string(&fmt
->program
, var
, value
);
760 static char *get_ssh_key_fingerprint(const char *signing_key
)
762 struct child_process ssh_keygen
= CHILD_PROCESS_INIT
;
764 struct strbuf fingerprint_stdout
= STRBUF_INIT
;
765 struct strbuf
**fingerprint
;
766 char *fingerprint_ret
;
769 * With SSH Signing this can contain a filename or a public key
770 * For textual representation we usually want a fingerprint
772 if (starts_with(signing_key
, "ssh-")) {
773 strvec_pushl(&ssh_keygen
.args
, "ssh-keygen", "-lf", "-", NULL
);
774 ret
= pipe_command(&ssh_keygen
, signing_key
,
775 strlen(signing_key
), &fingerprint_stdout
, 0,
778 strvec_pushl(&ssh_keygen
.args
, "ssh-keygen", "-lf",
779 configured_signing_key
, NULL
);
780 ret
= pipe_command(&ssh_keygen
, NULL
, 0, &fingerprint_stdout
, 0,
785 die_errno(_("failed to get the ssh fingerprint for key '%s'"),
788 fingerprint
= strbuf_split_max(&fingerprint_stdout
, ' ', 3);
790 die_errno(_("failed to get the ssh fingerprint for key '%s'"),
793 fingerprint_ret
= strbuf_detach(fingerprint
[1], NULL
);
794 strbuf_list_free(fingerprint
);
795 strbuf_release(&fingerprint_stdout
);
796 return fingerprint_ret
;
799 /* Returns the first public key from an ssh-agent to use for signing */
800 static const char *get_default_ssh_signing_key(void)
802 struct child_process ssh_default_key
= CHILD_PROCESS_INIT
;
804 struct strbuf key_stdout
= STRBUF_INIT
, key_stderr
= STRBUF_INIT
;
805 struct strbuf
**keys
;
806 char *key_command
= NULL
;
809 char *default_key
= NULL
;
811 if (!ssh_default_key_command
)
812 die(_("either user.signingkey or gpg.ssh.defaultKeyCommand needs to be configured"));
814 key_command
= xstrdup(ssh_default_key_command
);
815 n
= split_cmdline(key_command
, &argv
);
818 die("malformed build-time gpg.ssh.defaultKeyCommand: %s",
819 split_cmdline_strerror(n
));
821 strvec_pushv(&ssh_default_key
.args
, argv
);
822 ret
= pipe_command(&ssh_default_key
, NULL
, 0, &key_stdout
, 0,
826 keys
= strbuf_split_max(&key_stdout
, '\n', 2);
827 if (keys
[0] && starts_with(keys
[0]->buf
, "ssh-")) {
828 default_key
= strbuf_detach(keys
[0], NULL
);
830 warning(_("gpg.ssh.defaultKeyCommand succeeded but returned no keys: %s %s"),
831 key_stderr
.buf
, key_stdout
.buf
);
834 strbuf_list_free(keys
);
836 warning(_("gpg.ssh.defaultKeyCommand failed: %s %s"),
837 key_stderr
.buf
, key_stdout
.buf
);
842 strbuf_release(&key_stdout
);
847 static const char *get_ssh_key_id(void) {
848 return get_ssh_key_fingerprint(get_signing_key());
851 /* Returns a textual but unique representation of the signing key */
852 const char *get_signing_key_id(void)
854 if (use_format
->get_key_id
) {
855 return use_format
->get_key_id();
858 /* GPG/GPGSM only store a key id on this variable */
859 return get_signing_key();
862 const char *get_signing_key(void)
864 if (configured_signing_key
)
865 return configured_signing_key
;
866 if (use_format
->get_default_key
) {
867 return use_format
->get_default_key();
870 return git_committer_info(IDENT_STRICT
| IDENT_NO_DATE
);
873 int sign_buffer(struct strbuf
*buffer
, struct strbuf
*signature
, const char *signing_key
)
875 return use_format
->sign_buffer(buffer
, signature
, signing_key
);
879 * Strip CR from the line endings, in case we are on Windows.
880 * NEEDSWORK: make it trim only CRs before LFs and rename
882 static void remove_cr_after(struct strbuf
*buffer
, size_t offset
)
886 for (i
= j
= offset
; i
< buffer
->len
; i
++) {
887 if (buffer
->buf
[i
] != '\r') {
889 buffer
->buf
[j
] = buffer
->buf
[i
];
893 strbuf_setlen(buffer
, j
);
896 static int sign_buffer_gpg(struct strbuf
*buffer
, struct strbuf
*signature
,
897 const char *signing_key
)
899 struct child_process gpg
= CHILD_PROCESS_INIT
;
902 struct strbuf gpg_status
= STRBUF_INIT
;
904 strvec_pushl(&gpg
.args
,
907 "-bsau", signing_key
,
910 bottom
= signature
->len
;
913 * When the username signingkey is bad, program could be terminated
914 * because gpg exits without reading and then write gets SIGPIPE.
916 sigchain_push(SIGPIPE
, SIG_IGN
);
917 ret
= pipe_command(&gpg
, buffer
->buf
, buffer
->len
,
918 signature
, 1024, &gpg_status
, 0);
919 sigchain_pop(SIGPIPE
);
921 ret
|= !strstr(gpg_status
.buf
, "\n[GNUPG:] SIG_CREATED ");
922 strbuf_release(&gpg_status
);
924 return error(_("gpg failed to sign the data"));
926 /* Strip CR from the line endings, in case we are on Windows. */
927 remove_cr_after(signature
, bottom
);
932 static int sign_buffer_ssh(struct strbuf
*buffer
, struct strbuf
*signature
,
933 const char *signing_key
)
935 struct child_process signer
= CHILD_PROCESS_INIT
;
937 size_t bottom
, keylen
;
938 struct strbuf signer_stderr
= STRBUF_INIT
;
939 struct tempfile
*key_file
= NULL
, *buffer_file
= NULL
;
940 char *ssh_signing_key_file
= NULL
;
941 struct strbuf ssh_signature_filename
= STRBUF_INIT
;
943 if (!signing_key
|| signing_key
[0] == '\0')
945 _("user.signingkey needs to be set for ssh signing"));
947 if (starts_with(signing_key
, "ssh-")) {
948 /* A literal ssh key */
949 key_file
= mks_tempfile_t(".git_signing_key_tmpXXXXXX");
952 _("could not create temporary file"));
953 keylen
= strlen(signing_key
);
954 if (write_in_full(key_file
->fd
, signing_key
, keylen
) < 0 ||
955 close_tempfile_gently(key_file
) < 0) {
956 error_errno(_("failed writing ssh signing key to '%s'"),
957 key_file
->filename
.buf
);
960 ssh_signing_key_file
= strbuf_detach(&key_file
->filename
, NULL
);
962 /* We assume a file */
963 ssh_signing_key_file
= expand_user_path(signing_key
, 1);
966 buffer_file
= mks_tempfile_t(".git_signing_buffer_tmpXXXXXX");
968 error_errno(_("could not create temporary file"));
972 if (write_in_full(buffer_file
->fd
, buffer
->buf
, buffer
->len
) < 0 ||
973 close_tempfile_gently(buffer_file
) < 0) {
974 error_errno(_("failed writing ssh signing key buffer to '%s'"),
975 buffer_file
->filename
.buf
);
979 strvec_pushl(&signer
.args
, use_format
->program
,
982 "-f", ssh_signing_key_file
,
983 buffer_file
->filename
.buf
,
986 sigchain_push(SIGPIPE
, SIG_IGN
);
987 ret
= pipe_command(&signer
, NULL
, 0, NULL
, 0, &signer_stderr
, 0);
988 sigchain_pop(SIGPIPE
);
991 if (strstr(signer_stderr
.buf
, "usage:"))
992 error(_("ssh-keygen -Y sign is needed for ssh signing (available in openssh version 8.2p1+)"));
994 error("%s", signer_stderr
.buf
);
998 bottom
= signature
->len
;
1000 strbuf_addbuf(&ssh_signature_filename
, &buffer_file
->filename
);
1001 strbuf_addstr(&ssh_signature_filename
, ".sig");
1002 if (strbuf_read_file(signature
, ssh_signature_filename
.buf
, 0) < 0) {
1004 _("failed reading ssh signing data buffer from '%s'"),
1005 ssh_signature_filename
.buf
);
1007 unlink_or_warn(ssh_signature_filename
.buf
);
1009 /* Strip CR from the line endings, in case we are on Windows. */
1010 remove_cr_after(signature
, bottom
);
1014 delete_tempfile(&key_file
);
1016 delete_tempfile(&buffer_file
);
1017 strbuf_release(&signer_stderr
);
1018 strbuf_release(&ssh_signature_filename
);
1019 FREE_AND_NULL(ssh_signing_key_file
);