Second batch for 2.1
[git/git.git] / gpg-interface.c
CommitLineData
2f47eae2
JH
1#include "cache.h"
2#include "run-command.h"
3#include "strbuf.h"
4#include "gpg-interface.h"
5#include "sigchain.h"
6
7static char *configured_signing_key;
0c5e70f0 8static const char *gpg_program = "gpg";
2f47eae2
JH
9
10void set_signing_key(const char *key)
11{
12 free(configured_signing_key);
13 configured_signing_key = xstrdup(key);
14}
15
16int git_gpg_config(const char *var, const char *value, void *cb)
17{
18 if (!strcmp(var, "user.signingkey")) {
0c5e70f0
JH
19 set_signing_key(value);
20 }
21 if (!strcmp(var, "gpg.program")) {
2f47eae2
JH
22 if (!value)
23 return config_error_nonbool(var);
0c5e70f0 24 gpg_program = xstrdup(value);
2f47eae2
JH
25 }
26 return 0;
27}
28
29const char *get_signing_key(void)
30{
31 if (configured_signing_key)
32 return configured_signing_key;
f9bc573f 33 return git_committer_info(IDENT_STRICT|IDENT_NO_DATE);
2f47eae2
JH
34}
35
36/*
37 * Create a detached signature for the contents of "buffer" and append
38 * it after "signature"; "buffer" and "signature" can be the same
39 * strbuf instance, which would cause the detached signature appended
40 * at the end.
41 */
42int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *signing_key)
43{
44 struct child_process gpg;
45 const char *args[4];
46 ssize_t len;
47 size_t i, j, bottom;
48
49 memset(&gpg, 0, sizeof(gpg));
50 gpg.argv = args;
51 gpg.in = -1;
52 gpg.out = -1;
0c5e70f0 53 args[0] = gpg_program;
2f47eae2
JH
54 args[1] = "-bsau";
55 args[2] = signing_key;
56 args[3] = NULL;
57
58 if (start_command(&gpg))
59 return error(_("could not run gpg."));
60
61 /*
62 * When the username signingkey is bad, program could be terminated
63 * because gpg exits without reading and then write gets SIGPIPE.
64 */
65 sigchain_push(SIGPIPE, SIG_IGN);
66
67 if (write_in_full(gpg.in, buffer->buf, buffer->len) != buffer->len) {
68 close(gpg.in);
69 close(gpg.out);
70 finish_command(&gpg);
71 return error(_("gpg did not accept the data"));
72 }
73 close(gpg.in);
74
75 bottom = signature->len;
76 len = strbuf_read(signature, gpg.out, 1024);
77 close(gpg.out);
78
79 sigchain_pop(SIGPIPE);
80
81 if (finish_command(&gpg) || !len || len < 0)
82 return error(_("gpg failed to sign the data"));
83
84 /* Strip CR from the line endings, in case we are on Windows. */
85 for (i = j = bottom; i < signature->len; i++)
86 if (signature->buf[i] != '\r') {
87 if (i != j)
88 signature->buf[j] = signature->buf[i];
89 j++;
90 }
91 strbuf_setlen(signature, j);
92
93 return 0;
94}
95
96/*
97 * Run "gpg" to see if the payload matches the detached signature.
e3f55e07 98 * gpg_output, when set, receives the diagnostic output from GPG.
b60b7566 99 * gpg_status, when set, receives the status output from GPG.
2f47eae2
JH
100 */
101int verify_signed_buffer(const char *payload, size_t payload_size,
102 const char *signature, size_t signature_size,
9cc4ac8f 103 struct strbuf *gpg_output, struct strbuf *gpg_status)
2f47eae2
JH
104{
105 struct child_process gpg;
b60b7566 106 const char *args_gpg[] = {NULL, "--status-fd=1", "--verify", "FILE", "-", NULL};
2f47eae2
JH
107 char path[PATH_MAX];
108 int fd, ret;
b60b7566 109 struct strbuf buf = STRBUF_INIT;
9cc4ac8f 110 struct strbuf *pbuf = &buf;
2f47eae2 111
0c5e70f0 112 args_gpg[0] = gpg_program;
2f47eae2
JH
113 fd = git_mkstemp(path, PATH_MAX, ".git_vtag_tmpXXXXXX");
114 if (fd < 0)
4c9a4182 115 return error(_("could not create temporary file '%s': %s"),
2f47eae2
JH
116 path, strerror(errno));
117 if (write_in_full(fd, signature, signature_size) < 0)
4c9a4182 118 return error(_("failed writing detached signature to '%s': %s"),
2f47eae2
JH
119 path, strerror(errno));
120 close(fd);
121
122 memset(&gpg, 0, sizeof(gpg));
123 gpg.argv = args_gpg;
124 gpg.in = -1;
b60b7566 125 gpg.out = -1;
2f47eae2
JH
126 if (gpg_output)
127 gpg.err = -1;
b60b7566 128 args_gpg[3] = path;
2f47eae2
JH
129 if (start_command(&gpg)) {
130 unlink(path);
4c9a4182 131 return error(_("could not run gpg."));
2f47eae2
JH
132 }
133
134 write_in_full(gpg.in, payload, payload_size);
135 close(gpg.in);
136
7dac3f83 137 if (gpg_output) {
2f47eae2 138 strbuf_read(gpg_output, gpg.err, 0);
7dac3f83
SB
139 close(gpg.err);
140 }
9cc4ac8f
MG
141 if (gpg_status)
142 pbuf = gpg_status;
143 strbuf_read(pbuf, gpg.out, 0);
b60b7566
MG
144 close(gpg.out);
145
2f47eae2
JH
146 ret = finish_command(&gpg);
147
148 unlink_or_warn(path);
149
9cc4ac8f
MG
150 ret |= !strstr(pbuf->buf, "\n[GNUPG:] GOODSIG ");
151 strbuf_release(&buf); /* no matter it was used or not */
b60b7566 152
2f47eae2
JH
153 return ret;
154}