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..f57dbe0b826c6ea9aa0072535e62e939f73ed895 |
| --- /dev/null |
| +++ b/chrome/browser/net/packed_ct_ev_whitelist.cc |
| @@ -0,0 +1,139 @@ |
| +// 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 |
| + |
| +void SetNewEVWhitelistInSSLConfigService( |
| + const scoped_refptr<net::ct::EVCertsWhitelist>& new_whitelist) { |
| + net::SSLConfigService::SetEVCertsWhitelist(new_whitelist); |
| +} |
| + |
| +int TruncatedHashesComparator(const void* v1, const void* v2) { |
| + return *(static_cast<const uint64_t*>(v1)) < |
| + *(static_cast<const uint64_t*>(v2)); |
| +} |
| +} // namespace |
| + |
| +void SetEVWhitelistFromFile(const base::FilePath& compressed_whitelist_file) { |
| + 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 = |
|
Ryan Sleevi
2014/10/27 17:09:48
you can use base::Closure, but that's no big deal.
Eran Messeri
2014/10/28 11:43:58
Done.
|
| + base::Bind(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[kCertHashLength]; |
| + base::WriteBigEndian(hash_bytes, curr_hash); |
| + result.push_back(std::string(hash_bytes, kCertHashLength)); |
| + // M is the tunable parameter used by the Golomb coding. |
| + static const uint64_t kGolombParameterM = 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; |
| + } |
| + if (read_prefix > (UINT64_MAX / kGolombParameterM)) { |
| + VLOG(1) << "Received value that would cause overflow: " << read_prefix; |
| + return false; |
| + } |
| + |
| + uint64_t r = 0; |
| + if (!reader.ReadBits(kGolombMParameterBits, &r)) { |
| + VLOG(1) << "Failed reading " << kGolombMParameterBits << " bits."; |
| + return false; |
| + } |
| + DCHECK_LT(r, kGolombParameterM); |
| + |
| + uint64_t curr_diff = read_prefix * kGolombParameterM + r; |
| + curr_hash += curr_diff; |
| + |
| + base::WriteBigEndian(hash_bytes, curr_hash); |
| + result.push_back(std::string(hash_bytes, kCertHashLength)); |
|
Ryan Sleevi
2014/10/27 17:09:48
Why convert to an std::string, when you have it as
Eran Messeri
2014/10/28 11:43:58
No reason - changed.
|
| + } |
| + |
| + uncompressed_list->swap(result); |
| + return true; |
| +} |
| + |
| +PackedEVCertsWhitelist::PackedEVCertsWhitelist( |
| + const std::string& compressed_whitelist) |
| + : is_whitelist_valid_(false) { |
| + std::vector<std::string> new_whitelist; |
| + if (!UncompressEVWhitelist(compressed_whitelist, &new_whitelist)) { |
| + whitelist_.clear(); |
| + return; |
| + } |
| + |
| + whitelist_.resize(new_whitelist.size()); |
| + for (size_t i = 0; i < new_whitelist.size(); ++i) { |
| + base::ReadBigEndian(new_whitelist[i].data(), &(whitelist_[i])); |
| + } |
| + |
| + is_whitelist_valid_ = true; |
| +} |
| + |
| +PackedEVCertsWhitelist::~PackedEVCertsWhitelist() { |
| +} |
| + |
| +bool PackedEVCertsWhitelist::ContainsCertificateHash( |
| + const std::string& certificate_hash) const { |
| + DCHECK(!whitelist_.empty()); |
| + return bsearch(certificate_hash.c_str(), |
|
Ryan Sleevi
2014/10/27 17:09:48
Isn't this where you should be ensuring big endian
Eran Messeri
2014/10/28 11:43:58
Good catch, done.
|
| + &whitelist_[0], |
| + whitelist_.size(), |
| + kCertHashLength, |
| + TruncatedHashesComparator) != NULL; |
| +} |
| + |
| +bool PackedEVCertsWhitelist::IsValid() const { |
| + return is_whitelist_valid_; |
| +} |