OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/certificate_transparency/log_dns_client.h" | 5 #include "components/certificate_transparency/log_dns_client.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback_helpers.h" | |
8 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
9 #include "base/location.h" | 10 #include "base/location.h" |
10 #include "base/logging.h" | 11 #include "base/logging.h" |
11 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
12 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
13 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
14 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
15 #include "base/threading/thread_task_runner_handle.h" | 16 #include "base/threading/thread_task_runner_handle.h" |
16 #include "base/time/time.h" | 17 #include "base/time/time.h" |
17 #include "components/base32/base32.h" | 18 #include "components/base32/base32.h" |
18 #include "crypto/sha2.h" | 19 #include "crypto/sha2.h" |
19 #include "net/base/net_errors.h" | |
20 #include "net/cert/merkle_audit_proof.h" | 20 #include "net/cert/merkle_audit_proof.h" |
21 #include "net/dns/dns_client.h" | 21 #include "net/dns/dns_client.h" |
22 #include "net/dns/dns_config_service.h" | 22 #include "net/dns/dns_config_service.h" |
23 #include "net/dns/dns_protocol.h" | 23 #include "net/dns/dns_protocol.h" |
24 #include "net/dns/dns_response.h" | 24 #include "net/dns/dns_response.h" |
25 #include "net/dns/dns_transaction.h" | 25 #include "net/dns/dns_transaction.h" |
26 #include "net/dns/record_parsed.h" | 26 #include "net/dns/record_parsed.h" |
27 #include "net/dns/record_rdata.h" | 27 #include "net/dns/record_rdata.h" |
28 | 28 |
29 namespace certificate_transparency { | 29 namespace certificate_transparency { |
30 | 30 |
31 namespace { | 31 namespace { |
32 | 32 |
33 // Parses the DNS response and extracts a single string from the TXT RDATA. | 33 // Parses the DNS response and extracts a single string from the TXT RDATA. |
34 // If the response is malformed, not a TXT record, or contains any number of | 34 // If the response is malformed, not a TXT record, or contains any number of |
35 // strings other than 1, this returns false and extracts nothing. | 35 // strings other than 1, this returns false and extracts nothing. |
36 // Otherwise, it returns true and the extracted string is assigned to |*txt|. | 36 // Otherwise, it returns true and the extracted string is assigned to |*txt|. |
37 bool ParseTxtResponse(const net::DnsResponse& response, std::string* txt) { | 37 bool ParseTxtResponse(const net::DnsResponse& response, std::string* txt) { |
38 DCHECK(txt); | 38 DCHECK(txt); |
39 | 39 |
40 net::DnsRecordParser parser = response.Parser(); | 40 net::DnsRecordParser parser = response.Parser(); |
41 // We don't care about the creation time, since we're going to throw | 41 // We don't care about the creation time, since we're going to throw |
42 // |parsed_record| away as soon as we've extracted the payload, so provide | 42 // |parsed_record| away as soon as we've extracted the payload, so provide |
43 // the "null" time. | 43 // the "null" time. |
44 auto parsed_record = net::RecordParsed::CreateFrom(&parser, base::Time()); | 44 auto parsed_record = net::RecordParsed::CreateFrom(&parser, base::Time()); |
45 if (parsed_record == nullptr) | 45 if (!parsed_record) |
46 return false; | 46 return false; |
47 | 47 |
48 auto* txt_record = parsed_record->rdata<net::TxtRecordRdata>(); | 48 auto* txt_record = parsed_record->rdata<net::TxtRecordRdata>(); |
49 if (txt_record == nullptr) | 49 if (!txt_record) |
50 return false; | 50 return false; |
51 | 51 |
52 // The draft CT-over-DNS RFC says that there MUST be exactly one string in the | 52 // The draft CT-over-DNS RFC says that there MUST be exactly one string in the |
53 // TXT record. | 53 // TXT record. |
54 if (txt_record->texts().size() != 1) | 54 if (txt_record->texts().size() != 1) |
55 return false; | 55 return false; |
56 | 56 |
57 *txt = txt_record->texts().front(); | 57 *txt = txt_record->texts().front(); |
58 return true; | 58 return true; |
59 } | 59 } |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
96 return true; | 96 return true; |
97 } | 97 } |
98 | 98 |
99 } // namespace | 99 } // namespace |
100 | 100 |
101 // Encapsulates the state machine required to get an audit proof from a Merkle | 101 // Encapsulates the state machine required to get an audit proof from a Merkle |
102 // leaf hash. This requires a DNS request to obtain the leaf index, then a | 102 // leaf hash. This requires a DNS request to obtain the leaf index, then a |
103 // series of DNS requests to get the nodes of the proof. | 103 // series of DNS requests to get the nodes of the proof. |
104 class LogDnsClient::AuditProofQuery { | 104 class LogDnsClient::AuditProofQuery { |
105 public: | 105 public: |
106 using CompletionCallback = | |
107 base::Callback<void(int net_error, AuditProofQuery* query)>; | |
108 | |
109 // The LogDnsClient is guaranteed to outlive the AuditProofQuery, so it's safe | 106 // The LogDnsClient is guaranteed to outlive the AuditProofQuery, so it's safe |
110 // to leave ownership of |dns_client| with LogDnsClient. | 107 // to leave ownership of |dns_client| with LogDnsClient. |
111 AuditProofQuery(net::DnsClient* dns_client, | 108 AuditProofQuery(net::DnsClient* dns_client, |
112 const std::string& domain_for_log, | 109 const std::string& domain_for_log, |
113 uint64_t tree_size, | 110 uint64_t tree_size, |
114 const net::NetLogWithSource& net_log); | 111 const net::NetLogWithSource& net_log); |
115 | 112 |
116 // Begins the process of getting an audit proof for the CT log entry with a | 113 // Begins the process of getting an audit proof for the CT log entry with a |
117 // leaf hash of |leaf_hash|. The |callback| will be invoked when finished. | 114 // leaf hash of |leaf_hash|. The |callback| will be invoked when finished. |
118 void Start(base::StringPiece leaf_hash, CompletionCallback callback); | 115 net::Error Start(base::StringPiece leaf_hash, |
119 | 116 const net::CompletionCallback& callback, |
120 // Transfers the audit proof to the caller. | 117 net::ct::MerkleAuditProof* proof); |
Eran Messeri
2016/10/14 11:05:13
Is the caller expected to maintain ownership of |p
Rob Percival
2016/10/20 10:49:07
Done.
| |
121 // Only call this once the query has completed, otherwise the proof will be | |
122 // incomplete. | |
123 std::unique_ptr<net::ct::MerkleAuditProof> TakeProof(); | |
124 | 118 |
125 private: | 119 private: |
126 // Requests the leaf index of the CT log entry with |leaf_hash|. | 120 enum class State { |
127 void QueryLeafIndex(base::StringPiece leaf_hash); | 121 NONE, |
128 | 122 REQUEST_LEAF_INDEX, |
129 // Processes the response to a leaf index request. | 123 REQUEST_LEAF_INDEX_COMPLETE, |
130 // The received leaf index will be added to the proof. | 124 REQUEST_AUDIT_PROOF_NODES, |
131 void QueryLeafIndexComplete(net::DnsTransaction* transaction, | 125 REQUEST_AUDIT_PROOF_NODES_COMPLETE, |
132 int net_error, | 126 }; |
133 const net::DnsResponse* response); | 127 |
134 | 128 net::Error DoLoop(net::Error result); |
135 // Queries a CT log to retrieve part of an audit proof. The |node_index| | 129 |
136 // indicates which node of the audit proof/ should be requested. The CT log | 130 // When a DnsTransaction completes, store the response and resume the state |
137 // may return up to 7 nodes, starting from |node_index| (this is the maximum | 131 // machine. It is safe to store a pointer to |response| because |transaction| |
138 // that will fit in a DNS UDP packet). The nodes will be appended to the | 132 // is kept alive in |current_dns_transaction_|. |
139 // proof. | 133 void OnDnsTransactionComplete(net::DnsTransaction* transaction, |
140 void QueryAuditProofNodes(uint64_t node_index); | 134 int net_error, |
141 | 135 const net::DnsResponse* response); |
142 // Processes the response to an audit proof request. | 136 |
143 // This will contain some, but not necessarily all, of the audit proof nodes. | 137 // Requests the leaf index for the CT log entry with |leaf_hash_|. |
144 void QueryAuditProofNodesComplete(net::DnsTransaction* transaction, | 138 net::Error RequestLeafIndex(); |
145 int net_error, | 139 |
146 const net::DnsResponse* response); | 140 // Stores the received leaf index in |proof_->leaf_index|. |
147 | 141 // If successful, the audit proof nodes will be requested next. |
142 net::Error RequestLeafIndexComplete(net::Error result); | |
143 | |
144 // Requests the next batch of audit proof nodes from a CT log. | |
145 // The index of the first node required is determined by looking at how many | |
146 // nodes are already in |proof_->nodes|. | |
147 // The CT log may return up to 7 nodes - this is the maximum allowed by the | |
148 // CT-over-DNS draft RFC, as a TXT RDATA string can have a maximum length of | |
149 // 255 bytes and each node is 32 bytes long (a SHA-256 hash). | |
150 // | |
151 // The performance of this could be improved by sending all of the expected | |
152 // requests up front. Each response can contain a maximum of 7 audit path | |
153 // nodes, so for an audit proof of size 20, it could send 3 queries (for nodes | |
154 // 0-6, 7-13 and 14-19) immediately. Currently, it sends only the first and | |
155 // then, based on the number of nodes received, sends the next query. | |
156 // The complexity of the code would increase though, as it would need to | |
157 // detect gaps in the audit proof caused by the server not responding with the | |
158 // anticipated number of nodes. It would also undermine LogDnsClient's ability | |
159 // to rate-limit DNS requests. | |
160 net::Error RequestAuditProofNodes(); | |
161 | |
162 // Appends the received audit proof nodes to |proof_->nodes|. | |
163 // If any nodes are missing, another request will follow this one. | |
164 net::Error RequestAuditProofNodesComplete(net::Error result); | |
165 | |
166 bool StartDnsTransaction(const std::string& qname); | |
Eran Messeri
2016/10/14 11:05:13
Nit: documentation.
Rob Percival
2016/10/20 10:49:07
Done.
| |
167 | |
168 // The next state that this query will enter. | |
169 State next_state_; | |
170 // The DNS domain of the CT log that is being queried. | |
148 std::string domain_for_log_; | 171 std::string domain_for_log_; |
172 // The Merkle leaf hash of the CT log entry an audit proof is required for. | |
173 std::string leaf_hash_; | |
174 // The size of the CT log's tree, from which the proof is requested. | |
149 // TODO(robpercival): Remove |tree_size| once |proof_| has a tree_size member. | 175 // TODO(robpercival): Remove |tree_size| once |proof_| has a tree_size member. |
150 uint64_t tree_size_; | 176 uint64_t tree_size_; |
151 std::unique_ptr<net::ct::MerkleAuditProof> proof_; | 177 // The audit proof to populate. |
152 CompletionCallback callback_; | 178 net::ct::MerkleAuditProof* proof_; |
179 // The callback to invoke when the query is complete. | |
180 net::CompletionCallback callback_; | |
181 // The DnsClient to use for sending DNS requests to the CT log. | |
153 net::DnsClient* dns_client_; | 182 net::DnsClient* dns_client_; |
183 // The most recent DNS request. Null if no DNS requests have been made. | |
154 std::unique_ptr<net::DnsTransaction> current_dns_transaction_; | 184 std::unique_ptr<net::DnsTransaction> current_dns_transaction_; |
185 // The most recent DNS response. Only valid so long as the corresponding DNS | |
186 // request is stored in |current_dns_transaction_|. | |
187 const net::DnsResponse* last_dns_response_; | |
188 // The NetLog that DNS transactions will log to. | |
155 net::NetLogWithSource net_log_; | 189 net::NetLogWithSource net_log_; |
190 // Produces WeakPtrs to |this| for binding callbacks. | |
156 base::WeakPtrFactory<AuditProofQuery> weak_ptr_factory_; | 191 base::WeakPtrFactory<AuditProofQuery> weak_ptr_factory_; |
157 }; | 192 }; |
158 | 193 |
159 LogDnsClient::AuditProofQuery::AuditProofQuery( | 194 LogDnsClient::AuditProofQuery::AuditProofQuery( |
160 net::DnsClient* dns_client, | 195 net::DnsClient* dns_client, |
161 const std::string& domain_for_log, | 196 const std::string& domain_for_log, |
162 uint64_t tree_size, | 197 uint64_t tree_size, |
163 const net::NetLogWithSource& net_log) | 198 const net::NetLogWithSource& net_log) |
164 : domain_for_log_(domain_for_log), | 199 : next_state_(State::NONE), |
200 domain_for_log_(domain_for_log), | |
165 tree_size_(tree_size), | 201 tree_size_(tree_size), |
166 dns_client_(dns_client), | 202 dns_client_(dns_client), |
167 net_log_(net_log), | 203 net_log_(net_log), |
168 weak_ptr_factory_(this) { | 204 weak_ptr_factory_(this) { |
169 DCHECK(dns_client_); | 205 DCHECK(dns_client_); |
170 DCHECK(!domain_for_log_.empty()); | 206 DCHECK(!domain_for_log_.empty()); |
171 } | 207 } |
172 | 208 |
173 void LogDnsClient::AuditProofQuery::Start(base::StringPiece leaf_hash, | 209 net::Error LogDnsClient::AuditProofQuery::Start( |
174 CompletionCallback callback) { | 210 base::StringPiece leaf_hash, |
175 current_dns_transaction_.reset(); | 211 const net::CompletionCallback& callback, |
176 proof_ = base::MakeUnique<net::ct::MerkleAuditProof>(); | 212 net::ct::MerkleAuditProof* proof) { |
213 // It should not already be in progress. | |
214 DCHECK_EQ(State::NONE, next_state_); | |
215 proof_ = proof; | |
216 leaf_hash.CopyToString(&leaf_hash_); | |
Eran Messeri
2016/10/14 11:05:13
API nit: Can the API be changed to enable moving t
Rob Percival
2016/10/20 10:49:07
Done.
| |
177 callback_ = callback; | 217 callback_ = callback; |
178 QueryLeafIndex(leaf_hash); | 218 // The first step in the query is to request the leaf index corresponding to |
179 } | 219 // |leaf_hash| from the CT log. |
180 | 220 next_state_ = State::REQUEST_LEAF_INDEX; |
181 std::unique_ptr<net::ct::MerkleAuditProof> | 221 // Begin the state machine. |
182 LogDnsClient::AuditProofQuery::TakeProof() { | 222 return DoLoop(net::OK); |
183 return std::move(proof_); | 223 } |
184 } | 224 |
185 | 225 net::Error LogDnsClient::AuditProofQuery::DoLoop(net::Error result) { |
186 void LogDnsClient::AuditProofQuery::QueryLeafIndex( | 226 CHECK_NE(State::NONE, next_state_); |
187 base::StringPiece leaf_hash) { | 227 do { |
188 std::string encoded_leaf_hash = | 228 State state = next_state_; |
189 base32::Base32Encode(leaf_hash, base32::Base32EncodePolicy::OMIT_PADDING); | 229 next_state_ = State::NONE; |
190 DCHECK_EQ(encoded_leaf_hash.size(), 52u); | 230 switch (state) { |
191 | 231 case State::REQUEST_LEAF_INDEX: |
192 std::string qname = base::StringPrintf( | 232 result = RequestLeafIndex(); |
193 "%s.hash.%s.", encoded_leaf_hash.c_str(), domain_for_log_.data()); | 233 break; |
194 | 234 case State::REQUEST_LEAF_INDEX_COMPLETE: |
195 net::DnsTransactionFactory* factory = dns_client_->GetTransactionFactory(); | 235 result = RequestLeafIndexComplete(result); |
196 if (factory == nullptr) { | 236 break; |
197 base::ThreadTaskRunnerHandle::Get()->PostTask( | 237 case State::REQUEST_AUDIT_PROOF_NODES: |
198 FROM_HERE, base::Bind(callback_, net::Error::ERR_NAME_RESOLUTION_FAILED, | 238 result = RequestAuditProofNodes(); |
199 base::Unretained(this))); | 239 break; |
200 return; | 240 case State::REQUEST_AUDIT_PROOF_NODES_COMPLETE: |
201 } | 241 result = RequestAuditProofNodesComplete(result); |
202 | 242 break; |
203 net::DnsTransactionFactory::CallbackType transaction_callback = | 243 case State::NONE: |
204 base::Bind(&LogDnsClient::AuditProofQuery::QueryLeafIndexComplete, | 244 NOTREACHED(); |
205 weak_ptr_factory_.GetWeakPtr()); | 245 break; |
206 | 246 } |
207 current_dns_transaction_ = factory->CreateTransaction( | 247 } while (result != net::ERR_IO_PENDING && next_state_ != State::NONE); |
208 qname, net::dns_protocol::kTypeTXT, transaction_callback, net_log_); | 248 |
209 | 249 return result; |
210 current_dns_transaction_->Start(); | 250 } |
211 } | 251 |
212 | 252 void LogDnsClient::AuditProofQuery::OnDnsTransactionComplete( |
213 void LogDnsClient::AuditProofQuery::QueryLeafIndexComplete( | |
214 net::DnsTransaction* transaction, | 253 net::DnsTransaction* transaction, |
215 int net_error, | 254 int net_error, |
216 const net::DnsResponse* response) { | 255 const net::DnsResponse* response) { |
217 // If we've received no response but no net::error either (shouldn't | 256 DCHECK_EQ(current_dns_transaction_.get(), transaction); |
218 // happen), | 257 last_dns_response_ = response; |
219 // report the response as invalid. | 258 net::Error result = DoLoop(static_cast<net::Error>(net_error)); |
220 if (response == nullptr && net_error == net::OK) { | 259 |
221 net_error = net::ERR_INVALID_RESPONSE; | 260 // If DoLoop() indicates that I/O is pending, don't invoke the completion |
222 } | 261 // callback. OnDnsTransactionComplete() will be invoked again once the I/O |
223 | 262 // is complete, and can invoke the completion callback then if appropriate. |
224 if (net_error != net::OK) { | 263 if (result != net::ERR_IO_PENDING) { |
225 base::ThreadTaskRunnerHandle::Get()->PostTask( | 264 // The callback will delete this query (now that it has finished), so copy |
226 FROM_HERE, base::Bind(callback_, net_error, base::Unretained(this))); | 265 // |callback_| before running it so that it is not deleted along with the |
227 return; | 266 // query, mid-callback-execution (which would result in a crash). |
228 } | 267 base::ResetAndReturn(&callback_).Run(result); |
229 | 268 } |
230 if (!ParseLeafIndex(*response, &proof_->leaf_index)) { | 269 } |
231 base::ThreadTaskRunnerHandle::Get()->PostTask( | 270 |
232 FROM_HERE, base::Bind(callback_, net::ERR_DNS_MALFORMED_RESPONSE, | 271 net::Error LogDnsClient::AuditProofQuery::RequestLeafIndex() { |
233 base::Unretained(this))); | 272 std::string encoded_leaf_hash = base32::Base32Encode( |
234 return; | 273 leaf_hash_, base32::Base32EncodePolicy::OMIT_PADDING); |
274 DCHECK_EQ(encoded_leaf_hash.size(), 52u); | |
275 | |
276 std::string qname = base::StringPrintf( | |
277 "%s.hash.%s.", encoded_leaf_hash.c_str(), domain_for_log_.c_str()); | |
278 | |
279 if (!StartDnsTransaction(qname)) { | |
280 return net::ERR_NAME_RESOLUTION_FAILED; | |
281 } | |
282 | |
283 next_state_ = State::REQUEST_LEAF_INDEX_COMPLETE; | |
284 return net::ERR_IO_PENDING; | |
285 } | |
286 | |
287 // Stores the received leaf index in |proof_->leaf_index|. | |
288 // If successful, the audit proof nodes will be requested next. | |
289 net::Error LogDnsClient::AuditProofQuery::RequestLeafIndexComplete( | |
290 net::Error result) { | |
291 if (result != net::OK) { | |
292 return result; | |
293 } | |
294 | |
295 DCHECK(last_dns_response_); | |
296 if (!ParseLeafIndex(*last_dns_response_, &proof_->leaf_index)) { | |
297 return net::ERR_DNS_MALFORMED_RESPONSE; | |
235 } | 298 } |
236 | 299 |
237 // Reject leaf index if it is out-of-range. | 300 // Reject leaf index if it is out-of-range. |
238 // This indicates either: | 301 // This indicates either: |
239 // a) the wrong tree_size was provided. | 302 // a) the wrong tree_size was provided. |
240 // b) the wrong leaf hash was provided. | 303 // b) the wrong leaf hash was provided. |
241 // c) there is a bug server-side. | 304 // c) there is a bug server-side. |
242 // The first two are more likely, so return ERR_INVALID_ARGUMENT. | 305 // The first two are more likely, so return ERR_INVALID_ARGUMENT. |
243 if (proof_->leaf_index >= tree_size_) { | 306 if (proof_->leaf_index >= tree_size_) { |
244 base::ThreadTaskRunnerHandle::Get()->PostTask( | 307 return net::ERR_INVALID_ARGUMENT; |
245 FROM_HERE, base::Bind(callback_, net::ERR_INVALID_ARGUMENT, | 308 } |
246 base::Unretained(this))); | 309 |
247 return; | 310 next_state_ = State::REQUEST_AUDIT_PROOF_NODES; |
248 } | 311 return net::OK; |
249 | 312 } |
250 // QueryAuditProof for the first batch of audit proof_ nodes (i.e. starting | 313 |
251 // from 0). | 314 net::Error LogDnsClient::AuditProofQuery::RequestAuditProofNodes() { |
252 QueryAuditProofNodes(0 /* start node index */); | 315 // Test pre-conditions (should be guaranteed by DNS response validation). |
253 } | 316 if (proof_->leaf_index >= tree_size_ || |
254 | 317 proof_->nodes.size() >= |
255 void LogDnsClient::AuditProofQuery::QueryAuditProofNodes(uint64_t node_index) { | 318 net::ct::CalculateAuditPathLength(proof_->leaf_index, tree_size_)) { |
256 DCHECK_LT(proof_->leaf_index, tree_size_); | 319 return net::ERR_UNEXPECTED; |
257 DCHECK_LT(node_index, | 320 } |
258 net::ct::CalculateAuditPathLength(proof_->leaf_index, tree_size_)); | |
259 | 321 |
260 std::string qname = base::StringPrintf( | 322 std::string qname = base::StringPrintf( |
261 "%" PRIu64 ".%" PRIu64 ".%" PRIu64 ".tree.%s.", node_index, | 323 "%zu.%" PRIu64 ".%" PRIu64 ".tree.%s.", proof_->nodes.size(), |
262 proof_->leaf_index, tree_size_, domain_for_log_.data()); | 324 proof_->leaf_index, tree_size_, domain_for_log_.c_str()); |
263 | 325 |
264 net::DnsTransactionFactory* factory = dns_client_->GetTransactionFactory(); | 326 if (!StartDnsTransaction(qname)) { |
265 if (factory == nullptr) { | 327 return net::ERR_NAME_RESOLUTION_FAILED; |
266 base::ThreadTaskRunnerHandle::Get()->PostTask( | 328 } |
267 FROM_HERE, base::Bind(callback_, net::Error::ERR_NAME_RESOLUTION_FAILED, | 329 |
268 base::Unretained(this))); | 330 next_state_ = State::REQUEST_AUDIT_PROOF_NODES_COMPLETE; |
269 return; | 331 return net::ERR_IO_PENDING; |
270 } | 332 } |
271 | 333 |
272 net::DnsTransactionFactory::CallbackType transaction_callback = | 334 net::Error LogDnsClient::AuditProofQuery::RequestAuditProofNodesComplete( |
273 base::Bind(&LogDnsClient::AuditProofQuery::QueryAuditProofNodesComplete, | 335 net::Error result) { |
274 weak_ptr_factory_.GetWeakPtr()); | 336 if (result != net::OK) { |
275 | 337 return result; |
276 current_dns_transaction_ = factory->CreateTransaction( | |
277 qname, net::dns_protocol::kTypeTXT, transaction_callback, net_log_); | |
278 current_dns_transaction_->Start(); | |
279 } | |
280 | |
281 void LogDnsClient::AuditProofQuery::QueryAuditProofNodesComplete( | |
282 net::DnsTransaction* transaction, | |
283 int net_error, | |
284 const net::DnsResponse* response) { | |
285 // If we receive no response but no net::error either (shouldn't happen), | |
286 // report the response as invalid. | |
287 if (response == nullptr && net_error == net::OK) { | |
288 net_error = net::ERR_INVALID_RESPONSE; | |
289 } | |
290 | |
291 if (net_error != net::OK) { | |
292 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
293 FROM_HERE, base::Bind(callback_, net_error, base::Unretained(this))); | |
294 return; | |
295 } | 338 } |
296 | 339 |
297 const uint64_t audit_path_length = | 340 const uint64_t audit_path_length = |
298 net::ct::CalculateAuditPathLength(proof_->leaf_index, tree_size_); | 341 net::ct::CalculateAuditPathLength(proof_->leaf_index, tree_size_); |
342 | |
299 // The calculated |audit_path_length| can't ever be greater than 64, so | 343 // The calculated |audit_path_length| can't ever be greater than 64, so |
300 // deriving the amount of memory to reserve from the untrusted |leaf_index| | 344 // deriving the amount of memory to reserve from the untrusted |leaf_index| |
301 // is safe. | 345 // is safe. |
302 proof_->nodes.reserve(audit_path_length); | 346 proof_->nodes.reserve(audit_path_length); |
303 | 347 |
304 if (!ParseAuditPath(*response, proof_.get())) { | 348 DCHECK(last_dns_response_); |
305 base::ThreadTaskRunnerHandle::Get()->PostTask( | 349 if (!ParseAuditPath(*last_dns_response_, proof_)) { |
306 FROM_HERE, base::Bind(callback_, net::ERR_DNS_MALFORMED_RESPONSE, | 350 return net::ERR_DNS_MALFORMED_RESPONSE; |
307 base::Unretained(this))); | 351 } |
308 return; | 352 |
309 } | 353 // Keep requesting more proof nodes until all of them are received. |
310 | 354 if (proof_->nodes.size() < audit_path_length) { |
311 const uint64_t audit_path_nodes_received = proof_->nodes.size(); | 355 next_state_ = State::REQUEST_AUDIT_PROOF_NODES; |
312 if (audit_path_nodes_received < audit_path_length) { | 356 } |
313 QueryAuditProofNodes(audit_path_nodes_received); | 357 |
314 return; | 358 return net::OK; |
315 } | 359 } |
316 | 360 |
317 base::ThreadTaskRunnerHandle::Get()->PostTask( | 361 bool LogDnsClient::AuditProofQuery::StartDnsTransaction( |
318 FROM_HERE, base::Bind(callback_, net::OK, base::Unretained(this))); | 362 const std::string& qname) { |
363 net::DnsTransactionFactory* factory = dns_client_->GetTransactionFactory(); | |
364 if (!factory) { | |
365 return false; | |
366 } | |
367 | |
368 current_dns_transaction_ = factory->CreateTransaction( | |
369 qname, net::dns_protocol::kTypeTXT, | |
370 base::Bind(&LogDnsClient::AuditProofQuery::OnDnsTransactionComplete, | |
371 weak_ptr_factory_.GetWeakPtr()), | |
372 net_log_); | |
373 | |
374 current_dns_transaction_->Start(); | |
375 return true; | |
319 } | 376 } |
320 | 377 |
321 LogDnsClient::LogDnsClient(std::unique_ptr<net::DnsClient> dns_client, | 378 LogDnsClient::LogDnsClient(std::unique_ptr<net::DnsClient> dns_client, |
322 const net::NetLogWithSource& net_log, | 379 const net::NetLogWithSource& net_log, |
323 size_t max_concurrent_queries) | 380 size_t max_concurrent_queries) |
324 : dns_client_(std::move(dns_client)), | 381 : dns_client_(std::move(dns_client)), |
325 net_log_(net_log), | 382 net_log_(net_log), |
326 max_concurrent_queries_(max_concurrent_queries), | 383 max_concurrent_queries_(max_concurrent_queries), |
327 weak_ptr_factory_(this) { | 384 weak_ptr_factory_(this) { |
328 CHECK(dns_client_); | 385 CHECK(dns_client_); |
329 net::NetworkChangeNotifier::AddDNSObserver(this); | 386 net::NetworkChangeNotifier::AddDNSObserver(this); |
330 UpdateDnsConfig(); | 387 UpdateDnsConfig(); |
331 } | 388 } |
332 | 389 |
333 LogDnsClient::~LogDnsClient() { | 390 LogDnsClient::~LogDnsClient() { |
334 net::NetworkChangeNotifier::RemoveDNSObserver(this); | 391 net::NetworkChangeNotifier::RemoveDNSObserver(this); |
335 } | 392 } |
336 | 393 |
337 void LogDnsClient::OnDNSChanged() { | 394 void LogDnsClient::OnDNSChanged() { |
338 UpdateDnsConfig(); | 395 UpdateDnsConfig(); |
339 } | 396 } |
340 | 397 |
341 void LogDnsClient::OnInitialDNSConfigRead() { | 398 void LogDnsClient::OnInitialDNSConfigRead() { |
342 UpdateDnsConfig(); | 399 UpdateDnsConfig(); |
343 } | 400 } |
344 | 401 |
345 // The performance of this could be improved by sending all of the expected | 402 net::Error LogDnsClient::QueryAuditProof( |
346 // queries up front. Each response can contain a maximum of 7 audit path nodes, | 403 base::StringPiece domain_for_log, |
347 // so for an audit proof of size 20, it could send 3 queries (for nodes 0-6, | 404 base::StringPiece leaf_hash, |
348 // 7-13 and 14-19) immediately. Currently, it sends only the first and then, | 405 uint64_t tree_size, |
349 // based on the number of nodes received, sends the next query. The complexity | 406 net::ct::MerkleAuditProof* proof, |
350 // of the code would increase though, as it would need to detect gaps in the | 407 const net::CompletionCallback& callback) { |
351 // audit proof caused by the server not responding with the anticipated number | 408 DCHECK(proof); |
352 // of nodes. Ownership of the proof would need to change, as it would be shared | 409 |
353 // between simultaneous DNS transactions. Throttling of queries would also need | |
354 // to take into account this increase in parallelism. | |
355 void LogDnsClient::QueryAuditProof(const std::string& domain_for_log, | |
356 base::StringPiece leaf_hash, | |
357 uint64_t tree_size, | |
358 const AuditProofCallback& callback) { | |
359 if (domain_for_log.empty() || leaf_hash.size() != crypto::kSHA256Length) { | 410 if (domain_for_log.empty() || leaf_hash.size() != crypto::kSHA256Length) { |
360 base::ThreadTaskRunnerHandle::Get()->PostTask( | 411 return net::ERR_INVALID_ARGUMENT; |
361 FROM_HERE, | |
362 base::Bind(callback, net::Error::ERR_INVALID_ARGUMENT, nullptr)); | |
363 return; | |
364 } | 412 } |
365 | 413 |
366 if (HasMaxConcurrentQueriesInProgress()) { | 414 if (HasMaxConcurrentQueriesInProgress()) { |
367 base::ThreadTaskRunnerHandle::Get()->PostTask( | 415 return net::ERR_TEMPORARILY_THROTTLED; |
368 FROM_HERE, | |
369 base::Bind(callback, net::Error::ERR_TEMPORARILY_THROTTLED, nullptr)); | |
370 return; | |
371 } | 416 } |
372 | 417 |
373 audit_proof_queries_.emplace_back(new AuditProofQuery( | 418 AuditProofQuery* query = new AuditProofQuery( |
374 dns_client_.get(), domain_for_log, tree_size, net_log_)); | 419 dns_client_.get(), domain_for_log.as_string(), tree_size, net_log_); |
420 // Transfers ownership of |query| to |audit_proof_queries_|. | |
421 audit_proof_queries_.emplace_back(query); | |
375 | 422 |
376 AuditProofQuery::CompletionCallback internal_callback = | 423 return query->Start(leaf_hash, |
377 base::Bind(&LogDnsClient::QueryAuditProofComplete, | 424 base::Bind(&LogDnsClient::QueryAuditProofComplete, |
378 weak_ptr_factory_.GetWeakPtr(), callback); | 425 weak_ptr_factory_.GetWeakPtr(), |
379 | 426 base::Unretained(query), callback), |
380 audit_proof_queries_.back()->Start(leaf_hash, internal_callback); | 427 proof); |
381 } | 428 } |
382 | 429 |
383 void LogDnsClient::QueryAuditProofComplete(const AuditProofCallback& callback, | 430 void LogDnsClient::QueryAuditProofComplete( |
384 int result, | 431 AuditProofQuery* query, |
385 AuditProofQuery* query) { | 432 const net::CompletionCallback& callback, |
433 int net_error) { | |
386 DCHECK(query); | 434 DCHECK(query); |
387 | 435 |
388 std::unique_ptr<net::ct::MerkleAuditProof> proof; | |
389 if (result == net::OK) { | |
390 proof = query->TakeProof(); | |
391 } | |
392 | |
393 // Finished with the query - destroy it. | 436 // Finished with the query - destroy it. |
394 auto query_iterator = | 437 auto query_iterator = |
395 std::find_if(audit_proof_queries_.begin(), audit_proof_queries_.end(), | 438 std::find_if(audit_proof_queries_.begin(), audit_proof_queries_.end(), |
396 [query](const std::unique_ptr<AuditProofQuery>& p) { | 439 [query](const std::unique_ptr<AuditProofQuery>& p) { |
397 return p.get() == query; | 440 return p.get() == query; |
398 }); | 441 }); |
399 DCHECK(query_iterator != audit_proof_queries_.end()); | 442 DCHECK(query_iterator != audit_proof_queries_.end()); |
400 audit_proof_queries_.erase(query_iterator); | 443 audit_proof_queries_.erase(query_iterator); |
401 | 444 |
402 base::ThreadTaskRunnerHandle::Get()->PostTask( | 445 callback.Run(net_error); |
403 FROM_HERE, base::Bind(callback, result, base::Passed(&proof))); | |
404 } | 446 } |
405 | 447 |
406 bool LogDnsClient::HasMaxConcurrentQueriesInProgress() const { | 448 bool LogDnsClient::HasMaxConcurrentQueriesInProgress() const { |
407 return max_concurrent_queries_ != 0 && | 449 return max_concurrent_queries_ != 0 && |
408 audit_proof_queries_.size() >= max_concurrent_queries_; | 450 audit_proof_queries_.size() >= max_concurrent_queries_; |
409 } | 451 } |
410 | 452 |
411 void LogDnsClient::UpdateDnsConfig() { | 453 void LogDnsClient::UpdateDnsConfig() { |
412 net::DnsConfig config; | 454 net::DnsConfig config; |
413 net::NetworkChangeNotifier::GetDnsConfig(&config); | 455 net::NetworkChangeNotifier::GetDnsConfig(&config); |
414 if (config.IsValid()) | 456 if (config.IsValid()) |
415 dns_client_->SetConfig(config); | 457 dns_client_->SetConfig(config); |
416 } | 458 } |
417 | 459 |
418 } // namespace certificate_transparency | 460 } // namespace certificate_transparency |
OLD | NEW |