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 "net/cert/ct_ev_whitelist.h" | |
6 | |
7 #include <set> | |
8 | |
9 #include "base/big_endian.h" | |
10 #include "base/file_util.h" | |
11 #include "base/lazy_instance.h" | |
12 #include "base/logging.h" | |
13 | |
14 namespace net { | |
15 | |
16 namespace ct { | |
17 | |
18 namespace { | |
19 base::LazyInstance<std::set<std::string> >::Leaky g_current_ev_whitelist = | |
20 LAZY_INSTANCE_INITIALIZER; | |
21 | |
22 const uint8 kCertHashLengthBits = 64; // 8 bytes | |
23 const uint64 kGolombMParameterBits = 47; // 2^47 | |
24 } // namespace | |
25 | |
26 namespace internal { | |
27 | |
28 BitStreamReader::BitStreamReader(const char* source, size_t length) | |
29 : source_(source), length_(length), curr_byte_(0), curr_bit_(7) { | |
30 } | |
31 | |
32 bool BitStreamReader::ReadUnaryEncoding(uint64* out) { | |
33 uint64 res(0); | |
wtc
2014/08/20 02:52:07
Nit: for this line and other similar lines, I'd pr
Eran Messeri
2014/09/02 12:20:22
Done, throughout the file.
| |
34 if (!HasMoreBits()) { | |
35 return false; | |
36 } | |
wtc
2014/08/20 02:52:07
Nit: omit curly braces.
Eran Messeri
2014/09/02 12:20:22
Done, throughout the file.
| |
37 | |
38 uint8 b = ReadBit(); | |
39 while (HasMoreBits() && b) { | |
40 res += 1; | |
41 b = ReadBit(); | |
42 } | |
43 | |
44 *out = res; | |
45 return true; | |
46 } | |
47 | |
48 bool BitStreamReader::ReadBits(uint8 num_bits, uint64* out) { | |
49 if (num_bits > 64) | |
50 return false; | |
51 | |
52 if (BitsLeft() < num_bits) | |
53 return false; | |
54 | |
55 uint64 res(0); | |
56 for (uint8 i = 0; i < num_bits; ++i) { | |
57 res |= (static_cast<uint64>(ReadBit()) << (num_bits - (i + 1))); | |
58 } | |
wtc
2014/08/20 02:52:07
Nit: omit curly braces.
Eran Messeri
2014/09/02 12:20:22
Done.
| |
59 | |
60 *out = res; | |
61 return true; | |
62 } | |
63 | |
64 uint64 BitStreamReader::BitsLeft() const { | |
65 if (curr_byte_ == length_) | |
66 return 0; | |
67 return (length_ - (curr_byte_ + 1)) * 8 + curr_bit_ + 1; | |
68 } | |
69 | |
70 bool BitStreamReader::HasMoreBits() const { | |
wtc
2014/08/20 02:52:07
Nit: this function doesn't seem very useful. I wou
Eran Messeri
2014/09/02 12:20:22
Done. Personally I find it more readable not havin
| |
71 return BitsLeft() > 0; | |
72 } | |
73 | |
74 uint8 BitStreamReader::ReadBit() { | |
75 DCHECK(HasMoreBits()); | |
76 DCHECK(curr_bit_ < 8 && curr_bit_ >= 0); | |
77 uint8 res = (source_[curr_byte_] & (1 << curr_bit_)) >> curr_bit_; | |
78 curr_bit_--; | |
79 if (curr_bit_ < 0) { | |
80 curr_byte_ += 1; | |
81 curr_bit_ = 7; | |
82 } | |
83 | |
84 return res; | |
85 } | |
86 | |
87 void SetEVWhitelistData(std::set<std::string> ev_whitelist) { | |
wtc
2014/08/20 02:52:07
Nit: in the .h file this function is declared afte
Eran Messeri
2014/09/02 12:20:22
Done.
| |
88 g_current_ev_whitelist.Get().swap(ev_whitelist); | |
89 } | |
90 | |
91 bool UncompressEVWhitelist(const std::string& compressed_whitelist, | |
92 std::set<std::string>* uncompressed_list) { | |
93 BitStreamReader reader(compressed_whitelist.data(), | |
94 compressed_whitelist.size()); | |
95 std::set<std::string> result; | |
96 | |
97 VLOG(1) << "Uncompressing EV whitelist of size " | |
98 << compressed_whitelist.size(); | |
99 uint64 curr_hash(0); | |
100 if (!reader.ReadBits(kCertHashLengthBits, &curr_hash)) { | |
101 VLOG(1) << "Failed reading first hash."; | |
102 return false; | |
103 } | |
104 char hash_bytes[8]; | |
105 base::WriteBigEndian(hash_bytes, curr_hash); | |
106 result.insert(std::string(hash_bytes, 8)); | |
107 uint64 M = static_cast<uint64>(1) << kGolombMParameterBits; | |
wtc
2014/08/20 02:52:07
Nit: this can be defined as a compile-time constan
Eran Messeri
2014/09/02 12:20:22
Acknowledged, I prefer to leave it defined here th
| |
108 | |
109 while (reader.BitsLeft() > kGolombMParameterBits) { | |
110 uint64 read_prefix(0); | |
111 if (!reader.ReadUnaryEncoding(&read_prefix)) { | |
112 VLOG(1) << "Failed reading unary-encoded prefix."; | |
113 return false; | |
114 } | |
115 | |
116 uint64 r(0); | |
117 if (!reader.ReadBits(kGolombMParameterBits, &r)) { | |
118 VLOG(1) << "Failed reading " << kGolombMParameterBits << " bits."; | |
119 return false; | |
120 } | |
121 | |
122 uint64 curr_diff = read_prefix * M + r; | |
123 uint64 next_hash = curr_hash + curr_diff; | |
wtc
2014/08/20 02:52:07
Nit: we don't seem to need the |next_hash| variabl
Eran Messeri
2014/09/02 12:20:22
Done.
| |
124 | |
125 base::WriteBigEndian(hash_bytes, next_hash); | |
126 result.insert(std::string(hash_bytes, 8)); | |
127 curr_hash = next_hash; | |
128 } | |
129 | |
130 uncompressed_list->swap(result); | |
131 return true; | |
132 } | |
133 | |
134 } // namespace internal | |
135 | |
136 void SetEVWhitelistFromFile(const base::FilePath& compressed_whitelist_file) { | |
137 VLOG(1) << "Setting EV whitelist from file: " | |
138 << compressed_whitelist_file.value(); | |
139 std::string compressed_list; | |
140 if (!base::ReadFileToString(compressed_whitelist_file, &compressed_list)) { | |
141 VLOG(1) << "Failed reading from " << compressed_whitelist_file.value(); | |
142 return; | |
143 } | |
144 | |
145 std::set<std::string> uncompressed_list; | |
146 if (!internal::UncompressEVWhitelist(compressed_list, &uncompressed_list)) { | |
147 VLOG(1) << "Failed uncompressing."; | |
148 return; | |
149 } | |
150 VLOG(1) << "Uncompressing succeeded, hashes: " << uncompressed_list.size(); | |
151 | |
152 internal::SetEVWhitelistData(uncompressed_list); | |
153 } | |
154 | |
155 bool IsCertificateHashInWhitelist(const std::string& certificate_hash) { | |
156 return g_current_ev_whitelist.Get().find(certificate_hash) != | |
157 g_current_ev_whitelist.Get().end(); | |
wtc
2014/08/20 02:52:07
Nit: store the return value of g_current_ev_whitel
Eran Messeri
2014/09/02 12:20:22
Done.
| |
158 } | |
159 | |
160 } // namespace ct | |
161 | |
162 } // namespace net | |
OLD | NEW |