gpgv.cc 16 KB

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