]>
Commit | Line | Data |
---|---|---|
1 | #include <config.h> | |
2 | ||
3 | #include <apt-pkg/acquire-method.h> | |
4 | #include <apt-pkg/configuration.h> | |
5 | #include <apt-pkg/error.h> | |
6 | #include <apt-pkg/gpgv.h> | |
7 | #include <apt-pkg/strutl.h> | |
8 | #include <apt-pkg/fileutl.h> | |
9 | #include "aptmethod.h" | |
10 | ||
11 | #include <ctype.h> | |
12 | #include <errno.h> | |
13 | #include <stddef.h> | |
14 | #include <stdio.h> | |
15 | #include <stdlib.h> | |
16 | #include <string.h> | |
17 | #include <sys/wait.h> | |
18 | #include <unistd.h> | |
19 | ||
20 | #include <array> | |
21 | #include <algorithm> | |
22 | #include <sstream> | |
23 | #include <iterator> | |
24 | #include <iostream> | |
25 | #include <string> | |
26 | #include <vector> | |
27 | ||
28 | #include <apti18n.h> | |
29 | ||
30 | using std::string; | |
31 | using std::vector; | |
32 | ||
33 | #define GNUPGPREFIX "[GNUPG:]" | |
34 | #define GNUPGBADSIG "[GNUPG:] BADSIG" | |
35 | #define GNUPGNOPUBKEY "[GNUPG:] NO_PUBKEY" | |
36 | #define GNUPGVALIDSIG "[GNUPG:] VALIDSIG" | |
37 | #define GNUPGGOODSIG "[GNUPG:] GOODSIG" | |
38 | #define GNUPGKEYEXPIRED "[GNUPG:] KEYEXPIRED" | |
39 | #define GNUPGREVKEYSIG "[GNUPG:] REVKEYSIG" | |
40 | #define GNUPGNODATA "[GNUPG:] NODATA" | |
41 | ||
42 | static const std::vector<string> WeakDigests { | |
43 | "1", // MD5 | |
44 | // "2", // SHA1 | |
45 | // "3", // RIPEMD-160 | |
46 | }; | |
47 | ||
48 | static const std::vector<string> DeprecatedDigests { | |
49 | "2", // SHA1 | |
50 | "3", // RIPEMD-160 | |
51 | }; | |
52 | ||
53 | class GPGVMethod : public aptMethod | |
54 | { | |
55 | private: | |
56 | string VerifyGetSigners(const char *file, const char *outfile, | |
57 | std::string const &key, | |
58 | vector<string> &GoodSigners, | |
59 | vector<string> &BadSigners, | |
60 | vector<string> &WorthlessSigners, | |
61 | vector<string> &SoonWorthlessSigners, | |
62 | vector<string> &NoPubKeySigners); | |
63 | protected: | |
64 | virtual bool URIAcquire(std::string const &Message, FetchItem *Itm) APT_OVERRIDE; | |
65 | public: | |
66 | ||
67 | GPGVMethod() : aptMethod("gpgv","1.0",SingleInstance | SendConfig) {}; | |
68 | }; | |
69 | ||
70 | string GPGVMethod::VerifyGetSigners(const char *file, const char *outfile, | |
71 | std::string const &key, | |
72 | vector<string> &GoodSigners, | |
73 | vector<string> &BadSigners, | |
74 | vector<string> &WorthlessSigners, | |
75 | vector<string> &SoonWorthlessSigners, | |
76 | vector<string> &NoPubKeySigners) | |
77 | { | |
78 | bool const Debug = _config->FindB("Debug::Acquire::gpgv", false); | |
79 | ||
80 | if (Debug == true) | |
81 | std::clog << "inside VerifyGetSigners" << std::endl; | |
82 | ||
83 | int fd[2]; | |
84 | bool const keyIsID = (key.empty() == false && key[0] != '/'); | |
85 | ||
86 | if (pipe(fd) < 0) | |
87 | return "Couldn't create pipe"; | |
88 | ||
89 | pid_t pid = fork(); | |
90 | if (pid < 0) | |
91 | return string("Couldn't spawn new process") + strerror(errno); | |
92 | else if (pid == 0) | |
93 | ExecGPGV(outfile, file, 3, fd, (keyIsID ? "" : key)); | |
94 | close(fd[1]); | |
95 | ||
96 | FILE *pipein = fdopen(fd[0], "r"); | |
97 | ||
98 | // Loop over the output of apt-key (which really is gnupg), and check the signatures. | |
99 | std::vector<std::string> ValidSigners; | |
100 | size_t buffersize = 0; | |
101 | char *buffer = NULL; | |
102 | while (1) | |
103 | { | |
104 | if (getline(&buffer, &buffersize, pipein) == -1) | |
105 | break; | |
106 | if (Debug == true) | |
107 | std::clog << "Read: " << buffer << std::endl; | |
108 | ||
109 | // Push the data into three separate vectors, which | |
110 | // we later concatenate. They're kept separate so | |
111 | // if we improve the apt method communication stuff later | |
112 | // it will be better. | |
113 | if (strncmp(buffer, GNUPGBADSIG, sizeof(GNUPGBADSIG)-1) == 0) | |
114 | { | |
115 | if (Debug == true) | |
116 | std::clog << "Got BADSIG! " << std::endl; | |
117 | BadSigners.push_back(string(buffer+sizeof(GNUPGPREFIX))); | |
118 | } | |
119 | else if (strncmp(buffer, GNUPGNOPUBKEY, sizeof(GNUPGNOPUBKEY)-1) == 0) | |
120 | { | |
121 | if (Debug == true) | |
122 | std::clog << "Got NO_PUBKEY " << std::endl; | |
123 | NoPubKeySigners.push_back(string(buffer+sizeof(GNUPGPREFIX))); | |
124 | } | |
125 | else if (strncmp(buffer, GNUPGNODATA, sizeof(GNUPGBADSIG)-1) == 0) | |
126 | { | |
127 | if (Debug == true) | |
128 | std::clog << "Got NODATA! " << std::endl; | |
129 | BadSigners.push_back(string(buffer+sizeof(GNUPGPREFIX))); | |
130 | } | |
131 | else if (strncmp(buffer, GNUPGKEYEXPIRED, sizeof(GNUPGKEYEXPIRED)-1) == 0) | |
132 | { | |
133 | if (Debug == true) | |
134 | std::clog << "Got KEYEXPIRED! " << std::endl; | |
135 | WorthlessSigners.push_back(string(buffer+sizeof(GNUPGPREFIX))); | |
136 | } | |
137 | else if (strncmp(buffer, GNUPGREVKEYSIG, sizeof(GNUPGREVKEYSIG)-1) == 0) | |
138 | { | |
139 | if (Debug == true) | |
140 | std::clog << "Got REVKEYSIG! " << std::endl; | |
141 | WorthlessSigners.push_back(string(buffer+sizeof(GNUPGPREFIX))); | |
142 | } | |
143 | else if (strncmp(buffer, GNUPGGOODSIG, sizeof(GNUPGGOODSIG)-1) == 0) | |
144 | { | |
145 | char *sig = buffer + sizeof(GNUPGPREFIX); | |
146 | char *p = sig + sizeof("GOODSIG"); | |
147 | while (*p && isxdigit(*p)) | |
148 | p++; | |
149 | *p = 0; | |
150 | if (Debug == true) | |
151 | std::clog << "Got GOODSIG, key ID:" << sig << std::endl; | |
152 | GoodSigners.push_back(string(sig)); | |
153 | } | |
154 | else if (strncmp(buffer, GNUPGVALIDSIG, sizeof(GNUPGVALIDSIG)-1) == 0) | |
155 | { | |
156 | char *sig = buffer + sizeof(GNUPGVALIDSIG); | |
157 | std::istringstream iss((string(sig))); | |
158 | vector<string> tokens{std::istream_iterator<string>{iss}, | |
159 | std::istream_iterator<string>{}}; | |
160 | char *p = sig; | |
161 | while (*p && isxdigit(*p)) | |
162 | p++; | |
163 | *p = 0; | |
164 | if (Debug == true) | |
165 | std::clog << "Got VALIDSIG, key ID: " << sig << std::endl; | |
166 | // Reject weak digest algorithms | |
167 | if (std::find(DeprecatedDigests.begin(), DeprecatedDigests.end(), tokens[7]) != DeprecatedDigests.end()) | |
168 | { | |
169 | // Treat them like an expired key: For that a message about expiry | |
170 | // is emitted, a VALIDSIG, but no GOODSIG. | |
171 | SoonWorthlessSigners.push_back(string(sig)); | |
172 | } | |
173 | if (std::find(WeakDigests.begin(), WeakDigests.end(), tokens[7]) != WeakDigests.end()) | |
174 | { | |
175 | // Treat them like an expired key: For that a message about expiry | |
176 | // is emitted, a VALIDSIG, but no GOODSIG. | |
177 | WorthlessSigners.push_back("WEAKDIGEST " + string(sig)); | |
178 | GoodSigners.erase(std::remove(GoodSigners.begin(), GoodSigners.end(), string(sig))); | |
179 | } | |
180 | ||
181 | ValidSigners.push_back(string(sig)); | |
182 | } | |
183 | } | |
184 | fclose(pipein); | |
185 | free(buffer); | |
186 | ||
187 | // apt-key has a --keyid parameter, but this requires gpg, so we call it without it | |
188 | // and instead check after the fact which keyids where used for verification | |
189 | if (keyIsID == true) | |
190 | { | |
191 | if (Debug == true) | |
192 | std::clog << "GoodSigs needs to be limited to keyid " << key << std::endl; | |
193 | std::vector<std::string>::iterator const foundItr = std::find(ValidSigners.begin(), ValidSigners.end(), key); | |
194 | bool const found = (foundItr != ValidSigners.end()); | |
195 | std::copy(GoodSigners.begin(), GoodSigners.end(), std::back_insert_iterator<std::vector<std::string> >(NoPubKeySigners)); | |
196 | if (found) | |
197 | { | |
198 | // we look for GOODSIG here as well as an expired sig is a valid sig as well (but not a good one) | |
199 | std::string const goodlongkeyid = "GOODSIG " + key.substr(24, 16); | |
200 | bool const foundGood = std::find(GoodSigners.begin(), GoodSigners.end(), goodlongkeyid) != GoodSigners.end(); | |
201 | if (Debug == true) | |
202 | std::clog << "Key " << key << " is valid sig, is " << goodlongkeyid << " also a good one? " << (foundGood ? "yes" : "no") << std::endl; | |
203 | GoodSigners.clear(); | |
204 | if (foundGood) | |
205 | { | |
206 | GoodSigners.push_back(goodlongkeyid); | |
207 | NoPubKeySigners.erase(std::remove(NoPubKeySigners.begin(), NoPubKeySigners.end(), goodlongkeyid), NoPubKeySigners.end()); | |
208 | } | |
209 | } | |
210 | else | |
211 | GoodSigners.clear(); | |
212 | } | |
213 | ||
214 | int status; | |
215 | waitpid(pid, &status, 0); | |
216 | if (Debug == true) | |
217 | { | |
218 | ioprintf(std::clog, "gpgv exited with status %i\n", WEXITSTATUS(status)); | |
219 | } | |
220 | ||
221 | if (WEXITSTATUS(status) == 0) | |
222 | { | |
223 | if (keyIsID) | |
224 | { | |
225 | // gpgv will report success, but we want to enforce a certain keyring | |
226 | // so if we haven't found the key the valid we found is in fact invalid | |
227 | if (GoodSigners.empty()) | |
228 | return _("At least one invalid signature was encountered."); | |
229 | } | |
230 | else | |
231 | { | |
232 | if (GoodSigners.empty()) | |
233 | return _("Internal error: Good signature, but could not determine key fingerprint?!"); | |
234 | } | |
235 | return ""; | |
236 | } | |
237 | else if (WEXITSTATUS(status) == 1) | |
238 | return _("At least one invalid signature was encountered."); | |
239 | else if (WEXITSTATUS(status) == 111) | |
240 | return _("Could not execute 'apt-key' to verify signature (is gnupg installed?)"); | |
241 | else if (WEXITSTATUS(status) == 112) | |
242 | { | |
243 | // acquire system checks for "NODATA" to generate GPG errors (the others are only warnings) | |
244 | std::string errmsg; | |
245 | //TRANSLATORS: %s is a single techy word like 'NODATA' | |
246 | strprintf(errmsg, _("Clearsigned file isn't valid, got '%s' (does the network require authentication?)"), "NODATA"); | |
247 | return errmsg; | |
248 | } | |
249 | else | |
250 | return _("Unknown error executing apt-key"); | |
251 | } | |
252 | ||
253 | bool GPGVMethod::URIAcquire(std::string const &Message, FetchItem *Itm) | |
254 | { | |
255 | URI const Get = Itm->Uri; | |
256 | string const Path = Get.Host + Get.Path; // To account for relative paths | |
257 | std::string const key = LookupTag(Message, "Signed-By"); | |
258 | vector<string> GoodSigners; | |
259 | vector<string> BadSigners; | |
260 | // a worthless signature is a expired or revoked one | |
261 | vector<string> WorthlessSigners; | |
262 | vector<string> SoonWorthlessSigners; | |
263 | vector<string> NoPubKeySigners; | |
264 | ||
265 | FetchResult Res; | |
266 | Res.Filename = Itm->DestFile; | |
267 | URIStart(Res); | |
268 | ||
269 | // Run apt-key on file, extract contents and get the key ID of the signer | |
270 | string msg = VerifyGetSigners(Path.c_str(), Itm->DestFile.c_str(), key, | |
271 | GoodSigners, BadSigners, WorthlessSigners, | |
272 | SoonWorthlessSigners, NoPubKeySigners); | |
273 | ||
274 | ||
275 | // Check if there are any good signers that are not soon worthless | |
276 | std::vector<std::string> NotWarnAboutSigners(GoodSigners); | |
277 | for (auto const & Signer : SoonWorthlessSigners) | |
278 | NotWarnAboutSigners.erase(std::remove(NotWarnAboutSigners.begin(), NotWarnAboutSigners.end(), "GOODSIG " + Signer)); | |
279 | // If all signers are soon worthless, report them. | |
280 | if (NotWarnAboutSigners.empty()) { | |
281 | for (auto const & Signer : SoonWorthlessSigners) | |
282 | // TRANSLATORS: The second %s is the reason and is untranslated for repository owners. | |
283 | Warning(_("The repository is insufficiently signed by key %s (%s)"), (string(Signer)).c_str(), "weak digest"); | |
284 | } | |
285 | ||
286 | if (GoodSigners.empty() || !BadSigners.empty() || !NoPubKeySigners.empty()) | |
287 | { | |
288 | string errmsg; | |
289 | // In this case, something bad probably happened, so we just go | |
290 | // with what the other method gave us for an error message. | |
291 | if (BadSigners.empty() && WorthlessSigners.empty() && NoPubKeySigners.empty()) | |
292 | errmsg = msg; | |
293 | else | |
294 | { | |
295 | if (!BadSigners.empty()) | |
296 | { | |
297 | errmsg += _("The following signatures were invalid:\n"); | |
298 | for (vector<string>::iterator I = BadSigners.begin(); | |
299 | I != BadSigners.end(); ++I) | |
300 | errmsg += (*I + "\n"); | |
301 | } | |
302 | if (!WorthlessSigners.empty()) | |
303 | { | |
304 | errmsg += _("The following signatures were invalid:\n"); | |
305 | for (vector<string>::iterator I = WorthlessSigners.begin(); | |
306 | I != WorthlessSigners.end(); ++I) | |
307 | errmsg += (*I + "\n"); | |
308 | } | |
309 | if (!NoPubKeySigners.empty()) | |
310 | { | |
311 | errmsg += _("The following signatures couldn't be verified because the public key is not available:\n"); | |
312 | for (vector<string>::iterator I = NoPubKeySigners.begin(); | |
313 | I != NoPubKeySigners.end(); ++I) | |
314 | errmsg += (*I + "\n"); | |
315 | } | |
316 | } | |
317 | // this is only fatal if we have no good sigs or if we have at | |
318 | // least one bad signature. good signatures and NoPubKey signatures | |
319 | // happen easily when a file is signed with multiple signatures | |
320 | if(GoodSigners.empty() or !BadSigners.empty()) | |
321 | return _error->Error("%s", errmsg.c_str()); | |
322 | } | |
323 | ||
324 | // Just pass the raw output up, because passing it as a real data | |
325 | // structure is too difficult with the method stuff. We keep it | |
326 | // as three separate vectors for future extensibility. | |
327 | Res.GPGVOutput = GoodSigners; | |
328 | Res.GPGVOutput.insert(Res.GPGVOutput.end(),BadSigners.begin(),BadSigners.end()); | |
329 | Res.GPGVOutput.insert(Res.GPGVOutput.end(),NoPubKeySigners.begin(),NoPubKeySigners.end()); | |
330 | URIDone(Res); | |
331 | ||
332 | if (_config->FindB("Debug::Acquire::gpgv", false)) | |
333 | { | |
334 | std::clog << "apt-key succeeded\n"; | |
335 | } | |
336 | ||
337 | return true; | |
338 | } | |
339 | ||
340 | ||
341 | int main() | |
342 | { | |
343 | setlocale(LC_ALL, ""); | |
344 | ||
345 | GPGVMethod Mth; | |
346 | ||
347 | return Mth.Run(); | |
348 | } |