]> git.ipfire.org Git - thirdparty/git.git/blame - gpg-interface.c
gpg-interface: use child_process.args
[thirdparty/git.git] / gpg-interface.c
CommitLineData
2f47eae2
JH
1#include "cache.h"
2#include "run-command.h"
3#include "strbuf.h"
4#include "gpg-interface.h"
5#include "sigchain.h"
6
7static char *configured_signing_key;
0c5e70f0 8static const char *gpg_program = "gpg";
2f47eae2 9
d7c67668
JH
10#define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----"
11#define PGP_MESSAGE "-----BEGIN PGP MESSAGE-----"
12
01e57b5d
MG
13void signature_check_clear(struct signature_check *sigc)
14{
71c214c8 15 free(sigc->payload);
01e57b5d
MG
16 free(sigc->gpg_output);
17 free(sigc->gpg_status);
18 free(sigc->signer);
19 free(sigc->key);
71c214c8 20 sigc->payload = NULL;
01e57b5d
MG
21 sigc->gpg_output = NULL;
22 sigc->gpg_status = NULL;
23 sigc->signer = NULL;
24 sigc->key = NULL;
25}
26
a50e7ca3
JH
27static struct {
28 char result;
29 const char *check;
30} sigcheck_gpg_status[] = {
31 { 'G', "\n[GNUPG:] GOODSIG " },
32 { 'B', "\n[GNUPG:] BADSIG " },
33 { 'U', "\n[GNUPG:] TRUST_NEVER" },
34 { 'U', "\n[GNUPG:] TRUST_UNDEFINED" },
35};
36
37void parse_gpg_output(struct signature_check *sigc)
38{
39 const char *buf = sigc->gpg_status;
40 int i;
41
42 /* Iterate over all search strings */
43 for (i = 0; i < ARRAY_SIZE(sigcheck_gpg_status); i++) {
44 const char *found, *next;
45
46 if (!skip_prefix(buf, sigcheck_gpg_status[i].check + 1, &found)) {
47 found = strstr(buf, sigcheck_gpg_status[i].check);
48 if (!found)
49 continue;
50 found += strlen(sigcheck_gpg_status[i].check);
51 }
52 sigc->result = sigcheck_gpg_status[i].result;
53 /* The trust messages are not followed by key/signer information */
54 if (sigc->result != 'U') {
55 sigc->key = xmemdupz(found, 16);
56 found += 17;
57 next = strchrnul(found, '\n');
58 sigc->signer = xmemdupz(found, next - found);
59 }
60 }
61}
62
434060ec 63int check_signature(const char *payload, size_t plen, const char *signature,
a4cc18f2 64 size_t slen, struct signature_check *sigc)
65{
66 struct strbuf gpg_output = STRBUF_INIT;
67 struct strbuf gpg_status = STRBUF_INIT;
68 int status;
69
70 sigc->result = 'N';
71
72 status = verify_signed_buffer(payload, plen, signature, slen,
73 &gpg_output, &gpg_status);
74 if (status && !gpg_output.len)
75 goto out;
76 sigc->payload = xmemdupz(payload, plen);
77 sigc->gpg_output = strbuf_detach(&gpg_output, NULL);
78 sigc->gpg_status = strbuf_detach(&gpg_status, NULL);
79 parse_gpg_output(sigc);
80
81 out:
82 strbuf_release(&gpg_status);
83 strbuf_release(&gpg_output);
434060ec 84
85 return sigc->result != 'G' && sigc->result != 'U';
a4cc18f2 86}
87
ca194d50 88void print_signature_buffer(const struct signature_check *sigc, unsigned flags)
89{
aeff29dd 90 const char *output = flags & GPG_VERIFY_RAW ?
91 sigc->gpg_status : sigc->gpg_output;
92
ca194d50 93 if (flags & GPG_VERIFY_VERBOSE && sigc->payload)
94 fputs(sigc->payload, stdout);
95
aeff29dd 96 if (output)
97 fputs(output, stderr);
ca194d50 98}
99
d7c67668
JH
100/*
101 * Look at GPG signed content (e.g. a signed tag object), whose
102 * payload is followed by a detached signature on it. Return the
103 * offset where the embedded detached signature begins, or the end of
104 * the data when there is no such signature.
105 */
106size_t parse_signature(const char *buf, unsigned long size)
107{
108 char *eol;
109 size_t len = 0;
110 while (len < size && !starts_with(buf + len, PGP_SIGNATURE) &&
111 !starts_with(buf + len, PGP_MESSAGE)) {
112 eol = memchr(buf + len, '\n', size - len);
113 len += eol ? eol - (buf + len) + 1 : size - len;
114 }
115 return len;
116}
117
2f47eae2
JH
118void set_signing_key(const char *key)
119{
120 free(configured_signing_key);
121 configured_signing_key = xstrdup(key);
122}
123
124int git_gpg_config(const char *var, const char *value, void *cb)
125{
126 if (!strcmp(var, "user.signingkey")) {
0c5e70f0
JH
127 set_signing_key(value);
128 }
129 if (!strcmp(var, "gpg.program")) {
2f47eae2
JH
130 if (!value)
131 return config_error_nonbool(var);
0c5e70f0 132 gpg_program = xstrdup(value);
2f47eae2
JH
133 }
134 return 0;
135}
136
137const char *get_signing_key(void)
138{
139 if (configured_signing_key)
140 return configured_signing_key;
f9bc573f 141 return git_committer_info(IDENT_STRICT|IDENT_NO_DATE);
2f47eae2
JH
142}
143
144/*
145 * Create a detached signature for the contents of "buffer" and append
146 * it after "signature"; "buffer" and "signature" can be the same
147 * strbuf instance, which would cause the detached signature appended
148 * at the end.
149 */
150int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *signing_key)
151{
d3180279 152 struct child_process gpg = CHILD_PROCESS_INIT;
2f47eae2
JH
153 ssize_t len;
154 size_t i, j, bottom;
155
2f47eae2
JH
156 gpg.in = -1;
157 gpg.out = -1;
aedb5dc3
JK
158 argv_array_pushl(&gpg.args,
159 gpg_program,
160 "-bsau", signing_key,
161 NULL);
2f47eae2
JH
162
163 if (start_command(&gpg))
164 return error(_("could not run gpg."));
165
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);
171
172 if (write_in_full(gpg.in, buffer->buf, buffer->len) != buffer->len) {
173 close(gpg.in);
174 close(gpg.out);
175 finish_command(&gpg);
176 return error(_("gpg did not accept the data"));
177 }
178 close(gpg.in);
179
180 bottom = signature->len;
181 len = strbuf_read(signature, gpg.out, 1024);
182 close(gpg.out);
183
184 sigchain_pop(SIGPIPE);
185
186 if (finish_command(&gpg) || !len || len < 0)
187 return error(_("gpg failed to sign the data"));
188
189 /* Strip CR from the line endings, in case we are on Windows. */
190 for (i = j = bottom; i < signature->len; i++)
191 if (signature->buf[i] != '\r') {
192 if (i != j)
193 signature->buf[j] = signature->buf[i];
194 j++;
195 }
196 strbuf_setlen(signature, j);
197
198 return 0;
199}
200
201/*
202 * Run "gpg" to see if the payload matches the detached signature.
e3f55e07 203 * gpg_output, when set, receives the diagnostic output from GPG.
b60b7566 204 * gpg_status, when set, receives the status output from GPG.
2f47eae2
JH
205 */
206int verify_signed_buffer(const char *payload, size_t payload_size,
207 const char *signature, size_t signature_size,
9cc4ac8f 208 struct strbuf *gpg_output, struct strbuf *gpg_status)
2f47eae2 209{
d3180279 210 struct child_process gpg = CHILD_PROCESS_INIT;
2f47eae2
JH
211 char path[PATH_MAX];
212 int fd, ret;
b60b7566 213 struct strbuf buf = STRBUF_INIT;
9cc4ac8f 214 struct strbuf *pbuf = &buf;
2f47eae2
JH
215
216 fd = git_mkstemp(path, PATH_MAX, ".git_vtag_tmpXXXXXX");
217 if (fd < 0)
ddf362a2 218 return error_errno(_("could not create temporary file '%s'"), path);
2f47eae2 219 if (write_in_full(fd, signature, signature_size) < 0)
ddf362a2 220 return error_errno(_("failed writing detached signature to '%s'"), path);
2f47eae2
JH
221 close(fd);
222
aedb5dc3
JK
223 argv_array_pushl(&gpg.args,
224 gpg_program,
225 "--status-fd=1",
226 "--verify", path, "-",
227 NULL);
2f47eae2 228 gpg.in = -1;
b60b7566 229 gpg.out = -1;
2f47eae2
JH
230 if (gpg_output)
231 gpg.err = -1;
2f47eae2
JH
232 if (start_command(&gpg)) {
233 unlink(path);
4c9a4182 234 return error(_("could not run gpg."));
2f47eae2
JH
235 }
236
d281b45d 237 sigchain_push(SIGPIPE, SIG_IGN);
2f47eae2
JH
238 write_in_full(gpg.in, payload, payload_size);
239 close(gpg.in);
240
7dac3f83 241 if (gpg_output) {
2f47eae2 242 strbuf_read(gpg_output, gpg.err, 0);
7dac3f83
SB
243 close(gpg.err);
244 }
9cc4ac8f
MG
245 if (gpg_status)
246 pbuf = gpg_status;
247 strbuf_read(pbuf, gpg.out, 0);
b60b7566
MG
248 close(gpg.out);
249
2f47eae2 250 ret = finish_command(&gpg);
d281b45d 251 sigchain_pop(SIGPIPE);
2f47eae2
JH
252
253 unlink_or_warn(path);
254
9cc4ac8f
MG
255 ret |= !strstr(pbuf->buf, "\n[GNUPG:] GOODSIG ");
256 strbuf_release(&buf); /* no matter it was used or not */
b60b7566 257
2f47eae2
JH
258 return ret;
259}