]> git.ipfire.org Git - thirdparty/git.git/blame - gpg-interface.c
Merge branch 'jk/prune-top-level-refs-after-packing'
[thirdparty/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 9
01e57b5d
MG
10void signature_check_clear(struct signature_check *sigc)
11{
71c214c8 12 free(sigc->payload);
01e57b5d
MG
13 free(sigc->gpg_output);
14 free(sigc->gpg_status);
15 free(sigc->signer);
16 free(sigc->key);
71c214c8 17 sigc->payload = NULL;
01e57b5d
MG
18 sigc->gpg_output = NULL;
19 sigc->gpg_status = NULL;
20 sigc->signer = NULL;
21 sigc->key = NULL;
22}
23
2f47eae2
JH
24void set_signing_key(const char *key)
25{
26 free(configured_signing_key);
27 configured_signing_key = xstrdup(key);
28}
29
30int git_gpg_config(const char *var, const char *value, void *cb)
31{
32 if (!strcmp(var, "user.signingkey")) {
0c5e70f0
JH
33 set_signing_key(value);
34 }
35 if (!strcmp(var, "gpg.program")) {
2f47eae2
JH
36 if (!value)
37 return config_error_nonbool(var);
0c5e70f0 38 gpg_program = xstrdup(value);
2f47eae2
JH
39 }
40 return 0;
41}
42
43const char *get_signing_key(void)
44{
45 if (configured_signing_key)
46 return configured_signing_key;
f9bc573f 47 return git_committer_info(IDENT_STRICT|IDENT_NO_DATE);
2f47eae2
JH
48}
49
50/*
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.
55 */
56int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *signing_key)
57{
d3180279 58 struct child_process gpg = CHILD_PROCESS_INIT;
2f47eae2
JH
59 const char *args[4];
60 ssize_t len;
61 size_t i, j, bottom;
62
2f47eae2
JH
63 gpg.argv = args;
64 gpg.in = -1;
65 gpg.out = -1;
0c5e70f0 66 args[0] = gpg_program;
2f47eae2
JH
67 args[1] = "-bsau";
68 args[2] = signing_key;
69 args[3] = NULL;
70
71 if (start_command(&gpg))
72 return error(_("could not run gpg."));
73
74 /*
75 * When the username signingkey is bad, program could be terminated
76 * because gpg exits without reading and then write gets SIGPIPE.
77 */
78 sigchain_push(SIGPIPE, SIG_IGN);
79
80 if (write_in_full(gpg.in, buffer->buf, buffer->len) != buffer->len) {
81 close(gpg.in);
82 close(gpg.out);
83 finish_command(&gpg);
84 return error(_("gpg did not accept the data"));
85 }
86 close(gpg.in);
87
88 bottom = signature->len;
89 len = strbuf_read(signature, gpg.out, 1024);
90 close(gpg.out);
91
92 sigchain_pop(SIGPIPE);
93
94 if (finish_command(&gpg) || !len || len < 0)
95 return error(_("gpg failed to sign the data"));
96
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') {
100 if (i != j)
101 signature->buf[j] = signature->buf[i];
102 j++;
103 }
104 strbuf_setlen(signature, j);
105
106 return 0;
107}
108
109/*
110 * Run "gpg" to see if the payload matches the detached signature.
e3f55e07 111 * gpg_output, when set, receives the diagnostic output from GPG.
b60b7566 112 * gpg_status, when set, receives the status output from GPG.
2f47eae2
JH
113 */
114int verify_signed_buffer(const char *payload, size_t payload_size,
115 const char *signature, size_t signature_size,
9cc4ac8f 116 struct strbuf *gpg_output, struct strbuf *gpg_status)
2f47eae2 117{
d3180279 118 struct child_process gpg = CHILD_PROCESS_INIT;
b60b7566 119 const char *args_gpg[] = {NULL, "--status-fd=1", "--verify", "FILE", "-", NULL};
2f47eae2
JH
120 char path[PATH_MAX];
121 int fd, ret;
b60b7566 122 struct strbuf buf = STRBUF_INIT;
9cc4ac8f 123 struct strbuf *pbuf = &buf;
2f47eae2 124
0c5e70f0 125 args_gpg[0] = gpg_program;
2f47eae2
JH
126 fd = git_mkstemp(path, PATH_MAX, ".git_vtag_tmpXXXXXX");
127 if (fd < 0)
4c9a4182 128 return error(_("could not create temporary file '%s': %s"),
2f47eae2
JH
129 path, strerror(errno));
130 if (write_in_full(fd, signature, signature_size) < 0)
4c9a4182 131 return error(_("failed writing detached signature to '%s': %s"),
2f47eae2
JH
132 path, strerror(errno));
133 close(fd);
134
2f47eae2
JH
135 gpg.argv = args_gpg;
136 gpg.in = -1;
b60b7566 137 gpg.out = -1;
2f47eae2
JH
138 if (gpg_output)
139 gpg.err = -1;
b60b7566 140 args_gpg[3] = path;
2f47eae2
JH
141 if (start_command(&gpg)) {
142 unlink(path);
4c9a4182 143 return error(_("could not run gpg."));
2f47eae2
JH
144 }
145
146 write_in_full(gpg.in, payload, payload_size);
147 close(gpg.in);
148
7dac3f83 149 if (gpg_output) {
2f47eae2 150 strbuf_read(gpg_output, gpg.err, 0);
7dac3f83
SB
151 close(gpg.err);
152 }
9cc4ac8f
MG
153 if (gpg_status)
154 pbuf = gpg_status;
155 strbuf_read(pbuf, gpg.out, 0);
b60b7566
MG
156 close(gpg.out);
157
2f47eae2
JH
158 ret = finish_command(&gpg);
159
160 unlink_or_warn(path);
161
9cc4ac8f
MG
162 ret |= !strstr(pbuf->buf, "\n[GNUPG:] GOODSIG ");
163 strbuf_release(&buf); /* no matter it was used or not */
b60b7566 164
2f47eae2
JH
165 return ret;
166}