gpgv.cc 17 KB

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