]> git.saurik.com Git - apt.git/blob - methods/gpgv.cc
f2ef6b76e8ce537e3c7d0c7cebda617e4e9f1d82
[apt.git] / methods / gpgv.cc
1 #include <config.h>
2
3 #include <apt-pkg/configuration.h>
4 #include <apt-pkg/error.h>
5 #include <apt-pkg/gpgv.h>
6 #include <apt-pkg/strutl.h>
7 #include <apt-pkg/fileutl.h>
8 #include "aptmethod.h"
9
10 #include <ctype.h>
11 #include <errno.h>
12 #include <stddef.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <sys/wait.h>
17 #include <unistd.h>
18
19 #include <array>
20 #include <algorithm>
21 #include <sstream>
22 #include <iterator>
23 #include <iostream>
24 #include <string>
25 #include <vector>
26
27 #include <apti18n.h>
28
29 using std::string;
30 using std::vector;
31
32 #define GNUPGPREFIX "[GNUPG:]"
33 #define GNUPGBADSIG "[GNUPG:] BADSIG"
34 #define GNUPGERRSIG "[GNUPG:] ERRSIG"
35 #define GNUPGNOPUBKEY "[GNUPG:] NO_PUBKEY"
36 #define GNUPGVALIDSIG "[GNUPG:] VALIDSIG"
37 #define GNUPGGOODSIG "[GNUPG:] GOODSIG"
38 #define GNUPGEXPKEYSIG "[GNUPG:] EXPKEYSIG"
39 #define GNUPGEXPSIG "[GNUPG:] EXPSIG"
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 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 = DebugEnabled();
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 bool gotNODATA = false;
177 while (1)
178 {
179 if (getline(&buffer, &buffersize, pipein) == -1)
180 break;
181 if (Debug == true)
182 std::clog << "Read: " << buffer << std::endl;
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)
189 PushEntryWithUID(BadSigners, buffer, Debug);
190 else if (strncmp(buffer, GNUPGERRSIG, sizeof(GNUPGERRSIG)-1) == 0)
191 PushEntryWithKeyID(ErrSigners, buffer, Debug);
192 else if (strncmp(buffer, GNUPGNOPUBKEY, sizeof(GNUPGNOPUBKEY)-1) == 0)
193 {
194 PushEntryWithKeyID(NoPubKeySigners, buffer, Debug);
195 ErrSigners.erase(std::remove_if(ErrSigners.begin(), ErrSigners.end(), [&](std::string const &errsig) {
196 return errsig.compare(strlen("ERRSIG "), 16, buffer, sizeof(GNUPGNOPUBKEY), 16) == 0; }), ErrSigners.end());
197 }
198 else if (strncmp(buffer, GNUPGNODATA, sizeof(GNUPGNODATA)-1) == 0)
199 gotNODATA = true;
200 else if (strncmp(buffer, GNUPGEXPKEYSIG, sizeof(GNUPGEXPKEYSIG)-1) == 0)
201 PushEntryWithUID(WorthlessSigners, buffer, Debug);
202 else if (strncmp(buffer, GNUPGEXPSIG, sizeof(GNUPGEXPSIG)-1) == 0)
203 PushEntryWithUID(WorthlessSigners, buffer, Debug);
204 else if (strncmp(buffer, GNUPGREVKEYSIG, sizeof(GNUPGREVKEYSIG)-1) == 0)
205 PushEntryWithUID(WorthlessSigners, buffer, Debug);
206 else if (strncmp(buffer, GNUPGGOODSIG, sizeof(GNUPGGOODSIG)-1) == 0)
207 PushEntryWithKeyID(GoodSigners, buffer, Debug);
208 else if (strncmp(buffer, GNUPGVALIDSIG, sizeof(GNUPGVALIDSIG)-1) == 0)
209 {
210 std::istringstream iss(buffer + sizeof(GNUPGVALIDSIG));
211 vector<string> tokens{std::istream_iterator<string>{iss},
212 std::istream_iterator<string>{}};
213 auto const sig = tokens[0];
214 // Reject weak digest algorithms
215 Digest digest = FindDigest(tokens[7]);
216 switch (digest.getState()) {
217 case Digest::State::Weak:
218 // Treat them like an expired key: For that a message about expiry
219 // is emitted, a VALIDSIG, but no GOODSIG.
220 SoonWorthlessSigners.push_back({sig, digest.name});
221 if (Debug == true)
222 std::clog << "Got weak VALIDSIG, key ID: " << sig << std::endl;
223 break;
224 case Digest::State::Untrusted:
225 // Treat them like an expired key: For that a message about expiry
226 // is emitted, a VALIDSIG, but no GOODSIG.
227 WorthlessSigners.push_back(sig);
228 GoodSigners.erase(std::remove_if(GoodSigners.begin(), GoodSigners.end(), [&](std::string const &goodsig) {
229 return IsTheSameKey(sig, goodsig); }), GoodSigners.end());
230 if (Debug == true)
231 std::clog << "Got untrusted VALIDSIG, key ID: " << sig << std::endl;
232 break;
233
234 case Digest::State::Trusted:
235 if (Debug == true)
236 std::clog << "Got trusted VALIDSIG, key ID: " << sig << std::endl;
237 break;
238 }
239
240 ValidSigners.push_back(sig);
241 }
242 else if (strncmp(buffer, APTKEYWARNING, sizeof(APTKEYWARNING)-1) == 0)
243 Warning("%s", buffer + sizeof(APTKEYWARNING));
244 }
245 fclose(pipein);
246 free(buffer);
247 std::move(ErrSigners.begin(), ErrSigners.end(), std::back_inserter(WorthlessSigners));
248
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;
255 bool foundGood = false;
256 for (auto const &k: VectorizeString(key, ','))
257 {
258 if (std::find(ValidSigners.begin(), ValidSigners.end(), k) == ValidSigners.end())
259 continue;
260 // we look for GOODSIG here as well as an expired sig is a valid sig as well (but not a good one)
261 std::string const goodlongkeyid = "GOODSIG " + k.substr(24, 16);
262 foundGood = std::find(GoodSigners.begin(), GoodSigners.end(), goodlongkeyid) != GoodSigners.end();
263 if (Debug == true)
264 std::clog << "Key " << k << " is valid sig, is " << goodlongkeyid << " also a good one? " << (foundGood ? "yes" : "no") << std::endl;
265 if (foundGood == false)
266 continue;
267 std::copy(GoodSigners.begin(), GoodSigners.end(), std::back_insert_iterator<std::vector<std::string> >(NoPubKeySigners));
268 GoodSigners.clear();
269 GoodSigners.push_back(goodlongkeyid);
270 NoPubKeySigners.erase(std::remove(NoPubKeySigners.begin(), NoPubKeySigners.end(), goodlongkeyid), NoPubKeySigners.end());
271 break;
272 }
273 if (foundGood == false)
274 {
275 std::copy(GoodSigners.begin(), GoodSigners.end(), std::back_insert_iterator<std::vector<std::string> >(NoPubKeySigners));
276 GoodSigners.clear();
277 }
278 }
279
280 int status;
281 waitpid(pid, &status, 0);
282 if (Debug == true)
283 {
284 ioprintf(std::clog, "gpgv exited with status %i\n", WEXITSTATUS(status));
285 }
286
287 if (Debug)
288 {
289 std::cerr << "Summary:" << std::endl << " Good: ";
290 std::copy(GoodSigners.begin(), GoodSigners.end(), std::ostream_iterator<std::string>(std::cerr, ", "));
291 std::cerr << std::endl << " Bad: ";
292 std::copy(BadSigners.begin(), BadSigners.end(), std::ostream_iterator<std::string>(std::cerr, ", "));
293 std::cerr << std::endl << " Worthless: ";
294 std::copy(WorthlessSigners.begin(), WorthlessSigners.end(), std::ostream_iterator<std::string>(std::cerr, ", "));
295 std::cerr << std::endl << " SoonWorthless: ";
296 std::for_each(SoonWorthlessSigners.begin(), SoonWorthlessSigners.end(), [](Signer const &sig) { std::cerr << sig.key << ", "; });
297 std::cerr << std::endl << " NoPubKey: ";
298 std::copy(NoPubKeySigners.begin(), NoPubKeySigners.end(), std::ostream_iterator<std::string>(std::cerr, ", "));
299 std::cerr << std::endl << " NODATA: " << (gotNODATA ? "yes" : "no") << std::endl;
300 }
301
302 if (WEXITSTATUS(status) == 112)
303 {
304 // acquire system checks for "NODATA" to generate GPG errors (the others are only warnings)
305 std::string errmsg;
306 //TRANSLATORS: %s is a single techy word like 'NODATA'
307 strprintf(errmsg, _("Clearsigned file isn't valid, got '%s' (does the network require authentication?)"), "NODATA");
308 return errmsg;
309 }
310 else if (gotNODATA)
311 {
312 // acquire system checks for "NODATA" to generate GPG errors (the others are only warnings)
313 std::string errmsg;
314 //TRANSLATORS: %s is a single techy word like 'NODATA'
315 strprintf(errmsg, _("Signed file isn't valid, got '%s' (does the network require authentication?)"), "NODATA");
316 return errmsg;
317 }
318 else if (WEXITSTATUS(status) == 0)
319 {
320 if (keyIsID)
321 {
322 // gpgv will report success, but we want to enforce a certain keyring
323 // so if we haven't found the key the valid we found is in fact invalid
324 if (GoodSigners.empty())
325 return _("At least one invalid signature was encountered.");
326 }
327 else
328 {
329 if (GoodSigners.empty())
330 return _("Internal error: Good signature, but could not determine key fingerprint?!");
331 }
332 return "";
333 }
334 else if (WEXITSTATUS(status) == 1)
335 return _("At least one invalid signature was encountered.");
336 else if (WEXITSTATUS(status) == 111)
337 return _("Could not execute 'apt-key' to verify signature (is gnupg installed?)");
338 else
339 return _("Unknown error executing apt-key");
340 }
341
342 bool GPGVMethod::URIAcquire(std::string const &Message, FetchItem *Itm)
343 {
344 URI const Get = Itm->Uri;
345 string const Path = Get.Host + Get.Path; // To account for relative paths
346 std::string const key = LookupTag(Message, "Signed-By");
347 vector<string> GoodSigners;
348 vector<string> BadSigners;
349 // a worthless signature is a expired or revoked one
350 vector<string> WorthlessSigners;
351 vector<Signer> SoonWorthlessSigners;
352 vector<string> NoPubKeySigners;
353
354 FetchResult Res;
355 Res.Filename = Itm->DestFile;
356 URIStart(Res);
357
358 // Run apt-key on file, extract contents and get the key ID of the signer
359 string msg = VerifyGetSigners(Path.c_str(), Itm->DestFile.c_str(), key,
360 GoodSigners, BadSigners, WorthlessSigners,
361 SoonWorthlessSigners, NoPubKeySigners);
362
363 // Check if all good signers are soon worthless and warn in that case
364 if (std::all_of(GoodSigners.begin(), GoodSigners.end(), [&](std::string const &good) {
365 return std::any_of(SoonWorthlessSigners.begin(), SoonWorthlessSigners.end(), [&](Signer const &weak) {
366 return IsTheSameKey(weak.key, good);
367 });
368 }))
369 {
370 for (auto const & Signer : SoonWorthlessSigners)
371 // TRANSLATORS: The second %s is the reason and is untranslated for repository owners.
372 Warning(_("Signature by key %s uses weak digest algorithm (%s)"), Signer.key.c_str(), Signer.note.c_str());
373 }
374
375 if (GoodSigners.empty() || !BadSigners.empty() || !NoPubKeySigners.empty())
376 {
377 string errmsg;
378 // In this case, something bad probably happened, so we just go
379 // with what the other method gave us for an error message.
380 if (BadSigners.empty() && WorthlessSigners.empty() && NoPubKeySigners.empty())
381 errmsg = msg;
382 else
383 {
384 if (!BadSigners.empty())
385 {
386 errmsg += _("The following signatures were invalid:\n");
387 for (vector<string>::iterator I = BadSigners.begin();
388 I != BadSigners.end(); ++I)
389 errmsg += (*I + "\n");
390 }
391 if (!WorthlessSigners.empty())
392 {
393 errmsg += _("The following signatures were invalid:\n");
394 for (vector<string>::iterator I = WorthlessSigners.begin();
395 I != WorthlessSigners.end(); ++I)
396 errmsg += (*I + "\n");
397 }
398 if (!NoPubKeySigners.empty())
399 {
400 errmsg += _("The following signatures couldn't be verified because the public key is not available:\n");
401 for (vector<string>::iterator I = NoPubKeySigners.begin();
402 I != NoPubKeySigners.end(); ++I)
403 errmsg += (*I + "\n");
404 }
405 }
406 // this is only fatal if we have no good sigs or if we have at
407 // least one bad signature. good signatures and NoPubKey signatures
408 // happen easily when a file is signed with multiple signatures
409 if(GoodSigners.empty() or !BadSigners.empty())
410 return _error->Error("%s", errmsg.c_str());
411 }
412
413 // Just pass the raw output up, because passing it as a real data
414 // structure is too difficult with the method stuff. We keep it
415 // as three separate vectors for future extensibility.
416 Res.GPGVOutput = GoodSigners;
417 std::move(BadSigners.begin(), BadSigners.end(), std::back_inserter(Res.GPGVOutput));
418 std::move(NoPubKeySigners.begin(), NoPubKeySigners.end(), std::back_inserter(Res.GPGVOutput));
419 URIDone(Res);
420
421 if (DebugEnabled())
422 std::clog << "apt-key succeeded\n";
423
424 return true;
425 }
426
427
428 int main()
429 {
430 return GPGVMethod().Run();
431 }