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