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)
12 free(sigc->gpg_output);
13 free(sigc->gpg_status);
16 sigc->gpg_output = NULL;
17 sigc->gpg_status = NULL;
22 void set_signing_key(const char *key)
24 free(configured_signing_key);
25 configured_signing_key = xstrdup(key);
28 int git_gpg_config(const char *var, const char *value, void *cb)
30 if (!strcmp(var, "user.signingkey")) {
31 set_signing_key(value);
33 if (!strcmp(var, "gpg.program")) {
35 return config_error_nonbool(var);
36 gpg_program = xstrdup(value);
41 const char *get_signing_key(void)
43 if (configured_signing_key)
44 return configured_signing_key;
45 return git_committer_info(IDENT_STRICT|IDENT_NO_DATE);
49 * Create a detached signature for the contents of "buffer" and append
50 * it after "signature"; "buffer" and "signature" can be the same
51 * strbuf instance, which would cause the detached signature appended
54 int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *signing_key)
56 struct child_process gpg;
61 memset(&gpg, 0, sizeof(gpg));
65 args[0] = gpg_program;
67 args[2] = signing_key;
70 if (start_command(&gpg))
71 return error(_("could not run gpg."));
74 * When the username signingkey is bad, program could be terminated
75 * because gpg exits without reading and then write gets SIGPIPE.
77 sigchain_push(SIGPIPE, SIG_IGN);
79 if (write_in_full(gpg.in, buffer->buf, buffer->len) != buffer->len) {
83 return error(_("gpg did not accept the data"));
87 bottom = signature->len;
88 len = strbuf_read(signature, gpg.out, 1024);
91 sigchain_pop(SIGPIPE);
93 if (finish_command(&gpg) || !len || len < 0)
94 return error(_("gpg failed to sign the data"));
96 /* Strip CR from the line endings, in case we are on Windows. */
97 for (i = j = bottom; i < signature->len; i++)
98 if (signature->buf[i] != '\r') {
100 signature->buf[j] = signature->buf[i];
103 strbuf_setlen(signature, j);
109 * Run "gpg" to see if the payload matches the detached signature.
110 * gpg_output, when set, receives the diagnostic output from GPG.
111 * gpg_status, when set, receives the status output from GPG.
113 int verify_signed_buffer(const char *payload, size_t payload_size,
114 const char *signature, size_t signature_size,
115 struct strbuf *gpg_output, struct strbuf *gpg_status)
117 struct child_process gpg;
118 const char *args_gpg[] = {NULL, "--status-fd=1", "--verify", "FILE", "-", NULL};
121 struct strbuf buf = STRBUF_INIT;
122 struct strbuf *pbuf = &buf;
124 args_gpg[0] = gpg_program;
125 fd = git_mkstemp(path, PATH_MAX, ".git_vtag_tmpXXXXXX");
127 return error(_("could not create temporary file '%s': %s"),
128 path, strerror(errno));
129 if (write_in_full(fd, signature, signature_size) < 0)
130 return error(_("failed writing detached signature to '%s': %s"),
131 path, strerror(errno));
134 memset(&gpg, 0, sizeof(gpg));
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 */