gpgv.cc 13 KB

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