2 #include "run-command.h"
4 #include "gpg-interface.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)
17 free(sigc->gpg_output);
18 free(sigc->gpg_status);
22 sigc->gpg_output = NULL;
23 sigc->gpg_status = NULL;
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" },
38 void parse_gpg_output(struct signature_check *sigc)
40 const char *buf = sigc->gpg_status;
43 /* Iterate over all search strings */
44 for (i = 0; i < ARRAY_SIZE(sigcheck_gpg_status); i++) {
45 const char *found, *next;
47 if (!skip_prefix(buf, sigcheck_gpg_status[i].check + 1, &found)) {
48 found = strstr(buf, sigcheck_gpg_status[i].check);
51 found += strlen(sigcheck_gpg_status[i].check);
53 sigc->result = sigcheck_gpg_status[i].result;
54 /* The trust messages are not followed by key/signer information */
55 if (sigc->result != 'U') {
56 sigc->key = xmemdupz(found, 16);
58 next = strchrnul(found, '\n');
59 sigc->signer = xmemdupz(found, next - found);
64 int check_signature(const char *payload, size_t plen, const char *signature,
65 size_t slen, struct signature_check *sigc)
67 struct strbuf gpg_output = STRBUF_INIT;
68 struct strbuf gpg_status = STRBUF_INIT;
73 status = verify_signed_buffer(payload, plen, signature, slen,
74 &gpg_output, &gpg_status);
75 if (status && !gpg_output.len)
77 sigc->payload = xmemdupz(payload, plen);
78 sigc->gpg_output = strbuf_detach(&gpg_output, NULL);
79 sigc->gpg_status = strbuf_detach(&gpg_status, NULL);
80 parse_gpg_output(sigc);
83 strbuf_release(&gpg_status);
84 strbuf_release(&gpg_output);
86 return sigc->result != 'G' && sigc->result != 'U';
89 void print_signature_buffer(const struct signature_check *sigc, unsigned flags)
91 const char *output = flags & GPG_VERIFY_RAW ?
92 sigc->gpg_status : sigc->gpg_output;
94 if (flags & GPG_VERIFY_VERBOSE && sigc->payload)
95 fputs(sigc->payload, stdout);
98 fputs(output, stderr);
102 * Look at GPG signed content (e.g. a signed tag object), whose
103 * payload is followed by a detached signature on it. Return the
104 * offset where the embedded detached signature begins, or the end of
105 * the data when there is no such signature.
107 size_t parse_signature(const char *buf, unsigned long size)
111 while (len < size && !starts_with(buf + len, PGP_SIGNATURE) &&
112 !starts_with(buf + len, PGP_MESSAGE)) {
113 eol = memchr(buf + len, '\n', size - len);
114 len += eol ? eol - (buf + len) + 1 : size - len;
119 void set_signing_key(const char *key)
121 free(configured_signing_key);
122 configured_signing_key = xstrdup(key);
125 int git_gpg_config(const char *var, const char *value, void *cb)
127 if (!strcmp(var, "user.signingkey")) {
128 set_signing_key(value);
130 if (!strcmp(var, "gpg.program")) {
132 return config_error_nonbool(var);
133 gpg_program = xstrdup(value);
138 const char *get_signing_key(void)
140 if (configured_signing_key)
141 return configured_signing_key;
142 return git_committer_info(IDENT_STRICT|IDENT_NO_DATE);
146 * Create a detached signature for the contents of "buffer" and append
147 * it after "signature"; "buffer" and "signature" can be the same
148 * strbuf instance, which would cause the detached signature appended
151 int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *signing_key)
153 struct child_process gpg = CHILD_PROCESS_INIT;
156 struct strbuf gpg_status = STRBUF_INIT;
158 argv_array_pushl(&gpg.args,
161 "-bsau", signing_key,
164 bottom = signature->len;
167 * When the username signingkey is bad, program could be terminated
168 * because gpg exits without reading and then write gets SIGPIPE.
170 sigchain_push(SIGPIPE, SIG_IGN);
171 ret = pipe_command(&gpg, buffer->buf, buffer->len,
172 signature, 1024, &gpg_status, 0);
173 sigchain_pop(SIGPIPE);
175 ret |= !strstr(gpg_status.buf, "\n[GNUPG:] SIG_CREATED ");
176 strbuf_release(&gpg_status);
178 return error(_("gpg failed to sign the data"));
180 /* Strip CR from the line endings, in case we are on Windows. */
181 for (i = j = bottom; i < signature->len; i++)
182 if (signature->buf[i] != '\r') {
184 signature->buf[j] = signature->buf[i];
187 strbuf_setlen(signature, j);
193 * Run "gpg" to see if the payload matches the detached signature.
194 * gpg_output, when set, receives the diagnostic output from GPG.
195 * gpg_status, when set, receives the status output from GPG.
197 int verify_signed_buffer(const char *payload, size_t payload_size,
198 const char *signature, size_t signature_size,
199 struct strbuf *gpg_output, struct strbuf *gpg_status)
201 struct child_process gpg = CHILD_PROCESS_INIT;
202 static struct tempfile temp;
204 struct strbuf buf = STRBUF_INIT;
206 fd = mks_tempfile_t(&temp, ".git_vtag_tmpXXXXXX");
208 return error_errno(_("could not create temporary file"));
209 if (write_in_full(fd, signature, signature_size) < 0) {
210 error_errno(_("failed writing detached signature to '%s'"),
212 delete_tempfile(&temp);
217 argv_array_pushl(&gpg.args,
220 "--keyid-format=long",
221 "--verify", temp.filename.buf, "-",
227 sigchain_push(SIGPIPE, SIG_IGN);
228 ret = pipe_command(&gpg, payload, payload_size,
229 gpg_status, 0, gpg_output, 0);
230 sigchain_pop(SIGPIPE);
232 delete_tempfile(&temp);
234 ret |= !strstr(gpg_status->buf, "\n[GNUPG:] GOODSIG ");
235 strbuf_release(&buf); /* no matter it was used or not */