gpgv.cc 10 KB

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