| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/safe_browsing_db/v4_protocol_manager_util.h" | 5 #include "components/safe_browsing_db/v4_protocol_manager_util.h" |
| 6 | 6 |
| 7 #include "base/base64.h" | 7 #include "base/base64.h" |
| 8 #include "base/metrics/histogram_macros.h" | 8 #include "base/metrics/histogram_macros.h" |
| 9 #include "base/rand_util.h" | 9 #include "base/rand_util.h" |
| 10 #include "base/sha1.h" | 10 #include "base/sha1.h" |
| (...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 280 // NOTE(vakh): The following header informs the envelope server (which sits in | 280 // NOTE(vakh): The following header informs the envelope server (which sits in |
| 281 // front of Google's stubby server) that the received GET request should be | 281 // front of Google's stubby server) that the received GET request should be |
| 282 // interpreted as a POST. | 282 // interpreted as a POST. |
| 283 headers->SetHeaderIfMissing("X-HTTP-Method-Override", "POST"); | 283 headers->SetHeaderIfMissing("X-HTTP-Method-Override", "POST"); |
| 284 } | 284 } |
| 285 | 285 |
| 286 // static | 286 // static |
| 287 void V4ProtocolManagerUtil::UrlToFullHashes( | 287 void V4ProtocolManagerUtil::UrlToFullHashes( |
| 288 const GURL& url, | 288 const GURL& url, |
| 289 std::vector<FullHash>* full_hashes) { | 289 std::vector<FullHash>* full_hashes) { |
| 290 std::string canon_host, canon_path, canon_query; | 290 std::vector<std::string> hosts; |
| 291 CanonicalizeUrl(url, &canon_host, &canon_path, &canon_query); | 291 std::vector<std::string> paths; |
| 292 GenerateHostAndPathVariants(url, &hosts, &paths); |
| 292 | 293 |
| 293 std::vector<std::string> hosts; | |
| 294 if (url.HostIsIPAddress()) { | |
| 295 hosts.push_back(url.host()); | |
| 296 } else { | |
| 297 GenerateHostVariantsToCheck(canon_host, &hosts); | |
| 298 } | |
| 299 | |
| 300 std::vector<std::string> paths; | |
| 301 GeneratePathVariantsToCheck(canon_path, canon_query, &paths); | |
| 302 for (const std::string& host : hosts) { | 294 for (const std::string& host : hosts) { |
| 303 for (const std::string& path : paths) { | 295 for (const std::string& path : paths) { |
| 304 full_hashes->push_back(crypto::SHA256HashString(host + path)); | 296 full_hashes->push_back(crypto::SHA256HashString(host + path)); |
| 305 } | 297 } |
| 306 } | 298 } |
| 307 } | 299 } |
| 308 | 300 |
| 309 // static | 301 // static |
| 302 void V4ProtocolManagerUtil::GenerateHostAndPathVariants( |
| 303 const GURL& url, |
| 304 std::vector<std::string>* hosts, |
| 305 std::vector<std::string>* paths) { |
| 306 std::string canon_host, canon_path, canon_query; |
| 307 CanonicalizeUrl(url, &canon_host, &canon_path, &canon_query); |
| 308 |
| 309 hosts->clear(); |
| 310 paths->clear(); |
| 311 if (url.HostIsIPAddress()) { |
| 312 hosts->push_back(url.host()); |
| 313 } else { |
| 314 GenerateHostVariantsToCheck(canon_host, hosts); |
| 315 } |
| 316 GeneratePathVariantsToCheck(canon_path, canon_query, paths); |
| 317 } |
| 318 |
| 319 // static |
| 310 bool V4ProtocolManagerUtil::FullHashToHashPrefix(const FullHash& full_hash, | 320 bool V4ProtocolManagerUtil::FullHashToHashPrefix(const FullHash& full_hash, |
| 311 PrefixSize prefix_size, | 321 PrefixSize prefix_size, |
| 312 HashPrefix* hash_prefix) { | 322 HashPrefix* hash_prefix) { |
| 313 if (full_hash.size() < prefix_size) { | 323 if (full_hash.size() < prefix_size) { |
| 314 return false; | 324 return false; |
| 315 } | 325 } |
| 316 *hash_prefix = full_hash.substr(prefix_size); | 326 *hash_prefix = full_hash.substr(prefix_size); |
| 317 return true; | 327 return true; |
| 318 } | 328 } |
| 319 | 329 |
| (...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 581 | 591 |
| 582 const std::string hash = base::SHA1HashString(packed_ip); | 592 const std::string hash = base::SHA1HashString(packed_ip); |
| 583 DCHECK_EQ(20u, hash.size()); | 593 DCHECK_EQ(20u, hash.size()); |
| 584 hashed_encoded_ip->resize(hash.size() + 1); | 594 hashed_encoded_ip->resize(hash.size() + 1); |
| 585 hashed_encoded_ip->replace(0, hash.size(), hash); | 595 hashed_encoded_ip->replace(0, hash.size(), hash); |
| 586 (*hashed_encoded_ip)[hash.size()] = static_cast<unsigned char>(128); | 596 (*hashed_encoded_ip)[hash.size()] = static_cast<unsigned char>(128); |
| 587 return true; | 597 return true; |
| 588 } | 598 } |
| 589 | 599 |
| 590 } // namespace safe_browsing | 600 } // namespace safe_browsing |
| OLD | NEW |