blob: d936f3a32fe5d38c2b41ae0488ac2c02250a1618 [file] [log] [blame]
Junio C Hamano2f47eae2011-09-07 21:19:47 -07001#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07002#include "config.h"
Junio C Hamano2f47eae2011-09-07 21:19:47 -07003#include "run-command.h"
4#include "strbuf.h"
5#include "gpg-interface.h"
6#include "sigchain.h"
Jeff King43223532016-06-17 19:38:43 -04007#include "tempfile.h"
Junio C Hamano2f47eae2011-09-07 21:19:47 -07008
9static char *configured_signing_key;
Junio C Hamano0c5e70f2011-11-29 12:29:48 -080010static const char *gpg_program = "gpg";
Junio C Hamano2f47eae2011-09-07 21:19:47 -070011
Junio C Hamanod7c67662014-08-19 13:18:07 -070012#define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----"
13#define PGP_MESSAGE "-----BEGIN PGP MESSAGE-----"
14
Michael J Gruber01e57b52014-06-23 09:05:47 +020015void signature_check_clear(struct signature_check *sigc)
16{
Ævar Arnfjörð Bjarmason88ce3ef2017-06-15 23:15:49 +000017 FREE_AND_NULL(sigc->payload);
18 FREE_AND_NULL(sigc->gpg_output);
19 FREE_AND_NULL(sigc->gpg_status);
20 FREE_AND_NULL(sigc->signer);
21 FREE_AND_NULL(sigc->key);
Michael J Gruber01e57b52014-06-23 09:05:47 +020022}
23
Junio C Hamanoa50e7ca2014-08-14 15:31:13 -070024static struct {
25 char result;
26 const char *check;
27} sigcheck_gpg_status[] = {
28 { 'G', "\n[GNUPG:] GOODSIG " },
29 { 'B', "\n[GNUPG:] BADSIG " },
30 { 'U', "\n[GNUPG:] TRUST_NEVER" },
31 { 'U', "\n[GNUPG:] TRUST_UNDEFINED" },
Michael J Gruber661a1802016-10-12 15:04:15 +020032 { 'E', "\n[GNUPG:] ERRSIG "},
33 { 'X', "\n[GNUPG:] EXPSIG "},
34 { 'Y', "\n[GNUPG:] EXPKEYSIG "},
35 { 'R', "\n[GNUPG:] REVKEYSIG "},
Junio C Hamanoa50e7ca2014-08-14 15:31:13 -070036};
37
38void parse_gpg_output(struct signature_check *sigc)
39{
40 const char *buf = sigc->gpg_status;
41 int i;
42
43 /* Iterate over all search strings */
44 for (i = 0; i < ARRAY_SIZE(sigcheck_gpg_status); i++) {
45 const char *found, *next;
46
47 if (!skip_prefix(buf, sigcheck_gpg_status[i].check + 1, &found)) {
48 found = strstr(buf, sigcheck_gpg_status[i].check);
49 if (!found)
50 continue;
51 found += strlen(sigcheck_gpg_status[i].check);
52 }
53 sigc->result = sigcheck_gpg_status[i].result;
54 /* The trust messages are not followed by key/signer information */
55 if (sigc->result != 'U') {
56 sigc->key = xmemdupz(found, 16);
Michael J Gruber661a1802016-10-12 15:04:15 +020057 /* The ERRSIG message is not followed by signer information */
58 if (sigc-> result != 'E') {
59 found += 17;
60 next = strchrnul(found, '\n');
61 sigc->signer = xmemdupz(found, next - found);
62 }
Junio C Hamanoa50e7ca2014-08-14 15:31:13 -070063 }
64 }
65}
66
brian m. carlson434060e2015-06-21 23:14:40 +000067int check_signature(const char *payload, size_t plen, const char *signature,
brian m. carlsona4cc18f2015-06-21 23:14:38 +000068 size_t slen, struct signature_check *sigc)
69{
70 struct strbuf gpg_output = STRBUF_INIT;
71 struct strbuf gpg_status = STRBUF_INIT;
72 int status;
73
74 sigc->result = 'N';
75
76 status = verify_signed_buffer(payload, plen, signature, slen,
77 &gpg_output, &gpg_status);
78 if (status && !gpg_output.len)
79 goto out;
80 sigc->payload = xmemdupz(payload, plen);
81 sigc->gpg_output = strbuf_detach(&gpg_output, NULL);
82 sigc->gpg_status = strbuf_detach(&gpg_status, NULL);
83 parse_gpg_output(sigc);
84
85 out:
86 strbuf_release(&gpg_status);
87 strbuf_release(&gpg_output);
brian m. carlson434060e2015-06-21 23:14:40 +000088
89 return sigc->result != 'G' && sigc->result != 'U';
brian m. carlsona4cc18f2015-06-21 23:14:38 +000090}
91
brian m. carlsonca194d52015-06-21 23:14:41 +000092void print_signature_buffer(const struct signature_check *sigc, unsigned flags)
93{
brian m. carlsonaeff29d2015-06-21 23:14:42 +000094 const char *output = flags & GPG_VERIFY_RAW ?
95 sigc->gpg_status : sigc->gpg_output;
96
brian m. carlsonca194d52015-06-21 23:14:41 +000097 if (flags & GPG_VERIFY_VERBOSE && sigc->payload)
98 fputs(sigc->payload, stdout);
99
brian m. carlsonaeff29d2015-06-21 23:14:42 +0000100 if (output)
101 fputs(output, stderr);
brian m. carlsonca194d52015-06-21 23:14:41 +0000102}
103
Junio C Hamanod7c67662014-08-19 13:18:07 -0700104/*
105 * Look at GPG signed content (e.g. a signed tag object), whose
106 * payload is followed by a detached signature on it. Return the
107 * offset where the embedded detached signature begins, or the end of
108 * the data when there is no such signature.
109 */
110size_t parse_signature(const char *buf, unsigned long size)
111{
112 char *eol;
113 size_t len = 0;
114 while (len < size && !starts_with(buf + len, PGP_SIGNATURE) &&
115 !starts_with(buf + len, PGP_MESSAGE)) {
116 eol = memchr(buf + len, '\n', size - len);
117 len += eol ? eol - (buf + len) + 1 : size - len;
118 }
119 return len;
120}
121
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700122void set_signing_key(const char *key)
123{
124 free(configured_signing_key);
125 configured_signing_key = xstrdup(key);
126}
127
128int git_gpg_config(const char *var, const char *value, void *cb)
129{
130 if (!strcmp(var, "user.signingkey")) {
Junio C Hamano0c5e70f2011-11-29 12:29:48 -0800131 set_signing_key(value);
132 }
133 if (!strcmp(var, "gpg.program")) {
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700134 if (!value)
135 return config_error_nonbool(var);
Junio C Hamano0c5e70f2011-11-29 12:29:48 -0800136 gpg_program = xstrdup(value);
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700137 }
138 return 0;
139}
140
141const char *get_signing_key(void)
142{
143 if (configured_signing_key)
144 return configured_signing_key;
Jeff Kingf9bc5732012-05-24 19:28:40 -0400145 return git_committer_info(IDENT_STRICT|IDENT_NO_DATE);
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700146}
147
148/*
149 * Create a detached signature for the contents of "buffer" and append
150 * it after "signature"; "buffer" and "signature" can be the same
151 * strbuf instance, which would cause the detached signature appended
152 * at the end.
153 */
154int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *signing_key)
155{
René Scharfed3180272014-08-19 21:09:35 +0200156 struct child_process gpg = CHILD_PROCESS_INIT;
Jeff King0581b542016-06-17 19:38:55 -0400157 int ret;
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700158 size_t i, j, bottom;
Michael J Gruberefee9552016-06-17 19:38:59 -0400159 struct strbuf gpg_status = STRBUF_INIT;
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700160
Jeff Kingaedb5dc2016-06-17 19:38:35 -0400161 argv_array_pushl(&gpg.args,
162 gpg_program,
Michael J Gruberefee9552016-06-17 19:38:59 -0400163 "--status-fd=2",
Jeff Kingaedb5dc2016-06-17 19:38:35 -0400164 "-bsau", signing_key,
165 NULL);
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700166
Jeff King0581b542016-06-17 19:38:55 -0400167 bottom = signature->len;
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700168
169 /*
170 * When the username signingkey is bad, program could be terminated
171 * because gpg exits without reading and then write gets SIGPIPE.
172 */
173 sigchain_push(SIGPIPE, SIG_IGN);
Jeff King0581b542016-06-17 19:38:55 -0400174 ret = pipe_command(&gpg, buffer->buf, buffer->len,
Michael J Gruberefee9552016-06-17 19:38:59 -0400175 signature, 1024, &gpg_status, 0);
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700176 sigchain_pop(SIGPIPE);
177
Michael J Gruberefee9552016-06-17 19:38:59 -0400178 ret |= !strstr(gpg_status.buf, "\n[GNUPG:] SIG_CREATED ");
179 strbuf_release(&gpg_status);
180 if (ret)
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700181 return error(_("gpg failed to sign the data"));
182
183 /* Strip CR from the line endings, in case we are on Windows. */
184 for (i = j = bottom; i < signature->len; i++)
185 if (signature->buf[i] != '\r') {
186 if (i != j)
187 signature->buf[j] = signature->buf[i];
188 j++;
189 }
190 strbuf_setlen(signature, j);
191
192 return 0;
193}
194
195/*
196 * Run "gpg" to see if the payload matches the detached signature.
Junio C Hamanoe3f55e02012-01-04 12:43:02 -0800197 * gpg_output, when set, receives the diagnostic output from GPG.
Michael J Gruberb60b7562013-02-14 17:04:42 +0100198 * gpg_status, when set, receives the status output from GPG.
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700199 */
200int verify_signed_buffer(const char *payload, size_t payload_size,
201 const char *signature, size_t signature_size,
Michael J Gruber9cc4ac82013-02-14 17:04:44 +0100202 struct strbuf *gpg_output, struct strbuf *gpg_status)
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700203{
René Scharfed3180272014-08-19 21:09:35 +0200204 struct child_process gpg = CHILD_PROCESS_INIT;
Jeff King43223532016-06-17 19:38:43 -0400205 static struct tempfile temp;
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700206 int fd, ret;
Michael J Gruberb60b7562013-02-14 17:04:42 +0100207 struct strbuf buf = STRBUF_INIT;
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700208
Jeff King43223532016-06-17 19:38:43 -0400209 fd = mks_tempfile_t(&temp, ".git_vtag_tmpXXXXXX");
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700210 if (fd < 0)
Jeff King43223532016-06-17 19:38:43 -0400211 return error_errno(_("could not create temporary file"));
212 if (write_in_full(fd, signature, signature_size) < 0) {
213 error_errno(_("failed writing detached signature to '%s'"),
214 temp.filename.buf);
215 delete_tempfile(&temp);
216 return -1;
217 }
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700218 close(fd);
219
Jeff Kingaedb5dc2016-06-17 19:38:35 -0400220 argv_array_pushl(&gpg.args,
221 gpg_program,
222 "--status-fd=1",
Junio C Hamanoaf2b21e2016-08-16 15:04:13 -0700223 "--keyid-format=long",
Jeff King43223532016-06-17 19:38:43 -0400224 "--verify", temp.filename.buf, "-",
Jeff Kingaedb5dc2016-06-17 19:38:35 -0400225 NULL);
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700226
Jeff Kingc752fcc2016-06-17 19:38:39 -0400227 if (!gpg_status)
228 gpg_status = &buf;
Michael J Gruberb60b7562013-02-14 17:04:42 +0100229
Jeff King0d2b6642016-06-17 19:38:52 -0400230 sigchain_push(SIGPIPE, SIG_IGN);
231 ret = pipe_command(&gpg, payload, payload_size,
232 gpg_status, 0, gpg_output, 0);
Santiago Torresd281b452016-04-05 12:07:24 -0400233 sigchain_pop(SIGPIPE);
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700234
Jeff King43223532016-06-17 19:38:43 -0400235 delete_tempfile(&temp);
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700236
Jeff Kingc752fcc2016-06-17 19:38:39 -0400237 ret |= !strstr(gpg_status->buf, "\n[GNUPG:] GOODSIG ");
Michael J Gruber9cc4ac82013-02-14 17:04:44 +0100238 strbuf_release(&buf); /* no matter it was used or not */
Michael J Gruberb60b7562013-02-14 17:04:42 +0100239
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700240 return ret;
241}