hashes.cc 11 KB

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