| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2015 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 #include "components/certificate_transparency/tree_state_tracker.h" |
| 6 |
| 7 #include <sstream> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" |
| 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/thread_task_runner_handle.h" |
| 13 #include "base/time/time.h" |
| 14 #include "content/public/browser/browser_thread.h" |
| 15 #include "net/cert/ct_log_verifier.h" |
| 16 #include "net/cert/merkle_consistency_proof.h" |
| 17 #include "net/cert/signed_certificate_timestamp.h" |
| 18 #include "net/cert/signed_tree_head.h" |
| 19 #include "net/cert/x509_certificate.h" |
| 20 |
| 21 using net::X509Certificate; |
| 22 using net::CTLogVerifier; |
| 23 using net::ct::SignedCertificateTimestamp; |
| 24 using net::ct::SignedTreeHead; |
| 25 |
| 26 namespace { |
| 27 |
| 28 bool STHIsTooOld(const SignedTreeHead& sth, const base::Time& sct_timestamp) { |
| 29 return sth.timestamp < (sct_timestamp + base::TimeDelta::FromHours(24)); |
| 30 } |
| 31 |
| 32 std::string StringifySTH(const SignedTreeHead& sth) { |
| 33 std::ostringstream o; |
| 34 PrintTo(sth, &o); |
| 35 return o.str(); |
| 36 } |
| 37 |
| 38 } // namespace |
| 39 |
| 40 namespace certificate_transparency { |
| 41 |
| 42 TreeStateTracker::TreeStateTracker( |
| 43 const std::vector<scoped_refptr<const CTLogVerifier>>& ct_logs) |
| 44 : initial_load_pending_(true) { |
| 45 for (auto it = ct_logs.begin(); it != ct_logs.end(); ++it) { |
| 46 scoped_refptr<const CTLogVerifier> log(*it); |
| 47 ct_logs_[log->key_id()] = log; |
| 48 // TODO(eranm): initialize pending_scts_ |
| 49 } |
| 50 |
| 51 /* |
| 52 content::BrowserThread::PostAfterStartupTask( |
| 53 FROM_HERE, base::ThreadTaskRunnerHandle::Get(), |
| 54 base::Bind(&TreeStateTracker::LoadSTHs, base::Unretained(this))); |
| 55 */ |
| 56 } |
| 57 |
| 58 TreeStateTracker::~TreeStateTracker() {} |
| 59 |
| 60 void TreeStateTracker::OnSCTVerified(X509Certificate* cert, |
| 61 const SignedCertificateTimestamp* sct) { |
| 62 VLOG(0) << "Verified SCT observed: "; |
| 63 if (initial_load_pending_) { |
| 64 VLOG(0) << "But initial load pending. Skipping."; |
| 65 // pending_scts_[sct.log_id].insert(*sct); |
| 66 // TODO(eranm): Put in a queue. |
| 67 return; |
| 68 } |
| 69 |
| 70 const auto& it = verified_sths_.find(sct->log_id); |
| 71 std::string log_id(sct->log_id); |
| 72 std::string log_id_hex = |
| 73 base::HexEncode(sct->log_id.data(), sct->log_id.size()); |
| 74 if (it == verified_sths_.end() || STHIsTooOld(it->second, sct->timestamp)) { |
| 75 VLOG(0) << "No STH found for log " << log_id_hex << " or STH is too old."; |
| 76 // TODO(eranm): Enqueue SCT for inclusion check later. |
| 77 return; |
| 78 } |
| 79 |
| 80 VLOG(0) << "Found STH for log " << log_id_hex; |
| 81 const SignedTreeHead& sth(it->second); |
| 82 if (!STHIsTooOld(sth, sct->timestamp)) { |
| 83 VLOG(0) << "STH timestamp (" << sth.timestamp << ") is newer than SCT " |
| 84 << "timestamp (" << sct->timestamp << "), could check inclusion."; |
| 85 // TODO(eranm): Perform actual inclusion check. |
| 86 } else { |
| 87 VLOG(0) << "STH timestamp (" << sth.timestamp << ") is OLDER than SCT " |
| 88 << "timestamp (" << sct->timestamp << "), need fresh STH."; |
| 89 // TODO(eranm): Enqueue SCT for inclusion check later. |
| 90 } |
| 91 } |
| 92 |
| 93 void TreeStateTracker::NewSTHObserved(const SignedTreeHead& sth) { |
| 94 VLOG(0) << "New STH observed!" << StringifySTH(sth) |
| 95 << " Initial load pending? " << initial_load_pending_; |
| 96 initial_load_pending_ = false; |
| 97 auto it = ct_logs_.find(sth.log_id); |
| 98 if (it == ct_logs_.end()) { |
| 99 VLOG(0) << "STH is for unknown log!."; |
| 100 return; |
| 101 } |
| 102 |
| 103 const CTLogVerifier* log(it->second.get()); |
| 104 if (!log->VerifySignedTreeHead(sth)) { |
| 105 VLOG(0) << "STH is for " << log->url() << " could not be verified."; |
| 106 return; |
| 107 } |
| 108 |
| 109 VLOG(0) << "Signature for STH from " << log->url() << " for tree size " |
| 110 << sth.tree_size << " verified."; |
| 111 |
| 112 if (verified_sths_.find(sth.log_id) == verified_sths_.end()) { |
| 113 VLOG(0) << "No STH for that log, adding."; |
| 114 UpdateVerifiedSTH(sth.log_id, sth); |
| 115 } |
| 116 |
| 117 VLOG(0) << "But we already have an STH for that log." |
| 118 << " Is that one more recent?"; |
| 119 const SignedTreeHead& known_sth = verified_sths_[sth.log_id]; |
| 120 bool sths_for_same_tree = (known_sth.tree_size == sth.tree_size); |
| 121 bool received_sth_is_for_larger_tree = (known_sth.tree_size > sth.tree_size); |
| 122 bool received_sth_is_newer = (sth.timestamp > known_sth.timestamp); |
| 123 |
| 124 if (received_sth_is_for_larger_tree || |
| 125 (sths_for_same_tree && received_sth_is_newer)) { |
| 126 VLOG(0) << "Observed STH is newer; replacing."; |
| 127 UpdateVerifiedSTH(sth.log_id, sth); |
| 128 } else { |
| 129 VLOG(0) << "Known STH is same or newer, not replacing: " |
| 130 << StringifySTH(known_sth); |
| 131 } |
| 132 } |
| 133 |
| 134 /* |
| 135 void TreeStateTracker::LoadSTHs() { |
| 136 TreeStateStorage::VerifiedSthCallback callback = |
| 137 base::Bind(&TreeStateTracker::OnSTHLoaded, base::Unretained(this)); |
| 138 |
| 139 VLOG(0) << "Requesting load of all STHs."; |
| 140 content::BrowserThread::PostBlockingPoolTask( |
| 141 FROM_HERE, |
| 142 base::Bind(base::IgnoreResult(&TreeStateStorage::LoadSTHs), |
| 143 base::Unretained(storage_.get()), callback)); |
| 144 } |
| 145 */ |
| 146 |
| 147 void TreeStateTracker::OnSTHLoaded(const SignedTreeHead& sth) { |
| 148 VLOG(0) << "Loaded Signed Tree Head: " << StringifySTH(sth); |
| 149 initial_load_pending_ = false; |
| 150 verified_sths_[sth.log_id] = sth; |
| 151 } |
| 152 |
| 153 void TreeStateTracker::UpdateVerifiedSTH(const std::string& log_id, |
| 154 const SignedTreeHead& verified_sth) { |
| 155 /* |
| 156 if (verified_sths_.find(log_id) != verified_sths_.end()) { |
| 157 // Delete the old STH |
| 158 old_sth = verified_sths_[log_id]; |
| 159 content::BrowserThread::PostBlockingPoolTask( |
| 160 FROM_HERE, |
| 161 base::Bind(base::IgnoreResult(&TreeStateStorage::DeleteSth), |
| 162 base::Unretained(storage_.get()), old_sth)); |
| 163 } |
| 164 */ |
| 165 verified_sths_[log_id] = verified_sth; |
| 166 |
| 167 /* |
| 168 content::BrowserThread::PostBlockingPoolTask( |
| 169 FROM_HERE, |
| 170 base::Bind(base::IgnoreResult(&TreeStateStorage::SaveSth), |
| 171 base::Unretained(storage_.get()), verified_sth, true)); |
| 172 */ |
| 173 } |
| 174 |
| 175 } // namespace certificate_transparency |
| OLD | NEW |