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