gpg-interface: move parse_gpg_output() to where it should be
[git/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
01e57b5d
MG
10void signature_check_clear(struct signature_check *sigc)
11{
71c214c8 12 free(sigc->payload);
01e57b5d
MG
13 free(sigc->gpg_output);
14 free(sigc->gpg_status);
15 free(sigc->signer);
16 free(sigc->key);
71c214c8 17 sigc->payload = NULL;
01e57b5d
MG
18 sigc->gpg_output = NULL;
19 sigc->gpg_status = NULL;
20 sigc->signer = NULL;
21 sigc->key = NULL;
22}
23
a50e7ca3
JH
24static 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" },
32};
33
34void parse_gpg_output(struct signature_check *sigc)
35{
36 const char *buf = sigc->gpg_status;
37 int i;
38
39 /* Iterate over all search strings */
40 for (i = 0; i < ARRAY_SIZE(sigcheck_gpg_status); i++) {
41 const char *found, *next;
42
43 if (!skip_prefix(buf, sigcheck_gpg_status[i].check + 1, &found)) {
44 found = strstr(buf, sigcheck_gpg_status[i].check);
45 if (!found)
46 continue;
47 found += strlen(sigcheck_gpg_status[i].check);
48 }
49 sigc->result = sigcheck_gpg_status[i].result;
50 /* The trust messages are not followed by key/signer information */
51 if (sigc->result != 'U') {
52 sigc->key = xmemdupz(found, 16);
53 found += 17;
54 next = strchrnul(found, '\n');
55 sigc->signer = xmemdupz(found, next - found);
56 }
57 }
58}
59
2f47eae2
JH
60void set_signing_key(const char *key)
61{
62 free(configured_signing_key);
63 configured_signing_key = xstrdup(key);
64}
65
66int git_gpg_config(const char *var, const char *value, void *cb)
67{
68 if (!strcmp(var, "user.signingkey")) {
0c5e70f0
JH
69 set_signing_key(value);
70 }
71 if (!strcmp(var, "gpg.program")) {
2f47eae2
JH
72 if (!value)
73 return config_error_nonbool(var);
0c5e70f0 74 gpg_program = xstrdup(value);
2f47eae2
JH
75 }
76 return 0;
77}
78
79const char *get_signing_key(void)
80{
81 if (configured_signing_key)
82 return configured_signing_key;
f9bc573f 83 return git_committer_info(IDENT_STRICT|IDENT_NO_DATE);
2f47eae2
JH
84}
85
86/*
87 * Create a detached signature for the contents of "buffer" and append
88 * it after "signature"; "buffer" and "signature" can be the same
89 * strbuf instance, which would cause the detached signature appended
90 * at the end.
91 */
92int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *signing_key)
93{
94 struct child_process gpg;
95 const char *args[4];
96 ssize_t len;
97 size_t i, j, bottom;
98
99 memset(&gpg, 0, sizeof(gpg));
100 gpg.argv = args;
101 gpg.in = -1;
102 gpg.out = -1;
0c5e70f0 103 args[0] = gpg_program;
2f47eae2
JH
104 args[1] = "-bsau";
105 args[2] = signing_key;
106 args[3] = NULL;
107
108 if (start_command(&gpg))
109 return error(_("could not run gpg."));
110
111 /*
112 * When the username signingkey is bad, program could be terminated
113 * because gpg exits without reading and then write gets SIGPIPE.
114 */
115 sigchain_push(SIGPIPE, SIG_IGN);
116
117 if (write_in_full(gpg.in, buffer->buf, buffer->len) != buffer->len) {
118 close(gpg.in);
119 close(gpg.out);
120 finish_command(&gpg);
121 return error(_("gpg did not accept the data"));
122 }
123 close(gpg.in);
124
125 bottom = signature->len;
126 len = strbuf_read(signature, gpg.out, 1024);
127 close(gpg.out);
128
129 sigchain_pop(SIGPIPE);
130
131 if (finish_command(&gpg) || !len || len < 0)
132 return error(_("gpg failed to sign the data"));
133
134 /* Strip CR from the line endings, in case we are on Windows. */
135 for (i = j = bottom; i < signature->len; i++)
136 if (signature->buf[i] != '\r') {
137 if (i != j)
138 signature->buf[j] = signature->buf[i];
139 j++;
140 }
141 strbuf_setlen(signature, j);
142
143 return 0;
144}
145
146/*
147 * Run "gpg" to see if the payload matches the detached signature.
e3f55e07 148 * gpg_output, when set, receives the diagnostic output from GPG.
b60b7566 149 * gpg_status, when set, receives the status output from GPG.
2f47eae2
JH
150 */
151int verify_signed_buffer(const char *payload, size_t payload_size,
152 const char *signature, size_t signature_size,
9cc4ac8f 153 struct strbuf *gpg_output, struct strbuf *gpg_status)
2f47eae2
JH
154{
155 struct child_process gpg;
b60b7566 156 const char *args_gpg[] = {NULL, "--status-fd=1", "--verify", "FILE", "-", NULL};
2f47eae2
JH
157 char path[PATH_MAX];
158 int fd, ret;
b60b7566 159 struct strbuf buf = STRBUF_INIT;
9cc4ac8f 160 struct strbuf *pbuf = &buf;
2f47eae2 161
0c5e70f0 162 args_gpg[0] = gpg_program;
2f47eae2
JH
163 fd = git_mkstemp(path, PATH_MAX, ".git_vtag_tmpXXXXXX");
164 if (fd < 0)
4c9a4182 165 return error(_("could not create temporary file '%s': %s"),
2f47eae2
JH
166 path, strerror(errno));
167 if (write_in_full(fd, signature, signature_size) < 0)
4c9a4182 168 return error(_("failed writing detached signature to '%s': %s"),
2f47eae2
JH
169 path, strerror(errno));
170 close(fd);
171
172 memset(&gpg, 0, sizeof(gpg));
173 gpg.argv = args_gpg;
174 gpg.in = -1;
b60b7566 175 gpg.out = -1;
2f47eae2
JH
176 if (gpg_output)
177 gpg.err = -1;
b60b7566 178 args_gpg[3] = path;
2f47eae2
JH
179 if (start_command(&gpg)) {
180 unlink(path);
4c9a4182 181 return error(_("could not run gpg."));
2f47eae2
JH
182 }
183
184 write_in_full(gpg.in, payload, payload_size);
185 close(gpg.in);
186
7dac3f83 187 if (gpg_output) {
2f47eae2 188 strbuf_read(gpg_output, gpg.err, 0);
7dac3f83
SB
189 close(gpg.err);
190 }
9cc4ac8f
MG
191 if (gpg_status)
192 pbuf = gpg_status;
193 strbuf_read(pbuf, gpg.out, 0);
b60b7566
MG
194 close(gpg.out);
195
2f47eae2
JH
196 ret = finish_command(&gpg);
197
198 unlink_or_warn(path);
199
9cc4ac8f
MG
200 ret |= !strstr(pbuf->buf, "\n[GNUPG:] GOODSIG ");
201 strbuf_release(&buf); /* no matter it was used or not */
b60b7566 202
2f47eae2
JH
203 return ret;
204}