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