]> git.saurik.com Git - apt.git/blob - methods/gpgv.cc
https: Quote path in URL before passing it to curl
[apt.git] / methods / gpgv.cc
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 GNUPGERRSIG "[GNUPG:] ERRSIG"
36 #define GNUPGNOPUBKEY "[GNUPG:] NO_PUBKEY"
37 #define GNUPGVALIDSIG "[GNUPG:] VALIDSIG"
38 #define GNUPGGOODSIG "[GNUPG:] GOODSIG"
39 #define GNUPGKEYEXPIRED "[GNUPG:] KEYEXPIRED"
40 #define GNUPGREVKEYSIG "[GNUPG:] REVKEYSIG"
41 #define GNUPGNODATA "[GNUPG:] NODATA"
42 #define APTKEYWARNING "[APTKEY:] WARNING"
43
44 struct Digest {
45 enum class State {
46 Untrusted,
47 Weak,
48 Trusted,
49 } state;
50 char name[32];
51
52 State getState() const {
53 std::string optionUntrusted;
54 std::string optionWeak;
55 strprintf(optionUntrusted, "APT::Hashes::%s::Untrusted", name);
56 strprintf(optionWeak, "APT::Hashes::%s::Weak", name);
57 if (_config->FindB(optionUntrusted, state == State::Untrusted) == true)
58 return State::Untrusted;
59 if (_config->FindB(optionWeak, state == State::Weak) == true)
60 return State::Weak;
61
62 return state;
63 }
64 };
65
66 static constexpr Digest Digests[] = {
67 {Digest::State::Untrusted, "Invalid digest"},
68 {Digest::State::Untrusted, "MD5"},
69 {Digest::State::Weak, "SHA1"},
70 {Digest::State::Weak, "RIPE-MD/160"},
71 {Digest::State::Trusted, "Reserved digest"},
72 {Digest::State::Trusted, "Reserved digest"},
73 {Digest::State::Trusted, "Reserved digest"},
74 {Digest::State::Trusted, "Reserved digest"},
75 {Digest::State::Trusted, "SHA256"},
76 {Digest::State::Trusted, "SHA384"},
77 {Digest::State::Trusted, "SHA512"},
78 {Digest::State::Trusted, "SHA224"},
79 };
80
81 static Digest FindDigest(std::string const & Digest)
82 {
83 int id = atoi(Digest.c_str());
84 if (id >= 0 && static_cast<unsigned>(id) < _count(Digests)) {
85 return Digests[id];
86 } else {
87 return Digests[0];
88 }
89 }
90
91 struct Signer {
92 std::string key;
93 std::string note;
94 };
95 static bool IsTheSameKey(std::string const &validsig, std::string const &goodsig) {
96 // VALIDSIG reports a keyid (40 = 24 + 16), GOODSIG is a longid (16) only
97 return validsig.compare(24, 16, goodsig, strlen("GOODSIG "), 16) == 0;
98 }
99
100 class GPGVMethod : public aptMethod
101 {
102 private:
103 string VerifyGetSigners(const char *file, const char *outfile,
104 std::string const &key,
105 vector<string> &GoodSigners,
106 vector<string> &BadSigners,
107 vector<string> &WorthlessSigners,
108 vector<Signer> &SoonWorthlessSigners,
109 vector<string> &NoPubKeySigners);
110 protected:
111 virtual bool URIAcquire(std::string const &Message, FetchItem *Itm) APT_OVERRIDE;
112 public:
113 GPGVMethod() : aptMethod("gpgv","1.0",SingleInstance | SendConfig) {};
114 };
115
116 string GPGVMethod::VerifyGetSigners(const char *file, const char *outfile,
117 std::string const &key,
118 vector<string> &GoodSigners,
119 vector<string> &BadSigners,
120 vector<string> &WorthlessSigners,
121 vector<Signer> &SoonWorthlessSigners,
122 vector<string> &NoPubKeySigners)
123 {
124 bool const Debug = _config->FindB("Debug::Acquire::gpgv", false);
125
126 if (Debug == true)
127 std::clog << "inside VerifyGetSigners" << std::endl;
128
129 int fd[2];
130 bool const keyIsID = (key.empty() == false && key[0] != '/');
131
132 if (pipe(fd) < 0)
133 return "Couldn't create pipe";
134
135 pid_t pid = fork();
136 if (pid < 0)
137 return string("Couldn't spawn new process") + strerror(errno);
138 else if (pid == 0)
139 ExecGPGV(outfile, file, 3, fd, (keyIsID ? "" : key));
140 close(fd[1]);
141
142 FILE *pipein = fdopen(fd[0], "r");
143
144 // Loop over the output of apt-key (which really is gnupg), and check the signatures.
145 std::vector<std::string> ValidSigners;
146 std::vector<std::string> ErrSigners;
147 size_t buffersize = 0;
148 char *buffer = NULL;
149 while (1)
150 {
151 if (getline(&buffer, &buffersize, pipein) == -1)
152 break;
153 if (Debug == true)
154 std::clog << "Read: " << buffer << std::endl;
155
156 // Push the data into three separate vectors, which
157 // we later concatenate. They're kept separate so
158 // if we improve the apt method communication stuff later
159 // it will be better.
160 if (strncmp(buffer, GNUPGBADSIG, sizeof(GNUPGBADSIG)-1) == 0)
161 {
162 if (Debug == true)
163 std::clog << "Got BADSIG! " << std::endl;
164 BadSigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
165 }
166 else if (strncmp(buffer, GNUPGERRSIG, sizeof(GNUPGERRSIG)-1) == 0)
167 {
168 if (Debug == true)
169 std::clog << "Got ERRSIG " << std::endl;
170 ErrSigners.push_back(string(buffer, strlen(GNUPGPREFIX), strlen("ERRSIG ") + 16));
171 }
172 else if (strncmp(buffer, GNUPGNOPUBKEY, sizeof(GNUPGNOPUBKEY)-1) == 0)
173 {
174 if (Debug == true)
175 std::clog << "Got NO_PUBKEY " << std::endl;
176 NoPubKeySigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
177 ErrSigners.erase(std::remove_if(ErrSigners.begin(), ErrSigners.end(), [&](std::string const &errsig) {
178 return errsig.compare(strlen("ERRSIG "), 16, buffer, strlen(GNUPGNOPUBKEY), 16) == 0; }), ErrSigners.end());
179 }
180 else if (strncmp(buffer, GNUPGNODATA, sizeof(GNUPGBADSIG)-1) == 0)
181 {
182 if (Debug == true)
183 std::clog << "Got NODATA! " << std::endl;
184 BadSigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
185 }
186 else if (strncmp(buffer, GNUPGKEYEXPIRED, sizeof(GNUPGKEYEXPIRED)-1) == 0)
187 {
188 if (Debug == true)
189 std::clog << "Got KEYEXPIRED! " << std::endl;
190 WorthlessSigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
191 }
192 else if (strncmp(buffer, GNUPGREVKEYSIG, sizeof(GNUPGREVKEYSIG)-1) == 0)
193 {
194 if (Debug == true)
195 std::clog << "Got REVKEYSIG! " << std::endl;
196 WorthlessSigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
197 }
198 else if (strncmp(buffer, GNUPGGOODSIG, sizeof(GNUPGGOODSIG)-1) == 0)
199 {
200 char *sig = buffer + sizeof(GNUPGGOODSIG);
201 char *p = sig;
202 while (*p && isxdigit(*p))
203 p++;
204 *p = 0;
205 if (Debug == true)
206 std::clog << "Got GOODSIG, key ID: " << sig << std::endl;
207 GoodSigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
208 }
209 else if (strncmp(buffer, GNUPGVALIDSIG, sizeof(GNUPGVALIDSIG)-1) == 0)
210 {
211 char *sig = buffer + sizeof(GNUPGVALIDSIG);
212 std::istringstream iss((string(sig)));
213 vector<string> tokens{std::istream_iterator<string>{iss},
214 std::istream_iterator<string>{}};
215 char *p = sig;
216 while (*p && isxdigit(*p))
217 p++;
218 *p = 0;
219 // Reject weak digest algorithms
220 Digest digest = FindDigest(tokens[7]);
221 switch (digest.getState()) {
222 case Digest::State::Weak:
223 // Treat them like an expired key: For that a message about expiry
224 // is emitted, a VALIDSIG, but no GOODSIG.
225 SoonWorthlessSigners.push_back({string(sig), digest.name});
226 if (Debug == true)
227 std::clog << "Got weak VALIDSIG, key ID: " << sig << std::endl;
228 break;
229 case Digest::State::Untrusted:
230 // Treat them like an expired key: For that a message about expiry
231 // is emitted, a VALIDSIG, but no GOODSIG.
232 WorthlessSigners.push_back(string(sig));
233 GoodSigners.erase(std::remove_if(GoodSigners.begin(), GoodSigners.end(), [&](std::string const &goodsig) {
234 return IsTheSameKey(string(sig), goodsig); }), GoodSigners.end());
235 if (Debug == true)
236 std::clog << "Got untrusted VALIDSIG, key ID: " << sig << std::endl;
237 break;
238
239 case Digest::State::Trusted:
240 if (Debug == true)
241 std::clog << "Got trusted VALIDSIG, key ID: " << sig << std::endl;
242 break;
243 }
244
245 ValidSigners.push_back(string(sig));
246 }
247 else if (strncmp(buffer, APTKEYWARNING, sizeof(APTKEYWARNING)-1) == 0)
248 Warning("%s", buffer + sizeof(APTKEYWARNING));
249 }
250 fclose(pipein);
251 free(buffer);
252 std::move(ErrSigners.begin(), ErrSigners.end(), std::back_inserter(WorthlessSigners));
253
254 // apt-key has a --keyid parameter, but this requires gpg, so we call it without it
255 // and instead check after the fact which keyids where used for verification
256 if (keyIsID == true)
257 {
258 if (Debug == true)
259 std::clog << "GoodSigs needs to be limited to keyid " << key << std::endl;
260 std::vector<std::string>::iterator const foundItr = std::find(ValidSigners.begin(), ValidSigners.end(), key);
261 bool const found = (foundItr != ValidSigners.end());
262 std::copy(GoodSigners.begin(), GoodSigners.end(), std::back_insert_iterator<std::vector<std::string> >(NoPubKeySigners));
263 if (found)
264 {
265 // we look for GOODSIG here as well as an expired sig is a valid sig as well (but not a good one)
266 std::string const goodlongkeyid = "GOODSIG " + key.substr(24, 16);
267 bool const foundGood = std::find(GoodSigners.begin(), GoodSigners.end(), goodlongkeyid) != GoodSigners.end();
268 if (Debug == true)
269 std::clog << "Key " << key << " is valid sig, is " << goodlongkeyid << " also a good one? " << (foundGood ? "yes" : "no") << std::endl;
270 GoodSigners.clear();
271 if (foundGood)
272 {
273 GoodSigners.push_back(goodlongkeyid);
274 NoPubKeySigners.erase(std::remove(NoPubKeySigners.begin(), NoPubKeySigners.end(), goodlongkeyid), NoPubKeySigners.end());
275 }
276 }
277 else
278 GoodSigners.clear();
279 }
280
281 int status;
282 waitpid(pid, &status, 0);
283 if (Debug == true)
284 {
285 ioprintf(std::clog, "gpgv exited with status %i\n", WEXITSTATUS(status));
286 }
287
288 if (Debug)
289 {
290 std::cerr << "Summary:" << std::endl << " Good: ";
291 std::copy(GoodSigners.begin(), GoodSigners.end(), std::ostream_iterator<std::string>(std::cerr, ", "));
292 std::cerr << std::endl << " Bad: ";
293 std::copy(BadSigners.begin(), BadSigners.end(), std::ostream_iterator<std::string>(std::cerr, ", "));
294 std::cerr << std::endl << " Worthless: ";
295 std::copy(WorthlessSigners.begin(), WorthlessSigners.end(), std::ostream_iterator<std::string>(std::cerr, ", "));
296 std::cerr << std::endl << " SoonWorthless: ";
297 std::for_each(SoonWorthlessSigners.begin(), SoonWorthlessSigners.end(), [](Signer const &sig) { std::cerr << sig.key << ", "; });
298 std::cerr << std::endl << " NoPubKey: ";
299 std::copy(NoPubKeySigners.begin(), NoPubKeySigners.end(), std::ostream_iterator<std::string>(std::cerr, ", "));
300 std::cerr << std::endl;
301 }
302
303 if (WEXITSTATUS(status) == 0)
304 {
305 if (keyIsID)
306 {
307 // gpgv will report success, but we want to enforce a certain keyring
308 // so if we haven't found the key the valid we found is in fact invalid
309 if (GoodSigners.empty())
310 return _("At least one invalid signature was encountered.");
311 }
312 else
313 {
314 if (GoodSigners.empty())
315 return _("Internal error: Good signature, but could not determine key fingerprint?!");
316 }
317 return "";
318 }
319 else if (WEXITSTATUS(status) == 1)
320 return _("At least one invalid signature was encountered.");
321 else if (WEXITSTATUS(status) == 111)
322 return _("Could not execute 'apt-key' to verify signature (is gnupg installed?)");
323 else if (WEXITSTATUS(status) == 112)
324 {
325 // acquire system checks for "NODATA" to generate GPG errors (the others are only warnings)
326 std::string errmsg;
327 //TRANSLATORS: %s is a single techy word like 'NODATA'
328 strprintf(errmsg, _("Clearsigned file isn't valid, got '%s' (does the network require authentication?)"), "NODATA");
329 return errmsg;
330 }
331 else
332 return _("Unknown error executing apt-key");
333 }
334
335 bool GPGVMethod::URIAcquire(std::string const &Message, FetchItem *Itm)
336 {
337 URI const Get = Itm->Uri;
338 string const Path = Get.Host + Get.Path; // To account for relative paths
339 std::string const key = LookupTag(Message, "Signed-By");
340 vector<string> GoodSigners;
341 vector<string> BadSigners;
342 // a worthless signature is a expired or revoked one
343 vector<string> WorthlessSigners;
344 vector<Signer> SoonWorthlessSigners;
345 vector<string> NoPubKeySigners;
346
347 FetchResult Res;
348 Res.Filename = Itm->DestFile;
349 URIStart(Res);
350
351 // Run apt-key on file, extract contents and get the key ID of the signer
352 string msg = VerifyGetSigners(Path.c_str(), Itm->DestFile.c_str(), key,
353 GoodSigners, BadSigners, WorthlessSigners,
354 SoonWorthlessSigners, NoPubKeySigners);
355
356 // Check if all good signers are soon worthless and warn in that case
357 if (std::all_of(GoodSigners.begin(), GoodSigners.end(), [&](std::string const &good) {
358 return std::any_of(SoonWorthlessSigners.begin(), SoonWorthlessSigners.end(), [&](Signer const &weak) {
359 return IsTheSameKey(weak.key, good);
360 });
361 }))
362 {
363 for (auto const & Signer : SoonWorthlessSigners)
364 // TRANSLATORS: The second %s is the reason and is untranslated for repository owners.
365 Warning(_("Signature by key %s uses weak digest algorithm (%s)"), Signer.key.c_str(), Signer.note.c_str());
366 }
367
368 if (GoodSigners.empty() || !BadSigners.empty() || !NoPubKeySigners.empty())
369 {
370 string errmsg;
371 // In this case, something bad probably happened, so we just go
372 // with what the other method gave us for an error message.
373 if (BadSigners.empty() && WorthlessSigners.empty() && NoPubKeySigners.empty())
374 errmsg = msg;
375 else
376 {
377 if (!BadSigners.empty())
378 {
379 errmsg += _("The following signatures were invalid:\n");
380 for (vector<string>::iterator I = BadSigners.begin();
381 I != BadSigners.end(); ++I)
382 errmsg += (*I + "\n");
383 }
384 if (!WorthlessSigners.empty())
385 {
386 errmsg += _("The following signatures were invalid:\n");
387 for (vector<string>::iterator I = WorthlessSigners.begin();
388 I != WorthlessSigners.end(); ++I)
389 errmsg += (*I + "\n");
390 }
391 if (!NoPubKeySigners.empty())
392 {
393 errmsg += _("The following signatures couldn't be verified because the public key is not available:\n");
394 for (vector<string>::iterator I = NoPubKeySigners.begin();
395 I != NoPubKeySigners.end(); ++I)
396 errmsg += (*I + "\n");
397 }
398 }
399 // this is only fatal if we have no good sigs or if we have at
400 // least one bad signature. good signatures and NoPubKey signatures
401 // happen easily when a file is signed with multiple signatures
402 if(GoodSigners.empty() or !BadSigners.empty())
403 return _error->Error("%s", errmsg.c_str());
404 }
405
406 // Just pass the raw output up, because passing it as a real data
407 // structure is too difficult with the method stuff. We keep it
408 // as three separate vectors for future extensibility.
409 Res.GPGVOutput = GoodSigners;
410 Res.GPGVOutput.insert(Res.GPGVOutput.end(),BadSigners.begin(),BadSigners.end());
411 Res.GPGVOutput.insert(Res.GPGVOutput.end(),NoPubKeySigners.begin(),NoPubKeySigners.end());
412 URIDone(Res);
413
414 if (_config->FindB("Debug::Acquire::gpgv", false))
415 {
416 std::clog << "apt-key succeeded\n";
417 }
418
419 return true;
420 }
421
422
423 int main()
424 {
425 setlocale(LC_ALL, "");
426
427 GPGVMethod Mth;
428
429 return Mth.Run();
430 }