hashes.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: hashes.cc,v 1.1 2001/03/06 07:15:29 jgg Exp $
  4. /* ######################################################################
  5. Hashes - Simple wrapper around the hash functions
  6. This is just used to make building the methods simpler, this is the
  7. only interface required..
  8. ##################################################################### */
  9. /*}}}*/
  10. // Include Files /*{{{*/
  11. #include <config.h>
  12. #include <apt-pkg/hashes.h>
  13. #include <apt-pkg/fileutl.h>
  14. #include <apt-pkg/configuration.h>
  15. #include <apt-pkg/md5.h>
  16. #include <apt-pkg/sha1.h>
  17. #include <apt-pkg/sha2.h>
  18. #include <stddef.h>
  19. #include <algorithm>
  20. #include <unistd.h>
  21. #include <stdlib.h>
  22. #include <string>
  23. #include <iostream>
  24. /*}}}*/
  25. const char * HashString::_SupportedHashes[] =
  26. {
  27. "SHA512", "SHA256", "SHA1", "MD5Sum", "Checksum-FileSize", NULL
  28. };
  29. HashString::HashString()
  30. {
  31. }
  32. HashString::HashString(std::string Type, std::string Hash) : Type(Type), Hash(Hash)
  33. {
  34. }
  35. HashString::HashString(std::string StringedHash) /*{{{*/
  36. {
  37. if (StringedHash.find(":") == std::string::npos)
  38. {
  39. // legacy: md5sum without "MD5Sum:" prefix
  40. if (StringedHash.size() == 32)
  41. {
  42. Type = "MD5Sum";
  43. Hash = StringedHash;
  44. }
  45. if(_config->FindB("Debug::Hashes",false) == true)
  46. std::clog << "HashString(string): invalid StringedHash " << StringedHash << std::endl;
  47. return;
  48. }
  49. std::string::size_type pos = StringedHash.find(":");
  50. Type = StringedHash.substr(0,pos);
  51. Hash = StringedHash.substr(pos+1, StringedHash.size() - pos);
  52. if(_config->FindB("Debug::Hashes",false) == true)
  53. std::clog << "HashString(string): " << Type << " : " << Hash << std::endl;
  54. }
  55. /*}}}*/
  56. bool HashString::VerifyFile(std::string filename) const /*{{{*/
  57. {
  58. std::string fileHash = GetHashForFile(filename);
  59. if(_config->FindB("Debug::Hashes",false) == true)
  60. std::clog << "HashString::VerifyFile: got: " << fileHash << " expected: " << toStr() << std::endl;
  61. return (fileHash == Hash);
  62. }
  63. /*}}}*/
  64. bool HashString::FromFile(std::string filename) /*{{{*/
  65. {
  66. // pick the strongest hash
  67. if (Type == "")
  68. Type = _SupportedHashes[0];
  69. Hash = GetHashForFile(filename);
  70. return true;
  71. }
  72. /*}}}*/
  73. std::string HashString::GetHashForFile(std::string filename) const /*{{{*/
  74. {
  75. std::string fileHash;
  76. FileFd Fd(filename, FileFd::ReadOnly);
  77. if(strcasecmp(Type.c_str(), "MD5Sum") == 0)
  78. {
  79. MD5Summation MD5;
  80. MD5.AddFD(Fd);
  81. fileHash = (std::string)MD5.Result();
  82. }
  83. else if (strcasecmp(Type.c_str(), "SHA1") == 0)
  84. {
  85. SHA1Summation SHA1;
  86. SHA1.AddFD(Fd);
  87. fileHash = (std::string)SHA1.Result();
  88. }
  89. else if (strcasecmp(Type.c_str(), "SHA256") == 0)
  90. {
  91. SHA256Summation SHA256;
  92. SHA256.AddFD(Fd);
  93. fileHash = (std::string)SHA256.Result();
  94. }
  95. else if (strcasecmp(Type.c_str(), "SHA512") == 0)
  96. {
  97. SHA512Summation SHA512;
  98. SHA512.AddFD(Fd);
  99. fileHash = (std::string)SHA512.Result();
  100. }
  101. else if (strcasecmp(Type.c_str(), "Checksum-FileSize") == 0)
  102. strprintf(fileHash, "%llu", Fd.FileSize());
  103. Fd.Close();
  104. return fileHash;
  105. }
  106. /*}}}*/
  107. const char** HashString::SupportedHashes() /*{{{*/
  108. {
  109. return _SupportedHashes;
  110. }
  111. /*}}}*/
  112. APT_PURE bool HashString::empty() const /*{{{*/
  113. {
  114. return (Type.empty() || Hash.empty());
  115. }
  116. /*}}}*/
  117. APT_PURE static bool IsConfigured(const char *name, const char *what)
  118. {
  119. std::string option;
  120. strprintf(option, "APT::Hashes::%s::%s", name, what);
  121. return _config->FindB(option, false);
  122. }
  123. APT_PURE bool HashString::usable() const /*{{{*/
  124. {
  125. return (
  126. (Type != "Checksum-FileSize") &&
  127. //(Type != "MD5Sum") &&
  128. //(Type != "SHA1") &&
  129. !IsConfigured(Type.c_str(), "Untrusted")
  130. );
  131. }
  132. /*}}}*/
  133. std::string HashString::toStr() const /*{{{*/
  134. {
  135. return Type + ":" + Hash;
  136. }
  137. /*}}}*/
  138. APT_PURE bool HashString::operator==(HashString const &other) const /*{{{*/
  139. {
  140. return (strcasecmp(Type.c_str(), other.Type.c_str()) == 0 && Hash == other.Hash);
  141. }
  142. APT_PURE bool HashString::operator!=(HashString const &other) const
  143. {
  144. return !(*this == other);
  145. }
  146. /*}}}*/
  147. bool HashStringList::usable() const /*{{{*/
  148. {
  149. if (empty() == true)
  150. return false;
  151. std::string const forcedType = _config->Find("Acquire::ForceHash", "");
  152. if (forcedType.empty() == true)
  153. {
  154. // See if there is at least one usable hash
  155. for (auto const &hs: list)
  156. if (hs.usable())
  157. return true;
  158. return false;
  159. }
  160. return find(forcedType) != NULL;
  161. }
  162. /*}}}*/
  163. HashString const * HashStringList::find(char const * const type) const /*{{{*/
  164. {
  165. if (type == NULL || type[0] == '\0')
  166. {
  167. std::string const forcedType = _config->Find("Acquire::ForceHash", "");
  168. if (forcedType.empty() == false)
  169. return find(forcedType.c_str());
  170. for (char const * const * t = HashString::SupportedHashes(); *t != NULL; ++t)
  171. for (std::vector<HashString>::const_iterator hs = list.begin(); hs != list.end(); ++hs)
  172. if (strcasecmp(hs->HashType().c_str(), *t) == 0)
  173. return &*hs;
  174. return NULL;
  175. }
  176. for (std::vector<HashString>::const_iterator hs = list.begin(); hs != list.end(); ++hs)
  177. if (strcasecmp(hs->HashType().c_str(), type) == 0)
  178. return &*hs;
  179. return NULL;
  180. }
  181. /*}}}*/
  182. unsigned long long HashStringList::FileSize() const /*{{{*/
  183. {
  184. HashString const * const hsf = find("Checksum-FileSize");
  185. if (hsf == NULL)
  186. return 0;
  187. std::string const hv = hsf->HashValue();
  188. return strtoull(hv.c_str(), NULL, 10);
  189. }
  190. /*}}}*/
  191. bool HashStringList::FileSize(unsigned long long const Size) /*{{{*/
  192. {
  193. std::string size;
  194. strprintf(size, "%llu", Size);
  195. return push_back(HashString("Checksum-FileSize", size));
  196. }
  197. /*}}}*/
  198. bool HashStringList::supported(char const * const type) /*{{{*/
  199. {
  200. for (char const * const * t = HashString::SupportedHashes(); *t != NULL; ++t)
  201. if (strcasecmp(*t, type) == 0)
  202. return true;
  203. return false;
  204. }
  205. /*}}}*/
  206. bool HashStringList::push_back(const HashString &hashString) /*{{{*/
  207. {
  208. if (hashString.HashType().empty() == true ||
  209. hashString.HashValue().empty() == true ||
  210. supported(hashString.HashType().c_str()) == false)
  211. return false;
  212. // ensure that each type is added only once
  213. HashString const * const hs = find(hashString.HashType().c_str());
  214. if (hs != NULL)
  215. return *hs == hashString;
  216. list.push_back(hashString);
  217. return true;
  218. }
  219. /*}}}*/
  220. bool HashStringList::VerifyFile(std::string filename) const /*{{{*/
  221. {
  222. if (usable() == false)
  223. return false;
  224. Hashes hashes(*this);
  225. FileFd file(filename, FileFd::ReadOnly);
  226. HashString const * const hsf = find("Checksum-FileSize");
  227. if (hsf != NULL)
  228. {
  229. std::string fileSize;
  230. strprintf(fileSize, "%llu", file.FileSize());
  231. if (hsf->HashValue() != fileSize)
  232. return false;
  233. }
  234. hashes.AddFD(file);
  235. HashStringList const hsl = hashes.GetHashStringList();
  236. return hsl == *this;
  237. }
  238. /*}}}*/
  239. bool HashStringList::operator==(HashStringList const &other) const /*{{{*/
  240. {
  241. std::string const forcedType = _config->Find("Acquire::ForceHash", "");
  242. if (forcedType.empty() == false)
  243. {
  244. HashString const * const hs = find(forcedType);
  245. HashString const * const ohs = other.find(forcedType);
  246. if (hs == NULL || ohs == NULL)
  247. return false;
  248. return *hs == *ohs;
  249. }
  250. short matches = 0;
  251. for (const_iterator hs = begin(); hs != end(); ++hs)
  252. {
  253. HashString const * const ohs = other.find(hs->HashType());
  254. if (ohs == NULL)
  255. continue;
  256. if (*hs != *ohs)
  257. return false;
  258. ++matches;
  259. }
  260. if (matches == 0)
  261. return false;
  262. return true;
  263. }
  264. bool HashStringList::operator!=(HashStringList const &other) const
  265. {
  266. return !(*this == other);
  267. }
  268. /*}}}*/
  269. // PrivateHashes /*{{{*/
  270. class PrivateHashes {
  271. public:
  272. unsigned long long FileSize;
  273. unsigned int CalcHashes;
  274. explicit PrivateHashes(unsigned int const CalcHashes) : FileSize(0), CalcHashes(CalcHashes) {}
  275. explicit PrivateHashes(HashStringList const &Hashes) : FileSize(0) {
  276. unsigned int calcHashes = Hashes.usable() ? 0 : ~0;
  277. if (Hashes.find("MD5Sum") != NULL)
  278. calcHashes |= Hashes::MD5SUM;
  279. if (Hashes.find("SHA1") != NULL)
  280. calcHashes |= Hashes::SHA1SUM;
  281. if (Hashes.find("SHA256") != NULL)
  282. calcHashes |= Hashes::SHA256SUM;
  283. if (Hashes.find("SHA512") != NULL)
  284. calcHashes |= Hashes::SHA512SUM;
  285. CalcHashes = calcHashes;
  286. }
  287. };
  288. /*}}}*/
  289. // Hashes::Add* - Add the contents of data or FD /*{{{*/
  290. bool Hashes::Add(const unsigned char * const Data, unsigned long long const Size)
  291. {
  292. if (Size == 0)
  293. return true;
  294. bool Res = true;
  295. APT_IGNORE_DEPRECATED_PUSH
  296. if ((d->CalcHashes & MD5SUM) == MD5SUM)
  297. Res &= MD5.Add(Data, Size);
  298. if ((d->CalcHashes & SHA1SUM) == SHA1SUM)
  299. Res &= SHA1.Add(Data, Size);
  300. if ((d->CalcHashes & SHA256SUM) == SHA256SUM)
  301. Res &= SHA256.Add(Data, Size);
  302. if ((d->CalcHashes & SHA512SUM) == SHA512SUM)
  303. Res &= SHA512.Add(Data, Size);
  304. APT_IGNORE_DEPRECATED_POP
  305. d->FileSize += Size;
  306. return Res;
  307. }
  308. bool Hashes::Add(const unsigned char * const Data, unsigned long long const Size, unsigned int const Hashes)
  309. {
  310. d->CalcHashes = Hashes;
  311. return Add(Data, Size);
  312. }
  313. bool Hashes::AddFD(int const Fd,unsigned long long Size)
  314. {
  315. unsigned char Buf[64*64];
  316. bool const ToEOF = (Size == UntilEOF);
  317. while (Size != 0 || ToEOF)
  318. {
  319. unsigned long long n = sizeof(Buf);
  320. if (!ToEOF) n = std::min(Size, n);
  321. ssize_t const Res = read(Fd,Buf,n);
  322. if (Res < 0 || (!ToEOF && Res != (ssize_t) n)) // error, or short read
  323. return false;
  324. if (ToEOF && Res == 0) // EOF
  325. break;
  326. Size -= Res;
  327. if (Add(Buf, Res) == false)
  328. return false;
  329. }
  330. return true;
  331. }
  332. bool Hashes::AddFD(int const Fd,unsigned long long Size, unsigned int const Hashes)
  333. {
  334. d->CalcHashes = Hashes;
  335. return AddFD(Fd, Size);
  336. }
  337. bool Hashes::AddFD(FileFd &Fd,unsigned long long Size)
  338. {
  339. unsigned char Buf[64*64];
  340. bool const ToEOF = (Size == 0);
  341. while (Size != 0 || ToEOF)
  342. {
  343. unsigned long long n = sizeof(Buf);
  344. if (!ToEOF) n = std::min(Size, n);
  345. unsigned long long a = 0;
  346. if (Fd.Read(Buf, n, &a) == false) // error
  347. return false;
  348. if (ToEOF == false)
  349. {
  350. if (a != n) // short read
  351. return false;
  352. }
  353. else if (a == 0) // EOF
  354. break;
  355. Size -= a;
  356. if (Add(Buf, a) == false)
  357. return false;
  358. }
  359. return true;
  360. }
  361. bool Hashes::AddFD(FileFd &Fd,unsigned long long Size, unsigned int const Hashes)
  362. {
  363. d->CalcHashes = Hashes;
  364. return AddFD(Fd, Size);
  365. }
  366. /*}}}*/
  367. HashStringList Hashes::GetHashStringList()
  368. {
  369. HashStringList hashes;
  370. APT_IGNORE_DEPRECATED_PUSH
  371. if ((d->CalcHashes & MD5SUM) == MD5SUM)
  372. hashes.push_back(HashString("MD5Sum", MD5.Result().Value()));
  373. if ((d->CalcHashes & SHA1SUM) == SHA1SUM)
  374. hashes.push_back(HashString("SHA1", SHA1.Result().Value()));
  375. if ((d->CalcHashes & SHA256SUM) == SHA256SUM)
  376. hashes.push_back(HashString("SHA256", SHA256.Result().Value()));
  377. if ((d->CalcHashes & SHA512SUM) == SHA512SUM)
  378. hashes.push_back(HashString("SHA512", SHA512.Result().Value()));
  379. APT_IGNORE_DEPRECATED_POP
  380. hashes.FileSize(d->FileSize);
  381. return hashes;
  382. }
  383. APT_IGNORE_DEPRECATED_PUSH
  384. Hashes::Hashes() : d(new PrivateHashes(~0)) { }
  385. Hashes::Hashes(unsigned int const Hashes) : d(new PrivateHashes(Hashes)) {}
  386. Hashes::Hashes(HashStringList const &Hashes) : d(new PrivateHashes(Hashes)) {}
  387. Hashes::~Hashes() { delete d; }
  388. APT_IGNORE_DEPRECATED_POP