| 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 #ifndef COMPONENTS_CERTIFICATE_TRANSPARENCY_LOG_DNS_CLIENT_H_ | 5 #ifndef COMPONENTS_CERTIFICATE_TRANSPARENCY_LOG_DNS_CLIENT_H_ |
| 6 #define COMPONENTS_CERTIFICATE_TRANSPARENCY_LOG_DNS_CLIENT_H_ | 6 #define COMPONENTS_CERTIFICATE_TRANSPARENCY_LOG_DNS_CLIENT_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <list> | 10 #include <list> |
| 11 #include <string> | |
| 12 | 11 |
| 13 #include "base/callback.h" | 12 #include "base/callback.h" |
| 14 #include "base/macros.h" | 13 #include "base/macros.h" |
| 15 #include "base/strings/string_piece.h" | 14 #include "base/strings/string_piece.h" |
| 15 #include "net/base/completion_callback.h" |
| 16 #include "net/base/net_errors.h" |
| 16 #include "net/base/network_change_notifier.h" | 17 #include "net/base/network_change_notifier.h" |
| 17 #include "net/log/net_log_with_source.h" | 18 #include "net/log/net_log_with_source.h" |
| 18 | 19 |
| 19 namespace net { | 20 namespace net { |
| 20 class DnsClient; | 21 class DnsClient; |
| 21 class DnsResponse; | |
| 22 class DnsTransaction; | |
| 23 namespace ct { | 22 namespace ct { |
| 24 struct MerkleAuditProof; | 23 struct MerkleAuditProof; |
| 25 } // namespace ct | 24 } // namespace ct |
| 26 } // namespace net | 25 } // namespace net |
| 27 | 26 |
| 28 namespace certificate_transparency { | 27 namespace certificate_transparency { |
| 29 | 28 |
| 30 // Queries Certificate Transparency (CT) log servers via DNS. | 29 // Queries Certificate Transparency (CT) log servers via DNS. |
| 31 // All queries are performed asynchronously. | 30 // All queries are performed asynchronously. |
| 32 // For more information, see | 31 // For more information, see |
| 33 // https://github.com/google/certificate-transparency-rfcs/blob/master/dns/draft
-ct-over-dns.md. | 32 // https://github.com/google/certificate-transparency-rfcs/blob/master/dns/draft
-ct-over-dns.md. |
| 34 // It must be created and deleted on the same thread. It is not thread-safe. | 33 // It must be created and deleted on the same thread. It is not thread-safe. |
| 35 class LogDnsClient : public net::NetworkChangeNotifier::DNSObserver { | 34 class LogDnsClient : public net::NetworkChangeNotifier::DNSObserver { |
| 36 public: | 35 public: |
| 37 // Invoked when an audit proof query completes. | |
| 38 // If an error occurred, |net_error| will be a net::Error code, otherwise it | |
| 39 // will be net::OK and |proof| will be the audit proof that was received. | |
| 40 // The log ID of |proof| will not be set, as that is not known by this class, | |
| 41 // but the leaf index will be set. | |
| 42 using AuditProofCallback = | |
| 43 base::Callback<void(int net_error, | |
| 44 std::unique_ptr<net::ct::MerkleAuditProof> proof)>; | |
| 45 | |
| 46 // Creates a log client that will take ownership of |dns_client| and use it | 36 // Creates a log client that will take ownership of |dns_client| and use it |
| 47 // to perform DNS queries. Queries will be logged to |net_log|. | 37 // to perform DNS queries. Queries will be logged to |net_log|. |
| 48 // The |dns_client| does not need to be configured first - this will be done | 38 // The |dns_client| does not need to be configured first - this will be done |
| 49 // automatically as needed. | 39 // automatically as needed. |
| 50 // A limit can be set on the number of concurrent DNS queries by providing a | 40 // A limit can be set on the number of concurrent DNS queries by providing a |
| 51 // positive value for |max_concurrent_queries|. Queries that would exceed this | 41 // positive value for |max_concurrent_queries|. Queries that would exceed this |
| 52 // limit will fail with net::TEMPORARILY_THROTTLED. Setting this to 0 will | 42 // limit will fail with net::TEMPORARILY_THROTTLED. Setting this to 0 will |
| 53 // disable this limit. | 43 // disable this limit. |
| 54 LogDnsClient(std::unique_ptr<net::DnsClient> dns_client, | 44 LogDnsClient(std::unique_ptr<net::DnsClient> dns_client, |
| 55 const net::NetLogWithSource& net_log, | 45 const net::NetLogWithSource& net_log, |
| 56 size_t max_concurrent_queries); | 46 size_t max_concurrent_queries); |
| 57 // Must be deleted on the same thread that it was created on. | 47 // Must be deleted on the same thread that it was created on. |
| 58 ~LogDnsClient() override; | 48 ~LogDnsClient() override; |
| 59 | 49 |
| 60 // Called by NetworkChangeNotifier when the DNS config changes. | 50 // Called by NetworkChangeNotifier when the DNS config changes. |
| 61 // The DnsClient's config will be updated in response. | 51 // The DnsClient's config will be updated in response. |
| 62 void OnDNSChanged() override; | 52 void OnDNSChanged() override; |
| 63 | 53 |
| 64 // Called by NetworkChangeNotifier when the DNS config is first read. | 54 // Called by NetworkChangeNotifier when the DNS config is first read. |
| 65 // The DnsClient's config will be updated in response. | 55 // The DnsClient's config will be updated in response. |
| 66 void OnInitialDNSConfigRead() override; | 56 void OnInitialDNSConfigRead() override; |
| 67 | 57 |
| 68 // Queries a CT log to retrieve an audit proof for the leaf with |leaf_hash|. | 58 // Queries a CT log to retrieve an audit proof for the leaf with |leaf_hash|. |
| 69 // The |leaf_hash| is the SHA-256 Merkle leaf hash (see RFC6962, section 2.1). | |
| 70 // The size of the CT log tree must be provided in |tree_size|. | |
| 71 // The log is identified by |domain_for_log|, which is the DNS name used as a | 59 // The log is identified by |domain_for_log|, which is the DNS name used as a |
| 72 // suffix for all queries. | 60 // suffix for all queries. |
| 73 // The |callback| is invoked when the query is complete, or an error occurs. | 61 // The |leaf_hash| is the SHA-256 Merkle leaf hash (see RFC6962, section 2.1). |
| 74 void QueryAuditProof(const std::string& domain_for_log, | 62 // The size of the CT log tree, for which the proof is requested, must be |
| 75 base::StringPiece leaf_hash, | 63 // provided in |tree_size|. |
| 76 uint64_t tree_size, | 64 // The leaf index and audit proof obtained from the CT log will be placed in |
| 77 const AuditProofCallback& callback); | 65 // |proof|. |
| 66 // If the proof cannot be obtained synchronously, this method will return |
| 67 // net::ERR_IO_PENDING and invoke |callback| once the query is complete. |
| 68 // Returns: |
| 69 // - net::OK if the query was successful. |
| 70 // - net::ERR_IO_PENDING if the query was successfully started and is |
| 71 // continuing asynchronously. |
| 72 // - net::ERR_TEMPORARILY_THROTTLED if the maximum number of concurrent |
| 73 // queries are already in progress. Try again later. |
| 74 // TODO(robpercival): Provide a mechanism to notify the caller when no |
| 75 // longer throttled. |
| 76 // - net::ERR_NAME_RESOLUTION_FAILED if DNS queries are not possible. |
| 77 // Check that the DnsConfig returned by NetworkChangeNotifier is valid. |
| 78 // - net::ERR_INVALID_ARGUMENT if an argument is invalid, e.g. |leaf_hash| is |
| 79 // not a SHA-256 hash. |
| 80 net::Error QueryAuditProof(base::StringPiece domain_for_log, |
| 81 std::string leaf_hash, |
| 82 uint64_t tree_size, |
| 83 net::ct::MerkleAuditProof* proof, |
| 84 const net::CompletionCallback& callback); |
| 78 | 85 |
| 79 private: | 86 private: |
| 80 // An audit proof query that is in progress. | |
| 81 class AuditProofQuery; | 87 class AuditProofQuery; |
| 82 | 88 |
| 83 // Invoked when an audit proof query completes. | 89 // Invoked when an audit proof query completes. |
| 90 // |query| is the query that has completed. |
| 84 // |callback| is the user-provided callback that should be notified. | 91 // |callback| is the user-provided callback that should be notified. |
| 85 // |result| is a net::Error indicating success or failure. | 92 // |net_error| is a net::Error indicating success or failure. |
| 86 // |query| is the query that has completed. | 93 void QueryAuditProofComplete(AuditProofQuery* query, |
| 87 // The query is removed from |audit_proof_queries_| by this method. | 94 const net::CompletionCallback& callback, |
| 88 void QueryAuditProofComplete(const AuditProofCallback& callback, | 95 int net_error); |
| 89 int result, | |
| 90 AuditProofQuery* query); | |
| 91 | 96 |
| 92 // Returns true if the maximum number of queries are currently in flight. | 97 // Returns true if the maximum number of queries are currently in flight. |
| 93 // If the maximum number of concurrency queries is set to 0, this will always | 98 // If the maximum number of concurrency queries is set to 0, this will always |
| 94 // return false. | 99 // return false. |
| 95 bool HasMaxConcurrentQueriesInProgress() const; | 100 bool HasMaxConcurrentQueriesInProgress() const; |
| 96 | 101 |
| 97 // Updates the |dns_client_| config using NetworkChangeNotifier. | 102 // Updates the |dns_client_| config using NetworkChangeNotifier. |
| 98 void UpdateDnsConfig(); | 103 void UpdateDnsConfig(); |
| 99 | 104 |
| 100 // Used to perform DNS queries. | 105 // Used to perform DNS queries. |
| 101 std::unique_ptr<net::DnsClient> dns_client_; | 106 std::unique_ptr<net::DnsClient> dns_client_; |
| 102 // Passed to the DNS client for logging. | 107 // Passed to the DNS client for logging. |
| 103 net::NetLogWithSource net_log_; | 108 net::NetLogWithSource net_log_; |
| 104 // Audit proof queries that haven't completed yet. | 109 // A FIFO queue of ongoing queries. Since entries will always be appended to |
| 110 // the end and lookups will typically yield entries at the beginning, |
| 111 // std::list is an efficient choice. |
| 105 std::list<std::unique_ptr<AuditProofQuery>> audit_proof_queries_; | 112 std::list<std::unique_ptr<AuditProofQuery>> audit_proof_queries_; |
| 106 // The maximum number of queries that can be in flight at one time. | 113 // The maximum number of queries that can be in flight at one time. |
| 107 size_t max_concurrent_queries_; | 114 size_t max_concurrent_queries_; |
| 108 // Creates weak_ptrs to this, for callback purposes. | 115 // Creates weak_ptrs to this, for callback purposes. |
| 109 base::WeakPtrFactory<LogDnsClient> weak_ptr_factory_; | 116 base::WeakPtrFactory<LogDnsClient> weak_ptr_factory_; |
| 110 | 117 |
| 111 DISALLOW_COPY_AND_ASSIGN(LogDnsClient); | 118 DISALLOW_COPY_AND_ASSIGN(LogDnsClient); |
| 112 }; | 119 }; |
| 113 | 120 |
| 114 } // namespace certificate_transparency | 121 } // namespace certificate_transparency |
| 115 #endif // COMPONENTS_CERTIFICATE_TRANSPARENCY_LOG_DNS_CLIENT_H_ | 122 #endif // COMPONENTS_CERTIFICATE_TRANSPARENCY_LOG_DNS_CLIENT_H_ |
| OLD | NEW |