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 } // namespace | |
24 | |
25 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.
| |
26 | |
27 void SetNewEVWhitelistInSSLConfigService( | |
28 scoped_refptr<net::ct::EVCertsWhitelist> new_whitelist) { | |
29 net::SSLConfigService::SetEVCertsWhitelist(new_whitelist); | |
30 } | |
31 | |
32 } // namespace internal | |
33 | |
34 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.
| |
35 VLOG(1) << "Setting EV whitelist from file: " | |
36 << compressed_whitelist_file.value(); | |
37 std::string compressed_list; | |
38 if (!base::ReadFileToString(compressed_whitelist_file, &compressed_list)) { | |
39 VLOG(1) << "Failed reading from " << compressed_whitelist_file.value(); | |
40 return; | |
41 } | |
42 | |
43 scoped_refptr<net::ct::EVCertsWhitelist> new_whitelist( | |
44 new PackedEVCertsWhitelist(compressed_list)); | |
45 if (!new_whitelist->IsValid()) { | |
46 VLOG(1) << "Failed uncompressing EV certs whitelist."; | |
47 return; | |
48 } | |
49 | |
50 base::Callback<void(void)> assign_cb = | |
51 base::Bind(&internal::SetNewEVWhitelistInSSLConfigService, new_whitelist); | |
52 content::BrowserThread::PostTask( | |
53 content::BrowserThread::IO, FROM_HERE, assign_cb); | |
54 } | |
55 | |
56 bool PackedEVCertsWhitelist::UncompressEVWhitelist( | |
57 const std::string& compressed_whitelist, | |
58 std::vector<std::string>* uncompressed_list) { | |
59 internal::BitStreamReader reader(base::StringPiece( | |
60 compressed_whitelist.data(), compressed_whitelist.size())); | |
61 std::vector<std::string> result; | |
62 | |
63 VLOG(1) << "Uncompressing EV whitelist of size " | |
64 << compressed_whitelist.size(); | |
65 uint64_t curr_hash(0); | |
66 if (!reader.ReadBits(kCertHashLengthBits, &curr_hash)) { | |
67 VLOG(1) << "Failed reading first hash."; | |
68 return false; | |
69 } | |
70 char hash_bytes[8]; | |
71 base::WriteBigEndian(hash_bytes, curr_hash); | |
72 result.push_back(std::string(hash_bytes, 8)); | |
73 static const uint64_t M = static_cast<uint64_t>(1) << kGolombMParameterBits; | |
74 | |
75 while (reader.BitsLeft() > kGolombMParameterBits) { | |
76 uint64_t read_prefix = 0; | |
77 if (!reader.ReadUnaryEncoding(&read_prefix)) { | |
78 VLOG(1) << "Failed reading unary-encoded prefix."; | |
79 return false; | |
80 } | |
81 | |
82 uint64_t r = 0; | |
83 if (!reader.ReadBits(kGolombMParameterBits, &r)) { | |
84 VLOG(1) << "Failed reading " << kGolombMParameterBits << " bits."; | |
85 return false; | |
86 } | |
87 | |
88 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
| |
89 curr_hash += curr_diff; | |
90 | |
91 base::WriteBigEndian(hash_bytes, curr_hash); | |
92 result.push_back(std::string(hash_bytes, 8)); | |
93 } | |
94 | |
95 uncompressed_list->swap(result); | |
96 return true; | |
97 } | |
98 | |
99 namespace { | |
100 | |
101 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.
| |
102 return memcmp(v1, v2, kCertHashLength); | |
103 } | |
104 | |
105 } // namespace | |
106 | |
107 PackedEVCertsWhitelist::PackedEVCertsWhitelist( | |
108 const std::string& compressed_whitelist) | |
109 : is_whitelist_valid_(false), whitelist_(NULL), whitelist_size_(0) { | |
110 std::vector<std::string> new_whitelist; | |
111 if (!UncompressEVWhitelist(compressed_whitelist, &new_whitelist)) | |
112 return; | |
113 | |
114 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.
| |
115 for (size_t i = 0; i < new_whitelist.size(); ++i) { | |
116 memcpy(&(whitelist_[i * kCertHashLength]), | |
117 new_whitelist[i].c_str(), | |
118 kCertHashLength); | |
119 } | |
120 whitelist_size_ = new_whitelist.size(); | |
121 is_whitelist_valid_ = true; | |
122 } | |
123 | |
124 PackedEVCertsWhitelist::~PackedEVCertsWhitelist() { | |
125 } | |
126 | |
127 bool PackedEVCertsWhitelist::ContainsCertificateHash( | |
128 const std::string& certificate_hash) const { | |
129 return bsearch(certificate_hash.c_str(), | |
130 whitelist_, | |
131 whitelist_size_, | |
132 kCertHashLength, | |
133 hash_val_comp) != NULL; | |
134 } | |
135 | |
136 bool PackedEVCertsWhitelist::IsValid() const { | |
137 return is_whitelist_valid_; | |
138 } | |
OLD | NEW |