3c9624c4331d0bc73bfcce90d5dad50561e41659
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;
8static const char *gpg_program = "gpg";
9
10void signature_check_clear(struct signature_check *sigc)
11{
12 free(sigc->payload);
13 free(sigc->gpg_output);
14 free(sigc->gpg_status);
15 free(sigc->signer);
16 free(sigc->key);
17 sigc->payload = NULL;
18 sigc->gpg_output = NULL;
19 sigc->gpg_status = NULL;
20 sigc->signer = NULL;
21 sigc->key = NULL;
22}
23
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
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")) {
69 set_signing_key(value);
70 }
71 if (!strcmp(var, "gpg.program")) {
72 if (!value)
73 return config_error_nonbool(var);
74 gpg_program = xstrdup(value);
75 }
76 return 0;
77}
78
79const char *get_signing_key(void)
80{
81 if (configured_signing_key)
82 return configured_signing_key;
83 return git_committer_info(IDENT_STRICT|IDENT_NO_DATE);
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;
103 args[0] = gpg_program;
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.
148 * gpg_output, when set, receives the diagnostic output from GPG.
149 * gpg_status, when set, receives the status output from GPG.
150 */
151int verify_signed_buffer(const char *payload, size_t payload_size,
152 const char *signature, size_t signature_size,
153 struct strbuf *gpg_output, struct strbuf *gpg_status)
154{
155 struct child_process gpg;
156 const char *args_gpg[] = {NULL, "--status-fd=1", "--verify", "FILE", "-", NULL};
157 char path[PATH_MAX];
158 int fd, ret;
159 struct strbuf buf = STRBUF_INIT;
160 struct strbuf *pbuf = &buf;
161
162 args_gpg[0] = gpg_program;
163 fd = git_mkstemp(path, PATH_MAX, ".git_vtag_tmpXXXXXX");
164 if (fd < 0)
165 return error(_("could not create temporary file '%s': %s"),
166 path, strerror(errno));
167 if (write_in_full(fd, signature, signature_size) < 0)
168 return error(_("failed writing detached signature to '%s': %s"),
169 path, strerror(errno));
170 close(fd);
171
172 memset(&gpg, 0, sizeof(gpg));
173 gpg.argv = args_gpg;
174 gpg.in = -1;
175 gpg.out = -1;
176 if (gpg_output)
177 gpg.err = -1;
178 args_gpg[3] = path;
179 if (start_command(&gpg)) {
180 unlink(path);
181 return error(_("could not run gpg."));
182 }
183
184 write_in_full(gpg.in, payload, payload_size);
185 close(gpg.in);
186
187 if (gpg_output) {
188 strbuf_read(gpg_output, gpg.err, 0);
189 close(gpg.err);
190 }
191 if (gpg_status)
192 pbuf = gpg_status;
193 strbuf_read(pbuf, gpg.out, 0);
194 close(gpg.out);
195
196 ret = finish_command(&gpg);
197
198 unlink_or_warn(path);
199
200 ret |= !strstr(pbuf->buf, "\n[GNUPG:] GOODSIG ");
201 strbuf_release(&buf); /* no matter it was used or not */
202
203 return ret;
204}