]>
Commit | Line | Data |
---|---|---|
63b1700f AL |
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 /*{{{*/ | |
ea542140 DK |
14 | #include <config.h> |
15 | ||
63b1700f | 16 | #include <apt-pkg/hashes.h> |
495e5cb2 MV |
17 | #include <apt-pkg/fileutl.h> |
18 | #include <apt-pkg/configuration.h> | |
453b82a3 DK |
19 | #include <apt-pkg/md5.h> |
20 | #include <apt-pkg/sha1.h> | |
21 | #include <apt-pkg/sha2.h> | |
aea7f4c8 | 22 | |
453b82a3 DK |
23 | #include <stddef.h> |
24 | #include <algorithm> | |
ea542140 | 25 | #include <unistd.h> |
448c38bd | 26 | #include <stdlib.h> |
495e5cb2 MV |
27 | #include <string> |
28 | #include <iostream> | |
63b1700f AL |
29 | /*}}}*/ |
30 | ||
f4c3850e | 31 | const char * HashString::_SupportedHashes[] = |
495e5cb2 | 32 | { |
23397c9d | 33 | "SHA512", "SHA256", "SHA1", "MD5Sum", "Checksum-FileSize", NULL |
495e5cb2 MV |
34 | }; |
35 | ||
36 | HashString::HashString() | |
37 | { | |
38 | } | |
39 | ||
8f3ba4e8 | 40 | HashString::HashString(std::string Type, std::string Hash) : Type(Type), Hash(Hash) |
495e5cb2 MV |
41 | { |
42 | } | |
43 | ||
8f3ba4e8 | 44 | HashString::HashString(std::string StringedHash) /*{{{*/ |
495e5cb2 | 45 | { |
f4c3850e | 46 | if (StringedHash.find(":") == std::string::npos) |
495e5cb2 | 47 | { |
f4c3850e DK |
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; | |
495e5cb2 MV |
56 | return; |
57 | } | |
8f3ba4e8 | 58 | std::string::size_type pos = StringedHash.find(":"); |
8a8feb29 | 59 | Type = StringedHash.substr(0,pos); |
495e5cb2 MV |
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 | } | |
92fcbfc1 | 65 | /*}}}*/ |
8f3ba4e8 | 66 | bool HashString::VerifyFile(std::string filename) const /*{{{*/ |
e6645b9f MV |
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 | /*}}}*/ | |
76 | bool 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 | /*}}}*/ | |
86 | std::string HashString::GetHashForFile(std::string filename) const /*{{{*/ | |
495e5cb2 | 87 | { |
8f3ba4e8 | 88 | std::string fileHash; |
495e5cb2 MV |
89 | |
90 | FileFd Fd(filename, FileFd::ReadOnly); | |
f4c3850e | 91 | if(strcasecmp(Type.c_str(), "MD5Sum") == 0) |
495e5cb2 | 92 | { |
2dcf7b8f | 93 | MD5Summation MD5; |
109eb151 | 94 | MD5.AddFD(Fd); |
8f3ba4e8 | 95 | fileHash = (std::string)MD5.Result(); |
2dcf7b8f | 96 | } |
f4c3850e | 97 | else if (strcasecmp(Type.c_str(), "SHA1") == 0) |
495e5cb2 | 98 | { |
2dcf7b8f | 99 | SHA1Summation SHA1; |
109eb151 | 100 | SHA1.AddFD(Fd); |
8f3ba4e8 | 101 | fileHash = (std::string)SHA1.Result(); |
2dcf7b8f | 102 | } |
f4c3850e | 103 | else if (strcasecmp(Type.c_str(), "SHA256") == 0) |
495e5cb2 | 104 | { |
2dcf7b8f | 105 | SHA256Summation SHA256; |
109eb151 | 106 | SHA256.AddFD(Fd); |
8f3ba4e8 | 107 | fileHash = (std::string)SHA256.Result(); |
495e5cb2 | 108 | } |
f4c3850e | 109 | else if (strcasecmp(Type.c_str(), "SHA512") == 0) |
d9b9e9e2 | 110 | { |
2dcf7b8f | 111 | SHA512Summation SHA512; |
109eb151 | 112 | SHA512.AddFD(Fd); |
8f3ba4e8 | 113 | fileHash = (std::string)SHA512.Result(); |
d9b9e9e2 | 114 | } |
23397c9d DK |
115 | else if (strcasecmp(Type.c_str(), "Checksum-FileSize") == 0) |
116 | strprintf(fileHash, "%llu", Fd.FileSize()); | |
495e5cb2 MV |
117 | Fd.Close(); |
118 | ||
e6645b9f | 119 | return fileHash; |
495e5cb2 | 120 | } |
92fcbfc1 | 121 | /*}}}*/ |
f4c3850e | 122 | const char** HashString::SupportedHashes() /*{{{*/ |
495e5cb2 MV |
123 | { |
124 | return _SupportedHashes; | |
125 | } | |
f4c3850e DK |
126 | /*}}}*/ |
127 | APT_PURE bool HashString::empty() const /*{{{*/ | |
495e5cb2 MV |
128 | { |
129 | return (Type.empty() || Hash.empty()); | |
130 | } | |
f4c3850e | 131 | /*}}}*/ |
55ae7a51 MV |
132 | APT_PURE bool HashString::usable() const /*{{{*/ |
133 | { | |
134 | return ( | |
135 | (Type != "Checksum-FileSize") && | |
51c04562 JAK |
136 | (Type != "MD5Sum") && |
137 | (Type != "SHA1") | |
55ae7a51 MV |
138 | ); |
139 | } | |
63d60998 | 140 | /*}}}*/ |
f4c3850e DK |
141 | std::string HashString::toStr() const /*{{{*/ |
142 | { | |
143 | return Type + ":" + Hash; | |
144 | } | |
145 | /*}}}*/ | |
146 | APT_PURE bool HashString::operator==(HashString const &other) const /*{{{*/ | |
147 | { | |
148 | return (strcasecmp(Type.c_str(), other.Type.c_str()) == 0 && Hash == other.Hash); | |
149 | } | |
150 | APT_PURE bool HashString::operator!=(HashString const &other) const | |
151 | { | |
152 | return !(*this == other); | |
153 | } | |
154 | /*}}}*/ | |
155 | ||
b3501edb DK |
156 | bool HashStringList::usable() const /*{{{*/ |
157 | { | |
158 | if (empty() == true) | |
159 | return false; | |
160 | std::string const forcedType = _config->Find("Acquire::ForceHash", ""); | |
161 | if (forcedType.empty() == true) | |
23397c9d | 162 | { |
55ae7a51 MV |
163 | // See if there is at least one usable hash |
164 | for (auto const &hs: list) | |
165 | if (hs.usable()) | |
166 | return true; | |
23397c9d DK |
167 | return false; |
168 | } | |
b3501edb DK |
169 | return find(forcedType) != NULL; |
170 | } | |
171 | /*}}}*/ | |
f4c3850e DK |
172 | HashString const * HashStringList::find(char const * const type) const /*{{{*/ |
173 | { | |
174 | if (type == NULL || type[0] == '\0') | |
175 | { | |
b3501edb | 176 | std::string const forcedType = _config->Find("Acquire::ForceHash", ""); |
f4c3850e DK |
177 | if (forcedType.empty() == false) |
178 | return find(forcedType.c_str()); | |
179 | for (char const * const * t = HashString::SupportedHashes(); *t != NULL; ++t) | |
180 | for (std::vector<HashString>::const_iterator hs = list.begin(); hs != list.end(); ++hs) | |
181 | if (strcasecmp(hs->HashType().c_str(), *t) == 0) | |
182 | return &*hs; | |
183 | return NULL; | |
184 | } | |
185 | for (std::vector<HashString>::const_iterator hs = list.begin(); hs != list.end(); ++hs) | |
186 | if (strcasecmp(hs->HashType().c_str(), type) == 0) | |
187 | return &*hs; | |
188 | return NULL; | |
189 | } | |
190 | /*}}}*/ | |
448c38bd DK |
191 | unsigned long long HashStringList::FileSize() const /*{{{*/ |
192 | { | |
193 | HashString const * const hsf = find("Checksum-FileSize"); | |
194 | if (hsf == NULL) | |
195 | return 0; | |
196 | std::string const hv = hsf->HashValue(); | |
197 | return strtoull(hv.c_str(), NULL, 10); | |
198 | } | |
199 | /*}}}*/ | |
4f51fd86 DK |
200 | bool HashStringList::FileSize(unsigned long long const Size) /*{{{*/ |
201 | { | |
202 | std::string size; | |
203 | strprintf(size, "%llu", Size); | |
204 | return push_back(HashString("Checksum-FileSize", size)); | |
205 | } | |
206 | /*}}}*/ | |
f4c3850e DK |
207 | bool HashStringList::supported(char const * const type) /*{{{*/ |
208 | { | |
209 | for (char const * const * t = HashString::SupportedHashes(); *t != NULL; ++t) | |
210 | if (strcasecmp(*t, type) == 0) | |
211 | return true; | |
212 | return false; | |
213 | } | |
214 | /*}}}*/ | |
215 | bool HashStringList::push_back(const HashString &hashString) /*{{{*/ | |
216 | { | |
217 | if (hashString.HashType().empty() == true || | |
218 | hashString.HashValue().empty() == true || | |
219 | supported(hashString.HashType().c_str()) == false) | |
220 | return false; | |
495e5cb2 | 221 | |
f4c3850e DK |
222 | // ensure that each type is added only once |
223 | HashString const * const hs = find(hashString.HashType().c_str()); | |
224 | if (hs != NULL) | |
225 | return *hs == hashString; | |
226 | ||
227 | list.push_back(hashString); | |
228 | return true; | |
229 | } | |
230 | /*}}}*/ | |
231 | bool HashStringList::VerifyFile(std::string filename) const /*{{{*/ | |
232 | { | |
495b7a61 | 233 | if (usable() == false) |
f4c3850e | 234 | return false; |
495b7a61 DK |
235 | |
236 | Hashes hashes(*this); | |
237 | FileFd file(filename, FileFd::ReadOnly); | |
23397c9d | 238 | HashString const * const hsf = find("Checksum-FileSize"); |
495b7a61 DK |
239 | if (hsf != NULL) |
240 | { | |
241 | std::string fileSize; | |
242 | strprintf(fileSize, "%llu", file.FileSize()); | |
243 | if (hsf->HashValue() != fileSize) | |
244 | return false; | |
245 | } | |
246 | hashes.AddFD(file); | |
247 | HashStringList const hsl = hashes.GetHashStringList(); | |
248 | return hsl == *this; | |
f4c3850e DK |
249 | } |
250 | /*}}}*/ | |
251 | bool HashStringList::operator==(HashStringList const &other) const /*{{{*/ | |
495e5cb2 | 252 | { |
b3501edb DK |
253 | std::string const forcedType = _config->Find("Acquire::ForceHash", ""); |
254 | if (forcedType.empty() == false) | |
255 | { | |
f6d4ab9a | 256 | HashString const * const hs = find(forcedType); |
b3501edb DK |
257 | HashString const * const ohs = other.find(forcedType); |
258 | if (hs == NULL || ohs == NULL) | |
259 | return false; | |
f6d4ab9a | 260 | return *hs == *ohs; |
b3501edb | 261 | } |
f4c3850e DK |
262 | short matches = 0; |
263 | for (const_iterator hs = begin(); hs != end(); ++hs) | |
264 | { | |
265 | HashString const * const ohs = other.find(hs->HashType()); | |
266 | if (ohs == NULL) | |
267 | continue; | |
268 | if (*hs != *ohs) | |
269 | return false; | |
270 | ++matches; | |
271 | } | |
272 | if (matches == 0) | |
273 | return false; | |
274 | return true; | |
275 | } | |
276 | bool HashStringList::operator!=(HashStringList const &other) const | |
277 | { | |
278 | return !(*this == other); | |
495e5cb2 | 279 | } |
f4c3850e | 280 | /*}}}*/ |
495e5cb2 | 281 | |
23397c9d DK |
282 | // PrivateHashes /*{{{*/ |
283 | class PrivateHashes { | |
284 | public: | |
285 | unsigned long long FileSize; | |
9224ce3d | 286 | unsigned int CalcHashes; |
23397c9d | 287 | |
e8afd168 | 288 | explicit PrivateHashes(unsigned int const CalcHashes) : FileSize(0), CalcHashes(CalcHashes) {} |
6c55f07a DK |
289 | explicit PrivateHashes(HashStringList const &Hashes) : FileSize(0) { |
290 | unsigned int calcHashes = Hashes.usable() ? 0 : ~0; | |
291 | if (Hashes.find("MD5Sum") != NULL) | |
292 | calcHashes |= Hashes::MD5SUM; | |
293 | if (Hashes.find("SHA1") != NULL) | |
294 | calcHashes |= Hashes::SHA1SUM; | |
295 | if (Hashes.find("SHA256") != NULL) | |
296 | calcHashes |= Hashes::SHA256SUM; | |
297 | if (Hashes.find("SHA512") != NULL) | |
298 | calcHashes |= Hashes::SHA512SUM; | |
299 | CalcHashes = calcHashes; | |
300 | } | |
23397c9d DK |
301 | }; |
302 | /*}}}*/ | |
b3501edb | 303 | // Hashes::Add* - Add the contents of data or FD /*{{{*/ |
9224ce3d | 304 | bool Hashes::Add(const unsigned char * const Data, unsigned long long const Size) |
b3501edb DK |
305 | { |
306 | bool Res = true; | |
586d8704 | 307 | APT_IGNORE_DEPRECATED_PUSH |
9224ce3d | 308 | if ((d->CalcHashes & MD5SUM) == MD5SUM) |
b3501edb | 309 | Res &= MD5.Add(Data, Size); |
9224ce3d | 310 | if ((d->CalcHashes & SHA1SUM) == SHA1SUM) |
b3501edb | 311 | Res &= SHA1.Add(Data, Size); |
9224ce3d | 312 | if ((d->CalcHashes & SHA256SUM) == SHA256SUM) |
b3501edb | 313 | Res &= SHA256.Add(Data, Size); |
9224ce3d | 314 | if ((d->CalcHashes & SHA512SUM) == SHA512SUM) |
b3501edb | 315 | Res &= SHA512.Add(Data, Size); |
586d8704 | 316 | APT_IGNORE_DEPRECATED_POP |
23397c9d | 317 | d->FileSize += Size; |
b3501edb DK |
318 | return Res; |
319 | } | |
9224ce3d DK |
320 | bool Hashes::Add(const unsigned char * const Data, unsigned long long const Size, unsigned int const Hashes) |
321 | { | |
322 | d->CalcHashes = Hashes; | |
323 | return Add(Data, Size); | |
324 | } | |
325 | bool Hashes::AddFD(int const Fd,unsigned long long Size) | |
63b1700f AL |
326 | { |
327 | unsigned char Buf[64*64]; | |
ce928105 | 328 | bool const ToEOF = (Size == UntilEOF); |
04f4e1a3 | 329 | while (Size != 0 || ToEOF) |
63b1700f | 330 | { |
650faab0 | 331 | unsigned long long n = sizeof(Buf); |
8f3ba4e8 | 332 | if (!ToEOF) n = std::min(Size, n); |
9ce3cfc9 | 333 | ssize_t const Res = read(Fd,Buf,n); |
650faab0 | 334 | if (Res < 0 || (!ToEOF && Res != (ssize_t) n)) // error, or short read |
1dab797c | 335 | return false; |
04f4e1a3 | 336 | if (ToEOF && Res == 0) // EOF |
1dab797c | 337 | break; |
63b1700f | 338 | Size -= Res; |
9224ce3d | 339 | if (Add(Buf, Res) == false) |
b3501edb | 340 | return false; |
63b1700f AL |
341 | } |
342 | return true; | |
109eb151 | 343 | } |
9224ce3d DK |
344 | bool Hashes::AddFD(int const Fd,unsigned long long Size, unsigned int const Hashes) |
345 | { | |
346 | d->CalcHashes = Hashes; | |
347 | return AddFD(Fd, Size); | |
348 | } | |
349 | bool Hashes::AddFD(FileFd &Fd,unsigned long long Size) | |
109eb151 DK |
350 | { |
351 | unsigned char Buf[64*64]; | |
352 | bool const ToEOF = (Size == 0); | |
353 | while (Size != 0 || ToEOF) | |
354 | { | |
355 | unsigned long long n = sizeof(Buf); | |
356 | if (!ToEOF) n = std::min(Size, n); | |
357 | unsigned long long a = 0; | |
358 | if (Fd.Read(Buf, n, &a) == false) // error | |
359 | return false; | |
360 | if (ToEOF == false) | |
361 | { | |
362 | if (a != n) // short read | |
363 | return false; | |
364 | } | |
365 | else if (a == 0) // EOF | |
366 | break; | |
367 | Size -= a; | |
9224ce3d | 368 | if (Add(Buf, a) == false) |
b3501edb | 369 | return false; |
109eb151 DK |
370 | } |
371 | return true; | |
9224ce3d DK |
372 | } |
373 | bool Hashes::AddFD(FileFd &Fd,unsigned long long Size, unsigned int const Hashes) | |
374 | { | |
375 | d->CalcHashes = Hashes; | |
376 | return AddFD(Fd, Size); | |
63b1700f AL |
377 | } |
378 | /*}}}*/ | |
b3501edb DK |
379 | HashStringList Hashes::GetHashStringList() |
380 | { | |
381 | HashStringList hashes; | |
586d8704 | 382 | APT_IGNORE_DEPRECATED_PUSH |
9224ce3d DK |
383 | if ((d->CalcHashes & MD5SUM) == MD5SUM) |
384 | hashes.push_back(HashString("MD5Sum", MD5.Result().Value())); | |
385 | if ((d->CalcHashes & SHA1SUM) == SHA1SUM) | |
386 | hashes.push_back(HashString("SHA1", SHA1.Result().Value())); | |
387 | if ((d->CalcHashes & SHA256SUM) == SHA256SUM) | |
388 | hashes.push_back(HashString("SHA256", SHA256.Result().Value())); | |
389 | if ((d->CalcHashes & SHA512SUM) == SHA512SUM) | |
390 | hashes.push_back(HashString("SHA512", SHA512.Result().Value())); | |
586d8704 | 391 | APT_IGNORE_DEPRECATED_POP |
4f51fd86 | 392 | hashes.FileSize(d->FileSize); |
b3501edb DK |
393 | return hashes; |
394 | } | |
586d8704 | 395 | APT_IGNORE_DEPRECATED_PUSH |
6c55f07a DK |
396 | Hashes::Hashes() : d(new PrivateHashes(~0)) { } |
397 | Hashes::Hashes(unsigned int const Hashes) : d(new PrivateHashes(Hashes)) {} | |
398 | Hashes::Hashes(HashStringList const &Hashes) : d(new PrivateHashes(Hashes)) {} | |
23397c9d | 399 | Hashes::~Hashes() { delete d; } |
586d8704 | 400 | APT_IGNORE_DEPRECATED_POP |