gpgv.cc 10 KB

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