gpg-interface: provide clear helper for struct signature_check
[git/raj.git] / gpg-interface.c
blobe71b59daf67b28f0699035e7834bb6c0b56c5425
1 #include "cache.h"
2 #include "run-command.h"
3 #include "strbuf.h"
4 #include "gpg-interface.h"
5 #include "sigchain.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);
14 free(sigc->signer);
15 free(sigc->key);
16 sigc->gpg_output = NULL;
17 sigc->gpg_status = NULL;
18 sigc->signer = NULL;
19 sigc->key = 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")) {
34 if (!value)
35 return config_error_nonbool(var);
36 gpg_program = xstrdup(value);
38 return 0;
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
52 * at the end.
54 int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *signing_key)
56 struct child_process gpg;
57 const char *args[4];
58 ssize_t len;
59 size_t i, j, bottom;
61 memset(&gpg, 0, sizeof(gpg));
62 gpg.argv = args;
63 gpg.in = -1;
64 gpg.out = -1;
65 args[0] = gpg_program;
66 args[1] = "-bsau";
67 args[2] = signing_key;
68 args[3] = NULL;
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) {
80 close(gpg.in);
81 close(gpg.out);
82 finish_command(&gpg);
83 return error(_("gpg did not accept the data"));
85 close(gpg.in);
87 bottom = signature->len;
88 len = strbuf_read(signature, gpg.out, 1024);
89 close(gpg.out);
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') {
99 if (i != j)
100 signature->buf[j] = signature->buf[i];
101 j++;
103 strbuf_setlen(signature, j);
105 return 0;
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};
119 char path[PATH_MAX];
120 int fd, ret;
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");
126 if (fd < 0)
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));
132 close(fd);
134 memset(&gpg, 0, sizeof(gpg));
135 gpg.argv = args_gpg;
136 gpg.in = -1;
137 gpg.out = -1;
138 if (gpg_output)
139 gpg.err = -1;
140 args_gpg[3] = path;
141 if (start_command(&gpg)) {
142 unlink(path);
143 return error(_("could not run gpg."));
146 write_in_full(gpg.in, payload, payload_size);
147 close(gpg.in);
149 if (gpg_output) {
150 strbuf_read(gpg_output, gpg.err, 0);
151 close(gpg.err);
153 if (gpg_status)
154 pbuf = gpg_status;
155 strbuf_read(pbuf, gpg.out, 0);
156 close(gpg.out);
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 */
165 return ret;