hashes.cc 11 KB

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