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;
24 void set_signing_key(const char *key)
26 free(configured_signing_key);
27 configured_signing_key = xstrdup(key);
30 int git_gpg_config(const char *var, const char *value, void *cb)
32 if (!strcmp(var, "user.signingkey")) {
33 set_signing_key(value);
35 if (!strcmp(var, "gpg.program")) {
37 return config_error_nonbool(var);
38 gpg_program = xstrdup(value);
43 const char *get_signing_key(void)
45 if (configured_signing_key)
46 return configured_signing_key;
47 return git_committer_info(IDENT_STRICT|IDENT_NO_DATE);
51 * Create a detached signature for the contents of "buffer" and append
52 * it after "signature"; "buffer" and "signature" can be the same
53 * strbuf instance, which would cause the detached signature appended
56 int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *signing_key)
58 struct child_process gpg = CHILD_PROCESS_INIT;
66 args[0] = gpg_program;
68 args[2] = signing_key;
71 if (start_command(&gpg))
72 return error(_("could not run gpg."));
75 * When the username signingkey is bad, program could be terminated
76 * because gpg exits without reading and then write gets SIGPIPE.
78 sigchain_push(SIGPIPE, SIG_IGN);
80 if (write_in_full(gpg.in, buffer->buf, buffer->len) != buffer->len) {
84 return error(_("gpg did not accept the data"));
88 bottom = signature->len;
89 len = strbuf_read(signature, gpg.out, 1024);
92 sigchain_pop(SIGPIPE);
94 if (finish_command(&gpg) || !len || len < 0)
95 return error(_("gpg failed to sign the data"));
97 /* Strip CR from the line endings, in case we are on Windows. */
98 for (i = j = bottom; i < signature->len; i++)
99 if (signature->buf[i] != '\r') {
101 signature->buf[j] = signature->buf[i];
104 strbuf_setlen(signature, j);
110 * Run "gpg" to see if the payload matches the detached signature.
111 * gpg_output, when set, receives the diagnostic output from GPG.
112 * gpg_status, when set, receives the status output from GPG.
114 int verify_signed_buffer(const char *payload, size_t payload_size,
115 const char *signature, size_t signature_size,
116 struct strbuf *gpg_output, struct strbuf *gpg_status)
118 struct child_process gpg = CHILD_PROCESS_INIT;
119 const char *args_gpg[] = {NULL, "--status-fd=1", "--verify", "FILE", "-", NULL};
122 struct strbuf buf = STRBUF_INIT;
123 struct strbuf *pbuf = &buf;
125 args_gpg[0] = gpg_program;
126 fd = git_mkstemp(path, PATH_MAX, ".git_vtag_tmpXXXXXX");
128 return error(_("could not create temporary file '%s': %s"),
129 path, strerror(errno));
130 if (write_in_full(fd, signature, signature_size) < 0)
131 return error(_("failed writing detached signature to '%s': %s"),
132 path, strerror(errno));
141 if (start_command(&gpg)) {
143 return error(_("could not run gpg."));
146 write_in_full(gpg.in, payload, payload_size);
150 strbuf_read(gpg_output, gpg.err, 0);
155 strbuf_read(pbuf, gpg.out, 0);
158 ret = finish_command(&gpg);
160 unlink_or_warn(path);
162 ret |= !strstr(pbuf->buf, "\n[GNUPG:] GOODSIG ");
163 strbuf_release(&buf); /* no matter it was used or not */