]>
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 DK |
131 | /*}}}*/ |
132 | std::string HashString::toStr() const /*{{{*/ | |
133 | { | |
134 | return Type + ":" + Hash; | |
135 | } | |
136 | /*}}}*/ | |
137 | APT_PURE bool HashString::operator==(HashString const &other) const /*{{{*/ | |
138 | { | |
139 | return (strcasecmp(Type.c_str(), other.Type.c_str()) == 0 && Hash == other.Hash); | |
140 | } | |
141 | APT_PURE bool HashString::operator!=(HashString const &other) const | |
142 | { | |
143 | return !(*this == other); | |
144 | } | |
145 | /*}}}*/ | |
146 | ||
b3501edb DK |
147 | bool 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) | |
23397c9d DK |
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 | } | |
b3501edb DK |
160 | return find(forcedType) != NULL; |
161 | } | |
162 | /*}}}*/ | |
f4c3850e DK |
163 | HashString const * HashStringList::find(char const * const type) const /*{{{*/ |
164 | { | |
165 | if (type == NULL || type[0] == '\0') | |
166 | { | |
b3501edb | 167 | std::string const forcedType = _config->Find("Acquire::ForceHash", ""); |
f4c3850e DK |
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 | /*}}}*/ | |
448c38bd DK |
182 | unsigned 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 | /*}}}*/ | |
4f51fd86 DK |
191 | bool HashStringList::FileSize(unsigned long long const Size) /*{{{*/ |
192 | { | |
193 | std::string size; | |
194 | strprintf(size, "%llu", Size); | |
195 | return push_back(HashString("Checksum-FileSize", size)); | |
196 | } | |
197 | /*}}}*/ | |
f4c3850e DK |
198 | bool HashStringList::supported(char const * const type) /*{{{*/ |
199 | { | |
200 | for (char const * const * t = HashString::SupportedHashes(); *t != NULL; ++t) | |
201 | if (strcasecmp(*t, type) == 0) | |
202 | return true; | |
203 | return false; | |
204 | } | |
205 | /*}}}*/ | |
206 | bool HashStringList::push_back(const HashString &hashString) /*{{{*/ | |
207 | { | |
208 | if (hashString.HashType().empty() == true || | |
209 | hashString.HashValue().empty() == true || | |
210 | supported(hashString.HashType().c_str()) == false) | |
211 | return false; | |
495e5cb2 | 212 | |
f4c3850e DK |
213 | // ensure that each type is added only once |
214 | HashString const * const hs = find(hashString.HashType().c_str()); | |
215 | if (hs != NULL) | |
216 | return *hs == hashString; | |
217 | ||
218 | list.push_back(hashString); | |
219 | return true; | |
220 | } | |
221 | /*}}}*/ | |
222 | bool HashStringList::VerifyFile(std::string filename) const /*{{{*/ | |
223 | { | |
495b7a61 | 224 | if (usable() == false) |
f4c3850e | 225 | return false; |
495b7a61 DK |
226 | |
227 | Hashes hashes(*this); | |
228 | FileFd file(filename, FileFd::ReadOnly); | |
23397c9d | 229 | HashString const * const hsf = find("Checksum-FileSize"); |
495b7a61 DK |
230 | if (hsf != NULL) |
231 | { | |
232 | std::string fileSize; | |
233 | strprintf(fileSize, "%llu", file.FileSize()); | |
234 | if (hsf->HashValue() != fileSize) | |
235 | return false; | |
236 | } | |
237 | hashes.AddFD(file); | |
238 | HashStringList const hsl = hashes.GetHashStringList(); | |
239 | return hsl == *this; | |
f4c3850e DK |
240 | } |
241 | /*}}}*/ | |
242 | bool HashStringList::operator==(HashStringList const &other) const /*{{{*/ | |
495e5cb2 | 243 | { |
b3501edb DK |
244 | std::string const forcedType = _config->Find("Acquire::ForceHash", ""); |
245 | if (forcedType.empty() == false) | |
246 | { | |
f6d4ab9a | 247 | HashString const * const hs = find(forcedType); |
b3501edb DK |
248 | HashString const * const ohs = other.find(forcedType); |
249 | if (hs == NULL || ohs == NULL) | |
250 | return false; | |
f6d4ab9a | 251 | return *hs == *ohs; |
b3501edb | 252 | } |
f4c3850e DK |
253 | short matches = 0; |
254 | for (const_iterator hs = begin(); hs != end(); ++hs) | |
255 | { | |
256 | HashString const * const ohs = other.find(hs->HashType()); | |
257 | if (ohs == NULL) | |
258 | continue; | |
259 | if (*hs != *ohs) | |
260 | return false; | |
261 | ++matches; | |
262 | } | |
263 | if (matches == 0) | |
264 | return false; | |
265 | return true; | |
266 | } | |
267 | bool HashStringList::operator!=(HashStringList const &other) const | |
268 | { | |
269 | return !(*this == other); | |
495e5cb2 | 270 | } |
f4c3850e | 271 | /*}}}*/ |
495e5cb2 | 272 | |
23397c9d DK |
273 | // PrivateHashes /*{{{*/ |
274 | class PrivateHashes { | |
275 | public: | |
276 | unsigned long long FileSize; | |
9224ce3d | 277 | unsigned int CalcHashes; |
23397c9d | 278 | |
e8afd168 | 279 | explicit PrivateHashes(unsigned int const CalcHashes) : FileSize(0), CalcHashes(CalcHashes) {} |
6c55f07a DK |
280 | explicit PrivateHashes(HashStringList const &Hashes) : FileSize(0) { |
281 | unsigned int calcHashes = Hashes.usable() ? 0 : ~0; | |
282 | if (Hashes.find("MD5Sum") != NULL) | |
283 | calcHashes |= Hashes::MD5SUM; | |
284 | if (Hashes.find("SHA1") != NULL) | |
285 | calcHashes |= Hashes::SHA1SUM; | |
286 | if (Hashes.find("SHA256") != NULL) | |
287 | calcHashes |= Hashes::SHA256SUM; | |
288 | if (Hashes.find("SHA512") != NULL) | |
289 | calcHashes |= Hashes::SHA512SUM; | |
290 | CalcHashes = calcHashes; | |
291 | } | |
23397c9d DK |
292 | }; |
293 | /*}}}*/ | |
b3501edb | 294 | // Hashes::Add* - Add the contents of data or FD /*{{{*/ |
9224ce3d | 295 | bool Hashes::Add(const unsigned char * const Data, unsigned long long const Size) |
b3501edb DK |
296 | { |
297 | bool Res = true; | |
586d8704 | 298 | APT_IGNORE_DEPRECATED_PUSH |
9224ce3d | 299 | if ((d->CalcHashes & MD5SUM) == MD5SUM) |
b3501edb | 300 | Res &= MD5.Add(Data, Size); |
9224ce3d | 301 | if ((d->CalcHashes & SHA1SUM) == SHA1SUM) |
b3501edb | 302 | Res &= SHA1.Add(Data, Size); |
9224ce3d | 303 | if ((d->CalcHashes & SHA256SUM) == SHA256SUM) |
b3501edb | 304 | Res &= SHA256.Add(Data, Size); |
9224ce3d | 305 | if ((d->CalcHashes & SHA512SUM) == SHA512SUM) |
b3501edb | 306 | Res &= SHA512.Add(Data, Size); |
586d8704 | 307 | APT_IGNORE_DEPRECATED_POP |
23397c9d | 308 | d->FileSize += Size; |
b3501edb DK |
309 | return Res; |
310 | } | |
9224ce3d DK |
311 | bool Hashes::Add(const unsigned char * const Data, unsigned long long const Size, unsigned int const Hashes) |
312 | { | |
313 | d->CalcHashes = Hashes; | |
314 | return Add(Data, Size); | |
315 | } | |
316 | bool Hashes::AddFD(int const Fd,unsigned long long Size) | |
63b1700f AL |
317 | { |
318 | unsigned char Buf[64*64]; | |
ce928105 | 319 | bool const ToEOF = (Size == UntilEOF); |
04f4e1a3 | 320 | while (Size != 0 || ToEOF) |
63b1700f | 321 | { |
650faab0 | 322 | unsigned long long n = sizeof(Buf); |
8f3ba4e8 | 323 | if (!ToEOF) n = std::min(Size, n); |
9ce3cfc9 | 324 | ssize_t const Res = read(Fd,Buf,n); |
650faab0 | 325 | if (Res < 0 || (!ToEOF && Res != (ssize_t) n)) // error, or short read |
1dab797c | 326 | return false; |
04f4e1a3 | 327 | if (ToEOF && Res == 0) // EOF |
1dab797c | 328 | break; |
63b1700f | 329 | Size -= Res; |
9224ce3d | 330 | if (Add(Buf, Res) == false) |
b3501edb | 331 | return false; |
63b1700f AL |
332 | } |
333 | return true; | |
109eb151 | 334 | } |
9224ce3d DK |
335 | bool Hashes::AddFD(int const Fd,unsigned long long Size, unsigned int const Hashes) |
336 | { | |
337 | d->CalcHashes = Hashes; | |
338 | return AddFD(Fd, Size); | |
339 | } | |
340 | bool Hashes::AddFD(FileFd &Fd,unsigned long long Size) | |
109eb151 DK |
341 | { |
342 | unsigned char Buf[64*64]; | |
343 | bool const ToEOF = (Size == 0); | |
344 | while (Size != 0 || ToEOF) | |
345 | { | |
346 | unsigned long long n = sizeof(Buf); | |
347 | if (!ToEOF) n = std::min(Size, n); | |
348 | unsigned long long a = 0; | |
349 | if (Fd.Read(Buf, n, &a) == false) // error | |
350 | return false; | |
351 | if (ToEOF == false) | |
352 | { | |
353 | if (a != n) // short read | |
354 | return false; | |
355 | } | |
356 | else if (a == 0) // EOF | |
357 | break; | |
358 | Size -= a; | |
9224ce3d | 359 | if (Add(Buf, a) == false) |
b3501edb | 360 | return false; |
109eb151 DK |
361 | } |
362 | return true; | |
9224ce3d DK |
363 | } |
364 | bool Hashes::AddFD(FileFd &Fd,unsigned long long Size, unsigned int const Hashes) | |
365 | { | |
366 | d->CalcHashes = Hashes; | |
367 | return AddFD(Fd, Size); | |
63b1700f AL |
368 | } |
369 | /*}}}*/ | |
b3501edb DK |
370 | HashStringList Hashes::GetHashStringList() |
371 | { | |
372 | HashStringList hashes; | |
586d8704 | 373 | APT_IGNORE_DEPRECATED_PUSH |
9224ce3d DK |
374 | if ((d->CalcHashes & MD5SUM) == MD5SUM) |
375 | hashes.push_back(HashString("MD5Sum", MD5.Result().Value())); | |
376 | if ((d->CalcHashes & SHA1SUM) == SHA1SUM) | |
377 | hashes.push_back(HashString("SHA1", SHA1.Result().Value())); | |
378 | if ((d->CalcHashes & SHA256SUM) == SHA256SUM) | |
379 | hashes.push_back(HashString("SHA256", SHA256.Result().Value())); | |
380 | if ((d->CalcHashes & SHA512SUM) == SHA512SUM) | |
381 | hashes.push_back(HashString("SHA512", SHA512.Result().Value())); | |
586d8704 | 382 | APT_IGNORE_DEPRECATED_POP |
4f51fd86 | 383 | hashes.FileSize(d->FileSize); |
b3501edb DK |
384 | return hashes; |
385 | } | |
586d8704 | 386 | APT_IGNORE_DEPRECATED_PUSH |
6c55f07a DK |
387 | Hashes::Hashes() : d(new PrivateHashes(~0)) { } |
388 | Hashes::Hashes(unsigned int const Hashes) : d(new PrivateHashes(Hashes)) {} | |
389 | Hashes::Hashes(HashStringList const &Hashes) : d(new PrivateHashes(Hashes)) {} | |
23397c9d | 390 | Hashes::~Hashes() { delete d; } |
586d8704 | 391 | APT_IGNORE_DEPRECATED_POP |