2 #include "run-command.h"
4 #include "gpg-interface.h"
7 static char *configured_signing_key;
8 static const char *gpg_program = "gpg";
10 #define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----"
11 #define PGP_MESSAGE "-----BEGIN PGP MESSAGE-----"
13 void signature_check_clear(struct signature_check *sigc)
16 free(sigc->gpg_output);
17 free(sigc->gpg_status);
21 sigc->gpg_output = NULL;
22 sigc->gpg_status = NULL;
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" },
37 void parse_gpg_output(struct signature_check *sigc)
39 const char *buf = sigc->gpg_status;
42 /* Iterate over all search strings */
43 for (i = 0; i < ARRAY_SIZE(sigcheck_gpg_status); i++) {
44 const char *found, *next;
46 if (!skip_prefix(buf, sigcheck_gpg_status[i].check + 1, &found)) {
47 found = strstr(buf, sigcheck_gpg_status[i].check);
50 found += strlen(sigcheck_gpg_status[i].check);
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);
57 next = strchrnul(found, '\n');
58 sigc->signer = xmemdupz(found, next - found);
63 int check_signature(const char *payload, size_t plen, const char *signature,
64 size_t slen, struct signature_check *sigc)
66 struct strbuf gpg_output = STRBUF_INIT;
67 struct strbuf gpg_status = STRBUF_INIT;
72 status = verify_signed_buffer(payload, plen, signature, slen,
73 &gpg_output, &gpg_status);
74 if (status && !gpg_output.len)
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);
82 strbuf_release(&gpg_status);
83 strbuf_release(&gpg_output);
85 return sigc->result != 'G' && sigc->result != 'U';
89 * Look at GPG signed content (e.g. a signed tag object), whose
90 * payload is followed by a detached signature on it. Return the
91 * offset where the embedded detached signature begins, or the end of
92 * the data when there is no such signature.
94 size_t parse_signature(const char *buf, unsigned long size)
98 while (len < size && !starts_with(buf + len, PGP_SIGNATURE) &&
99 !starts_with(buf + len, PGP_MESSAGE)) {
100 eol = memchr(buf + len, '\n', size - len);
101 len += eol ? eol - (buf + len) + 1 : size - len;
106 void set_signing_key(const char *key)
108 free(configured_signing_key);
109 configured_signing_key = xstrdup(key);
112 int git_gpg_config(const char *var, const char *value, void *cb)
114 if (!strcmp(var, "user.signingkey")) {
115 set_signing_key(value);
117 if (!strcmp(var, "gpg.program")) {
119 return config_error_nonbool(var);
120 gpg_program = xstrdup(value);
125 const char *get_signing_key(void)
127 if (configured_signing_key)
128 return configured_signing_key;
129 return git_committer_info(IDENT_STRICT|IDENT_NO_DATE);
133 * Create a detached signature for the contents of "buffer" and append
134 * it after "signature"; "buffer" and "signature" can be the same
135 * strbuf instance, which would cause the detached signature appended
138 int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *signing_key)
140 struct child_process gpg = CHILD_PROCESS_INIT;
148 args[0] = gpg_program;
150 args[2] = signing_key;
153 if (start_command(&gpg))
154 return error(_("could not run gpg."));
157 * When the username signingkey is bad, program could be terminated
158 * because gpg exits without reading and then write gets SIGPIPE.
160 sigchain_push(SIGPIPE, SIG_IGN);
162 if (write_in_full(gpg.in, buffer->buf, buffer->len) != buffer->len) {
165 finish_command(&gpg);
166 return error(_("gpg did not accept the data"));
170 bottom = signature->len;
171 len = strbuf_read(signature, gpg.out, 1024);
174 sigchain_pop(SIGPIPE);
176 if (finish_command(&gpg) || !len || len < 0)
177 return error(_("gpg failed to sign the data"));
179 /* Strip CR from the line endings, in case we are on Windows. */
180 for (i = j = bottom; i < signature->len; i++)
181 if (signature->buf[i] != '\r') {
183 signature->buf[j] = signature->buf[i];
186 strbuf_setlen(signature, j);
192 * Run "gpg" to see if the payload matches the detached signature.
193 * gpg_output, when set, receives the diagnostic output from GPG.
194 * gpg_status, when set, receives the status output from GPG.
196 int verify_signed_buffer(const char *payload, size_t payload_size,
197 const char *signature, size_t signature_size,
198 struct strbuf *gpg_output, struct strbuf *gpg_status)
200 struct child_process gpg = CHILD_PROCESS_INIT;
201 const char *args_gpg[] = {NULL, "--status-fd=1", "--verify", "FILE", "-", NULL};
204 struct strbuf buf = STRBUF_INIT;
205 struct strbuf *pbuf = &buf;
207 args_gpg[0] = gpg_program;
208 fd = git_mkstemp(path, PATH_MAX, ".git_vtag_tmpXXXXXX");
210 return error(_("could not create temporary file '%s': %s"),
211 path, strerror(errno));
212 if (write_in_full(fd, signature, signature_size) < 0)
213 return error(_("failed writing detached signature to '%s': %s"),
214 path, strerror(errno));
223 if (start_command(&gpg)) {
225 return error(_("could not run gpg."));
228 write_in_full(gpg.in, payload, payload_size);
232 strbuf_read(gpg_output, gpg.err, 0);
237 strbuf_read(pbuf, gpg.out, 0);
240 ret = finish_command(&gpg);
242 unlink_or_warn(path);
244 ret |= !strstr(pbuf->buf, "\n[GNUPG:] GOODSIG ");
245 strbuf_release(&buf); /* no matter it was used or not */