Commit | Line | Data |
---|---|---|
2f47eae2 | 1 | #include "cache.h" |
b2141fc1 | 2 | #include "config.h" |
2f47eae2 JH |
3 | #include "run-command.h" |
4 | #include "strbuf.h" | |
5 | #include "gpg-interface.h" | |
6 | #include "sigchain.h" | |
4322353b | 7 | #include "tempfile.h" |
2f47eae2 JH |
8 | |
9 | static char *configured_signing_key; | |
0c5e70f0 | 10 | static const char *gpg_program = "gpg"; |
2f47eae2 | 11 | |
d7c67668 JH |
12 | #define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----" |
13 | #define PGP_MESSAGE "-----BEGIN PGP MESSAGE-----" | |
14 | ||
01e57b5d MG |
15 | void signature_check_clear(struct signature_check *sigc) |
16 | { | |
88ce3ef6 ÆAB |
17 | FREE_AND_NULL(sigc->payload); |
18 | FREE_AND_NULL(sigc->gpg_output); | |
19 | FREE_AND_NULL(sigc->gpg_status); | |
20 | FREE_AND_NULL(sigc->signer); | |
21 | FREE_AND_NULL(sigc->key); | |
01e57b5d MG |
22 | } |
23 | ||
a50e7ca3 JH |
24 | static struct { |
25 | char result; | |
26 | const char *check; | |
27 | } sigcheck_gpg_status[] = { | |
28 | { 'G', "\n[GNUPG:] GOODSIG " }, | |
29 | { 'B', "\n[GNUPG:] BADSIG " }, | |
30 | { 'U', "\n[GNUPG:] TRUST_NEVER" }, | |
31 | { 'U', "\n[GNUPG:] TRUST_UNDEFINED" }, | |
661a1806 MG |
32 | { 'E', "\n[GNUPG:] ERRSIG "}, |
33 | { 'X', "\n[GNUPG:] EXPSIG "}, | |
34 | { 'Y', "\n[GNUPG:] EXPKEYSIG "}, | |
35 | { 'R', "\n[GNUPG:] REVKEYSIG "}, | |
a50e7ca3 JH |
36 | }; |
37 | ||
38 | void parse_gpg_output(struct signature_check *sigc) | |
39 | { | |
40 | const char *buf = sigc->gpg_status; | |
41 | int i; | |
42 | ||
43 | /* Iterate over all search strings */ | |
44 | for (i = 0; i < ARRAY_SIZE(sigcheck_gpg_status); i++) { | |
45 | const char *found, *next; | |
46 | ||
47 | if (!skip_prefix(buf, sigcheck_gpg_status[i].check + 1, &found)) { | |
48 | found = strstr(buf, sigcheck_gpg_status[i].check); | |
49 | if (!found) | |
50 | continue; | |
51 | found += strlen(sigcheck_gpg_status[i].check); | |
52 | } | |
53 | sigc->result = sigcheck_gpg_status[i].result; | |
54 | /* The trust messages are not followed by key/signer information */ | |
55 | if (sigc->result != 'U') { | |
56 | sigc->key = xmemdupz(found, 16); | |
661a1806 MG |
57 | /* The ERRSIG message is not followed by signer information */ |
58 | if (sigc-> result != 'E') { | |
59 | found += 17; | |
60 | next = strchrnul(found, '\n'); | |
61 | sigc->signer = xmemdupz(found, next - found); | |
62 | } | |
a50e7ca3 JH |
63 | } |
64 | } | |
65 | } | |
66 | ||
434060ec | 67 | int check_signature(const char *payload, size_t plen, const char *signature, |
a4cc18f2 | 68 | size_t slen, struct signature_check *sigc) |
69 | { | |
70 | struct strbuf gpg_output = STRBUF_INIT; | |
71 | struct strbuf gpg_status = STRBUF_INIT; | |
72 | int status; | |
73 | ||
74 | sigc->result = 'N'; | |
75 | ||
76 | status = verify_signed_buffer(payload, plen, signature, slen, | |
77 | &gpg_output, &gpg_status); | |
78 | if (status && !gpg_output.len) | |
79 | goto out; | |
80 | sigc->payload = xmemdupz(payload, plen); | |
81 | sigc->gpg_output = strbuf_detach(&gpg_output, NULL); | |
82 | sigc->gpg_status = strbuf_detach(&gpg_status, NULL); | |
83 | parse_gpg_output(sigc); | |
84 | ||
85 | out: | |
86 | strbuf_release(&gpg_status); | |
87 | strbuf_release(&gpg_output); | |
434060ec | 88 | |
89 | return sigc->result != 'G' && sigc->result != 'U'; | |
a4cc18f2 | 90 | } |
91 | ||
ca194d50 | 92 | void print_signature_buffer(const struct signature_check *sigc, unsigned flags) |
93 | { | |
aeff29dd | 94 | const char *output = flags & GPG_VERIFY_RAW ? |
95 | sigc->gpg_status : sigc->gpg_output; | |
96 | ||
ca194d50 | 97 | if (flags & GPG_VERIFY_VERBOSE && sigc->payload) |
98 | fputs(sigc->payload, stdout); | |
99 | ||
aeff29dd | 100 | if (output) |
101 | fputs(output, stderr); | |
ca194d50 | 102 | } |
103 | ||
f68f2dd5 JK |
104 | static int is_gpg_start(const char *line) |
105 | { | |
106 | return starts_with(line, PGP_SIGNATURE) || | |
107 | starts_with(line, PGP_MESSAGE); | |
108 | } | |
109 | ||
e6fa6cde | 110 | size_t parse_signature(const char *buf, size_t size) |
d7c67668 | 111 | { |
d7c67668 | 112 | size_t len = 0; |
f68f2dd5 | 113 | while (len < size && !is_gpg_start(buf + len)) { |
17ef3a42 | 114 | const char *eol = memchr(buf + len, '\n', size - len); |
d7c67668 JH |
115 | len += eol ? eol - (buf + len) + 1 : size - len; |
116 | } | |
117 | return len; | |
118 | } | |
119 | ||
2f47eae2 JH |
120 | void set_signing_key(const char *key) |
121 | { | |
122 | free(configured_signing_key); | |
123 | configured_signing_key = xstrdup(key); | |
124 | } | |
125 | ||
126 | int git_gpg_config(const char *var, const char *value, void *cb) | |
127 | { | |
128 | if (!strcmp(var, "user.signingkey")) { | |
1b0eeec3 JK |
129 | if (!value) |
130 | return config_error_nonbool(var); | |
0c5e70f0 | 131 | set_signing_key(value); |
1b0eeec3 | 132 | return 0; |
0c5e70f0 | 133 | } |
1b0eeec3 | 134 | |
0c5e70f0 | 135 | if (!strcmp(var, "gpg.program")) { |
2f47eae2 JH |
136 | if (!value) |
137 | return config_error_nonbool(var); | |
0c5e70f0 | 138 | gpg_program = xstrdup(value); |
1b0eeec3 | 139 | return 0; |
2f47eae2 | 140 | } |
1b0eeec3 | 141 | |
2f47eae2 JH |
142 | return 0; |
143 | } | |
144 | ||
145 | const char *get_signing_key(void) | |
146 | { | |
147 | if (configured_signing_key) | |
148 | return configured_signing_key; | |
f9bc573f | 149 | return git_committer_info(IDENT_STRICT|IDENT_NO_DATE); |
2f47eae2 JH |
150 | } |
151 | ||
2f47eae2 JH |
152 | int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *signing_key) |
153 | { | |
d3180279 | 154 | struct child_process gpg = CHILD_PROCESS_INIT; |
0581b546 | 155 | int ret; |
2f47eae2 | 156 | size_t i, j, bottom; |
efee9553 | 157 | struct strbuf gpg_status = STRBUF_INIT; |
2f47eae2 | 158 | |
aedb5dc3 JK |
159 | argv_array_pushl(&gpg.args, |
160 | gpg_program, | |
efee9553 | 161 | "--status-fd=2", |
aedb5dc3 JK |
162 | "-bsau", signing_key, |
163 | NULL); | |
2f47eae2 | 164 | |
0581b546 | 165 | bottom = signature->len; |
2f47eae2 JH |
166 | |
167 | /* | |
168 | * When the username signingkey is bad, program could be terminated | |
169 | * because gpg exits without reading and then write gets SIGPIPE. | |
170 | */ | |
171 | sigchain_push(SIGPIPE, SIG_IGN); | |
0581b546 | 172 | ret = pipe_command(&gpg, buffer->buf, buffer->len, |
efee9553 | 173 | signature, 1024, &gpg_status, 0); |
2f47eae2 JH |
174 | sigchain_pop(SIGPIPE); |
175 | ||
efee9553 MG |
176 | ret |= !strstr(gpg_status.buf, "\n[GNUPG:] SIG_CREATED "); |
177 | strbuf_release(&gpg_status); | |
178 | if (ret) | |
2f47eae2 JH |
179 | return error(_("gpg failed to sign the data")); |
180 | ||
181 | /* Strip CR from the line endings, in case we are on Windows. */ | |
182 | for (i = j = bottom; i < signature->len; i++) | |
183 | if (signature->buf[i] != '\r') { | |
184 | if (i != j) | |
185 | signature->buf[j] = signature->buf[i]; | |
186 | j++; | |
187 | } | |
188 | strbuf_setlen(signature, j); | |
189 | ||
190 | return 0; | |
191 | } | |
192 | ||
2f47eae2 JH |
193 | int verify_signed_buffer(const char *payload, size_t payload_size, |
194 | const char *signature, size_t signature_size, | |
9cc4ac8f | 195 | struct strbuf *gpg_output, struct strbuf *gpg_status) |
2f47eae2 | 196 | { |
d3180279 | 197 | struct child_process gpg = CHILD_PROCESS_INIT; |
076aa2cb JK |
198 | struct tempfile *temp; |
199 | int ret; | |
b60b7566 | 200 | struct strbuf buf = STRBUF_INIT; |
2f47eae2 | 201 | |
076aa2cb JK |
202 | temp = mks_tempfile_t(".git_vtag_tmpXXXXXX"); |
203 | if (!temp) | |
4322353b | 204 | return error_errno(_("could not create temporary file")); |
076aa2cb JK |
205 | if (write_in_full(temp->fd, signature, signature_size) < 0 || |
206 | close_tempfile_gently(temp) < 0) { | |
4322353b | 207 | error_errno(_("failed writing detached signature to '%s'"), |
076aa2cb | 208 | temp->filename.buf); |
4322353b JK |
209 | delete_tempfile(&temp); |
210 | return -1; | |
211 | } | |
2f47eae2 | 212 | |
aedb5dc3 JK |
213 | argv_array_pushl(&gpg.args, |
214 | gpg_program, | |
215 | "--status-fd=1", | |
af2b21ec | 216 | "--keyid-format=long", |
076aa2cb | 217 | "--verify", temp->filename.buf, "-", |
aedb5dc3 | 218 | NULL); |
2f47eae2 | 219 | |
c752fcc8 JK |
220 | if (!gpg_status) |
221 | gpg_status = &buf; | |
b60b7566 | 222 | |
0d2b664e JK |
223 | sigchain_push(SIGPIPE, SIG_IGN); |
224 | ret = pipe_command(&gpg, payload, payload_size, | |
225 | gpg_status, 0, gpg_output, 0); | |
d281b45d | 226 | sigchain_pop(SIGPIPE); |
2f47eae2 | 227 | |
4322353b | 228 | delete_tempfile(&temp); |
2f47eae2 | 229 | |
c752fcc8 | 230 | ret |= !strstr(gpg_status->buf, "\n[GNUPG:] GOODSIG "); |
9cc4ac8f | 231 | strbuf_release(&buf); /* no matter it was used or not */ |
b60b7566 | 232 | |
2f47eae2 JH |
233 | return ret; |
234 | } |