blob: 280f1fa1a58233f032cce7f2fe8bf4317f1e0ab8 [file] [log] [blame]
Junio C Hamano2f47eae2011-09-07 21:19:47 -07001#include "cache.h"
brian m. carlson9b27b492021-02-11 02:08:06 +00002#include "commit.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07003#include "config.h"
Junio C Hamano2f47eae2011-09-07 21:19:47 -07004#include "run-command.h"
5#include "strbuf.h"
Fabian Stelzerfacca532021-09-10 20:07:39 +00006#include "dir.h"
Junio C Hamano2f47eae2011-09-07 21:19:47 -07007#include "gpg-interface.h"
8#include "sigchain.h"
Jeff King43223532016-06-17 19:38:43 -04009#include "tempfile.h"
Fabian Stelzerfd9e2262021-09-10 20:07:37 +000010#include "alias.h"
Junio C Hamano2f47eae2011-09-07 21:19:47 -070011
12static char *configured_signing_key;
Fabian Stelzerfacca532021-09-10 20:07:39 +000013static const char *ssh_default_key_command, *ssh_allowed_signers, *ssh_revocation_file;
Hans Jerry Illikainen54887b42019-12-27 13:55:57 +000014static enum signature_trust_level configured_min_trust_level = TRUST_UNDEFINED;
15
Henning Schild58af57e2018-07-17 14:50:09 +020016struct gpg_format {
17 const char *name;
18 const char *program;
19 const char **verify_args;
20 const char **sigs;
Fabian Stelzerb5726a52021-09-10 20:07:34 +000021 int (*verify_signed_buffer)(struct signature_check *sigc,
Fabian Stelzer02769432021-12-09 09:52:43 +010022 struct gpg_format *fmt,
23 const char *signature,
Fabian Stelzerb5726a52021-09-10 20:07:34 +000024 size_t signature_size);
25 int (*sign_buffer)(struct strbuf *buffer, struct strbuf *signature,
26 const char *signing_key);
Fabian Stelzerfd9e2262021-09-10 20:07:37 +000027 const char *(*get_default_key)(void);
Fabian Stelzer4838f622021-09-10 20:07:38 +000028 const char *(*get_key_id)(void);
Henning Schild58af57e2018-07-17 14:50:09 +020029};
Junio C Hamano2f47eae2011-09-07 21:19:47 -070030
Henning Schild58af57e2018-07-17 14:50:09 +020031static const char *openpgp_verify_args[] = {
32 "--keyid-format=long",
33 NULL
34};
35static const char *openpgp_sigs[] = {
36 "-----BEGIN PGP SIGNATURE-----",
37 "-----BEGIN PGP MESSAGE-----",
38 NULL
39};
40
Henning Schild1e7adb92018-07-17 14:50:12 +020041static const char *x509_verify_args[] = {
42 NULL
43};
44static const char *x509_sigs[] = {
45 "-----BEGIN SIGNED MESSAGE-----",
46 NULL
47};
48
Fabian Stelzer29b31572021-09-10 20:07:36 +000049static const char *ssh_verify_args[] = { NULL };
50static const char *ssh_sigs[] = {
51 "-----BEGIN SSH SIGNATURE-----",
52 NULL
53};
54
Fabian Stelzerb5726a52021-09-10 20:07:34 +000055static int verify_gpg_signed_buffer(struct signature_check *sigc,
Fabian Stelzer02769432021-12-09 09:52:43 +010056 struct gpg_format *fmt,
57 const char *signature,
Fabian Stelzerb5726a52021-09-10 20:07:34 +000058 size_t signature_size);
Fabian Stelzerfacca532021-09-10 20:07:39 +000059static int verify_ssh_signed_buffer(struct signature_check *sigc,
Fabian Stelzer02769432021-12-09 09:52:43 +010060 struct gpg_format *fmt,
61 const char *signature,
Fabian Stelzerfacca532021-09-10 20:07:39 +000062 size_t signature_size);
Fabian Stelzerb5726a52021-09-10 20:07:34 +000063static int sign_buffer_gpg(struct strbuf *buffer, struct strbuf *signature,
64 const char *signing_key);
Fabian Stelzer29b31572021-09-10 20:07:36 +000065static int sign_buffer_ssh(struct strbuf *buffer, struct strbuf *signature,
66 const char *signing_key);
Fabian Stelzerb5726a52021-09-10 20:07:34 +000067
Fabian Stelzerfd9e2262021-09-10 20:07:37 +000068static const char *get_default_ssh_signing_key(void);
69
Fabian Stelzer4838f622021-09-10 20:07:38 +000070static const char *get_ssh_key_id(void);
71
Henning Schild58af57e2018-07-17 14:50:09 +020072static struct gpg_format gpg_format[] = {
Fabian Stelzerb5726a52021-09-10 20:07:34 +000073 {
74 .name = "openpgp",
75 .program = "gpg",
76 .verify_args = openpgp_verify_args,
77 .sigs = openpgp_sigs,
78 .verify_signed_buffer = verify_gpg_signed_buffer,
79 .sign_buffer = sign_buffer_gpg,
Fabian Stelzerfd9e2262021-09-10 20:07:37 +000080 .get_default_key = NULL,
Fabian Stelzer4838f622021-09-10 20:07:38 +000081 .get_key_id = NULL,
Henning Schild58af57e2018-07-17 14:50:09 +020082 },
Fabian Stelzerb5726a52021-09-10 20:07:34 +000083 {
84 .name = "x509",
85 .program = "gpgsm",
86 .verify_args = x509_verify_args,
87 .sigs = x509_sigs,
88 .verify_signed_buffer = verify_gpg_signed_buffer,
89 .sign_buffer = sign_buffer_gpg,
Fabian Stelzerfd9e2262021-09-10 20:07:37 +000090 .get_default_key = NULL,
Fabian Stelzer4838f622021-09-10 20:07:38 +000091 .get_key_id = NULL,
Henning Schild1e7adb92018-07-17 14:50:12 +020092 },
Fabian Stelzer29b31572021-09-10 20:07:36 +000093 {
94 .name = "ssh",
95 .program = "ssh-keygen",
96 .verify_args = ssh_verify_args,
97 .sigs = ssh_sigs,
Fabian Stelzerfacca532021-09-10 20:07:39 +000098 .verify_signed_buffer = verify_ssh_signed_buffer,
Fabian Stelzerfd9e2262021-09-10 20:07:37 +000099 .sign_buffer = sign_buffer_ssh,
100 .get_default_key = get_default_ssh_signing_key,
Fabian Stelzer4838f622021-09-10 20:07:38 +0000101 .get_key_id = get_ssh_key_id,
Fabian Stelzer29b31572021-09-10 20:07:36 +0000102 },
Henning Schild58af57e2018-07-17 14:50:09 +0200103};
104
105static struct gpg_format *use_format = &gpg_format[0];
106
107static struct gpg_format *get_format_by_name(const char *str)
108{
109 int i;
110
111 for (i = 0; i < ARRAY_SIZE(gpg_format); i++)
112 if (!strcmp(gpg_format[i].name, str))
113 return gpg_format + i;
114 return NULL;
115}
116
117static struct gpg_format *get_format_by_sig(const char *sig)
118{
119 int i, j;
120
121 for (i = 0; i < ARRAY_SIZE(gpg_format); i++)
122 for (j = 0; gpg_format[i].sigs[j]; j++)
123 if (starts_with(sig, gpg_format[i].sigs[j]))
124 return gpg_format + i;
125 return NULL;
126}
Junio C Hamanod7c67662014-08-19 13:18:07 -0700127
Michael J Gruber01e57b52014-06-23 09:05:47 +0200128void signature_check_clear(struct signature_check *sigc)
129{
Ævar Arnfjörð Bjarmason88ce3ef2017-06-15 23:15:49 +0000130 FREE_AND_NULL(sigc->payload);
Fabian Stelzerb5726a52021-09-10 20:07:34 +0000131 FREE_AND_NULL(sigc->output);
Ævar Arnfjörð Bjarmason88ce3ef2017-06-15 23:15:49 +0000132 FREE_AND_NULL(sigc->gpg_status);
133 FREE_AND_NULL(sigc->signer);
134 FREE_AND_NULL(sigc->key);
MichaÅ‚ GĂ³rny3daaaab2018-10-22 18:38:20 +0200135 FREE_AND_NULL(sigc->fingerprint);
MichaÅ‚ GĂ³rny4de93942018-10-22 18:38:21 +0200136 FREE_AND_NULL(sigc->primary_key_fingerprint);
Michael J Gruber01e57b52014-06-23 09:05:47 +0200137}
138
MichaÅ‚ GĂ³rnyda6cf1b2018-10-20 21:30:20 +0200139/* An exclusive status -- only one of them can appear in output */
140#define GPG_STATUS_EXCLUSIVE (1<<0)
MichaÅ‚ GĂ³rny0b11a842018-10-22 18:38:19 +0200141/* The status includes key identifier */
142#define GPG_STATUS_KEYID (1<<1)
143/* The status includes user identifier */
144#define GPG_STATUS_UID (1<<2)
MichaÅ‚ GĂ³rny3daaaab2018-10-22 18:38:20 +0200145/* The status includes key fingerprints */
146#define GPG_STATUS_FINGERPRINT (1<<3)
Hans Jerry Illikainen54887b42019-12-27 13:55:57 +0000147/* The status includes trust level */
148#define GPG_STATUS_TRUST_LEVEL (1<<4)
MichaÅ‚ GĂ³rny0b11a842018-10-22 18:38:19 +0200149
150/* Short-hand for standard exclusive *SIG status with keyid & UID */
151#define GPG_STATUS_STDSIG (GPG_STATUS_EXCLUSIVE|GPG_STATUS_KEYID|GPG_STATUS_UID)
MichaÅ‚ GĂ³rnyda6cf1b2018-10-20 21:30:20 +0200152
Junio C Hamanoa50e7ca2014-08-14 15:31:13 -0700153static struct {
154 char result;
155 const char *check;
MichaÅ‚ GĂ³rnyda6cf1b2018-10-20 21:30:20 +0200156 unsigned int flags;
Junio C Hamanoa50e7ca2014-08-14 15:31:13 -0700157} sigcheck_gpg_status[] = {
MichaÅ‚ GĂ³rny0b11a842018-10-22 18:38:19 +0200158 { 'G', "GOODSIG ", GPG_STATUS_STDSIG },
159 { 'B', "BADSIG ", GPG_STATUS_STDSIG },
MichaÅ‚ GĂ³rny0b11a842018-10-22 18:38:19 +0200160 { 'E', "ERRSIG ", GPG_STATUS_EXCLUSIVE|GPG_STATUS_KEYID },
161 { 'X', "EXPSIG ", GPG_STATUS_STDSIG },
162 { 'Y', "EXPKEYSIG ", GPG_STATUS_STDSIG },
163 { 'R', "REVKEYSIG ", GPG_STATUS_STDSIG },
MichaÅ‚ GĂ³rny3daaaab2018-10-22 18:38:20 +0200164 { 0, "VALIDSIG ", GPG_STATUS_FINGERPRINT },
Hans Jerry Illikainen54887b42019-12-27 13:55:57 +0000165 { 0, "TRUST_", GPG_STATUS_TRUST_LEVEL },
166};
167
168static struct {
169 const char *key;
170 enum signature_trust_level value;
171} sigcheck_gpg_trust_level[] = {
172 { "UNDEFINED", TRUST_UNDEFINED },
173 { "NEVER", TRUST_NEVER },
174 { "MARGINAL", TRUST_MARGINAL },
175 { "FULLY", TRUST_FULLY },
176 { "ULTIMATE", TRUST_ULTIMATE },
Junio C Hamanoa50e7ca2014-08-14 15:31:13 -0700177};
178
Hans Jerry Illikainen392b8622019-11-21 23:43:35 +0000179static void replace_cstring(char **field, const char *line, const char *next)
180{
181 free(*field);
182
183 if (line && next)
184 *field = xmemdupz(line, next - line);
185 else
186 *field = NULL;
187}
188
Hans Jerry Illikainen54887b42019-12-27 13:55:57 +0000189static int parse_gpg_trust_level(const char *level,
190 enum signature_trust_level *res)
191{
192 size_t i;
193
194 for (i = 0; i < ARRAY_SIZE(sigcheck_gpg_trust_level); i++) {
195 if (!strcmp(sigcheck_gpg_trust_level[i].key, level)) {
196 *res = sigcheck_gpg_trust_level[i].value;
197 return 0;
198 }
199 }
200 return 1;
201}
202
Henning Schildfbd0f162018-07-11 10:38:25 +0200203static void parse_gpg_output(struct signature_check *sigc)
Junio C Hamanoa50e7ca2014-08-14 15:31:13 -0700204{
205 const char *buf = sigc->gpg_status;
MichaÅ‚ GĂ³rnyda6cf1b2018-10-20 21:30:20 +0200206 const char *line, *next;
MichaÅ‚ GĂ³rny4de93942018-10-22 18:38:21 +0200207 int i, j;
MichaÅ‚ GĂ³rnyda6cf1b2018-10-20 21:30:20 +0200208 int seen_exclusive_status = 0;
Junio C Hamanoa50e7ca2014-08-14 15:31:13 -0700209
MichaÅ‚ GĂ³rnyda6cf1b2018-10-20 21:30:20 +0200210 /* Iterate over all lines */
211 for (line = buf; *line; line = strchrnul(line+1, '\n')) {
212 while (*line == '\n')
213 line++;
Steven Roberts64c45dc2019-07-16 11:47:37 -0700214 if (!*line)
215 break;
216
MichaÅ‚ GĂ³rnyda6cf1b2018-10-20 21:30:20 +0200217 /* Skip lines that don't start with GNUPG status */
218 if (!skip_prefix(line, "[GNUPG:] ", &line))
219 continue;
Junio C Hamanoa50e7ca2014-08-14 15:31:13 -0700220
MichaÅ‚ GĂ³rnyda6cf1b2018-10-20 21:30:20 +0200221 /* Iterate over all search strings */
222 for (i = 0; i < ARRAY_SIZE(sigcheck_gpg_status); i++) {
223 if (skip_prefix(line, sigcheck_gpg_status[i].check, &line)) {
Hans Jerry Illikainen54887b42019-12-27 13:55:57 +0000224 /*
225 * GOODSIG, BADSIG etc. can occur only once for
226 * each signature. Therefore, if we had more
227 * than one then we're dealing with multiple
228 * signatures. We don't support them
229 * currently, and they're rather hard to
230 * create, so something is likely fishy and we
231 * should reject them altogether.
232 */
MichaÅ‚ GĂ³rnyda6cf1b2018-10-20 21:30:20 +0200233 if (sigcheck_gpg_status[i].flags & GPG_STATUS_EXCLUSIVE) {
Junio C Hamano02561892018-11-03 00:53:57 +0900234 if (seen_exclusive_status++)
Hans Jerry Illikainen54887b42019-12-27 13:55:57 +0000235 goto error;
MichaÅ‚ GĂ³rnyda6cf1b2018-10-20 21:30:20 +0200236 }
237
MichaÅ‚ GĂ³rny3daaaab2018-10-22 18:38:20 +0200238 if (sigcheck_gpg_status[i].result)
239 sigc->result = sigcheck_gpg_status[i].result;
MichaÅ‚ GĂ³rny0b11a842018-10-22 18:38:19 +0200240 /* Do we have key information? */
241 if (sigcheck_gpg_status[i].flags & GPG_STATUS_KEYID) {
MichaÅ‚ GĂ³rnyda6cf1b2018-10-20 21:30:20 +0200242 next = strchrnul(line, ' ');
Hans Jerry Illikainen392b8622019-11-21 23:43:35 +0000243 replace_cstring(&sigc->key, line, next);
MichaÅ‚ GĂ³rny0b11a842018-10-22 18:38:19 +0200244 /* Do we have signer information? */
245 if (*next && (sigcheck_gpg_status[i].flags & GPG_STATUS_UID)) {
MichaÅ‚ GĂ³rnyda6cf1b2018-10-20 21:30:20 +0200246 line = next + 1;
247 next = strchrnul(line, '\n');
Hans Jerry Illikainen392b8622019-11-21 23:43:35 +0000248 replace_cstring(&sigc->signer, line, next);
MichaÅ‚ GĂ³rnyda6cf1b2018-10-20 21:30:20 +0200249 }
250 }
Hans Jerry Illikainen54887b42019-12-27 13:55:57 +0000251
252 /* Do we have trust level? */
253 if (sigcheck_gpg_status[i].flags & GPG_STATUS_TRUST_LEVEL) {
254 /*
255 * GPG v1 and v2 differs in how the
256 * TRUST_ lines are written. Some
257 * trust lines contain no additional
258 * space-separated information for v1.
259 */
260 size_t trust_size = strcspn(line, " \n");
261 char *trust = xmemdupz(line, trust_size);
262
263 if (parse_gpg_trust_level(trust, &sigc->trust_level)) {
264 free(trust);
265 goto error;
266 }
267 free(trust);
268 }
269
MichaÅ‚ GĂ³rny3daaaab2018-10-22 18:38:20 +0200270 /* Do we have fingerprint? */
271 if (sigcheck_gpg_status[i].flags & GPG_STATUS_FINGERPRINT) {
Hans Jerry Illikainen67a6ea62019-11-22 20:23:12 +0000272 const char *limit;
273 char **field;
274
MichaÅ‚ GĂ³rny3daaaab2018-10-22 18:38:20 +0200275 next = strchrnul(line, ' ');
Hans Jerry Illikainen392b8622019-11-21 23:43:35 +0000276 replace_cstring(&sigc->fingerprint, line, next);
MichaÅ‚ GĂ³rny4de93942018-10-22 18:38:21 +0200277
Hans Jerry Illikainen67a6ea62019-11-22 20:23:12 +0000278 /*
279 * Skip interim fields. The search is
280 * limited to the same line since only
281 * OpenPGP signatures has a field with
282 * the primary fingerprint.
283 */
284 limit = strchrnul(line, '\n');
MichaÅ‚ GĂ³rny4de93942018-10-22 18:38:21 +0200285 for (j = 9; j > 0; j--) {
Hans Jerry Illikainen67a6ea62019-11-22 20:23:12 +0000286 if (!*next || limit <= next)
MichaÅ‚ GĂ³rny4de93942018-10-22 18:38:21 +0200287 break;
288 line = next + 1;
289 next = strchrnul(line, ' ');
290 }
291
Hans Jerry Illikainen67a6ea62019-11-22 20:23:12 +0000292 field = &sigc->primary_key_fingerprint;
293 if (!j) {
294 next = strchrnul(line, '\n');
295 replace_cstring(field, line, next);
296 } else {
297 replace_cstring(field, NULL, NULL);
298 }
MichaÅ‚ GĂ³rny3daaaab2018-10-22 18:38:20 +0200299 }
MichaÅ‚ GĂ³rnyda6cf1b2018-10-20 21:30:20 +0200300
301 break;
Michael J Gruber661a1802016-10-12 15:04:15 +0200302 }
Junio C Hamanoa50e7ca2014-08-14 15:31:13 -0700303 }
304 }
MichaÅ‚ GĂ³rnyda6cf1b2018-10-20 21:30:20 +0200305 return;
306
Hans Jerry Illikainen54887b42019-12-27 13:55:57 +0000307error:
MichaÅ‚ GĂ³rnyda6cf1b2018-10-20 21:30:20 +0200308 sigc->result = 'E';
309 /* Clear partial data to avoid confusion */
MichaÅ‚ GĂ³rny4de93942018-10-22 18:38:21 +0200310 FREE_AND_NULL(sigc->primary_key_fingerprint);
MichaÅ‚ GĂ³rny3daaaab2018-10-22 18:38:20 +0200311 FREE_AND_NULL(sigc->fingerprint);
MichaÅ‚ GĂ³rnyda6cf1b2018-10-20 21:30:20 +0200312 FREE_AND_NULL(sigc->signer);
313 FREE_AND_NULL(sigc->key);
Junio C Hamanoa50e7ca2014-08-14 15:31:13 -0700314}
315
Fabian Stelzerb5726a52021-09-10 20:07:34 +0000316static int verify_gpg_signed_buffer(struct signature_check *sigc,
Fabian Stelzer02769432021-12-09 09:52:43 +0100317 struct gpg_format *fmt,
318 const char *signature,
Fabian Stelzerb5726a52021-09-10 20:07:34 +0000319 size_t signature_size)
Hans Jerry Illikainen67948982020-03-04 11:48:04 +0000320{
321 struct child_process gpg = CHILD_PROCESS_INIT;
Hans Jerry Illikainen67948982020-03-04 11:48:04 +0000322 struct tempfile *temp;
323 int ret;
Fabian Stelzerb5726a52021-09-10 20:07:34 +0000324 struct strbuf gpg_stdout = STRBUF_INIT;
325 struct strbuf gpg_stderr = STRBUF_INIT;
Hans Jerry Illikainen67948982020-03-04 11:48:04 +0000326
327 temp = mks_tempfile_t(".git_vtag_tmpXXXXXX");
328 if (!temp)
329 return error_errno(_("could not create temporary file"));
330 if (write_in_full(temp->fd, signature, signature_size) < 0 ||
331 close_tempfile_gently(temp) < 0) {
332 error_errno(_("failed writing detached signature to '%s'"),
333 temp->filename.buf);
334 delete_tempfile(&temp);
335 return -1;
336 }
337
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400338 strvec_push(&gpg.args, fmt->program);
339 strvec_pushv(&gpg.args, fmt->verify_args);
340 strvec_pushl(&gpg.args,
Jeff Kingf6d89422020-07-28 16:26:31 -0400341 "--status-fd=1",
342 "--verify", temp->filename.buf, "-",
343 NULL);
Hans Jerry Illikainen67948982020-03-04 11:48:04 +0000344
Hans Jerry Illikainen67948982020-03-04 11:48:04 +0000345 sigchain_push(SIGPIPE, SIG_IGN);
Fabian Stelzer02769432021-12-09 09:52:43 +0100346 ret = pipe_command(&gpg, sigc->payload, sigc->payload_len, &gpg_stdout, 0,
Fabian Stelzerb5726a52021-09-10 20:07:34 +0000347 &gpg_stderr, 0);
Hans Jerry Illikainen67948982020-03-04 11:48:04 +0000348 sigchain_pop(SIGPIPE);
349
350 delete_tempfile(&temp);
351
Fabian Stelzerb5726a52021-09-10 20:07:34 +0000352 ret |= !strstr(gpg_stdout.buf, "\n[GNUPG:] GOODSIG ");
Fabian Stelzerb5726a52021-09-10 20:07:34 +0000353 sigc->output = strbuf_detach(&gpg_stderr, NULL);
354 sigc->gpg_status = strbuf_detach(&gpg_stdout, NULL);
355
356 parse_gpg_output(sigc);
357
358 strbuf_release(&gpg_stdout);
359 strbuf_release(&gpg_stderr);
Hans Jerry Illikainen67948982020-03-04 11:48:04 +0000360
361 return ret;
362}
363
Fabian Stelzerfacca532021-09-10 20:07:39 +0000364static void parse_ssh_output(struct signature_check *sigc)
365{
366 const char *line, *principal, *search;
Jeff King78d468f2021-10-18 13:15:00 -0400367 char *to_free;
Fabian Stelzerfacca532021-09-10 20:07:39 +0000368 char *key = NULL;
369
370 /*
371 * ssh-keygen output should be:
372 * Good "git" signature for PRINCIPAL with RSA key SHA256:FINGERPRINT
373 *
374 * or for valid but unknown keys:
375 * Good "git" signature with RSA key SHA256:FINGERPRINT
376 *
377 * Note that "PRINCIPAL" can contain whitespace, "RSA" and
378 * "SHA256" part could be a different token that names of
379 * the algorithms used, and "FINGERPRINT" is a hexadecimal
380 * string. By finding the last occurence of " with ", we can
381 * reliably parse out the PRINCIPAL.
382 */
383 sigc->result = 'B';
384 sigc->trust_level = TRUST_NEVER;
385
Jeff King78d468f2021-10-18 13:15:00 -0400386 line = to_free = xmemdupz(sigc->output, strcspn(sigc->output, "\n"));
Fabian Stelzerfacca532021-09-10 20:07:39 +0000387
388 if (skip_prefix(line, "Good \"git\" signature for ", &line)) {
Fabian Stelzerfacca532021-09-10 20:07:39 +0000389 /* Search for the last "with" to get the full principal */
390 principal = line;
391 do {
392 search = strstr(line, " with ");
393 if (search)
394 line = search + 1;
395 } while (search != NULL);
René Scharfe18b18502021-10-30 19:04:56 +0200396 if (line == principal)
397 goto cleanup;
398
399 /* Valid signature and known principal */
400 sigc->result = 'G';
401 sigc->trust_level = TRUST_FULLY;
Fabian Stelzerfacca532021-09-10 20:07:39 +0000402 sigc->signer = xmemdupz(principal, line - principal - 1);
403 } else if (skip_prefix(line, "Good \"git\" signature with ", &line)) {
404 /* Valid signature, but key unknown */
405 sigc->result = 'G';
406 sigc->trust_level = TRUST_UNDEFINED;
407 } else {
Jeff King78d468f2021-10-18 13:15:00 -0400408 goto cleanup;
Fabian Stelzerfacca532021-09-10 20:07:39 +0000409 }
410
René Scharfe65db97b2021-10-30 19:07:38 +0200411 key = strstr(line, "key ");
Fabian Stelzerfacca532021-09-10 20:07:39 +0000412 if (key) {
René Scharfe65db97b2021-10-30 19:07:38 +0200413 sigc->fingerprint = xstrdup(strstr(line, "key ") + 4);
Fabian Stelzerfacca532021-09-10 20:07:39 +0000414 sigc->key = xstrdup(sigc->fingerprint);
415 } else {
416 /*
417 * Output did not match what we expected
418 * Treat the signature as bad
419 */
420 sigc->result = 'B';
421 }
Jeff King78d468f2021-10-18 13:15:00 -0400422
423cleanup:
424 free(to_free);
Fabian Stelzerfacca532021-09-10 20:07:39 +0000425}
426
427static int verify_ssh_signed_buffer(struct signature_check *sigc,
Fabian Stelzer02769432021-12-09 09:52:43 +0100428 struct gpg_format *fmt,
429 const char *signature,
Fabian Stelzerfacca532021-09-10 20:07:39 +0000430 size_t signature_size)
431{
432 struct child_process ssh_keygen = CHILD_PROCESS_INIT;
433 struct tempfile *buffer_file;
434 int ret = -1;
435 const char *line;
Fabian Stelzerfacca532021-09-10 20:07:39 +0000436 char *principal;
437 struct strbuf ssh_principals_out = STRBUF_INIT;
438 struct strbuf ssh_principals_err = STRBUF_INIT;
439 struct strbuf ssh_keygen_out = STRBUF_INIT;
440 struct strbuf ssh_keygen_err = STRBUF_INIT;
Fabian Stelzer6393c952021-12-09 09:52:45 +0100441 struct strbuf verify_time = STRBUF_INIT;
442 const struct date_mode verify_date_mode = {
443 .type = DATE_STRFTIME,
444 .strftime_fmt = "%Y%m%d%H%M%S",
445 /* SSH signing key validity has no timezone information - Use the local timezone */
446 .local = 1,
447 };
Fabian Stelzerfacca532021-09-10 20:07:39 +0000448
449 if (!ssh_allowed_signers) {
450 error(_("gpg.ssh.allowedSignersFile needs to be configured and exist for ssh signature verification"));
451 return -1;
452 }
453
454 buffer_file = mks_tempfile_t(".git_vtag_tmpXXXXXX");
455 if (!buffer_file)
456 return error_errno(_("could not create temporary file"));
457 if (write_in_full(buffer_file->fd, signature, signature_size) < 0 ||
458 close_tempfile_gently(buffer_file) < 0) {
459 error_errno(_("failed writing detached signature to '%s'"),
460 buffer_file->filename.buf);
461 delete_tempfile(&buffer_file);
462 return -1;
463 }
464
Fabian Stelzer6393c952021-12-09 09:52:45 +0100465 if (sigc->payload_timestamp)
466 strbuf_addf(&verify_time, "-Overify-time=%s",
467 show_date(sigc->payload_timestamp, 0, &verify_date_mode));
468
Fabian Stelzerfacca532021-09-10 20:07:39 +0000469 /* Find the principal from the signers */
470 strvec_pushl(&ssh_keygen.args, fmt->program,
471 "-Y", "find-principals",
472 "-f", ssh_allowed_signers,
473 "-s", buffer_file->filename.buf,
Fabian Stelzer6393c952021-12-09 09:52:45 +0100474 verify_time.buf,
Fabian Stelzerfacca532021-09-10 20:07:39 +0000475 NULL);
476 ret = pipe_command(&ssh_keygen, NULL, 0, &ssh_principals_out, 0,
477 &ssh_principals_err, 0);
478 if (ret && strstr(ssh_principals_err.buf, "usage:")) {
479 error(_("ssh-keygen -Y find-principals/verify is needed for ssh signature verification (available in openssh version 8.2p1+)"));
480 goto out;
481 }
482 if (ret || !ssh_principals_out.len) {
483 /*
484 * We did not find a matching principal in the allowedSigners
485 * Check without validation
486 */
487 child_process_init(&ssh_keygen);
488 strvec_pushl(&ssh_keygen.args, fmt->program,
489 "-Y", "check-novalidate",
490 "-n", "git",
491 "-s", buffer_file->filename.buf,
Fabian Stelzer6393c952021-12-09 09:52:45 +0100492 verify_time.buf,
Fabian Stelzerfacca532021-09-10 20:07:39 +0000493 NULL);
Fabian Stelzer02769432021-12-09 09:52:43 +0100494 pipe_command(&ssh_keygen, sigc->payload, sigc->payload_len,
Fabian Stelzerfacca532021-09-10 20:07:39 +0000495 &ssh_keygen_out, 0, &ssh_keygen_err, 0);
496
497 /*
498 * Fail on unknown keys
499 * we still call check-novalidate to display the signature info
500 */
501 ret = -1;
502 } else {
503 /* Check every principal we found (one per line) */
Fabian Stelzercaeef012022-01-07 10:07:35 +0100504 const char *next;
505 for (line = ssh_principals_out.buf;
506 *line;
507 line = next) {
508 const char *end_of_text;
Fabian Stelzerfacca532021-09-10 20:07:39 +0000509
Fabian Stelzercaeef012022-01-07 10:07:35 +0100510 next = end_of_text = strchrnul(line, '\n');
511
512 /* Did we find a LF, and did we have CR before it? */
513 if (*end_of_text &&
514 line < end_of_text &&
515 end_of_text[-1] == '\r')
516 end_of_text--;
517
518 /* Unless we hit NUL, skip over the LF we found */
519 if (*next)
520 next++;
521
522 /* Not all lines are data. Skip empty ones */
523 if (line == end_of_text)
524 continue;
525
526 /* We now know we have an non-empty line. Process it */
527 principal = xmemdupz(line, end_of_text - line);
Fabian Stelzerfacca532021-09-10 20:07:39 +0000528
529 child_process_init(&ssh_keygen);
530 strbuf_release(&ssh_keygen_out);
531 strbuf_release(&ssh_keygen_err);
532 strvec_push(&ssh_keygen.args, fmt->program);
533 /*
534 * We found principals
535 * Try with each until we find a match
536 */
537 strvec_pushl(&ssh_keygen.args, "-Y", "verify",
538 "-n", "git",
539 "-f", ssh_allowed_signers,
540 "-I", principal,
541 "-s", buffer_file->filename.buf,
Fabian Stelzer6393c952021-12-09 09:52:45 +0100542 verify_time.buf,
Fabian Stelzerfacca532021-09-10 20:07:39 +0000543 NULL);
544
545 if (ssh_revocation_file) {
546 if (file_exists(ssh_revocation_file)) {
547 strvec_pushl(&ssh_keygen.args, "-r",
548 ssh_revocation_file, NULL);
549 } else {
550 warning(_("ssh signing revocation file configured but not found: %s"),
551 ssh_revocation_file);
552 }
553 }
554
555 sigchain_push(SIGPIPE, SIG_IGN);
Fabian Stelzer02769432021-12-09 09:52:43 +0100556 ret = pipe_command(&ssh_keygen, sigc->payload, sigc->payload_len,
Fabian Stelzerfacca532021-09-10 20:07:39 +0000557 &ssh_keygen_out, 0, &ssh_keygen_err, 0);
558 sigchain_pop(SIGPIPE);
559
560 FREE_AND_NULL(principal);
561
562 if (!ret)
563 ret = !starts_with(ssh_keygen_out.buf, "Good");
564
565 if (!ret)
566 break;
567 }
568 }
569
Fabian Stelzerfacca532021-09-10 20:07:39 +0000570 strbuf_stripspace(&ssh_keygen_out, 0);
571 strbuf_stripspace(&ssh_keygen_err, 0);
572 /* Add stderr outputs to show the user actual ssh-keygen errors */
573 strbuf_add(&ssh_keygen_out, ssh_principals_err.buf, ssh_principals_err.len);
574 strbuf_add(&ssh_keygen_out, ssh_keygen_err.buf, ssh_keygen_err.len);
575 sigc->output = strbuf_detach(&ssh_keygen_out, NULL);
576 sigc->gpg_status = xstrdup(sigc->output);
577
578 parse_ssh_output(sigc);
579
580out:
581 if (buffer_file)
582 delete_tempfile(&buffer_file);
583 strbuf_release(&ssh_principals_out);
584 strbuf_release(&ssh_principals_err);
585 strbuf_release(&ssh_keygen_out);
586 strbuf_release(&ssh_keygen_err);
Fabian Stelzer6393c952021-12-09 09:52:45 +0100587 strbuf_release(&verify_time);
Fabian Stelzerfacca532021-09-10 20:07:39 +0000588
589 return ret;
590}
591
Fabian Stelzer6393c952021-12-09 09:52:45 +0100592static int parse_payload_metadata(struct signature_check *sigc)
593{
594 const char *ident_line = NULL;
595 size_t ident_len;
596 struct ident_split ident;
597 const char *signer_header;
598
599 switch (sigc->payload_type) {
600 case SIGNATURE_PAYLOAD_COMMIT:
601 signer_header = "committer";
602 break;
603 case SIGNATURE_PAYLOAD_TAG:
604 signer_header = "tagger";
605 break;
606 case SIGNATURE_PAYLOAD_UNDEFINED:
607 case SIGNATURE_PAYLOAD_PUSH_CERT:
608 /* Ignore payloads we don't want to parse */
609 return 0;
610 default:
611 BUG("invalid value for sigc->payload_type");
612 }
613
614 ident_line = find_commit_header(sigc->payload, signer_header, &ident_len);
615 if (!ident_line || !ident_len)
616 return 1;
617
618 if (split_ident_line(&ident, ident_line, ident_len))
619 return 1;
620
621 if (!sigc->payload_timestamp && ident.date_begin && ident.date_end)
622 sigc->payload_timestamp = parse_timestamp(ident.date_begin, NULL, 10);
623
624 return 0;
625}
626
Fabian Stelzer02769432021-12-09 09:52:43 +0100627int check_signature(struct signature_check *sigc,
628 const char *signature, size_t slen)
brian m. carlsona4cc18f2015-06-21 23:14:38 +0000629{
Fabian Stelzerb5726a52021-09-10 20:07:34 +0000630 struct gpg_format *fmt;
brian m. carlsona4cc18f2015-06-21 23:14:38 +0000631 int status;
632
633 sigc->result = 'N';
Hans Jerry Illikainen54887b42019-12-27 13:55:57 +0000634 sigc->trust_level = -1;
brian m. carlsona4cc18f2015-06-21 23:14:38 +0000635
Fabian Stelzerb5726a52021-09-10 20:07:34 +0000636 fmt = get_format_by_sig(signature);
637 if (!fmt)
638 die(_("bad/incompatible signature '%s'"), signature);
639
Fabian Stelzer6393c952021-12-09 09:52:45 +0100640 if (parse_payload_metadata(sigc))
641 return 1;
642
Fabian Stelzer02769432021-12-09 09:52:43 +0100643 status = fmt->verify_signed_buffer(sigc, fmt, signature, slen);
Fabian Stelzerb5726a52021-09-10 20:07:34 +0000644
645 if (status && !sigc->output)
646 return !!status;
647
Hans Jerry Illikainen54887b42019-12-27 13:55:57 +0000648 status |= sigc->result != 'G';
649 status |= sigc->trust_level < configured_min_trust_level;
brian m. carlsona4cc18f2015-06-21 23:14:38 +0000650
Junio C Hamano4e5dc9c2018-08-09 11:40:27 -0700651 return !!status;
brian m. carlsona4cc18f2015-06-21 23:14:38 +0000652}
653
brian m. carlsonca194d52015-06-21 23:14:41 +0000654void print_signature_buffer(const struct signature_check *sigc, unsigned flags)
655{
Fabian Stelzerb5726a52021-09-10 20:07:34 +0000656 const char *output = flags & GPG_VERIFY_RAW ? sigc->gpg_status :
657 sigc->output;
brian m. carlsonaeff29d2015-06-21 23:14:42 +0000658
brian m. carlsonca194d52015-06-21 23:14:41 +0000659 if (flags & GPG_VERIFY_VERBOSE && sigc->payload)
Fabian Stelzer02769432021-12-09 09:52:43 +0100660 fwrite(sigc->payload, 1, sigc->payload_len, stdout);
brian m. carlsonca194d52015-06-21 23:14:41 +0000661
brian m. carlsonaeff29d2015-06-21 23:14:42 +0000662 if (output)
663 fputs(output, stderr);
brian m. carlsonca194d52015-06-21 23:14:41 +0000664}
665
brian m. carlson482c1192021-02-11 02:08:03 +0000666size_t parse_signed_buffer(const char *buf, size_t size)
Junio C Hamanod7c67662014-08-19 13:18:07 -0700667{
Junio C Hamanod7c67662014-08-19 13:18:07 -0700668 size_t len = 0;
Jeff King8b44b2b2018-04-13 15:18:35 -0600669 size_t match = size;
670 while (len < size) {
671 const char *eol;
672
Henning Schild58af57e2018-07-17 14:50:09 +0200673 if (get_format_by_sig(buf + len))
Jeff King8b44b2b2018-04-13 15:18:35 -0600674 match = len;
675
676 eol = memchr(buf + len, '\n', size - len);
Junio C Hamanod7c67662014-08-19 13:18:07 -0700677 len += eol ? eol - (buf + len) + 1 : size - len;
678 }
Jeff King8b44b2b2018-04-13 15:18:35 -0600679 return match;
Junio C Hamanod7c67662014-08-19 13:18:07 -0700680}
681
brian m. carlson482c1192021-02-11 02:08:03 +0000682int parse_signature(const char *buf, size_t size, struct strbuf *payload, struct strbuf *signature)
683{
684 size_t match = parse_signed_buffer(buf, size);
685 if (match != size) {
686 strbuf_add(payload, buf, match);
brian m. carlson9b27b492021-02-11 02:08:06 +0000687 remove_signature(payload);
brian m. carlson482c1192021-02-11 02:08:03 +0000688 strbuf_add(signature, buf + match, size - match);
689 return 1;
690 }
691 return 0;
692}
693
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700694void set_signing_key(const char *key)
695{
696 free(configured_signing_key);
697 configured_signing_key = xstrdup(key);
698}
699
700int git_gpg_config(const char *var, const char *value, void *cb)
701{
Henning Schild58af57e2018-07-17 14:50:09 +0200702 struct gpg_format *fmt = NULL;
703 char *fmtname = NULL;
Hans Jerry Illikainen54887b42019-12-27 13:55:57 +0000704 char *trust;
705 int ret;
Henning Schild58af57e2018-07-17 14:50:09 +0200706
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700707 if (!strcmp(var, "user.signingkey")) {
Jeff King1b0eeec2018-04-13 15:18:30 -0600708 if (!value)
709 return config_error_nonbool(var);
Junio C Hamano0c5e70f2011-11-29 12:29:48 -0800710 set_signing_key(value);
Jeff King1b0eeec2018-04-13 15:18:30 -0600711 return 0;
Junio C Hamano0c5e70f2011-11-29 12:29:48 -0800712 }
Jeff King1b0eeec2018-04-13 15:18:30 -0600713
Henning Schild57a8dd72018-07-17 14:50:07 +0200714 if (!strcmp(var, "gpg.format")) {
715 if (!value)
716 return config_error_nonbool(var);
Henning Schild58af57e2018-07-17 14:50:09 +0200717 fmt = get_format_by_name(value);
718 if (!fmt)
Jean-Noël Avila1a8aea82022-01-31 22:07:47 +0000719 return error(_("invalid value for '%s': '%s'"),
Henning Schild57a8dd72018-07-17 14:50:07 +0200720 var, value);
Henning Schild58af57e2018-07-17 14:50:09 +0200721 use_format = fmt;
722 return 0;
Henning Schild57a8dd72018-07-17 14:50:07 +0200723 }
724
Hans Jerry Illikainen54887b42019-12-27 13:55:57 +0000725 if (!strcmp(var, "gpg.mintrustlevel")) {
726 if (!value)
727 return config_error_nonbool(var);
728
729 trust = xstrdup_toupper(value);
730 ret = parse_gpg_trust_level(trust, &configured_min_trust_level);
731 free(trust);
732
733 if (ret)
Jean-Noël Avila1a8aea82022-01-31 22:07:47 +0000734 return error(_("invalid value for '%s': '%s'"),
735 var, value);
Hans Jerry Illikainen54887b42019-12-27 13:55:57 +0000736 return 0;
737 }
738
Fabian Stelzerfd9e2262021-09-10 20:07:37 +0000739 if (!strcmp(var, "gpg.ssh.defaultkeycommand")) {
740 if (!value)
741 return config_error_nonbool(var);
742 return git_config_string(&ssh_default_key_command, var, value);
743 }
744
Fabian Stelzerfacca532021-09-10 20:07:39 +0000745 if (!strcmp(var, "gpg.ssh.allowedsignersfile")) {
746 if (!value)
747 return config_error_nonbool(var);
748 return git_config_pathname(&ssh_allowed_signers, var, value);
749 }
750
751 if (!strcmp(var, "gpg.ssh.revocationfile")) {
752 if (!value)
753 return config_error_nonbool(var);
754 return git_config_pathname(&ssh_revocation_file, var, value);
755 }
756
Henning Schildb02f51b2018-07-17 14:50:11 +0200757 if (!strcmp(var, "gpg.program") || !strcmp(var, "gpg.openpgp.program"))
Henning Schild58af57e2018-07-17 14:50:09 +0200758 fmtname = "openpgp";
759
Henning Schild1e7adb92018-07-17 14:50:12 +0200760 if (!strcmp(var, "gpg.x509.program"))
761 fmtname = "x509";
762
Fabian Stelzer29b31572021-09-10 20:07:36 +0000763 if (!strcmp(var, "gpg.ssh.program"))
764 fmtname = "ssh";
765
Henning Schild58af57e2018-07-17 14:50:09 +0200766 if (fmtname) {
767 fmt = get_format_by_name(fmtname);
768 return git_config_string(&fmt->program, var, value);
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700769 }
Jeff King1b0eeec2018-04-13 15:18:30 -0600770
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700771 return 0;
772}
773
Fabian Stelzer350a2512021-11-19 16:07:06 +0100774/*
775 * Returns 1 if `string` contains a literal ssh key, 0 otherwise
776 * `key` will be set to the start of the actual key if a prefix is present.
777 */
778static int is_literal_ssh_key(const char *string, const char **key)
779{
780 if (skip_prefix(string, "key::", key))
781 return 1;
782 if (starts_with(string, "ssh-")) {
783 *key = string;
784 return 1;
785 }
786 return 0;
787}
788
Fabian Stelzer4838f622021-09-10 20:07:38 +0000789static char *get_ssh_key_fingerprint(const char *signing_key)
790{
791 struct child_process ssh_keygen = CHILD_PROCESS_INIT;
792 int ret = -1;
793 struct strbuf fingerprint_stdout = STRBUF_INIT;
794 struct strbuf **fingerprint;
Jeff Kingf3af71c2021-10-18 13:15:37 -0400795 char *fingerprint_ret;
Fabian Stelzer350a2512021-11-19 16:07:06 +0100796 const char *literal_key = NULL;
Fabian Stelzer4838f622021-09-10 20:07:38 +0000797
798 /*
799 * With SSH Signing this can contain a filename or a public key
800 * For textual representation we usually want a fingerprint
801 */
Fabian Stelzer350a2512021-11-19 16:07:06 +0100802 if (is_literal_ssh_key(signing_key, &literal_key)) {
Fabian Stelzer4838f622021-09-10 20:07:38 +0000803 strvec_pushl(&ssh_keygen.args, "ssh-keygen", "-lf", "-", NULL);
Fabian Stelzer350a2512021-11-19 16:07:06 +0100804 ret = pipe_command(&ssh_keygen, literal_key,
805 strlen(literal_key), &fingerprint_stdout, 0,
Fabian Stelzer4838f622021-09-10 20:07:38 +0000806 NULL, 0);
807 } else {
808 strvec_pushl(&ssh_keygen.args, "ssh-keygen", "-lf",
809 configured_signing_key, NULL);
810 ret = pipe_command(&ssh_keygen, NULL, 0, &fingerprint_stdout, 0,
811 NULL, 0);
812 }
813
814 if (!!ret)
815 die_errno(_("failed to get the ssh fingerprint for key '%s'"),
816 signing_key);
817
818 fingerprint = strbuf_split_max(&fingerprint_stdout, ' ', 3);
819 if (!fingerprint[1])
820 die_errno(_("failed to get the ssh fingerprint for key '%s'"),
821 signing_key);
822
Jeff Kingf3af71c2021-10-18 13:15:37 -0400823 fingerprint_ret = strbuf_detach(fingerprint[1], NULL);
824 strbuf_list_free(fingerprint);
825 strbuf_release(&fingerprint_stdout);
826 return fingerprint_ret;
Fabian Stelzer4838f622021-09-10 20:07:38 +0000827}
828
Fabian Stelzerfd9e2262021-09-10 20:07:37 +0000829/* Returns the first public key from an ssh-agent to use for signing */
830static const char *get_default_ssh_signing_key(void)
831{
832 struct child_process ssh_default_key = CHILD_PROCESS_INIT;
833 int ret = -1;
834 struct strbuf key_stdout = STRBUF_INIT, key_stderr = STRBUF_INIT;
835 struct strbuf **keys;
836 char *key_command = NULL;
837 const char **argv;
838 int n;
839 char *default_key = NULL;
Fabian Stelzer350a2512021-11-19 16:07:06 +0100840 const char *literal_key = NULL;
Fabian Stelzerfd9e2262021-09-10 20:07:37 +0000841
842 if (!ssh_default_key_command)
843 die(_("either user.signingkey or gpg.ssh.defaultKeyCommand needs to be configured"));
844
845 key_command = xstrdup(ssh_default_key_command);
846 n = split_cmdline(key_command, &argv);
847
848 if (n < 0)
849 die("malformed build-time gpg.ssh.defaultKeyCommand: %s",
850 split_cmdline_strerror(n));
851
852 strvec_pushv(&ssh_default_key.args, argv);
853 ret = pipe_command(&ssh_default_key, NULL, 0, &key_stdout, 0,
854 &key_stderr, 0);
855
856 if (!ret) {
857 keys = strbuf_split_max(&key_stdout, '\n', 2);
Fabian Stelzer350a2512021-11-19 16:07:06 +0100858 if (keys[0] && is_literal_ssh_key(keys[0]->buf, &literal_key)) {
859 /*
860 * We only use `is_literal_ssh_key` here to check validity
861 * The prefix will be stripped when the key is used.
862 */
Fabian Stelzerfd9e2262021-09-10 20:07:37 +0000863 default_key = strbuf_detach(keys[0], NULL);
864 } else {
Jiang Xinf7337192021-11-01 10:14:17 +0800865 warning(_("gpg.ssh.defaultKeyCommand succeeded but returned no keys: %s %s"),
Fabian Stelzerfd9e2262021-09-10 20:07:37 +0000866 key_stderr.buf, key_stdout.buf);
867 }
868
869 strbuf_list_free(keys);
870 } else {
871 warning(_("gpg.ssh.defaultKeyCommand failed: %s %s"),
872 key_stderr.buf, key_stdout.buf);
873 }
874
875 free(key_command);
876 free(argv);
877 strbuf_release(&key_stdout);
878
879 return default_key;
880}
881
Fabian Stelzer4838f622021-09-10 20:07:38 +0000882static const char *get_ssh_key_id(void) {
883 return get_ssh_key_fingerprint(get_signing_key());
884}
885
886/* Returns a textual but unique representation of the signing key */
887const char *get_signing_key_id(void)
888{
889 if (use_format->get_key_id) {
890 return use_format->get_key_id();
891 }
892
893 /* GPG/GPGSM only store a key id on this variable */
894 return get_signing_key();
895}
896
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700897const char *get_signing_key(void)
898{
899 if (configured_signing_key)
900 return configured_signing_key;
Fabian Stelzerfd9e2262021-09-10 20:07:37 +0000901 if (use_format->get_default_key) {
902 return use_format->get_default_key();
903 }
904
905 return git_committer_info(IDENT_STRICT | IDENT_NO_DATE);
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700906}
907
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700908int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *signing_key)
909{
Fabian Stelzerb5726a52021-09-10 20:07:34 +0000910 return use_format->sign_buffer(buffer, signature, signing_key);
911}
912
Fabian Stelzer29b31572021-09-10 20:07:36 +0000913/*
914 * Strip CR from the line endings, in case we are on Windows.
915 * NEEDSWORK: make it trim only CRs before LFs and rename
916 */
917static void remove_cr_after(struct strbuf *buffer, size_t offset)
918{
919 size_t i, j;
920
921 for (i = j = offset; i < buffer->len; i++) {
922 if (buffer->buf[i] != '\r') {
923 if (i != j)
924 buffer->buf[j] = buffer->buf[i];
925 j++;
926 }
927 }
928 strbuf_setlen(buffer, j);
929}
930
Fabian Stelzerb5726a52021-09-10 20:07:34 +0000931static int sign_buffer_gpg(struct strbuf *buffer, struct strbuf *signature,
932 const char *signing_key)
933{
René Scharfed3180272014-08-19 21:09:35 +0200934 struct child_process gpg = CHILD_PROCESS_INIT;
Jeff King0581b542016-06-17 19:38:55 -0400935 int ret;
Fabian Stelzer29b31572021-09-10 20:07:36 +0000936 size_t bottom;
Fabian Stelzera075e792022-03-04 11:25:17 +0100937 const char *cp;
Michael J Gruberefee9552016-06-17 19:38:59 -0400938 struct strbuf gpg_status = STRBUF_INIT;
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700939
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400940 strvec_pushl(&gpg.args,
Jeff Kingf6d89422020-07-28 16:26:31 -0400941 use_format->program,
942 "--status-fd=2",
943 "-bsau", signing_key,
944 NULL);
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700945
Jeff King0581b542016-06-17 19:38:55 -0400946 bottom = signature->len;
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700947
948 /*
949 * When the username signingkey is bad, program could be terminated
950 * because gpg exits without reading and then write gets SIGPIPE.
951 */
952 sigchain_push(SIGPIPE, SIG_IGN);
Jeff King0581b542016-06-17 19:38:55 -0400953 ret = pipe_command(&gpg, buffer->buf, buffer->len,
Michael J Gruberefee9552016-06-17 19:38:59 -0400954 signature, 1024, &gpg_status, 0);
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700955 sigchain_pop(SIGPIPE);
956
Fabian Stelzera075e792022-03-04 11:25:17 +0100957 for (cp = gpg_status.buf;
958 cp && (cp = strstr(cp, "[GNUPG:] SIG_CREATED "));
959 cp++) {
960 if (cp == gpg_status.buf || cp[-1] == '\n')
961 break; /* found */
962 }
963 ret |= !cp;
Michael J Gruberefee9552016-06-17 19:38:59 -0400964 strbuf_release(&gpg_status);
965 if (ret)
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700966 return error(_("gpg failed to sign the data"));
967
968 /* Strip CR from the line endings, in case we are on Windows. */
Fabian Stelzer29b31572021-09-10 20:07:36 +0000969 remove_cr_after(signature, bottom);
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700970
971 return 0;
972}
Fabian Stelzer29b31572021-09-10 20:07:36 +0000973
974static int sign_buffer_ssh(struct strbuf *buffer, struct strbuf *signature,
975 const char *signing_key)
976{
977 struct child_process signer = CHILD_PROCESS_INIT;
978 int ret = -1;
979 size_t bottom, keylen;
980 struct strbuf signer_stderr = STRBUF_INIT;
981 struct tempfile *key_file = NULL, *buffer_file = NULL;
982 char *ssh_signing_key_file = NULL;
983 struct strbuf ssh_signature_filename = STRBUF_INIT;
Fabian Stelzer350a2512021-11-19 16:07:06 +0100984 const char *literal_key = NULL;
Fabian Stelzer29b31572021-09-10 20:07:36 +0000985
986 if (!signing_key || signing_key[0] == '\0')
987 return error(
988 _("user.signingkey needs to be set for ssh signing"));
989
Fabian Stelzer350a2512021-11-19 16:07:06 +0100990 if (is_literal_ssh_key(signing_key, &literal_key)) {
Fabian Stelzer29b31572021-09-10 20:07:36 +0000991 /* A literal ssh key */
992 key_file = mks_tempfile_t(".git_signing_key_tmpXXXXXX");
993 if (!key_file)
994 return error_errno(
995 _("could not create temporary file"));
Fabian Stelzer350a2512021-11-19 16:07:06 +0100996 keylen = strlen(literal_key);
997 if (write_in_full(key_file->fd, literal_key, keylen) < 0 ||
Fabian Stelzer29b31572021-09-10 20:07:36 +0000998 close_tempfile_gently(key_file) < 0) {
999 error_errno(_("failed writing ssh signing key to '%s'"),
1000 key_file->filename.buf);
1001 goto out;
1002 }
1003 ssh_signing_key_file = strbuf_detach(&key_file->filename, NULL);
1004 } else {
1005 /* We assume a file */
1006 ssh_signing_key_file = expand_user_path(signing_key, 1);
1007 }
1008
1009 buffer_file = mks_tempfile_t(".git_signing_buffer_tmpXXXXXX");
1010 if (!buffer_file) {
1011 error_errno(_("could not create temporary file"));
1012 goto out;
1013 }
1014
1015 if (write_in_full(buffer_file->fd, buffer->buf, buffer->len) < 0 ||
1016 close_tempfile_gently(buffer_file) < 0) {
1017 error_errno(_("failed writing ssh signing key buffer to '%s'"),
1018 buffer_file->filename.buf);
1019 goto out;
1020 }
1021
1022 strvec_pushl(&signer.args, use_format->program,
1023 "-Y", "sign",
1024 "-n", "git",
1025 "-f", ssh_signing_key_file,
1026 buffer_file->filename.buf,
1027 NULL);
1028
1029 sigchain_push(SIGPIPE, SIG_IGN);
1030 ret = pipe_command(&signer, NULL, 0, NULL, 0, &signer_stderr, 0);
1031 sigchain_pop(SIGPIPE);
1032
1033 if (ret) {
1034 if (strstr(signer_stderr.buf, "usage:"))
1035 error(_("ssh-keygen -Y sign is needed for ssh signing (available in openssh version 8.2p1+)"));
1036
1037 error("%s", signer_stderr.buf);
1038 goto out;
1039 }
1040
1041 bottom = signature->len;
1042
1043 strbuf_addbuf(&ssh_signature_filename, &buffer_file->filename);
1044 strbuf_addstr(&ssh_signature_filename, ".sig");
1045 if (strbuf_read_file(signature, ssh_signature_filename.buf, 0) < 0) {
1046 error_errno(
1047 _("failed reading ssh signing data buffer from '%s'"),
1048 ssh_signature_filename.buf);
1049 }
1050 unlink_or_warn(ssh_signature_filename.buf);
1051
1052 /* Strip CR from the line endings, in case we are on Windows. */
1053 remove_cr_after(signature, bottom);
1054
1055out:
1056 if (key_file)
1057 delete_tempfile(&key_file);
1058 if (buffer_file)
1059 delete_tempfile(&buffer_file);
1060 strbuf_release(&signer_stderr);
1061 strbuf_release(&ssh_signature_filename);
1062 FREE_AND_NULL(ssh_signing_key_file);
1063 return ret;
1064}