]>
Commit | Line | Data |
---|---|---|
2f47eae2 | 1 | #include "cache.h" |
b2141fc1 | 2 | #include "config.h" |
2f47eae2 JH |
3 | #include "run-command.h" |
4 | #include "strbuf.h" | |
5 | #include "gpg-interface.h" | |
6 | #include "sigchain.h" | |
4322353b | 7 | #include "tempfile.h" |
2f47eae2 JH |
8 | |
9 | static char *configured_signing_key; | |
58af57e1 HS |
10 | struct gpg_format { |
11 | const char *name; | |
12 | const char *program; | |
13 | const char **verify_args; | |
14 | const char **sigs; | |
15 | }; | |
16 | ||
17 | static const char *openpgp_verify_args[] = { | |
18 | "--keyid-format=long", | |
19 | NULL | |
20 | }; | |
21 | static const char *openpgp_sigs[] = { | |
22 | "-----BEGIN PGP SIGNATURE-----", | |
23 | "-----BEGIN PGP MESSAGE-----", | |
24 | NULL | |
25 | }; | |
26 | ||
1e7adb97 HS |
27 | static const char *x509_verify_args[] = { |
28 | NULL | |
29 | }; | |
30 | static const char *x509_sigs[] = { | |
31 | "-----BEGIN SIGNED MESSAGE-----", | |
32 | NULL | |
33 | }; | |
34 | ||
58af57e1 HS |
35 | static struct gpg_format gpg_format[] = { |
36 | { .name = "openpgp", .program = "gpg", | |
37 | .verify_args = openpgp_verify_args, | |
38 | .sigs = openpgp_sigs | |
39 | }, | |
1e7adb97 HS |
40 | { .name = "x509", .program = "gpgsm", |
41 | .verify_args = x509_verify_args, | |
42 | .sigs = x509_sigs | |
43 | }, | |
58af57e1 HS |
44 | }; |
45 | ||
46 | static struct gpg_format *use_format = &gpg_format[0]; | |
2f47eae2 | 47 | |
58af57e1 HS |
48 | static struct gpg_format *get_format_by_name(const char *str) |
49 | { | |
50 | int i; | |
51 | ||
52 | for (i = 0; i < ARRAY_SIZE(gpg_format); i++) | |
53 | if (!strcmp(gpg_format[i].name, str)) | |
54 | return gpg_format + i; | |
55 | return NULL; | |
56 | } | |
57 | ||
58 | static struct gpg_format *get_format_by_sig(const char *sig) | |
59 | { | |
60 | int i, j; | |
61 | ||
62 | for (i = 0; i < ARRAY_SIZE(gpg_format); i++) | |
63 | for (j = 0; gpg_format[i].sigs[j]; j++) | |
64 | if (starts_with(sig, gpg_format[i].sigs[j])) | |
65 | return gpg_format + i; | |
66 | return NULL; | |
67 | } | |
d7c67668 | 68 | |
01e57b5d MG |
69 | void signature_check_clear(struct signature_check *sigc) |
70 | { | |
88ce3ef6 ÆAB |
71 | FREE_AND_NULL(sigc->payload); |
72 | FREE_AND_NULL(sigc->gpg_output); | |
73 | FREE_AND_NULL(sigc->gpg_status); | |
74 | FREE_AND_NULL(sigc->signer); | |
75 | FREE_AND_NULL(sigc->key); | |
3daaaabe | 76 | FREE_AND_NULL(sigc->fingerprint); |
4de9394d | 77 | FREE_AND_NULL(sigc->primary_key_fingerprint); |
01e57b5d MG |
78 | } |
79 | ||
da6cf1b3 MG |
80 | /* An exclusive status -- only one of them can appear in output */ |
81 | #define GPG_STATUS_EXCLUSIVE (1<<0) | |
0b11a84e MG |
82 | /* The status includes key identifier */ |
83 | #define GPG_STATUS_KEYID (1<<1) | |
84 | /* The status includes user identifier */ | |
85 | #define GPG_STATUS_UID (1<<2) | |
3daaaabe MG |
86 | /* The status includes key fingerprints */ |
87 | #define GPG_STATUS_FINGERPRINT (1<<3) | |
0b11a84e MG |
88 | |
89 | /* Short-hand for standard exclusive *SIG status with keyid & UID */ | |
90 | #define GPG_STATUS_STDSIG (GPG_STATUS_EXCLUSIVE|GPG_STATUS_KEYID|GPG_STATUS_UID) | |
da6cf1b3 | 91 | |
a50e7ca3 JH |
92 | static struct { |
93 | char result; | |
94 | const char *check; | |
da6cf1b3 | 95 | unsigned int flags; |
a50e7ca3 | 96 | } sigcheck_gpg_status[] = { |
0b11a84e MG |
97 | { 'G', "GOODSIG ", GPG_STATUS_STDSIG }, |
98 | { 'B', "BADSIG ", GPG_STATUS_STDSIG }, | |
da6cf1b3 MG |
99 | { 'U', "TRUST_NEVER", 0 }, |
100 | { 'U', "TRUST_UNDEFINED", 0 }, | |
0b11a84e MG |
101 | { 'E', "ERRSIG ", GPG_STATUS_EXCLUSIVE|GPG_STATUS_KEYID }, |
102 | { 'X', "EXPSIG ", GPG_STATUS_STDSIG }, | |
103 | { 'Y', "EXPKEYSIG ", GPG_STATUS_STDSIG }, | |
104 | { 'R', "REVKEYSIG ", GPG_STATUS_STDSIG }, | |
3daaaabe | 105 | { 0, "VALIDSIG ", GPG_STATUS_FINGERPRINT }, |
a50e7ca3 JH |
106 | }; |
107 | ||
392b862e HJI |
108 | static void replace_cstring(char **field, const char *line, const char *next) |
109 | { | |
110 | free(*field); | |
111 | ||
112 | if (line && next) | |
113 | *field = xmemdupz(line, next - line); | |
114 | else | |
115 | *field = NULL; | |
116 | } | |
117 | ||
fbd0f166 | 118 | static void parse_gpg_output(struct signature_check *sigc) |
a50e7ca3 JH |
119 | { |
120 | const char *buf = sigc->gpg_status; | |
da6cf1b3 | 121 | const char *line, *next; |
4de9394d | 122 | int i, j; |
da6cf1b3 MG |
123 | int seen_exclusive_status = 0; |
124 | ||
125 | /* Iterate over all lines */ | |
126 | for (line = buf; *line; line = strchrnul(line+1, '\n')) { | |
127 | while (*line == '\n') | |
128 | line++; | |
64c45dc7 SR |
129 | if (!*line) |
130 | break; | |
131 | ||
da6cf1b3 MG |
132 | /* Skip lines that don't start with GNUPG status */ |
133 | if (!skip_prefix(line, "[GNUPG:] ", &line)) | |
134 | continue; | |
135 | ||
136 | /* Iterate over all search strings */ | |
137 | for (i = 0; i < ARRAY_SIZE(sigcheck_gpg_status); i++) { | |
138 | if (skip_prefix(line, sigcheck_gpg_status[i].check, &line)) { | |
139 | if (sigcheck_gpg_status[i].flags & GPG_STATUS_EXCLUSIVE) { | |
02561896 | 140 | if (seen_exclusive_status++) |
da6cf1b3 MG |
141 | goto found_duplicate_status; |
142 | } | |
143 | ||
3daaaabe MG |
144 | if (sigcheck_gpg_status[i].result) |
145 | sigc->result = sigcheck_gpg_status[i].result; | |
0b11a84e MG |
146 | /* Do we have key information? */ |
147 | if (sigcheck_gpg_status[i].flags & GPG_STATUS_KEYID) { | |
da6cf1b3 | 148 | next = strchrnul(line, ' '); |
392b862e | 149 | replace_cstring(&sigc->key, line, next); |
0b11a84e MG |
150 | /* Do we have signer information? */ |
151 | if (*next && (sigcheck_gpg_status[i].flags & GPG_STATUS_UID)) { | |
da6cf1b3 MG |
152 | line = next + 1; |
153 | next = strchrnul(line, '\n'); | |
392b862e | 154 | replace_cstring(&sigc->signer, line, next); |
da6cf1b3 MG |
155 | } |
156 | } | |
3daaaabe MG |
157 | /* Do we have fingerprint? */ |
158 | if (sigcheck_gpg_status[i].flags & GPG_STATUS_FINGERPRINT) { | |
67a6ea63 HJI |
159 | const char *limit; |
160 | char **field; | |
161 | ||
3daaaabe | 162 | next = strchrnul(line, ' '); |
392b862e | 163 | replace_cstring(&sigc->fingerprint, line, next); |
4de9394d | 164 | |
67a6ea63 HJI |
165 | /* |
166 | * Skip interim fields. The search is | |
167 | * limited to the same line since only | |
168 | * OpenPGP signatures has a field with | |
169 | * the primary fingerprint. | |
170 | */ | |
171 | limit = strchrnul(line, '\n'); | |
4de9394d | 172 | for (j = 9; j > 0; j--) { |
67a6ea63 | 173 | if (!*next || limit <= next) |
4de9394d MG |
174 | break; |
175 | line = next + 1; | |
176 | next = strchrnul(line, ' '); | |
177 | } | |
178 | ||
67a6ea63 HJI |
179 | field = &sigc->primary_key_fingerprint; |
180 | if (!j) { | |
181 | next = strchrnul(line, '\n'); | |
182 | replace_cstring(field, line, next); | |
183 | } else { | |
184 | replace_cstring(field, NULL, NULL); | |
185 | } | |
3daaaabe | 186 | } |
da6cf1b3 MG |
187 | |
188 | break; | |
661a1806 | 189 | } |
a50e7ca3 JH |
190 | } |
191 | } | |
da6cf1b3 MG |
192 | return; |
193 | ||
194 | found_duplicate_status: | |
195 | /* | |
196 | * GOODSIG, BADSIG etc. can occur only once for each signature. | |
197 | * Therefore, if we had more than one then we're dealing with multiple | |
198 | * signatures. We don't support them currently, and they're rather | |
199 | * hard to create, so something is likely fishy and we should reject | |
200 | * them altogether. | |
201 | */ | |
202 | sigc->result = 'E'; | |
203 | /* Clear partial data to avoid confusion */ | |
4de9394d | 204 | FREE_AND_NULL(sigc->primary_key_fingerprint); |
3daaaabe | 205 | FREE_AND_NULL(sigc->fingerprint); |
da6cf1b3 MG |
206 | FREE_AND_NULL(sigc->signer); |
207 | FREE_AND_NULL(sigc->key); | |
a50e7ca3 JH |
208 | } |
209 | ||
434060ec | 210 | int check_signature(const char *payload, size_t plen, const char *signature, |
a4cc18f2 | 211 | size_t slen, struct signature_check *sigc) |
212 | { | |
213 | struct strbuf gpg_output = STRBUF_INIT; | |
214 | struct strbuf gpg_status = STRBUF_INIT; | |
215 | int status; | |
216 | ||
217 | sigc->result = 'N'; | |
218 | ||
219 | status = verify_signed_buffer(payload, plen, signature, slen, | |
220 | &gpg_output, &gpg_status); | |
221 | if (status && !gpg_output.len) | |
222 | goto out; | |
223 | sigc->payload = xmemdupz(payload, plen); | |
224 | sigc->gpg_output = strbuf_detach(&gpg_output, NULL); | |
225 | sigc->gpg_status = strbuf_detach(&gpg_status, NULL); | |
226 | parse_gpg_output(sigc); | |
4e5dc9ca | 227 | status |= sigc->result != 'G' && sigc->result != 'U'; |
a4cc18f2 | 228 | |
229 | out: | |
230 | strbuf_release(&gpg_status); | |
231 | strbuf_release(&gpg_output); | |
434060ec | 232 | |
4e5dc9ca | 233 | return !!status; |
a4cc18f2 | 234 | } |
235 | ||
ca194d50 | 236 | void print_signature_buffer(const struct signature_check *sigc, unsigned flags) |
237 | { | |
aeff29dd | 238 | const char *output = flags & GPG_VERIFY_RAW ? |
239 | sigc->gpg_status : sigc->gpg_output; | |
240 | ||
ca194d50 | 241 | if (flags & GPG_VERIFY_VERBOSE && sigc->payload) |
242 | fputs(sigc->payload, stdout); | |
243 | ||
aeff29dd | 244 | if (output) |
245 | fputs(output, stderr); | |
ca194d50 | 246 | } |
247 | ||
e6fa6cde | 248 | size_t parse_signature(const char *buf, size_t size) |
d7c67668 | 249 | { |
d7c67668 | 250 | size_t len = 0; |
8b44b2be JK |
251 | size_t match = size; |
252 | while (len < size) { | |
253 | const char *eol; | |
254 | ||
58af57e1 | 255 | if (get_format_by_sig(buf + len)) |
8b44b2be JK |
256 | match = len; |
257 | ||
258 | eol = memchr(buf + len, '\n', size - len); | |
d7c67668 JH |
259 | len += eol ? eol - (buf + len) + 1 : size - len; |
260 | } | |
8b44b2be | 261 | return match; |
d7c67668 JH |
262 | } |
263 | ||
2f47eae2 JH |
264 | void set_signing_key(const char *key) |
265 | { | |
266 | free(configured_signing_key); | |
267 | configured_signing_key = xstrdup(key); | |
268 | } | |
269 | ||
270 | int git_gpg_config(const char *var, const char *value, void *cb) | |
271 | { | |
58af57e1 HS |
272 | struct gpg_format *fmt = NULL; |
273 | char *fmtname = NULL; | |
274 | ||
2f47eae2 | 275 | if (!strcmp(var, "user.signingkey")) { |
1b0eeec3 JK |
276 | if (!value) |
277 | return config_error_nonbool(var); | |
0c5e70f0 | 278 | set_signing_key(value); |
1b0eeec3 | 279 | return 0; |
0c5e70f0 | 280 | } |
1b0eeec3 | 281 | |
57a8dd75 HS |
282 | if (!strcmp(var, "gpg.format")) { |
283 | if (!value) | |
284 | return config_error_nonbool(var); | |
58af57e1 HS |
285 | fmt = get_format_by_name(value); |
286 | if (!fmt) | |
57a8dd75 HS |
287 | return error("unsupported value for %s: %s", |
288 | var, value); | |
58af57e1 HS |
289 | use_format = fmt; |
290 | return 0; | |
57a8dd75 HS |
291 | } |
292 | ||
b02f51b1 | 293 | if (!strcmp(var, "gpg.program") || !strcmp(var, "gpg.openpgp.program")) |
58af57e1 HS |
294 | fmtname = "openpgp"; |
295 | ||
1e7adb97 HS |
296 | if (!strcmp(var, "gpg.x509.program")) |
297 | fmtname = "x509"; | |
298 | ||
58af57e1 HS |
299 | if (fmtname) { |
300 | fmt = get_format_by_name(fmtname); | |
301 | return git_config_string(&fmt->program, var, value); | |
2f47eae2 | 302 | } |
1b0eeec3 | 303 | |
2f47eae2 JH |
304 | return 0; |
305 | } | |
306 | ||
307 | const char *get_signing_key(void) | |
308 | { | |
309 | if (configured_signing_key) | |
310 | return configured_signing_key; | |
f9bc573f | 311 | return git_committer_info(IDENT_STRICT|IDENT_NO_DATE); |
2f47eae2 JH |
312 | } |
313 | ||
2f47eae2 JH |
314 | int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *signing_key) |
315 | { | |
d3180279 | 316 | struct child_process gpg = CHILD_PROCESS_INIT; |
0581b546 | 317 | int ret; |
2f47eae2 | 318 | size_t i, j, bottom; |
efee9553 | 319 | struct strbuf gpg_status = STRBUF_INIT; |
2f47eae2 | 320 | |
aedb5dc3 | 321 | argv_array_pushl(&gpg.args, |
58af57e1 | 322 | use_format->program, |
efee9553 | 323 | "--status-fd=2", |
aedb5dc3 JK |
324 | "-bsau", signing_key, |
325 | NULL); | |
2f47eae2 | 326 | |
0581b546 | 327 | bottom = signature->len; |
2f47eae2 JH |
328 | |
329 | /* | |
330 | * When the username signingkey is bad, program could be terminated | |
331 | * because gpg exits without reading and then write gets SIGPIPE. | |
332 | */ | |
333 | sigchain_push(SIGPIPE, SIG_IGN); | |
0581b546 | 334 | ret = pipe_command(&gpg, buffer->buf, buffer->len, |
efee9553 | 335 | signature, 1024, &gpg_status, 0); |
2f47eae2 JH |
336 | sigchain_pop(SIGPIPE); |
337 | ||
efee9553 MG |
338 | ret |= !strstr(gpg_status.buf, "\n[GNUPG:] SIG_CREATED "); |
339 | strbuf_release(&gpg_status); | |
340 | if (ret) | |
2f47eae2 JH |
341 | return error(_("gpg failed to sign the data")); |
342 | ||
343 | /* Strip CR from the line endings, in case we are on Windows. */ | |
344 | for (i = j = bottom; i < signature->len; i++) | |
345 | if (signature->buf[i] != '\r') { | |
346 | if (i != j) | |
347 | signature->buf[j] = signature->buf[i]; | |
348 | j++; | |
349 | } | |
350 | strbuf_setlen(signature, j); | |
351 | ||
352 | return 0; | |
353 | } | |
0106b1d4 JH |
354 | |
355 | int verify_signed_buffer(const char *payload, size_t payload_size, | |
356 | const char *signature, size_t signature_size, | |
357 | struct strbuf *gpg_output, struct strbuf *gpg_status) | |
358 | { | |
359 | struct child_process gpg = CHILD_PROCESS_INIT; | |
360 | struct gpg_format *fmt; | |
361 | struct tempfile *temp; | |
362 | int ret; | |
363 | struct strbuf buf = STRBUF_INIT; | |
364 | ||
365 | temp = mks_tempfile_t(".git_vtag_tmpXXXXXX"); | |
366 | if (!temp) | |
367 | return error_errno(_("could not create temporary file")); | |
368 | if (write_in_full(temp->fd, signature, signature_size) < 0 || | |
369 | close_tempfile_gently(temp) < 0) { | |
370 | error_errno(_("failed writing detached signature to '%s'"), | |
371 | temp->filename.buf); | |
372 | delete_tempfile(&temp); | |
373 | return -1; | |
374 | } | |
375 | ||
376 | fmt = get_format_by_sig(signature); | |
377 | if (!fmt) | |
378 | BUG("bad signature '%s'", signature); | |
379 | ||
380 | argv_array_push(&gpg.args, fmt->program); | |
381 | argv_array_pushv(&gpg.args, fmt->verify_args); | |
382 | argv_array_pushl(&gpg.args, | |
383 | "--status-fd=1", | |
384 | "--verify", temp->filename.buf, "-", | |
385 | NULL); | |
386 | ||
387 | if (!gpg_status) | |
388 | gpg_status = &buf; | |
389 | ||
390 | sigchain_push(SIGPIPE, SIG_IGN); | |
391 | ret = pipe_command(&gpg, payload, payload_size, | |
392 | gpg_status, 0, gpg_output, 0); | |
393 | sigchain_pop(SIGPIPE); | |
394 | ||
395 | delete_tempfile(&temp); | |
396 | ||
397 | ret |= !strstr(gpg_status->buf, "\n[GNUPG:] GOODSIG "); | |
398 | strbuf_release(&buf); /* no matter it was used or not */ | |
399 | ||
400 | return ret; | |
401 | } |