| 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 <sstream> | |
| 8 | |
| 9 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/format_macros.h" |
| 10 #include "base/location.h" | 9 #include "base/location.h" |
| 11 #include "base/logging.h" | 10 #include "base/logging.h" |
| 12 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 13 #include "base/strings/stringprintf.h" |
| 14 #include "base/threading/thread_task_runner_handle.h" | 14 #include "base/threading/thread_task_runner_handle.h" |
| 15 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 16 #include "components/base32/base32.h" | 16 #include "components/base32/base32.h" |
| 17 #include "crypto/sha2.h" | 17 #include "crypto/sha2.h" |
| 18 #include "net/base/net_errors.h" | 18 #include "net/base/net_errors.h" |
| 19 #include "net/cert/merkle_audit_proof.h" | 19 #include "net/cert/merkle_audit_proof.h" |
| 20 #include "net/dns/dns_client.h" | 20 #include "net/dns/dns_client.h" |
| 21 #include "net/dns/dns_config_service.h" | 21 #include "net/dns/dns_config_service.h" |
| 22 #include "net/dns/dns_protocol.h" | 22 #include "net/dns/dns_protocol.h" |
| 23 #include "net/dns/dns_response.h" | 23 #include "net/dns/dns_response.h" |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 for (size_t i = 0; i < audit_path.size(); i += crypto::kSHA256Length) { | 75 for (size_t i = 0; i < audit_path.size(); i += crypto::kSHA256Length) { |
| 76 proof->nodes.push_back(audit_path.substr(i, crypto::kSHA256Length)); | 76 proof->nodes.push_back(audit_path.substr(i, crypto::kSHA256Length)); |
| 77 } | 77 } |
| 78 | 78 |
| 79 return true; | 79 return true; |
| 80 } | 80 } |
| 81 | 81 |
| 82 } // namespace | 82 } // namespace |
| 83 | 83 |
| 84 LogDnsClient::LogDnsClient(std::unique_ptr<net::DnsClient> dns_client, | 84 LogDnsClient::LogDnsClient(std::unique_ptr<net::DnsClient> dns_client, |
| 85 const net::NetLogWithSource& net_log) | 85 const net::NetLogWithSource& net_log, |
| 86 size_t max_concurrent_queries) |
| 86 : dns_client_(std::move(dns_client)), | 87 : dns_client_(std::move(dns_client)), |
| 87 net_log_(net_log), | 88 net_log_(net_log), |
| 89 max_concurrent_queries_(max_concurrent_queries), |
| 88 weak_ptr_factory_(this) { | 90 weak_ptr_factory_(this) { |
| 89 CHECK(dns_client_); | 91 CHECK(dns_client_); |
| 90 net::NetworkChangeNotifier::AddDNSObserver(this); | 92 net::NetworkChangeNotifier::AddDNSObserver(this); |
| 91 UpdateDnsConfig(); | 93 UpdateDnsConfig(); |
| 92 } | 94 } |
| 93 | 95 |
| 94 LogDnsClient::~LogDnsClient() { | 96 LogDnsClient::~LogDnsClient() { |
| 95 net::NetworkChangeNotifier::RemoveDNSObserver(this); | 97 net::NetworkChangeNotifier::RemoveDNSObserver(this); |
| 96 } | 98 } |
| 97 | 99 |
| 98 void LogDnsClient::OnDNSChanged() { | 100 void LogDnsClient::OnDNSChanged() { |
| 99 UpdateDnsConfig(); | 101 UpdateDnsConfig(); |
| 100 } | 102 } |
| 101 | 103 |
| 102 void LogDnsClient::OnInitialDNSConfigRead() { | 104 void LogDnsClient::OnInitialDNSConfigRead() { |
| 103 UpdateDnsConfig(); | 105 UpdateDnsConfig(); |
| 104 } | 106 } |
| 105 | 107 |
| 106 void LogDnsClient::QueryLeafIndex(base::StringPiece domain_for_log, | 108 void LogDnsClient::QueryLeafIndex(base::StringPiece domain_for_log, |
| 107 base::StringPiece leaf_hash, | 109 base::StringPiece leaf_hash, |
| 108 const LeafIndexCallback& callback) { | 110 const LeafIndexCallback& callback) { |
| 109 if (domain_for_log.empty() || leaf_hash.size() != crypto::kSHA256Length) { | 111 if (domain_for_log.empty() || leaf_hash.size() != crypto::kSHA256Length) { |
| 110 base::ThreadTaskRunnerHandle::Get()->PostTask( | 112 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 111 FROM_HERE, base::Bind(callback, net::Error::ERR_INVALID_ARGUMENT, 0)); | 113 FROM_HERE, base::Bind(callback, net::Error::ERR_INVALID_ARGUMENT, 0)); |
| 112 return; | 114 return; |
| 113 } | 115 } |
| 114 | 116 |
| 117 if (HasMaxConcurrentQueriesInProgress()) { |
| 118 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 119 FROM_HERE, |
| 120 base::Bind(callback, net::Error::ERR_TEMPORARILY_THROTTLED, 0)); |
| 121 return; |
| 122 } |
| 123 |
| 115 std::string encoded_leaf_hash = | 124 std::string encoded_leaf_hash = |
| 116 base32::Base32Encode(leaf_hash, base32::Base32EncodePolicy::OMIT_PADDING); | 125 base32::Base32Encode(leaf_hash, base32::Base32EncodePolicy::OMIT_PADDING); |
| 117 DCHECK_EQ(encoded_leaf_hash.size(), 52u); | 126 DCHECK_EQ(encoded_leaf_hash.size(), 52u); |
| 118 | 127 |
| 119 net::DnsTransactionFactory* factory = dns_client_->GetTransactionFactory(); | 128 net::DnsTransactionFactory* factory = dns_client_->GetTransactionFactory(); |
| 120 if (factory == nullptr) { | 129 if (factory == nullptr) { |
| 121 base::ThreadTaskRunnerHandle::Get()->PostTask( | 130 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 122 FROM_HERE, | 131 FROM_HERE, |
| 123 base::Bind(callback, net::Error::ERR_NAME_RESOLUTION_FAILED, 0)); | 132 base::Bind(callback, net::Error::ERR_NAME_RESOLUTION_FAILED, 0)); |
| 124 return; | 133 return; |
| 125 } | 134 } |
| 126 | 135 |
| 127 std::ostringstream qname; | 136 std::string qname = base::StringPrintf( |
| 128 qname << encoded_leaf_hash << ".hash." << domain_for_log << "."; | 137 "%s.hash.%s.", encoded_leaf_hash.c_str(), domain_for_log.data()); |
| 129 | 138 |
| 130 net::DnsTransactionFactory::CallbackType transaction_callback = base::Bind( | 139 net::DnsTransactionFactory::CallbackType transaction_callback = base::Bind( |
| 131 &LogDnsClient::QueryLeafIndexComplete, weak_ptr_factory_.GetWeakPtr()); | 140 &LogDnsClient::QueryLeafIndexComplete, weak_ptr_factory_.GetWeakPtr()); |
| 132 | 141 |
| 133 std::unique_ptr<net::DnsTransaction> dns_transaction = | 142 std::unique_ptr<net::DnsTransaction> dns_transaction = |
| 134 factory->CreateTransaction(qname.str(), net::dns_protocol::kTypeTXT, | 143 factory->CreateTransaction(qname, net::dns_protocol::kTypeTXT, |
| 135 transaction_callback, net_log_); | 144 transaction_callback, net_log_); |
| 136 | 145 |
| 137 dns_transaction->Start(); | 146 dns_transaction->Start(); |
| 138 leaf_index_queries_.push_back({std::move(dns_transaction), callback}); | 147 leaf_index_queries_.push_back({std::move(dns_transaction), callback}); |
| 139 } | 148 } |
| 140 | 149 |
| 141 // The performance of this could be improved by sending all of the expected | 150 // The performance of this could be improved by sending all of the expected |
| 142 // queries up front. Each response can contain a maximum of 7 audit path nodes, | 151 // queries up front. Each response can contain a maximum of 7 audit path nodes, |
| 143 // so for an audit proof of size 20, it could send 3 queries (for nodes 0-6, | 152 // so for an audit proof of size 20, it could send 3 queries (for nodes 0-6, |
| 144 // 7-13 and 14-19) immediately. Currently, it sends only the first and then, | 153 // 7-13 and 14-19) immediately. Currently, it sends only the first and then, |
| 145 // based on the number of nodes received, sends the next query. The complexity | 154 // based on the number of nodes received, sends the next query. The complexity |
| 146 // of the code would increase though, as it would need to detect gaps in the | 155 // of the code would increase though, as it would need to detect gaps in the |
| 147 // audit proof caused by the server not responding with the anticipated number | 156 // audit proof caused by the server not responding with the anticipated number |
| 148 // of nodes. Ownership of the proof would need to change, as it would be shared | 157 // of nodes. Ownership of the proof would need to change, as it would be shared |
| 149 // between simultaneous DNS transactions. | 158 // between simultaneous DNS transactions. Throttling of queries would also need |
| 159 // to take into account this increase in parallelism. |
| 150 void LogDnsClient::QueryAuditProof(base::StringPiece domain_for_log, | 160 void LogDnsClient::QueryAuditProof(base::StringPiece domain_for_log, |
| 151 uint64_t leaf_index, | 161 uint64_t leaf_index, |
| 152 uint64_t tree_size, | 162 uint64_t tree_size, |
| 153 const AuditProofCallback& callback) { | 163 const AuditProofCallback& callback) { |
| 154 if (domain_for_log.empty() || leaf_index >= tree_size) { | 164 if (domain_for_log.empty() || leaf_index >= tree_size) { |
| 155 base::ThreadTaskRunnerHandle::Get()->PostTask( | 165 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 156 FROM_HERE, | 166 FROM_HERE, |
| 157 base::Bind(callback, net::Error::ERR_INVALID_ARGUMENT, nullptr)); | 167 base::Bind(callback, net::Error::ERR_INVALID_ARGUMENT, nullptr)); |
| 158 return; | 168 return; |
| 159 } | 169 } |
| 160 | 170 |
| 171 if (HasMaxConcurrentQueriesInProgress()) { |
| 172 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 173 FROM_HERE, |
| 174 base::Bind(callback, net::Error::ERR_TEMPORARILY_THROTTLED, nullptr)); |
| 175 return; |
| 176 } |
| 177 |
| 161 std::unique_ptr<net::ct::MerkleAuditProof> proof( | 178 std::unique_ptr<net::ct::MerkleAuditProof> proof( |
| 162 new net::ct::MerkleAuditProof); | 179 new net::ct::MerkleAuditProof); |
| 163 proof->leaf_index = leaf_index; | 180 proof->leaf_index = leaf_index; |
| 164 // TODO(robpercival): Once a "tree_size" field is added to MerkleAuditProof, | 181 // TODO(robpercival): Once a "tree_size" field is added to MerkleAuditProof, |
| 165 // pass |tree_size| to QueryAuditProofNodes using that. | 182 // pass |tree_size| to QueryAuditProofNodes using that. |
| 166 | 183 |
| 167 // Query for the first batch of audit proof nodes (i.e. starting from 0). | 184 // Query for the first batch of audit proof nodes (i.e. starting from 0). |
| 168 QueryAuditProofNodes(std::move(proof), domain_for_log, tree_size, 0, | 185 QueryAuditProofNodes(std::move(proof), domain_for_log, tree_size, 0, |
| 169 callback); | 186 callback); |
| 170 } | 187 } |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 222 net::ct::CalculateAuditPathLength(proof->leaf_index, tree_size)); | 239 net::ct::CalculateAuditPathLength(proof->leaf_index, tree_size)); |
| 223 | 240 |
| 224 net::DnsTransactionFactory* factory = dns_client_->GetTransactionFactory(); | 241 net::DnsTransactionFactory* factory = dns_client_->GetTransactionFactory(); |
| 225 if (factory == nullptr) { | 242 if (factory == nullptr) { |
| 226 base::ThreadTaskRunnerHandle::Get()->PostTask( | 243 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 227 FROM_HERE, | 244 FROM_HERE, |
| 228 base::Bind(callback, net::Error::ERR_NAME_RESOLUTION_FAILED, nullptr)); | 245 base::Bind(callback, net::Error::ERR_NAME_RESOLUTION_FAILED, nullptr)); |
| 229 return; | 246 return; |
| 230 } | 247 } |
| 231 | 248 |
| 232 std::ostringstream qname; | 249 std::string qname = base::StringPrintf( |
| 233 qname << node_index << "." << proof->leaf_index << "." << tree_size | 250 "%" PRIu64 ".%" PRIu64 ".%" PRIu64 ".tree.%s.", node_index, |
| 234 << ".tree." << domain_for_log << "."; | 251 proof->leaf_index, tree_size, domain_for_log.data()); |
| 235 | 252 |
| 236 net::DnsTransactionFactory::CallbackType transaction_callback = | 253 net::DnsTransactionFactory::CallbackType transaction_callback = |
| 237 base::Bind(&LogDnsClient::QueryAuditProofNodesComplete, | 254 base::Bind(&LogDnsClient::QueryAuditProofNodesComplete, |
| 238 weak_ptr_factory_.GetWeakPtr(), base::Passed(std::move(proof)), | 255 weak_ptr_factory_.GetWeakPtr(), base::Passed(std::move(proof)), |
| 239 domain_for_log, tree_size); | 256 domain_for_log, tree_size); |
| 240 | 257 |
| 241 std::unique_ptr<net::DnsTransaction> dns_transaction = | 258 std::unique_ptr<net::DnsTransaction> dns_transaction = |
| 242 factory->CreateTransaction(qname.str(), net::dns_protocol::kTypeTXT, | 259 factory->CreateTransaction(qname, net::dns_protocol::kTypeTXT, |
| 243 transaction_callback, net_log_); | 260 transaction_callback, net_log_); |
| 244 dns_transaction->Start(); | 261 dns_transaction->Start(); |
| 245 audit_proof_queries_.push_back({std::move(dns_transaction), callback}); | 262 audit_proof_queries_.push_back({std::move(dns_transaction), callback}); |
| 246 } | 263 } |
| 247 | 264 |
| 248 void LogDnsClient::QueryAuditProofNodesComplete( | 265 void LogDnsClient::QueryAuditProofNodesComplete( |
| 249 std::unique_ptr<net::ct::MerkleAuditProof> proof, | 266 std::unique_ptr<net::ct::MerkleAuditProof> proof, |
| 250 base::StringPiece domain_for_log, | 267 base::StringPiece domain_for_log, |
| 251 uint64_t tree_size, | 268 uint64_t tree_size, |
| 252 net::DnsTransaction* transaction, | 269 net::DnsTransaction* transaction, |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 QueryAuditProofNodes(std::move(proof), domain_for_log, tree_size, | 314 QueryAuditProofNodes(std::move(proof), domain_for_log, tree_size, |
| 298 audit_path_nodes_received, query.callback); | 315 audit_path_nodes_received, query.callback); |
| 299 return; | 316 return; |
| 300 } | 317 } |
| 301 | 318 |
| 302 base::ThreadTaskRunnerHandle::Get()->PostTask( | 319 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 303 FROM_HERE, | 320 FROM_HERE, |
| 304 base::Bind(query.callback, net::OK, base::Passed(std::move(proof)))); | 321 base::Bind(query.callback, net::OK, base::Passed(std::move(proof)))); |
| 305 } | 322 } |
| 306 | 323 |
| 324 bool LogDnsClient::HasMaxConcurrentQueriesInProgress() const { |
| 325 const size_t queries_in_progress = |
| 326 leaf_index_queries_.size() + audit_proof_queries_.size(); |
| 327 |
| 328 return max_concurrent_queries_ != 0 && |
| 329 queries_in_progress >= max_concurrent_queries_; |
| 330 } |
| 331 |
| 307 void LogDnsClient::UpdateDnsConfig() { | 332 void LogDnsClient::UpdateDnsConfig() { |
| 308 net::DnsConfig config; | 333 net::DnsConfig config; |
| 309 net::NetworkChangeNotifier::GetDnsConfig(&config); | 334 net::NetworkChangeNotifier::GetDnsConfig(&config); |
| 310 if (config.IsValid()) | 335 if (config.IsValid()) |
| 311 dns_client_->SetConfig(config); | 336 dns_client_->SetConfig(config); |
| 312 } | 337 } |
| 313 | 338 |
| 314 } // namespace certificate_transparency | 339 } // namespace certificate_transparency |
| OLD | NEW |