1 #include "git-compat-util.h"
6 #include "run-command.h"
10 #include "gpg-interface.h"
16 static int git_gpg_config(const char *, const char *,
17 const struct config_context
*, void *);
19 static void gpg_interface_lazy_init(void)
26 git_config(git_gpg_config
, NULL
);
29 static char *configured_signing_key
;
30 static const char *ssh_default_key_command
, *ssh_allowed_signers
, *ssh_revocation_file
;
31 static enum signature_trust_level configured_min_trust_level
= TRUST_UNDEFINED
;
36 const char **verify_args
;
38 int (*verify_signed_buffer
)(struct signature_check
*sigc
,
39 struct gpg_format
*fmt
,
40 const char *signature
,
41 size_t signature_size
);
42 int (*sign_buffer
)(struct strbuf
*buffer
, struct strbuf
*signature
,
43 const char *signing_key
);
44 const char *(*get_default_key
)(void);
45 const char *(*get_key_id
)(void);
48 static const char *openpgp_verify_args
[] = {
49 "--keyid-format=long",
52 static const char *openpgp_sigs
[] = {
53 "-----BEGIN PGP SIGNATURE-----",
54 "-----BEGIN PGP MESSAGE-----",
58 static const char *x509_verify_args
[] = {
61 static const char *x509_sigs
[] = {
62 "-----BEGIN SIGNED MESSAGE-----",
66 static const char *ssh_verify_args
[] = { NULL
};
67 static const char *ssh_sigs
[] = {
68 "-----BEGIN SSH SIGNATURE-----",
72 static int verify_gpg_signed_buffer(struct signature_check
*sigc
,
73 struct gpg_format
*fmt
,
74 const char *signature
,
75 size_t signature_size
);
76 static int verify_ssh_signed_buffer(struct signature_check
*sigc
,
77 struct gpg_format
*fmt
,
78 const char *signature
,
79 size_t signature_size
);
80 static int sign_buffer_gpg(struct strbuf
*buffer
, struct strbuf
*signature
,
81 const char *signing_key
);
82 static int sign_buffer_ssh(struct strbuf
*buffer
, struct strbuf
*signature
,
83 const char *signing_key
);
85 static const char *get_default_ssh_signing_key(void);
87 static const char *get_ssh_key_id(void);
89 static struct gpg_format gpg_format
[] = {
93 .verify_args
= openpgp_verify_args
,
95 .verify_signed_buffer
= verify_gpg_signed_buffer
,
96 .sign_buffer
= sign_buffer_gpg
,
97 .get_default_key
= NULL
,
103 .verify_args
= x509_verify_args
,
105 .verify_signed_buffer
= verify_gpg_signed_buffer
,
106 .sign_buffer
= sign_buffer_gpg
,
107 .get_default_key
= NULL
,
112 .program
= "ssh-keygen",
113 .verify_args
= ssh_verify_args
,
115 .verify_signed_buffer
= verify_ssh_signed_buffer
,
116 .sign_buffer
= sign_buffer_ssh
,
117 .get_default_key
= get_default_ssh_signing_key
,
118 .get_key_id
= get_ssh_key_id
,
122 static struct gpg_format
*use_format
= &gpg_format
[0];
124 static struct gpg_format
*get_format_by_name(const char *str
)
128 for (i
= 0; i
< ARRAY_SIZE(gpg_format
); i
++)
129 if (!strcmp(gpg_format
[i
].name
, str
))
130 return gpg_format
+ i
;
134 static struct gpg_format
*get_format_by_sig(const char *sig
)
138 for (i
= 0; i
< ARRAY_SIZE(gpg_format
); i
++)
139 for (j
= 0; gpg_format
[i
].sigs
[j
]; j
++)
140 if (starts_with(sig
, gpg_format
[i
].sigs
[j
]))
141 return gpg_format
+ i
;
145 void signature_check_clear(struct signature_check
*sigc
)
147 FREE_AND_NULL(sigc
->payload
);
148 FREE_AND_NULL(sigc
->output
);
149 FREE_AND_NULL(sigc
->gpg_status
);
150 FREE_AND_NULL(sigc
->signer
);
151 FREE_AND_NULL(sigc
->key
);
152 FREE_AND_NULL(sigc
->fingerprint
);
153 FREE_AND_NULL(sigc
->primary_key_fingerprint
);
156 /* An exclusive status -- only one of them can appear in output */
157 #define GPG_STATUS_EXCLUSIVE (1<<0)
158 /* The status includes key identifier */
159 #define GPG_STATUS_KEYID (1<<1)
160 /* The status includes user identifier */
161 #define GPG_STATUS_UID (1<<2)
162 /* The status includes key fingerprints */
163 #define GPG_STATUS_FINGERPRINT (1<<3)
164 /* The status includes trust level */
165 #define GPG_STATUS_TRUST_LEVEL (1<<4)
167 /* Short-hand for standard exclusive *SIG status with keyid & UID */
168 #define GPG_STATUS_STDSIG (GPG_STATUS_EXCLUSIVE|GPG_STATUS_KEYID|GPG_STATUS_UID)
174 } sigcheck_gpg_status
[] = {
175 { 'G', "GOODSIG ", GPG_STATUS_STDSIG
},
176 { 'B', "BADSIG ", GPG_STATUS_STDSIG
},
177 { 'E', "ERRSIG ", GPG_STATUS_EXCLUSIVE
|GPG_STATUS_KEYID
},
178 { 'X', "EXPSIG ", GPG_STATUS_STDSIG
},
179 { 'Y', "EXPKEYSIG ", GPG_STATUS_STDSIG
},
180 { 'R', "REVKEYSIG ", GPG_STATUS_STDSIG
},
181 { 0, "VALIDSIG ", GPG_STATUS_FINGERPRINT
},
182 { 0, "TRUST_", GPG_STATUS_TRUST_LEVEL
},
185 /* Keep the order same as enum signature_trust_level */
186 static struct sigcheck_gpg_trust_level
{
188 const char *display_key
;
189 enum signature_trust_level value
;
190 } sigcheck_gpg_trust_level
[] = {
191 { "UNDEFINED", "undefined", TRUST_UNDEFINED
},
192 { "NEVER", "never", TRUST_NEVER
},
193 { "MARGINAL", "marginal", TRUST_MARGINAL
},
194 { "FULLY", "fully", TRUST_FULLY
},
195 { "ULTIMATE", "ultimate", TRUST_ULTIMATE
},
198 static void replace_cstring(char **field
, const char *line
, const char *next
)
203 *field
= xmemdupz(line
, next
- line
);
208 static int parse_gpg_trust_level(const char *level
,
209 enum signature_trust_level
*res
)
213 for (i
= 0; i
< ARRAY_SIZE(sigcheck_gpg_trust_level
); i
++) {
214 if (!strcmp(sigcheck_gpg_trust_level
[i
].key
, level
)) {
215 *res
= sigcheck_gpg_trust_level
[i
].value
;
222 static void parse_gpg_output(struct signature_check
*sigc
)
224 const char *buf
= sigc
->gpg_status
;
225 const char *line
, *next
;
227 int seen_exclusive_status
= 0;
229 /* Iterate over all lines */
230 for (line
= buf
; *line
; line
= strchrnul(line
+1, '\n')) {
231 while (*line
== '\n')
236 /* Skip lines that don't start with GNUPG status */
237 if (!skip_prefix(line
, "[GNUPG:] ", &line
))
240 /* Iterate over all search strings */
241 for (i
= 0; i
< ARRAY_SIZE(sigcheck_gpg_status
); i
++) {
242 if (skip_prefix(line
, sigcheck_gpg_status
[i
].check
, &line
)) {
244 * GOODSIG, BADSIG etc. can occur only once for
245 * each signature. Therefore, if we had more
246 * than one then we're dealing with multiple
247 * signatures. We don't support them
248 * currently, and they're rather hard to
249 * create, so something is likely fishy and we
250 * should reject them altogether.
252 if (sigcheck_gpg_status
[i
].flags
& GPG_STATUS_EXCLUSIVE
) {
253 if (seen_exclusive_status
++)
257 if (sigcheck_gpg_status
[i
].result
)
258 sigc
->result
= sigcheck_gpg_status
[i
].result
;
259 /* Do we have key information? */
260 if (sigcheck_gpg_status
[i
].flags
& GPG_STATUS_KEYID
) {
261 next
= strchrnul(line
, ' ');
262 replace_cstring(&sigc
->key
, line
, next
);
263 /* Do we have signer information? */
264 if (*next
&& (sigcheck_gpg_status
[i
].flags
& GPG_STATUS_UID
)) {
266 next
= strchrnul(line
, '\n');
267 replace_cstring(&sigc
->signer
, line
, next
);
271 /* Do we have trust level? */
272 if (sigcheck_gpg_status
[i
].flags
& GPG_STATUS_TRUST_LEVEL
) {
274 * GPG v1 and v2 differs in how the
275 * TRUST_ lines are written. Some
276 * trust lines contain no additional
277 * space-separated information for v1.
279 size_t trust_size
= strcspn(line
, " \n");
280 char *trust
= xmemdupz(line
, trust_size
);
282 if (parse_gpg_trust_level(trust
, &sigc
->trust_level
)) {
289 /* Do we have fingerprint? */
290 if (sigcheck_gpg_status
[i
].flags
& GPG_STATUS_FINGERPRINT
) {
294 next
= strchrnul(line
, ' ');
295 replace_cstring(&sigc
->fingerprint
, line
, next
);
298 * Skip interim fields. The search is
299 * limited to the same line since only
300 * OpenPGP signatures has a field with
301 * the primary fingerprint.
303 limit
= strchrnul(line
, '\n');
304 for (j
= 9; j
> 0; j
--) {
305 if (!*next
|| limit
<= next
)
308 next
= strchrnul(line
, ' ');
311 field
= &sigc
->primary_key_fingerprint
;
313 next
= strchrnul(line
, '\n');
314 replace_cstring(field
, line
, next
);
316 replace_cstring(field
, NULL
, NULL
);
328 /* Clear partial data to avoid confusion */
329 FREE_AND_NULL(sigc
->primary_key_fingerprint
);
330 FREE_AND_NULL(sigc
->fingerprint
);
331 FREE_AND_NULL(sigc
->signer
);
332 FREE_AND_NULL(sigc
->key
);
335 static int verify_gpg_signed_buffer(struct signature_check
*sigc
,
336 struct gpg_format
*fmt
,
337 const char *signature
,
338 size_t signature_size
)
340 struct child_process gpg
= CHILD_PROCESS_INIT
;
341 struct tempfile
*temp
;
343 struct strbuf gpg_stdout
= STRBUF_INIT
;
344 struct strbuf gpg_stderr
= STRBUF_INIT
;
346 temp
= mks_tempfile_t(".git_vtag_tmpXXXXXX");
348 return error_errno(_("could not create temporary file"));
349 if (write_in_full(temp
->fd
, signature
, signature_size
) < 0 ||
350 close_tempfile_gently(temp
) < 0) {
351 error_errno(_("failed writing detached signature to '%s'"),
353 delete_tempfile(&temp
);
357 strvec_push(&gpg
.args
, fmt
->program
);
358 strvec_pushv(&gpg
.args
, fmt
->verify_args
);
359 strvec_pushl(&gpg
.args
,
361 "--verify", temp
->filename
.buf
, "-",
364 sigchain_push(SIGPIPE
, SIG_IGN
);
365 ret
= pipe_command(&gpg
, sigc
->payload
, sigc
->payload_len
, &gpg_stdout
, 0,
367 sigchain_pop(SIGPIPE
);
369 delete_tempfile(&temp
);
371 ret
|= !strstr(gpg_stdout
.buf
, "\n[GNUPG:] GOODSIG ");
372 sigc
->output
= strbuf_detach(&gpg_stderr
, NULL
);
373 sigc
->gpg_status
= strbuf_detach(&gpg_stdout
, NULL
);
375 parse_gpg_output(sigc
);
377 strbuf_release(&gpg_stdout
);
378 strbuf_release(&gpg_stderr
);
383 static void parse_ssh_output(struct signature_check
*sigc
)
385 const char *line
, *principal
, *search
;
390 * ssh-keygen output should be:
391 * Good "git" signature for PRINCIPAL with RSA key SHA256:FINGERPRINT
393 * or for valid but unknown keys:
394 * Good "git" signature with RSA key SHA256:FINGERPRINT
396 * Note that "PRINCIPAL" can contain whitespace, "RSA" and
397 * "SHA256" part could be a different token that names of
398 * the algorithms used, and "FINGERPRINT" is a hexadecimal
399 * string. By finding the last occurence of " with ", we can
400 * reliably parse out the PRINCIPAL.
403 sigc
->trust_level
= TRUST_NEVER
;
405 line
= to_free
= xmemdupz(sigc
->output
, strcspn(sigc
->output
, "\n"));
407 if (skip_prefix(line
, "Good \"git\" signature for ", &line
)) {
408 /* Search for the last "with" to get the full principal */
411 search
= strstr(line
, " with ");
414 } while (search
!= NULL
);
415 if (line
== principal
)
418 /* Valid signature and known principal */
420 sigc
->trust_level
= TRUST_FULLY
;
421 sigc
->signer
= xmemdupz(principal
, line
- principal
- 1);
422 } else if (skip_prefix(line
, "Good \"git\" signature with ", &line
)) {
423 /* Valid signature, but key unknown */
425 sigc
->trust_level
= TRUST_UNDEFINED
;
430 key
= strstr(line
, "key ");
432 sigc
->fingerprint
= xstrdup(strstr(line
, "key ") + 4);
433 sigc
->key
= xstrdup(sigc
->fingerprint
);
436 * Output did not match what we expected
437 * Treat the signature as bad
446 static int verify_ssh_signed_buffer(struct signature_check
*sigc
,
447 struct gpg_format
*fmt
,
448 const char *signature
,
449 size_t signature_size
)
451 struct child_process ssh_keygen
= CHILD_PROCESS_INIT
;
452 struct tempfile
*buffer_file
;
456 struct strbuf ssh_principals_out
= STRBUF_INIT
;
457 struct strbuf ssh_principals_err
= STRBUF_INIT
;
458 struct strbuf ssh_keygen_out
= STRBUF_INIT
;
459 struct strbuf ssh_keygen_err
= STRBUF_INIT
;
460 struct strbuf verify_time
= STRBUF_INIT
;
461 const struct date_mode verify_date_mode
= {
462 .type
= DATE_STRFTIME
,
463 .strftime_fmt
= "%Y%m%d%H%M%S",
464 /* SSH signing key validity has no timezone information - Use the local timezone */
468 if (!ssh_allowed_signers
) {
469 error(_("gpg.ssh.allowedSignersFile needs to be configured and exist for ssh signature verification"));
473 buffer_file
= mks_tempfile_t(".git_vtag_tmpXXXXXX");
475 return error_errno(_("could not create temporary file"));
476 if (write_in_full(buffer_file
->fd
, signature
, signature_size
) < 0 ||
477 close_tempfile_gently(buffer_file
) < 0) {
478 error_errno(_("failed writing detached signature to '%s'"),
479 buffer_file
->filename
.buf
);
480 delete_tempfile(&buffer_file
);
484 if (sigc
->payload_timestamp
)
485 strbuf_addf(&verify_time
, "-Overify-time=%s",
486 show_date(sigc
->payload_timestamp
, 0, verify_date_mode
));
488 /* Find the principal from the signers */
489 strvec_pushl(&ssh_keygen
.args
, fmt
->program
,
490 "-Y", "find-principals",
491 "-f", ssh_allowed_signers
,
492 "-s", buffer_file
->filename
.buf
,
495 ret
= pipe_command(&ssh_keygen
, NULL
, 0, &ssh_principals_out
, 0,
496 &ssh_principals_err
, 0);
497 if (ret
&& strstr(ssh_principals_err
.buf
, "usage:")) {
498 error(_("ssh-keygen -Y find-principals/verify is needed for ssh signature verification (available in openssh version 8.2p1+)"));
501 if (ret
|| !ssh_principals_out
.len
) {
503 * We did not find a matching principal in the allowedSigners
504 * Check without validation
506 child_process_init(&ssh_keygen
);
507 strvec_pushl(&ssh_keygen
.args
, fmt
->program
,
508 "-Y", "check-novalidate",
510 "-s", buffer_file
->filename
.buf
,
513 pipe_command(&ssh_keygen
, sigc
->payload
, sigc
->payload_len
,
514 &ssh_keygen_out
, 0, &ssh_keygen_err
, 0);
517 * Fail on unknown keys
518 * we still call check-novalidate to display the signature info
522 /* Check every principal we found (one per line) */
524 for (line
= ssh_principals_out
.buf
;
527 const char *end_of_text
;
529 next
= end_of_text
= strchrnul(line
, '\n');
531 /* Did we find a LF, and did we have CR before it? */
533 line
< end_of_text
&&
534 end_of_text
[-1] == '\r')
537 /* Unless we hit NUL, skip over the LF we found */
541 /* Not all lines are data. Skip empty ones */
542 if (line
== end_of_text
)
545 /* We now know we have an non-empty line. Process it */
546 principal
= xmemdupz(line
, end_of_text
- line
);
548 child_process_init(&ssh_keygen
);
549 strbuf_release(&ssh_keygen_out
);
550 strbuf_release(&ssh_keygen_err
);
551 strvec_push(&ssh_keygen
.args
, fmt
->program
);
553 * We found principals
554 * Try with each until we find a match
556 strvec_pushl(&ssh_keygen
.args
, "-Y", "verify",
558 "-f", ssh_allowed_signers
,
560 "-s", buffer_file
->filename
.buf
,
564 if (ssh_revocation_file
) {
565 if (file_exists(ssh_revocation_file
)) {
566 strvec_pushl(&ssh_keygen
.args
, "-r",
567 ssh_revocation_file
, NULL
);
569 warning(_("ssh signing revocation file configured but not found: %s"),
570 ssh_revocation_file
);
574 sigchain_push(SIGPIPE
, SIG_IGN
);
575 ret
= pipe_command(&ssh_keygen
, sigc
->payload
, sigc
->payload_len
,
576 &ssh_keygen_out
, 0, &ssh_keygen_err
, 0);
577 sigchain_pop(SIGPIPE
);
579 FREE_AND_NULL(principal
);
582 ret
= !starts_with(ssh_keygen_out
.buf
, "Good");
589 strbuf_stripspace(&ssh_keygen_out
, NULL
);
590 strbuf_stripspace(&ssh_keygen_err
, NULL
);
591 /* Add stderr outputs to show the user actual ssh-keygen errors */
592 strbuf_add(&ssh_keygen_out
, ssh_principals_err
.buf
, ssh_principals_err
.len
);
593 strbuf_add(&ssh_keygen_out
, ssh_keygen_err
.buf
, ssh_keygen_err
.len
);
594 sigc
->output
= strbuf_detach(&ssh_keygen_out
, NULL
);
595 sigc
->gpg_status
= xstrdup(sigc
->output
);
597 parse_ssh_output(sigc
);
601 delete_tempfile(&buffer_file
);
602 strbuf_release(&ssh_principals_out
);
603 strbuf_release(&ssh_principals_err
);
604 strbuf_release(&ssh_keygen_out
);
605 strbuf_release(&ssh_keygen_err
);
606 strbuf_release(&verify_time
);
611 static int parse_payload_metadata(struct signature_check
*sigc
)
613 const char *ident_line
= NULL
;
615 struct ident_split ident
;
616 const char *signer_header
;
618 switch (sigc
->payload_type
) {
619 case SIGNATURE_PAYLOAD_COMMIT
:
620 signer_header
= "committer";
622 case SIGNATURE_PAYLOAD_TAG
:
623 signer_header
= "tagger";
625 case SIGNATURE_PAYLOAD_UNDEFINED
:
626 case SIGNATURE_PAYLOAD_PUSH_CERT
:
627 /* Ignore payloads we don't want to parse */
630 BUG("invalid value for sigc->payload_type");
633 ident_line
= find_commit_header(sigc
->payload
, signer_header
, &ident_len
);
634 if (!ident_line
|| !ident_len
)
637 if (split_ident_line(&ident
, ident_line
, ident_len
))
640 if (!sigc
->payload_timestamp
&& ident
.date_begin
&& ident
.date_end
)
641 sigc
->payload_timestamp
= parse_timestamp(ident
.date_begin
, NULL
, 10);
646 int check_signature(struct signature_check
*sigc
,
647 const char *signature
, size_t slen
)
649 struct gpg_format
*fmt
;
652 gpg_interface_lazy_init();
655 sigc
->trust_level
= TRUST_UNDEFINED
;
657 fmt
= get_format_by_sig(signature
);
659 die(_("bad/incompatible signature '%s'"), signature
);
661 if (parse_payload_metadata(sigc
))
664 status
= fmt
->verify_signed_buffer(sigc
, fmt
, signature
, slen
);
666 if (status
&& !sigc
->output
)
669 status
|= sigc
->result
!= 'G';
670 status
|= sigc
->trust_level
< configured_min_trust_level
;
675 void print_signature_buffer(const struct signature_check
*sigc
, unsigned flags
)
677 const char *output
= flags
& GPG_VERIFY_RAW
? sigc
->gpg_status
:
680 if (flags
& GPG_VERIFY_VERBOSE
&& sigc
->payload
)
681 fwrite(sigc
->payload
, 1, sigc
->payload_len
, stdout
);
684 fputs(output
, stderr
);
687 size_t parse_signed_buffer(const char *buf
, size_t size
)
694 if (get_format_by_sig(buf
+ len
))
697 eol
= memchr(buf
+ len
, '\n', size
- len
);
698 len
+= eol
? eol
- (buf
+ len
) + 1 : size
- len
;
703 int parse_signature(const char *buf
, size_t size
, struct strbuf
*payload
, struct strbuf
*signature
)
705 size_t match
= parse_signed_buffer(buf
, size
);
707 strbuf_add(payload
, buf
, match
);
708 remove_signature(payload
);
709 strbuf_add(signature
, buf
+ match
, size
- match
);
715 void set_signing_key(const char *key
)
717 gpg_interface_lazy_init();
719 free(configured_signing_key
);
720 configured_signing_key
= xstrdup(key
);
723 static int git_gpg_config(const char *var
, const char *value
,
724 const struct config_context
*ctx UNUSED
,
727 struct gpg_format
*fmt
= NULL
;
728 char *fmtname
= NULL
;
732 if (!strcmp(var
, "user.signingkey")) {
734 return config_error_nonbool(var
);
735 set_signing_key(value
);
739 if (!strcmp(var
, "gpg.format")) {
741 return config_error_nonbool(var
);
742 fmt
= get_format_by_name(value
);
744 return error(_("invalid value for '%s': '%s'"),
750 if (!strcmp(var
, "gpg.mintrustlevel")) {
752 return config_error_nonbool(var
);
754 trust
= xstrdup_toupper(value
);
755 ret
= parse_gpg_trust_level(trust
, &configured_min_trust_level
);
759 return error(_("invalid value for '%s': '%s'"),
764 if (!strcmp(var
, "gpg.ssh.defaultkeycommand"))
765 return git_config_string(&ssh_default_key_command
, var
, value
);
767 if (!strcmp(var
, "gpg.ssh.allowedsignersfile"))
768 return git_config_pathname(&ssh_allowed_signers
, var
, value
);
770 if (!strcmp(var
, "gpg.ssh.revocationfile"))
771 return git_config_pathname(&ssh_revocation_file
, var
, value
);
773 if (!strcmp(var
, "gpg.program") || !strcmp(var
, "gpg.openpgp.program"))
776 if (!strcmp(var
, "gpg.x509.program"))
779 if (!strcmp(var
, "gpg.ssh.program"))
783 fmt
= get_format_by_name(fmtname
);
784 return git_config_string(&fmt
->program
, var
, value
);
791 * Returns 1 if `string` contains a literal ssh key, 0 otherwise
792 * `key` will be set to the start of the actual key if a prefix is present.
794 static int is_literal_ssh_key(const char *string
, const char **key
)
796 if (skip_prefix(string
, "key::", key
))
798 if (starts_with(string
, "ssh-")) {
805 static char *get_ssh_key_fingerprint(const char *signing_key
)
807 struct child_process ssh_keygen
= CHILD_PROCESS_INIT
;
809 struct strbuf fingerprint_stdout
= STRBUF_INIT
;
810 struct strbuf
**fingerprint
;
811 char *fingerprint_ret
;
812 const char *literal_key
= NULL
;
815 * With SSH Signing this can contain a filename or a public key
816 * For textual representation we usually want a fingerprint
818 if (is_literal_ssh_key(signing_key
, &literal_key
)) {
819 strvec_pushl(&ssh_keygen
.args
, "ssh-keygen", "-lf", "-", NULL
);
820 ret
= pipe_command(&ssh_keygen
, literal_key
,
821 strlen(literal_key
), &fingerprint_stdout
, 0,
824 strvec_pushl(&ssh_keygen
.args
, "ssh-keygen", "-lf",
825 configured_signing_key
, NULL
);
826 ret
= pipe_command(&ssh_keygen
, NULL
, 0, &fingerprint_stdout
, 0,
831 die_errno(_("failed to get the ssh fingerprint for key '%s'"),
834 fingerprint
= strbuf_split_max(&fingerprint_stdout
, ' ', 3);
836 die_errno(_("failed to get the ssh fingerprint for key '%s'"),
839 fingerprint_ret
= strbuf_detach(fingerprint
[1], NULL
);
840 strbuf_list_free(fingerprint
);
841 strbuf_release(&fingerprint_stdout
);
842 return fingerprint_ret
;
845 /* Returns the first public key from an ssh-agent to use for signing */
846 static const char *get_default_ssh_signing_key(void)
848 struct child_process ssh_default_key
= CHILD_PROCESS_INIT
;
850 struct strbuf key_stdout
= STRBUF_INIT
, key_stderr
= STRBUF_INIT
;
851 struct strbuf
**keys
;
852 char *key_command
= NULL
;
855 char *default_key
= NULL
;
856 const char *literal_key
= NULL
;
858 if (!ssh_default_key_command
)
859 die(_("either user.signingkey or gpg.ssh.defaultKeyCommand needs to be configured"));
861 key_command
= xstrdup(ssh_default_key_command
);
862 n
= split_cmdline(key_command
, &argv
);
865 die("malformed build-time gpg.ssh.defaultKeyCommand: %s",
866 split_cmdline_strerror(n
));
868 strvec_pushv(&ssh_default_key
.args
, argv
);
869 ret
= pipe_command(&ssh_default_key
, NULL
, 0, &key_stdout
, 0,
873 keys
= strbuf_split_max(&key_stdout
, '\n', 2);
874 if (keys
[0] && is_literal_ssh_key(keys
[0]->buf
, &literal_key
)) {
876 * We only use `is_literal_ssh_key` here to check validity
877 * The prefix will be stripped when the key is used.
879 default_key
= strbuf_detach(keys
[0], NULL
);
881 warning(_("gpg.ssh.defaultKeyCommand succeeded but returned no keys: %s %s"),
882 key_stderr
.buf
, key_stdout
.buf
);
885 strbuf_list_free(keys
);
887 warning(_("gpg.ssh.defaultKeyCommand failed: %s %s"),
888 key_stderr
.buf
, key_stdout
.buf
);
893 strbuf_release(&key_stdout
);
898 static const char *get_ssh_key_id(void) {
899 return get_ssh_key_fingerprint(get_signing_key());
902 /* Returns a textual but unique representation of the signing key */
903 const char *get_signing_key_id(void)
905 gpg_interface_lazy_init();
907 if (use_format
->get_key_id
) {
908 return use_format
->get_key_id();
911 /* GPG/GPGSM only store a key id on this variable */
912 return get_signing_key();
915 const char *get_signing_key(void)
917 gpg_interface_lazy_init();
919 if (configured_signing_key
)
920 return configured_signing_key
;
921 if (use_format
->get_default_key
) {
922 return use_format
->get_default_key();
925 return git_committer_info(IDENT_STRICT
| IDENT_NO_DATE
);
928 const char *gpg_trust_level_to_str(enum signature_trust_level level
)
930 struct sigcheck_gpg_trust_level
*trust
;
932 if (level
< 0 || level
>= ARRAY_SIZE(sigcheck_gpg_trust_level
))
933 BUG("invalid trust level requested %d", level
);
935 trust
= &sigcheck_gpg_trust_level
[level
];
936 if (trust
->value
!= level
)
937 BUG("sigcheck_gpg_trust_level[] unsorted");
939 return sigcheck_gpg_trust_level
[level
].display_key
;
942 int sign_buffer(struct strbuf
*buffer
, struct strbuf
*signature
, const char *signing_key
)
944 gpg_interface_lazy_init();
946 return use_format
->sign_buffer(buffer
, signature
, signing_key
);
950 * Strip CR from the line endings, in case we are on Windows.
951 * NEEDSWORK: make it trim only CRs before LFs and rename
953 static void remove_cr_after(struct strbuf
*buffer
, size_t offset
)
957 for (i
= j
= offset
; i
< buffer
->len
; i
++) {
958 if (buffer
->buf
[i
] != '\r') {
960 buffer
->buf
[j
] = buffer
->buf
[i
];
964 strbuf_setlen(buffer
, j
);
967 static int sign_buffer_gpg(struct strbuf
*buffer
, struct strbuf
*signature
,
968 const char *signing_key
)
970 struct child_process gpg
= CHILD_PROCESS_INIT
;
974 struct strbuf gpg_status
= STRBUF_INIT
;
976 strvec_pushl(&gpg
.args
,
979 "-bsau", signing_key
,
982 bottom
= signature
->len
;
985 * When the username signingkey is bad, program could be terminated
986 * because gpg exits without reading and then write gets SIGPIPE.
988 sigchain_push(SIGPIPE
, SIG_IGN
);
989 ret
= pipe_command(&gpg
, buffer
->buf
, buffer
->len
,
990 signature
, 1024, &gpg_status
, 0);
991 sigchain_pop(SIGPIPE
);
993 for (cp
= gpg_status
.buf
;
994 cp
&& (cp
= strstr(cp
, "[GNUPG:] SIG_CREATED "));
996 if (cp
== gpg_status
.buf
|| cp
[-1] == '\n')
1001 error(_("gpg failed to sign the data:\n%s"),
1002 gpg_status
.len
? gpg_status
.buf
: "(no gpg output)");
1003 strbuf_release(&gpg_status
);
1006 strbuf_release(&gpg_status
);
1008 /* Strip CR from the line endings, in case we are on Windows. */
1009 remove_cr_after(signature
, bottom
);
1014 static int sign_buffer_ssh(struct strbuf
*buffer
, struct strbuf
*signature
,
1015 const char *signing_key
)
1017 struct child_process signer
= CHILD_PROCESS_INIT
;
1019 size_t bottom
, keylen
;
1020 struct strbuf signer_stderr
= STRBUF_INIT
;
1021 struct tempfile
*key_file
= NULL
, *buffer_file
= NULL
;
1022 char *ssh_signing_key_file
= NULL
;
1023 struct strbuf ssh_signature_filename
= STRBUF_INIT
;
1024 const char *literal_key
= NULL
;
1025 int literal_ssh_key
= 0;
1027 if (!signing_key
|| signing_key
[0] == '\0')
1029 _("user.signingKey needs to be set for ssh signing"));
1031 if (is_literal_ssh_key(signing_key
, &literal_key
)) {
1032 /* A literal ssh key */
1033 literal_ssh_key
= 1;
1034 key_file
= mks_tempfile_t(".git_signing_key_tmpXXXXXX");
1037 _("could not create temporary file"));
1038 keylen
= strlen(literal_key
);
1039 if (write_in_full(key_file
->fd
, literal_key
, keylen
) < 0 ||
1040 close_tempfile_gently(key_file
) < 0) {
1041 error_errno(_("failed writing ssh signing key to '%s'"),
1042 key_file
->filename
.buf
);
1045 ssh_signing_key_file
= strbuf_detach(&key_file
->filename
, NULL
);
1047 /* We assume a file */
1048 ssh_signing_key_file
= interpolate_path(signing_key
, 1);
1051 buffer_file
= mks_tempfile_t(".git_signing_buffer_tmpXXXXXX");
1053 error_errno(_("could not create temporary file"));
1057 if (write_in_full(buffer_file
->fd
, buffer
->buf
, buffer
->len
) < 0 ||
1058 close_tempfile_gently(buffer_file
) < 0) {
1059 error_errno(_("failed writing ssh signing key buffer to '%s'"),
1060 buffer_file
->filename
.buf
);
1064 strvec_pushl(&signer
.args
, use_format
->program
,
1067 "-f", ssh_signing_key_file
,
1069 if (literal_ssh_key
)
1070 strvec_push(&signer
.args
, "-U");
1071 strvec_push(&signer
.args
, buffer_file
->filename
.buf
);
1073 sigchain_push(SIGPIPE
, SIG_IGN
);
1074 ret
= pipe_command(&signer
, NULL
, 0, NULL
, 0, &signer_stderr
, 0);
1075 sigchain_pop(SIGPIPE
);
1078 if (strstr(signer_stderr
.buf
, "usage:"))
1079 error(_("ssh-keygen -Y sign is needed for ssh signing (available in openssh version 8.2p1+)"));
1081 ret
= error("%s", signer_stderr
.buf
);
1085 bottom
= signature
->len
;
1087 strbuf_addbuf(&ssh_signature_filename
, &buffer_file
->filename
);
1088 strbuf_addstr(&ssh_signature_filename
, ".sig");
1089 if (strbuf_read_file(signature
, ssh_signature_filename
.buf
, 0) < 0) {
1091 _("failed reading ssh signing data buffer from '%s'"),
1092 ssh_signature_filename
.buf
);
1095 /* Strip CR from the line endings, in case we are on Windows. */
1096 remove_cr_after(signature
, bottom
);
1100 delete_tempfile(&key_file
);
1102 delete_tempfile(&buffer_file
);
1103 if (ssh_signature_filename
.len
)
1104 unlink_or_warn(ssh_signature_filename
.buf
);
1105 strbuf_release(&signer_stderr
);
1106 strbuf_release(&ssh_signature_filename
);
1107 FREE_AND_NULL(ssh_signing_key_file
);