rebase -i: use full object name internally throughout the script
[alt-git.git] / gpg-interface.c
blobff07012726ea28daa2551966d0555d2e8efa2375
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->payload);
13 free(sigc->gpg_output);
14 free(sigc->gpg_status);
15 free(sigc->signer);
16 free(sigc->key);
17 sigc->payload = NULL;
18 sigc->gpg_output = NULL;
19 sigc->gpg_status = NULL;
20 sigc->signer = NULL;
21 sigc->key = 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")) {
36 if (!value)
37 return config_error_nonbool(var);
38 gpg_program = xstrdup(value);
40 return 0;
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
54 * at the end.
56 int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *signing_key)
58 struct child_process gpg;
59 const char *args[4];
60 ssize_t len;
61 size_t i, j, bottom;
63 memset(&gpg, 0, sizeof(gpg));
64 gpg.argv = args;
65 gpg.in = -1;
66 gpg.out = -1;
67 args[0] = gpg_program;
68 args[1] = "-bsau";
69 args[2] = signing_key;
70 args[3] = NULL;
72 if (start_command(&gpg))
73 return error(_("could not run gpg."));
76 * When the username signingkey is bad, program could be terminated
77 * because gpg exits without reading and then write gets SIGPIPE.
79 sigchain_push(SIGPIPE, SIG_IGN);
81 if (write_in_full(gpg.in, buffer->buf, buffer->len) != buffer->len) {
82 close(gpg.in);
83 close(gpg.out);
84 finish_command(&gpg);
85 return error(_("gpg did not accept the data"));
87 close(gpg.in);
89 bottom = signature->len;
90 len = strbuf_read(signature, gpg.out, 1024);
91 close(gpg.out);
93 sigchain_pop(SIGPIPE);
95 if (finish_command(&gpg) || !len || len < 0)
96 return error(_("gpg failed to sign the data"));
98 /* Strip CR from the line endings, in case we are on Windows. */
99 for (i = j = bottom; i < signature->len; i++)
100 if (signature->buf[i] != '\r') {
101 if (i != j)
102 signature->buf[j] = signature->buf[i];
103 j++;
105 strbuf_setlen(signature, j);
107 return 0;
111 * Run "gpg" to see if the payload matches the detached signature.
112 * gpg_output, when set, receives the diagnostic output from GPG.
113 * gpg_status, when set, receives the status output from GPG.
115 int verify_signed_buffer(const char *payload, size_t payload_size,
116 const char *signature, size_t signature_size,
117 struct strbuf *gpg_output, struct strbuf *gpg_status)
119 struct child_process gpg;
120 const char *args_gpg[] = {NULL, "--status-fd=1", "--verify", "FILE", "-", NULL};
121 char path[PATH_MAX];
122 int fd, ret;
123 struct strbuf buf = STRBUF_INIT;
124 struct strbuf *pbuf = &buf;
126 args_gpg[0] = gpg_program;
127 fd = git_mkstemp(path, PATH_MAX, ".git_vtag_tmpXXXXXX");
128 if (fd < 0)
129 return error(_("could not create temporary file '%s': %s"),
130 path, strerror(errno));
131 if (write_in_full(fd, signature, signature_size) < 0)
132 return error(_("failed writing detached signature to '%s': %s"),
133 path, strerror(errno));
134 close(fd);
136 memset(&gpg, 0, sizeof(gpg));
137 gpg.argv = args_gpg;
138 gpg.in = -1;
139 gpg.out = -1;
140 if (gpg_output)
141 gpg.err = -1;
142 args_gpg[3] = path;
143 if (start_command(&gpg)) {
144 unlink(path);
145 return error(_("could not run gpg."));
148 write_in_full(gpg.in, payload, payload_size);
149 close(gpg.in);
151 if (gpg_output) {
152 strbuf_read(gpg_output, gpg.err, 0);
153 close(gpg.err);
155 if (gpg_status)
156 pbuf = gpg_status;
157 strbuf_read(pbuf, gpg.out, 0);
158 close(gpg.out);
160 ret = finish_command(&gpg);
162 unlink_or_warn(path);
164 ret |= !strstr(pbuf->buf, "\n[GNUPG:] GOODSIG ");
165 strbuf_release(&buf); /* no matter it was used or not */
167 return ret;