| 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" |
| 16 #include "net/base/hash_value.h" |
| 13 #include "net/cert/ct_verifier.h" | 17 #include "net/cert/ct_verifier.h" |
| 14 #include "net/cert/signed_tree_head.h" | 18 #include "net/cert/signed_tree_head.h" |
| 15 #include "net/cert/sth_observer.h" | 19 #include "net/cert/sth_observer.h" |
| 16 | 20 |
| 17 namespace net { | 21 namespace net { |
| 22 |
| 18 class CTLogVerifier; | 23 class CTLogVerifier; |
| 19 class X509Certificate; | 24 class X509Certificate; |
| 20 | 25 |
| 21 namespace ct { | 26 namespace ct { |
| 27 |
| 28 struct MerkleAuditProof; |
| 22 struct SignedCertificateTimestamp; | 29 struct SignedCertificateTimestamp; |
| 30 |
| 23 } // namespace ct | 31 } // namespace ct |
| 24 | 32 |
| 25 } // namespace net | 33 } // namespace net |
| 26 | 34 |
| 27 namespace certificate_transparency { | 35 namespace certificate_transparency { |
| 28 | 36 |
| 37 class LogDnsClient; |
| 38 |
| 29 // Tracks the state of an individual Certificate Transparency Log's Merkle Tree. | 39 // 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 | 40 // 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, | 41 // 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 | 42 // 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 | 43 // 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 | 44 // the SCTs and the new STH, and a consistency proof between the old STH and the |
| 35 // new STH. | 45 // new STH. |
| 36 // This class receives STHs provided by/observed by the embedder, with the | 46 // 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 | 47 // 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 | 48 // 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 | 49 // 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 | 50 // 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. | 51 // SCTs, the SCTs (and their corresponding entries) are present in the log. |
| 42 // | 52 // |
| 43 // To accomplish this, this class needs to be notified of when new SCTs are | 53 // 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 | 54 // observed (which it does by implementing net::CTVerifier::Observer) and when |
| 45 // new STHs are observed (which it does by implementing net::ct::STHObserver). | 55 // 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 | 56 // Once connected to sources providing that data, the status for a given SCT |
| 47 // can be queried by calling GetLogEntryInclusionCheck. | 57 // can be queried by calling GetLogEntryInclusionCheck. |
| 48 class SingleTreeTracker : public net::CTVerifier::Observer, | 58 class SingleTreeTracker : public net::CTVerifier::Observer, |
| 49 public net::ct::STHObserver { | 59 public net::ct::STHObserver { |
| 50 public: | 60 public: |
| 51 // TODO(eranm): This enum will expand to include check success/failure, | |
| 52 // see crbug.com/506227 | |
| 53 enum SCTInclusionStatus { | 61 enum SCTInclusionStatus { |
| 54 // SCT was not observed by this class and is not currently pending | 62 // 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 | 63 // inclusion check. As there's no evidence the SCT this status relates |
| 56 // to is verified (it was never observed via OnSCTVerified), nothing | 64 // to is verified (it was never observed via OnSCTVerified), nothing |
| 57 // is done with it. | 65 // is done with it. |
| 58 SCT_NOT_OBSERVED, | 66 SCT_NOT_OBSERVED, |
| 59 | 67 |
| 60 // SCT was observed but the STH known to this class is not old | 68 // 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. | 69 // enough to check for inclusion, so a newer STH is needed first. |
| 62 SCT_PENDING_NEWER_STH, | 70 SCT_PENDING_NEWER_STH, |
| 63 | 71 |
| 64 // SCT is known and there's a new-enough STH to check inclusion against. | 72 // SCT is known and there's a new-enough STH to check inclusion against. |
| 65 // Actual inclusion check has to be performed. | 73 // It's in the process of being checked for inclusion. |
| 66 SCT_PENDING_INCLUSION_CHECK | 74 SCT_PENDING_INCLUSION_CHECK, |
| 75 |
| 76 // Inclusion check succeeded. |
| 77 SCT_INCLUDED_IN_LOG, |
| 67 }; | 78 }; |
| 68 | 79 |
| 69 explicit SingleTreeTracker(scoped_refptr<const net::CTLogVerifier> ct_log); | 80 SingleTreeTracker(scoped_refptr<const net::CTLogVerifier> ct_log, |
| 81 LogDnsClient* dns_client); |
| 70 ~SingleTreeTracker() override; | 82 ~SingleTreeTracker() override; |
| 71 | 83 |
| 72 // net::ct::CTVerifier::Observer implementation. | 84 // net::ct::CTVerifier::Observer implementation. |
| 73 | 85 |
| 74 // TODO(eranm): Extract CTVerifier::Observer to SCTObserver | 86 // TODO(eranm): Extract CTVerifier::Observer to SCTObserver |
| 75 // Performs an inclusion check for the given certificate if the latest | 87 // 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, | 88 // STH known for this log is older than sct.timestamp + Maximum Merge Delay, |
| 77 // enqueues the SCT for future checking later on. | 89 // enqueues the SCT for future checking later on. |
| 78 // Should only be called with SCTs issued by the log this instance tracks. | 90 // 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 | 91 // TODO(eranm): Make sure not to perform any synchronous, blocking operation |
| 80 // here as this callback is invoked during certificate validation. | 92 // here as this callback is invoked during certificate validation. |
| 81 void OnSCTVerified(net::X509Certificate* cert, | 93 void OnSCTVerified(net::X509Certificate* cert, |
| 82 const net::ct::SignedCertificateTimestamp* sct) override; | 94 const net::ct::SignedCertificateTimestamp* sct) override; |
| 83 | 95 |
| 84 // net::ct::STHObserver implementation. | 96 // net::ct::STHObserver implementation. |
| 85 // After verification of the signature over the |sth|, uses this | 97 // After verification of the signature over the |sth|, uses this |
| 86 // STH for future inclusion checks. | 98 // STH for future inclusion checks. |
| 87 // Must only be called for STHs issued by the log this instance tracks. | 99 // Must only be called for STHs issued by the log this instance tracks. |
| 88 void NewSTHObserved(const net::ct::SignedTreeHead& sth) override; | 100 void NewSTHObserved(const net::ct::SignedTreeHead& sth) override; |
| 89 | 101 |
| 90 // Returns the status of a given log entry that is assembled from | 102 // Returns the status of a given log entry that is assembled from |
| 91 // |cert| and |sct|. If |cert| and |sct| were not previously observed, | 103 // |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, | 104 // |sct| is not an SCT for |cert| or |sct| is not for this log, |
| 93 // SCT_NOT_OBSERVED will be returned. | 105 // SCT_NOT_OBSERVED will be returned. |
| 94 SCTInclusionStatus GetLogEntryInclusionStatus( | 106 SCTInclusionStatus GetLogEntryInclusionStatus( |
| 95 net::X509Certificate* cert, | 107 net::X509Certificate* cert, |
| 96 const net::ct::SignedCertificateTimestamp* sct); | 108 const net::ct::SignedCertificateTimestamp* sct); |
| 97 | 109 |
| 98 private: | 110 private: |
| 111 struct EntryToAudit; |
| 112 struct EntryAuditState; |
| 113 struct EntryAuditResult {}; |
| 114 |
| 115 // Less-than comparator that sorts EntryToAudits based on the SCT timestamp, |
| 116 // with smaller (older) SCTs appearing less than larger (newer) SCTs. |
| 117 struct OrderByTimestamp { |
| 118 bool operator()(const EntryToAudit& lhs, const EntryToAudit& rhs) const; |
| 119 }; |
| 120 |
| 121 // Requests an inclusion proof for each of the entries in |pending_entries_| |
| 122 // until throttled by the LogDnsClient. |
| 123 void ProcessPendingEntries(); |
| 124 |
| 125 // Returns the inclusion status of the given |entry|, similar to |
| 126 // GetLogEntryInclusionStatus(). The |entry| is an internal representation of |
| 127 // a certificate + SCT combination. |
| 128 SCTInclusionStatus GetAuditedEntryInclusionStatus(const EntryToAudit& entry); |
| 129 |
| 130 // Processes the result of obtaining an audit proof for |entry|. |
| 131 // * If an audit proof was successfully obtained and validated, |
| 132 // updates |checked_entries_| so that future calls to |
| 133 // GetLogEntryInclusionStatus() will indicate the entry's |
| 134 // inclusion. |
| 135 // * If there was a failure to obtain or validate an inclusion |
| 136 // proof, removes |entry| from the queue of entries to validate. |
| 137 // Future calls to GetLogEntryInclusionStatus() will indicate the entry |
| 138 // has not been observed. |
| 139 void OnAuditProofObtained(const EntryToAudit& entry, int net_error); |
| 140 |
| 141 // Clears entries to reduce memory overhead. |
| 142 void OnMemoryPressure( |
| 143 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level); |
| 144 |
| 99 // Holds the latest STH fetched and verified for this log. | 145 // Holds the latest STH fetched and verified for this log. |
| 100 net::ct::SignedTreeHead verified_sth_; | 146 net::ct::SignedTreeHead verified_sth_; |
| 101 | 147 |
| 102 // The log being tracked. | 148 // The log being tracked. |
| 103 scoped_refptr<const net::CTLogVerifier> ct_log_; | 149 scoped_refptr<const net::CTLogVerifier> ct_log_; |
| 104 | 150 |
| 105 // List of log entries pending inclusion check. | 151 // Log entries waiting to be checked for inclusion, or being checked for |
| 106 // TODO(eranm): Rather than rely on the timestamp, extend to to use the | 152 // inclusion, and their state. |
| 107 // whole MerkleTreeLeaf (RFC6962, section 3.4.) as a key. See | 153 std::map<EntryToAudit, EntryAuditState, OrderByTimestamp> pending_entries_; |
| 108 // https://crbug.com/506227#c22 and https://crbug.com/613495 | 154 |
| 109 std::map<base::Time, SCTInclusionStatus> entries_status_; | 155 // A cache of leaf hashes identifying entries which were checked for |
| 156 // inclusion (the key is the Leaf Hash of the log entry). |
| 157 // NOTE: The current implementation does not cache failures, so the presence |
| 158 // of an entry in |checked_entries_| indicates success. |
| 159 // To extend support for caching failures, a success indicator should be |
| 160 // added to the EntryAuditResult struct. |
| 161 base::MRUCache<net::SHA256HashValue, |
| 162 EntryAuditResult, |
| 163 net::SHA256HashValueLessThan> |
| 164 checked_entries_; |
| 165 |
| 166 LogDnsClient* dns_client_; |
| 167 |
| 168 std::unique_ptr<base::MemoryPressureListener> memory_pressure_listener_; |
| 169 |
| 170 base::WeakPtrFactory<SingleTreeTracker> weak_factory_; |
| 110 | 171 |
| 111 DISALLOW_COPY_AND_ASSIGN(SingleTreeTracker); | 172 DISALLOW_COPY_AND_ASSIGN(SingleTreeTracker); |
| 112 }; | 173 }; |
| 113 | 174 |
| 114 } // namespace certificate_transparency | 175 } // namespace certificate_transparency |
| 115 | 176 |
| 116 #endif // COMPONENTS_CERTIFICATE_TRANSPARENCY_SINGLE_TREE_TRACKER_H_ | 177 #endif // COMPONENTS_CERTIFICATE_TRANSPARENCY_SINGLE_TREE_TRACKER_H_ |
| OLD | NEW |