sub-process: move sub-process functions into separate files
[git/debian.git] / gpg-interface.c
blobe44cc27da15f03629989ee0083664d0af09c55b3
1 #include "cache.h"
2 #include "run-command.h"
3 #include "strbuf.h"
4 #include "gpg-interface.h"
5 #include "sigchain.h"
6 #include "tempfile.h"
8 static char *configured_signing_key;
9 static const char *gpg_program = "gpg";
11 #define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----"
12 #define PGP_MESSAGE "-----BEGIN PGP MESSAGE-----"
14 void signature_check_clear(struct signature_check *sigc)
16 free(sigc->payload);
17 free(sigc->gpg_output);
18 free(sigc->gpg_status);
19 free(sigc->signer);
20 free(sigc->key);
21 sigc->payload = NULL;
22 sigc->gpg_output = NULL;
23 sigc->gpg_status = NULL;
24 sigc->signer = NULL;
25 sigc->key = NULL;
28 static 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 { 'E', "\n[GNUPG:] ERRSIG "},
37 { 'X', "\n[GNUPG:] EXPSIG "},
38 { 'Y', "\n[GNUPG:] EXPKEYSIG "},
39 { 'R', "\n[GNUPG:] REVKEYSIG "},
42 void parse_gpg_output(struct signature_check *sigc)
44 const char *buf = sigc->gpg_status;
45 int i;
47 /* Iterate over all search strings */
48 for (i = 0; i < ARRAY_SIZE(sigcheck_gpg_status); i++) {
49 const char *found, *next;
51 if (!skip_prefix(buf, sigcheck_gpg_status[i].check + 1, &found)) {
52 found = strstr(buf, sigcheck_gpg_status[i].check);
53 if (!found)
54 continue;
55 found += strlen(sigcheck_gpg_status[i].check);
57 sigc->result = sigcheck_gpg_status[i].result;
58 /* The trust messages are not followed by key/signer information */
59 if (sigc->result != 'U') {
60 sigc->key = xmemdupz(found, 16);
61 /* The ERRSIG message is not followed by signer information */
62 if (sigc-> result != 'E') {
63 found += 17;
64 next = strchrnul(found, '\n');
65 sigc->signer = xmemdupz(found, next - found);
71 int check_signature(const char *payload, size_t plen, const char *signature,
72 size_t slen, struct signature_check *sigc)
74 struct strbuf gpg_output = STRBUF_INIT;
75 struct strbuf gpg_status = STRBUF_INIT;
76 int status;
78 sigc->result = 'N';
80 status = verify_signed_buffer(payload, plen, signature, slen,
81 &gpg_output, &gpg_status);
82 if (status && !gpg_output.len)
83 goto out;
84 sigc->payload = xmemdupz(payload, plen);
85 sigc->gpg_output = strbuf_detach(&gpg_output, NULL);
86 sigc->gpg_status = strbuf_detach(&gpg_status, NULL);
87 parse_gpg_output(sigc);
89 out:
90 strbuf_release(&gpg_status);
91 strbuf_release(&gpg_output);
93 return sigc->result != 'G' && sigc->result != 'U';
96 void print_signature_buffer(const struct signature_check *sigc, unsigned flags)
98 const char *output = flags & GPG_VERIFY_RAW ?
99 sigc->gpg_status : sigc->gpg_output;
101 if (flags & GPG_VERIFY_VERBOSE && sigc->payload)
102 fputs(sigc->payload, stdout);
104 if (output)
105 fputs(output, stderr);
109 * Look at GPG signed content (e.g. a signed tag object), whose
110 * payload is followed by a detached signature on it. Return the
111 * offset where the embedded detached signature begins, or the end of
112 * the data when there is no such signature.
114 size_t parse_signature(const char *buf, unsigned long size)
116 char *eol;
117 size_t len = 0;
118 while (len < size && !starts_with(buf + len, PGP_SIGNATURE) &&
119 !starts_with(buf + len, PGP_MESSAGE)) {
120 eol = memchr(buf + len, '\n', size - len);
121 len += eol ? eol - (buf + len) + 1 : size - len;
123 return len;
126 void set_signing_key(const char *key)
128 free(configured_signing_key);
129 configured_signing_key = xstrdup(key);
132 int git_gpg_config(const char *var, const char *value, void *cb)
134 if (!strcmp(var, "user.signingkey")) {
135 set_signing_key(value);
137 if (!strcmp(var, "gpg.program")) {
138 if (!value)
139 return config_error_nonbool(var);
140 gpg_program = xstrdup(value);
142 return 0;
145 const char *get_signing_key(void)
147 if (configured_signing_key)
148 return configured_signing_key;
149 return git_committer_info(IDENT_STRICT|IDENT_NO_DATE);
153 * Create a detached signature for the contents of "buffer" and append
154 * it after "signature"; "buffer" and "signature" can be the same
155 * strbuf instance, which would cause the detached signature appended
156 * at the end.
158 int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *signing_key)
160 struct child_process gpg = CHILD_PROCESS_INIT;
161 int ret;
162 size_t i, j, bottom;
163 struct strbuf gpg_status = STRBUF_INIT;
165 argv_array_pushl(&gpg.args,
166 gpg_program,
167 "--status-fd=2",
168 "-bsau", signing_key,
169 NULL);
171 bottom = signature->len;
174 * When the username signingkey is bad, program could be terminated
175 * because gpg exits without reading and then write gets SIGPIPE.
177 sigchain_push(SIGPIPE, SIG_IGN);
178 ret = pipe_command(&gpg, buffer->buf, buffer->len,
179 signature, 1024, &gpg_status, 0);
180 sigchain_pop(SIGPIPE);
182 ret |= !strstr(gpg_status.buf, "\n[GNUPG:] SIG_CREATED ");
183 strbuf_release(&gpg_status);
184 if (ret)
185 return error(_("gpg failed to sign the data"));
187 /* Strip CR from the line endings, in case we are on Windows. */
188 for (i = j = bottom; i < signature->len; i++)
189 if (signature->buf[i] != '\r') {
190 if (i != j)
191 signature->buf[j] = signature->buf[i];
192 j++;
194 strbuf_setlen(signature, j);
196 return 0;
200 * Run "gpg" to see if the payload matches the detached signature.
201 * gpg_output, when set, receives the diagnostic output from GPG.
202 * gpg_status, when set, receives the status output from GPG.
204 int verify_signed_buffer(const char *payload, size_t payload_size,
205 const char *signature, size_t signature_size,
206 struct strbuf *gpg_output, struct strbuf *gpg_status)
208 struct child_process gpg = CHILD_PROCESS_INIT;
209 static struct tempfile temp;
210 int fd, ret;
211 struct strbuf buf = STRBUF_INIT;
213 fd = mks_tempfile_t(&temp, ".git_vtag_tmpXXXXXX");
214 if (fd < 0)
215 return error_errno(_("could not create temporary file"));
216 if (write_in_full(fd, signature, signature_size) < 0) {
217 error_errno(_("failed writing detached signature to '%s'"),
218 temp.filename.buf);
219 delete_tempfile(&temp);
220 return -1;
222 close(fd);
224 argv_array_pushl(&gpg.args,
225 gpg_program,
226 "--status-fd=1",
227 "--keyid-format=long",
228 "--verify", temp.filename.buf, "-",
229 NULL);
231 if (!gpg_status)
232 gpg_status = &buf;
234 sigchain_push(SIGPIPE, SIG_IGN);
235 ret = pipe_command(&gpg, payload, payload_size,
236 gpg_status, 0, gpg_output, 0);
237 sigchain_pop(SIGPIPE);
239 delete_tempfile(&temp);
241 ret |= !strstr(gpg_status->buf, "\n[GNUPG:] GOODSIG ");
242 strbuf_release(&buf); /* no matter it was used or not */
244 return ret;