t3434: mark successful test as such
[git.git] / gpg-interface.c
blob5134ce27806866c41d5eda19cf01110cbf7849f2
1 #include "cache.h"
2 #include "config.h"
3 #include "run-command.h"
4 #include "strbuf.h"
5 #include "gpg-interface.h"
6 #include "sigchain.h"
7 #include "tempfile.h"
9 static char *configured_signing_key;
10 struct gpg_format {
11 const char *name;
12 const char *program;
13 const char **verify_args;
14 const char **sigs;
17 static const char *openpgp_verify_args[] = {
18 "--keyid-format=long",
19 NULL
21 static const char *openpgp_sigs[] = {
22 "-----BEGIN PGP SIGNATURE-----",
23 "-----BEGIN PGP MESSAGE-----",
24 NULL
27 static const char *x509_verify_args[] = {
28 NULL
30 static const char *x509_sigs[] = {
31 "-----BEGIN SIGNED MESSAGE-----",
32 NULL
35 static struct gpg_format gpg_format[] = {
36 { .name = "openpgp", .program = "gpg",
37 .verify_args = openpgp_verify_args,
38 .sigs = openpgp_sigs
40 { .name = "x509", .program = "gpgsm",
41 .verify_args = x509_verify_args,
42 .sigs = x509_sigs
46 static struct gpg_format *use_format = &gpg_format[0];
48 static struct gpg_format *get_format_by_name(const char *str)
50 int i;
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;
58 static struct gpg_format *get_format_by_sig(const char *sig)
60 int i, j;
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;
69 void signature_check_clear(struct signature_check *sigc)
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);
76 FREE_AND_NULL(sigc->fingerprint);
77 FREE_AND_NULL(sigc->primary_key_fingerprint);
80 /* An exclusive status -- only one of them can appear in output */
81 #define GPG_STATUS_EXCLUSIVE (1<<0)
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)
86 /* The status includes key fingerprints */
87 #define GPG_STATUS_FINGERPRINT (1<<3)
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)
92 static struct {
93 char result;
94 const char *check;
95 unsigned int flags;
96 } sigcheck_gpg_status[] = {
97 { 'G', "GOODSIG ", GPG_STATUS_STDSIG },
98 { 'B', "BADSIG ", GPG_STATUS_STDSIG },
99 { 'U', "TRUST_NEVER", 0 },
100 { 'U', "TRUST_UNDEFINED", 0 },
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 },
105 { 0, "VALIDSIG ", GPG_STATUS_FINGERPRINT },
108 static void replace_cstring(char **field, const char *line, const char *next)
110 free(*field);
112 if (line && next)
113 *field = xmemdupz(line, next - line);
114 else
115 *field = NULL;
118 static void parse_gpg_output(struct signature_check *sigc)
120 const char *buf = sigc->gpg_status;
121 const char *line, *next;
122 int i, j;
123 int seen_exclusive_status = 0;
125 /* Iterate over all lines */
126 for (line = buf; *line; line = strchrnul(line+1, '\n')) {
127 while (*line == '\n')
128 line++;
129 if (!*line)
130 break;
132 /* Skip lines that don't start with GNUPG status */
133 if (!skip_prefix(line, "[GNUPG:] ", &line))
134 continue;
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) {
140 if (seen_exclusive_status++)
141 goto found_duplicate_status;
144 if (sigcheck_gpg_status[i].result)
145 sigc->result = sigcheck_gpg_status[i].result;
146 /* Do we have key information? */
147 if (sigcheck_gpg_status[i].flags & GPG_STATUS_KEYID) {
148 next = strchrnul(line, ' ');
149 replace_cstring(&sigc->key, line, next);
150 /* Do we have signer information? */
151 if (*next && (sigcheck_gpg_status[i].flags & GPG_STATUS_UID)) {
152 line = next + 1;
153 next = strchrnul(line, '\n');
154 replace_cstring(&sigc->signer, line, next);
157 /* Do we have fingerprint? */
158 if (sigcheck_gpg_status[i].flags & GPG_STATUS_FINGERPRINT) {
159 const char *limit;
160 char **field;
162 next = strchrnul(line, ' ');
163 replace_cstring(&sigc->fingerprint, line, next);
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.
171 limit = strchrnul(line, '\n');
172 for (j = 9; j > 0; j--) {
173 if (!*next || limit <= next)
174 break;
175 line = next + 1;
176 next = strchrnul(line, ' ');
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);
188 break;
192 return;
194 found_duplicate_status:
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.
202 sigc->result = 'E';
203 /* Clear partial data to avoid confusion */
204 FREE_AND_NULL(sigc->primary_key_fingerprint);
205 FREE_AND_NULL(sigc->fingerprint);
206 FREE_AND_NULL(sigc->signer);
207 FREE_AND_NULL(sigc->key);
210 static int verify_signed_buffer(const char *payload, size_t payload_size,
211 const char *signature, size_t signature_size,
212 struct strbuf *gpg_output,
213 struct strbuf *gpg_status)
215 struct child_process gpg = CHILD_PROCESS_INIT;
216 struct gpg_format *fmt;
217 struct tempfile *temp;
218 int ret;
219 struct strbuf buf = STRBUF_INIT;
221 temp = mks_tempfile_t(".git_vtag_tmpXXXXXX");
222 if (!temp)
223 return error_errno(_("could not create temporary file"));
224 if (write_in_full(temp->fd, signature, signature_size) < 0 ||
225 close_tempfile_gently(temp) < 0) {
226 error_errno(_("failed writing detached signature to '%s'"),
227 temp->filename.buf);
228 delete_tempfile(&temp);
229 return -1;
232 fmt = get_format_by_sig(signature);
233 if (!fmt)
234 BUG("bad signature '%s'", signature);
236 argv_array_push(&gpg.args, fmt->program);
237 argv_array_pushv(&gpg.args, fmt->verify_args);
238 argv_array_pushl(&gpg.args,
239 "--status-fd=1",
240 "--verify", temp->filename.buf, "-",
241 NULL);
243 if (!gpg_status)
244 gpg_status = &buf;
246 sigchain_push(SIGPIPE, SIG_IGN);
247 ret = pipe_command(&gpg, payload, payload_size,
248 gpg_status, 0, gpg_output, 0);
249 sigchain_pop(SIGPIPE);
251 delete_tempfile(&temp);
253 ret |= !strstr(gpg_status->buf, "\n[GNUPG:] GOODSIG ");
254 strbuf_release(&buf); /* no matter it was used or not */
256 return ret;
259 int check_signature(const char *payload, size_t plen, const char *signature,
260 size_t slen, struct signature_check *sigc)
262 struct strbuf gpg_output = STRBUF_INIT;
263 struct strbuf gpg_status = STRBUF_INIT;
264 int status;
266 sigc->result = 'N';
268 status = verify_signed_buffer(payload, plen, signature, slen,
269 &gpg_output, &gpg_status);
270 if (status && !gpg_output.len)
271 goto out;
272 sigc->payload = xmemdupz(payload, plen);
273 sigc->gpg_output = strbuf_detach(&gpg_output, NULL);
274 sigc->gpg_status = strbuf_detach(&gpg_status, NULL);
275 parse_gpg_output(sigc);
276 status |= sigc->result != 'G' && sigc->result != 'U';
278 out:
279 strbuf_release(&gpg_status);
280 strbuf_release(&gpg_output);
282 return !!status;
285 void print_signature_buffer(const struct signature_check *sigc, unsigned flags)
287 const char *output = flags & GPG_VERIFY_RAW ?
288 sigc->gpg_status : sigc->gpg_output;
290 if (flags & GPG_VERIFY_VERBOSE && sigc->payload)
291 fputs(sigc->payload, stdout);
293 if (output)
294 fputs(output, stderr);
297 size_t parse_signature(const char *buf, size_t size)
299 size_t len = 0;
300 size_t match = size;
301 while (len < size) {
302 const char *eol;
304 if (get_format_by_sig(buf + len))
305 match = len;
307 eol = memchr(buf + len, '\n', size - len);
308 len += eol ? eol - (buf + len) + 1 : size - len;
310 return match;
313 void set_signing_key(const char *key)
315 free(configured_signing_key);
316 configured_signing_key = xstrdup(key);
319 int git_gpg_config(const char *var, const char *value, void *cb)
321 struct gpg_format *fmt = NULL;
322 char *fmtname = NULL;
324 if (!strcmp(var, "user.signingkey")) {
325 if (!value)
326 return config_error_nonbool(var);
327 set_signing_key(value);
328 return 0;
331 if (!strcmp(var, "gpg.format")) {
332 if (!value)
333 return config_error_nonbool(var);
334 fmt = get_format_by_name(value);
335 if (!fmt)
336 return error("unsupported value for %s: %s",
337 var, value);
338 use_format = fmt;
339 return 0;
342 if (!strcmp(var, "gpg.program") || !strcmp(var, "gpg.openpgp.program"))
343 fmtname = "openpgp";
345 if (!strcmp(var, "gpg.x509.program"))
346 fmtname = "x509";
348 if (fmtname) {
349 fmt = get_format_by_name(fmtname);
350 return git_config_string(&fmt->program, var, value);
353 return 0;
356 const char *get_signing_key(void)
358 if (configured_signing_key)
359 return configured_signing_key;
360 return git_committer_info(IDENT_STRICT|IDENT_NO_DATE);
363 int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *signing_key)
365 struct child_process gpg = CHILD_PROCESS_INIT;
366 int ret;
367 size_t i, j, bottom;
368 struct strbuf gpg_status = STRBUF_INIT;
370 argv_array_pushl(&gpg.args,
371 use_format->program,
372 "--status-fd=2",
373 "-bsau", signing_key,
374 NULL);
376 bottom = signature->len;
379 * When the username signingkey is bad, program could be terminated
380 * because gpg exits without reading and then write gets SIGPIPE.
382 sigchain_push(SIGPIPE, SIG_IGN);
383 ret = pipe_command(&gpg, buffer->buf, buffer->len,
384 signature, 1024, &gpg_status, 0);
385 sigchain_pop(SIGPIPE);
387 ret |= !strstr(gpg_status.buf, "\n[GNUPG:] SIG_CREATED ");
388 strbuf_release(&gpg_status);
389 if (ret)
390 return error(_("gpg failed to sign the data"));
392 /* Strip CR from the line endings, in case we are on Windows. */
393 for (i = j = bottom; i < signature->len; i++)
394 if (signature->buf[i] != '\r') {
395 if (i != j)
396 signature->buf[j] = signature->buf[i];
397 j++;
399 strbuf_setlen(signature, j);
401 return 0;