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