2 #include "run-command.h"
4 #include "gpg-interface.h"
7 static char *configured_signing_key;
8 static const char *gpg_program = "gpg";
10 void signature_check_clear(struct signature_check *sigc)
13 free(sigc->gpg_output);
14 free(sigc->gpg_status);
18 sigc->gpg_output = NULL;
19 sigc->gpg_status = NULL;
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" },
34 void parse_gpg_output(struct signature_check *sigc)
36 const char *buf = sigc->gpg_status;
39 /* Iterate over all search strings */
40 for (i = 0; i < ARRAY_SIZE(sigcheck_gpg_status); i++) {
41 const char *found, *next;
43 if (!skip_prefix(buf, sigcheck_gpg_status[i].check + 1, &found)) {
44 found = strstr(buf, sigcheck_gpg_status[i].check);
47 found += strlen(sigcheck_gpg_status[i].check);
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);
54 next = strchrnul(found, '\n');
55 sigc->signer = xmemdupz(found, next - found);
60 void set_signing_key(const char *key)
62 free(configured_signing_key);
63 configured_signing_key = xstrdup(key);
66 int git_gpg_config(const char *var, const char *value, void *cb)
68 if (!strcmp(var, "user.signingkey")) {
69 set_signing_key(value);
71 if (!strcmp(var, "gpg.program")) {
73 return config_error_nonbool(var);
74 gpg_program = xstrdup(value);
79 const char *get_signing_key(void)
81 if (configured_signing_key)
82 return configured_signing_key;
83 return git_committer_info(IDENT_STRICT|IDENT_NO_DATE);
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
92 int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *signing_key)
94 struct child_process gpg;
99 memset(&gpg, 0, sizeof(gpg));
103 args[0] = gpg_program;
105 args[2] = signing_key;
108 if (start_command(&gpg))
109 return error(_("could not run gpg."));
112 * When the username signingkey is bad, program could be terminated
113 * because gpg exits without reading and then write gets SIGPIPE.
115 sigchain_push(SIGPIPE, SIG_IGN);
117 if (write_in_full(gpg.in, buffer->buf, buffer->len) != buffer->len) {
120 finish_command(&gpg);
121 return error(_("gpg did not accept the data"));
125 bottom = signature->len;
126 len = strbuf_read(signature, gpg.out, 1024);
129 sigchain_pop(SIGPIPE);
131 if (finish_command(&gpg) || !len || len < 0)
132 return error(_("gpg failed to sign the data"));
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') {
138 signature->buf[j] = signature->buf[i];
141 strbuf_setlen(signature, j);
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.
151 int 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)
155 struct child_process gpg;
156 const char *args_gpg[] = {NULL, "--status-fd=1", "--verify", "FILE", "-", NULL};
159 struct strbuf buf = STRBUF_INIT;
160 struct strbuf *pbuf = &buf;
162 args_gpg[0] = gpg_program;
163 fd = git_mkstemp(path, PATH_MAX, ".git_vtag_tmpXXXXXX");
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));
172 memset(&gpg, 0, sizeof(gpg));
179 if (start_command(&gpg)) {
181 return error(_("could not run gpg."));
184 write_in_full(gpg.in, payload, payload_size);
188 strbuf_read(gpg_output, gpg.err, 0);
193 strbuf_read(pbuf, gpg.out, 0);
196 ret = finish_command(&gpg);
198 unlink_or_warn(path);
200 ret |= !strstr(pbuf->buf, "\n[GNUPG:] GOODSIG ");
201 strbuf_release(&buf); /* no matter it was used or not */