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