blob: 8672edaf4823daafbfa1c51b37faca11250374d4 [file] [log] [blame]
Junio C Hamano2f47eae2011-09-07 21:19:47 -07001#include "cache.h"
2#include "run-command.h"
3#include "strbuf.h"
4#include "gpg-interface.h"
5#include "sigchain.h"
Jeff King43223532016-06-17 19:38:43 -04006#include "tempfile.h"
Junio C Hamano2f47eae2011-09-07 21:19:47 -07007
8static char *configured_signing_key;
Junio C Hamano0c5e70f2011-11-29 12:29:48 -08009static const char *gpg_program = "gpg";
Junio C Hamano2f47eae2011-09-07 21:19:47 -070010
Junio C Hamanod7c67662014-08-19 13:18:07 -070011#define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----"
12#define PGP_MESSAGE "-----BEGIN PGP MESSAGE-----"
13
Michael J Gruber01e57b52014-06-23 09:05:47 +020014void signature_check_clear(struct signature_check *sigc)
15{
Michael J Gruber71c214c2014-06-23 09:05:48 +020016 free(sigc->payload);
Michael J Gruber01e57b52014-06-23 09:05:47 +020017 free(sigc->gpg_output);
18 free(sigc->gpg_status);
19 free(sigc->signer);
20 free(sigc->key);
Michael J Gruber71c214c2014-06-23 09:05:48 +020021 sigc->payload = NULL;
Michael J Gruber01e57b52014-06-23 09:05:47 +020022 sigc->gpg_output = NULL;
23 sigc->gpg_status = NULL;
24 sigc->signer = NULL;
25 sigc->key = NULL;
26}
27
Junio C Hamanoa50e7ca2014-08-14 15:31:13 -070028static struct {
29 char result;
30 const char *check;
31} sigcheck_gpg_status[] = {
32 { 'G', "\n[GNUPG:] GOODSIG " },
33 { 'B', "\n[GNUPG:] BADSIG " },
34 { 'U', "\n[GNUPG:] TRUST_NEVER" },
35 { 'U', "\n[GNUPG:] TRUST_UNDEFINED" },
36};
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);
57 found += 17;
58 next = strchrnul(found, '\n');
59 sigc->signer = xmemdupz(found, next - found);
60 }
61 }
62}
63
brian m. carlson434060e2015-06-21 23:14:40 +000064int check_signature(const char *payload, size_t plen, const char *signature,
brian m. carlsona4cc18f2015-06-21 23:14:38 +000065 size_t slen, struct signature_check *sigc)
66{
67 struct strbuf gpg_output = STRBUF_INIT;
68 struct strbuf gpg_status = STRBUF_INIT;
69 int status;
70
71 sigc->result = 'N';
72
73 status = verify_signed_buffer(payload, plen, signature, slen,
74 &gpg_output, &gpg_status);
75 if (status && !gpg_output.len)
76 goto out;
77 sigc->payload = xmemdupz(payload, plen);
78 sigc->gpg_output = strbuf_detach(&gpg_output, NULL);
79 sigc->gpg_status = strbuf_detach(&gpg_status, NULL);
80 parse_gpg_output(sigc);
81
82 out:
83 strbuf_release(&gpg_status);
84 strbuf_release(&gpg_output);
brian m. carlson434060e2015-06-21 23:14:40 +000085
86 return sigc->result != 'G' && sigc->result != 'U';
brian m. carlsona4cc18f2015-06-21 23:14:38 +000087}
88
brian m. carlsonca194d52015-06-21 23:14:41 +000089void print_signature_buffer(const struct signature_check *sigc, unsigned flags)
90{
brian m. carlsonaeff29d2015-06-21 23:14:42 +000091 const char *output = flags & GPG_VERIFY_RAW ?
92 sigc->gpg_status : sigc->gpg_output;
93
brian m. carlsonca194d52015-06-21 23:14:41 +000094 if (flags & GPG_VERIFY_VERBOSE && sigc->payload)
95 fputs(sigc->payload, stdout);
96
brian m. carlsonaeff29d2015-06-21 23:14:42 +000097 if (output)
98 fputs(output, stderr);
brian m. carlsonca194d52015-06-21 23:14:41 +000099}
100
Junio C Hamanod7c67662014-08-19 13:18:07 -0700101/*
102 * Look at GPG signed content (e.g. a signed tag object), whose
103 * payload is followed by a detached signature on it. Return the
104 * offset where the embedded detached signature begins, or the end of
105 * the data when there is no such signature.
106 */
107size_t parse_signature(const char *buf, unsigned long size)
108{
109 char *eol;
110 size_t len = 0;
111 while (len < size && !starts_with(buf + len, PGP_SIGNATURE) &&
112 !starts_with(buf + len, PGP_MESSAGE)) {
113 eol = memchr(buf + len, '\n', size - len);
114 len += eol ? eol - (buf + len) + 1 : size - len;
115 }
116 return len;
117}
118
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700119void set_signing_key(const char *key)
120{
121 free(configured_signing_key);
122 configured_signing_key = xstrdup(key);
123}
124
125int git_gpg_config(const char *var, const char *value, void *cb)
126{
127 if (!strcmp(var, "user.signingkey")) {
Junio C Hamano0c5e70f2011-11-29 12:29:48 -0800128 set_signing_key(value);
129 }
130 if (!strcmp(var, "gpg.program")) {
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700131 if (!value)
132 return config_error_nonbool(var);
Junio C Hamano0c5e70f2011-11-29 12:29:48 -0800133 gpg_program = xstrdup(value);
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700134 }
135 return 0;
136}
137
138const char *get_signing_key(void)
139{
140 if (configured_signing_key)
141 return configured_signing_key;
Jeff Kingf9bc5732012-05-24 19:28:40 -0400142 return git_committer_info(IDENT_STRICT|IDENT_NO_DATE);
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700143}
144
145/*
146 * Create a detached signature for the contents of "buffer" and append
147 * it after "signature"; "buffer" and "signature" can be the same
148 * strbuf instance, which would cause the detached signature appended
149 * at the end.
150 */
151int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *signing_key)
152{
René Scharfed3180272014-08-19 21:09:35 +0200153 struct child_process gpg = CHILD_PROCESS_INIT;
Jeff King0581b542016-06-17 19:38:55 -0400154 int ret;
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700155 size_t i, j, bottom;
Michael J Gruberefee9552016-06-17 19:38:59 -0400156 struct strbuf gpg_status = STRBUF_INIT;
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700157
Jeff Kingaedb5dc2016-06-17 19:38:35 -0400158 argv_array_pushl(&gpg.args,
159 gpg_program,
Michael J Gruberefee9552016-06-17 19:38:59 -0400160 "--status-fd=2",
Jeff Kingaedb5dc2016-06-17 19:38:35 -0400161 "-bsau", signing_key,
162 NULL);
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700163
Jeff King0581b542016-06-17 19:38:55 -0400164 bottom = signature->len;
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700165
166 /*
167 * When the username signingkey is bad, program could be terminated
168 * because gpg exits without reading and then write gets SIGPIPE.
169 */
170 sigchain_push(SIGPIPE, SIG_IGN);
Jeff King0581b542016-06-17 19:38:55 -0400171 ret = pipe_command(&gpg, buffer->buf, buffer->len,
Michael J Gruberefee9552016-06-17 19:38:59 -0400172 signature, 1024, &gpg_status, 0);
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700173 sigchain_pop(SIGPIPE);
174
Michael J Gruberefee9552016-06-17 19:38:59 -0400175 ret |= !strstr(gpg_status.buf, "\n[GNUPG:] SIG_CREATED ");
176 strbuf_release(&gpg_status);
177 if (ret)
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700178 return error(_("gpg failed to sign the data"));
179
180 /* Strip CR from the line endings, in case we are on Windows. */
181 for (i = j = bottom; i < signature->len; i++)
182 if (signature->buf[i] != '\r') {
183 if (i != j)
184 signature->buf[j] = signature->buf[i];
185 j++;
186 }
187 strbuf_setlen(signature, j);
188
189 return 0;
190}
191
192/*
193 * Run "gpg" to see if the payload matches the detached signature.
Junio C Hamanoe3f55e02012-01-04 12:43:02 -0800194 * gpg_output, when set, receives the diagnostic output from GPG.
Michael J Gruberb60b7562013-02-14 17:04:42 +0100195 * gpg_status, when set, receives the status output from GPG.
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700196 */
197int verify_signed_buffer(const char *payload, size_t payload_size,
198 const char *signature, size_t signature_size,
Michael J Gruber9cc4ac82013-02-14 17:04:44 +0100199 struct strbuf *gpg_output, struct strbuf *gpg_status)
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700200{
René Scharfed3180272014-08-19 21:09:35 +0200201 struct child_process gpg = CHILD_PROCESS_INIT;
Jeff King43223532016-06-17 19:38:43 -0400202 static struct tempfile temp;
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700203 int fd, ret;
Michael J Gruberb60b7562013-02-14 17:04:42 +0100204 struct strbuf buf = STRBUF_INIT;
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700205
Jeff King43223532016-06-17 19:38:43 -0400206 fd = mks_tempfile_t(&temp, ".git_vtag_tmpXXXXXX");
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700207 if (fd < 0)
Jeff King43223532016-06-17 19:38:43 -0400208 return error_errno(_("could not create temporary file"));
209 if (write_in_full(fd, signature, signature_size) < 0) {
210 error_errno(_("failed writing detached signature to '%s'"),
211 temp.filename.buf);
212 delete_tempfile(&temp);
213 return -1;
214 }
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700215 close(fd);
216
Jeff Kingaedb5dc2016-06-17 19:38:35 -0400217 argv_array_pushl(&gpg.args,
218 gpg_program,
219 "--status-fd=1",
Junio C Hamanoaf2b21e2016-08-16 15:04:13 -0700220 "--keyid-format=long",
Jeff King43223532016-06-17 19:38:43 -0400221 "--verify", temp.filename.buf, "-",
Jeff Kingaedb5dc2016-06-17 19:38:35 -0400222 NULL);
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700223
Jeff Kingc752fcc2016-06-17 19:38:39 -0400224 if (!gpg_status)
225 gpg_status = &buf;
Michael J Gruberb60b7562013-02-14 17:04:42 +0100226
Jeff King0d2b6642016-06-17 19:38:52 -0400227 sigchain_push(SIGPIPE, SIG_IGN);
228 ret = pipe_command(&gpg, payload, payload_size,
229 gpg_status, 0, gpg_output, 0);
Santiago Torresd281b452016-04-05 12:07:24 -0400230 sigchain_pop(SIGPIPE);
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700231
Jeff King43223532016-06-17 19:38:43 -0400232 delete_tempfile(&temp);
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700233
Jeff Kingc752fcc2016-06-17 19:38:39 -0400234 ret |= !strstr(gpg_status->buf, "\n[GNUPG:] GOODSIG ");
Michael J Gruber9cc4ac82013-02-14 17:04:44 +0100235 strbuf_release(&buf); /* no matter it was used or not */
Michael J Gruberb60b7562013-02-14 17:04:42 +0100236
Junio C Hamano2f47eae2011-09-07 21:19:47 -0700237 return ret;
238}