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

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: Created 6 years, 3 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 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), current_byte_(0), current_bit_(7) {
30 }
31
32 bool BitStreamReader::ReadUnaryEncoding(uint64* out) {
33 uint64 res = 0;
34 if (BitsLeft() == 0)
35 return false;
36
37 uint8 b = 1;
38 while ((BitsLeft() > 0) && b) {
39 b = ReadBit();
40 res += b;
41 }
wtc 2014/09/02 23:03:02 Nit: we should be able to simplify this (lines 37-
Eran Messeri 2014/09/03 09:38:50 Done.
42
43 *out = res;
44 return true;
45 }
46
47 bool BitStreamReader::ReadBits(uint8 num_bits, uint64* out) {
48 if (num_bits > 64)
49 return false;
50
51 if (BitsLeft() < num_bits)
52 return false;
53
54 uint64 res = 0;
55 for (uint8 i = 0; i < num_bits; ++i)
56 res |= (static_cast<uint64>(ReadBit()) << (num_bits - (i + 1)));
57
58 *out = res;
59 return true;
60 }
61
62 uint64 BitStreamReader::BitsLeft() const {
63 if (current_byte_ == length_)
64 return 0;
65 return (length_ - (current_byte_ + 1)) * 8 + current_bit_ + 1;
66 }
67
68 uint8 BitStreamReader::ReadBit() {
69 DCHECK(BitsLeft() > 0);
wtc 2014/09/02 23:03:02 Nit: you can use DCHECK_GT.
Eran Messeri 2014/09/03 09:38:50 Done.
70 DCHECK(current_bit_ < 8 && current_bit_ >= 0);
71 uint8 res = (source_[current_byte_] & (1 << current_bit_)) >> current_bit_;
72 current_bit_--;
73 if (current_bit_ < 0) {
74 current_byte_ += 1;
wtc 2014/09/02 23:03:02 Nit: use ++.
Eran Messeri 2014/09/03 09:38:50 Done.
75 current_bit_ = 7;
76 }
77
78 return res;
79 }
80
81 bool UncompressEVWhitelist(const std::string& compressed_whitelist,
82 std::set<std::string>* uncompressed_list) {
83 BitStreamReader reader(compressed_whitelist.data(),
84 compressed_whitelist.size());
85 std::set<std::string> result;
86
87 VLOG(1) << "Uncompressing EV whitelist of size "
88 << compressed_whitelist.size();
89 uint64 curr_hash(0);
90 if (!reader.ReadBits(kCertHashLengthBits, &curr_hash)) {
91 VLOG(1) << "Failed reading first hash.";
92 return false;
93 }
94 char hash_bytes[8];
95 base::WriteBigEndian(hash_bytes, curr_hash);
96 result.insert(std::string(hash_bytes, 8));
97 uint64 M = static_cast<uint64>(1) << kGolombMParameterBits;
wtc 2014/09/02 23:03:02 What I meant is to declare this as either "const"
Eran Messeri 2014/09/03 09:38:50 Done.
98
99 while (reader.BitsLeft() > kGolombMParameterBits) {
100 uint64 read_prefix = 0;
101 if (!reader.ReadUnaryEncoding(&read_prefix)) {
102 VLOG(1) << "Failed reading unary-encoded prefix.";
103 return false;
104 }
105
106 uint64 r = 0;
107 if (!reader.ReadBits(kGolombMParameterBits, &r)) {
108 VLOG(1) << "Failed reading " << kGolombMParameterBits << " bits.";
109 return false;
110 }
111
112 uint64 curr_diff = read_prefix * M + r;
113
114 base::WriteBigEndian(hash_bytes, curr_hash + curr_diff);
115 result.insert(std::string(hash_bytes, 8));
116 curr_hash += curr_diff;
wtc 2014/09/02 23:03:01 What I meant is to replace lines 113-116 with the
Eran Messeri 2014/09/03 09:38:50 Done, good point.
117 }
118
119 uncompressed_list->swap(result);
120 return true;
121 }
122
123 void SetEVWhitelistData(std::set<std::string>& ev_whitelist) {
124 g_current_ev_whitelist.Get().swap(ev_whitelist);
125 }
126
127 } // namespace internal
128
129 void SetEVWhitelistFromFile(const base::FilePath& compressed_whitelist_file) {
130 VLOG(1) << "Setting EV whitelist from file: "
131 << compressed_whitelist_file.value();
132 std::string compressed_list;
133 if (!base::ReadFileToString(compressed_whitelist_file, &compressed_list)) {
134 VLOG(1) << "Failed reading from " << compressed_whitelist_file.value();
135 return;
136 }
137
138 std::set<std::string> uncompressed_list;
139 if (!internal::UncompressEVWhitelist(compressed_list, &uncompressed_list)) {
140 VLOG(1) << "Failed uncompressing.";
141 return;
142 }
143 VLOG(1) << "Uncompressing succeeded, hashes: " << uncompressed_list.size();
144
145 internal::SetEVWhitelistData(uncompressed_list);
146 }
147
148 bool IsCertificateHashInWhitelist(const std::string& certificate_hash) {
149 const std::set<std::string>& current_ev_whitelist =
150 g_current_ev_whitelist.Get();
151 return current_ev_whitelist.find(certificate_hash) !=
152 current_ev_whitelist.end();
153 }
154
155 } // namespace ct
156
157 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698