]>
Commit | Line | Data |
---|---|---|
2f47eae2 | 1 | #include "cache.h" |
9b27b492 | 2 | #include "commit.h" |
b2141fc1 | 3 | #include "config.h" |
2f47eae2 JH |
4 | #include "run-command.h" |
5 | #include "strbuf.h" | |
facca53a | 6 | #include "dir.h" |
2f47eae2 JH |
7 | #include "gpg-interface.h" |
8 | #include "sigchain.h" | |
4322353b | 9 | #include "tempfile.h" |
fd9e2267 | 10 | #include "alias.h" |
2f47eae2 JH |
11 | |
12 | static char *configured_signing_key; | |
facca53a | 13 | static const char *ssh_default_key_command, *ssh_allowed_signers, *ssh_revocation_file; |
54887b46 HJI |
14 | static enum signature_trust_level configured_min_trust_level = TRUST_UNDEFINED; |
15 | ||
58af57e1 HS |
16 | struct gpg_format { |
17 | const char *name; | |
18 | const char *program; | |
19 | const char **verify_args; | |
20 | const char **sigs; | |
b5726a5d FS |
21 | int (*verify_signed_buffer)(struct signature_check *sigc, |
22 | struct gpg_format *fmt, const char *payload, | |
23 | size_t payload_size, const char *signature, | |
24 | size_t signature_size); | |
25 | int (*sign_buffer)(struct strbuf *buffer, struct strbuf *signature, | |
26 | const char *signing_key); | |
fd9e2267 | 27 | const char *(*get_default_key)(void); |
4838f62c | 28 | const char *(*get_key_id)(void); |
58af57e1 HS |
29 | }; |
30 | ||
31 | static const char *openpgp_verify_args[] = { | |
32 | "--keyid-format=long", | |
33 | NULL | |
34 | }; | |
35 | static const char *openpgp_sigs[] = { | |
36 | "-----BEGIN PGP SIGNATURE-----", | |
37 | "-----BEGIN PGP MESSAGE-----", | |
38 | NULL | |
39 | }; | |
40 | ||
1e7adb97 HS |
41 | static const char *x509_verify_args[] = { |
42 | NULL | |
43 | }; | |
44 | static const char *x509_sigs[] = { | |
45 | "-----BEGIN SIGNED MESSAGE-----", | |
46 | NULL | |
47 | }; | |
48 | ||
29b31577 FS |
49 | static const char *ssh_verify_args[] = { NULL }; |
50 | static const char *ssh_sigs[] = { | |
51 | "-----BEGIN SSH SIGNATURE-----", | |
52 | NULL | |
53 | }; | |
54 | ||
b5726a5d FS |
55 | static int verify_gpg_signed_buffer(struct signature_check *sigc, |
56 | struct gpg_format *fmt, const char *payload, | |
57 | size_t payload_size, const char *signature, | |
58 | size_t signature_size); | |
facca53a FS |
59 | static int verify_ssh_signed_buffer(struct signature_check *sigc, |
60 | struct gpg_format *fmt, const char *payload, | |
61 | size_t payload_size, const char *signature, | |
62 | size_t signature_size); | |
b5726a5d FS |
63 | static int sign_buffer_gpg(struct strbuf *buffer, struct strbuf *signature, |
64 | const char *signing_key); | |
29b31577 FS |
65 | static int sign_buffer_ssh(struct strbuf *buffer, struct strbuf *signature, |
66 | const char *signing_key); | |
b5726a5d | 67 | |
fd9e2267 FS |
68 | static const char *get_default_ssh_signing_key(void); |
69 | ||
4838f62c FS |
70 | static const char *get_ssh_key_id(void); |
71 | ||
58af57e1 | 72 | static struct gpg_format gpg_format[] = { |
b5726a5d FS |
73 | { |
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, | |
fd9e2267 | 80 | .get_default_key = NULL, |
4838f62c | 81 | .get_key_id = NULL, |
58af57e1 | 82 | }, |
b5726a5d FS |
83 | { |
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, | |
fd9e2267 | 90 | .get_default_key = NULL, |
4838f62c | 91 | .get_key_id = NULL, |
1e7adb97 | 92 | }, |
29b31577 FS |
93 | { |
94 | .name = "ssh", | |
95 | .program = "ssh-keygen", | |
96 | .verify_args = ssh_verify_args, | |
97 | .sigs = ssh_sigs, | |
facca53a | 98 | .verify_signed_buffer = verify_ssh_signed_buffer, |
fd9e2267 FS |
99 | .sign_buffer = sign_buffer_ssh, |
100 | .get_default_key = get_default_ssh_signing_key, | |
4838f62c | 101 | .get_key_id = get_ssh_key_id, |
29b31577 | 102 | }, |
58af57e1 HS |
103 | }; |
104 | ||
105 | static struct gpg_format *use_format = &gpg_format[0]; | |
2f47eae2 | 106 | |
58af57e1 HS |
107 | static 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 | ||
117 | static 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 | } | |
d7c67668 | 127 | |
01e57b5d MG |
128 | void signature_check_clear(struct signature_check *sigc) |
129 | { | |
88ce3ef6 | 130 | FREE_AND_NULL(sigc->payload); |
b5726a5d | 131 | FREE_AND_NULL(sigc->output); |
88ce3ef6 ÆAB |
132 | FREE_AND_NULL(sigc->gpg_status); |
133 | FREE_AND_NULL(sigc->signer); | |
134 | FREE_AND_NULL(sigc->key); | |
3daaaabe | 135 | FREE_AND_NULL(sigc->fingerprint); |
4de9394d | 136 | FREE_AND_NULL(sigc->primary_key_fingerprint); |
01e57b5d MG |
137 | } |
138 | ||
da6cf1b3 MG |
139 | /* An exclusive status -- only one of them can appear in output */ |
140 | #define GPG_STATUS_EXCLUSIVE (1<<0) | |
0b11a84e MG |
141 | /* 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) | |
3daaaabe MG |
145 | /* The status includes key fingerprints */ |
146 | #define GPG_STATUS_FINGERPRINT (1<<3) | |
54887b46 HJI |
147 | /* The status includes trust level */ |
148 | #define GPG_STATUS_TRUST_LEVEL (1<<4) | |
0b11a84e MG |
149 | |
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) | |
da6cf1b3 | 152 | |
a50e7ca3 JH |
153 | static struct { |
154 | char result; | |
155 | const char *check; | |
da6cf1b3 | 156 | unsigned int flags; |
a50e7ca3 | 157 | } sigcheck_gpg_status[] = { |
0b11a84e MG |
158 | { 'G', "GOODSIG ", GPG_STATUS_STDSIG }, |
159 | { 'B', "BADSIG ", GPG_STATUS_STDSIG }, | |
0b11a84e MG |
160 | { '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 }, | |
3daaaabe | 164 | { 0, "VALIDSIG ", GPG_STATUS_FINGERPRINT }, |
54887b46 HJI |
165 | { 0, "TRUST_", GPG_STATUS_TRUST_LEVEL }, |
166 | }; | |
167 | ||
168 | static 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 }, | |
a50e7ca3 JH |
177 | }; |
178 | ||
392b862e HJI |
179 | static 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 | ||
54887b46 HJI |
189 | static 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 | ||
fbd0f166 | 203 | static void parse_gpg_output(struct signature_check *sigc) |
a50e7ca3 JH |
204 | { |
205 | const char *buf = sigc->gpg_status; | |
da6cf1b3 | 206 | const char *line, *next; |
4de9394d | 207 | int i, j; |
da6cf1b3 MG |
208 | int seen_exclusive_status = 0; |
209 | ||
210 | /* Iterate over all lines */ | |
211 | for (line = buf; *line; line = strchrnul(line+1, '\n')) { | |
212 | while (*line == '\n') | |
213 | line++; | |
64c45dc7 SR |
214 | if (!*line) |
215 | break; | |
216 | ||
da6cf1b3 MG |
217 | /* Skip lines that don't start with GNUPG status */ |
218 | if (!skip_prefix(line, "[GNUPG:] ", &line)) | |
219 | continue; | |
220 | ||
221 | /* 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)) { | |
54887b46 HJI |
224 | /* |
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 | */ | |
da6cf1b3 | 233 | if (sigcheck_gpg_status[i].flags & GPG_STATUS_EXCLUSIVE) { |
02561896 | 234 | if (seen_exclusive_status++) |
54887b46 | 235 | goto error; |
da6cf1b3 MG |
236 | } |
237 | ||
3daaaabe MG |
238 | if (sigcheck_gpg_status[i].result) |
239 | sigc->result = sigcheck_gpg_status[i].result; | |
0b11a84e MG |
240 | /* Do we have key information? */ |
241 | if (sigcheck_gpg_status[i].flags & GPG_STATUS_KEYID) { | |
da6cf1b3 | 242 | next = strchrnul(line, ' '); |
392b862e | 243 | replace_cstring(&sigc->key, line, next); |
0b11a84e MG |
244 | /* Do we have signer information? */ |
245 | if (*next && (sigcheck_gpg_status[i].flags & GPG_STATUS_UID)) { | |
da6cf1b3 MG |
246 | line = next + 1; |
247 | next = strchrnul(line, '\n'); | |
392b862e | 248 | replace_cstring(&sigc->signer, line, next); |
da6cf1b3 MG |
249 | } |
250 | } | |
54887b46 HJI |
251 | |
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 | ||
3daaaabe MG |
270 | /* Do we have fingerprint? */ |
271 | if (sigcheck_gpg_status[i].flags & GPG_STATUS_FINGERPRINT) { | |
67a6ea63 HJI |
272 | const char *limit; |
273 | char **field; | |
274 | ||
3daaaabe | 275 | next = strchrnul(line, ' '); |
392b862e | 276 | replace_cstring(&sigc->fingerprint, line, next); |
4de9394d | 277 | |
67a6ea63 HJI |
278 | /* |
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'); | |
4de9394d | 285 | for (j = 9; j > 0; j--) { |
67a6ea63 | 286 | if (!*next || limit <= next) |
4de9394d MG |
287 | break; |
288 | line = next + 1; | |
289 | next = strchrnul(line, ' '); | |
290 | } | |
291 | ||
67a6ea63 HJI |
292 | 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 | } | |
3daaaabe | 299 | } |
da6cf1b3 MG |
300 | |
301 | break; | |
661a1806 | 302 | } |
a50e7ca3 JH |
303 | } |
304 | } | |
da6cf1b3 MG |
305 | return; |
306 | ||
54887b46 | 307 | error: |
da6cf1b3 MG |
308 | sigc->result = 'E'; |
309 | /* Clear partial data to avoid confusion */ | |
4de9394d | 310 | FREE_AND_NULL(sigc->primary_key_fingerprint); |
3daaaabe | 311 | FREE_AND_NULL(sigc->fingerprint); |
da6cf1b3 MG |
312 | FREE_AND_NULL(sigc->signer); |
313 | FREE_AND_NULL(sigc->key); | |
a50e7ca3 JH |
314 | } |
315 | ||
b5726a5d FS |
316 | static int verify_gpg_signed_buffer(struct signature_check *sigc, |
317 | struct gpg_format *fmt, const char *payload, | |
318 | size_t payload_size, const char *signature, | |
319 | size_t signature_size) | |
67948981 HJI |
320 | { |
321 | struct child_process gpg = CHILD_PROCESS_INIT; | |
67948981 HJI |
322 | struct tempfile *temp; |
323 | int ret; | |
b5726a5d FS |
324 | struct strbuf gpg_stdout = STRBUF_INIT; |
325 | struct strbuf gpg_stderr = STRBUF_INIT; | |
67948981 HJI |
326 | |
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 | ||
ef8d7ac4 JK |
338 | strvec_push(&gpg.args, fmt->program); |
339 | strvec_pushv(&gpg.args, fmt->verify_args); | |
340 | strvec_pushl(&gpg.args, | |
f6d8942b JK |
341 | "--status-fd=1", |
342 | "--verify", temp->filename.buf, "-", | |
343 | NULL); | |
67948981 | 344 | |
67948981 | 345 | sigchain_push(SIGPIPE, SIG_IGN); |
b5726a5d FS |
346 | ret = pipe_command(&gpg, payload, payload_size, &gpg_stdout, 0, |
347 | &gpg_stderr, 0); | |
67948981 HJI |
348 | sigchain_pop(SIGPIPE); |
349 | ||
350 | delete_tempfile(&temp); | |
351 | ||
b5726a5d FS |
352 | ret |= !strstr(gpg_stdout.buf, "\n[GNUPG:] GOODSIG "); |
353 | sigc->payload = xmemdupz(payload, payload_size); | |
354 | sigc->output = strbuf_detach(&gpg_stderr, NULL); | |
355 | sigc->gpg_status = strbuf_detach(&gpg_stdout, NULL); | |
356 | ||
357 | parse_gpg_output(sigc); | |
358 | ||
359 | strbuf_release(&gpg_stdout); | |
360 | strbuf_release(&gpg_stderr); | |
67948981 HJI |
361 | |
362 | return ret; | |
363 | } | |
364 | ||
facca53a FS |
365 | static void parse_ssh_output(struct signature_check *sigc) |
366 | { | |
367 | const char *line, *principal, *search; | |
368 | 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 | ||
386 | line = xmemdupz(sigc->output, strcspn(sigc->output, "\n")); | |
387 | ||
388 | if (skip_prefix(line, "Good \"git\" signature for ", &line)) { | |
389 | /* Valid signature and known principal */ | |
390 | sigc->result = 'G'; | |
391 | sigc->trust_level = TRUST_FULLY; | |
392 | ||
393 | /* Search for the last "with" to get the full principal */ | |
394 | principal = line; | |
395 | do { | |
396 | search = strstr(line, " with "); | |
397 | if (search) | |
398 | line = search + 1; | |
399 | } while (search != NULL); | |
400 | sigc->signer = xmemdupz(principal, line - principal - 1); | |
401 | } else if (skip_prefix(line, "Good \"git\" signature with ", &line)) { | |
402 | /* Valid signature, but key unknown */ | |
403 | sigc->result = 'G'; | |
404 | sigc->trust_level = TRUST_UNDEFINED; | |
405 | } else { | |
406 | return; | |
407 | } | |
408 | ||
409 | key = strstr(line, "key"); | |
410 | if (key) { | |
411 | sigc->fingerprint = xstrdup(strstr(line, "key") + 4); | |
412 | sigc->key = xstrdup(sigc->fingerprint); | |
413 | } else { | |
414 | /* | |
415 | * Output did not match what we expected | |
416 | * Treat the signature as bad | |
417 | */ | |
418 | sigc->result = 'B'; | |
419 | } | |
420 | } | |
421 | ||
422 | static int verify_ssh_signed_buffer(struct signature_check *sigc, | |
423 | struct gpg_format *fmt, const char *payload, | |
424 | size_t payload_size, const char *signature, | |
425 | size_t signature_size) | |
426 | { | |
427 | struct child_process ssh_keygen = CHILD_PROCESS_INIT; | |
428 | struct tempfile *buffer_file; | |
429 | int ret = -1; | |
430 | const char *line; | |
431 | size_t trust_size; | |
432 | char *principal; | |
433 | struct strbuf ssh_principals_out = STRBUF_INIT; | |
434 | struct strbuf ssh_principals_err = STRBUF_INIT; | |
435 | struct strbuf ssh_keygen_out = STRBUF_INIT; | |
436 | struct strbuf ssh_keygen_err = STRBUF_INIT; | |
437 | ||
438 | if (!ssh_allowed_signers) { | |
439 | error(_("gpg.ssh.allowedSignersFile needs to be configured and exist for ssh signature verification")); | |
440 | return -1; | |
441 | } | |
442 | ||
443 | buffer_file = mks_tempfile_t(".git_vtag_tmpXXXXXX"); | |
444 | if (!buffer_file) | |
445 | return error_errno(_("could not create temporary file")); | |
446 | if (write_in_full(buffer_file->fd, signature, signature_size) < 0 || | |
447 | close_tempfile_gently(buffer_file) < 0) { | |
448 | error_errno(_("failed writing detached signature to '%s'"), | |
449 | buffer_file->filename.buf); | |
450 | delete_tempfile(&buffer_file); | |
451 | return -1; | |
452 | } | |
453 | ||
454 | /* Find the principal from the signers */ | |
455 | strvec_pushl(&ssh_keygen.args, fmt->program, | |
456 | "-Y", "find-principals", | |
457 | "-f", ssh_allowed_signers, | |
458 | "-s", buffer_file->filename.buf, | |
459 | NULL); | |
460 | ret = pipe_command(&ssh_keygen, NULL, 0, &ssh_principals_out, 0, | |
461 | &ssh_principals_err, 0); | |
462 | if (ret && strstr(ssh_principals_err.buf, "usage:")) { | |
463 | error(_("ssh-keygen -Y find-principals/verify is needed for ssh signature verification (available in openssh version 8.2p1+)")); | |
464 | goto out; | |
465 | } | |
466 | if (ret || !ssh_principals_out.len) { | |
467 | /* | |
468 | * We did not find a matching principal in the allowedSigners | |
469 | * Check without validation | |
470 | */ | |
471 | child_process_init(&ssh_keygen); | |
472 | strvec_pushl(&ssh_keygen.args, fmt->program, | |
473 | "-Y", "check-novalidate", | |
474 | "-n", "git", | |
475 | "-s", buffer_file->filename.buf, | |
476 | NULL); | |
477 | pipe_command(&ssh_keygen, payload, payload_size, | |
478 | &ssh_keygen_out, 0, &ssh_keygen_err, 0); | |
479 | ||
480 | /* | |
481 | * Fail on unknown keys | |
482 | * we still call check-novalidate to display the signature info | |
483 | */ | |
484 | ret = -1; | |
485 | } else { | |
486 | /* Check every principal we found (one per line) */ | |
487 | for (line = ssh_principals_out.buf; *line; | |
488 | line = strchrnul(line + 1, '\n')) { | |
489 | while (*line == '\n') | |
490 | line++; | |
491 | if (!*line) | |
492 | break; | |
493 | ||
494 | trust_size = strcspn(line, "\n"); | |
495 | principal = xmemdupz(line, trust_size); | |
496 | ||
497 | child_process_init(&ssh_keygen); | |
498 | strbuf_release(&ssh_keygen_out); | |
499 | strbuf_release(&ssh_keygen_err); | |
500 | strvec_push(&ssh_keygen.args, fmt->program); | |
501 | /* | |
502 | * We found principals | |
503 | * Try with each until we find a match | |
504 | */ | |
505 | strvec_pushl(&ssh_keygen.args, "-Y", "verify", | |
506 | "-n", "git", | |
507 | "-f", ssh_allowed_signers, | |
508 | "-I", principal, | |
509 | "-s", buffer_file->filename.buf, | |
510 | NULL); | |
511 | ||
512 | if (ssh_revocation_file) { | |
513 | if (file_exists(ssh_revocation_file)) { | |
514 | strvec_pushl(&ssh_keygen.args, "-r", | |
515 | ssh_revocation_file, NULL); | |
516 | } else { | |
517 | warning(_("ssh signing revocation file configured but not found: %s"), | |
518 | ssh_revocation_file); | |
519 | } | |
520 | } | |
521 | ||
522 | sigchain_push(SIGPIPE, SIG_IGN); | |
523 | ret = pipe_command(&ssh_keygen, payload, payload_size, | |
524 | &ssh_keygen_out, 0, &ssh_keygen_err, 0); | |
525 | sigchain_pop(SIGPIPE); | |
526 | ||
527 | FREE_AND_NULL(principal); | |
528 | ||
529 | if (!ret) | |
530 | ret = !starts_with(ssh_keygen_out.buf, "Good"); | |
531 | ||
532 | if (!ret) | |
533 | break; | |
534 | } | |
535 | } | |
536 | ||
537 | sigc->payload = xmemdupz(payload, payload_size); | |
538 | strbuf_stripspace(&ssh_keygen_out, 0); | |
539 | strbuf_stripspace(&ssh_keygen_err, 0); | |
540 | /* Add stderr outputs to show the user actual ssh-keygen errors */ | |
541 | strbuf_add(&ssh_keygen_out, ssh_principals_err.buf, ssh_principals_err.len); | |
542 | strbuf_add(&ssh_keygen_out, ssh_keygen_err.buf, ssh_keygen_err.len); | |
543 | sigc->output = strbuf_detach(&ssh_keygen_out, NULL); | |
544 | sigc->gpg_status = xstrdup(sigc->output); | |
545 | ||
546 | parse_ssh_output(sigc); | |
547 | ||
548 | out: | |
549 | if (buffer_file) | |
550 | delete_tempfile(&buffer_file); | |
551 | strbuf_release(&ssh_principals_out); | |
552 | strbuf_release(&ssh_principals_err); | |
553 | strbuf_release(&ssh_keygen_out); | |
554 | strbuf_release(&ssh_keygen_err); | |
555 | ||
556 | return ret; | |
557 | } | |
558 | ||
434060ec | 559 | int check_signature(const char *payload, size_t plen, const char *signature, |
a4cc18f2 | 560 | size_t slen, struct signature_check *sigc) |
561 | { | |
b5726a5d | 562 | struct gpg_format *fmt; |
a4cc18f2 | 563 | int status; |
564 | ||
565 | sigc->result = 'N'; | |
54887b46 | 566 | sigc->trust_level = -1; |
a4cc18f2 | 567 | |
b5726a5d FS |
568 | fmt = get_format_by_sig(signature); |
569 | if (!fmt) | |
570 | die(_("bad/incompatible signature '%s'"), signature); | |
571 | ||
572 | status = fmt->verify_signed_buffer(sigc, fmt, payload, plen, signature, | |
573 | slen); | |
574 | ||
575 | if (status && !sigc->output) | |
576 | return !!status; | |
577 | ||
54887b46 HJI |
578 | status |= sigc->result != 'G'; |
579 | status |= sigc->trust_level < configured_min_trust_level; | |
a4cc18f2 | 580 | |
4e5dc9ca | 581 | return !!status; |
a4cc18f2 | 582 | } |
583 | ||
ca194d50 | 584 | void print_signature_buffer(const struct signature_check *sigc, unsigned flags) |
585 | { | |
b5726a5d FS |
586 | const char *output = flags & GPG_VERIFY_RAW ? sigc->gpg_status : |
587 | sigc->output; | |
aeff29dd | 588 | |
ca194d50 | 589 | if (flags & GPG_VERIFY_VERBOSE && sigc->payload) |
590 | fputs(sigc->payload, stdout); | |
591 | ||
aeff29dd | 592 | if (output) |
593 | fputs(output, stderr); | |
ca194d50 | 594 | } |
595 | ||
482c1191 | 596 | size_t parse_signed_buffer(const char *buf, size_t size) |
d7c67668 | 597 | { |
d7c67668 | 598 | size_t len = 0; |
8b44b2be JK |
599 | size_t match = size; |
600 | while (len < size) { | |
601 | const char *eol; | |
602 | ||
58af57e1 | 603 | if (get_format_by_sig(buf + len)) |
8b44b2be JK |
604 | match = len; |
605 | ||
606 | eol = memchr(buf + len, '\n', size - len); | |
d7c67668 JH |
607 | len += eol ? eol - (buf + len) + 1 : size - len; |
608 | } | |
8b44b2be | 609 | return match; |
d7c67668 JH |
610 | } |
611 | ||
482c1191 | 612 | int parse_signature(const char *buf, size_t size, struct strbuf *payload, struct strbuf *signature) |
613 | { | |
614 | size_t match = parse_signed_buffer(buf, size); | |
615 | if (match != size) { | |
616 | strbuf_add(payload, buf, match); | |
9b27b492 | 617 | remove_signature(payload); |
482c1191 | 618 | strbuf_add(signature, buf + match, size - match); |
619 | return 1; | |
620 | } | |
621 | return 0; | |
622 | } | |
623 | ||
2f47eae2 JH |
624 | void set_signing_key(const char *key) |
625 | { | |
626 | free(configured_signing_key); | |
627 | configured_signing_key = xstrdup(key); | |
628 | } | |
629 | ||
630 | int git_gpg_config(const char *var, const char *value, void *cb) | |
631 | { | |
58af57e1 HS |
632 | struct gpg_format *fmt = NULL; |
633 | char *fmtname = NULL; | |
54887b46 HJI |
634 | char *trust; |
635 | int ret; | |
58af57e1 | 636 | |
2f47eae2 | 637 | if (!strcmp(var, "user.signingkey")) { |
1b0eeec3 JK |
638 | if (!value) |
639 | return config_error_nonbool(var); | |
0c5e70f0 | 640 | set_signing_key(value); |
1b0eeec3 | 641 | return 0; |
0c5e70f0 | 642 | } |
1b0eeec3 | 643 | |
57a8dd75 HS |
644 | if (!strcmp(var, "gpg.format")) { |
645 | if (!value) | |
646 | return config_error_nonbool(var); | |
58af57e1 HS |
647 | fmt = get_format_by_name(value); |
648 | if (!fmt) | |
57a8dd75 HS |
649 | return error("unsupported value for %s: %s", |
650 | var, value); | |
58af57e1 HS |
651 | use_format = fmt; |
652 | return 0; | |
57a8dd75 HS |
653 | } |
654 | ||
54887b46 HJI |
655 | if (!strcmp(var, "gpg.mintrustlevel")) { |
656 | if (!value) | |
657 | return config_error_nonbool(var); | |
658 | ||
659 | trust = xstrdup_toupper(value); | |
660 | ret = parse_gpg_trust_level(trust, &configured_min_trust_level); | |
661 | free(trust); | |
662 | ||
663 | if (ret) | |
664 | return error("unsupported value for %s: %s", var, | |
665 | value); | |
666 | return 0; | |
667 | } | |
668 | ||
fd9e2267 FS |
669 | if (!strcmp(var, "gpg.ssh.defaultkeycommand")) { |
670 | if (!value) | |
671 | return config_error_nonbool(var); | |
672 | return git_config_string(&ssh_default_key_command, var, value); | |
673 | } | |
674 | ||
facca53a FS |
675 | if (!strcmp(var, "gpg.ssh.allowedsignersfile")) { |
676 | if (!value) | |
677 | return config_error_nonbool(var); | |
678 | return git_config_pathname(&ssh_allowed_signers, var, value); | |
679 | } | |
680 | ||
681 | if (!strcmp(var, "gpg.ssh.revocationfile")) { | |
682 | if (!value) | |
683 | return config_error_nonbool(var); | |
684 | return git_config_pathname(&ssh_revocation_file, var, value); | |
685 | } | |
686 | ||
b02f51b1 | 687 | if (!strcmp(var, "gpg.program") || !strcmp(var, "gpg.openpgp.program")) |
58af57e1 HS |
688 | fmtname = "openpgp"; |
689 | ||
1e7adb97 HS |
690 | if (!strcmp(var, "gpg.x509.program")) |
691 | fmtname = "x509"; | |
692 | ||
29b31577 FS |
693 | if (!strcmp(var, "gpg.ssh.program")) |
694 | fmtname = "ssh"; | |
695 | ||
58af57e1 HS |
696 | if (fmtname) { |
697 | fmt = get_format_by_name(fmtname); | |
698 | return git_config_string(&fmt->program, var, value); | |
2f47eae2 | 699 | } |
1b0eeec3 | 700 | |
2f47eae2 JH |
701 | return 0; |
702 | } | |
703 | ||
4838f62c FS |
704 | static char *get_ssh_key_fingerprint(const char *signing_key) |
705 | { | |
706 | struct child_process ssh_keygen = CHILD_PROCESS_INIT; | |
707 | int ret = -1; | |
708 | struct strbuf fingerprint_stdout = STRBUF_INIT; | |
709 | struct strbuf **fingerprint; | |
710 | ||
711 | /* | |
712 | * With SSH Signing this can contain a filename or a public key | |
713 | * For textual representation we usually want a fingerprint | |
714 | */ | |
715 | if (starts_with(signing_key, "ssh-")) { | |
716 | strvec_pushl(&ssh_keygen.args, "ssh-keygen", "-lf", "-", NULL); | |
717 | ret = pipe_command(&ssh_keygen, signing_key, | |
718 | strlen(signing_key), &fingerprint_stdout, 0, | |
719 | NULL, 0); | |
720 | } else { | |
721 | strvec_pushl(&ssh_keygen.args, "ssh-keygen", "-lf", | |
722 | configured_signing_key, NULL); | |
723 | ret = pipe_command(&ssh_keygen, NULL, 0, &fingerprint_stdout, 0, | |
724 | NULL, 0); | |
725 | } | |
726 | ||
727 | if (!!ret) | |
728 | die_errno(_("failed to get the ssh fingerprint for key '%s'"), | |
729 | signing_key); | |
730 | ||
731 | fingerprint = strbuf_split_max(&fingerprint_stdout, ' ', 3); | |
732 | if (!fingerprint[1]) | |
733 | die_errno(_("failed to get the ssh fingerprint for key '%s'"), | |
734 | signing_key); | |
735 | ||
736 | return strbuf_detach(fingerprint[1], NULL); | |
737 | } | |
738 | ||
fd9e2267 FS |
739 | /* Returns the first public key from an ssh-agent to use for signing */ |
740 | static const char *get_default_ssh_signing_key(void) | |
741 | { | |
742 | struct child_process ssh_default_key = CHILD_PROCESS_INIT; | |
743 | int ret = -1; | |
744 | struct strbuf key_stdout = STRBUF_INIT, key_stderr = STRBUF_INIT; | |
745 | struct strbuf **keys; | |
746 | char *key_command = NULL; | |
747 | const char **argv; | |
748 | int n; | |
749 | char *default_key = NULL; | |
750 | ||
751 | if (!ssh_default_key_command) | |
752 | die(_("either user.signingkey or gpg.ssh.defaultKeyCommand needs to be configured")); | |
753 | ||
754 | key_command = xstrdup(ssh_default_key_command); | |
755 | n = split_cmdline(key_command, &argv); | |
756 | ||
757 | if (n < 0) | |
758 | die("malformed build-time gpg.ssh.defaultKeyCommand: %s", | |
759 | split_cmdline_strerror(n)); | |
760 | ||
761 | strvec_pushv(&ssh_default_key.args, argv); | |
762 | ret = pipe_command(&ssh_default_key, NULL, 0, &key_stdout, 0, | |
763 | &key_stderr, 0); | |
764 | ||
765 | if (!ret) { | |
766 | keys = strbuf_split_max(&key_stdout, '\n', 2); | |
767 | if (keys[0] && starts_with(keys[0]->buf, "ssh-")) { | |
768 | default_key = strbuf_detach(keys[0], NULL); | |
769 | } else { | |
770 | warning(_("gpg.ssh.defaultKeycommand succeeded but returned no keys: %s %s"), | |
771 | key_stderr.buf, key_stdout.buf); | |
772 | } | |
773 | ||
774 | strbuf_list_free(keys); | |
775 | } else { | |
776 | warning(_("gpg.ssh.defaultKeyCommand failed: %s %s"), | |
777 | key_stderr.buf, key_stdout.buf); | |
778 | } | |
779 | ||
780 | free(key_command); | |
781 | free(argv); | |
782 | strbuf_release(&key_stdout); | |
783 | ||
784 | return default_key; | |
785 | } | |
786 | ||
4838f62c FS |
787 | static const char *get_ssh_key_id(void) { |
788 | return get_ssh_key_fingerprint(get_signing_key()); | |
789 | } | |
790 | ||
791 | /* Returns a textual but unique representation of the signing key */ | |
792 | const char *get_signing_key_id(void) | |
793 | { | |
794 | if (use_format->get_key_id) { | |
795 | return use_format->get_key_id(); | |
796 | } | |
797 | ||
798 | /* GPG/GPGSM only store a key id on this variable */ | |
799 | return get_signing_key(); | |
800 | } | |
801 | ||
2f47eae2 JH |
802 | const char *get_signing_key(void) |
803 | { | |
804 | if (configured_signing_key) | |
805 | return configured_signing_key; | |
fd9e2267 FS |
806 | if (use_format->get_default_key) { |
807 | return use_format->get_default_key(); | |
808 | } | |
809 | ||
810 | return git_committer_info(IDENT_STRICT | IDENT_NO_DATE); | |
2f47eae2 JH |
811 | } |
812 | ||
2f47eae2 | 813 | int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *signing_key) |
b5726a5d FS |
814 | { |
815 | return use_format->sign_buffer(buffer, signature, signing_key); | |
816 | } | |
817 | ||
29b31577 FS |
818 | /* |
819 | * Strip CR from the line endings, in case we are on Windows. | |
820 | * NEEDSWORK: make it trim only CRs before LFs and rename | |
821 | */ | |
822 | static void remove_cr_after(struct strbuf *buffer, size_t offset) | |
823 | { | |
824 | size_t i, j; | |
825 | ||
826 | for (i = j = offset; i < buffer->len; i++) { | |
827 | if (buffer->buf[i] != '\r') { | |
828 | if (i != j) | |
829 | buffer->buf[j] = buffer->buf[i]; | |
830 | j++; | |
831 | } | |
832 | } | |
833 | strbuf_setlen(buffer, j); | |
834 | } | |
835 | ||
b5726a5d FS |
836 | static int sign_buffer_gpg(struct strbuf *buffer, struct strbuf *signature, |
837 | const char *signing_key) | |
2f47eae2 | 838 | { |
d3180279 | 839 | struct child_process gpg = CHILD_PROCESS_INIT; |
0581b546 | 840 | int ret; |
29b31577 | 841 | size_t bottom; |
efee9553 | 842 | struct strbuf gpg_status = STRBUF_INIT; |
2f47eae2 | 843 | |
ef8d7ac4 | 844 | strvec_pushl(&gpg.args, |
f6d8942b JK |
845 | use_format->program, |
846 | "--status-fd=2", | |
847 | "-bsau", signing_key, | |
848 | NULL); | |
2f47eae2 | 849 | |
0581b546 | 850 | bottom = signature->len; |
2f47eae2 JH |
851 | |
852 | /* | |
853 | * When the username signingkey is bad, program could be terminated | |
854 | * because gpg exits without reading and then write gets SIGPIPE. | |
855 | */ | |
856 | sigchain_push(SIGPIPE, SIG_IGN); | |
0581b546 | 857 | ret = pipe_command(&gpg, buffer->buf, buffer->len, |
efee9553 | 858 | signature, 1024, &gpg_status, 0); |
2f47eae2 JH |
859 | sigchain_pop(SIGPIPE); |
860 | ||
efee9553 MG |
861 | ret |= !strstr(gpg_status.buf, "\n[GNUPG:] SIG_CREATED "); |
862 | strbuf_release(&gpg_status); | |
863 | if (ret) | |
2f47eae2 JH |
864 | return error(_("gpg failed to sign the data")); |
865 | ||
866 | /* Strip CR from the line endings, in case we are on Windows. */ | |
29b31577 | 867 | remove_cr_after(signature, bottom); |
2f47eae2 JH |
868 | |
869 | return 0; | |
870 | } | |
29b31577 FS |
871 | |
872 | static int sign_buffer_ssh(struct strbuf *buffer, struct strbuf *signature, | |
873 | const char *signing_key) | |
874 | { | |
875 | struct child_process signer = CHILD_PROCESS_INIT; | |
876 | int ret = -1; | |
877 | size_t bottom, keylen; | |
878 | struct strbuf signer_stderr = STRBUF_INIT; | |
879 | struct tempfile *key_file = NULL, *buffer_file = NULL; | |
880 | char *ssh_signing_key_file = NULL; | |
881 | struct strbuf ssh_signature_filename = STRBUF_INIT; | |
882 | ||
883 | if (!signing_key || signing_key[0] == '\0') | |
884 | return error( | |
885 | _("user.signingkey needs to be set for ssh signing")); | |
886 | ||
887 | if (starts_with(signing_key, "ssh-")) { | |
888 | /* A literal ssh key */ | |
889 | key_file = mks_tempfile_t(".git_signing_key_tmpXXXXXX"); | |
890 | if (!key_file) | |
891 | return error_errno( | |
892 | _("could not create temporary file")); | |
893 | keylen = strlen(signing_key); | |
894 | if (write_in_full(key_file->fd, signing_key, keylen) < 0 || | |
895 | close_tempfile_gently(key_file) < 0) { | |
896 | error_errno(_("failed writing ssh signing key to '%s'"), | |
897 | key_file->filename.buf); | |
898 | goto out; | |
899 | } | |
900 | ssh_signing_key_file = strbuf_detach(&key_file->filename, NULL); | |
901 | } else { | |
902 | /* We assume a file */ | |
903 | ssh_signing_key_file = expand_user_path(signing_key, 1); | |
904 | } | |
905 | ||
906 | buffer_file = mks_tempfile_t(".git_signing_buffer_tmpXXXXXX"); | |
907 | if (!buffer_file) { | |
908 | error_errno(_("could not create temporary file")); | |
909 | goto out; | |
910 | } | |
911 | ||
912 | if (write_in_full(buffer_file->fd, buffer->buf, buffer->len) < 0 || | |
913 | close_tempfile_gently(buffer_file) < 0) { | |
914 | error_errno(_("failed writing ssh signing key buffer to '%s'"), | |
915 | buffer_file->filename.buf); | |
916 | goto out; | |
917 | } | |
918 | ||
919 | strvec_pushl(&signer.args, use_format->program, | |
920 | "-Y", "sign", | |
921 | "-n", "git", | |
922 | "-f", ssh_signing_key_file, | |
923 | buffer_file->filename.buf, | |
924 | NULL); | |
925 | ||
926 | sigchain_push(SIGPIPE, SIG_IGN); | |
927 | ret = pipe_command(&signer, NULL, 0, NULL, 0, &signer_stderr, 0); | |
928 | sigchain_pop(SIGPIPE); | |
929 | ||
930 | if (ret) { | |
931 | if (strstr(signer_stderr.buf, "usage:")) | |
932 | error(_("ssh-keygen -Y sign is needed for ssh signing (available in openssh version 8.2p1+)")); | |
933 | ||
934 | error("%s", signer_stderr.buf); | |
935 | goto out; | |
936 | } | |
937 | ||
938 | bottom = signature->len; | |
939 | ||
940 | strbuf_addbuf(&ssh_signature_filename, &buffer_file->filename); | |
941 | strbuf_addstr(&ssh_signature_filename, ".sig"); | |
942 | if (strbuf_read_file(signature, ssh_signature_filename.buf, 0) < 0) { | |
943 | error_errno( | |
944 | _("failed reading ssh signing data buffer from '%s'"), | |
945 | ssh_signature_filename.buf); | |
946 | } | |
947 | unlink_or_warn(ssh_signature_filename.buf); | |
948 | ||
949 | /* Strip CR from the line endings, in case we are on Windows. */ | |
950 | remove_cr_after(signature, bottom); | |
951 | ||
952 | out: | |
953 | if (key_file) | |
954 | delete_tempfile(&key_file); | |
955 | if (buffer_file) | |
956 | delete_tempfile(&buffer_file); | |
957 | strbuf_release(&signer_stderr); | |
958 | strbuf_release(&ssh_signature_filename); | |
959 | FREE_AND_NULL(ssh_signing_key_file); | |
960 | return ret; | |
961 | } |