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 #include "components/certificate_transparency/single_tree_tracker.h" | 5 #include "components/certificate_transparency/single_tree_tracker.h" |
| 6 | 6 |
| 7 #include <algorithm> | |
| 8 #include <iterator> | |
| 7 #include <utility> | 9 #include <utility> |
| 8 | 10 |
| 11 #include "net/base/hash_value.h" | |
| 9 #include "net/cert/ct_log_verifier.h" | 12 #include "net/cert/ct_log_verifier.h" |
| 10 #include "net/cert/signed_certificate_timestamp.h" | 13 #include "net/cert/signed_certificate_timestamp.h" |
| 11 #include "net/cert/x509_certificate.h" | 14 #include "net/cert/x509_certificate.h" |
| 12 | 15 |
| 16 using net::ct::LogEntry; | |
| 17 using net::ct::MerkleTreeLeaf; | |
| 13 using net::ct::SignedTreeHead; | 18 using net::ct::SignedTreeHead; |
| 14 | 19 |
| 15 namespace certificate_transparency { | 20 namespace certificate_transparency { |
| 21 bool OrderByTimestamp::operator()(const TimestampedLeaf& lhs, | |
| 22 const TimestampedLeaf& rhs) { | |
| 23 const MerkleTreeLeaf& lhs_leaf = lhs.GetLeaf(); | |
| 24 const MerkleTreeLeaf& rhs_leaf = rhs.GetLeaf(); | |
| 25 // This comparator should only used for containers where leaves from | |
| 26 // different logs are not mixed. | |
| 27 DCHECK(lhs_leaf.log_id == rhs_leaf.log_id); | |
| 28 | |
| 29 if (lhs_leaf.timestamp != rhs_leaf.timestamp) | |
| 30 return lhs_leaf.timestamp < rhs_leaf.timestamp; | |
| 31 | |
| 32 // Either two leaves with the same timestamp or the same leaf, have to | |
| 33 // compare the actual LogEntries to find out. | |
| 34 const LogEntry& lhs_entry = lhs_leaf.log_entry; | |
| 35 const LogEntry& rhs_entry = rhs_leaf.log_entry; | |
| 36 if (lhs_entry.type != rhs_entry.type) | |
| 37 return lhs_entry.type < rhs_entry.type; | |
| 38 | |
| 39 if (lhs_entry.type == net::ct::LogEntry::LOG_ENTRY_TYPE_X509) | |
| 40 return lhs_entry.leaf_certificate < rhs_entry.leaf_certificate; | |
| 41 | |
| 42 // lhs_entry.type == LOG_ENTRY_TYPE_PRECERT | |
| 43 return lhs_entry.tbs_certificate < rhs_entry.tbs_certificate && | |
| 44 net::SHA256HashValueLessThan().operator()(lhs_entry.issuer_key_hash, | |
| 45 rhs_entry.issuer_key_hash); | |
| 46 } | |
| 16 | 47 |
| 17 SingleTreeTracker::SingleTreeTracker( | 48 SingleTreeTracker::SingleTreeTracker( |
| 18 scoped_refptr<const net::CTLogVerifier> ct_log) | 49 scoped_refptr<const net::CTLogVerifier> ct_log) |
| 19 : ct_log_(std::move(ct_log)) {} | 50 : ct_log_(std::move(ct_log)) {} |
| 20 | 51 |
| 21 SingleTreeTracker::~SingleTreeTracker() {} | 52 SingleTreeTracker::~SingleTreeTracker() {} |
| 22 | 53 |
| 23 void SingleTreeTracker::OnSCTVerified( | 54 void SingleTreeTracker::OnSCTVerified( |
| 24 net::X509Certificate* cert, | 55 net::X509Certificate* cert, |
| 25 const net::ct::SignedCertificateTimestamp* sct) { | 56 const net::ct::SignedCertificateTimestamp* sct) { |
| 26 DCHECK_EQ(ct_log_->key_id(), sct->log_id); | 57 DCHECK_EQ(ct_log_->key_id(), sct->log_id); |
| 27 | 58 |
| 59 MerkleTreeLeaf leaf; | |
| 60 if (!GetMerkleTreeLeaf(cert, sct, &leaf)) | |
| 61 return; | |
| 62 | |
| 28 // SCT was previously observed, so its status should not be changed. | 63 // SCT was previously observed, so its status should not be changed. |
| 29 if (entries_status_.find(sct->timestamp) != entries_status_.end()) | 64 if (EntryPendingNewSTH(leaf) || EntryPendingInclusionProof(leaf)) |
| 30 return; | 65 return; |
| 31 | 66 |
| 67 TimestampedLeaf timestamped_leaf(leaf, base::Time::Now()); | |
| 68 | |
| 32 // If there isn't a valid STH or the STH is not fresh enough to check | 69 // 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. | 70 // inclusion against, store the SCT for future checking and return. |
| 34 if (verified_sth_.timestamp.is_null() || | 71 if (verified_sth_.timestamp.is_null() || |
| 35 (verified_sth_.timestamp < | 72 (verified_sth_.timestamp < |
| 36 (sct->timestamp + base::TimeDelta::FromHours(24)))) { | 73 (sct->timestamp + base::TimeDelta::FromHours(24)))) { |
|
Rob Percival
2016/06/30 14:15:57
Extract "base::TimeDelta::FromHours(24)" to a cons
Eran Messeri
2016/06/30 19:58:28
Done.
The Chrome policy is not clear about whether
| |
| 37 // TODO(eranm): UMA - how often SCTs have to wait for a newer STH for | 74 // TODO(eranm): UMA - how often SCTs have to wait for a newer STH for |
| 38 // inclusion check. | 75 // inclusion check. |
| 39 entries_status_.insert( | 76 pending_new_sth_.insert(std::move(timestamped_leaf)); |
| 40 std::make_pair(sct->timestamp, SCT_PENDING_NEWER_STH)); | |
| 41 return; | 77 return; |
| 42 } | 78 } |
| 43 | 79 |
| 44 // TODO(eranm): Check inclusion here. | 80 // TODO(eranm): Check inclusion here. |
| 45 // TODO(eranm): UMA - how often inclusion can be checked immediately. | 81 // TODO(eranm): UMA - how often inclusion can be checked immediately. |
| 46 entries_status_.insert( | 82 pending_inclusion_check_.insert(std::move(timestamped_leaf)); |
| 47 std::make_pair(sct->timestamp, SCT_PENDING_INCLUSION_CHECK)); | |
| 48 } | 83 } |
| 49 | 84 |
| 50 void SingleTreeTracker::NewSTHObserved(const SignedTreeHead& sth) { | 85 void SingleTreeTracker::NewSTHObserved(const SignedTreeHead& sth) { |
| 51 DCHECK_EQ(ct_log_->key_id(), sth.log_id); | 86 DCHECK_EQ(ct_log_->key_id(), sth.log_id); |
| 52 | 87 |
| 53 if (!ct_log_->VerifySignedTreeHead(sth)) { | 88 if (!ct_log_->VerifySignedTreeHead(sth)) { |
| 54 // Sanity check the STH; the caller should have done this | 89 // Sanity check the STH; the caller should have done this |
| 55 // already, but being paranoid here. | 90 // already, but being paranoid here. |
| 56 // NOTE(eranm): Right now there's no way to get rid of this check here | 91 // 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 | 92 // as this is the first object in the chain that has an instance of |
| 58 // a CTLogVerifier to verify the STH. | 93 // a CTLogVerifier to verify the STH. |
| 59 return; | 94 return; |
| 60 } | 95 } |
| 61 | 96 |
| 62 // In order to avoid updating |verified_sth_| to an older STH in case | 97 // 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 | 98 // 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 | 99 // a larger tree size or that it is for the same tree size but has |
| 65 // a newer timestamp. | 100 // a newer timestamp. |
| 66 const bool sths_for_same_tree = verified_sth_.tree_size == sth.tree_size; | 101 const bool sths_for_same_tree = verified_sth_.tree_size == sth.tree_size; |
| 67 const bool received_sth_is_for_larger_tree = | 102 const bool received_sth_is_for_larger_tree = |
| 68 (verified_sth_.tree_size > sth.tree_size); | 103 (verified_sth_.tree_size > sth.tree_size); |
| 69 const bool received_sth_is_newer = (sth.timestamp > verified_sth_.timestamp); | 104 const bool received_sth_is_newer = (sth.timestamp > verified_sth_.timestamp); |
| 70 | 105 |
| 71 if (verified_sth_.timestamp.is_null() || received_sth_is_for_larger_tree || | 106 if (verified_sth_.timestamp.is_null() || received_sth_is_for_larger_tree || |
| 72 (sths_for_same_tree && received_sth_is_newer)) { | 107 (sths_for_same_tree && received_sth_is_newer)) { |
| 73 verified_sth_ = sth; | 108 verified_sth_ = sth; |
| 74 } | 109 } |
| 75 | 110 |
| 111 if (pending_new_sth_.empty()) | |
| 112 return; | |
| 76 // Find out which SCTs can now be checked for inclusion. | 113 // Find out which SCTs can now be checked for inclusion. |
| 77 // TODO(eranm): Keep two maps of MerkleTreeLeaf instances, one for leaves | 114 auto sth_timestamp_compare = [&sth](const TimestampedLeaf& leaf) { |
| 78 // pending inclusion checks and one for leaves pending a new STH. | 115 return leaf.GetLeaf().timestamp > sth.timestamp; |
| 79 // The comparison function between MerkleTreeLeaf instances should use the | 116 }; |
| 80 // timestamp to determine sorting order, so that bulk moving from one | 117 auto first_entry_not_included = std::find_if( |
| 81 // map to the other can happen. | 118 pending_new_sth_.begin(), pending_new_sth_.end(), sth_timestamp_compare); |
| 82 auto entry = entries_status_.begin(); | 119 // Move all entries up to the first entry not covered by this STH to |
| 83 while (entry != entries_status_.end() && | 120 // the set of entries pending inclusion check. |
| 84 entry->first < verified_sth_.timestamp) { | 121 std::move(pending_new_sth_.begin(), first_entry_not_included, |
| 85 entry->second = SCT_PENDING_INCLUSION_CHECK; | 122 std::inserter(pending_inclusion_check_, |
| 86 ++entry; | 123 pending_inclusion_check_.begin())); |
| 87 // TODO(eranm): Check inclusion here. | 124 // Clear moved entries. |
| 88 } | 125 pending_new_sth_.erase(pending_new_sth_.begin(), first_entry_not_included); |
| 126 | |
| 127 // TODO(eranm): Check inclusion here of entries that can now be checked. | |
| 89 } | 128 } |
| 90 | 129 |
| 91 SingleTreeTracker::SCTInclusionStatus | 130 SingleTreeTracker::SCTInclusionStatus |
| 92 SingleTreeTracker::GetLogEntryInclusionStatus( | 131 SingleTreeTracker::GetLogEntryInclusionStatus( |
| 93 net::X509Certificate* cert, | 132 net::X509Certificate* cert, |
| 94 const net::ct::SignedCertificateTimestamp* sct) { | 133 const net::ct::SignedCertificateTimestamp* sct) { |
| 95 auto it = entries_status_.find(sct->timestamp); | 134 MerkleTreeLeaf leaf; |
| 135 if (!GetMerkleTreeLeaf(cert, sct, &leaf)) | |
| 136 return SCT_NOT_OBSERVED; | |
| 96 | 137 |
| 97 return it == entries_status_.end() ? SCT_NOT_OBSERVED : it->second; | 138 if (EntryPendingNewSTH(leaf)) |
| 139 return SCT_PENDING_NEWER_STH; | |
| 140 | |
| 141 if (EntryPendingInclusionProof(leaf)) | |
| 142 return SCT_PENDING_INCLUSION_CHECK; | |
| 143 | |
| 144 return SCT_NOT_OBSERVED; | |
| 145 } | |
| 146 | |
| 147 bool SingleTreeTracker::EntryPendingNewSTH( | |
| 148 const net::ct::MerkleTreeLeaf& leaf) { | |
| 149 TimestampedLeaf timestamped_leaf(leaf, base::Time::UnixEpoch()); | |
| 150 return pending_new_sth_.find(timestamped_leaf) != pending_new_sth_.end(); | |
| 151 } | |
| 152 | |
| 153 bool SingleTreeTracker::EntryPendingInclusionProof( | |
| 154 const net::ct::MerkleTreeLeaf& leaf) { | |
| 155 // Time does not matter here. | |
| 156 TimestampedLeaf timestamped_leaf(leaf, base::Time::UnixEpoch()); | |
| 157 return pending_inclusion_check_.find(timestamped_leaf) != | |
| 158 pending_inclusion_check_.end(); | |
| 98 } | 159 } |
| 99 | 160 |
| 100 } // namespace certificate_transparency | 161 } // namespace certificate_transparency |
| OLD | NEW |