gpgv.cc 11 KB

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