gpgv.cc 12 KB

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