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