Chromium Code Reviews| OLD | NEW | 
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/net/packed_ct_ev_whitelist.h" | |
| 6 | |
| 7 #include <string.h> | |
| 8 | |
| 9 #include <algorithm> | |
| 10 | |
| 11 #include "base/big_endian.h" | |
| 12 #include "base/files/file_util.h" | |
| 13 #include "base/lazy_instance.h" | |
| 14 #include "base/logging.h" | |
| 15 #include "chrome/browser/net/bit_stream_reader.h" | |
| 16 #include "content/public/browser/browser_thread.h" | |
| 17 #include "net/ssl/ssl_config_service.h" | |
| 18 | |
| 19 namespace { | |
| 20 const uint8_t kCertHashLengthBits = 64; // 8 bytes | |
| 21 const uint8_t kCertHashLength = kCertHashLengthBits / 8; | |
| 22 const uint64_t kGolombMParameterBits = 47; // 2^47 | |
| 23 | |
| 24 void SetNewEVWhitelistInSSLConfigService( | |
| 25 const scoped_refptr<net::ct::EVCertsWhitelist>& new_whitelist) { | |
| 26 net::SSLConfigService::SetEVCertsWhitelist(new_whitelist); | |
| 27 } | |
| 28 | |
| 29 int TruncatedHashesComparator(const void* v1, const void* v2) { | |
| 30 return *(static_cast<const uint64_t*>(v1)) < | |
| 31 *(static_cast<const uint64_t*>(v2)); | |
| 32 } | |
| 33 } // namespace | |
| 34 | |
| 35 void SetEVWhitelistFromFile(const base::FilePath& compressed_whitelist_file) { | |
| 36 VLOG(1) << "Setting EV whitelist from file: " | |
| 37 << compressed_whitelist_file.value(); | |
| 38 std::string compressed_list; | |
| 39 if (!base::ReadFileToString(compressed_whitelist_file, &compressed_list)) { | |
| 40 VLOG(1) << "Failed reading from " << compressed_whitelist_file.value(); | |
| 41 return; | |
| 42 } | |
| 43 | |
| 44 scoped_refptr<net::ct::EVCertsWhitelist> new_whitelist( | |
| 45 new PackedEVCertsWhitelist(compressed_list)); | |
| 46 if (!new_whitelist->IsValid()) { | |
| 47 VLOG(1) << "Failed uncompressing EV certs whitelist."; | |
| 48 return; | |
| 49 } | |
| 50 | |
| 51 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.
 
 | |
| 52 base::Bind(SetNewEVWhitelistInSSLConfigService, new_whitelist); | |
| 53 content::BrowserThread::PostTask( | |
| 54 content::BrowserThread::IO, FROM_HERE, assign_cb); | |
| 55 } | |
| 56 | |
| 57 bool PackedEVCertsWhitelist::UncompressEVWhitelist( | |
| 58 const std::string& compressed_whitelist, | |
| 59 std::vector<std::string>* uncompressed_list) { | |
| 60 internal::BitStreamReader reader(base::StringPiece( | |
| 61 compressed_whitelist.data(), compressed_whitelist.size())); | |
| 62 std::vector<std::string> result; | |
| 63 | |
| 64 VLOG(1) << "Uncompressing EV whitelist of size " | |
| 65 << compressed_whitelist.size(); | |
| 66 uint64_t curr_hash(0); | |
| 67 if (!reader.ReadBits(kCertHashLengthBits, &curr_hash)) { | |
| 68 VLOG(1) << "Failed reading first hash."; | |
| 69 return false; | |
| 70 } | |
| 71 char hash_bytes[kCertHashLength]; | |
| 72 base::WriteBigEndian(hash_bytes, curr_hash); | |
| 73 result.push_back(std::string(hash_bytes, kCertHashLength)); | |
| 74 // M is the tunable parameter used by the Golomb coding. | |
| 75 static const uint64_t kGolombParameterM = static_cast<uint64_t>(1) | |
| 76 << kGolombMParameterBits; | |
| 77 | |
| 78 while (reader.BitsLeft() > kGolombMParameterBits) { | |
| 79 uint64_t read_prefix = 0; | |
| 80 if (!reader.ReadUnaryEncoding(&read_prefix)) { | |
| 81 VLOG(1) << "Failed reading unary-encoded prefix."; | |
| 82 return false; | |
| 83 } | |
| 84 if (read_prefix > (UINT64_MAX / kGolombParameterM)) { | |
| 85 VLOG(1) << "Received value that would cause overflow: " << read_prefix; | |
| 86 return false; | |
| 87 } | |
| 88 | |
| 89 uint64_t r = 0; | |
| 90 if (!reader.ReadBits(kGolombMParameterBits, &r)) { | |
| 91 VLOG(1) << "Failed reading " << kGolombMParameterBits << " bits."; | |
| 92 return false; | |
| 93 } | |
| 94 DCHECK_LT(r, kGolombParameterM); | |
| 95 | |
| 96 uint64_t curr_diff = read_prefix * kGolombParameterM + r; | |
| 97 curr_hash += curr_diff; | |
| 98 | |
| 99 base::WriteBigEndian(hash_bytes, curr_hash); | |
| 100 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.
 
 | |
| 101 } | |
| 102 | |
| 103 uncompressed_list->swap(result); | |
| 104 return true; | |
| 105 } | |
| 106 | |
| 107 PackedEVCertsWhitelist::PackedEVCertsWhitelist( | |
| 108 const std::string& compressed_whitelist) | |
| 109 : is_whitelist_valid_(false) { | |
| 110 std::vector<std::string> new_whitelist; | |
| 111 if (!UncompressEVWhitelist(compressed_whitelist, &new_whitelist)) { | |
| 112 whitelist_.clear(); | |
| 113 return; | |
| 114 } | |
| 115 | |
| 116 whitelist_.resize(new_whitelist.size()); | |
| 117 for (size_t i = 0; i < new_whitelist.size(); ++i) { | |
| 118 base::ReadBigEndian(new_whitelist[i].data(), &(whitelist_[i])); | |
| 119 } | |
| 120 | |
| 121 is_whitelist_valid_ = true; | |
| 122 } | |
| 123 | |
| 124 PackedEVCertsWhitelist::~PackedEVCertsWhitelist() { | |
| 125 } | |
| 126 | |
| 127 bool PackedEVCertsWhitelist::ContainsCertificateHash( | |
| 128 const std::string& certificate_hash) const { | |
| 129 DCHECK(!whitelist_.empty()); | |
| 130 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.
 
 | |
| 131 &whitelist_[0], | |
| 132 whitelist_.size(), | |
| 133 kCertHashLength, | |
| 134 TruncatedHashesComparator) != NULL; | |
| 135 } | |
| 136 | |
| 137 bool PackedEVCertsWhitelist::IsValid() const { | |
| 138 return is_whitelist_valid_; | |
| 139 } | |
| OLD | NEW |