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