hashes.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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 bool HashString::usable() const /*{{{*/
  118. {
  119. return (
  120. (Type != "Checksum-FileSize") &&
  121. (Type != "MD5Sum") &&
  122. (Type != "SHA1")
  123. );
  124. }
  125. /*}}}*/
  126. std::string HashString::toStr() const /*{{{*/
  127. {
  128. return Type + ":" + Hash;
  129. }
  130. /*}}}*/
  131. APT_PURE bool HashString::operator==(HashString const &other) const /*{{{*/
  132. {
  133. return (strcasecmp(Type.c_str(), other.Type.c_str()) == 0 && Hash == other.Hash);
  134. }
  135. APT_PURE bool HashString::operator!=(HashString const &other) const
  136. {
  137. return !(*this == other);
  138. }
  139. /*}}}*/
  140. bool HashStringList::usable() const /*{{{*/
  141. {
  142. if (empty() == true)
  143. return false;
  144. std::string const forcedType = _config->Find("Acquire::ForceHash", "");
  145. if (forcedType.empty() == true)
  146. {
  147. // See if there is at least one usable hash
  148. for (auto const &hs: list)
  149. if (hs.usable())
  150. return true;
  151. return false;
  152. }
  153. return find(forcedType) != NULL;
  154. }
  155. /*}}}*/
  156. HashString const * HashStringList::find(char const * const type) const /*{{{*/
  157. {
  158. if (type == NULL || type[0] == '\0')
  159. {
  160. std::string const forcedType = _config->Find("Acquire::ForceHash", "");
  161. if (forcedType.empty() == false)
  162. return find(forcedType.c_str());
  163. for (char const * const * t = HashString::SupportedHashes(); *t != NULL; ++t)
  164. for (std::vector<HashString>::const_iterator hs = list.begin(); hs != list.end(); ++hs)
  165. if (strcasecmp(hs->HashType().c_str(), *t) == 0)
  166. return &*hs;
  167. return NULL;
  168. }
  169. for (std::vector<HashString>::const_iterator hs = list.begin(); hs != list.end(); ++hs)
  170. if (strcasecmp(hs->HashType().c_str(), type) == 0)
  171. return &*hs;
  172. return NULL;
  173. }
  174. /*}}}*/
  175. unsigned long long HashStringList::FileSize() const /*{{{*/
  176. {
  177. HashString const * const hsf = find("Checksum-FileSize");
  178. if (hsf == NULL)
  179. return 0;
  180. std::string const hv = hsf->HashValue();
  181. return strtoull(hv.c_str(), NULL, 10);
  182. }
  183. /*}}}*/
  184. bool HashStringList::FileSize(unsigned long long const Size) /*{{{*/
  185. {
  186. std::string size;
  187. strprintf(size, "%llu", Size);
  188. return push_back(HashString("Checksum-FileSize", size));
  189. }
  190. /*}}}*/
  191. bool HashStringList::supported(char const * const type) /*{{{*/
  192. {
  193. for (char const * const * t = HashString::SupportedHashes(); *t != NULL; ++t)
  194. if (strcasecmp(*t, type) == 0)
  195. return true;
  196. return false;
  197. }
  198. /*}}}*/
  199. bool HashStringList::push_back(const HashString &hashString) /*{{{*/
  200. {
  201. if (hashString.HashType().empty() == true ||
  202. hashString.HashValue().empty() == true ||
  203. supported(hashString.HashType().c_str()) == false)
  204. return false;
  205. // ensure that each type is added only once
  206. HashString const * const hs = find(hashString.HashType().c_str());
  207. if (hs != NULL)
  208. return *hs == hashString;
  209. list.push_back(hashString);
  210. return true;
  211. }
  212. /*}}}*/
  213. bool HashStringList::VerifyFile(std::string filename) const /*{{{*/
  214. {
  215. if (usable() == false)
  216. return false;
  217. Hashes hashes(*this);
  218. FileFd file(filename, FileFd::ReadOnly);
  219. HashString const * const hsf = find("Checksum-FileSize");
  220. if (hsf != NULL)
  221. {
  222. std::string fileSize;
  223. strprintf(fileSize, "%llu", file.FileSize());
  224. if (hsf->HashValue() != fileSize)
  225. return false;
  226. }
  227. hashes.AddFD(file);
  228. HashStringList const hsl = hashes.GetHashStringList();
  229. return hsl == *this;
  230. }
  231. /*}}}*/
  232. bool HashStringList::operator==(HashStringList const &other) const /*{{{*/
  233. {
  234. std::string const forcedType = _config->Find("Acquire::ForceHash", "");
  235. if (forcedType.empty() == false)
  236. {
  237. HashString const * const hs = find(forcedType);
  238. HashString const * const ohs = other.find(forcedType);
  239. if (hs == NULL || ohs == NULL)
  240. return false;
  241. return *hs == *ohs;
  242. }
  243. short matches = 0;
  244. for (const_iterator hs = begin(); hs != end(); ++hs)
  245. {
  246. HashString const * const ohs = other.find(hs->HashType());
  247. if (ohs == NULL)
  248. continue;
  249. if (*hs != *ohs)
  250. return false;
  251. ++matches;
  252. }
  253. if (matches == 0)
  254. return false;
  255. return true;
  256. }
  257. bool HashStringList::operator!=(HashStringList const &other) const
  258. {
  259. return !(*this == other);
  260. }
  261. /*}}}*/
  262. // PrivateHashes /*{{{*/
  263. class PrivateHashes {
  264. public:
  265. unsigned long long FileSize;
  266. unsigned int CalcHashes;
  267. explicit PrivateHashes(unsigned int const CalcHashes) : FileSize(0), CalcHashes(CalcHashes) {}
  268. explicit PrivateHashes(HashStringList const &Hashes) : FileSize(0) {
  269. unsigned int calcHashes = Hashes.usable() ? 0 : ~0;
  270. if (Hashes.find("MD5Sum") != NULL)
  271. calcHashes |= Hashes::MD5SUM;
  272. if (Hashes.find("SHA1") != NULL)
  273. calcHashes |= Hashes::SHA1SUM;
  274. if (Hashes.find("SHA256") != NULL)
  275. calcHashes |= Hashes::SHA256SUM;
  276. if (Hashes.find("SHA512") != NULL)
  277. calcHashes |= Hashes::SHA512SUM;
  278. CalcHashes = calcHashes;
  279. }
  280. };
  281. /*}}}*/
  282. // Hashes::Add* - Add the contents of data or FD /*{{{*/
  283. bool Hashes::Add(const unsigned char * const Data, unsigned long long const Size)
  284. {
  285. bool Res = true;
  286. APT_IGNORE_DEPRECATED_PUSH
  287. if ((d->CalcHashes & MD5SUM) == MD5SUM)
  288. Res &= MD5.Add(Data, Size);
  289. if ((d->CalcHashes & SHA1SUM) == SHA1SUM)
  290. Res &= SHA1.Add(Data, Size);
  291. if ((d->CalcHashes & SHA256SUM) == SHA256SUM)
  292. Res &= SHA256.Add(Data, Size);
  293. if ((d->CalcHashes & SHA512SUM) == SHA512SUM)
  294. Res &= SHA512.Add(Data, Size);
  295. APT_IGNORE_DEPRECATED_POP
  296. d->FileSize += Size;
  297. return Res;
  298. }
  299. bool Hashes::Add(const unsigned char * const Data, unsigned long long const Size, unsigned int const Hashes)
  300. {
  301. d->CalcHashes = Hashes;
  302. return Add(Data, Size);
  303. }
  304. bool Hashes::AddFD(int const Fd,unsigned long long Size)
  305. {
  306. unsigned char Buf[64*64];
  307. bool const ToEOF = (Size == UntilEOF);
  308. while (Size != 0 || ToEOF)
  309. {
  310. unsigned long long n = sizeof(Buf);
  311. if (!ToEOF) n = std::min(Size, n);
  312. ssize_t const Res = read(Fd,Buf,n);
  313. if (Res < 0 || (!ToEOF && Res != (ssize_t) n)) // error, or short read
  314. return false;
  315. if (ToEOF && Res == 0) // EOF
  316. break;
  317. Size -= Res;
  318. if (Add(Buf, Res) == false)
  319. return false;
  320. }
  321. return true;
  322. }
  323. bool Hashes::AddFD(int const Fd,unsigned long long Size, unsigned int const Hashes)
  324. {
  325. d->CalcHashes = Hashes;
  326. return AddFD(Fd, Size);
  327. }
  328. bool Hashes::AddFD(FileFd &Fd,unsigned long long Size)
  329. {
  330. unsigned char Buf[64*64];
  331. bool const ToEOF = (Size == 0);
  332. while (Size != 0 || ToEOF)
  333. {
  334. unsigned long long n = sizeof(Buf);
  335. if (!ToEOF) n = std::min(Size, n);
  336. unsigned long long a = 0;
  337. if (Fd.Read(Buf, n, &a) == false) // error
  338. return false;
  339. if (ToEOF == false)
  340. {
  341. if (a != n) // short read
  342. return false;
  343. }
  344. else if (a == 0) // EOF
  345. break;
  346. Size -= a;
  347. if (Add(Buf, a) == false)
  348. return false;
  349. }
  350. return true;
  351. }
  352. bool Hashes::AddFD(FileFd &Fd,unsigned long long Size, unsigned int const Hashes)
  353. {
  354. d->CalcHashes = Hashes;
  355. return AddFD(Fd, Size);
  356. }
  357. /*}}}*/
  358. HashStringList Hashes::GetHashStringList()
  359. {
  360. HashStringList hashes;
  361. APT_IGNORE_DEPRECATED_PUSH
  362. if ((d->CalcHashes & MD5SUM) == MD5SUM)
  363. hashes.push_back(HashString("MD5Sum", MD5.Result().Value()));
  364. if ((d->CalcHashes & SHA1SUM) == SHA1SUM)
  365. hashes.push_back(HashString("SHA1", SHA1.Result().Value()));
  366. if ((d->CalcHashes & SHA256SUM) == SHA256SUM)
  367. hashes.push_back(HashString("SHA256", SHA256.Result().Value()));
  368. if ((d->CalcHashes & SHA512SUM) == SHA512SUM)
  369. hashes.push_back(HashString("SHA512", SHA512.Result().Value()));
  370. APT_IGNORE_DEPRECATED_POP
  371. hashes.FileSize(d->FileSize);
  372. return hashes;
  373. }
  374. APT_IGNORE_DEPRECATED_PUSH
  375. Hashes::Hashes() : d(new PrivateHashes(~0)) { }
  376. Hashes::Hashes(unsigned int const Hashes) : d(new PrivateHashes(Hashes)) {}
  377. Hashes::Hashes(HashStringList const &Hashes) : d(new PrivateHashes(Hashes)) {}
  378. Hashes::~Hashes() { delete d; }
  379. APT_IGNORE_DEPRECATED_POP