1 #include "git-compat-util.h"
5 #include "run-command.h"
9 #include "gpg-interface.h"
15 static int git_gpg_config(const char *, const char *, void *);
17 static void gpg_interface_lazy_init(void)
24 git_config(git_gpg_config
, NULL
);
27 static char *configured_signing_key
;
28 static const char *ssh_default_key_command
, *ssh_allowed_signers
, *ssh_revocation_file
;
29 static enum signature_trust_level configured_min_trust_level
= TRUST_UNDEFINED
;
34 const char **verify_args
;
36 int (*verify_signed_buffer
)(struct signature_check
*sigc
,
37 struct gpg_format
*fmt
,
38 const char *signature
,
39 size_t signature_size
);
40 int (*sign_buffer
)(struct strbuf
*buffer
, struct strbuf
*signature
,
41 const char *signing_key
);
42 const char *(*get_default_key
)(void);
43 const char *(*get_key_id
)(void);
46 static const char *openpgp_verify_args
[] = {
47 "--keyid-format=long",
50 static const char *openpgp_sigs
[] = {
51 "-----BEGIN PGP SIGNATURE-----",
52 "-----BEGIN PGP MESSAGE-----",
56 static const char *x509_verify_args
[] = {
59 static const char *x509_sigs
[] = {
60 "-----BEGIN SIGNED MESSAGE-----",
64 static const char *ssh_verify_args
[] = { NULL
};
65 static const char *ssh_sigs
[] = {
66 "-----BEGIN SSH SIGNATURE-----",
70 static int verify_gpg_signed_buffer(struct signature_check
*sigc
,
71 struct gpg_format
*fmt
,
72 const char *signature
,
73 size_t signature_size
);
74 static int verify_ssh_signed_buffer(struct signature_check
*sigc
,
75 struct gpg_format
*fmt
,
76 const char *signature
,
77 size_t signature_size
);
78 static int sign_buffer_gpg(struct strbuf
*buffer
, struct strbuf
*signature
,
79 const char *signing_key
);
80 static int sign_buffer_ssh(struct strbuf
*buffer
, struct strbuf
*signature
,
81 const char *signing_key
);
83 static const char *get_default_ssh_signing_key(void);
85 static const char *get_ssh_key_id(void);
87 static struct gpg_format gpg_format
[] = {
91 .verify_args
= openpgp_verify_args
,
93 .verify_signed_buffer
= verify_gpg_signed_buffer
,
94 .sign_buffer
= sign_buffer_gpg
,
95 .get_default_key
= NULL
,
101 .verify_args
= x509_verify_args
,
103 .verify_signed_buffer
= verify_gpg_signed_buffer
,
104 .sign_buffer
= sign_buffer_gpg
,
105 .get_default_key
= NULL
,
110 .program
= "ssh-keygen",
111 .verify_args
= ssh_verify_args
,
113 .verify_signed_buffer
= verify_ssh_signed_buffer
,
114 .sign_buffer
= sign_buffer_ssh
,
115 .get_default_key
= get_default_ssh_signing_key
,
116 .get_key_id
= get_ssh_key_id
,
120 static struct gpg_format
*use_format
= &gpg_format
[0];
122 static struct gpg_format
*get_format_by_name(const char *str
)
126 for (i
= 0; i
< ARRAY_SIZE(gpg_format
); i
++)
127 if (!strcmp(gpg_format
[i
].name
, str
))
128 return gpg_format
+ i
;
132 static struct gpg_format
*get_format_by_sig(const char *sig
)
136 for (i
= 0; i
< ARRAY_SIZE(gpg_format
); i
++)
137 for (j
= 0; gpg_format
[i
].sigs
[j
]; j
++)
138 if (starts_with(sig
, gpg_format
[i
].sigs
[j
]))
139 return gpg_format
+ i
;
143 void signature_check_clear(struct signature_check
*sigc
)
145 FREE_AND_NULL(sigc
->payload
);
146 FREE_AND_NULL(sigc
->output
);
147 FREE_AND_NULL(sigc
->gpg_status
);
148 FREE_AND_NULL(sigc
->signer
);
149 FREE_AND_NULL(sigc
->key
);
150 FREE_AND_NULL(sigc
->fingerprint
);
151 FREE_AND_NULL(sigc
->primary_key_fingerprint
);
154 /* An exclusive status -- only one of them can appear in output */
155 #define GPG_STATUS_EXCLUSIVE (1<<0)
156 /* The status includes key identifier */
157 #define GPG_STATUS_KEYID (1<<1)
158 /* The status includes user identifier */
159 #define GPG_STATUS_UID (1<<2)
160 /* The status includes key fingerprints */
161 #define GPG_STATUS_FINGERPRINT (1<<3)
162 /* The status includes trust level */
163 #define GPG_STATUS_TRUST_LEVEL (1<<4)
165 /* Short-hand for standard exclusive *SIG status with keyid & UID */
166 #define GPG_STATUS_STDSIG (GPG_STATUS_EXCLUSIVE|GPG_STATUS_KEYID|GPG_STATUS_UID)
172 } sigcheck_gpg_status
[] = {
173 { 'G', "GOODSIG ", GPG_STATUS_STDSIG
},
174 { 'B', "BADSIG ", GPG_STATUS_STDSIG
},
175 { 'E', "ERRSIG ", GPG_STATUS_EXCLUSIVE
|GPG_STATUS_KEYID
},
176 { 'X', "EXPSIG ", GPG_STATUS_STDSIG
},
177 { 'Y', "EXPKEYSIG ", GPG_STATUS_STDSIG
},
178 { 'R', "REVKEYSIG ", GPG_STATUS_STDSIG
},
179 { 0, "VALIDSIG ", GPG_STATUS_FINGERPRINT
},
180 { 0, "TRUST_", GPG_STATUS_TRUST_LEVEL
},
183 /* Keep the order same as enum signature_trust_level */
184 static struct sigcheck_gpg_trust_level
{
186 const char *display_key
;
187 enum signature_trust_level value
;
188 } sigcheck_gpg_trust_level
[] = {
189 { "UNDEFINED", "undefined", TRUST_UNDEFINED
},
190 { "NEVER", "never", TRUST_NEVER
},
191 { "MARGINAL", "marginal", TRUST_MARGINAL
},
192 { "FULLY", "fully", TRUST_FULLY
},
193 { "ULTIMATE", "ultimate", TRUST_ULTIMATE
},
196 static void replace_cstring(char **field
, const char *line
, const char *next
)
201 *field
= xmemdupz(line
, next
- line
);
206 static int parse_gpg_trust_level(const char *level
,
207 enum signature_trust_level
*res
)
211 for (i
= 0; i
< ARRAY_SIZE(sigcheck_gpg_trust_level
); i
++) {
212 if (!strcmp(sigcheck_gpg_trust_level
[i
].key
, level
)) {
213 *res
= sigcheck_gpg_trust_level
[i
].value
;
220 static void parse_gpg_output(struct signature_check
*sigc
)
222 const char *buf
= sigc
->gpg_status
;
223 const char *line
, *next
;
225 int seen_exclusive_status
= 0;
227 /* Iterate over all lines */
228 for (line
= buf
; *line
; line
= strchrnul(line
+1, '\n')) {
229 while (*line
== '\n')
234 /* Skip lines that don't start with GNUPG status */
235 if (!skip_prefix(line
, "[GNUPG:] ", &line
))
238 /* Iterate over all search strings */
239 for (i
= 0; i
< ARRAY_SIZE(sigcheck_gpg_status
); i
++) {
240 if (skip_prefix(line
, sigcheck_gpg_status
[i
].check
, &line
)) {
242 * GOODSIG, BADSIG etc. can occur only once for
243 * each signature. Therefore, if we had more
244 * than one then we're dealing with multiple
245 * signatures. We don't support them
246 * currently, and they're rather hard to
247 * create, so something is likely fishy and we
248 * should reject them altogether.
250 if (sigcheck_gpg_status
[i
].flags
& GPG_STATUS_EXCLUSIVE
) {
251 if (seen_exclusive_status
++)
255 if (sigcheck_gpg_status
[i
].result
)
256 sigc
->result
= sigcheck_gpg_status
[i
].result
;
257 /* Do we have key information? */
258 if (sigcheck_gpg_status
[i
].flags
& GPG_STATUS_KEYID
) {
259 next
= strchrnul(line
, ' ');
260 replace_cstring(&sigc
->key
, line
, next
);
261 /* Do we have signer information? */
262 if (*next
&& (sigcheck_gpg_status
[i
].flags
& GPG_STATUS_UID
)) {
264 next
= strchrnul(line
, '\n');
265 replace_cstring(&sigc
->signer
, line
, next
);
269 /* Do we have trust level? */
270 if (sigcheck_gpg_status
[i
].flags
& GPG_STATUS_TRUST_LEVEL
) {
272 * GPG v1 and v2 differs in how the
273 * TRUST_ lines are written. Some
274 * trust lines contain no additional
275 * space-separated information for v1.
277 size_t trust_size
= strcspn(line
, " \n");
278 char *trust
= xmemdupz(line
, trust_size
);
280 if (parse_gpg_trust_level(trust
, &sigc
->trust_level
)) {
287 /* Do we have fingerprint? */
288 if (sigcheck_gpg_status
[i
].flags
& GPG_STATUS_FINGERPRINT
) {
292 next
= strchrnul(line
, ' ');
293 replace_cstring(&sigc
->fingerprint
, line
, next
);
296 * Skip interim fields. The search is
297 * limited to the same line since only
298 * OpenPGP signatures has a field with
299 * the primary fingerprint.
301 limit
= strchrnul(line
, '\n');
302 for (j
= 9; j
> 0; j
--) {
303 if (!*next
|| limit
<= next
)
306 next
= strchrnul(line
, ' ');
309 field
= &sigc
->primary_key_fingerprint
;
311 next
= strchrnul(line
, '\n');
312 replace_cstring(field
, line
, next
);
314 replace_cstring(field
, NULL
, NULL
);
326 /* Clear partial data to avoid confusion */
327 FREE_AND_NULL(sigc
->primary_key_fingerprint
);
328 FREE_AND_NULL(sigc
->fingerprint
);
329 FREE_AND_NULL(sigc
->signer
);
330 FREE_AND_NULL(sigc
->key
);
333 static int verify_gpg_signed_buffer(struct signature_check
*sigc
,
334 struct gpg_format
*fmt
,
335 const char *signature
,
336 size_t signature_size
)
338 struct child_process gpg
= CHILD_PROCESS_INIT
;
339 struct tempfile
*temp
;
341 struct strbuf gpg_stdout
= STRBUF_INIT
;
342 struct strbuf gpg_stderr
= STRBUF_INIT
;
344 temp
= mks_tempfile_t(".git_vtag_tmpXXXXXX");
346 return error_errno(_("could not create temporary file"));
347 if (write_in_full(temp
->fd
, signature
, signature_size
) < 0 ||
348 close_tempfile_gently(temp
) < 0) {
349 error_errno(_("failed writing detached signature to '%s'"),
351 delete_tempfile(&temp
);
355 strvec_push(&gpg
.args
, fmt
->program
);
356 strvec_pushv(&gpg
.args
, fmt
->verify_args
);
357 strvec_pushl(&gpg
.args
,
359 "--verify", temp
->filename
.buf
, "-",
362 sigchain_push(SIGPIPE
, SIG_IGN
);
363 ret
= pipe_command(&gpg
, sigc
->payload
, sigc
->payload_len
, &gpg_stdout
, 0,
365 sigchain_pop(SIGPIPE
);
367 delete_tempfile(&temp
);
369 ret
|= !strstr(gpg_stdout
.buf
, "\n[GNUPG:] GOODSIG ");
370 sigc
->output
= strbuf_detach(&gpg_stderr
, NULL
);
371 sigc
->gpg_status
= strbuf_detach(&gpg_stdout
, NULL
);
373 parse_gpg_output(sigc
);
375 strbuf_release(&gpg_stdout
);
376 strbuf_release(&gpg_stderr
);
381 static void parse_ssh_output(struct signature_check
*sigc
)
383 const char *line
, *principal
, *search
;
388 * ssh-keygen output should be:
389 * Good "git" signature for PRINCIPAL with RSA key SHA256:FINGERPRINT
391 * or for valid but unknown keys:
392 * Good "git" signature with RSA key SHA256:FINGERPRINT
394 * Note that "PRINCIPAL" can contain whitespace, "RSA" and
395 * "SHA256" part could be a different token that names of
396 * the algorithms used, and "FINGERPRINT" is a hexadecimal
397 * string. By finding the last occurence of " with ", we can
398 * reliably parse out the PRINCIPAL.
401 sigc
->trust_level
= TRUST_NEVER
;
403 line
= to_free
= xmemdupz(sigc
->output
, strcspn(sigc
->output
, "\n"));
405 if (skip_prefix(line
, "Good \"git\" signature for ", &line
)) {
406 /* Search for the last "with" to get the full principal */
409 search
= strstr(line
, " with ");
412 } while (search
!= NULL
);
413 if (line
== principal
)
416 /* Valid signature and known principal */
418 sigc
->trust_level
= TRUST_FULLY
;
419 sigc
->signer
= xmemdupz(principal
, line
- principal
- 1);
420 } else if (skip_prefix(line
, "Good \"git\" signature with ", &line
)) {
421 /* Valid signature, but key unknown */
423 sigc
->trust_level
= TRUST_UNDEFINED
;
428 key
= strstr(line
, "key ");
430 sigc
->fingerprint
= xstrdup(strstr(line
, "key ") + 4);
431 sigc
->key
= xstrdup(sigc
->fingerprint
);
434 * Output did not match what we expected
435 * Treat the signature as bad
444 static int verify_ssh_signed_buffer(struct signature_check
*sigc
,
445 struct gpg_format
*fmt
,
446 const char *signature
,
447 size_t signature_size
)
449 struct child_process ssh_keygen
= CHILD_PROCESS_INIT
;
450 struct tempfile
*buffer_file
;
454 struct strbuf ssh_principals_out
= STRBUF_INIT
;
455 struct strbuf ssh_principals_err
= STRBUF_INIT
;
456 struct strbuf ssh_keygen_out
= STRBUF_INIT
;
457 struct strbuf ssh_keygen_err
= STRBUF_INIT
;
458 struct strbuf verify_time
= STRBUF_INIT
;
459 const struct date_mode verify_date_mode
= {
460 .type
= DATE_STRFTIME
,
461 .strftime_fmt
= "%Y%m%d%H%M%S",
462 /* SSH signing key validity has no timezone information - Use the local timezone */
466 if (!ssh_allowed_signers
) {
467 error(_("gpg.ssh.allowedSignersFile needs to be configured and exist for ssh signature verification"));
471 buffer_file
= mks_tempfile_t(".git_vtag_tmpXXXXXX");
473 return error_errno(_("could not create temporary file"));
474 if (write_in_full(buffer_file
->fd
, signature
, signature_size
) < 0 ||
475 close_tempfile_gently(buffer_file
) < 0) {
476 error_errno(_("failed writing detached signature to '%s'"),
477 buffer_file
->filename
.buf
);
478 delete_tempfile(&buffer_file
);
482 if (sigc
->payload_timestamp
)
483 strbuf_addf(&verify_time
, "-Overify-time=%s",
484 show_date(sigc
->payload_timestamp
, 0, &verify_date_mode
));
486 /* Find the principal from the signers */
487 strvec_pushl(&ssh_keygen
.args
, fmt
->program
,
488 "-Y", "find-principals",
489 "-f", ssh_allowed_signers
,
490 "-s", buffer_file
->filename
.buf
,
493 ret
= pipe_command(&ssh_keygen
, NULL
, 0, &ssh_principals_out
, 0,
494 &ssh_principals_err
, 0);
495 if (ret
&& strstr(ssh_principals_err
.buf
, "usage:")) {
496 error(_("ssh-keygen -Y find-principals/verify is needed for ssh signature verification (available in openssh version 8.2p1+)"));
499 if (ret
|| !ssh_principals_out
.len
) {
501 * We did not find a matching principal in the allowedSigners
502 * Check without validation
504 child_process_init(&ssh_keygen
);
505 strvec_pushl(&ssh_keygen
.args
, fmt
->program
,
506 "-Y", "check-novalidate",
508 "-s", buffer_file
->filename
.buf
,
511 pipe_command(&ssh_keygen
, sigc
->payload
, sigc
->payload_len
,
512 &ssh_keygen_out
, 0, &ssh_keygen_err
, 0);
515 * Fail on unknown keys
516 * we still call check-novalidate to display the signature info
520 /* Check every principal we found (one per line) */
522 for (line
= ssh_principals_out
.buf
;
525 const char *end_of_text
;
527 next
= end_of_text
= strchrnul(line
, '\n');
529 /* Did we find a LF, and did we have CR before it? */
531 line
< end_of_text
&&
532 end_of_text
[-1] == '\r')
535 /* Unless we hit NUL, skip over the LF we found */
539 /* Not all lines are data. Skip empty ones */
540 if (line
== end_of_text
)
543 /* We now know we have an non-empty line. Process it */
544 principal
= xmemdupz(line
, end_of_text
- line
);
546 child_process_init(&ssh_keygen
);
547 strbuf_release(&ssh_keygen_out
);
548 strbuf_release(&ssh_keygen_err
);
549 strvec_push(&ssh_keygen
.args
, fmt
->program
);
551 * We found principals
552 * Try with each until we find a match
554 strvec_pushl(&ssh_keygen
.args
, "-Y", "verify",
556 "-f", ssh_allowed_signers
,
558 "-s", buffer_file
->filename
.buf
,
562 if (ssh_revocation_file
) {
563 if (file_exists(ssh_revocation_file
)) {
564 strvec_pushl(&ssh_keygen
.args
, "-r",
565 ssh_revocation_file
, NULL
);
567 warning(_("ssh signing revocation file configured but not found: %s"),
568 ssh_revocation_file
);
572 sigchain_push(SIGPIPE
, SIG_IGN
);
573 ret
= pipe_command(&ssh_keygen
, sigc
->payload
, sigc
->payload_len
,
574 &ssh_keygen_out
, 0, &ssh_keygen_err
, 0);
575 sigchain_pop(SIGPIPE
);
577 FREE_AND_NULL(principal
);
580 ret
= !starts_with(ssh_keygen_out
.buf
, "Good");
587 strbuf_stripspace(&ssh_keygen_out
, 0);
588 strbuf_stripspace(&ssh_keygen_err
, 0);
589 /* Add stderr outputs to show the user actual ssh-keygen errors */
590 strbuf_add(&ssh_keygen_out
, ssh_principals_err
.buf
, ssh_principals_err
.len
);
591 strbuf_add(&ssh_keygen_out
, ssh_keygen_err
.buf
, ssh_keygen_err
.len
);
592 sigc
->output
= strbuf_detach(&ssh_keygen_out
, NULL
);
593 sigc
->gpg_status
= xstrdup(sigc
->output
);
595 parse_ssh_output(sigc
);
599 delete_tempfile(&buffer_file
);
600 strbuf_release(&ssh_principals_out
);
601 strbuf_release(&ssh_principals_err
);
602 strbuf_release(&ssh_keygen_out
);
603 strbuf_release(&ssh_keygen_err
);
604 strbuf_release(&verify_time
);
609 static int parse_payload_metadata(struct signature_check
*sigc
)
611 const char *ident_line
= NULL
;
613 struct ident_split ident
;
614 const char *signer_header
;
616 switch (sigc
->payload_type
) {
617 case SIGNATURE_PAYLOAD_COMMIT
:
618 signer_header
= "committer";
620 case SIGNATURE_PAYLOAD_TAG
:
621 signer_header
= "tagger";
623 case SIGNATURE_PAYLOAD_UNDEFINED
:
624 case SIGNATURE_PAYLOAD_PUSH_CERT
:
625 /* Ignore payloads we don't want to parse */
628 BUG("invalid value for sigc->payload_type");
631 ident_line
= find_commit_header(sigc
->payload
, signer_header
, &ident_len
);
632 if (!ident_line
|| !ident_len
)
635 if (split_ident_line(&ident
, ident_line
, ident_len
))
638 if (!sigc
->payload_timestamp
&& ident
.date_begin
&& ident
.date_end
)
639 sigc
->payload_timestamp
= parse_timestamp(ident
.date_begin
, NULL
, 10);
644 int check_signature(struct signature_check
*sigc
,
645 const char *signature
, size_t slen
)
647 struct gpg_format
*fmt
;
650 gpg_interface_lazy_init();
653 sigc
->trust_level
= -1;
655 fmt
= get_format_by_sig(signature
);
657 die(_("bad/incompatible signature '%s'"), signature
);
659 if (parse_payload_metadata(sigc
))
662 status
= fmt
->verify_signed_buffer(sigc
, fmt
, signature
, slen
);
664 if (status
&& !sigc
->output
)
667 status
|= sigc
->result
!= 'G';
668 status
|= sigc
->trust_level
< configured_min_trust_level
;
673 void print_signature_buffer(const struct signature_check
*sigc
, unsigned flags
)
675 const char *output
= flags
& GPG_VERIFY_RAW
? sigc
->gpg_status
:
678 if (flags
& GPG_VERIFY_VERBOSE
&& sigc
->payload
)
679 fwrite(sigc
->payload
, 1, sigc
->payload_len
, stdout
);
682 fputs(output
, stderr
);
685 size_t parse_signed_buffer(const char *buf
, size_t size
)
692 if (get_format_by_sig(buf
+ len
))
695 eol
= memchr(buf
+ len
, '\n', size
- len
);
696 len
+= eol
? eol
- (buf
+ len
) + 1 : size
- len
;
701 int parse_signature(const char *buf
, size_t size
, struct strbuf
*payload
, struct strbuf
*signature
)
703 size_t match
= parse_signed_buffer(buf
, size
);
705 strbuf_add(payload
, buf
, match
);
706 remove_signature(payload
);
707 strbuf_add(signature
, buf
+ match
, size
- match
);
713 void set_signing_key(const char *key
)
715 gpg_interface_lazy_init();
717 free(configured_signing_key
);
718 configured_signing_key
= xstrdup(key
);
721 static int git_gpg_config(const char *var
, const char *value
, void *cb UNUSED
)
723 struct gpg_format
*fmt
= NULL
;
724 char *fmtname
= NULL
;
728 if (!strcmp(var
, "user.signingkey")) {
730 return config_error_nonbool(var
);
731 set_signing_key(value
);
735 if (!strcmp(var
, "gpg.format")) {
737 return config_error_nonbool(var
);
738 fmt
= get_format_by_name(value
);
740 return error(_("invalid value for '%s': '%s'"),
746 if (!strcmp(var
, "gpg.mintrustlevel")) {
748 return config_error_nonbool(var
);
750 trust
= xstrdup_toupper(value
);
751 ret
= parse_gpg_trust_level(trust
, &configured_min_trust_level
);
755 return error(_("invalid value for '%s': '%s'"),
760 if (!strcmp(var
, "gpg.ssh.defaultkeycommand")) {
762 return config_error_nonbool(var
);
763 return git_config_string(&ssh_default_key_command
, var
, value
);
766 if (!strcmp(var
, "gpg.ssh.allowedsignersfile")) {
768 return config_error_nonbool(var
);
769 return git_config_pathname(&ssh_allowed_signers
, var
, value
);
772 if (!strcmp(var
, "gpg.ssh.revocationfile")) {
774 return config_error_nonbool(var
);
775 return git_config_pathname(&ssh_revocation_file
, var
, value
);
778 if (!strcmp(var
, "gpg.program") || !strcmp(var
, "gpg.openpgp.program"))
781 if (!strcmp(var
, "gpg.x509.program"))
784 if (!strcmp(var
, "gpg.ssh.program"))
788 fmt
= get_format_by_name(fmtname
);
789 return git_config_string(&fmt
->program
, var
, value
);
796 * Returns 1 if `string` contains a literal ssh key, 0 otherwise
797 * `key` will be set to the start of the actual key if a prefix is present.
799 static int is_literal_ssh_key(const char *string
, const char **key
)
801 if (skip_prefix(string
, "key::", key
))
803 if (starts_with(string
, "ssh-")) {
810 static char *get_ssh_key_fingerprint(const char *signing_key
)
812 struct child_process ssh_keygen
= CHILD_PROCESS_INIT
;
814 struct strbuf fingerprint_stdout
= STRBUF_INIT
;
815 struct strbuf
**fingerprint
;
816 char *fingerprint_ret
;
817 const char *literal_key
= NULL
;
820 * With SSH Signing this can contain a filename or a public key
821 * For textual representation we usually want a fingerprint
823 if (is_literal_ssh_key(signing_key
, &literal_key
)) {
824 strvec_pushl(&ssh_keygen
.args
, "ssh-keygen", "-lf", "-", NULL
);
825 ret
= pipe_command(&ssh_keygen
, literal_key
,
826 strlen(literal_key
), &fingerprint_stdout
, 0,
829 strvec_pushl(&ssh_keygen
.args
, "ssh-keygen", "-lf",
830 configured_signing_key
, NULL
);
831 ret
= pipe_command(&ssh_keygen
, NULL
, 0, &fingerprint_stdout
, 0,
836 die_errno(_("failed to get the ssh fingerprint for key '%s'"),
839 fingerprint
= strbuf_split_max(&fingerprint_stdout
, ' ', 3);
841 die_errno(_("failed to get the ssh fingerprint for key '%s'"),
844 fingerprint_ret
= strbuf_detach(fingerprint
[1], NULL
);
845 strbuf_list_free(fingerprint
);
846 strbuf_release(&fingerprint_stdout
);
847 return fingerprint_ret
;
850 /* Returns the first public key from an ssh-agent to use for signing */
851 static const char *get_default_ssh_signing_key(void)
853 struct child_process ssh_default_key
= CHILD_PROCESS_INIT
;
855 struct strbuf key_stdout
= STRBUF_INIT
, key_stderr
= STRBUF_INIT
;
856 struct strbuf
**keys
;
857 char *key_command
= NULL
;
860 char *default_key
= NULL
;
861 const char *literal_key
= NULL
;
863 if (!ssh_default_key_command
)
864 die(_("either user.signingkey or gpg.ssh.defaultKeyCommand needs to be configured"));
866 key_command
= xstrdup(ssh_default_key_command
);
867 n
= split_cmdline(key_command
, &argv
);
870 die("malformed build-time gpg.ssh.defaultKeyCommand: %s",
871 split_cmdline_strerror(n
));
873 strvec_pushv(&ssh_default_key
.args
, argv
);
874 ret
= pipe_command(&ssh_default_key
, NULL
, 0, &key_stdout
, 0,
878 keys
= strbuf_split_max(&key_stdout
, '\n', 2);
879 if (keys
[0] && is_literal_ssh_key(keys
[0]->buf
, &literal_key
)) {
881 * We only use `is_literal_ssh_key` here to check validity
882 * The prefix will be stripped when the key is used.
884 default_key
= strbuf_detach(keys
[0], NULL
);
886 warning(_("gpg.ssh.defaultKeyCommand succeeded but returned no keys: %s %s"),
887 key_stderr
.buf
, key_stdout
.buf
);
890 strbuf_list_free(keys
);
892 warning(_("gpg.ssh.defaultKeyCommand failed: %s %s"),
893 key_stderr
.buf
, key_stdout
.buf
);
898 strbuf_release(&key_stdout
);
903 static const char *get_ssh_key_id(void) {
904 return get_ssh_key_fingerprint(get_signing_key());
907 /* Returns a textual but unique representation of the signing key */
908 const char *get_signing_key_id(void)
910 gpg_interface_lazy_init();
912 if (use_format
->get_key_id
) {
913 return use_format
->get_key_id();
916 /* GPG/GPGSM only store a key id on this variable */
917 return get_signing_key();
920 const char *get_signing_key(void)
922 gpg_interface_lazy_init();
924 if (configured_signing_key
)
925 return configured_signing_key
;
926 if (use_format
->get_default_key
) {
927 return use_format
->get_default_key();
930 return git_committer_info(IDENT_STRICT
| IDENT_NO_DATE
);
933 const char *gpg_trust_level_to_str(enum signature_trust_level level
)
935 struct sigcheck_gpg_trust_level
*trust
;
937 if (level
< 0 || level
>= ARRAY_SIZE(sigcheck_gpg_trust_level
))
938 BUG("invalid trust level requested %d", level
);
940 trust
= &sigcheck_gpg_trust_level
[level
];
941 if (trust
->value
!= level
)
942 BUG("sigcheck_gpg_trust_level[] unsorted");
944 return sigcheck_gpg_trust_level
[level
].display_key
;
947 int sign_buffer(struct strbuf
*buffer
, struct strbuf
*signature
, const char *signing_key
)
949 gpg_interface_lazy_init();
951 return use_format
->sign_buffer(buffer
, signature
, signing_key
);
955 * Strip CR from the line endings, in case we are on Windows.
956 * NEEDSWORK: make it trim only CRs before LFs and rename
958 static void remove_cr_after(struct strbuf
*buffer
, size_t offset
)
962 for (i
= j
= offset
; i
< buffer
->len
; i
++) {
963 if (buffer
->buf
[i
] != '\r') {
965 buffer
->buf
[j
] = buffer
->buf
[i
];
969 strbuf_setlen(buffer
, j
);
972 static int sign_buffer_gpg(struct strbuf
*buffer
, struct strbuf
*signature
,
973 const char *signing_key
)
975 struct child_process gpg
= CHILD_PROCESS_INIT
;
979 struct strbuf gpg_status
= STRBUF_INIT
;
981 strvec_pushl(&gpg
.args
,
984 "-bsau", signing_key
,
987 bottom
= signature
->len
;
990 * When the username signingkey is bad, program could be terminated
991 * because gpg exits without reading and then write gets SIGPIPE.
993 sigchain_push(SIGPIPE
, SIG_IGN
);
994 ret
= pipe_command(&gpg
, buffer
->buf
, buffer
->len
,
995 signature
, 1024, &gpg_status
, 0);
996 sigchain_pop(SIGPIPE
);
998 for (cp
= gpg_status
.buf
;
999 cp
&& (cp
= strstr(cp
, "[GNUPG:] SIG_CREATED "));
1001 if (cp
== gpg_status
.buf
|| cp
[-1] == '\n')
1006 error(_("gpg failed to sign the data:\n%s"),
1007 gpg_status
.len
? gpg_status
.buf
: "(no gpg output)");
1008 strbuf_release(&gpg_status
);
1011 strbuf_release(&gpg_status
);
1013 /* Strip CR from the line endings, in case we are on Windows. */
1014 remove_cr_after(signature
, bottom
);
1019 static int sign_buffer_ssh(struct strbuf
*buffer
, struct strbuf
*signature
,
1020 const char *signing_key
)
1022 struct child_process signer
= CHILD_PROCESS_INIT
;
1024 size_t bottom
, keylen
;
1025 struct strbuf signer_stderr
= STRBUF_INIT
;
1026 struct tempfile
*key_file
= NULL
, *buffer_file
= NULL
;
1027 char *ssh_signing_key_file
= NULL
;
1028 struct strbuf ssh_signature_filename
= STRBUF_INIT
;
1029 const char *literal_key
= NULL
;
1030 int literal_ssh_key
= 0;
1032 if (!signing_key
|| signing_key
[0] == '\0')
1034 _("user.signingKey needs to be set for ssh signing"));
1036 if (is_literal_ssh_key(signing_key
, &literal_key
)) {
1037 /* A literal ssh key */
1038 literal_ssh_key
= 1;
1039 key_file
= mks_tempfile_t(".git_signing_key_tmpXXXXXX");
1042 _("could not create temporary file"));
1043 keylen
= strlen(literal_key
);
1044 if (write_in_full(key_file
->fd
, literal_key
, keylen
) < 0 ||
1045 close_tempfile_gently(key_file
) < 0) {
1046 error_errno(_("failed writing ssh signing key to '%s'"),
1047 key_file
->filename
.buf
);
1050 ssh_signing_key_file
= strbuf_detach(&key_file
->filename
, NULL
);
1052 /* We assume a file */
1053 ssh_signing_key_file
= interpolate_path(signing_key
, 1);
1056 buffer_file
= mks_tempfile_t(".git_signing_buffer_tmpXXXXXX");
1058 error_errno(_("could not create temporary file"));
1062 if (write_in_full(buffer_file
->fd
, buffer
->buf
, buffer
->len
) < 0 ||
1063 close_tempfile_gently(buffer_file
) < 0) {
1064 error_errno(_("failed writing ssh signing key buffer to '%s'"),
1065 buffer_file
->filename
.buf
);
1069 strvec_pushl(&signer
.args
, use_format
->program
,
1072 "-f", ssh_signing_key_file
,
1074 if (literal_ssh_key
)
1075 strvec_push(&signer
.args
, "-U");
1076 strvec_push(&signer
.args
, buffer_file
->filename
.buf
);
1078 sigchain_push(SIGPIPE
, SIG_IGN
);
1079 ret
= pipe_command(&signer
, NULL
, 0, NULL
, 0, &signer_stderr
, 0);
1080 sigchain_pop(SIGPIPE
);
1083 if (strstr(signer_stderr
.buf
, "usage:"))
1084 error(_("ssh-keygen -Y sign is needed for ssh signing (available in openssh version 8.2p1+)"));
1086 error("%s", signer_stderr
.buf
);
1090 bottom
= signature
->len
;
1092 strbuf_addbuf(&ssh_signature_filename
, &buffer_file
->filename
);
1093 strbuf_addstr(&ssh_signature_filename
, ".sig");
1094 if (strbuf_read_file(signature
, ssh_signature_filename
.buf
, 0) < 0) {
1096 _("failed reading ssh signing data buffer from '%s'"),
1097 ssh_signature_filename
.buf
);
1100 /* Strip CR from the line endings, in case we are on Windows. */
1101 remove_cr_after(signature
, bottom
);
1105 delete_tempfile(&key_file
);
1107 delete_tempfile(&buffer_file
);
1108 if (ssh_signature_filename
.len
)
1109 unlink_or_warn(ssh_signature_filename
.buf
);
1110 strbuf_release(&signer_stderr
);
1111 strbuf_release(&ssh_signature_filename
);
1112 FREE_AND_NULL(ssh_signing_key_file
);