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 { |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 = | 106 using CompletionCallback = |
107 base::Callback<void(int net_error, AuditProofQuery* query)>; | 107 base::Callback<void(net::Error result, AuditProofQuery* query)>; |
Ryan Sleevi
2016/10/07 15:37:31
Same remarks on the other CL - I don't think you n
Rob Percival
2016/10/12 16:18:43
Done.
| |
108 | 108 |
109 // The LogDnsClient is guaranteed to outlive the AuditProofQuery, so it's safe | 109 // The LogDnsClient is guaranteed to outlive the AuditProofQuery, so it's safe |
110 // to leave ownership of |dns_client| with LogDnsClient. | 110 // to leave ownership of |dns_client| with LogDnsClient. |
111 AuditProofQuery(net::DnsClient* dns_client, | 111 AuditProofQuery(net::DnsClient* dns_client, |
112 const std::string& domain_for_log, | 112 const std::string& domain_for_log, |
113 uint64_t tree_size, | 113 uint64_t tree_size, |
114 const net::NetLogWithSource& net_log); | 114 const net::NetLogWithSource& net_log); |
115 | 115 |
116 // Begins the process of getting an audit proof for the CT log entry with a | 116 // 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. | 117 // leaf hash of |leaf_hash|. The |callback| will be invoked when finished. |
118 void Start(base::StringPiece leaf_hash, CompletionCallback callback); | 118 net::Error Start(base::StringPiece leaf_hash, CompletionCallback callback); |
119 | 119 |
120 // Transfers the audit proof to the caller. | 120 // Take the audit proof obtained by the query. |
121 // Only call this once the query has completed, otherwise the proof will be | 121 // Should only be called once the CompletionCallback is invoked. |
122 // incomplete. | |
123 std::unique_ptr<net::ct::MerkleAuditProof> TakeProof(); | 122 std::unique_ptr<net::ct::MerkleAuditProof> TakeProof(); |
124 | 123 |
125 private: | 124 private: |
126 // Requests the leaf index of the CT log entry with |leaf_hash|. | 125 enum class State { |
127 void QueryLeafIndex(base::StringPiece leaf_hash); | 126 NONE, |
128 | 127 REQUEST_LEAF_INDEX, |
129 // Processes the response to a leaf index request. | 128 REQUEST_LEAF_INDEX_COMPLETE, |
130 // The received leaf index will be added to the proof. | 129 REQUEST_AUDIT_PROOF_NODES, |
131 void QueryLeafIndexComplete(net::DnsTransaction* transaction, | 130 REQUEST_AUDIT_PROOF_NODES_COMPLETE, |
132 int net_error, | 131 }; |
133 const net::DnsResponse* response); | 132 |
134 | 133 net::Error DoLoop(net::Error result); |
135 // Queries a CT log to retrieve part of an audit proof. The |node_index| | 134 |
136 // indicates which node of the audit proof/ should be requested. The CT log | 135 // 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 | 136 // 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 | 137 // is kept alive in |current_dns_transaction_|. |
139 // proof. | 138 void OnDnsTransactionComplete(net::DnsTransaction* transaction, |
140 void QueryAuditProofNodes(uint64_t node_index); | 139 int net_error, |
141 | 140 const net::DnsResponse* response); |
142 // Processes the response to an audit proof request. | 141 |
143 // This will contain some, but not necessarily all, of the audit proof nodes. | 142 // Requests the leaf index for the CT log entry with |leaf_hash_|. |
144 void QueryAuditProofNodesComplete(net::DnsTransaction* transaction, | 143 net::Error RequestLeafIndex(); |
145 int net_error, | 144 |
146 const net::DnsResponse* response); | 145 // Stores the received leaf index in |proof_->leaf_index|. |
147 | 146 // If successful, the audit proof nodes will be requested next. |
147 net::Error RequestLeafIndexComplete(net::Error result); | |
148 | |
149 // Requests the next batch of audit proof nodes from a CT log. | |
150 // The index of the first node required is determined by looking at how many | |
151 // nodes are already in |proof_->nodes|. | |
152 // The CT log may return up to 7 nodes - this is the maximum allowed by the | |
153 // CT-over-DNS draft RFC, as a TXT RDATA string can have a maximum length of | |
154 // 255 bytes and each node is 32 bytes long (a SHA-256 hash). | |
155 // | |
156 // The performance of this could be improved by sending all of the expected | |
157 // requests up front. Each response can contain a maximum of 7 audit path | |
158 // nodes, so for an audit proof of size 20, it could send 3 queries (for nodes | |
159 // 0-6, 7-13 and 14-19) immediately. Currently, it sends only the first and | |
160 // then, based on the number of nodes received, sends the next query. | |
161 // The complexity of the code would increase though, as it would need to | |
162 // detect gaps in the audit proof caused by the server not responding with the | |
163 // anticipated number of nodes. It would also undermine LogDnsClient's ability | |
164 // to rate-limit DNS requests. | |
165 net::Error RequestAuditProofNodes(); | |
166 | |
167 // Appends the received audit proof nodes to |proof_->nodes|. | |
168 // If any nodes are missing, another request will follow this one. | |
169 net::Error RequestAuditProofNodesComplete(net::Error result); | |
170 | |
171 bool StartDnsTransaction(const std::string& qname); | |
172 | |
173 // The next state that this query will enter. | |
174 State next_state_; | |
175 // The DNS domain of the CT log that is being queried. | |
148 std::string domain_for_log_; | 176 std::string domain_for_log_; |
177 // The Merkle leaf hash of the CT log entry an audit proof is required for. | |
178 std::string leaf_hash_; | |
179 // 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. | 180 // TODO(robpercival): Remove |tree_size| once |proof_| has a tree_size member. |
150 uint64_t tree_size_; | 181 uint64_t tree_size_; |
182 // The audit proof. It will be null until the query is started and incomplete | |
183 // until the query is finished. | |
151 std::unique_ptr<net::ct::MerkleAuditProof> proof_; | 184 std::unique_ptr<net::ct::MerkleAuditProof> proof_; |
185 // The callback to invoke when the query is complete. | |
152 CompletionCallback callback_; | 186 CompletionCallback callback_; |
187 // The DnsClient to use for sending DNS requests to the CT log. | |
153 net::DnsClient* dns_client_; | 188 net::DnsClient* dns_client_; |
189 // The most recent DNS request. Null if no DNS requests have been made. | |
154 std::unique_ptr<net::DnsTransaction> current_dns_transaction_; | 190 std::unique_ptr<net::DnsTransaction> current_dns_transaction_; |
191 // The most recent DNS response. Only valid so long as the corresponding DNS | |
192 // request is stored in |current_dns_transaction_|. | |
193 const net::DnsResponse* last_dns_response_; | |
194 // The NetLog that DNS transactions will log to. | |
155 net::NetLogWithSource net_log_; | 195 net::NetLogWithSource net_log_; |
196 // Produces WeakPtrs to |this| for binding callbacks. | |
156 base::WeakPtrFactory<AuditProofQuery> weak_ptr_factory_; | 197 base::WeakPtrFactory<AuditProofQuery> weak_ptr_factory_; |
157 }; | 198 }; |
158 | 199 |
159 LogDnsClient::AuditProofQuery::AuditProofQuery( | 200 LogDnsClient::AuditProofQuery::AuditProofQuery( |
160 net::DnsClient* dns_client, | 201 net::DnsClient* dns_client, |
161 const std::string& domain_for_log, | 202 const std::string& domain_for_log, |
162 uint64_t tree_size, | 203 uint64_t tree_size, |
163 const net::NetLogWithSource& net_log) | 204 const net::NetLogWithSource& net_log) |
164 : domain_for_log_(domain_for_log), | 205 : next_state_(State::NONE), |
206 domain_for_log_(domain_for_log), | |
165 tree_size_(tree_size), | 207 tree_size_(tree_size), |
166 dns_client_(dns_client), | 208 dns_client_(dns_client), |
167 net_log_(net_log), | 209 net_log_(net_log), |
168 weak_ptr_factory_(this) { | 210 weak_ptr_factory_(this) { |
169 DCHECK(dns_client_); | 211 DCHECK(dns_client_); |
170 DCHECK(!domain_for_log_.empty()); | 212 DCHECK(!domain_for_log_.empty()); |
171 } | 213 } |
172 | 214 |
173 void LogDnsClient::AuditProofQuery::Start(base::StringPiece leaf_hash, | 215 net::Error LogDnsClient::AuditProofQuery::Start(base::StringPiece leaf_hash, |
174 CompletionCallback callback) { | 216 CompletionCallback callback) { |
175 current_dns_transaction_.reset(); | 217 // It should not already be in progress. |
218 DCHECK_EQ(State::NONE, next_state_); | |
176 proof_ = base::MakeUnique<net::ct::MerkleAuditProof>(); | 219 proof_ = base::MakeUnique<net::ct::MerkleAuditProof>(); |
220 leaf_hash.CopyToString(&leaf_hash_); | |
177 callback_ = callback; | 221 callback_ = callback; |
178 QueryLeafIndex(leaf_hash); | 222 // The first step in the query is to request the leaf index corresponding to |
223 // |leaf_hash| from the CT log. | |
224 next_state_ = State::REQUEST_LEAF_INDEX; | |
225 // Begin the state machine. | |
226 return DoLoop(net::OK); | |
179 } | 227 } |
180 | 228 |
181 std::unique_ptr<net::ct::MerkleAuditProof> | 229 std::unique_ptr<net::ct::MerkleAuditProof> |
182 LogDnsClient::AuditProofQuery::TakeProof() { | 230 LogDnsClient::AuditProofQuery::TakeProof() { |
183 return std::move(proof_); | 231 return std::move(proof_); |
184 } | 232 } |
185 | 233 |
186 void LogDnsClient::AuditProofQuery::QueryLeafIndex( | 234 net::Error LogDnsClient::AuditProofQuery::DoLoop(net::Error result) { |
187 base::StringPiece leaf_hash) { | 235 CHECK_NE(State::NONE, next_state_); |
188 std::string encoded_leaf_hash = | 236 do { |
189 base32::Base32Encode(leaf_hash, base32::Base32EncodePolicy::OMIT_PADDING); | 237 State state = next_state_; |
190 DCHECK_EQ(encoded_leaf_hash.size(), 52u); | 238 next_state_ = State::NONE; |
191 | 239 switch (state) { |
192 std::string qname = base::StringPrintf( | 240 case State::REQUEST_LEAF_INDEX: |
193 "%s.hash.%s.", encoded_leaf_hash.c_str(), domain_for_log_.data()); | 241 result = RequestLeafIndex(); |
194 | 242 break; |
195 net::DnsTransactionFactory* factory = dns_client_->GetTransactionFactory(); | 243 case State::REQUEST_LEAF_INDEX_COMPLETE: |
196 if (factory == nullptr) { | 244 result = RequestLeafIndexComplete(result); |
197 base::ThreadTaskRunnerHandle::Get()->PostTask( | 245 break; |
198 FROM_HERE, base::Bind(callback_, net::Error::ERR_NAME_RESOLUTION_FAILED, | 246 case State::REQUEST_AUDIT_PROOF_NODES: |
199 base::Unretained(this))); | 247 result = RequestAuditProofNodes(); |
200 return; | 248 break; |
201 } | 249 case State::REQUEST_AUDIT_PROOF_NODES_COMPLETE: |
202 | 250 result = RequestAuditProofNodesComplete(result); |
203 net::DnsTransactionFactory::CallbackType transaction_callback = | 251 break; |
204 base::Bind(&LogDnsClient::AuditProofQuery::QueryLeafIndexComplete, | 252 case State::NONE: |
205 weak_ptr_factory_.GetWeakPtr()); | 253 NOTREACHED(); |
206 | 254 break; |
207 current_dns_transaction_ = factory->CreateTransaction( | 255 } |
208 qname, net::dns_protocol::kTypeTXT, transaction_callback, net_log_); | 256 } while (result != net::ERR_IO_PENDING && next_state_ != State::NONE); |
209 | 257 |
210 current_dns_transaction_->Start(); | 258 return result; |
211 } | 259 } |
212 | 260 |
213 void LogDnsClient::AuditProofQuery::QueryLeafIndexComplete( | 261 void LogDnsClient::AuditProofQuery::OnDnsTransactionComplete( |
214 net::DnsTransaction* transaction, | 262 net::DnsTransaction* transaction, |
215 int net_error, | 263 int net_error, |
216 const net::DnsResponse* response) { | 264 const net::DnsResponse* response) { |
217 // If we've received no response but no net::error either (shouldn't | 265 DCHECK_EQ(current_dns_transaction_.get(), transaction); |
218 // happen), | 266 last_dns_response_ = response; |
219 // report the response as invalid. | 267 net::Error result = DoLoop(static_cast<net::Error>(net_error)); |
220 if (response == nullptr && net_error == net::OK) { | 268 |
221 net_error = net::ERR_INVALID_RESPONSE; | 269 // If DoLoop() indicates that I/O is pending, don't invoke the completion |
222 } | 270 // callback. OnDnsTransactionComplete() will be invoked again once the I/O |
223 | 271 // is complete, and can invoke the completion callback then if appropriate. |
224 if (net_error != net::OK) { | 272 if (result != net::ERR_IO_PENDING) { |
225 base::ThreadTaskRunnerHandle::Get()->PostTask( | 273 // The callback will delete this query (now that it has finished), so copy |
226 FROM_HERE, base::Bind(callback_, net_error, base::Unretained(this))); | 274 // |callback_| before running it so that it is not deleted along with the |
227 return; | 275 // query, mid-callback-execution (which would result in a crash). |
228 } | 276 base::ResetAndReturn(&callback_).Run(result, this); |
229 | 277 } |
230 if (!ParseLeafIndex(*response, &proof_->leaf_index)) { | 278 } |
231 base::ThreadTaskRunnerHandle::Get()->PostTask( | 279 |
232 FROM_HERE, base::Bind(callback_, net::ERR_DNS_MALFORMED_RESPONSE, | 280 net::Error LogDnsClient::AuditProofQuery::RequestLeafIndex() { |
233 base::Unretained(this))); | 281 std::string encoded_leaf_hash = base32::Base32Encode( |
234 return; | 282 leaf_hash_, base32::Base32EncodePolicy::OMIT_PADDING); |
283 DCHECK_EQ(encoded_leaf_hash.size(), 52u); | |
284 | |
285 std::string qname = base::StringPrintf( | |
286 "%s.hash.%s.", encoded_leaf_hash.c_str(), domain_for_log_.c_str()); | |
287 | |
288 if (!StartDnsTransaction(qname)) { | |
289 return net::ERR_NAME_RESOLUTION_FAILED; | |
290 } | |
291 | |
292 next_state_ = State::REQUEST_LEAF_INDEX_COMPLETE; | |
293 return net::ERR_IO_PENDING; | |
294 } | |
295 | |
296 // Stores the received leaf index in |proof_->leaf_index|. | |
297 // If successful, the audit proof nodes will be requested next. | |
298 net::Error LogDnsClient::AuditProofQuery::RequestLeafIndexComplete( | |
299 net::Error result) { | |
300 if (result != net::OK) { | |
301 return result; | |
302 } | |
303 | |
304 DCHECK(last_dns_response_); | |
305 if (!ParseLeafIndex(*last_dns_response_, &proof_->leaf_index)) { | |
306 return net::ERR_DNS_MALFORMED_RESPONSE; | |
235 } | 307 } |
236 | 308 |
237 // Reject leaf index if it is out-of-range. | 309 // Reject leaf index if it is out-of-range. |
238 // This indicates either: | 310 // This indicates either: |
239 // a) the wrong tree_size was provided. | 311 // a) the wrong tree_size was provided. |
240 // b) the wrong leaf hash was provided. | 312 // b) the wrong leaf hash was provided. |
241 // c) there is a bug server-side. | 313 // c) there is a bug server-side. |
242 // The first two are more likely, so return ERR_INVALID_ARGUMENT. | 314 // The first two are more likely, so return ERR_INVALID_ARGUMENT. |
243 if (proof_->leaf_index >= tree_size_) { | 315 if (proof_->leaf_index >= tree_size_) { |
244 base::ThreadTaskRunnerHandle::Get()->PostTask( | 316 return net::ERR_INVALID_ARGUMENT; |
245 FROM_HERE, base::Bind(callback_, net::ERR_INVALID_ARGUMENT, | 317 } |
246 base::Unretained(this))); | 318 |
247 return; | 319 next_state_ = State::REQUEST_AUDIT_PROOF_NODES; |
248 } | 320 return net::OK; |
249 | 321 } |
250 // QueryAuditProof for the first batch of audit proof_ nodes (i.e. starting | 322 |
251 // from 0). | 323 net::Error LogDnsClient::AuditProofQuery::RequestAuditProofNodes() { |
252 QueryAuditProofNodes(0 /* start node index */); | 324 // Test pre-conditions (should be guaranteed by DNS response validation). |
253 } | 325 if (proof_->leaf_index >= tree_size_ || |
254 | 326 proof_->nodes.size() >= |
255 void LogDnsClient::AuditProofQuery::QueryAuditProofNodes(uint64_t node_index) { | 327 net::ct::CalculateAuditPathLength(proof_->leaf_index, tree_size_)) { |
256 DCHECK_LT(proof_->leaf_index, tree_size_); | 328 return net::ERR_UNEXPECTED; |
257 DCHECK_LT(node_index, | 329 } |
258 net::ct::CalculateAuditPathLength(proof_->leaf_index, tree_size_)); | |
259 | 330 |
260 std::string qname = base::StringPrintf( | 331 std::string qname = base::StringPrintf( |
261 "%" PRIu64 ".%" PRIu64 ".%" PRIu64 ".tree.%s.", node_index, | 332 "%zu.%" PRIu64 ".%" PRIu64 ".tree.%s.", proof_->nodes.size(), |
262 proof_->leaf_index, tree_size_, domain_for_log_.data()); | 333 proof_->leaf_index, tree_size_, domain_for_log_.c_str()); |
263 | 334 |
264 net::DnsTransactionFactory* factory = dns_client_->GetTransactionFactory(); | 335 if (!StartDnsTransaction(qname)) { |
265 if (factory == nullptr) { | 336 return net::ERR_NAME_RESOLUTION_FAILED; |
266 base::ThreadTaskRunnerHandle::Get()->PostTask( | 337 } |
267 FROM_HERE, base::Bind(callback_, net::Error::ERR_NAME_RESOLUTION_FAILED, | 338 |
268 base::Unretained(this))); | 339 next_state_ = State::REQUEST_AUDIT_PROOF_NODES_COMPLETE; |
269 return; | 340 return net::ERR_IO_PENDING; |
270 } | 341 } |
271 | 342 |
272 net::DnsTransactionFactory::CallbackType transaction_callback = | 343 net::Error LogDnsClient::AuditProofQuery::RequestAuditProofNodesComplete( |
273 base::Bind(&LogDnsClient::AuditProofQuery::QueryAuditProofNodesComplete, | 344 net::Error result) { |
274 weak_ptr_factory_.GetWeakPtr()); | 345 if (result != net::OK) { |
275 | 346 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 } | 347 } |
296 | 348 |
297 const uint64_t audit_path_length = | 349 const uint64_t audit_path_length = |
298 net::ct::CalculateAuditPathLength(proof_->leaf_index, tree_size_); | 350 net::ct::CalculateAuditPathLength(proof_->leaf_index, tree_size_); |
351 | |
299 // The calculated |audit_path_length| can't ever be greater than 64, so | 352 // 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| | 353 // deriving the amount of memory to reserve from the untrusted |leaf_index| |
301 // is safe. | 354 // is safe. |
302 proof_->nodes.reserve(audit_path_length); | 355 proof_->nodes.reserve(audit_path_length); |
303 | 356 |
304 if (!ParseAuditPath(*response, proof_.get())) { | 357 DCHECK(last_dns_response_); |
305 base::ThreadTaskRunnerHandle::Get()->PostTask( | 358 if (!ParseAuditPath(*last_dns_response_, proof_.get())) { |
306 FROM_HERE, base::Bind(callback_, net::ERR_DNS_MALFORMED_RESPONSE, | 359 return net::ERR_DNS_MALFORMED_RESPONSE; |
307 base::Unretained(this))); | 360 } |
308 return; | 361 |
309 } | 362 // Keep requesting more proof nodes until all of them are received. |
310 | 363 if (proof_->nodes.size() < audit_path_length) { |
311 const uint64_t audit_path_nodes_received = proof_->nodes.size(); | 364 next_state_ = State::REQUEST_AUDIT_PROOF_NODES; |
312 if (audit_path_nodes_received < audit_path_length) { | 365 } |
313 QueryAuditProofNodes(audit_path_nodes_received); | 366 |
314 return; | 367 return net::OK; |
315 } | 368 } |
316 | 369 |
317 base::ThreadTaskRunnerHandle::Get()->PostTask( | 370 bool LogDnsClient::AuditProofQuery::StartDnsTransaction( |
318 FROM_HERE, base::Bind(callback_, net::OK, base::Unretained(this))); | 371 const std::string& qname) { |
372 net::DnsTransactionFactory* factory = dns_client_->GetTransactionFactory(); | |
373 if (factory == nullptr) { | |
Ryan Sleevi
2016/10/07 15:37:30
nit: if (!factory) - while mostly semantic/pedanti
Rob Percival
2016/10/12 16:18:43
Done.
| |
374 return false; | |
375 } | |
376 | |
377 current_dns_transaction_ = factory->CreateTransaction( | |
378 qname, net::dns_protocol::kTypeTXT, | |
379 base::Bind(&LogDnsClient::AuditProofQuery::OnDnsTransactionComplete, | |
380 weak_ptr_factory_.GetWeakPtr()), | |
381 net_log_); | |
382 | |
383 current_dns_transaction_->Start(); | |
384 return true; | |
319 } | 385 } |
320 | 386 |
321 LogDnsClient::LogDnsClient(std::unique_ptr<net::DnsClient> dns_client, | 387 LogDnsClient::LogDnsClient(std::unique_ptr<net::DnsClient> dns_client, |
322 const net::NetLogWithSource& net_log, | 388 const net::NetLogWithSource& net_log, |
323 size_t max_concurrent_queries) | 389 size_t max_concurrent_queries) |
324 : dns_client_(std::move(dns_client)), | 390 : dns_client_(std::move(dns_client)), |
325 net_log_(net_log), | 391 net_log_(net_log), |
326 max_concurrent_queries_(max_concurrent_queries), | 392 max_concurrent_queries_(max_concurrent_queries), |
327 weak_ptr_factory_(this) { | 393 weak_ptr_factory_(this) { |
328 CHECK(dns_client_); | 394 CHECK(dns_client_); |
329 net::NetworkChangeNotifier::AddDNSObserver(this); | 395 net::NetworkChangeNotifier::AddDNSObserver(this); |
330 UpdateDnsConfig(); | 396 UpdateDnsConfig(); |
331 } | 397 } |
332 | 398 |
333 LogDnsClient::~LogDnsClient() { | 399 LogDnsClient::~LogDnsClient() { |
334 net::NetworkChangeNotifier::RemoveDNSObserver(this); | 400 net::NetworkChangeNotifier::RemoveDNSObserver(this); |
335 } | 401 } |
336 | 402 |
337 void LogDnsClient::OnDNSChanged() { | 403 void LogDnsClient::OnDNSChanged() { |
338 UpdateDnsConfig(); | 404 UpdateDnsConfig(); |
339 } | 405 } |
340 | 406 |
341 void LogDnsClient::OnInitialDNSConfigRead() { | 407 void LogDnsClient::OnInitialDNSConfigRead() { |
342 UpdateDnsConfig(); | 408 UpdateDnsConfig(); |
343 } | 409 } |
344 | 410 |
345 // The performance of this could be improved by sending all of the expected | 411 net::Error LogDnsClient::QueryAuditProof(base::StringPiece domain_for_log, |
346 // queries up front. Each response can contain a maximum of 7 audit path nodes, | 412 base::StringPiece leaf_hash, |
347 // so for an audit proof of size 20, it could send 3 queries (for nodes 0-6, | 413 uint64_t tree_size, |
348 // 7-13 and 14-19) immediately. Currently, it sends only the first and then, | 414 const AuditProofCallback& callback) { |
349 // based on the number of nodes received, sends the next query. The complexity | |
350 // of the code would increase though, as it would need to detect gaps in the | |
351 // audit proof caused by the server not responding with the anticipated number | |
352 // of nodes. Ownership of the proof would need to change, as it would be shared | |
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) { | 415 if (domain_for_log.empty() || leaf_hash.size() != crypto::kSHA256Length) { |
360 base::ThreadTaskRunnerHandle::Get()->PostTask( | 416 return net::ERR_INVALID_ARGUMENT; |
361 FROM_HERE, | |
362 base::Bind(callback, net::Error::ERR_INVALID_ARGUMENT, nullptr)); | |
363 return; | |
364 } | 417 } |
365 | 418 |
366 if (HasMaxConcurrentQueriesInProgress()) { | 419 if (HasMaxConcurrentQueriesInProgress()) { |
367 base::ThreadTaskRunnerHandle::Get()->PostTask( | 420 return net::ERR_TEMPORARILY_THROTTLED; |
368 FROM_HERE, | |
369 base::Bind(callback, net::Error::ERR_TEMPORARILY_THROTTLED, nullptr)); | |
370 return; | |
371 } | 421 } |
372 | 422 |
373 audit_proof_queries_.emplace_back(new AuditProofQuery( | 423 AuditProofQuery* query = new AuditProofQuery( |
374 dns_client_.get(), domain_for_log, tree_size, net_log_)); | 424 dns_client_.get(), domain_for_log.as_string(), tree_size, net_log_); |
425 // Transfers ownership of |query| to |audit_proof_queries_|. | |
426 audit_proof_queries_.emplace_back(query); | |
375 | 427 |
376 AuditProofQuery::CompletionCallback internal_callback = | 428 AuditProofQuery::CompletionCallback internal_callback = |
377 base::Bind(&LogDnsClient::QueryAuditProofComplete, | 429 base::Bind(&LogDnsClient::QueryAuditProofComplete, |
378 weak_ptr_factory_.GetWeakPtr(), callback); | 430 weak_ptr_factory_.GetWeakPtr(), callback); |
379 | 431 |
380 audit_proof_queries_.back()->Start(leaf_hash, internal_callback); | 432 return query->Start(leaf_hash, internal_callback); |
Ryan Sleevi
2016/10/07 15:37:30
return query->Start(leaf_hash, base::Bind(&LogDnsC
Rob Percival
2016/10/12 16:18:43
Done.
| |
381 } | 433 } |
382 | 434 |
383 void LogDnsClient::QueryAuditProofComplete(const AuditProofCallback& callback, | 435 void LogDnsClient::QueryAuditProofComplete(const AuditProofCallback& callback, |
384 int result, | 436 net::Error result, |
385 AuditProofQuery* query) { | 437 AuditProofQuery* query) { |
386 DCHECK(query); | 438 DCHECK(query); |
387 | 439 |
388 std::unique_ptr<net::ct::MerkleAuditProof> proof; | 440 std::unique_ptr<net::ct::MerkleAuditProof> proof; |
389 if (result == net::OK) { | 441 if (result == net::OK) { |
390 proof = query->TakeProof(); | 442 proof = query->TakeProof(); |
391 } | 443 } |
392 | 444 |
393 // Finished with the query - destroy it. | 445 // Finished with the query - destroy it. |
394 auto query_iterator = | 446 auto query_iterator = |
(...skipping 14 matching lines...) Expand all Loading... | |
409 } | 461 } |
410 | 462 |
411 void LogDnsClient::UpdateDnsConfig() { | 463 void LogDnsClient::UpdateDnsConfig() { |
412 net::DnsConfig config; | 464 net::DnsConfig config; |
413 net::NetworkChangeNotifier::GetDnsConfig(&config); | 465 net::NetworkChangeNotifier::GetDnsConfig(&config); |
414 if (config.IsValid()) | 466 if (config.IsValid()) |
415 dns_client_->SetConfig(config); | 467 dns_client_->SetConfig(config); |
416 } | 468 } |
417 | 469 |
418 } // namespace certificate_transparency | 470 } // namespace certificate_transparency |
OLD | NEW |