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