Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(73)

Side by Side Diff: net/cert/ct_ev_whitelist.cc

Issue 462543002: Certificate Transparency: Code for unpacking EV cert hashes whitelist (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebasing on master Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 uint8 kCertHashLengthBits = 64; // 8 bytes
23 uint64 kGolombMParameterBits = 47; // 2^47
wtc 2014/08/14 02:04:43 Add 'const' to these two constants.
Eran Messeri 2014/08/14 11:55:07 Done.
24
25 }
wtc 2014/08/14 02:04:43 Add a comment: } // namespace
Eran Messeri 2014/08/14 11:55:07 Done.
26
27 namespace internal {
28
29 BitStreamReader::BitStreamReader(const char* source, size_t length) :
30 source_(source), length_(length), curr_byte_(0), curr_bit_(7) {
31 }
32
33 bool BitStreamReader::ReadUnaryEncoding(uint64* out) {
34 uint64 res(0);
35 if (!HasMoreBits()) {
36 return false;
37 }
38
39 uint8 b = ReadBit();
40 while (HasMoreBits() && b) {
41 res += 1;
42 b = ReadBit();
43 }
44
45 *out = res;
46 return true;
47 }
48
49 bool BitStreamReader::ReadBits(uint8 num_bits, uint64* out) {
50 if (num_bits > 64)
51 return false;
52
53 if (BitsLeft() < num_bits)
54 return false;
55
56 uint64 res(0);
57 for (uint8 i = 0; i < num_bits; ++i) {
58 res |= (static_cast<uint64>(ReadBit()) << (num_bits - (i + 1)));
59 }
60
61 *out = res;
62 return true;
63 }
64
65 uint64 BitStreamReader::BitsLeft() {
66 if (curr_byte_ == length_)
67 return 0;
68 return (length_ - (curr_byte_ + 1)) * 8 + curr_bit_ + 1;
69 }
70
71 bool BitStreamReader::HasMoreBits() {
72 return BitsLeft() > 0;
73 }
74
75 uint8 BitStreamReader::ReadBit() {
76 DCHECK(HasMoreBits());
77 DCHECK(curr_bit_ < 8 && curr_bit_ >= 0);
78 uint8 res = (source_[curr_byte_] & (1 << curr_bit_)) >> curr_bit_;
79 curr_bit_--;
80 if (curr_bit_ < 0) {
81 curr_byte_ += 1;
82 curr_bit_ = 7;
83 }
84
85 return res;
86 }
87
88 void SetEVWhitelistData(std::set<std::string> ev_whitelist) {
89 g_current_ev_whitelist.Get().swap(ev_whitelist);
90 }
91
92 bool UncompressEVWhitelist(
93 const std::string& compressed_whitelist,
94 std::set<std::string>* uncompressed_list) {
95 BitStreamReader reader(
96 compressed_whitelist.data(), compressed_whitelist.size());
97 std::set<std::string> result;
98
99 VLOG(1) << "Uncompressing EV whitelist of size "
100 << compressed_whitelist.size();
101 uint64 curr_hash(0);
102 if (!reader.ReadBits(kCertHashLengthBits, &curr_hash)) {
103 VLOG(1) << "Failed reading first hash.";
104 return false;
105 }
106 char hash_bytes[8];
107 base::WriteBigEndian(hash_bytes, curr_hash);
108 result.insert(std::string(hash_bytes, 8));
109 uint64 M = static_cast<uint64>(1) << kGolombMParameterBits;
110
111 while (reader.BitsLeft() > kGolombMParameterBits) {
112 uint64 read_prefix(0);
113 if (!reader.ReadUnaryEncoding(&read_prefix)) {
114 VLOG(1) << "Failed reading unary-encoded prefix.";
115 return false;
116 }
117
118 uint64 r(0);
119 if (!reader.ReadBits(kGolombMParameterBits, &r)) {
120 VLOG(1) << "Failed reading " << kGolombMParameterBits << " bits.";
121 return false;
122 }
123
124 uint64 curr_diff = read_prefix * M + r;
125 uint64 next_hash = curr_hash + curr_diff;
126
127 base::WriteBigEndian(hash_bytes, next_hash);
128 result.insert(std::string(hash_bytes, 8));
129 curr_hash = next_hash;
130 }
131
132 uncompressed_list->swap(result);
133 return true;
134 }
135
136 } // namespace internal
137
138 void SetEVWhitelistFromFile(const base::FilePath& compressed_whitelist_file) {
139 VLOG(1) << "Setting EV whitelist from file: "
140 << compressed_whitelist_file.value();
141 std::string compressed_list;
142 if (!base::ReadFileToString(compressed_whitelist_file, &compressed_list)) {
143 VLOG(1) << "Failed reading from " << compressed_whitelist_file.value();
144 return;
145 }
146
147 std::set<std::string> uncompressed_list;
148 if (!internal::UncompressEVWhitelist(compressed_list, &uncompressed_list)) {
149 VLOG(1) << "Failed uncompressing.";
150 return;
151 }
152 VLOG(1) << "Uncompressing succeeded, hashes: " << uncompressed_list.size();
153
154 internal::SetEVWhitelistData(uncompressed_list);
155 }
156
157 bool IsCertificateHashInWhitelist(const std::string& cert_hash) {
158 return g_current_ev_whitelist.Get().find(cert_hash) !=
159 g_current_ev_whitelist.Get().end();
160 }
161
162 } // namespace ct
163
164 } // namespace net
165
166
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698