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