]>
Commit | Line | Data |
---|---|---|
2f47eae2 | 1 | #include "cache.h" |
9b27b492 | 2 | #include "commit.h" |
b2141fc1 | 3 | #include "config.h" |
f394e093 | 4 | #include "gettext.h" |
2f47eae2 JH |
5 | #include "run-command.h" |
6 | #include "strbuf.h" | |
facca53a | 7 | #include "dir.h" |
b5fa6081 | 8 | #include "ident.h" |
2f47eae2 JH |
9 | #include "gpg-interface.h" |
10 | #include "sigchain.h" | |
4322353b | 11 | #include "tempfile.h" |
fd9e2267 | 12 | #include "alias.h" |
2f47eae2 | 13 | |
fd2d4c13 JH |
14 | static int git_gpg_config(const char *, const char *, void *); |
15 | ||
16 | static void gpg_interface_lazy_init(void) | |
17 | { | |
18 | static int done; | |
19 | ||
20 | if (done) | |
21 | return; | |
22 | done = 1; | |
23 | git_config(git_gpg_config, NULL); | |
24 | } | |
25 | ||
2f47eae2 | 26 | static char *configured_signing_key; |
facca53a | 27 | static const char *ssh_default_key_command, *ssh_allowed_signers, *ssh_revocation_file; |
54887b46 HJI |
28 | static enum signature_trust_level configured_min_trust_level = TRUST_UNDEFINED; |
29 | ||
58af57e1 HS |
30 | struct gpg_format { |
31 | const char *name; | |
32 | const char *program; | |
33 | const char **verify_args; | |
34 | const char **sigs; | |
b5726a5d | 35 | int (*verify_signed_buffer)(struct signature_check *sigc, |
02769437 FS |
36 | struct gpg_format *fmt, |
37 | const char *signature, | |
b5726a5d FS |
38 | size_t signature_size); |
39 | int (*sign_buffer)(struct strbuf *buffer, struct strbuf *signature, | |
40 | const char *signing_key); | |
fd9e2267 | 41 | const char *(*get_default_key)(void); |
4838f62c | 42 | const char *(*get_key_id)(void); |
58af57e1 HS |
43 | }; |
44 | ||
45 | static const char *openpgp_verify_args[] = { | |
46 | "--keyid-format=long", | |
47 | NULL | |
48 | }; | |
49 | static const char *openpgp_sigs[] = { | |
50 | "-----BEGIN PGP SIGNATURE-----", | |
51 | "-----BEGIN PGP MESSAGE-----", | |
52 | NULL | |
53 | }; | |
54 | ||
1e7adb97 HS |
55 | static const char *x509_verify_args[] = { |
56 | NULL | |
57 | }; | |
58 | static const char *x509_sigs[] = { | |
59 | "-----BEGIN SIGNED MESSAGE-----", | |
60 | NULL | |
61 | }; | |
62 | ||
29b31577 FS |
63 | static const char *ssh_verify_args[] = { NULL }; |
64 | static const char *ssh_sigs[] = { | |
65 | "-----BEGIN SSH SIGNATURE-----", | |
66 | NULL | |
67 | }; | |
68 | ||
b5726a5d | 69 | static int verify_gpg_signed_buffer(struct signature_check *sigc, |
02769437 FS |
70 | struct gpg_format *fmt, |
71 | const char *signature, | |
b5726a5d | 72 | size_t signature_size); |
facca53a | 73 | static int verify_ssh_signed_buffer(struct signature_check *sigc, |
02769437 FS |
74 | struct gpg_format *fmt, |
75 | const char *signature, | |
facca53a | 76 | size_t signature_size); |
b5726a5d FS |
77 | static int sign_buffer_gpg(struct strbuf *buffer, struct strbuf *signature, |
78 | const char *signing_key); | |
29b31577 FS |
79 | static int sign_buffer_ssh(struct strbuf *buffer, struct strbuf *signature, |
80 | const char *signing_key); | |
b5726a5d | 81 | |
fd9e2267 FS |
82 | static const char *get_default_ssh_signing_key(void); |
83 | ||
4838f62c FS |
84 | static const char *get_ssh_key_id(void); |
85 | ||
58af57e1 | 86 | static struct gpg_format gpg_format[] = { |
b5726a5d FS |
87 | { |
88 | .name = "openpgp", | |
89 | .program = "gpg", | |
90 | .verify_args = openpgp_verify_args, | |
91 | .sigs = openpgp_sigs, | |
92 | .verify_signed_buffer = verify_gpg_signed_buffer, | |
93 | .sign_buffer = sign_buffer_gpg, | |
fd9e2267 | 94 | .get_default_key = NULL, |
4838f62c | 95 | .get_key_id = NULL, |
58af57e1 | 96 | }, |
b5726a5d FS |
97 | { |
98 | .name = "x509", | |
99 | .program = "gpgsm", | |
100 | .verify_args = x509_verify_args, | |
101 | .sigs = x509_sigs, | |
102 | .verify_signed_buffer = verify_gpg_signed_buffer, | |
103 | .sign_buffer = sign_buffer_gpg, | |
fd9e2267 | 104 | .get_default_key = NULL, |
4838f62c | 105 | .get_key_id = NULL, |
1e7adb97 | 106 | }, |
29b31577 FS |
107 | { |
108 | .name = "ssh", | |
109 | .program = "ssh-keygen", | |
110 | .verify_args = ssh_verify_args, | |
111 | .sigs = ssh_sigs, | |
facca53a | 112 | .verify_signed_buffer = verify_ssh_signed_buffer, |
fd9e2267 FS |
113 | .sign_buffer = sign_buffer_ssh, |
114 | .get_default_key = get_default_ssh_signing_key, | |
4838f62c | 115 | .get_key_id = get_ssh_key_id, |
29b31577 | 116 | }, |
58af57e1 HS |
117 | }; |
118 | ||
119 | static struct gpg_format *use_format = &gpg_format[0]; | |
2f47eae2 | 120 | |
58af57e1 HS |
121 | static struct gpg_format *get_format_by_name(const char *str) |
122 | { | |
123 | int i; | |
124 | ||
125 | for (i = 0; i < ARRAY_SIZE(gpg_format); i++) | |
126 | if (!strcmp(gpg_format[i].name, str)) | |
127 | return gpg_format + i; | |
128 | return NULL; | |
129 | } | |
130 | ||
131 | static struct gpg_format *get_format_by_sig(const char *sig) | |
132 | { | |
133 | int i, j; | |
134 | ||
135 | for (i = 0; i < ARRAY_SIZE(gpg_format); i++) | |
136 | for (j = 0; gpg_format[i].sigs[j]; j++) | |
137 | if (starts_with(sig, gpg_format[i].sigs[j])) | |
138 | return gpg_format + i; | |
139 | return NULL; | |
140 | } | |
d7c67668 | 141 | |
01e57b5d MG |
142 | void signature_check_clear(struct signature_check *sigc) |
143 | { | |
88ce3ef6 | 144 | FREE_AND_NULL(sigc->payload); |
b5726a5d | 145 | FREE_AND_NULL(sigc->output); |
88ce3ef6 ÆAB |
146 | FREE_AND_NULL(sigc->gpg_status); |
147 | FREE_AND_NULL(sigc->signer); | |
148 | FREE_AND_NULL(sigc->key); | |
3daaaabe | 149 | FREE_AND_NULL(sigc->fingerprint); |
4de9394d | 150 | FREE_AND_NULL(sigc->primary_key_fingerprint); |
01e57b5d MG |
151 | } |
152 | ||
da6cf1b3 MG |
153 | /* An exclusive status -- only one of them can appear in output */ |
154 | #define GPG_STATUS_EXCLUSIVE (1<<0) | |
0b11a84e MG |
155 | /* The status includes key identifier */ |
156 | #define GPG_STATUS_KEYID (1<<1) | |
157 | /* The status includes user identifier */ | |
158 | #define GPG_STATUS_UID (1<<2) | |
3daaaabe MG |
159 | /* The status includes key fingerprints */ |
160 | #define GPG_STATUS_FINGERPRINT (1<<3) | |
54887b46 HJI |
161 | /* The status includes trust level */ |
162 | #define GPG_STATUS_TRUST_LEVEL (1<<4) | |
0b11a84e MG |
163 | |
164 | /* Short-hand for standard exclusive *SIG status with keyid & UID */ | |
165 | #define GPG_STATUS_STDSIG (GPG_STATUS_EXCLUSIVE|GPG_STATUS_KEYID|GPG_STATUS_UID) | |
da6cf1b3 | 166 | |
a50e7ca3 JH |
167 | static struct { |
168 | char result; | |
169 | const char *check; | |
da6cf1b3 | 170 | unsigned int flags; |
a50e7ca3 | 171 | } sigcheck_gpg_status[] = { |
0b11a84e MG |
172 | { 'G', "GOODSIG ", GPG_STATUS_STDSIG }, |
173 | { 'B', "BADSIG ", GPG_STATUS_STDSIG }, | |
0b11a84e MG |
174 | { 'E', "ERRSIG ", GPG_STATUS_EXCLUSIVE|GPG_STATUS_KEYID }, |
175 | { 'X', "EXPSIG ", GPG_STATUS_STDSIG }, | |
176 | { 'Y', "EXPKEYSIG ", GPG_STATUS_STDSIG }, | |
177 | { 'R', "REVKEYSIG ", GPG_STATUS_STDSIG }, | |
3daaaabe | 178 | { 0, "VALIDSIG ", GPG_STATUS_FINGERPRINT }, |
54887b46 HJI |
179 | { 0, "TRUST_", GPG_STATUS_TRUST_LEVEL }, |
180 | }; | |
181 | ||
803978da JD |
182 | /* Keep the order same as enum signature_trust_level */ |
183 | static struct sigcheck_gpg_trust_level { | |
54887b46 | 184 | const char *key; |
803978da | 185 | const char *display_key; |
54887b46 HJI |
186 | enum signature_trust_level value; |
187 | } sigcheck_gpg_trust_level[] = { | |
803978da JD |
188 | { "UNDEFINED", "undefined", TRUST_UNDEFINED }, |
189 | { "NEVER", "never", TRUST_NEVER }, | |
190 | { "MARGINAL", "marginal", TRUST_MARGINAL }, | |
191 | { "FULLY", "fully", TRUST_FULLY }, | |
192 | { "ULTIMATE", "ultimate", TRUST_ULTIMATE }, | |
a50e7ca3 JH |
193 | }; |
194 | ||
392b862e HJI |
195 | static void replace_cstring(char **field, const char *line, const char *next) |
196 | { | |
197 | free(*field); | |
198 | ||
199 | if (line && next) | |
200 | *field = xmemdupz(line, next - line); | |
201 | else | |
202 | *field = NULL; | |
203 | } | |
204 | ||
54887b46 HJI |
205 | static int parse_gpg_trust_level(const char *level, |
206 | enum signature_trust_level *res) | |
207 | { | |
208 | size_t i; | |
209 | ||
210 | for (i = 0; i < ARRAY_SIZE(sigcheck_gpg_trust_level); i++) { | |
211 | if (!strcmp(sigcheck_gpg_trust_level[i].key, level)) { | |
212 | *res = sigcheck_gpg_trust_level[i].value; | |
213 | return 0; | |
214 | } | |
215 | } | |
216 | return 1; | |
217 | } | |
218 | ||
fbd0f166 | 219 | static void parse_gpg_output(struct signature_check *sigc) |
a50e7ca3 JH |
220 | { |
221 | const char *buf = sigc->gpg_status; | |
da6cf1b3 | 222 | const char *line, *next; |
4de9394d | 223 | int i, j; |
da6cf1b3 MG |
224 | int seen_exclusive_status = 0; |
225 | ||
226 | /* Iterate over all lines */ | |
227 | for (line = buf; *line; line = strchrnul(line+1, '\n')) { | |
228 | while (*line == '\n') | |
229 | line++; | |
64c45dc7 SR |
230 | if (!*line) |
231 | break; | |
232 | ||
da6cf1b3 MG |
233 | /* Skip lines that don't start with GNUPG status */ |
234 | if (!skip_prefix(line, "[GNUPG:] ", &line)) | |
235 | continue; | |
236 | ||
237 | /* Iterate over all search strings */ | |
238 | for (i = 0; i < ARRAY_SIZE(sigcheck_gpg_status); i++) { | |
239 | if (skip_prefix(line, sigcheck_gpg_status[i].check, &line)) { | |
54887b46 HJI |
240 | /* |
241 | * GOODSIG, BADSIG etc. can occur only once for | |
242 | * each signature. Therefore, if we had more | |
243 | * than one then we're dealing with multiple | |
244 | * signatures. We don't support them | |
245 | * currently, and they're rather hard to | |
246 | * create, so something is likely fishy and we | |
247 | * should reject them altogether. | |
248 | */ | |
da6cf1b3 | 249 | if (sigcheck_gpg_status[i].flags & GPG_STATUS_EXCLUSIVE) { |
02561896 | 250 | if (seen_exclusive_status++) |
54887b46 | 251 | goto error; |
da6cf1b3 MG |
252 | } |
253 | ||
3daaaabe MG |
254 | if (sigcheck_gpg_status[i].result) |
255 | sigc->result = sigcheck_gpg_status[i].result; | |
0b11a84e MG |
256 | /* Do we have key information? */ |
257 | if (sigcheck_gpg_status[i].flags & GPG_STATUS_KEYID) { | |
da6cf1b3 | 258 | next = strchrnul(line, ' '); |
392b862e | 259 | replace_cstring(&sigc->key, line, next); |
0b11a84e MG |
260 | /* Do we have signer information? */ |
261 | if (*next && (sigcheck_gpg_status[i].flags & GPG_STATUS_UID)) { | |
da6cf1b3 MG |
262 | line = next + 1; |
263 | next = strchrnul(line, '\n'); | |
392b862e | 264 | replace_cstring(&sigc->signer, line, next); |
da6cf1b3 MG |
265 | } |
266 | } | |
54887b46 HJI |
267 | |
268 | /* Do we have trust level? */ | |
269 | if (sigcheck_gpg_status[i].flags & GPG_STATUS_TRUST_LEVEL) { | |
270 | /* | |
271 | * GPG v1 and v2 differs in how the | |
272 | * TRUST_ lines are written. Some | |
273 | * trust lines contain no additional | |
274 | * space-separated information for v1. | |
275 | */ | |
276 | size_t trust_size = strcspn(line, " \n"); | |
277 | char *trust = xmemdupz(line, trust_size); | |
278 | ||
279 | if (parse_gpg_trust_level(trust, &sigc->trust_level)) { | |
280 | free(trust); | |
281 | goto error; | |
282 | } | |
283 | free(trust); | |
284 | } | |
285 | ||
3daaaabe MG |
286 | /* Do we have fingerprint? */ |
287 | if (sigcheck_gpg_status[i].flags & GPG_STATUS_FINGERPRINT) { | |
67a6ea63 HJI |
288 | const char *limit; |
289 | char **field; | |
290 | ||
3daaaabe | 291 | next = strchrnul(line, ' '); |
392b862e | 292 | replace_cstring(&sigc->fingerprint, line, next); |
4de9394d | 293 | |
67a6ea63 HJI |
294 | /* |
295 | * Skip interim fields. The search is | |
296 | * limited to the same line since only | |
297 | * OpenPGP signatures has a field with | |
298 | * the primary fingerprint. | |
299 | */ | |
300 | limit = strchrnul(line, '\n'); | |
4de9394d | 301 | for (j = 9; j > 0; j--) { |
67a6ea63 | 302 | if (!*next || limit <= next) |
4de9394d MG |
303 | break; |
304 | line = next + 1; | |
305 | next = strchrnul(line, ' '); | |
306 | } | |
307 | ||
67a6ea63 HJI |
308 | field = &sigc->primary_key_fingerprint; |
309 | if (!j) { | |
310 | next = strchrnul(line, '\n'); | |
311 | replace_cstring(field, line, next); | |
312 | } else { | |
313 | replace_cstring(field, NULL, NULL); | |
314 | } | |
3daaaabe | 315 | } |
da6cf1b3 MG |
316 | |
317 | break; | |
661a1806 | 318 | } |
a50e7ca3 JH |
319 | } |
320 | } | |
da6cf1b3 MG |
321 | return; |
322 | ||
54887b46 | 323 | error: |
da6cf1b3 MG |
324 | sigc->result = 'E'; |
325 | /* Clear partial data to avoid confusion */ | |
4de9394d | 326 | FREE_AND_NULL(sigc->primary_key_fingerprint); |
3daaaabe | 327 | FREE_AND_NULL(sigc->fingerprint); |
da6cf1b3 MG |
328 | FREE_AND_NULL(sigc->signer); |
329 | FREE_AND_NULL(sigc->key); | |
a50e7ca3 JH |
330 | } |
331 | ||
b5726a5d | 332 | static int verify_gpg_signed_buffer(struct signature_check *sigc, |
02769437 FS |
333 | struct gpg_format *fmt, |
334 | const char *signature, | |
b5726a5d | 335 | size_t signature_size) |
67948981 HJI |
336 | { |
337 | struct child_process gpg = CHILD_PROCESS_INIT; | |
67948981 HJI |
338 | struct tempfile *temp; |
339 | int ret; | |
b5726a5d FS |
340 | struct strbuf gpg_stdout = STRBUF_INIT; |
341 | struct strbuf gpg_stderr = STRBUF_INIT; | |
67948981 HJI |
342 | |
343 | temp = mks_tempfile_t(".git_vtag_tmpXXXXXX"); | |
344 | if (!temp) | |
345 | return error_errno(_("could not create temporary file")); | |
346 | if (write_in_full(temp->fd, signature, signature_size) < 0 || | |
347 | close_tempfile_gently(temp) < 0) { | |
348 | error_errno(_("failed writing detached signature to '%s'"), | |
349 | temp->filename.buf); | |
350 | delete_tempfile(&temp); | |
351 | return -1; | |
352 | } | |
353 | ||
ef8d7ac4 JK |
354 | strvec_push(&gpg.args, fmt->program); |
355 | strvec_pushv(&gpg.args, fmt->verify_args); | |
356 | strvec_pushl(&gpg.args, | |
f6d8942b JK |
357 | "--status-fd=1", |
358 | "--verify", temp->filename.buf, "-", | |
359 | NULL); | |
67948981 | 360 | |
67948981 | 361 | sigchain_push(SIGPIPE, SIG_IGN); |
02769437 | 362 | ret = pipe_command(&gpg, sigc->payload, sigc->payload_len, &gpg_stdout, 0, |
b5726a5d | 363 | &gpg_stderr, 0); |
67948981 HJI |
364 | sigchain_pop(SIGPIPE); |
365 | ||
366 | delete_tempfile(&temp); | |
367 | ||
b5726a5d | 368 | ret |= !strstr(gpg_stdout.buf, "\n[GNUPG:] GOODSIG "); |
b5726a5d FS |
369 | sigc->output = strbuf_detach(&gpg_stderr, NULL); |
370 | sigc->gpg_status = strbuf_detach(&gpg_stdout, NULL); | |
371 | ||
372 | parse_gpg_output(sigc); | |
373 | ||
374 | strbuf_release(&gpg_stdout); | |
375 | strbuf_release(&gpg_stderr); | |
67948981 HJI |
376 | |
377 | return ret; | |
378 | } | |
379 | ||
facca53a FS |
380 | static void parse_ssh_output(struct signature_check *sigc) |
381 | { | |
382 | const char *line, *principal, *search; | |
78d468f1 | 383 | char *to_free; |
facca53a FS |
384 | char *key = NULL; |
385 | ||
386 | /* | |
387 | * ssh-keygen output should be: | |
388 | * Good "git" signature for PRINCIPAL with RSA key SHA256:FINGERPRINT | |
389 | * | |
390 | * or for valid but unknown keys: | |
391 | * Good "git" signature with RSA key SHA256:FINGERPRINT | |
392 | * | |
393 | * Note that "PRINCIPAL" can contain whitespace, "RSA" and | |
394 | * "SHA256" part could be a different token that names of | |
395 | * the algorithms used, and "FINGERPRINT" is a hexadecimal | |
396 | * string. By finding the last occurence of " with ", we can | |
397 | * reliably parse out the PRINCIPAL. | |
398 | */ | |
399 | sigc->result = 'B'; | |
400 | sigc->trust_level = TRUST_NEVER; | |
401 | ||
78d468f1 | 402 | line = to_free = xmemdupz(sigc->output, strcspn(sigc->output, "\n")); |
facca53a FS |
403 | |
404 | if (skip_prefix(line, "Good \"git\" signature for ", &line)) { | |
facca53a FS |
405 | /* Search for the last "with" to get the full principal */ |
406 | principal = line; | |
407 | do { | |
408 | search = strstr(line, " with "); | |
409 | if (search) | |
410 | line = search + 1; | |
411 | } while (search != NULL); | |
18b18503 RS |
412 | if (line == principal) |
413 | goto cleanup; | |
414 | ||
415 | /* Valid signature and known principal */ | |
416 | sigc->result = 'G'; | |
417 | sigc->trust_level = TRUST_FULLY; | |
facca53a FS |
418 | sigc->signer = xmemdupz(principal, line - principal - 1); |
419 | } else if (skip_prefix(line, "Good \"git\" signature with ", &line)) { | |
420 | /* Valid signature, but key unknown */ | |
421 | sigc->result = 'G'; | |
422 | sigc->trust_level = TRUST_UNDEFINED; | |
423 | } else { | |
78d468f1 | 424 | goto cleanup; |
facca53a FS |
425 | } |
426 | ||
65db97b4 | 427 | key = strstr(line, "key "); |
facca53a | 428 | if (key) { |
65db97b4 | 429 | sigc->fingerprint = xstrdup(strstr(line, "key ") + 4); |
facca53a FS |
430 | sigc->key = xstrdup(sigc->fingerprint); |
431 | } else { | |
432 | /* | |
433 | * Output did not match what we expected | |
434 | * Treat the signature as bad | |
435 | */ | |
436 | sigc->result = 'B'; | |
437 | } | |
78d468f1 JK |
438 | |
439 | cleanup: | |
440 | free(to_free); | |
facca53a FS |
441 | } |
442 | ||
443 | static int verify_ssh_signed_buffer(struct signature_check *sigc, | |
02769437 FS |
444 | struct gpg_format *fmt, |
445 | const char *signature, | |
facca53a FS |
446 | size_t signature_size) |
447 | { | |
448 | struct child_process ssh_keygen = CHILD_PROCESS_INIT; | |
449 | struct tempfile *buffer_file; | |
450 | int ret = -1; | |
451 | const char *line; | |
facca53a FS |
452 | char *principal; |
453 | struct strbuf ssh_principals_out = STRBUF_INIT; | |
454 | struct strbuf ssh_principals_err = STRBUF_INIT; | |
455 | struct strbuf ssh_keygen_out = STRBUF_INIT; | |
456 | struct strbuf ssh_keygen_err = STRBUF_INIT; | |
6393c956 FS |
457 | struct strbuf verify_time = STRBUF_INIT; |
458 | const struct date_mode verify_date_mode = { | |
459 | .type = DATE_STRFTIME, | |
460 | .strftime_fmt = "%Y%m%d%H%M%S", | |
461 | /* SSH signing key validity has no timezone information - Use the local timezone */ | |
462 | .local = 1, | |
463 | }; | |
facca53a FS |
464 | |
465 | if (!ssh_allowed_signers) { | |
466 | error(_("gpg.ssh.allowedSignersFile needs to be configured and exist for ssh signature verification")); | |
467 | return -1; | |
468 | } | |
469 | ||
470 | buffer_file = mks_tempfile_t(".git_vtag_tmpXXXXXX"); | |
471 | if (!buffer_file) | |
472 | return error_errno(_("could not create temporary file")); | |
473 | if (write_in_full(buffer_file->fd, signature, signature_size) < 0 || | |
474 | close_tempfile_gently(buffer_file) < 0) { | |
475 | error_errno(_("failed writing detached signature to '%s'"), | |
476 | buffer_file->filename.buf); | |
477 | delete_tempfile(&buffer_file); | |
478 | return -1; | |
479 | } | |
480 | ||
6393c956 FS |
481 | if (sigc->payload_timestamp) |
482 | strbuf_addf(&verify_time, "-Overify-time=%s", | |
483 | show_date(sigc->payload_timestamp, 0, &verify_date_mode)); | |
484 | ||
facca53a FS |
485 | /* Find the principal from the signers */ |
486 | strvec_pushl(&ssh_keygen.args, fmt->program, | |
487 | "-Y", "find-principals", | |
488 | "-f", ssh_allowed_signers, | |
489 | "-s", buffer_file->filename.buf, | |
6393c956 | 490 | verify_time.buf, |
facca53a FS |
491 | NULL); |
492 | ret = pipe_command(&ssh_keygen, NULL, 0, &ssh_principals_out, 0, | |
493 | &ssh_principals_err, 0); | |
494 | if (ret && strstr(ssh_principals_err.buf, "usage:")) { | |
495 | error(_("ssh-keygen -Y find-principals/verify is needed for ssh signature verification (available in openssh version 8.2p1+)")); | |
496 | goto out; | |
497 | } | |
498 | if (ret || !ssh_principals_out.len) { | |
499 | /* | |
500 | * We did not find a matching principal in the allowedSigners | |
501 | * Check without validation | |
502 | */ | |
503 | child_process_init(&ssh_keygen); | |
504 | strvec_pushl(&ssh_keygen.args, fmt->program, | |
505 | "-Y", "check-novalidate", | |
506 | "-n", "git", | |
507 | "-s", buffer_file->filename.buf, | |
6393c956 | 508 | verify_time.buf, |
facca53a | 509 | NULL); |
02769437 | 510 | pipe_command(&ssh_keygen, sigc->payload, sigc->payload_len, |
facca53a FS |
511 | &ssh_keygen_out, 0, &ssh_keygen_err, 0); |
512 | ||
513 | /* | |
514 | * Fail on unknown keys | |
515 | * we still call check-novalidate to display the signature info | |
516 | */ | |
517 | ret = -1; | |
518 | } else { | |
519 | /* Check every principal we found (one per line) */ | |
caeef01e FS |
520 | const char *next; |
521 | for (line = ssh_principals_out.buf; | |
522 | *line; | |
523 | line = next) { | |
524 | const char *end_of_text; | |
525 | ||
526 | next = end_of_text = strchrnul(line, '\n'); | |
527 | ||
528 | /* Did we find a LF, and did we have CR before it? */ | |
529 | if (*end_of_text && | |
530 | line < end_of_text && | |
531 | end_of_text[-1] == '\r') | |
532 | end_of_text--; | |
533 | ||
534 | /* Unless we hit NUL, skip over the LF we found */ | |
535 | if (*next) | |
536 | next++; | |
537 | ||
538 | /* Not all lines are data. Skip empty ones */ | |
539 | if (line == end_of_text) | |
540 | continue; | |
541 | ||
542 | /* We now know we have an non-empty line. Process it */ | |
543 | principal = xmemdupz(line, end_of_text - line); | |
facca53a FS |
544 | |
545 | child_process_init(&ssh_keygen); | |
546 | strbuf_release(&ssh_keygen_out); | |
547 | strbuf_release(&ssh_keygen_err); | |
548 | strvec_push(&ssh_keygen.args, fmt->program); | |
549 | /* | |
550 | * We found principals | |
551 | * Try with each until we find a match | |
552 | */ | |
553 | strvec_pushl(&ssh_keygen.args, "-Y", "verify", | |
554 | "-n", "git", | |
555 | "-f", ssh_allowed_signers, | |
556 | "-I", principal, | |
557 | "-s", buffer_file->filename.buf, | |
6393c956 | 558 | verify_time.buf, |
facca53a FS |
559 | NULL); |
560 | ||
561 | if (ssh_revocation_file) { | |
562 | if (file_exists(ssh_revocation_file)) { | |
563 | strvec_pushl(&ssh_keygen.args, "-r", | |
564 | ssh_revocation_file, NULL); | |
565 | } else { | |
566 | warning(_("ssh signing revocation file configured but not found: %s"), | |
567 | ssh_revocation_file); | |
568 | } | |
569 | } | |
570 | ||
571 | sigchain_push(SIGPIPE, SIG_IGN); | |
02769437 | 572 | ret = pipe_command(&ssh_keygen, sigc->payload, sigc->payload_len, |
facca53a FS |
573 | &ssh_keygen_out, 0, &ssh_keygen_err, 0); |
574 | sigchain_pop(SIGPIPE); | |
575 | ||
576 | FREE_AND_NULL(principal); | |
577 | ||
578 | if (!ret) | |
579 | ret = !starts_with(ssh_keygen_out.buf, "Good"); | |
580 | ||
581 | if (!ret) | |
582 | break; | |
583 | } | |
584 | } | |
585 | ||
facca53a FS |
586 | strbuf_stripspace(&ssh_keygen_out, 0); |
587 | strbuf_stripspace(&ssh_keygen_err, 0); | |
588 | /* Add stderr outputs to show the user actual ssh-keygen errors */ | |
589 | strbuf_add(&ssh_keygen_out, ssh_principals_err.buf, ssh_principals_err.len); | |
590 | strbuf_add(&ssh_keygen_out, ssh_keygen_err.buf, ssh_keygen_err.len); | |
591 | sigc->output = strbuf_detach(&ssh_keygen_out, NULL); | |
592 | sigc->gpg_status = xstrdup(sigc->output); | |
593 | ||
594 | parse_ssh_output(sigc); | |
595 | ||
596 | out: | |
597 | if (buffer_file) | |
598 | delete_tempfile(&buffer_file); | |
599 | strbuf_release(&ssh_principals_out); | |
600 | strbuf_release(&ssh_principals_err); | |
601 | strbuf_release(&ssh_keygen_out); | |
602 | strbuf_release(&ssh_keygen_err); | |
6393c956 | 603 | strbuf_release(&verify_time); |
facca53a FS |
604 | |
605 | return ret; | |
606 | } | |
607 | ||
6393c956 FS |
608 | static int parse_payload_metadata(struct signature_check *sigc) |
609 | { | |
610 | const char *ident_line = NULL; | |
611 | size_t ident_len; | |
612 | struct ident_split ident; | |
613 | const char *signer_header; | |
614 | ||
615 | switch (sigc->payload_type) { | |
616 | case SIGNATURE_PAYLOAD_COMMIT: | |
617 | signer_header = "committer"; | |
618 | break; | |
619 | case SIGNATURE_PAYLOAD_TAG: | |
620 | signer_header = "tagger"; | |
621 | break; | |
622 | case SIGNATURE_PAYLOAD_UNDEFINED: | |
623 | case SIGNATURE_PAYLOAD_PUSH_CERT: | |
624 | /* Ignore payloads we don't want to parse */ | |
625 | return 0; | |
626 | default: | |
627 | BUG("invalid value for sigc->payload_type"); | |
628 | } | |
629 | ||
630 | ident_line = find_commit_header(sigc->payload, signer_header, &ident_len); | |
631 | if (!ident_line || !ident_len) | |
632 | return 1; | |
633 | ||
634 | if (split_ident_line(&ident, ident_line, ident_len)) | |
635 | return 1; | |
636 | ||
637 | if (!sigc->payload_timestamp && ident.date_begin && ident.date_end) | |
638 | sigc->payload_timestamp = parse_timestamp(ident.date_begin, NULL, 10); | |
639 | ||
640 | return 0; | |
641 | } | |
642 | ||
02769437 FS |
643 | int check_signature(struct signature_check *sigc, |
644 | const char *signature, size_t slen) | |
a4cc18f2 | 645 | { |
b5726a5d | 646 | struct gpg_format *fmt; |
a4cc18f2 | 647 | int status; |
648 | ||
fd2d4c13 JH |
649 | gpg_interface_lazy_init(); |
650 | ||
a4cc18f2 | 651 | sigc->result = 'N'; |
54887b46 | 652 | sigc->trust_level = -1; |
a4cc18f2 | 653 | |
b5726a5d FS |
654 | fmt = get_format_by_sig(signature); |
655 | if (!fmt) | |
656 | die(_("bad/incompatible signature '%s'"), signature); | |
657 | ||
6393c956 FS |
658 | if (parse_payload_metadata(sigc)) |
659 | return 1; | |
660 | ||
02769437 | 661 | status = fmt->verify_signed_buffer(sigc, fmt, signature, slen); |
b5726a5d FS |
662 | |
663 | if (status && !sigc->output) | |
664 | return !!status; | |
665 | ||
54887b46 HJI |
666 | status |= sigc->result != 'G'; |
667 | status |= sigc->trust_level < configured_min_trust_level; | |
a4cc18f2 | 668 | |
4e5dc9ca | 669 | return !!status; |
a4cc18f2 | 670 | } |
671 | ||
ca194d50 | 672 | void print_signature_buffer(const struct signature_check *sigc, unsigned flags) |
673 | { | |
b5726a5d FS |
674 | const char *output = flags & GPG_VERIFY_RAW ? sigc->gpg_status : |
675 | sigc->output; | |
aeff29dd | 676 | |
ca194d50 | 677 | if (flags & GPG_VERIFY_VERBOSE && sigc->payload) |
02769437 | 678 | fwrite(sigc->payload, 1, sigc->payload_len, stdout); |
ca194d50 | 679 | |
aeff29dd | 680 | if (output) |
681 | fputs(output, stderr); | |
ca194d50 | 682 | } |
683 | ||
482c1191 | 684 | size_t parse_signed_buffer(const char *buf, size_t size) |
d7c67668 | 685 | { |
d7c67668 | 686 | size_t len = 0; |
8b44b2be JK |
687 | size_t match = size; |
688 | while (len < size) { | |
689 | const char *eol; | |
690 | ||
58af57e1 | 691 | if (get_format_by_sig(buf + len)) |
8b44b2be JK |
692 | match = len; |
693 | ||
694 | eol = memchr(buf + len, '\n', size - len); | |
d7c67668 JH |
695 | len += eol ? eol - (buf + len) + 1 : size - len; |
696 | } | |
8b44b2be | 697 | return match; |
d7c67668 JH |
698 | } |
699 | ||
482c1191 | 700 | int parse_signature(const char *buf, size_t size, struct strbuf *payload, struct strbuf *signature) |
701 | { | |
702 | size_t match = parse_signed_buffer(buf, size); | |
703 | if (match != size) { | |
704 | strbuf_add(payload, buf, match); | |
9b27b492 | 705 | remove_signature(payload); |
482c1191 | 706 | strbuf_add(signature, buf + match, size - match); |
707 | return 1; | |
708 | } | |
709 | return 0; | |
710 | } | |
711 | ||
2f47eae2 JH |
712 | void set_signing_key(const char *key) |
713 | { | |
fd2d4c13 JH |
714 | gpg_interface_lazy_init(); |
715 | ||
2f47eae2 JH |
716 | free(configured_signing_key); |
717 | configured_signing_key = xstrdup(key); | |
718 | } | |
719 | ||
fd2d4c13 | 720 | static int git_gpg_config(const char *var, const char *value, void *cb UNUSED) |
2f47eae2 | 721 | { |
58af57e1 HS |
722 | struct gpg_format *fmt = NULL; |
723 | char *fmtname = NULL; | |
54887b46 HJI |
724 | char *trust; |
725 | int ret; | |
58af57e1 | 726 | |
2f47eae2 | 727 | if (!strcmp(var, "user.signingkey")) { |
1b0eeec3 JK |
728 | if (!value) |
729 | return config_error_nonbool(var); | |
0c5e70f0 | 730 | set_signing_key(value); |
1b0eeec3 | 731 | return 0; |
0c5e70f0 | 732 | } |
1b0eeec3 | 733 | |
57a8dd75 HS |
734 | if (!strcmp(var, "gpg.format")) { |
735 | if (!value) | |
736 | return config_error_nonbool(var); | |
58af57e1 HS |
737 | fmt = get_format_by_name(value); |
738 | if (!fmt) | |
1a8aea85 | 739 | return error(_("invalid value for '%s': '%s'"), |
57a8dd75 | 740 | var, value); |
58af57e1 HS |
741 | use_format = fmt; |
742 | return 0; | |
57a8dd75 HS |
743 | } |
744 | ||
54887b46 HJI |
745 | if (!strcmp(var, "gpg.mintrustlevel")) { |
746 | if (!value) | |
747 | return config_error_nonbool(var); | |
748 | ||
749 | trust = xstrdup_toupper(value); | |
750 | ret = parse_gpg_trust_level(trust, &configured_min_trust_level); | |
751 | free(trust); | |
752 | ||
753 | if (ret) | |
1a8aea85 JNA |
754 | return error(_("invalid value for '%s': '%s'"), |
755 | var, value); | |
54887b46 HJI |
756 | return 0; |
757 | } | |
758 | ||
fd9e2267 FS |
759 | if (!strcmp(var, "gpg.ssh.defaultkeycommand")) { |
760 | if (!value) | |
761 | return config_error_nonbool(var); | |
762 | return git_config_string(&ssh_default_key_command, var, value); | |
763 | } | |
764 | ||
facca53a FS |
765 | if (!strcmp(var, "gpg.ssh.allowedsignersfile")) { |
766 | if (!value) | |
767 | return config_error_nonbool(var); | |
768 | return git_config_pathname(&ssh_allowed_signers, var, value); | |
769 | } | |
770 | ||
771 | if (!strcmp(var, "gpg.ssh.revocationfile")) { | |
772 | if (!value) | |
773 | return config_error_nonbool(var); | |
774 | return git_config_pathname(&ssh_revocation_file, var, value); | |
775 | } | |
776 | ||
b02f51b1 | 777 | if (!strcmp(var, "gpg.program") || !strcmp(var, "gpg.openpgp.program")) |
58af57e1 HS |
778 | fmtname = "openpgp"; |
779 | ||
1e7adb97 HS |
780 | if (!strcmp(var, "gpg.x509.program")) |
781 | fmtname = "x509"; | |
782 | ||
29b31577 FS |
783 | if (!strcmp(var, "gpg.ssh.program")) |
784 | fmtname = "ssh"; | |
785 | ||
58af57e1 HS |
786 | if (fmtname) { |
787 | fmt = get_format_by_name(fmtname); | |
788 | return git_config_string(&fmt->program, var, value); | |
2f47eae2 | 789 | } |
1b0eeec3 | 790 | |
2f47eae2 JH |
791 | return 0; |
792 | } | |
793 | ||
350a2518 FS |
794 | /* |
795 | * Returns 1 if `string` contains a literal ssh key, 0 otherwise | |
796 | * `key` will be set to the start of the actual key if a prefix is present. | |
797 | */ | |
798 | static int is_literal_ssh_key(const char *string, const char **key) | |
799 | { | |
800 | if (skip_prefix(string, "key::", key)) | |
801 | return 1; | |
802 | if (starts_with(string, "ssh-")) { | |
803 | *key = string; | |
804 | return 1; | |
805 | } | |
806 | return 0; | |
807 | } | |
808 | ||
4838f62c FS |
809 | static char *get_ssh_key_fingerprint(const char *signing_key) |
810 | { | |
811 | struct child_process ssh_keygen = CHILD_PROCESS_INIT; | |
812 | int ret = -1; | |
813 | struct strbuf fingerprint_stdout = STRBUF_INIT; | |
814 | struct strbuf **fingerprint; | |
f3af71c9 | 815 | char *fingerprint_ret; |
350a2518 | 816 | const char *literal_key = NULL; |
4838f62c FS |
817 | |
818 | /* | |
819 | * With SSH Signing this can contain a filename or a public key | |
820 | * For textual representation we usually want a fingerprint | |
821 | */ | |
350a2518 | 822 | if (is_literal_ssh_key(signing_key, &literal_key)) { |
4838f62c | 823 | strvec_pushl(&ssh_keygen.args, "ssh-keygen", "-lf", "-", NULL); |
350a2518 FS |
824 | ret = pipe_command(&ssh_keygen, literal_key, |
825 | strlen(literal_key), &fingerprint_stdout, 0, | |
4838f62c FS |
826 | NULL, 0); |
827 | } else { | |
828 | strvec_pushl(&ssh_keygen.args, "ssh-keygen", "-lf", | |
829 | configured_signing_key, NULL); | |
830 | ret = pipe_command(&ssh_keygen, NULL, 0, &fingerprint_stdout, 0, | |
831 | NULL, 0); | |
832 | } | |
833 | ||
834 | if (!!ret) | |
835 | die_errno(_("failed to get the ssh fingerprint for key '%s'"), | |
836 | signing_key); | |
837 | ||
838 | fingerprint = strbuf_split_max(&fingerprint_stdout, ' ', 3); | |
839 | if (!fingerprint[1]) | |
840 | die_errno(_("failed to get the ssh fingerprint for key '%s'"), | |
841 | signing_key); | |
842 | ||
f3af71c9 JK |
843 | fingerprint_ret = strbuf_detach(fingerprint[1], NULL); |
844 | strbuf_list_free(fingerprint); | |
845 | strbuf_release(&fingerprint_stdout); | |
846 | return fingerprint_ret; | |
4838f62c FS |
847 | } |
848 | ||
fd9e2267 FS |
849 | /* Returns the first public key from an ssh-agent to use for signing */ |
850 | static const char *get_default_ssh_signing_key(void) | |
851 | { | |
852 | struct child_process ssh_default_key = CHILD_PROCESS_INIT; | |
853 | int ret = -1; | |
854 | struct strbuf key_stdout = STRBUF_INIT, key_stderr = STRBUF_INIT; | |
855 | struct strbuf **keys; | |
856 | char *key_command = NULL; | |
857 | const char **argv; | |
858 | int n; | |
859 | char *default_key = NULL; | |
350a2518 | 860 | const char *literal_key = NULL; |
fd9e2267 FS |
861 | |
862 | if (!ssh_default_key_command) | |
863 | die(_("either user.signingkey or gpg.ssh.defaultKeyCommand needs to be configured")); | |
864 | ||
865 | key_command = xstrdup(ssh_default_key_command); | |
866 | n = split_cmdline(key_command, &argv); | |
867 | ||
868 | if (n < 0) | |
869 | die("malformed build-time gpg.ssh.defaultKeyCommand: %s", | |
870 | split_cmdline_strerror(n)); | |
871 | ||
872 | strvec_pushv(&ssh_default_key.args, argv); | |
873 | ret = pipe_command(&ssh_default_key, NULL, 0, &key_stdout, 0, | |
874 | &key_stderr, 0); | |
875 | ||
876 | if (!ret) { | |
877 | keys = strbuf_split_max(&key_stdout, '\n', 2); | |
350a2518 FS |
878 | if (keys[0] && is_literal_ssh_key(keys[0]->buf, &literal_key)) { |
879 | /* | |
880 | * We only use `is_literal_ssh_key` here to check validity | |
881 | * The prefix will be stripped when the key is used. | |
882 | */ | |
fd9e2267 FS |
883 | default_key = strbuf_detach(keys[0], NULL); |
884 | } else { | |
f7337193 | 885 | warning(_("gpg.ssh.defaultKeyCommand succeeded but returned no keys: %s %s"), |
fd9e2267 FS |
886 | key_stderr.buf, key_stdout.buf); |
887 | } | |
888 | ||
889 | strbuf_list_free(keys); | |
890 | } else { | |
891 | warning(_("gpg.ssh.defaultKeyCommand failed: %s %s"), | |
892 | key_stderr.buf, key_stdout.buf); | |
893 | } | |
894 | ||
895 | free(key_command); | |
896 | free(argv); | |
897 | strbuf_release(&key_stdout); | |
898 | ||
899 | return default_key; | |
900 | } | |
901 | ||
4838f62c FS |
902 | static const char *get_ssh_key_id(void) { |
903 | return get_ssh_key_fingerprint(get_signing_key()); | |
904 | } | |
905 | ||
906 | /* Returns a textual but unique representation of the signing key */ | |
907 | const char *get_signing_key_id(void) | |
908 | { | |
fd2d4c13 JH |
909 | gpg_interface_lazy_init(); |
910 | ||
4838f62c FS |
911 | if (use_format->get_key_id) { |
912 | return use_format->get_key_id(); | |
913 | } | |
914 | ||
915 | /* GPG/GPGSM only store a key id on this variable */ | |
916 | return get_signing_key(); | |
917 | } | |
918 | ||
2f47eae2 JH |
919 | const char *get_signing_key(void) |
920 | { | |
fd2d4c13 JH |
921 | gpg_interface_lazy_init(); |
922 | ||
2f47eae2 JH |
923 | if (configured_signing_key) |
924 | return configured_signing_key; | |
fd9e2267 FS |
925 | if (use_format->get_default_key) { |
926 | return use_format->get_default_key(); | |
927 | } | |
928 | ||
929 | return git_committer_info(IDENT_STRICT | IDENT_NO_DATE); | |
2f47eae2 JH |
930 | } |
931 | ||
803978da JD |
932 | const char *gpg_trust_level_to_str(enum signature_trust_level level) |
933 | { | |
934 | struct sigcheck_gpg_trust_level *trust; | |
935 | ||
936 | if (level < 0 || level >= ARRAY_SIZE(sigcheck_gpg_trust_level)) | |
937 | BUG("invalid trust level requested %d", level); | |
938 | ||
939 | trust = &sigcheck_gpg_trust_level[level]; | |
940 | if (trust->value != level) | |
941 | BUG("sigcheck_gpg_trust_level[] unsorted"); | |
942 | ||
943 | return sigcheck_gpg_trust_level[level].display_key; | |
944 | } | |
945 | ||
2f47eae2 | 946 | int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *signing_key) |
b5726a5d | 947 | { |
fd2d4c13 JH |
948 | gpg_interface_lazy_init(); |
949 | ||
b5726a5d FS |
950 | return use_format->sign_buffer(buffer, signature, signing_key); |
951 | } | |
952 | ||
29b31577 FS |
953 | /* |
954 | * Strip CR from the line endings, in case we are on Windows. | |
955 | * NEEDSWORK: make it trim only CRs before LFs and rename | |
956 | */ | |
957 | static void remove_cr_after(struct strbuf *buffer, size_t offset) | |
958 | { | |
959 | size_t i, j; | |
960 | ||
961 | for (i = j = offset; i < buffer->len; i++) { | |
962 | if (buffer->buf[i] != '\r') { | |
963 | if (i != j) | |
964 | buffer->buf[j] = buffer->buf[i]; | |
965 | j++; | |
966 | } | |
967 | } | |
968 | strbuf_setlen(buffer, j); | |
969 | } | |
970 | ||
b5726a5d FS |
971 | static int sign_buffer_gpg(struct strbuf *buffer, struct strbuf *signature, |
972 | const char *signing_key) | |
2f47eae2 | 973 | { |
d3180279 | 974 | struct child_process gpg = CHILD_PROCESS_INIT; |
0581b546 | 975 | int ret; |
29b31577 | 976 | size_t bottom; |
a075e79d | 977 | const char *cp; |
efee9553 | 978 | struct strbuf gpg_status = STRBUF_INIT; |
2f47eae2 | 979 | |
ef8d7ac4 | 980 | strvec_pushl(&gpg.args, |
f6d8942b JK |
981 | use_format->program, |
982 | "--status-fd=2", | |
983 | "-bsau", signing_key, | |
984 | NULL); | |
2f47eae2 | 985 | |
0581b546 | 986 | bottom = signature->len; |
2f47eae2 JH |
987 | |
988 | /* | |
989 | * When the username signingkey is bad, program could be terminated | |
990 | * because gpg exits without reading and then write gets SIGPIPE. | |
991 | */ | |
992 | sigchain_push(SIGPIPE, SIG_IGN); | |
0581b546 | 993 | ret = pipe_command(&gpg, buffer->buf, buffer->len, |
efee9553 | 994 | signature, 1024, &gpg_status, 0); |
2f47eae2 JH |
995 | sigchain_pop(SIGPIPE); |
996 | ||
a075e79d FS |
997 | for (cp = gpg_status.buf; |
998 | cp && (cp = strstr(cp, "[GNUPG:] SIG_CREATED ")); | |
999 | cp++) { | |
1000 | if (cp == gpg_status.buf || cp[-1] == '\n') | |
1001 | break; /* found */ | |
1002 | } | |
1003 | ret |= !cp; | |
ad6b3207 JS |
1004 | if (ret) { |
1005 | error(_("gpg failed to sign the data:\n%s"), | |
1006 | gpg_status.len ? gpg_status.buf : "(no gpg output)"); | |
1007 | strbuf_release(&gpg_status); | |
1008 | return -1; | |
1009 | } | |
efee9553 | 1010 | strbuf_release(&gpg_status); |
2f47eae2 JH |
1011 | |
1012 | /* Strip CR from the line endings, in case we are on Windows. */ | |
29b31577 | 1013 | remove_cr_after(signature, bottom); |
2f47eae2 JH |
1014 | |
1015 | return 0; | |
1016 | } | |
29b31577 FS |
1017 | |
1018 | static int sign_buffer_ssh(struct strbuf *buffer, struct strbuf *signature, | |
1019 | const char *signing_key) | |
1020 | { | |
1021 | struct child_process signer = CHILD_PROCESS_INIT; | |
1022 | int ret = -1; | |
1023 | size_t bottom, keylen; | |
1024 | struct strbuf signer_stderr = STRBUF_INIT; | |
1025 | struct tempfile *key_file = NULL, *buffer_file = NULL; | |
1026 | char *ssh_signing_key_file = NULL; | |
1027 | struct strbuf ssh_signature_filename = STRBUF_INIT; | |
350a2518 | 1028 | const char *literal_key = NULL; |
dce7b311 | 1029 | int literal_ssh_key = 0; |
29b31577 FS |
1030 | |
1031 | if (!signing_key || signing_key[0] == '\0') | |
1032 | return error( | |
b4eda05d | 1033 | _("user.signingKey needs to be set for ssh signing")); |
29b31577 | 1034 | |
350a2518 | 1035 | if (is_literal_ssh_key(signing_key, &literal_key)) { |
29b31577 | 1036 | /* A literal ssh key */ |
dce7b311 | 1037 | literal_ssh_key = 1; |
29b31577 FS |
1038 | key_file = mks_tempfile_t(".git_signing_key_tmpXXXXXX"); |
1039 | if (!key_file) | |
1040 | return error_errno( | |
1041 | _("could not create temporary file")); | |
350a2518 FS |
1042 | keylen = strlen(literal_key); |
1043 | if (write_in_full(key_file->fd, literal_key, keylen) < 0 || | |
29b31577 FS |
1044 | close_tempfile_gently(key_file) < 0) { |
1045 | error_errno(_("failed writing ssh signing key to '%s'"), | |
1046 | key_file->filename.buf); | |
1047 | goto out; | |
1048 | } | |
1049 | ssh_signing_key_file = strbuf_detach(&key_file->filename, NULL); | |
1050 | } else { | |
1051 | /* We assume a file */ | |
1052 | ssh_signing_key_file = expand_user_path(signing_key, 1); | |
1053 | } | |
1054 | ||
1055 | buffer_file = mks_tempfile_t(".git_signing_buffer_tmpXXXXXX"); | |
1056 | if (!buffer_file) { | |
1057 | error_errno(_("could not create temporary file")); | |
1058 | goto out; | |
1059 | } | |
1060 | ||
1061 | if (write_in_full(buffer_file->fd, buffer->buf, buffer->len) < 0 || | |
1062 | close_tempfile_gently(buffer_file) < 0) { | |
1063 | error_errno(_("failed writing ssh signing key buffer to '%s'"), | |
1064 | buffer_file->filename.buf); | |
1065 | goto out; | |
1066 | } | |
1067 | ||
1068 | strvec_pushl(&signer.args, use_format->program, | |
1069 | "-Y", "sign", | |
1070 | "-n", "git", | |
1071 | "-f", ssh_signing_key_file, | |
29b31577 | 1072 | NULL); |
dce7b311 AS |
1073 | if (literal_ssh_key) |
1074 | strvec_push(&signer.args, "-U"); | |
1075 | strvec_push(&signer.args, buffer_file->filename.buf); | |
29b31577 FS |
1076 | |
1077 | sigchain_push(SIGPIPE, SIG_IGN); | |
1078 | ret = pipe_command(&signer, NULL, 0, NULL, 0, &signer_stderr, 0); | |
1079 | sigchain_pop(SIGPIPE); | |
1080 | ||
1081 | if (ret) { | |
1082 | if (strstr(signer_stderr.buf, "usage:")) | |
1083 | error(_("ssh-keygen -Y sign is needed for ssh signing (available in openssh version 8.2p1+)")); | |
1084 | ||
1085 | error("%s", signer_stderr.buf); | |
1086 | goto out; | |
1087 | } | |
1088 | ||
1089 | bottom = signature->len; | |
1090 | ||
1091 | strbuf_addbuf(&ssh_signature_filename, &buffer_file->filename); | |
1092 | strbuf_addstr(&ssh_signature_filename, ".sig"); | |
1093 | if (strbuf_read_file(signature, ssh_signature_filename.buf, 0) < 0) { | |
36fb0d07 | 1094 | ret = error_errno( |
29b31577 FS |
1095 | _("failed reading ssh signing data buffer from '%s'"), |
1096 | ssh_signature_filename.buf); | |
36fb0d07 | 1097 | goto out; |
29b31577 | 1098 | } |
29b31577 FS |
1099 | /* Strip CR from the line endings, in case we are on Windows. */ |
1100 | remove_cr_after(signature, bottom); | |
1101 | ||
1102 | out: | |
1103 | if (key_file) | |
1104 | delete_tempfile(&key_file); | |
1105 | if (buffer_file) | |
1106 | delete_tempfile(&buffer_file); | |
36fb0d07 PW |
1107 | if (ssh_signature_filename.len) |
1108 | unlink_or_warn(ssh_signature_filename.buf); | |
29b31577 FS |
1109 | strbuf_release(&signer_stderr); |
1110 | strbuf_release(&ssh_signature_filename); | |
1111 | FREE_AND_NULL(ssh_signing_key_file); | |
1112 | return ret; | |
1113 | } |