3 #include "run-command.h"
5 #include "gpg-interface.h"
9 static char *configured_signing_key;
10 static const char *gpg_program = "gpg";
12 #define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----"
13 #define PGP_MESSAGE "-----BEGIN PGP MESSAGE-----"
15 void signature_check_clear(struct signature_check *sigc)
18 free(sigc->gpg_output);
19 free(sigc->gpg_status);
23 sigc->gpg_output = NULL;
24 sigc->gpg_status = NULL;
32 } sigcheck_gpg_status[] = {
33 { 'G', "\n[GNUPG:] GOODSIG " },
34 { 'B', "\n[GNUPG:] BADSIG " },
35 { 'U', "\n[GNUPG:] TRUST_NEVER" },
36 { 'U', "\n[GNUPG:] TRUST_UNDEFINED" },
37 { 'E', "\n[GNUPG:] ERRSIG "},
38 { 'X', "\n[GNUPG:] EXPSIG "},
39 { 'Y', "\n[GNUPG:] EXPKEYSIG "},
40 { 'R', "\n[GNUPG:] REVKEYSIG "},
43 void parse_gpg_output(struct signature_check *sigc)
45 const char *buf = sigc->gpg_status;
48 /* Iterate over all search strings */
49 for (i = 0; i < ARRAY_SIZE(sigcheck_gpg_status); i++) {
50 const char *found, *next;
52 if (!skip_prefix(buf, sigcheck_gpg_status[i].check + 1, &found)) {
53 found = strstr(buf, sigcheck_gpg_status[i].check);
56 found += strlen(sigcheck_gpg_status[i].check);
58 sigc->result = sigcheck_gpg_status[i].result;
59 /* The trust messages are not followed by key/signer information */
60 if (sigc->result != 'U') {
61 sigc->key = xmemdupz(found, 16);
62 /* The ERRSIG message is not followed by signer information */
63 if (sigc-> result != 'E') {
65 next = strchrnul(found, '\n');
66 sigc->signer = xmemdupz(found, next - found);
72 int check_signature(const char *payload, size_t plen, const char *signature,
73 size_t slen, struct signature_check *sigc)
75 struct strbuf gpg_output = STRBUF_INIT;
76 struct strbuf gpg_status = STRBUF_INIT;
81 status = verify_signed_buffer(payload, plen, signature, slen,
82 &gpg_output, &gpg_status);
83 if (status && !gpg_output.len)
85 sigc->payload = xmemdupz(payload, plen);
86 sigc->gpg_output = strbuf_detach(&gpg_output, NULL);
87 sigc->gpg_status = strbuf_detach(&gpg_status, NULL);
88 parse_gpg_output(sigc);
91 strbuf_release(&gpg_status);
92 strbuf_release(&gpg_output);
94 return sigc->result != 'G' && sigc->result != 'U';
97 void print_signature_buffer(const struct signature_check *sigc, unsigned flags)
99 const char *output = flags & GPG_VERIFY_RAW ?
100 sigc->gpg_status : sigc->gpg_output;
102 if (flags & GPG_VERIFY_VERBOSE && sigc->payload)
103 fputs(sigc->payload, stdout);
106 fputs(output, stderr);
110 * Look at GPG signed content (e.g. a signed tag object), whose
111 * payload is followed by a detached signature on it. Return the
112 * offset where the embedded detached signature begins, or the end of
113 * the data when there is no such signature.
115 size_t parse_signature(const char *buf, unsigned long size)
119 while (len < size && !starts_with(buf + len, PGP_SIGNATURE) &&
120 !starts_with(buf + len, PGP_MESSAGE)) {
121 eol = memchr(buf + len, '\n', size - len);
122 len += eol ? eol - (buf + len) + 1 : size - len;
127 void set_signing_key(const char *key)
129 free(configured_signing_key);
130 configured_signing_key = xstrdup(key);
133 int git_gpg_config(const char *var, const char *value, void *cb)
135 if (!strcmp(var, "user.signingkey")) {
136 set_signing_key(value);
138 if (!strcmp(var, "gpg.program")) {
140 return config_error_nonbool(var);
141 gpg_program = xstrdup(value);
146 const char *get_signing_key(void)
148 if (configured_signing_key)
149 return configured_signing_key;
150 return git_committer_info(IDENT_STRICT|IDENT_NO_DATE);
154 * Create a detached signature for the contents of "buffer" and append
155 * it after "signature"; "buffer" and "signature" can be the same
156 * strbuf instance, which would cause the detached signature appended
159 int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *signing_key)
161 struct child_process gpg = CHILD_PROCESS_INIT;
164 struct strbuf gpg_status = STRBUF_INIT;
166 argv_array_pushl(&gpg.args,
169 "-bsau", signing_key,
172 bottom = signature->len;
175 * When the username signingkey is bad, program could be terminated
176 * because gpg exits without reading and then write gets SIGPIPE.
178 sigchain_push(SIGPIPE, SIG_IGN);
179 ret = pipe_command(&gpg, buffer->buf, buffer->len,
180 signature, 1024, &gpg_status, 0);
181 sigchain_pop(SIGPIPE);
183 ret |= !strstr(gpg_status.buf, "\n[GNUPG:] SIG_CREATED ");
184 strbuf_release(&gpg_status);
186 return error(_("gpg failed to sign the data"));
188 /* Strip CR from the line endings, in case we are on Windows. */
189 for (i = j = bottom; i < signature->len; i++)
190 if (signature->buf[i] != '\r') {
192 signature->buf[j] = signature->buf[i];
195 strbuf_setlen(signature, j);
201 * Run "gpg" to see if the payload matches the detached signature.
202 * gpg_output, when set, receives the diagnostic output from GPG.
203 * gpg_status, when set, receives the status output from GPG.
205 int verify_signed_buffer(const char *payload, size_t payload_size,
206 const char *signature, size_t signature_size,
207 struct strbuf *gpg_output, struct strbuf *gpg_status)
209 struct child_process gpg = CHILD_PROCESS_INIT;
210 static struct tempfile temp;
212 struct strbuf buf = STRBUF_INIT;
214 fd = mks_tempfile_t(&temp, ".git_vtag_tmpXXXXXX");
216 return error_errno(_("could not create temporary file"));
217 if (write_in_full(fd, signature, signature_size) < 0) {
218 error_errno(_("failed writing detached signature to '%s'"),
220 delete_tempfile(&temp);
225 argv_array_pushl(&gpg.args,
228 "--keyid-format=long",
229 "--verify", temp.filename.buf, "-",
235 sigchain_push(SIGPIPE, SIG_IGN);
236 ret = pipe_command(&gpg, payload, payload_size,
237 gpg_status, 0, gpg_output, 0);
238 sigchain_pop(SIGPIPE);
240 delete_tempfile(&temp);
242 ret |= !strstr(gpg_status->buf, "\n[GNUPG:] GOODSIG ");
243 strbuf_release(&buf); /* no matter it was used or not */