blob: 95e764acb14b3e069a255a8261a7d724c5f8d1ad [file] [log] [blame]
Elijah Newrena64acf72023-03-21 06:26:02 +00001#include "git-compat-util.h"
brian m. carlson9b27b492021-02-11 02:08:06 +00002#include "commit.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07003#include "config.h"
Elijah Newrend4a4f922023-04-22 20:17:26 +00004#include "date.h"
Elijah Newrenf394e092023-03-21 06:25:54 +00005#include "gettext.h"
Junio C Hamano2f47eae2011-09-07 21:19:47 -07006#include "run-command.h"
7#include "strbuf.h"
Fabian Stelzerfacca532021-09-10 20:07:39 +00008#include "dir.h"
Elijah Newrenb5fa6082023-02-24 00:09:29 +00009#include "ident.h"
Junio C Hamano2f47eae2011-09-07 21:19:47 -070010#include "gpg-interface.h"
Elijah Newrend1cbe1e2023-04-22 20:17:20 +000011#include "path.h"
Junio C Hamano2f47eae2011-09-07 21:19:47 -070012#include "sigchain.h"
Jeff King43223532016-06-17 19:38:43 -040013#include "tempfile.h"
Fabian Stelzerfd9e2262021-09-10 20:07:37 +000014#include "alias.h"
Junio C Hamano2f47eae2011-09-07 21:19:47 -070015
Glen Chooa4e7e312023-06-28 19:26:22 +000016static int git_gpg_config(const char *, const char *,
17 const struct config_context *, void *);
Junio C Hamanofd2d4c12023-02-09 12:24:14 -080018
19static void gpg_interface_lazy_init(void)
20{
21 static int done;
22
23 if (done)
24 return;
25 done = 1;
26 git_config(git_gpg_config, NULL);
27}
28
Junio C Hamano2f47eae2011-09-07 21:19:47 -070029static char *configured_signing_key;
Fabian Stelzerfacca532021-09-10 20:07:39 +000030static const char *ssh_default_key_command, *ssh_allowed_signers, *ssh_revocation_file;
Hans Jerry Illikainen54887b42019-12-27 13:55:57 +000031static enum signature_trust_level configured_min_trust_level = TRUST_UNDEFINED;
32
Henning Schild58af57e2018-07-17 14:50:09 +020033struct gpg_format {
34 const char *name;
35 const char *program;
36 const char **verify_args;
37 const char **sigs;
Fabian Stelzerb5726a52021-09-10 20:07:34 +000038 int (*verify_signed_buffer)(struct signature_check *sigc,
Fabian Stelzer02769432021-12-09 09:52:43 +010039 struct gpg_format *fmt,
40 const char *signature,
Fabian Stelzerb5726a52021-09-10 20:07:34 +000041 size_t signature_size);
42 int (*sign_buffer)(struct strbuf *buffer, struct strbuf *signature,
43 const char *signing_key);
Fabian Stelzerfd9e2262021-09-10 20:07:37 +000044 const char *(*get_default_key)(void);
Fabian Stelzer4838f622021-09-10 20:07:38 +000045 const char *(*get_key_id)(void);
Henning Schild58af57e2018-07-17 14:50:09 +020046};
Junio C Hamano2f47eae2011-09-07 21:19:47 -070047
Henning Schild58af57e2018-07-17 14:50:09 +020048static const char *openpgp_verify_args[] = {
49 "--keyid-format=long",
50 NULL
51};
52static const char *openpgp_sigs[] = {
53 "-----BEGIN PGP SIGNATURE-----",
54 "-----BEGIN PGP MESSAGE-----",
55 NULL
56};
57
Henning Schild1e7adb92018-07-17 14:50:12 +020058static const char *x509_verify_args[] = {
59 NULL
60};
61static const char *x509_sigs[] = {
62 "-----BEGIN SIGNED MESSAGE-----",
63 NULL
64};
65
Fabian Stelzer29b31572021-09-10 20:07:36 +000066static const char *ssh_verify_args[] = { NULL };
67static const char *ssh_sigs[] = {
68 "-----BEGIN SSH SIGNATURE-----",
69 NULL
70};
71
Fabian Stelzerb5726a52021-09-10 20:07:34 +000072static int verify_gpg_signed_buffer(struct signature_check *sigc,
Fabian Stelzer02769432021-12-09 09:52:43 +010073 struct gpg_format *fmt,
74 const char *signature,
Fabian Stelzerb5726a52021-09-10 20:07:34 +000075 size_t signature_size);
Fabian Stelzerfacca532021-09-10 20:07:39 +000076static int verify_ssh_signed_buffer(struct signature_check *sigc,
Fabian Stelzer02769432021-12-09 09:52:43 +010077 struct gpg_format *fmt,
78 const char *signature,
Fabian Stelzerfacca532021-09-10 20:07:39 +000079 size_t signature_size);
Fabian Stelzerb5726a52021-09-10 20:07:34 +000080static int sign_buffer_gpg(struct strbuf *buffer, struct strbuf *signature,
81 const char *signing_key);
Fabian Stelzer29b31572021-09-10 20:07:36 +000082static int sign_buffer_ssh(struct strbuf *buffer, struct strbuf *signature,
83 const char *signing_key);
Fabian Stelzerb5726a52021-09-10 20:07:34 +000084
Fabian Stelzerfd9e2262021-09-10 20:07:37 +000085static const char *get_default_ssh_signing_key(void);
86
Fabian Stelzer4838f622021-09-10 20:07:38 +000087static const char *get_ssh_key_id(void);
88
Henning Schild58af57e2018-07-17 14:50:09 +020089static struct gpg_format gpg_format[] = {
Fabian Stelzerb5726a52021-09-10 20:07:34 +000090 {
91 .name = "openpgp",
92 .program = "gpg",
93 .verify_args = openpgp_verify_args,
94 .sigs = openpgp_sigs,
95 .verify_signed_buffer = verify_gpg_signed_buffer,
96 .sign_buffer = sign_buffer_gpg,
Fabian Stelzerfd9e2262021-09-10 20:07:37 +000097 .get_default_key = NULL,
Fabian Stelzer4838f622021-09-10 20:07:38 +000098 .get_key_id = NULL,
Henning Schild58af57e2018-07-17 14:50:09 +020099 },
Fabian Stelzerb5726a52021-09-10 20:07:34 +0000100 {
101 .name = "x509",
102 .program = "gpgsm",
103 .verify_args = x509_verify_args,
104 .sigs = x509_sigs,
105 .verify_signed_buffer = verify_gpg_signed_buffer,
106 .sign_buffer = sign_buffer_gpg,
Fabian Stelzerfd9e2262021-09-10 20:07:37 +0000107 .get_default_key = NULL,
Fabian Stelzer4838f622021-09-10 20:07:38 +0000108 .get_key_id = NULL,
Henning Schild1e7adb92018-07-17 14:50:12 +0200109 },
Fabian Stelzer29b31572021-09-10 20:07:36 +0000110 {
111 .name = "ssh",
112 .program = "ssh-keygen",
113 .verify_args = ssh_verify_args,
114 .sigs = ssh_sigs,
Fabian Stelzerfacca532021-09-10 20:07:39 +0000115 .verify_signed_buffer = verify_ssh_signed_buffer,
Fabian Stelzerfd9e2262021-09-10 20:07:37 +0000116 .sign_buffer = sign_buffer_ssh,
117 .get_default_key = get_default_ssh_signing_key,
Fabian Stelzer4838f622021-09-10 20:07:38 +0000118 .get_key_id = get_ssh_key_id,
Fabian Stelzer29b31572021-09-10 20:07:36 +0000119 },
Henning Schild58af57e2018-07-17 14:50:09 +0200120};
121
122static struct gpg_format *use_format = &gpg_format[0];
123
124static struct gpg_format *get_format_by_name(const char *str)
125{
126 int i;
127
128 for (i = 0; i < ARRAY_SIZE(gpg_format); i++)
129 if (!strcmp(gpg_format[i].name, str))
130 return gpg_format + i;
131 return NULL;
132}
133
134static struct gpg_format *get_format_by_sig(const char *sig)
135{
136 int i, j;
137
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;
142 return NULL;
143}
Junio C Hamanod7c67662014-08-19 13:18:07 -0700144
Michael J Gruber01e57b52014-06-23 09:05:47 +0200145void signature_check_clear(struct signature_check *sigc)
146{
Ævar Arnfjörð Bjarmason88ce3ef2017-06-15 23:15:49 +0000147 FREE_AND_NULL(sigc->payload);
Fabian Stelzerb5726a52021-09-10 20:07:34 +0000148 FREE_AND_NULL(sigc->output);
Ævar Arnfjörð Bjarmason88ce3ef2017-06-15 23:15:49 +0000149 FREE_AND_NULL(sigc->gpg_status);
150 FREE_AND_NULL(sigc->signer);
151 FREE_AND_NULL(sigc->key);
Michał Górny3daaaab2018-10-22 18:38:20 +0200152 FREE_AND_NULL(sigc->fingerprint);
Michał Górny4de93942018-10-22 18:38:21 +0200153 FREE_AND_NULL(sigc->primary_key_fingerprint);
Michael J Gruber01e57b52014-06-23 09:05:47 +0200154}
155
Michał Górnyda6cf1b2018-10-20 21:30:20 +0200156/* An exclusive status -- only one of them can appear in output */
157#define GPG_STATUS_EXCLUSIVE (1<<0)
Michał Górny0b11a842018-10-22 18:38:19 +0200158/* 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)
Michał Górny3daaaab2018-10-22 18:38:20 +0200162/* The status includes key fingerprints */
163#define GPG_STATUS_FINGERPRINT (1<<3)
Hans Jerry Illikainen54887b42019-12-27 13:55:57 +0000164/* The status includes trust level */
165#define GPG_STATUS_TRUST_LEVEL (1<<4)
Michał Górny0b11a842018-10-22 18:38:19 +0200166
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)
Michał Górnyda6cf1b2018-10-20 21:30:20 +0200169
Junio C Hamanoa50e7ca2014-08-14 15:31:13 -0700170static struct {
171 char result;
172 const char *check;
Michał Górnyda6cf1b2018-10-20 21:30:20 +0200173 unsigned int flags;
Junio C Hamanoa50e7ca2014-08-14 15:31:13 -0700174} sigcheck_gpg_status[] = {
Michał Górny0b11a842018-10-22 18:38:19 +0200175 { 'G', "GOODSIG ", GPG_STATUS_STDSIG },
176 { 'B', "BADSIG ", GPG_STATUS_STDSIG },
Michał Górny0b11a842018-10-22 18:38:19 +0200177 { '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 },
Michał Górny3daaaab2018-10-22 18:38:20 +0200181 { 0, "VALIDSIG ", GPG_STATUS_FINGERPRINT },
Hans Jerry Illikainen54887b42019-12-27 13:55:57 +0000182 { 0, "TRUST_", GPG_STATUS_TRUST_LEVEL },
183};
184
Jaydeep Das803978d2022-07-11 05:00:50 +0000185/* Keep the order same as enum signature_trust_level */
186static struct sigcheck_gpg_trust_level {
Hans Jerry Illikainen54887b42019-12-27 13:55:57 +0000187 const char *key;
Jaydeep Das803978d2022-07-11 05:00:50 +0000188 const char *display_key;
Hans Jerry Illikainen54887b42019-12-27 13:55:57 +0000189 enum signature_trust_level value;
190} sigcheck_gpg_trust_level[] = {
Jaydeep Das803978d2022-07-11 05:00:50 +0000191 { "UNDEFINED", "undefined", TRUST_UNDEFINED },
192 { "NEVER", "never", TRUST_NEVER },
193 { "MARGINAL", "marginal", TRUST_MARGINAL },
194 { "FULLY", "fully", TRUST_FULLY },
195 { "ULTIMATE", "ultimate", TRUST_ULTIMATE },
Junio C Hamanoa50e7ca2014-08-14 15:31:13 -0700196};
197
Hans Jerry Illikainen392b8622019-11-21 23:43:35 +0000198static void replace_cstring(char **field, const char *line, const char *next)
199{
200 free(*field);
201
202 if (line && next)
203 *field = xmemdupz(line, next - line);
204 else
205 *field = NULL;
206}
207
Hans Jerry Illikainen54887b42019-12-27 13:55:57 +0000208static int parse_gpg_trust_level(const char *level,
209 enum signature_trust_level *res)
210{
211 size_t i;
212
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;
216 return 0;
217 }
218 }
219 return 1;
220}
221
Henning Schildfbd0f162018-07-11 10:38:25 +0200222static void parse_gpg_output(struct signature_check *sigc)
Junio C Hamanoa50e7ca2014-08-14 15:31:13 -0700223{
224 const char *buf = sigc->gpg_status;
Michał Górnyda6cf1b2018-10-20 21:30:20 +0200225 const char *line, *next;
Michał Górny4de93942018-10-22 18:38:21 +0200226 int i, j;
Michał Górnyda6cf1b2018-10-20 21:30:20 +0200227 int seen_exclusive_status = 0;
Junio C Hamanoa50e7ca2014-08-14 15:31:13 -0700228
Michał Górnyda6cf1b2018-10-20 21:30:20 +0200229 /* Iterate over all lines */
230 for (line = buf; *line; line = strchrnul(line+1, '\n')) {
231 while (*line == '\n')
232 line++;
Steven Roberts64c45dc2019-07-16 11:47:37 -0700233 if (!*line)
234 break;
235
Michał Górnyda6cf1b2018-10-20 21:30:20 +0200236 /* Skip lines that don't start with GNUPG status */
237 if (!skip_prefix(line, "[GNUPG:] ", &line))
238 continue;
Junio C Hamanoa50e7ca2014-08-14 15:31:13 -0700239
Michał Górnyda6cf1b2018-10-20 21:30:20 +0200240 /* 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)) {
Hans Jerry Illikainen54887b42019-12-27 13:55:57 +0000243 /*
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.
251 */
Michał Górnyda6cf1b2018-10-20 21:30:20 +0200252 if (sigcheck_gpg_status[i].flags & GPG_STATUS_EXCLUSIVE) {
Junio C Hamano02561892018-11-03 00:53:57 +0900253 if (seen_exclusive_status++)
Hans Jerry Illikainen54887b42019-12-27 13:55:57 +0000254 goto error;
Michał Górnyda6cf1b2018-10-20 21:30:20 +0200255 }
256
Michał Górny3daaaab2018-10-22 18:38:20 +0200257 if (sigcheck_gpg_status[i].result)
258 sigc->result = sigcheck_gpg_status[i].result;
Michał Górny0b11a842018-10-22 18:38:19 +0200259 /* Do we have key information? */
260 if (sigcheck_gpg_status[i].flags & GPG_STATUS_KEYID) {
Michał Górnyda6cf1b2018-10-20 21:30:20 +0200261 next = strchrnul(line, ' ');
Hans Jerry Illikainen392b8622019-11-21 23:43:35 +0000262 replace_cstring(&sigc->key, line, next);
Michał Górny0b11a842018-10-22 18:38:19 +0200263 /* Do we have signer information? */
264 if (*next && (sigcheck_gpg_status[i].flags & GPG_STATUS_UID)) {
Michał Górnyda6cf1b2018-10-20 21:30:20 +0200265 line = next + 1;
266 next = strchrnul(line, '\n');
Hans Jerry Illikainen392b8622019-11-21 23:43:35 +0000267 replace_cstring(&sigc->signer, line, next);
Michał Górnyda6cf1b2018-10-20 21:30:20 +0200268 }
269 }
Hans Jerry Illikainen54887b42019-12-27 13:55:57 +0000270
271 /* Do we have trust level? */
272 if (sigcheck_gpg_status[i].flags & GPG_STATUS_TRUST_LEVEL) {
273 /*
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.
278 */
279 size_t trust_size = strcspn(line, " \n");
280 char *trust = xmemdupz(line, trust_size);
281
282 if (parse_gpg_trust_level(trust, &sigc->trust_level)) {
283 free(trust);
284 goto error;
285 }
286 free(trust);
287 }
288
Michał Górny3daaaab2018-10-22 18:38:20 +0200289 /* Do we have fingerprint? */
290 if (sigcheck_gpg_status[i].flags & GPG_STATUS_FINGERPRINT) {
Hans Jerry Illikainen67a6ea62019-11-22 20:23:12 +0000291 const char *limit;
292 char **field;
293
Michał Górny3daaaab2018-10-22 18:38:20 +0200294 next = strchrnul(line, ' ');
Hans Jerry Illikainen392b8622019-11-21 23:43:35 +0000295 replace_cstring(&sigc->fingerprint, line, next);
Michał Górny4de93942018-10-22 18:38:21 +0200296
Hans Jerry Illikainen67a6ea62019-11-22 20:23:12 +0000297 /*
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.
302 */
303 limit = strchrnul(line, '\n');
Michał Górny4de93942018-10-22 18:38:21 +0200304 for (j = 9; j > 0; j--) {
Hans Jerry Illikainen67a6ea62019-11-22 20:23:12 +0000305 if (!*next || limit <= next)
Michał Górny4de93942018-10-22 18:38:21 +0200306 break;
307 line = next + 1;
308 next = strchrnul(line, ' ');
309 }
310
Hans Jerry Illikainen67a6ea62019-11-22 20:23:12 +0000311 field = &sigc->primary_key_fingerprint;
312 if (!j) {
313 next = strchrnul(line, '\n');
314 replace_cstring(field, line, next);
315 } else {
316 replace_cstring(field, NULL, NULL);
317 }
Michał Górny3daaaab2018-10-22 18:38:20 +0200318 }
Michał Górnyda6cf1b2018-10-20 21:30:20 +0200319
320 break;
Michael J Gruber661a1802016-10-12 15:04:15 +0200321 }
Junio C Hamanoa50e7ca2014-08-14 15:31:13 -0700322 }
323 }
Michał Górnyda6cf1b2018-10-20 21:30:20 +0200324 return;
325
Hans Jerry Illikainen54887b42019-12-27 13:55:57 +0000326error:
Michał Górnyda6cf1b2018-10-20 21:30:20 +0200327 sigc->result = 'E';
328 /* Clear partial data to avoid confusion */
Michał Górny4de93942018-10-22 18:38:21 +0200329 FREE_AND_NULL(sigc->primary_key_fingerprint);
Michał Górny3daaaab2018-10-22 18:38:20 +0200330 FREE_AND_NULL(sigc->fingerprint);
Michał Górnyda6cf1b2018-10-20 21:30:20 +0200331 FREE_AND_NULL(sigc->signer);
332 FREE_AND_NULL(sigc->key);
Junio C Hamanoa50e7ca2014-08-14 15:31:13 -0700333}
334
Fabian Stelzerb5726a52021-09-10 20:07:34 +0000335static int verify_gpg_signed_buffer(struct signature_check *sigc,
Fabian Stelzer02769432021-12-09 09:52:43 +0100336 struct gpg_format *fmt,
337 const char *signature,
Fabian Stelzerb5726a52021-09-10 20:07:34 +0000338 size_t signature_size)
Hans Jerry Illikainen67948982020-03-04 11:48:04 +0000339{
340 struct child_process gpg = CHILD_PROCESS_INIT;
Hans Jerry Illikainen67948982020-03-04 11:48:04 +0000341 struct tempfile *temp;
342 int ret;
Fabian Stelzerb5726a52021-09-10 20:07:34 +0000343 struct strbuf gpg_stdout = STRBUF_INIT;
344 struct strbuf gpg_stderr = STRBUF_INIT;
Hans Jerry Illikainen67948982020-03-04 11:48:04 +0000345
346 temp = mks_tempfile_t(".git_vtag_tmpXXXXXX");
347 if (!temp)
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'"),
352 temp->filename.buf);
353 delete_tempfile(&temp);
354 return -1;
355 }
356
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400357 strvec_push(&gpg.args, fmt->program);
358 strvec_pushv(&gpg.args, fmt->verify_args);
359 strvec_pushl(&gpg.args,
Jeff Kingf6d89422020-07-28 16:26:31 -0400360 "--status-fd=1",
361 "--verify", temp->filename.buf, "-",
362 NULL);
Hans Jerry Illikainen67948982020-03-04 11:48:04 +0000363
Hans Jerry Illikainen67948982020-03-04 11:48:04 +0000364 sigchain_push(SIGPIPE, SIG_IGN);
Fabian Stelzer02769432021-12-09 09:52:43 +0100365 ret = pipe_command(&gpg, sigc->payload, sigc->payload_len, &gpg_stdout, 0,
Fabian Stelzerb5726a52021-09-10 20:07:34 +0000366 &gpg_stderr, 0);
Hans Jerry Illikainen67948982020-03-04 11:48:04 +0000367 sigchain_pop(SIGPIPE);
368
369 delete_tempfile(&temp);
370
Fabian Stelzerb5726a52021-09-10 20:07:34 +0000371 ret |= !strstr(gpg_stdout.buf, "\n[GNUPG:] GOODSIG ");
Fabian Stelzerb5726a52021-09-10 20:07:34 +0000372 sigc->output = strbuf_detach(&gpg_stderr, NULL);
373 sigc->gpg_status = strbuf_detach(&gpg_stdout, NULL);
374
375 parse_gpg_output(sigc);
376
377 strbuf_release(&gpg_stdout);
378 strbuf_release(&gpg_stderr);
Hans Jerry Illikainen67948982020-03-04 11:48:04 +0000379
380 return ret;
381}
382
Fabian Stelzerfacca532021-09-10 20:07:39 +0000383static void parse_ssh_output(struct signature_check *sigc)
384{
385 const char *line, *principal, *search;
Jeff King78d468f2021-10-18 13:15:00 -0400386 char *to_free;
Fabian Stelzerfacca532021-09-10 20:07:39 +0000387 char *key = NULL;
388
389 /*
390 * ssh-keygen output should be:
391 * Good "git" signature for PRINCIPAL with RSA key SHA256:FINGERPRINT
392 *
393 * or for valid but unknown keys:
394 * Good "git" signature with RSA key SHA256:FINGERPRINT
395 *
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.
401 */
402 sigc->result = 'B';
403 sigc->trust_level = TRUST_NEVER;
404
Jeff King78d468f2021-10-18 13:15:00 -0400405 line = to_free = xmemdupz(sigc->output, strcspn(sigc->output, "\n"));
Fabian Stelzerfacca532021-09-10 20:07:39 +0000406
407 if (skip_prefix(line, "Good \"git\" signature for ", &line)) {
Fabian Stelzerfacca532021-09-10 20:07:39 +0000408 /* Search for the last "with" to get the full principal */
409 principal = line;
410 do {
411 search = strstr(line, " with ");
412 if (search)
413 line = search + 1;
414 } while (search != NULL);
René Scharfe18b18502021-10-30 19:04:56 +0200415 if (line == principal)
416 goto cleanup;
417
418 /* Valid signature and known principal */
419 sigc->result = 'G';
420 sigc->trust_level = TRUST_FULLY;
Fabian Stelzerfacca532021-09-10 20:07:39 +0000421 sigc->signer = xmemdupz(principal, line - principal - 1);
422 } else if (skip_prefix(line, "Good \"git\" signature with ", &line)) {
423 /* Valid signature, but key unknown */
424 sigc->result = 'G';
425 sigc->trust_level = TRUST_UNDEFINED;
426 } else {
Jeff King78d468f2021-10-18 13:15:00 -0400427 goto cleanup;
Fabian Stelzerfacca532021-09-10 20:07:39 +0000428 }
429
René Scharfe65db97b2021-10-30 19:07:38 +0200430 key = strstr(line, "key ");
Fabian Stelzerfacca532021-09-10 20:07:39 +0000431 if (key) {
René Scharfe65db97b2021-10-30 19:07:38 +0200432 sigc->fingerprint = xstrdup(strstr(line, "key ") + 4);
Fabian Stelzerfacca532021-09-10 20:07:39 +0000433 sigc->key = xstrdup(sigc->fingerprint);
434 } else {
435 /*
436 * Output did not match what we expected
437 * Treat the signature as bad
438 */
439 sigc->result = 'B';
440 }
Jeff King78d468f2021-10-18 13:15:00 -0400441
442cleanup:
443 free(to_free);
Fabian Stelzerfacca532021-09-10 20:07:39 +0000444}
445
446static int verify_ssh_signed_buffer(struct signature_check *sigc,
Fabian Stelzer02769432021-12-09 09:52:43 +0100447 struct gpg_format *fmt,
448 const char *signature,
Fabian Stelzerfacca532021-09-10 20:07:39 +0000449 size_t signature_size)
450{
451 struct child_process ssh_keygen = CHILD_PROCESS_INIT;
452 struct tempfile *buffer_file;
453 int ret = -1;
454 const char *line;
Fabian Stelzerfacca532021-09-10 20:07:39 +0000455 char *principal;
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;
Fabian Stelzer6393c952021-12-09 09:52:45 +0100460 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 */
465 .local = 1,
466 };
Fabian Stelzerfacca532021-09-10 20:07:39 +0000467
468 if (!ssh_allowed_signers) {
469 error(_("gpg.ssh.allowedSignersFile needs to be configured and exist for ssh signature verification"));
470 return -1;
471 }
472
473 buffer_file = mks_tempfile_t(".git_vtag_tmpXXXXXX");
474 if (!buffer_file)
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);
481 return -1;
482 }
483
Fabian Stelzer6393c952021-12-09 09:52:45 +0100484 if (sigc->payload_timestamp)
485 strbuf_addf(&verify_time, "-Overify-time=%s",
486 show_date(sigc->payload_timestamp, 0, &verify_date_mode));
487
Fabian Stelzerfacca532021-09-10 20:07:39 +0000488 /* 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,
Fabian Stelzer6393c952021-12-09 09:52:45 +0100493 verify_time.buf,
Fabian Stelzerfacca532021-09-10 20:07:39 +0000494 NULL);
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+)"));
499 goto out;
500 }
501 if (ret || !ssh_principals_out.len) {
502 /*
503 * We did not find a matching principal in the allowedSigners
504 * Check without validation
505 */
506 child_process_init(&ssh_keygen);
507 strvec_pushl(&ssh_keygen.args, fmt->program,
508 "-Y", "check-novalidate",
509 "-n", "git",
510 "-s", buffer_file->filename.buf,
Fabian Stelzer6393c952021-12-09 09:52:45 +0100511 verify_time.buf,
Fabian Stelzerfacca532021-09-10 20:07:39 +0000512 NULL);
Fabian Stelzer02769432021-12-09 09:52:43 +0100513 pipe_command(&ssh_keygen, sigc->payload, sigc->payload_len,
Fabian Stelzerfacca532021-09-10 20:07:39 +0000514 &ssh_keygen_out, 0, &ssh_keygen_err, 0);
515
516 /*
517 * Fail on unknown keys
518 * we still call check-novalidate to display the signature info
519 */
520 ret = -1;
521 } else {
522 /* Check every principal we found (one per line) */
Fabian Stelzercaeef012022-01-07 10:07:35 +0100523 const char *next;
524 for (line = ssh_principals_out.buf;
525 *line;
526 line = next) {
527 const char *end_of_text;
Fabian Stelzerfacca532021-09-10 20:07:39 +0000528
Fabian Stelzercaeef012022-01-07 10:07:35 +0100529 next = end_of_text = strchrnul(line, '\n');
530
531 /* Did we find a LF, and did we have CR before it? */
532 if (*end_of_text &&
533 line < end_of_text &&
534 end_of_text[-1] == '\r')
535 end_of_text--;
536
537 /* Unless we hit NUL, skip over the LF we found */
538 if (*next)
539 next++;
540
541 /* Not all lines are data. Skip empty ones */
542 if (line == end_of_text)
543 continue;
544
545 /* We now know we have an non-empty line. Process it */
546 principal = xmemdupz(line, end_of_text - line);
Fabian Stelzerfacca532021-09-10 20:07:39 +0000547
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);
552 /*
553 * We found principals
554 * Try with each until we find a match
555 */
556 strvec_pushl(&ssh_keygen.args, "-Y", "verify",
557 "-n", "git",
558 "-f", ssh_allowed_signers,
559 "-I", principal,
560 "-s", buffer_file->filename.buf,
Fabian Stelzer6393c952021-12-09 09:52:45 +0100561 verify_time.buf,
Fabian Stelzerfacca532021-09-10 20:07:39 +0000562 NULL);
563
564 if (ssh_revocation_file) {
565 if (file_exists(ssh_revocation_file)) {
566 strvec_pushl(&ssh_keygen.args, "-r",
567 ssh_revocation_file, NULL);
568 } else {
569 warning(_("ssh signing revocation file configured but not found: %s"),
570 ssh_revocation_file);
571 }
572 }
573
574 sigchain_push(SIGPIPE, SIG_IGN);
Fabian Stelzer02769432021-12-09 09:52:43 +0100575 ret = pipe_command(&ssh_keygen, sigc->payload, sigc->payload_len,
Fabian Stelzerfacca532021-09-10 20:07:39 +0000576 &ssh_keygen_out, 0, &ssh_keygen_err, 0);
577 sigchain_pop(SIGPIPE);
578
579 FREE_AND_NULL(principal);
580
581 if (!ret)
582 ret = !starts_with(ssh_keygen_out.buf, "Good");
583
584 if (!ret)
585 break;
586 }
587 }
588
Calvin Wan787cb8a2023-06-06 19:48:43 +0000589 strbuf_stripspace(&ssh_keygen_out, '\0');
590 strbuf_stripspace(&ssh_keygen_err, '\0');
Fabian Stelzerfacca532021-09-10 20:07:39 +0000591 /* 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);
596
597 parse_ssh_output(sigc);
598
599out:
600 if (buffer_file)
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);
Fabian Stelzer6393c952021-12-09 09:52:45 +0100606 strbuf_release(&verify_time);
Fabian Stelzerfacca532021-09-10 20:07:39 +0000607
608 return ret;
609}
610
Fabian Stelzer6393c952021-12-09 09:52:45 +0100611static int parse_payload_metadata(struct signature_check *sigc)
612{
613 const char *ident_line = NULL;
614 size_t ident_len;
615 struct ident_split ident;
616 const char *signer_header;
617
618 switch (sigc->payload_type) {
619 case SIGNATURE_PAYLOAD_COMMIT:
620 signer_header = "committer";
621 break;
622 case SIGNATURE_PAYLOAD_TAG:
623 signer_header = "tagger";
624 break;
625 case SIGNATURE_PAYLOAD_UNDEFINED:
626 case SIGNATURE_PAYLOAD_PUSH_CERT:
627 /* Ignore payloads we don't want to parse */
628 return 0;
629 default:
630 BUG("invalid value for sigc->payload_type");
631 }
632
633 ident_line = find_commit_header(sigc->payload, signer_header, &ident_len);
634 if (!ident_line || !ident_len)
635 return 1;
636
637 if (split_ident_line(&ident, ident_line, ident_len))
638 return 1;
639
640 if (!sigc->payload_timestamp && ident.date_begin && ident.date_end)
641 sigc->payload_timestamp = parse_timestamp(ident.date_begin, NULL, 10);
642
643 return 0;
644}
645
Fabian Stelzer02769432021-12-09 09:52:43 +0100646int check_signature(struct signature_check *sigc,
647 const char *signature, size_t slen)
brian m. carlsona4cc18f2015-06-21 23:14:38 +0000648{
Fabian Stelzerb5726a52021-09-10 20:07:34 +0000649 struct gpg_format *fmt;
brian m. carlsona4cc18f2015-06-21 23:14:38 +0000650 int status;
651
Junio C Hamanofd2d4c12023-02-09 12:24:14 -0800652 gpg_interface_lazy_init();
653
brian m. carlsona4cc18f2015-06-21 23:14:38 +0000654 sigc->result = 'N';
Jeff King7891e462023-04-18 21:29:57 -0400655 sigc->trust_level = TRUST_UNDEFINED;
brian m. carlsona4cc18f2015-06-21 23:14:38 +0000656
Fabian Stelzerb5726a52021-09-10 20:07:34 +0000657 fmt = get_format_by_sig(signature);
658 if (!fmt)
659 die(_("bad/incompatible signature '%s'"), signature);
660
Fabian Stelzer6393c952021-12-09 09:52:45 +0100661 if (parse_payload_metadata(sigc))
662 return 1;
663
Fabian Stelzer02769432021-12-09 09:52:43 +0100664 status = fmt->verify_signed_buffer(sigc, fmt, signature, slen);
Fabian Stelzerb5726a52021-09-10 20:07:34 +0000665
666 if (status && !sigc->output)
667 return !!status;
668
Hans Jerry Illikainen54887b42019-12-27 13:55:57 +0000669 status |= sigc->result != 'G';
670 status |= sigc->trust_level < configured_min_trust_level;
brian m. carlsona4cc18f2015-06-21 23:14:38 +0000671
Junio C Hamano4e5dc9c2018-08-09 11:40:27 -0700672 return !!status;
brian m. carlsona4cc18f2015-06-21 23:14:38 +0000673}
674
brian m. carlsonca194d52015-06-21 23:14:41 +0000675void print_signature_buffer(const struct signature_check *sigc, unsigned flags)
676{
Fabian Stelzerb5726a52021-09-10 20:07:34 +0000677 const char *output = flags & GPG_VERIFY_RAW ? sigc->gpg_status :
678 sigc->output;
brian m. carlsonaeff29d2015-06-21 23:14:42 +0000679
brian m. carlsonca194d52015-06-21 23:14:41 +0000680 if (flags & GPG_VERIFY_VERBOSE && sigc->payload)
Fabian Stelzer02769432021-12-09 09:52:43 +0100681 fwrite(sigc->payload, 1, sigc->payload_len, stdout);
brian m. carlsonca194d52015-06-21 23:14:41 +0000682
brian m. carlsonaeff29d2015-06-21 23:14:42 +0000683 if (output)
684 fputs(output, stderr);
brian m. carlsonca194d52015-06-21 23:14:41 +0000685}
686
brian m. carlson482c1192021-02-11 02:08:03 +0000687size_t parse_signed_buffer(const char *buf, size_t size)
Junio C Hamanod7c67662014-08-19 13:18:07 -0700688{
Junio C Hamanod7c67662014-08-19 13:18:07 -0700689 size_t len = 0;
Jeff King8b44b2b2018-04-13 15:18:35 -0600690 size_t match = size;
691 while (len < size) {
692 const char *eol;
693
Henning Schild58af57e2018-07-17 14:50:09 +0200694 if (get_format_by_sig(buf + len))
Jeff King8b44b2b2018-04-13 15:18:35 -0600695 match = len;
696
697 eol = memchr(buf + len, '\n', size - len);
Junio C Hamanod7c67662014-08-19 13:18:07 -0700698 len += eol ? eol - (buf + len) + 1 : size - len;
699 }
Jeff King8b44b2b2018-04-13 15:18:35 -0600700 return match;
Junio C Hamanod7c67662014-08-19 13:18:07 -0700701}
702
brian m. carlson482c1192021-02-11 02:08:03 +0000703int parse_signature(const char *buf, size_t size, struct strbuf *payload, struct strbuf *signature)
704{
705 size_t match = parse_signed_buffer(buf, size);
706 if (match != size) {
707 strbuf_add(payload, buf, match);
brian m. carlson9b27b492021-02-11 02:08:06 +0000708 remove_signature(payload);
brian m. carlson482c1192021-02-11 02:08:03 +0000709 strbuf_add(signature, buf + match, size - match);
710 return 1;
711 }
712 return 0;
713}
714
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700715void set_signing_key(const char *key)
716{
Junio C Hamanofd2d4c12023-02-09 12:24:14 -0800717 gpg_interface_lazy_init();
718
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700719 free(configured_signing_key);
720 configured_signing_key = xstrdup(key);
721}
722
Glen Chooa4e7e312023-06-28 19:26:22 +0000723static int git_gpg_config(const char *var, const char *value,
724 const struct config_context *ctx UNUSED,
725 void *cb UNUSED)
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700726{
Henning Schild58af57e2018-07-17 14:50:09 +0200727 struct gpg_format *fmt = NULL;
728 char *fmtname = NULL;
Hans Jerry Illikainen54887b42019-12-27 13:55:57 +0000729 char *trust;
730 int ret;
Henning Schild58af57e2018-07-17 14:50:09 +0200731
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700732 if (!strcmp(var, "user.signingkey")) {
Jeff King1b0eeec2018-04-13 15:18:30 -0600733 if (!value)
734 return config_error_nonbool(var);
Junio C Hamano0c5e70f2011-11-29 12:29:48 -0800735 set_signing_key(value);
Jeff King1b0eeec2018-04-13 15:18:30 -0600736 return 0;
Junio C Hamano0c5e70f2011-11-29 12:29:48 -0800737 }
Jeff King1b0eeec2018-04-13 15:18:30 -0600738
Henning Schild57a8dd72018-07-17 14:50:07 +0200739 if (!strcmp(var, "gpg.format")) {
740 if (!value)
741 return config_error_nonbool(var);
Henning Schild58af57e2018-07-17 14:50:09 +0200742 fmt = get_format_by_name(value);
743 if (!fmt)
Jean-Noël Avila1a8aea82022-01-31 22:07:47 +0000744 return error(_("invalid value for '%s': '%s'"),
Henning Schild57a8dd72018-07-17 14:50:07 +0200745 var, value);
Henning Schild58af57e2018-07-17 14:50:09 +0200746 use_format = fmt;
747 return 0;
Henning Schild57a8dd72018-07-17 14:50:07 +0200748 }
749
Hans Jerry Illikainen54887b42019-12-27 13:55:57 +0000750 if (!strcmp(var, "gpg.mintrustlevel")) {
751 if (!value)
752 return config_error_nonbool(var);
753
754 trust = xstrdup_toupper(value);
755 ret = parse_gpg_trust_level(trust, &configured_min_trust_level);
756 free(trust);
757
758 if (ret)
Jean-Noël Avila1a8aea82022-01-31 22:07:47 +0000759 return error(_("invalid value for '%s': '%s'"),
760 var, value);
Hans Jerry Illikainen54887b42019-12-27 13:55:57 +0000761 return 0;
762 }
763
Jeff King004c9432023-12-07 02:26:31 -0500764 if (!strcmp(var, "gpg.ssh.defaultkeycommand"))
Fabian Stelzerfd9e2262021-09-10 20:07:37 +0000765 return git_config_string(&ssh_default_key_command, var, value);
Fabian Stelzerfd9e2262021-09-10 20:07:37 +0000766
Jeff King004c9432023-12-07 02:26:31 -0500767 if (!strcmp(var, "gpg.ssh.allowedsignersfile"))
Fabian Stelzerfacca532021-09-10 20:07:39 +0000768 return git_config_pathname(&ssh_allowed_signers, var, value);
Fabian Stelzerfacca532021-09-10 20:07:39 +0000769
Jeff King004c9432023-12-07 02:26:31 -0500770 if (!strcmp(var, "gpg.ssh.revocationfile"))
Fabian Stelzerfacca532021-09-10 20:07:39 +0000771 return git_config_pathname(&ssh_revocation_file, var, value);
Fabian Stelzerfacca532021-09-10 20:07:39 +0000772
Henning Schildb02f51b2018-07-17 14:50:11 +0200773 if (!strcmp(var, "gpg.program") || !strcmp(var, "gpg.openpgp.program"))
Henning Schild58af57e2018-07-17 14:50:09 +0200774 fmtname = "openpgp";
775
Henning Schild1e7adb92018-07-17 14:50:12 +0200776 if (!strcmp(var, "gpg.x509.program"))
777 fmtname = "x509";
778
Fabian Stelzer29b31572021-09-10 20:07:36 +0000779 if (!strcmp(var, "gpg.ssh.program"))
780 fmtname = "ssh";
781
Henning Schild58af57e2018-07-17 14:50:09 +0200782 if (fmtname) {
783 fmt = get_format_by_name(fmtname);
784 return git_config_string(&fmt->program, var, value);
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700785 }
Jeff King1b0eeec2018-04-13 15:18:30 -0600786
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700787 return 0;
788}
789
Fabian Stelzer350a2512021-11-19 16:07:06 +0100790/*
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.
793 */
794static int is_literal_ssh_key(const char *string, const char **key)
795{
796 if (skip_prefix(string, "key::", key))
797 return 1;
798 if (starts_with(string, "ssh-")) {
799 *key = string;
800 return 1;
801 }
802 return 0;
803}
804
Fabian Stelzer4838f622021-09-10 20:07:38 +0000805static char *get_ssh_key_fingerprint(const char *signing_key)
806{
807 struct child_process ssh_keygen = CHILD_PROCESS_INIT;
808 int ret = -1;
809 struct strbuf fingerprint_stdout = STRBUF_INIT;
810 struct strbuf **fingerprint;
Jeff Kingf3af71c2021-10-18 13:15:37 -0400811 char *fingerprint_ret;
Fabian Stelzer350a2512021-11-19 16:07:06 +0100812 const char *literal_key = NULL;
Fabian Stelzer4838f622021-09-10 20:07:38 +0000813
814 /*
815 * With SSH Signing this can contain a filename or a public key
816 * For textual representation we usually want a fingerprint
817 */
Fabian Stelzer350a2512021-11-19 16:07:06 +0100818 if (is_literal_ssh_key(signing_key, &literal_key)) {
Fabian Stelzer4838f622021-09-10 20:07:38 +0000819 strvec_pushl(&ssh_keygen.args, "ssh-keygen", "-lf", "-", NULL);
Fabian Stelzer350a2512021-11-19 16:07:06 +0100820 ret = pipe_command(&ssh_keygen, literal_key,
821 strlen(literal_key), &fingerprint_stdout, 0,
Fabian Stelzer4838f622021-09-10 20:07:38 +0000822 NULL, 0);
823 } else {
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,
827 NULL, 0);
828 }
829
830 if (!!ret)
831 die_errno(_("failed to get the ssh fingerprint for key '%s'"),
832 signing_key);
833
834 fingerprint = strbuf_split_max(&fingerprint_stdout, ' ', 3);
835 if (!fingerprint[1])
836 die_errno(_("failed to get the ssh fingerprint for key '%s'"),
837 signing_key);
838
Jeff Kingf3af71c2021-10-18 13:15:37 -0400839 fingerprint_ret = strbuf_detach(fingerprint[1], NULL);
840 strbuf_list_free(fingerprint);
841 strbuf_release(&fingerprint_stdout);
842 return fingerprint_ret;
Fabian Stelzer4838f622021-09-10 20:07:38 +0000843}
844
Fabian Stelzerfd9e2262021-09-10 20:07:37 +0000845/* Returns the first public key from an ssh-agent to use for signing */
846static const char *get_default_ssh_signing_key(void)
847{
848 struct child_process ssh_default_key = CHILD_PROCESS_INIT;
849 int ret = -1;
850 struct strbuf key_stdout = STRBUF_INIT, key_stderr = STRBUF_INIT;
851 struct strbuf **keys;
852 char *key_command = NULL;
853 const char **argv;
854 int n;
855 char *default_key = NULL;
Fabian Stelzer350a2512021-11-19 16:07:06 +0100856 const char *literal_key = NULL;
Fabian Stelzerfd9e2262021-09-10 20:07:37 +0000857
858 if (!ssh_default_key_command)
859 die(_("either user.signingkey or gpg.ssh.defaultKeyCommand needs to be configured"));
860
861 key_command = xstrdup(ssh_default_key_command);
862 n = split_cmdline(key_command, &argv);
863
864 if (n < 0)
865 die("malformed build-time gpg.ssh.defaultKeyCommand: %s",
866 split_cmdline_strerror(n));
867
868 strvec_pushv(&ssh_default_key.args, argv);
869 ret = pipe_command(&ssh_default_key, NULL, 0, &key_stdout, 0,
870 &key_stderr, 0);
871
872 if (!ret) {
873 keys = strbuf_split_max(&key_stdout, '\n', 2);
Fabian Stelzer350a2512021-11-19 16:07:06 +0100874 if (keys[0] && is_literal_ssh_key(keys[0]->buf, &literal_key)) {
875 /*
876 * We only use `is_literal_ssh_key` here to check validity
877 * The prefix will be stripped when the key is used.
878 */
Fabian Stelzerfd9e2262021-09-10 20:07:37 +0000879 default_key = strbuf_detach(keys[0], NULL);
880 } else {
Jiang Xinf7337192021-11-01 10:14:17 +0800881 warning(_("gpg.ssh.defaultKeyCommand succeeded but returned no keys: %s %s"),
Fabian Stelzerfd9e2262021-09-10 20:07:37 +0000882 key_stderr.buf, key_stdout.buf);
883 }
884
885 strbuf_list_free(keys);
886 } else {
887 warning(_("gpg.ssh.defaultKeyCommand failed: %s %s"),
888 key_stderr.buf, key_stdout.buf);
889 }
890
891 free(key_command);
892 free(argv);
893 strbuf_release(&key_stdout);
894
895 return default_key;
896}
897
Fabian Stelzer4838f622021-09-10 20:07:38 +0000898static const char *get_ssh_key_id(void) {
899 return get_ssh_key_fingerprint(get_signing_key());
900}
901
902/* Returns a textual but unique representation of the signing key */
903const char *get_signing_key_id(void)
904{
Junio C Hamanofd2d4c12023-02-09 12:24:14 -0800905 gpg_interface_lazy_init();
906
Fabian Stelzer4838f622021-09-10 20:07:38 +0000907 if (use_format->get_key_id) {
908 return use_format->get_key_id();
909 }
910
911 /* GPG/GPGSM only store a key id on this variable */
912 return get_signing_key();
913}
914
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700915const char *get_signing_key(void)
916{
Junio C Hamanofd2d4c12023-02-09 12:24:14 -0800917 gpg_interface_lazy_init();
918
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700919 if (configured_signing_key)
920 return configured_signing_key;
Fabian Stelzerfd9e2262021-09-10 20:07:37 +0000921 if (use_format->get_default_key) {
922 return use_format->get_default_key();
923 }
924
925 return git_committer_info(IDENT_STRICT | IDENT_NO_DATE);
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700926}
927
Jaydeep Das803978d2022-07-11 05:00:50 +0000928const char *gpg_trust_level_to_str(enum signature_trust_level level)
929{
930 struct sigcheck_gpg_trust_level *trust;
931
932 if (level < 0 || level >= ARRAY_SIZE(sigcheck_gpg_trust_level))
933 BUG("invalid trust level requested %d", level);
934
935 trust = &sigcheck_gpg_trust_level[level];
936 if (trust->value != level)
937 BUG("sigcheck_gpg_trust_level[] unsorted");
938
939 return sigcheck_gpg_trust_level[level].display_key;
940}
941
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700942int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *signing_key)
943{
Junio C Hamanofd2d4c12023-02-09 12:24:14 -0800944 gpg_interface_lazy_init();
945
Fabian Stelzerb5726a52021-09-10 20:07:34 +0000946 return use_format->sign_buffer(buffer, signature, signing_key);
947}
948
Fabian Stelzer29b31572021-09-10 20:07:36 +0000949/*
950 * Strip CR from the line endings, in case we are on Windows.
951 * NEEDSWORK: make it trim only CRs before LFs and rename
952 */
953static void remove_cr_after(struct strbuf *buffer, size_t offset)
954{
955 size_t i, j;
956
957 for (i = j = offset; i < buffer->len; i++) {
958 if (buffer->buf[i] != '\r') {
959 if (i != j)
960 buffer->buf[j] = buffer->buf[i];
961 j++;
962 }
963 }
964 strbuf_setlen(buffer, j);
965}
966
Fabian Stelzerb5726a52021-09-10 20:07:34 +0000967static int sign_buffer_gpg(struct strbuf *buffer, struct strbuf *signature,
968 const char *signing_key)
969{
René Scharfed3180272014-08-19 21:09:35 +0200970 struct child_process gpg = CHILD_PROCESS_INIT;
Jeff King0581b542016-06-17 19:38:55 -0400971 int ret;
Fabian Stelzer29b31572021-09-10 20:07:36 +0000972 size_t bottom;
Fabian Stelzera075e792022-03-04 11:25:17 +0100973 const char *cp;
Michael J Gruberefee9552016-06-17 19:38:59 -0400974 struct strbuf gpg_status = STRBUF_INIT;
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700975
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400976 strvec_pushl(&gpg.args,
Jeff Kingf6d89422020-07-28 16:26:31 -0400977 use_format->program,
978 "--status-fd=2",
979 "-bsau", signing_key,
980 NULL);
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700981
Jeff King0581b542016-06-17 19:38:55 -0400982 bottom = signature->len;
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700983
984 /*
985 * When the username signingkey is bad, program could be terminated
986 * because gpg exits without reading and then write gets SIGPIPE.
987 */
988 sigchain_push(SIGPIPE, SIG_IGN);
Jeff King0581b542016-06-17 19:38:55 -0400989 ret = pipe_command(&gpg, buffer->buf, buffer->len,
Michael J Gruberefee9552016-06-17 19:38:59 -0400990 signature, 1024, &gpg_status, 0);
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700991 sigchain_pop(SIGPIPE);
992
Fabian Stelzera075e792022-03-04 11:25:17 +0100993 for (cp = gpg_status.buf;
994 cp && (cp = strstr(cp, "[GNUPG:] SIG_CREATED "));
995 cp++) {
996 if (cp == gpg_status.buf || cp[-1] == '\n')
997 break; /* found */
998 }
999 ret |= !cp;
Johannes Schindelinad6b3202023-02-15 05:58:34 +00001000 if (ret) {
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);
1004 return -1;
1005 }
Michael J Gruberefee9552016-06-17 19:38:59 -04001006 strbuf_release(&gpg_status);
Junio C Hamano2f47eae2011-09-07 21:19:47 -07001007
1008 /* Strip CR from the line endings, in case we are on Windows. */
Fabian Stelzer29b31572021-09-10 20:07:36 +00001009 remove_cr_after(signature, bottom);
Junio C Hamano2f47eae2011-09-07 21:19:47 -07001010
1011 return 0;
1012}
Fabian Stelzer29b31572021-09-10 20:07:36 +00001013
1014static int sign_buffer_ssh(struct strbuf *buffer, struct strbuf *signature,
1015 const char *signing_key)
1016{
1017 struct child_process signer = CHILD_PROCESS_INIT;
1018 int ret = -1;
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;
Fabian Stelzer350a2512021-11-19 16:07:06 +01001024 const char *literal_key = NULL;
Adam Szkodadce7b312023-01-25 12:40:50 +00001025 int literal_ssh_key = 0;
Fabian Stelzer29b31572021-09-10 20:07:36 +00001026
1027 if (!signing_key || signing_key[0] == '\0')
1028 return error(
Jiang Xinb4eda052022-06-17 18:03:09 +08001029 _("user.signingKey needs to be set for ssh signing"));
Fabian Stelzer29b31572021-09-10 20:07:36 +00001030
Fabian Stelzer350a2512021-11-19 16:07:06 +01001031 if (is_literal_ssh_key(signing_key, &literal_key)) {
Fabian Stelzer29b31572021-09-10 20:07:36 +00001032 /* A literal ssh key */
Adam Szkodadce7b312023-01-25 12:40:50 +00001033 literal_ssh_key = 1;
Fabian Stelzer29b31572021-09-10 20:07:36 +00001034 key_file = mks_tempfile_t(".git_signing_key_tmpXXXXXX");
1035 if (!key_file)
1036 return error_errno(
1037 _("could not create temporary file"));
Fabian Stelzer350a2512021-11-19 16:07:06 +01001038 keylen = strlen(literal_key);
1039 if (write_in_full(key_file->fd, literal_key, keylen) < 0 ||
Fabian Stelzer29b31572021-09-10 20:07:36 +00001040 close_tempfile_gently(key_file) < 0) {
1041 error_errno(_("failed writing ssh signing key to '%s'"),
1042 key_file->filename.buf);
1043 goto out;
1044 }
1045 ssh_signing_key_file = strbuf_detach(&key_file->filename, NULL);
1046 } else {
1047 /* We assume a file */
Elijah Newrenf7e552d2023-03-21 06:25:59 +00001048 ssh_signing_key_file = interpolate_path(signing_key, 1);
Fabian Stelzer29b31572021-09-10 20:07:36 +00001049 }
1050
1051 buffer_file = mks_tempfile_t(".git_signing_buffer_tmpXXXXXX");
1052 if (!buffer_file) {
1053 error_errno(_("could not create temporary file"));
1054 goto out;
1055 }
1056
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);
1061 goto out;
1062 }
1063
1064 strvec_pushl(&signer.args, use_format->program,
1065 "-Y", "sign",
1066 "-n", "git",
1067 "-f", ssh_signing_key_file,
Fabian Stelzer29b31572021-09-10 20:07:36 +00001068 NULL);
Adam Szkodadce7b312023-01-25 12:40:50 +00001069 if (literal_ssh_key)
1070 strvec_push(&signer.args, "-U");
1071 strvec_push(&signer.args, buffer_file->filename.buf);
Fabian Stelzer29b31572021-09-10 20:07:36 +00001072
1073 sigchain_push(SIGPIPE, SIG_IGN);
1074 ret = pipe_command(&signer, NULL, 0, NULL, 0, &signer_stderr, 0);
1075 sigchain_pop(SIGPIPE);
1076
1077 if (ret) {
1078 if (strstr(signer_stderr.buf, "usage:"))
1079 error(_("ssh-keygen -Y sign is needed for ssh signing (available in openssh version 8.2p1+)"));
1080
Junio C Hamano69310492024-02-07 21:29:00 -08001081 ret = error("%s", signer_stderr.buf);
Fabian Stelzer29b31572021-09-10 20:07:36 +00001082 goto out;
1083 }
1084
1085 bottom = signature->len;
1086
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) {
Phillip Wood36fb0d02022-10-04 10:01:34 +00001090 ret = error_errno(
Fabian Stelzer29b31572021-09-10 20:07:36 +00001091 _("failed reading ssh signing data buffer from '%s'"),
1092 ssh_signature_filename.buf);
Phillip Wood36fb0d02022-10-04 10:01:34 +00001093 goto out;
Fabian Stelzer29b31572021-09-10 20:07:36 +00001094 }
Fabian Stelzer29b31572021-09-10 20:07:36 +00001095 /* Strip CR from the line endings, in case we are on Windows. */
1096 remove_cr_after(signature, bottom);
1097
1098out:
1099 if (key_file)
1100 delete_tempfile(&key_file);
1101 if (buffer_file)
1102 delete_tempfile(&buffer_file);
Phillip Wood36fb0d02022-10-04 10:01:34 +00001103 if (ssh_signature_filename.len)
1104 unlink_or_warn(ssh_signature_filename.buf);
Fabian Stelzer29b31572021-09-10 20:07:36 +00001105 strbuf_release(&signer_stderr);
1106 strbuf_release(&ssh_signature_filename);
1107 FREE_AND_NULL(ssh_signing_key_file);
1108 return ret;
1109}