| 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 #include "components/certificate_transparency/single_tree_tracker.h" |
| 6 |
| 7 #include <utility> |
| 8 |
| 9 #include "net/cert/ct_log_verifier.h" |
| 10 #include "net/cert/signed_certificate_timestamp.h" |
| 11 #include "net/cert/x509_certificate.h" |
| 12 |
| 13 using net::ct::SignedTreeHead; |
| 14 |
| 15 namespace certificate_transparency { |
| 16 |
| 17 SingleTreeTracker::SingleTreeTracker( |
| 18 scoped_refptr<const net::CTLogVerifier> ct_log) |
| 19 : ct_log_(std::move(ct_log)) {} |
| 20 |
| 21 SingleTreeTracker::~SingleTreeTracker() {} |
| 22 |
| 23 void SingleTreeTracker::OnSCTVerified( |
| 24 net::X509Certificate* cert, |
| 25 const net::ct::SignedCertificateTimestamp* sct) { |
| 26 DCHECK_EQ(ct_log_->key_id(), sct->log_id); |
| 27 |
| 28 // SCT was previously observed, so its status should not be changed. |
| 29 if (entries_status_.find(sct->timestamp) != entries_status_.end()) |
| 30 return; |
| 31 |
| 32 // If there isn't a valid STH or the STH is not fresh enough to check |
| 33 // inclusion against, store the SCT for future checking and return. |
| 34 if (verified_sth_.timestamp.is_null() || |
| 35 (verified_sth_.timestamp < |
| 36 (sct->timestamp + base::TimeDelta::FromHours(24)))) { |
| 37 // TODO(eranm): UMA - how often SCTs have to wait for a newer STH for |
| 38 // inclusion check. |
| 39 entries_status_.insert( |
| 40 std::make_pair(sct->timestamp, SCT_PENDING_NEWER_STH)); |
| 41 return; |
| 42 } |
| 43 |
| 44 // TODO(eranm): Check inclusion here. |
| 45 // TODO(eranm): UMA - how often inclusion can be checked immediately. |
| 46 entries_status_.insert( |
| 47 std::make_pair(sct->timestamp, SCT_PENDING_INCLUSION_CHECK)); |
| 48 } |
| 49 |
| 50 void SingleTreeTracker::NewSTHObserved(const SignedTreeHead& sth) { |
| 51 DCHECK_EQ(ct_log_->key_id(), sth.log_id); |
| 52 |
| 53 if (!ct_log_->VerifySignedTreeHead(sth)) { |
| 54 // Sanity check the STH; the caller should have done this |
| 55 // already, but being paranoid here. |
| 56 // NOTE(eranm): Right now there's no way to get rid of this check here |
| 57 // as this is the first object in the chain that has an instance of |
| 58 // a CTLogVerifier to verify the STH. |
| 59 return; |
| 60 } |
| 61 |
| 62 // In order to avoid updating |verified_sth_| to an older STH in case |
| 63 // an older STH is observed, check that either the observed STH is for |
| 64 // a larger tree size or that it is for the same tree size but has |
| 65 // a newer timestamp. |
| 66 const bool sths_for_same_tree = verified_sth_.tree_size == sth.tree_size; |
| 67 const bool received_sth_is_for_larger_tree = |
| 68 (verified_sth_.tree_size > sth.tree_size); |
| 69 const bool received_sth_is_newer = (sth.timestamp > verified_sth_.timestamp); |
| 70 |
| 71 if (verified_sth_.timestamp.is_null() || received_sth_is_for_larger_tree || |
| 72 (sths_for_same_tree && received_sth_is_newer)) { |
| 73 verified_sth_ = sth; |
| 74 } |
| 75 |
| 76 // Find out which SCTs can now be checked for inclusion. |
| 77 // TODO(eranm): Keep two maps of MerkleTreeLeaf instances, one for leaves |
| 78 // pending inclusion checks and one for leaves pending a new STH. |
| 79 // The comparison function between MerkleTreeLeaf instances should use the |
| 80 // timestamp to determine sorting order, so that bulk moving from one |
| 81 // map to the other can happen. |
| 82 auto entry = entries_status_.begin(); |
| 83 while (entry != entries_status_.end() && |
| 84 entry->first < verified_sth_.timestamp) { |
| 85 entry->second = SCT_PENDING_INCLUSION_CHECK; |
| 86 ++entry; |
| 87 // TODO(eranm): Check inclusion here. |
| 88 } |
| 89 } |
| 90 |
| 91 SingleTreeTracker::SCTInclusionStatus |
| 92 SingleTreeTracker::GetLogEntryInclusionStatus( |
| 93 net::X509Certificate* cert, |
| 94 const net::ct::SignedCertificateTimestamp* sct) { |
| 95 auto it = entries_status_.find(sct->timestamp); |
| 96 |
| 97 return it == entries_status_.end() ? SCT_NOT_OBSERVED : it->second; |
| 98 } |
| 99 |
| 100 } // namespace certificate_transparency |
| OLD | NEW |