hashes.cc 11 KB

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