OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef COMPONENTS_CERTIFICATE_TRANSPARENCY_LOG_DNS_CLIENT_H_ | |
6 #define COMPONENTS_CERTIFICATE_TRANSPARENCY_LOG_DNS_CLIENT_H_ | |
7 | |
8 #include <stdint.h> | |
9 | |
10 #include <list> | |
11 #include <string> | |
12 | |
13 #include "base/callback.h" | |
14 #include "base/macros.h" | |
15 #include "base/strings/string_piece.h" | |
16 #include "net/log/net_log.h" | |
17 | |
18 namespace net { | |
19 class DnsClient; | |
20 class DnsResponse; | |
21 class DnsTransaction; | |
22 namespace ct { | |
23 struct MerkleAuditProof; | |
24 } // namespace ct | |
25 } // namespace net | |
26 | |
27 namespace certificate_transparency { | |
28 | |
29 // Queries Certificate Transparency (CT) log servers via DNS. | |
30 // All queries are performed asynchronously. | |
31 // For more information, see | |
32 // https://github.com/google/certificate-transparency-rfcs/blob/master/dns/draft
-ct-over-dns.md. | |
33 class LogDnsClient { | |
34 public: | |
35 // Invoked when a leaf index query completes. | |
36 // If an error occured, |net_error| will be a net::Error code, otherwise it | |
37 // will be net::OK and |leaf_index| will be the leaf index that was received. | |
38 using LeafIndexCallback = | |
39 base::Callback<void(int net_error, uint64_t leaf_index)>; | |
40 // Invoked when an audit proof query completes. | |
41 // If an error occurred, |net_error| will be a net::Error code, otherwise it | |
42 // will be net::OK and |proof| will be the audit proof that was received. | |
43 // The log ID of |proof| will not be set, as that is not known by this class, | |
44 // but the leaf index will be set. | |
45 using AuditProofCallback = | |
46 base::Callback<void(int net_error, | |
47 std::unique_ptr<net::ct::MerkleAuditProof> proof)>; | |
48 | |
49 // Creates a log client that will take ownership of |dns_client| and use it | |
50 // to perform DNS queries. Queries will be logged to |net_log|. | |
51 LogDnsClient(std::unique_ptr<net::DnsClient> dns_client, | |
52 const net::BoundNetLog& net_log); | |
53 virtual ~LogDnsClient(); | |
54 | |
55 // Queries a CT log to discover the index of the leaf with |leaf_hash|. | |
56 // The log is identified by |domain_for_log|, which is the DNS name used as a | |
57 // suffix for all queries. | |
58 // The |leaf_hash| is the SHA-256 hash of a Merkle tree leaf in that log. | |
59 // The |callback| is invoked when the query is complete, or an error occurs. | |
60 void QueryLeafIndex(base::StringPiece domain_for_log, | |
61 base::StringPiece leaf_hash, | |
62 const LeafIndexCallback& callback); | |
63 | |
64 // Queries a CT log to retrieve an audit proof for the leaf at |leaf_index|. | |
65 // The size of the CT log tree must be provided in |tree_size|. | |
66 // The log is identified by |domain_for_log|, which is the DNS name used as a | |
67 // suffix for all queries. | |
68 // The |callback| is invoked when the query is complete, or an error occurs. | |
69 void QueryAuditProof(base::StringPiece domain_for_log, | |
70 uint64_t leaf_index, | |
71 uint64_t tree_size, | |
72 const AuditProofCallback& callback); | |
73 | |
74 private: | |
75 void QueryLeafIndexComplete(net::DnsTransaction* transaction, | |
76 int neterror, | |
77 const net::DnsResponse* response); | |
78 | |
79 // Queries a CT log to retrieve part of an audit |proof|. The |node_index| | |
80 // indicates which node of the audit proof/ should be requested. The CT log | |
81 // may return up to 7 nodes, starting from |node_index| (this is the maximum | |
82 // that will fit in a DNS UDP packet). The nodes will be appended to | |
83 // |proof->nodes|. | |
84 void QueryAuditProofNodes(std::unique_ptr<net::ct::MerkleAuditProof> proof, | |
85 base::StringPiece domain_for_log, | |
86 uint64_t tree_size, | |
87 uint64_t node_index, | |
88 const AuditProofCallback& callback); | |
89 | |
90 void QueryAuditProofNodesComplete( | |
91 std::unique_ptr<net::ct::MerkleAuditProof> proof, | |
92 base::StringPiece domain_for_log, | |
93 uint64_t tree_size, | |
94 net::DnsTransaction* transaction, | |
95 int net_error, | |
96 const net::DnsResponse* response); | |
97 | |
98 // A DNS query that is in flight. | |
99 template <typename CallbackType> | |
100 struct Query { | |
101 std::unique_ptr<net::DnsTransaction> transaction; | |
102 CallbackType callback; | |
103 }; | |
104 | |
105 // Used to perform DNS queries. | |
106 std::unique_ptr<net::DnsClient> dns_client_; | |
107 // Passed to the DNS client for logging. | |
108 net::BoundNetLog net_log_; | |
109 // Leaf index queries that haven't completed yet. | |
110 std::list<Query<LeafIndexCallback>> leaf_index_queries_; | |
111 // Audit proof queries that haven't completed yet. | |
112 std::list<Query<AuditProofCallback>> audit_proof_queries_; | |
113 // Creates weak_ptrs to this, for callback purposes. | |
114 base::WeakPtrFactory<LogDnsClient> weak_ptr_factory_; | |
115 | |
116 DISALLOW_COPY_AND_ASSIGN(LogDnsClient); | |
117 }; | |
118 | |
119 } // namespace certificate_transparency | |
120 #endif // COMPONENTS_CERTIFICATE_TRANSPARENCY_LOG_DNS_CLIENT_H_ | |
OLD | NEW |