Chromium Code Reviews| Index: chrome/browser/net/packed_ct_ev_whitelist.cc |
| diff --git a/chrome/browser/net/packed_ct_ev_whitelist.cc b/chrome/browser/net/packed_ct_ev_whitelist.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..adca99d0269d1db302d1432f8d4d1815ef0d9a7e |
| --- /dev/null |
| +++ b/chrome/browser/net/packed_ct_ev_whitelist.cc |
| @@ -0,0 +1,138 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/net/packed_ct_ev_whitelist.h" |
| + |
| +#include <string.h> |
| + |
| +#include <algorithm> |
| + |
| +#include "base/big_endian.h" |
| +#include "base/files/file_util.h" |
| +#include "base/lazy_instance.h" |
| +#include "base/logging.h" |
| +#include "chrome/browser/net/bit_stream_reader.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "net/ssl/ssl_config_service.h" |
| + |
| +namespace { |
| +const uint8_t kCertHashLengthBits = 64; // 8 bytes |
| +const uint8_t kCertHashLength = kCertHashLengthBits / 8; |
| +const uint64_t kGolombMParameterBits = 47; // 2^47 |
| +} // namespace |
| + |
| +namespace internal { |
|
Ryan Sleevi
2014/10/20 19:18:25
There's no need for this to be in the internal nam
Eran Messeri
2014/10/21 14:59:59
Done.
|
| + |
| +void SetNewEVWhitelistInSSLConfigService( |
| + scoped_refptr<net::ct::EVCertsWhitelist> new_whitelist) { |
| + net::SSLConfigService::SetEVCertsWhitelist(new_whitelist); |
| +} |
| + |
| +} // namespace internal |
| + |
| +void SetEVWhitelistFromFile(const base::FilePath& compressed_whitelist_file) { |
|
Ryan Sleevi
2014/10/20 19:18:25
The documentation for this method needs to be upda
Eran Messeri
2014/10/21 14:59:59
Done.
|
| + VLOG(1) << "Setting EV whitelist from file: " |
| + << compressed_whitelist_file.value(); |
| + std::string compressed_list; |
| + if (!base::ReadFileToString(compressed_whitelist_file, &compressed_list)) { |
| + VLOG(1) << "Failed reading from " << compressed_whitelist_file.value(); |
| + return; |
| + } |
| + |
| + scoped_refptr<net::ct::EVCertsWhitelist> new_whitelist( |
| + new PackedEVCertsWhitelist(compressed_list)); |
| + if (!new_whitelist->IsValid()) { |
| + VLOG(1) << "Failed uncompressing EV certs whitelist."; |
| + return; |
| + } |
| + |
| + base::Callback<void(void)> assign_cb = |
| + base::Bind(&internal::SetNewEVWhitelistInSSLConfigService, new_whitelist); |
| + content::BrowserThread::PostTask( |
| + content::BrowserThread::IO, FROM_HERE, assign_cb); |
| +} |
| + |
| +bool PackedEVCertsWhitelist::UncompressEVWhitelist( |
| + const std::string& compressed_whitelist, |
| + std::vector<std::string>* uncompressed_list) { |
| + internal::BitStreamReader reader(base::StringPiece( |
| + compressed_whitelist.data(), compressed_whitelist.size())); |
| + std::vector<std::string> result; |
| + |
| + VLOG(1) << "Uncompressing EV whitelist of size " |
| + << compressed_whitelist.size(); |
| + uint64_t curr_hash(0); |
| + if (!reader.ReadBits(kCertHashLengthBits, &curr_hash)) { |
| + VLOG(1) << "Failed reading first hash."; |
| + return false; |
| + } |
| + char hash_bytes[8]; |
| + base::WriteBigEndian(hash_bytes, curr_hash); |
| + result.push_back(std::string(hash_bytes, 8)); |
| + static const uint64_t M = static_cast<uint64_t>(1) << kGolombMParameterBits; |
| + |
| + while (reader.BitsLeft() > kGolombMParameterBits) { |
| + uint64_t read_prefix = 0; |
| + if (!reader.ReadUnaryEncoding(&read_prefix)) { |
| + VLOG(1) << "Failed reading unary-encoded prefix."; |
| + return false; |
| + } |
| + |
| + uint64_t r = 0; |
| + if (!reader.ReadBits(kGolombMParameterBits, &r)) { |
| + VLOG(1) << "Failed reading " << kGolombMParameterBits << " bits."; |
| + return false; |
| + } |
| + |
| + uint64_t curr_diff = read_prefix * M + r; |
|
Ryan Sleevi
2014/10/20 19:18:25
Overflow?
Eran Messeri
2014/10/21 14:59:59
Good catch, added checks for validity of data and
|
| + curr_hash += curr_diff; |
| + |
| + base::WriteBigEndian(hash_bytes, curr_hash); |
| + result.push_back(std::string(hash_bytes, 8)); |
| + } |
| + |
| + uncompressed_list->swap(result); |
| + return true; |
| +} |
| + |
| +namespace { |
| + |
| +int hash_val_comp(const void* v1, const void* v2) { |
|
Ryan Sleevi
2014/10/20 19:18:25
Group your methods from the unnamed namespace into
Eran Messeri
2014/10/21 14:59:59
Done.
|
| + return memcmp(v1, v2, kCertHashLength); |
| +} |
| + |
| +} // namespace |
| + |
| +PackedEVCertsWhitelist::PackedEVCertsWhitelist( |
| + const std::string& compressed_whitelist) |
| + : is_whitelist_valid_(false), whitelist_(NULL), whitelist_size_(0) { |
| + std::vector<std::string> new_whitelist; |
| + if (!UncompressEVWhitelist(compressed_whitelist, &new_whitelist)) |
| + return; |
| + |
| + whitelist_ = new char[new_whitelist.size() * kCertHashLength]; |
|
Ryan Sleevi
2014/10/20 19:18:25
LEAK: You never deallocate this. What if someone u
Eran Messeri
2014/10/21 14:59:59
Good catch, done.
|
| + for (size_t i = 0; i < new_whitelist.size(); ++i) { |
| + memcpy(&(whitelist_[i * kCertHashLength]), |
| + new_whitelist[i].c_str(), |
| + kCertHashLength); |
| + } |
| + whitelist_size_ = new_whitelist.size(); |
| + is_whitelist_valid_ = true; |
| +} |
| + |
| +PackedEVCertsWhitelist::~PackedEVCertsWhitelist() { |
| +} |
| + |
| +bool PackedEVCertsWhitelist::ContainsCertificateHash( |
| + const std::string& certificate_hash) const { |
| + return bsearch(certificate_hash.c_str(), |
| + whitelist_, |
| + whitelist_size_, |
| + kCertHashLength, |
| + hash_val_comp) != NULL; |
| +} |
| + |
| +bool PackedEVCertsWhitelist::IsValid() const { |
| + return is_whitelist_valid_; |
| +} |