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