Chromium Code Reviews| 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_SINGLE_TREE_TRACKER_H_ | 5 #ifndef COMPONENTS_CERTIFICATE_TRANSPARENCY_SINGLE_TREE_TRACKER_H_ |
| 6 #define COMPONENTS_CERTIFICATE_TRANSPARENCY_SINGLE_TREE_TRACKER_H_ | 6 #define COMPONENTS_CERTIFICATE_TRANSPARENCY_SINGLE_TREE_TRACKER_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <memory> | |
| 9 #include <string> | 10 #include <string> |
| 10 | 11 |
| 12 #include "base/containers/mru_cache.h" | |
| 13 #include "base/memory/memory_pressure_monitor.h" | |
| 11 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 12 #include "base/time/time.h" | 15 #include "base/memory/weak_ptr.h" |
| 13 #include "net/cert/ct_verifier.h" | 16 #include "net/cert/ct_verifier.h" |
| 14 #include "net/cert/signed_tree_head.h" | 17 #include "net/cert/signed_tree_head.h" |
| 15 #include "net/cert/sth_observer.h" | 18 #include "net/cert/sth_observer.h" |
| 16 | 19 |
| 17 namespace net { | 20 namespace net { |
| 21 | |
| 18 class CTLogVerifier; | 22 class CTLogVerifier; |
| 19 class X509Certificate; | 23 class X509Certificate; |
| 20 | 24 |
| 21 namespace ct { | 25 namespace ct { |
| 26 | |
| 27 struct MerkleAuditProof; | |
| 22 struct SignedCertificateTimestamp; | 28 struct SignedCertificateTimestamp; |
| 29 | |
| 23 } // namespace ct | 30 } // namespace ct |
| 24 | 31 |
| 25 } // namespace net | 32 } // namespace net |
| 26 | 33 |
| 27 namespace certificate_transparency { | 34 namespace certificate_transparency { |
| 28 | 35 |
| 36 class LogDnsClient; | |
| 37 | |
| 29 // Tracks the state of an individual Certificate Transparency Log's Merkle Tree. | 38 // Tracks the state of an individual Certificate Transparency Log's Merkle Tree. |
| 30 // A CT Log constantly issues Signed Tree Heads, for which every older STH must | 39 // A CT Log constantly issues Signed Tree Heads, for which every older STH must |
| 31 // be incorporated into the current/newer STH. As new certificates are logged, | 40 // be incorporated into the current/newer STH. As new certificates are logged, |
| 32 // new SCTs are produced, and eventually, those SCTs are incorporated into the | 41 // new SCTs are produced, and eventually, those SCTs are incorporated into the |
| 33 // log and a new STH is produced, with there being an inclusion proof between | 42 // log and a new STH is produced, with there being an inclusion proof between |
| 34 // the SCTs and the new STH, and a consistency proof between the old STH and the | 43 // the SCTs and the new STH, and a consistency proof between the old STH and the |
| 35 // new STH. | 44 // new STH. |
| 36 // This class receives STHs provided by/observed by the embedder, with the | 45 // This class receives STHs provided by/observed by the embedder, with the |
| 37 // assumption that STHs have been checked for consistency already. As SCTs are | 46 // assumption that STHs have been checked for consistency already. As SCTs are |
| 38 // observed, their status is checked against the latest STH to ensure they were | 47 // observed, their status is checked against the latest STH to ensure they were |
| 39 // properly logged. If an SCT is newer than the latest STH, then this class | 48 // properly logged. If an SCT is newer than the latest STH, then this class |
| 40 // verifies that when an STH is observed that should have incorporated those | 49 // verifies that when an STH is observed that should have incorporated those |
| 41 // SCTs, the SCTs (and their corresponding entries) are present in the log. | 50 // SCTs, the SCTs (and their corresponding entries) are present in the log. |
| 42 // | 51 // |
| 43 // To accomplish this, this class needs to be notified of when new SCTs are | 52 // To accomplish this, this class needs to be notified of when new SCTs are |
| 44 // observed (which it does by implementing net::CTVerifier::Observer) and when | 53 // observed (which it does by implementing net::CTVerifier::Observer) and when |
| 45 // new STHs are observed (which it does by implementing net::ct::STHObserver). | 54 // new STHs are observed (which it does by implementing net::ct::STHObserver). |
| 46 // Once connected to sources providing that data, the status for a given SCT | 55 // Once connected to sources providing that data, the status for a given SCT |
| 47 // can be queried by calling GetLogEntryInclusionCheck. | 56 // can be queried by calling GetLogEntryInclusionCheck. |
| 48 class SingleTreeTracker : public net::CTVerifier::Observer, | 57 class SingleTreeTracker : public net::CTVerifier::Observer, |
| 49 public net::ct::STHObserver { | 58 public net::ct::STHObserver { |
| 50 public: | 59 public: |
| 51 // TODO(eranm): This enum will expand to include check success/failure, | |
| 52 // see crbug.com/506227 | |
| 53 enum SCTInclusionStatus { | 60 enum SCTInclusionStatus { |
| 54 // SCT was not observed by this class and is not currently pending | 61 // SCT was not observed by this class and is not currently pending |
| 55 // inclusion check. As there's no evidence the SCT this status relates | 62 // inclusion check. As there's no evidence the SCT this status relates |
| 56 // to is verified (it was never observed via OnSCTVerified), nothing | 63 // to is verified (it was never observed via OnSCTVerified), nothing |
| 57 // is done with it. | 64 // is done with it. |
| 58 SCT_NOT_OBSERVED, | 65 SCT_NOT_OBSERVED, |
| 59 | 66 |
| 60 // SCT was observed but the STH known to this class is not old | 67 // SCT was observed but the STH known to this class is not old |
| 61 // enough to check for inclusion, so a newer STH is needed first. | 68 // enough to check for inclusion, so a newer STH is needed first. |
| 62 SCT_PENDING_NEWER_STH, | 69 SCT_PENDING_NEWER_STH, |
| 63 | 70 |
| 64 // SCT is known and there's a new-enough STH to check inclusion against. | 71 // SCT is known and there's a new-enough STH to check inclusion against. |
| 65 // Actual inclusion check has to be performed. | 72 // It's in the process of being checked for inclusion. |
| 66 SCT_PENDING_INCLUSION_CHECK | 73 SCT_PENDING_INCLUSION_CHECK, |
| 74 | |
| 75 // Inclusion check succeeded. | |
| 76 SCT_INCLUDED_IN_LOG, | |
| 67 }; | 77 }; |
| 68 | 78 |
| 69 explicit SingleTreeTracker(scoped_refptr<const net::CTLogVerifier> ct_log); | 79 SingleTreeTracker(scoped_refptr<const net::CTLogVerifier> ct_log, |
| 80 LogDnsClient* dns_client); | |
| 70 ~SingleTreeTracker() override; | 81 ~SingleTreeTracker() override; |
| 71 | 82 |
| 72 // net::ct::CTVerifier::Observer implementation. | 83 // net::ct::CTVerifier::Observer implementation. |
| 73 | 84 |
| 74 // TODO(eranm): Extract CTVerifier::Observer to SCTObserver | 85 // TODO(eranm): Extract CTVerifier::Observer to SCTObserver |
| 75 // Performs an inclusion check for the given certificate if the latest | 86 // Performs an inclusion check for the given certificate if the latest |
| 76 // STH known for this log is older than sct.timestamp + Maximum Merge Delay, | 87 // STH known for this log is older than sct.timestamp + Maximum Merge Delay, |
| 77 // enqueues the SCT for future checking later on. | 88 // enqueues the SCT for future checking later on. |
| 78 // Should only be called with SCTs issued by the log this instance tracks. | 89 // Should only be called with SCTs issued by the log this instance tracks. |
| 79 // TODO(eranm): Make sure not to perform any synchronous, blocking operation | 90 // TODO(eranm): Make sure not to perform any synchronous, blocking operation |
| 80 // here as this callback is invoked during certificate validation. | 91 // here as this callback is invoked during certificate validation. |
| 81 void OnSCTVerified(net::X509Certificate* cert, | 92 void OnSCTVerified(net::X509Certificate* cert, |
| 82 const net::ct::SignedCertificateTimestamp* sct) override; | 93 const net::ct::SignedCertificateTimestamp* sct) override; |
| 83 | 94 |
| 84 // net::ct::STHObserver implementation. | 95 // net::ct::STHObserver implementation. |
| 85 // After verification of the signature over the |sth|, uses this | 96 // After verification of the signature over the |sth|, uses this |
| 86 // STH for future inclusion checks. | 97 // STH for future inclusion checks. |
| 87 // Must only be called for STHs issued by the log this instance tracks. | 98 // Must only be called for STHs issued by the log this instance tracks. |
| 88 void NewSTHObserved(const net::ct::SignedTreeHead& sth) override; | 99 void NewSTHObserved(const net::ct::SignedTreeHead& sth) override; |
| 89 | 100 |
| 90 // Returns the status of a given log entry that is assembled from | 101 // Returns the status of a given log entry that is assembled from |
| 91 // |cert| and |sct|. If |cert| and |sct| were not previously observed, | 102 // |cert| and |sct|. If |cert| and |sct| were not previously observed, |
| 92 // |sct| is not an SCT for |cert| or |sct| is not for this log, | 103 // |sct| is not an SCT for |cert| or |sct| is not for this log, |
| 93 // SCT_NOT_OBSERVED will be returned. | 104 // SCT_NOT_OBSERVED will be returned. |
| 94 SCTInclusionStatus GetLogEntryInclusionStatus( | 105 SCTInclusionStatus GetLogEntryInclusionStatus( |
| 95 net::X509Certificate* cert, | 106 net::X509Certificate* cert, |
| 96 const net::ct::SignedCertificateTimestamp* sct); | 107 const net::ct::SignedCertificateTimestamp* sct); |
| 97 | 108 |
| 98 private: | 109 private: |
| 110 struct EntryToAudit; | |
| 111 struct EntryAuditState; | |
| 112 struct EntryAuditResult; | |
| 113 | |
| 114 // Less-than comparator that orders entries from the oldest SCT timestamp to | |
| 115 // the newest SCT timestamp | |
| 116 struct OrderByTimestamp { | |
| 117 bool operator()(const EntryToAudit& lhs, const EntryToAudit& rhs) const; | |
| 118 }; | |
| 119 | |
| 120 // Requests an inclusion proof for each of the entries in |pending_entries_| | |
| 121 // until throttled by the LogDnsClient. | |
| 122 // NOTE: See the EntryAuditState documentation on the different states | |
| 123 // an entry can be at. | |
|
Ryan Sleevi
2016/12/22 21:33:21
Considering that EntryAuditState is in the .cc fil
Eran Messeri
2017/01/03 23:07:41
Done - removed the NOTE.
| |
| 124 void ProcessPendingEntries(); | |
| 125 | |
| 126 // Identical to the public GetLogEntryInclusionStatus, except it | |
| 127 // operates on an |entry| rather than cert, SCT combination. | |
| 128 SCTInclusionStatus GetAuditedEntryInclusionStatus(const EntryToAudit& entry); | |
| 129 | |
| 130 // Invoked by the LogDnsClient once an audit proof request was completed. | |
| 131 // Verifies the audit proof and updates the state of the entry accordingly: | |
| 132 // * If the audit proof was obtained successfully and validated, then | |
| 133 // calls to GetLogEntryInclusionStatus with this entry will indicate | |
| 134 // that the entry is included. | |
| 135 // * If there was a failure to obtain the inclusion proof or it did not | |
| 136 // validate, it is removed from the internal queue and considered to be | |
| 137 // un-audited. | |
| 138 void OnAuditProofObtained(const EntryToAudit& entry, int net_error); | |
| 139 | |
| 140 // Clear entries on low memory notifications callback. | |
| 141 void OnMemoryPressure( | |
| 142 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level); | |
| 143 | |
| 99 // Holds the latest STH fetched and verified for this log. | 144 // Holds the latest STH fetched and verified for this log. |
| 100 net::ct::SignedTreeHead verified_sth_; | 145 net::ct::SignedTreeHead verified_sth_; |
| 101 | 146 |
| 102 // The log being tracked. | 147 // The log being tracked. |
| 103 scoped_refptr<const net::CTLogVerifier> ct_log_; | 148 scoped_refptr<const net::CTLogVerifier> ct_log_; |
| 104 | 149 |
| 105 // List of log entries pending inclusion check. | 150 // Map of pending log entries to their state. |
| 106 // TODO(eranm): Rather than rely on the timestamp, extend to to use the | 151 std::map<EntryToAudit, EntryAuditState, OrderByTimestamp> pending_entries_; |
| 107 // whole MerkleTreeLeaf (RFC6962, section 3.4.) as a key. See | 152 |
| 108 // https://crbug.com/506227#c22 and https://crbug.com/613495 | 153 // A cache of leaf hashes identifying entries which were checked for |
| 109 std::map<base::Time, SCTInclusionStatus> entries_status_; | 154 // inclusion (the key is the Leaf Hash of the log entry). |
| 155 // NOTE: The current implementation does not cache failures, the | |
| 156 // EntryAuditResult | |
| 157 // struct is empty. | |
|
Ryan Sleevi
2016/12/22 21:33:21
wrapping?
Eran Messeri
2017/01/03 23:07:41
Done.
| |
| 158 // TODO(eranm): The std::string type is used as the key for the MRUCache | |
| 159 // because it is not possible to use the SHA256HashValue class directly | |
| 160 // (see https://crbug.com/665735). Once that's possible, switch to using | |
| 161 // SHA256HashValue as the key type, thus sparing copying the hash value. | |
| 162 base::MRUCache<std::string, EntryAuditResult> checked_entries_; | |
| 163 | |
| 164 // A DNS client for querying the log. | |
|
Ryan Sleevi
2016/12/22 21:33:21
This seems to be more descriptive of "what it is"
Eran Messeri
2017/01/03 23:07:41
Done - per your recommendation & the recent update
| |
| 165 LogDnsClient* dns_client_; | |
| 166 | |
| 167 std::unique_ptr<base::MemoryPressureListener> memory_pressure_listener_; | |
| 168 | |
| 169 base::WeakPtrFactory<SingleTreeTracker> weak_factory_; | |
| 110 | 170 |
| 111 DISALLOW_COPY_AND_ASSIGN(SingleTreeTracker); | 171 DISALLOW_COPY_AND_ASSIGN(SingleTreeTracker); |
| 112 }; | 172 }; |
| 113 | 173 |
| 114 } // namespace certificate_transparency | 174 } // namespace certificate_transparency |
| 115 | 175 |
| 116 #endif // COMPONENTS_CERTIFICATE_TRANSPARENCY_SINGLE_TREE_TRACKER_H_ | 176 #endif // COMPONENTS_CERTIFICATE_TRANSPARENCY_SINGLE_TREE_TRACKER_H_ |
| OLD | NEW |