1 #include "git-compat-util.h"
6 #include "run-command.h"
10 #include "gpg-interface.h"
16 #include "environment.h"
18 static int git_gpg_config(const char *, const char *,
19 const struct config_context
*, void *);
21 static void gpg_interface_lazy_init(void)
28 git_config(git_gpg_config
, NULL
);
31 static char *configured_signing_key
;
32 static const char *ssh_default_key_command
, *ssh_allowed_signers
, *ssh_revocation_file
;
33 static enum signature_trust_level configured_min_trust_level
= TRUST_UNDEFINED
;
38 const char **verify_args
;
40 int (*verify_signed_buffer
)(struct signature_check
*sigc
,
41 struct gpg_format
*fmt
,
42 const char *signature
,
43 size_t signature_size
);
44 int (*sign_buffer
)(struct strbuf
*buffer
, struct strbuf
*signature
,
45 const char *signing_key
);
46 const char *(*get_default_key
)(void);
47 const char *(*get_key_id
)(void);
50 static const char *openpgp_verify_args
[] = {
51 "--keyid-format=long",
54 static const char *openpgp_sigs
[] = {
55 "-----BEGIN PGP SIGNATURE-----",
56 "-----BEGIN PGP MESSAGE-----",
60 static const char *x509_verify_args
[] = {
63 static const char *x509_sigs
[] = {
64 "-----BEGIN SIGNED MESSAGE-----",
68 static const char *ssh_verify_args
[] = { NULL
};
69 static const char *ssh_sigs
[] = {
70 "-----BEGIN SSH SIGNATURE-----",
74 static int verify_gpg_signed_buffer(struct signature_check
*sigc
,
75 struct gpg_format
*fmt
,
76 const char *signature
,
77 size_t signature_size
);
78 static int verify_ssh_signed_buffer(struct signature_check
*sigc
,
79 struct gpg_format
*fmt
,
80 const char *signature
,
81 size_t signature_size
);
82 static int sign_buffer_gpg(struct strbuf
*buffer
, struct strbuf
*signature
,
83 const char *signing_key
);
84 static int sign_buffer_ssh(struct strbuf
*buffer
, struct strbuf
*signature
,
85 const char *signing_key
);
87 static const char *get_default_ssh_signing_key(void);
89 static const char *get_ssh_key_id(void);
91 static struct gpg_format gpg_format
[] = {
95 .verify_args
= openpgp_verify_args
,
97 .verify_signed_buffer
= verify_gpg_signed_buffer
,
98 .sign_buffer
= sign_buffer_gpg
,
99 .get_default_key
= NULL
,
105 .verify_args
= x509_verify_args
,
107 .verify_signed_buffer
= verify_gpg_signed_buffer
,
108 .sign_buffer
= sign_buffer_gpg
,
109 .get_default_key
= NULL
,
114 .program
= "ssh-keygen",
115 .verify_args
= ssh_verify_args
,
117 .verify_signed_buffer
= verify_ssh_signed_buffer
,
118 .sign_buffer
= sign_buffer_ssh
,
119 .get_default_key
= get_default_ssh_signing_key
,
120 .get_key_id
= get_ssh_key_id
,
124 static struct gpg_format
*use_format
= &gpg_format
[0];
126 static struct gpg_format
*get_format_by_name(const char *str
)
130 for (i
= 0; i
< ARRAY_SIZE(gpg_format
); i
++)
131 if (!strcmp(gpg_format
[i
].name
, str
))
132 return gpg_format
+ i
;
136 static struct gpg_format
*get_format_by_sig(const char *sig
)
140 for (i
= 0; i
< ARRAY_SIZE(gpg_format
); i
++)
141 for (j
= 0; gpg_format
[i
].sigs
[j
]; j
++)
142 if (starts_with(sig
, gpg_format
[i
].sigs
[j
]))
143 return gpg_format
+ i
;
147 void signature_check_clear(struct signature_check
*sigc
)
149 FREE_AND_NULL(sigc
->payload
);
150 FREE_AND_NULL(sigc
->output
);
151 FREE_AND_NULL(sigc
->gpg_status
);
152 FREE_AND_NULL(sigc
->signer
);
153 FREE_AND_NULL(sigc
->key
);
154 FREE_AND_NULL(sigc
->fingerprint
);
155 FREE_AND_NULL(sigc
->primary_key_fingerprint
);
158 /* An exclusive status -- only one of them can appear in output */
159 #define GPG_STATUS_EXCLUSIVE (1<<0)
160 /* The status includes key identifier */
161 #define GPG_STATUS_KEYID (1<<1)
162 /* The status includes user identifier */
163 #define GPG_STATUS_UID (1<<2)
164 /* The status includes key fingerprints */
165 #define GPG_STATUS_FINGERPRINT (1<<3)
166 /* The status includes trust level */
167 #define GPG_STATUS_TRUST_LEVEL (1<<4)
169 /* Short-hand for standard exclusive *SIG status with keyid & UID */
170 #define GPG_STATUS_STDSIG (GPG_STATUS_EXCLUSIVE|GPG_STATUS_KEYID|GPG_STATUS_UID)
176 } sigcheck_gpg_status
[] = {
177 { 'G', "GOODSIG ", GPG_STATUS_STDSIG
},
178 { 'B', "BADSIG ", GPG_STATUS_STDSIG
},
179 { 'E', "ERRSIG ", GPG_STATUS_EXCLUSIVE
|GPG_STATUS_KEYID
},
180 { 'X', "EXPSIG ", GPG_STATUS_STDSIG
},
181 { 'Y', "EXPKEYSIG ", GPG_STATUS_STDSIG
},
182 { 'R', "REVKEYSIG ", GPG_STATUS_STDSIG
},
183 { 0, "VALIDSIG ", GPG_STATUS_FINGERPRINT
},
184 { 0, "TRUST_", GPG_STATUS_TRUST_LEVEL
},
187 /* Keep the order same as enum signature_trust_level */
188 static struct sigcheck_gpg_trust_level
{
190 const char *display_key
;
191 enum signature_trust_level value
;
192 } sigcheck_gpg_trust_level
[] = {
193 { "UNDEFINED", "undefined", TRUST_UNDEFINED
},
194 { "NEVER", "never", TRUST_NEVER
},
195 { "MARGINAL", "marginal", TRUST_MARGINAL
},
196 { "FULLY", "fully", TRUST_FULLY
},
197 { "ULTIMATE", "ultimate", TRUST_ULTIMATE
},
200 static void replace_cstring(char **field
, const char *line
, const char *next
)
205 *field
= xmemdupz(line
, next
- line
);
210 static int parse_gpg_trust_level(const char *level
,
211 enum signature_trust_level
*res
)
215 for (i
= 0; i
< ARRAY_SIZE(sigcheck_gpg_trust_level
); i
++) {
216 if (!strcmp(sigcheck_gpg_trust_level
[i
].key
, level
)) {
217 *res
= sigcheck_gpg_trust_level
[i
].value
;
224 static void parse_gpg_output(struct signature_check
*sigc
)
226 const char *buf
= sigc
->gpg_status
;
227 const char *line
, *next
;
229 int seen_exclusive_status
= 0;
231 /* Iterate over all lines */
232 for (line
= buf
; *line
; line
= strchrnul(line
+1, '\n')) {
233 while (*line
== '\n')
238 /* Skip lines that don't start with GNUPG status */
239 if (!skip_prefix(line
, "[GNUPG:] ", &line
))
242 /* Iterate over all search strings */
243 for (i
= 0; i
< ARRAY_SIZE(sigcheck_gpg_status
); i
++) {
244 if (skip_prefix(line
, sigcheck_gpg_status
[i
].check
, &line
)) {
246 * GOODSIG, BADSIG etc. can occur only once for
247 * each signature. Therefore, if we had more
248 * than one then we're dealing with multiple
249 * signatures. We don't support them
250 * currently, and they're rather hard to
251 * create, so something is likely fishy and we
252 * should reject them altogether.
254 if (sigcheck_gpg_status
[i
].flags
& GPG_STATUS_EXCLUSIVE
) {
255 if (seen_exclusive_status
++)
259 if (sigcheck_gpg_status
[i
].result
)
260 sigc
->result
= sigcheck_gpg_status
[i
].result
;
261 /* Do we have key information? */
262 if (sigcheck_gpg_status
[i
].flags
& GPG_STATUS_KEYID
) {
263 next
= strchrnul(line
, ' ');
264 replace_cstring(&sigc
->key
, line
, next
);
265 /* Do we have signer information? */
266 if (*next
&& (sigcheck_gpg_status
[i
].flags
& GPG_STATUS_UID
)) {
268 next
= strchrnul(line
, '\n');
269 replace_cstring(&sigc
->signer
, line
, next
);
273 /* Do we have trust level? */
274 if (sigcheck_gpg_status
[i
].flags
& GPG_STATUS_TRUST_LEVEL
) {
276 * GPG v1 and v2 differs in how the
277 * TRUST_ lines are written. Some
278 * trust lines contain no additional
279 * space-separated information for v1.
281 size_t trust_size
= strcspn(line
, " \n");
282 char *trust
= xmemdupz(line
, trust_size
);
284 if (parse_gpg_trust_level(trust
, &sigc
->trust_level
)) {
291 /* Do we have fingerprint? */
292 if (sigcheck_gpg_status
[i
].flags
& GPG_STATUS_FINGERPRINT
) {
296 next
= strchrnul(line
, ' ');
297 replace_cstring(&sigc
->fingerprint
, line
, next
);
300 * Skip interim fields. The search is
301 * limited to the same line since only
302 * OpenPGP signatures has a field with
303 * the primary fingerprint.
305 limit
= strchrnul(line
, '\n');
306 for (j
= 9; j
> 0; j
--) {
307 if (!*next
|| limit
<= next
)
310 next
= strchrnul(line
, ' ');
313 field
= &sigc
->primary_key_fingerprint
;
315 next
= strchrnul(line
, '\n');
316 replace_cstring(field
, line
, next
);
318 replace_cstring(field
, NULL
, NULL
);
330 /* Clear partial data to avoid confusion */
331 FREE_AND_NULL(sigc
->primary_key_fingerprint
);
332 FREE_AND_NULL(sigc
->fingerprint
);
333 FREE_AND_NULL(sigc
->signer
);
334 FREE_AND_NULL(sigc
->key
);
337 static int verify_gpg_signed_buffer(struct signature_check
*sigc
,
338 struct gpg_format
*fmt
,
339 const char *signature
,
340 size_t signature_size
)
342 struct child_process gpg
= CHILD_PROCESS_INIT
;
343 struct tempfile
*temp
;
345 struct strbuf gpg_stdout
= STRBUF_INIT
;
346 struct strbuf gpg_stderr
= STRBUF_INIT
;
348 temp
= mks_tempfile_t(".git_vtag_tmpXXXXXX");
350 return error_errno(_("could not create temporary file"));
351 if (write_in_full(temp
->fd
, signature
, signature_size
) < 0 ||
352 close_tempfile_gently(temp
) < 0) {
353 error_errno(_("failed writing detached signature to '%s'"),
355 delete_tempfile(&temp
);
359 strvec_push(&gpg
.args
, fmt
->program
);
360 strvec_pushv(&gpg
.args
, fmt
->verify_args
);
361 strvec_pushl(&gpg
.args
,
363 "--verify", temp
->filename
.buf
, "-",
366 sigchain_push(SIGPIPE
, SIG_IGN
);
367 ret
= pipe_command(&gpg
, sigc
->payload
, sigc
->payload_len
, &gpg_stdout
, 0,
369 sigchain_pop(SIGPIPE
);
371 delete_tempfile(&temp
);
373 ret
|= !strstr(gpg_stdout
.buf
, "\n[GNUPG:] GOODSIG ");
374 sigc
->output
= strbuf_detach(&gpg_stderr
, NULL
);
375 sigc
->gpg_status
= strbuf_detach(&gpg_stdout
, NULL
);
377 parse_gpg_output(sigc
);
379 strbuf_release(&gpg_stdout
);
380 strbuf_release(&gpg_stderr
);
385 static void parse_ssh_output(struct signature_check
*sigc
)
387 const char *line
, *principal
, *search
;
392 * ssh-keygen output should be:
393 * Good "git" signature for PRINCIPAL with RSA key SHA256:FINGERPRINT
395 * or for valid but unknown keys:
396 * Good "git" signature with RSA key SHA256:FINGERPRINT
398 * Note that "PRINCIPAL" can contain whitespace, "RSA" and
399 * "SHA256" part could be a different token that names of
400 * the algorithms used, and "FINGERPRINT" is a hexadecimal
401 * string. By finding the last occurence of " with ", we can
402 * reliably parse out the PRINCIPAL.
405 sigc
->trust_level
= TRUST_NEVER
;
407 line
= to_free
= xmemdupz(sigc
->output
, strcspn(sigc
->output
, "\n"));
409 if (skip_prefix(line
, "Good \"git\" signature for ", &line
)) {
410 /* Search for the last "with" to get the full principal */
413 search
= strstr(line
, " with ");
416 } while (search
!= NULL
);
417 if (line
== principal
)
420 /* Valid signature and known principal */
422 sigc
->trust_level
= TRUST_FULLY
;
423 sigc
->signer
= xmemdupz(principal
, line
- principal
- 1);
424 } else if (skip_prefix(line
, "Good \"git\" signature with ", &line
)) {
425 /* Valid signature, but key unknown */
427 sigc
->trust_level
= TRUST_UNDEFINED
;
432 key
= strstr(line
, "key ");
434 sigc
->fingerprint
= xstrdup(strstr(line
, "key ") + 4);
435 sigc
->key
= xstrdup(sigc
->fingerprint
);
438 * Output did not match what we expected
439 * Treat the signature as bad
448 static int verify_ssh_signed_buffer(struct signature_check
*sigc
,
449 struct gpg_format
*fmt
,
450 const char *signature
,
451 size_t signature_size
)
453 struct child_process ssh_keygen
= CHILD_PROCESS_INIT
;
454 struct tempfile
*buffer_file
;
458 struct strbuf ssh_principals_out
= STRBUF_INIT
;
459 struct strbuf ssh_principals_err
= STRBUF_INIT
;
460 struct strbuf ssh_keygen_out
= STRBUF_INIT
;
461 struct strbuf ssh_keygen_err
= STRBUF_INIT
;
462 struct strbuf verify_time
= STRBUF_INIT
;
463 const struct date_mode verify_date_mode
= {
464 .type
= DATE_STRFTIME
,
465 .strftime_fmt
= "%Y%m%d%H%M%S",
466 /* SSH signing key validity has no timezone information - Use the local timezone */
470 if (!ssh_allowed_signers
) {
471 error(_("gpg.ssh.allowedSignersFile needs to be configured and exist for ssh signature verification"));
475 buffer_file
= mks_tempfile_t(".git_vtag_tmpXXXXXX");
477 return error_errno(_("could not create temporary file"));
478 if (write_in_full(buffer_file
->fd
, signature
, signature_size
) < 0 ||
479 close_tempfile_gently(buffer_file
) < 0) {
480 error_errno(_("failed writing detached signature to '%s'"),
481 buffer_file
->filename
.buf
);
482 delete_tempfile(&buffer_file
);
486 if (sigc
->payload_timestamp
)
487 strbuf_addf(&verify_time
, "-Overify-time=%s",
488 show_date(sigc
->payload_timestamp
, 0, &verify_date_mode
));
490 /* Find the principal from the signers */
491 strvec_pushl(&ssh_keygen
.args
, fmt
->program
,
492 "-Y", "find-principals",
493 "-f", ssh_allowed_signers
,
494 "-s", buffer_file
->filename
.buf
,
497 ret
= pipe_command(&ssh_keygen
, NULL
, 0, &ssh_principals_out
, 0,
498 &ssh_principals_err
, 0);
499 if (ret
&& strstr(ssh_principals_err
.buf
, "usage:")) {
500 error(_("ssh-keygen -Y find-principals/verify is needed for ssh signature verification (available in openssh version 8.2p1+)"));
503 if (ret
|| !ssh_principals_out
.len
) {
505 * We did not find a matching principal in the allowedSigners
506 * Check without validation
508 child_process_init(&ssh_keygen
);
509 strvec_pushl(&ssh_keygen
.args
, fmt
->program
,
510 "-Y", "check-novalidate",
512 "-s", buffer_file
->filename
.buf
,
515 pipe_command(&ssh_keygen
, sigc
->payload
, sigc
->payload_len
,
516 &ssh_keygen_out
, 0, &ssh_keygen_err
, 0);
519 * Fail on unknown keys
520 * we still call check-novalidate to display the signature info
524 /* Check every principal we found (one per line) */
526 for (line
= ssh_principals_out
.buf
;
529 const char *end_of_text
;
531 next
= end_of_text
= strchrnul(line
, '\n');
533 /* Did we find a LF, and did we have CR before it? */
535 line
< end_of_text
&&
536 end_of_text
[-1] == '\r')
539 /* Unless we hit NUL, skip over the LF we found */
543 /* Not all lines are data. Skip empty ones */
544 if (line
== end_of_text
)
547 /* We now know we have an non-empty line. Process it */
548 principal
= xmemdupz(line
, end_of_text
- line
);
550 child_process_init(&ssh_keygen
);
551 strbuf_release(&ssh_keygen_out
);
552 strbuf_release(&ssh_keygen_err
);
553 strvec_push(&ssh_keygen
.args
, fmt
->program
);
555 * We found principals
556 * Try with each until we find a match
558 strvec_pushl(&ssh_keygen
.args
, "-Y", "verify",
560 "-f", ssh_allowed_signers
,
562 "-s", buffer_file
->filename
.buf
,
566 if (ssh_revocation_file
) {
567 if (file_exists(ssh_revocation_file
)) {
568 strvec_pushl(&ssh_keygen
.args
, "-r",
569 ssh_revocation_file
, NULL
);
571 warning(_("ssh signing revocation file configured but not found: %s"),
572 ssh_revocation_file
);
576 sigchain_push(SIGPIPE
, SIG_IGN
);
577 ret
= pipe_command(&ssh_keygen
, sigc
->payload
, sigc
->payload_len
,
578 &ssh_keygen_out
, 0, &ssh_keygen_err
, 0);
579 sigchain_pop(SIGPIPE
);
581 FREE_AND_NULL(principal
);
584 ret
= !starts_with(ssh_keygen_out
.buf
, "Good");
591 strbuf_stripspace(&ssh_keygen_out
, '\0');
592 strbuf_stripspace(&ssh_keygen_err
, '\0');
593 /* Add stderr outputs to show the user actual ssh-keygen errors */
594 strbuf_add(&ssh_keygen_out
, ssh_principals_err
.buf
, ssh_principals_err
.len
);
595 strbuf_add(&ssh_keygen_out
, ssh_keygen_err
.buf
, ssh_keygen_err
.len
);
596 sigc
->output
= strbuf_detach(&ssh_keygen_out
, NULL
);
597 sigc
->gpg_status
= xstrdup(sigc
->output
);
599 parse_ssh_output(sigc
);
603 delete_tempfile(&buffer_file
);
604 strbuf_release(&ssh_principals_out
);
605 strbuf_release(&ssh_principals_err
);
606 strbuf_release(&ssh_keygen_out
);
607 strbuf_release(&ssh_keygen_err
);
608 strbuf_release(&verify_time
);
613 static int parse_payload_metadata(struct signature_check
*sigc
)
615 const char *ident_line
= NULL
;
617 struct ident_split ident
;
618 const char *signer_header
;
620 switch (sigc
->payload_type
) {
621 case SIGNATURE_PAYLOAD_COMMIT
:
622 signer_header
= "committer";
624 case SIGNATURE_PAYLOAD_TAG
:
625 signer_header
= "tagger";
627 case SIGNATURE_PAYLOAD_UNDEFINED
:
628 case SIGNATURE_PAYLOAD_PUSH_CERT
:
629 /* Ignore payloads we don't want to parse */
632 BUG("invalid value for sigc->payload_type");
635 ident_line
= find_commit_header(sigc
->payload
, signer_header
, &ident_len
);
636 if (!ident_line
|| !ident_len
)
639 if (split_ident_line(&ident
, ident_line
, ident_len
))
642 if (!sigc
->payload_timestamp
&& ident
.date_begin
&& ident
.date_end
)
643 sigc
->payload_timestamp
= parse_timestamp(ident
.date_begin
, NULL
, 10);
648 int check_signature(struct signature_check
*sigc
,
649 const char *signature
, size_t slen
)
651 struct gpg_format
*fmt
;
654 gpg_interface_lazy_init();
657 sigc
->trust_level
= TRUST_UNDEFINED
;
659 fmt
= get_format_by_sig(signature
);
661 die(_("bad/incompatible signature '%s'"), signature
);
663 if (parse_payload_metadata(sigc
))
666 status
= fmt
->verify_signed_buffer(sigc
, fmt
, signature
, slen
);
668 if (status
&& !sigc
->output
)
671 status
|= sigc
->result
!= 'G';
672 status
|= sigc
->trust_level
< configured_min_trust_level
;
677 void print_signature_buffer(const struct signature_check
*sigc
, unsigned flags
)
679 const char *output
= flags
& GPG_VERIFY_RAW
? sigc
->gpg_status
:
682 if (flags
& GPG_VERIFY_VERBOSE
&& sigc
->payload
)
683 fwrite(sigc
->payload
, 1, sigc
->payload_len
, stdout
);
686 fputs(output
, stderr
);
689 size_t parse_signed_buffer(const char *buf
, size_t size
)
696 if (get_format_by_sig(buf
+ len
))
699 eol
= memchr(buf
+ len
, '\n', size
- len
);
700 len
+= eol
? eol
- (buf
+ len
) + 1 : size
- len
;
705 int parse_signature(const char *buf
, size_t size
, struct strbuf
*payload
, struct strbuf
*signature
)
707 size_t match
= parse_signed_buffer(buf
, size
);
709 strbuf_add(payload
, buf
, match
);
710 remove_signature(payload
);
711 strbuf_add(signature
, buf
+ match
, size
- match
);
717 void set_signing_key(const char *key
)
719 gpg_interface_lazy_init();
721 free(configured_signing_key
);
722 configured_signing_key
= xstrdup(key
);
725 static int git_gpg_config(const char *var
, const char *value
,
726 const struct config_context
*ctx UNUSED
,
729 struct gpg_format
*fmt
= NULL
;
730 char *fmtname
= NULL
;
734 if (!strcmp(var
, "user.signingkey")) {
736 return config_error_nonbool(var
);
737 set_signing_key(value
);
741 if (!strcmp(var
, "gpg.format")) {
743 return config_error_nonbool(var
);
744 fmt
= get_format_by_name(value
);
746 return error(_("invalid value for '%s': '%s'"),
752 if (!strcmp(var
, "gpg.mintrustlevel")) {
754 return config_error_nonbool(var
);
756 trust
= xstrdup_toupper(value
);
757 ret
= parse_gpg_trust_level(trust
, &configured_min_trust_level
);
761 return error(_("invalid value for '%s': '%s'"),
766 if (!strcmp(var
, "gpg.ssh.defaultkeycommand")) {
768 return config_error_nonbool(var
);
769 return git_config_string(&ssh_default_key_command
, var
, value
);
772 if (!strcmp(var
, "gpg.ssh.allowedsignersfile")) {
774 return config_error_nonbool(var
);
775 return git_config_pathname(&ssh_allowed_signers
, var
, value
);
778 if (!strcmp(var
, "gpg.ssh.revocationfile")) {
780 return config_error_nonbool(var
);
781 return git_config_pathname(&ssh_revocation_file
, var
, value
);
784 if (!strcmp(var
, "gpg.program") || !strcmp(var
, "gpg.openpgp.program"))
787 if (!strcmp(var
, "gpg.x509.program"))
790 if (!strcmp(var
, "gpg.ssh.program"))
794 fmt
= get_format_by_name(fmtname
);
795 return git_config_string(&fmt
->program
, var
, value
);
802 * Returns 1 if `string` contains a literal ssh key, 0 otherwise
803 * `key` will be set to the start of the actual key if a prefix is present.
805 static int is_literal_ssh_key(const char *string
, const char **key
)
807 if (skip_prefix(string
, "key::", key
))
809 if (starts_with(string
, "ssh-")) {
816 static char *get_ssh_key_fingerprint(const char *signing_key
)
818 struct child_process ssh_keygen
= CHILD_PROCESS_INIT
;
820 struct strbuf fingerprint_stdout
= STRBUF_INIT
;
821 struct strbuf
**fingerprint
;
822 char *fingerprint_ret
;
823 const char *literal_key
= NULL
;
826 * With SSH Signing this can contain a filename or a public key
827 * For textual representation we usually want a fingerprint
829 if (is_literal_ssh_key(signing_key
, &literal_key
)) {
830 strvec_pushl(&ssh_keygen
.args
, "ssh-keygen", "-lf", "-", NULL
);
831 ret
= pipe_command(&ssh_keygen
, literal_key
,
832 strlen(literal_key
), &fingerprint_stdout
, 0,
835 strvec_pushl(&ssh_keygen
.args
, "ssh-keygen", "-lf",
836 configured_signing_key
, NULL
);
837 ret
= pipe_command(&ssh_keygen
, NULL
, 0, &fingerprint_stdout
, 0,
842 die_errno(_("failed to get the ssh fingerprint for key '%s'"),
845 fingerprint
= strbuf_split_max(&fingerprint_stdout
, ' ', 3);
847 die_errno(_("failed to get the ssh fingerprint for key '%s'"),
850 fingerprint_ret
= strbuf_detach(fingerprint
[1], NULL
);
851 strbuf_list_free(fingerprint
);
852 strbuf_release(&fingerprint_stdout
);
853 return fingerprint_ret
;
856 /* Returns the first public key from an ssh-agent to use for signing */
857 static const char *get_default_ssh_signing_key(void)
859 struct child_process ssh_default_key
= CHILD_PROCESS_INIT
;
861 struct strbuf key_stdout
= STRBUF_INIT
, key_stderr
= STRBUF_INIT
;
862 struct strbuf
**keys
;
863 char *key_command
= NULL
;
866 char *default_key
= NULL
;
867 const char *literal_key
= NULL
;
869 if (!ssh_default_key_command
)
870 die(_("either user.signingkey or gpg.ssh.defaultKeyCommand needs to be configured"));
872 key_command
= xstrdup(ssh_default_key_command
);
873 n
= split_cmdline(key_command
, &argv
);
876 die("malformed build-time gpg.ssh.defaultKeyCommand: %s",
877 split_cmdline_strerror(n
));
879 strvec_pushv(&ssh_default_key
.args
, argv
);
880 ret
= pipe_command(&ssh_default_key
, NULL
, 0, &key_stdout
, 0,
884 keys
= strbuf_split_max(&key_stdout
, '\n', 2);
885 if (keys
[0] && is_literal_ssh_key(keys
[0]->buf
, &literal_key
)) {
887 * We only use `is_literal_ssh_key` here to check validity
888 * The prefix will be stripped when the key is used.
890 default_key
= strbuf_detach(keys
[0], NULL
);
892 warning(_("gpg.ssh.defaultKeyCommand succeeded but returned no keys: %s %s"),
893 key_stderr
.buf
, key_stdout
.buf
);
896 strbuf_list_free(keys
);
898 warning(_("gpg.ssh.defaultKeyCommand failed: %s %s"),
899 key_stderr
.buf
, key_stdout
.buf
);
904 strbuf_release(&key_stdout
);
909 static const char *get_ssh_key_id(void) {
910 return get_ssh_key_fingerprint(get_signing_key());
913 /* Returns a textual but unique representation of the signing key */
914 const char *get_signing_key_id(void)
916 gpg_interface_lazy_init();
918 if (use_format
->get_key_id
) {
919 return use_format
->get_key_id();
922 /* GPG/GPGSM only store a key id on this variable */
923 return get_signing_key();
926 const char *get_signing_key(void)
928 gpg_interface_lazy_init();
930 if (configured_signing_key
)
931 return configured_signing_key
;
932 if (use_format
->get_default_key
) {
933 return use_format
->get_default_key();
936 return git_committer_info(IDENT_STRICT
| IDENT_NO_DATE
);
939 const char *gpg_trust_level_to_str(enum signature_trust_level level
)
941 struct sigcheck_gpg_trust_level
*trust
;
943 if (level
< 0 || level
>= ARRAY_SIZE(sigcheck_gpg_trust_level
))
944 BUG("invalid trust level requested %d", level
);
946 trust
= &sigcheck_gpg_trust_level
[level
];
947 if (trust
->value
!= level
)
948 BUG("sigcheck_gpg_trust_level[] unsorted");
950 return sigcheck_gpg_trust_level
[level
].display_key
;
953 int sign_buffer(struct strbuf
*buffer
, struct strbuf
*signature
, const char *signing_key
)
955 gpg_interface_lazy_init();
957 return use_format
->sign_buffer(buffer
, signature
, signing_key
);
961 * Strip CR from the line endings, in case we are on Windows.
962 * NEEDSWORK: make it trim only CRs before LFs and rename
964 static void remove_cr_after(struct strbuf
*buffer
, size_t offset
)
968 for (i
= j
= offset
; i
< buffer
->len
; i
++) {
969 if (buffer
->buf
[i
] != '\r') {
971 buffer
->buf
[j
] = buffer
->buf
[i
];
975 strbuf_setlen(buffer
, j
);
978 static int sign_buffer_gpg(struct strbuf
*buffer
, struct strbuf
*signature
,
979 const char *signing_key
)
981 struct child_process gpg
= CHILD_PROCESS_INIT
;
985 struct strbuf gpg_status
= STRBUF_INIT
;
987 strvec_pushl(&gpg
.args
,
990 "-bsau", signing_key
,
993 bottom
= signature
->len
;
996 * When the username signingkey is bad, program could be terminated
997 * because gpg exits without reading and then write gets SIGPIPE.
999 sigchain_push(SIGPIPE
, SIG_IGN
);
1000 ret
= pipe_command(&gpg
, buffer
->buf
, buffer
->len
,
1001 signature
, 1024, &gpg_status
, 0);
1002 sigchain_pop(SIGPIPE
);
1004 for (cp
= gpg_status
.buf
;
1005 cp
&& (cp
= strstr(cp
, "[GNUPG:] SIG_CREATED "));
1007 if (cp
== gpg_status
.buf
|| cp
[-1] == '\n')
1012 error(_("gpg failed to sign the data:\n%s"),
1013 gpg_status
.len
? gpg_status
.buf
: "(no gpg output)");
1014 strbuf_release(&gpg_status
);
1017 strbuf_release(&gpg_status
);
1019 /* Strip CR from the line endings, in case we are on Windows. */
1020 remove_cr_after(signature
, bottom
);
1025 static int sign_buffer_ssh(struct strbuf
*buffer
, struct strbuf
*signature
,
1026 const char *signing_key
)
1028 struct child_process signer
= CHILD_PROCESS_INIT
;
1030 size_t bottom
, keylen
;
1031 struct strbuf signer_stderr
= STRBUF_INIT
;
1032 struct tempfile
*key_file
= NULL
, *buffer_file
= NULL
;
1033 char *ssh_signing_key_file
= NULL
;
1034 struct strbuf ssh_signature_filename
= STRBUF_INIT
;
1035 const char *literal_key
= NULL
;
1036 int literal_ssh_key
= 0;
1038 if (!signing_key
|| signing_key
[0] == '\0')
1040 _("user.signingKey needs to be set for ssh signing"));
1042 if (is_literal_ssh_key(signing_key
, &literal_key
)) {
1043 /* A literal ssh key */
1044 literal_ssh_key
= 1;
1045 key_file
= mks_tempfile_t(".git_signing_key_tmpXXXXXX");
1048 _("could not create temporary file"));
1049 keylen
= strlen(literal_key
);
1050 if (write_in_full(key_file
->fd
, literal_key
, keylen
) < 0 ||
1051 close_tempfile_gently(key_file
) < 0) {
1052 error_errno(_("failed writing ssh signing key to '%s'"),
1053 key_file
->filename
.buf
);
1056 ssh_signing_key_file
= strbuf_detach(&key_file
->filename
, NULL
);
1058 /* We assume a file */
1059 ssh_signing_key_file
= interpolate_path(signing_key
, 1);
1062 buffer_file
= mks_tempfile_t(".git_signing_buffer_tmpXXXXXX");
1064 error_errno(_("could not create temporary file"));
1068 if (write_in_full(buffer_file
->fd
, buffer
->buf
, buffer
->len
) < 0 ||
1069 close_tempfile_gently(buffer_file
) < 0) {
1070 error_errno(_("failed writing ssh signing key buffer to '%s'"),
1071 buffer_file
->filename
.buf
);
1075 strvec_pushl(&signer
.args
, use_format
->program
,
1078 "-f", ssh_signing_key_file
,
1080 if (literal_ssh_key
)
1081 strvec_push(&signer
.args
, "-U");
1082 strvec_push(&signer
.args
, buffer_file
->filename
.buf
);
1084 sigchain_push(SIGPIPE
, SIG_IGN
);
1085 ret
= pipe_command(&signer
, NULL
, 0, NULL
, 0, &signer_stderr
, 0);
1086 sigchain_pop(SIGPIPE
);
1089 if (strstr(signer_stderr
.buf
, "usage:"))
1090 error(_("ssh-keygen -Y sign is needed for ssh signing (available in openssh version 8.2p1+)"));
1092 error("%s", signer_stderr
.buf
);
1096 bottom
= signature
->len
;
1098 strbuf_addbuf(&ssh_signature_filename
, &buffer_file
->filename
);
1099 strbuf_addstr(&ssh_signature_filename
, ".sig");
1100 if (strbuf_read_file(signature
, ssh_signature_filename
.buf
, 0) < 0) {
1102 _("failed reading ssh signing data buffer from '%s'"),
1103 ssh_signature_filename
.buf
);
1106 /* Strip CR from the line endings, in case we are on Windows. */
1107 remove_cr_after(signature
, bottom
);
1111 delete_tempfile(&key_file
);
1113 delete_tempfile(&buffer_file
);
1114 if (ssh_signature_filename
.len
)
1115 unlink_or_warn(ssh_signature_filename
.buf
);
1116 strbuf_release(&signer_stderr
);
1117 strbuf_release(&ssh_signature_filename
);
1118 FREE_AND_NULL(ssh_signing_key_file
);