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