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 |
| 9 #include "net/cert/ct_log_verifier.h" | 11 #include "net/cert/ct_log_verifier.h" |
| 12 #include "net/cert/merkle_tree_leaf.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; | |
| 18 using net::ct::SignedCertificateTimestamp; | |
| 13 using net::ct::SignedTreeHead; | 19 using net::ct::SignedTreeHead; |
| 14 | 20 |
| 21 namespace { | |
| 22 | |
| 23 // TODO(eranm): HACK HACK HACK! | |
| 24 // GetMerkleTreeLeaf has the logic for constructing the LogEntry so I'm | |
| 25 // reusing it here, but the right approach is to have a GetLogEntry | |
| 26 // function declared in signed_certificate_timestamp.h which will | |
| 27 // fill in a LogEntry given the cert and the SCT origin. | |
| 28 bool GetLogEntry(const net::X509Certificate* cert, | |
| 29 const SignedCertificateTimestamp* sct, | |
| 30 LogEntry* entry) { | |
| 31 MerkleTreeLeaf leaf; | |
| 32 if (!GetMerkleTreeLeaf(cert, sct, &leaf)) | |
| 33 return false; | |
| 34 | |
| 35 *entry = leaf.log_entry; | |
| 36 return true; | |
| 37 } | |
| 38 | |
| 39 // TODO(eranm): HACK HACK HACK! | |
| 40 // Need to change the CTVerifier interface to pass the scoped_refptr | |
| 41 // to the SignedCertificateTimestamp so we can avoid copying it. | |
|
Ryan Sleevi
2016/07/18 23:38:01
I don't understand why this hack is necessary?
Be
Eran Messeri
2016/09/21 21:10:43
This is now obsolete.
| |
| 42 void ManuallyCopySignedCertificateTimestamp( | |
| 43 const SignedCertificateTimestamp* sct, | |
| 44 scoped_refptr<SignedCertificateTimestamp> out) { | |
| 45 out->version = sct->version; | |
| 46 out->log_id = sct->log_id; | |
| 47 out->timestamp = sct->timestamp; | |
| 48 out->extensions = sct->extensions; | |
| 49 out->signature = sct->signature; | |
| 50 out->origin = sct->origin; | |
| 51 out->log_description = sct->log_description; | |
| 52 } | |
| 53 } | |
| 54 | |
| 15 namespace certificate_transparency { | 55 namespace certificate_transparency { |
| 16 | 56 |
| 57 struct SingleTreeTracker::EntryToAudit { | |
|
Ryan Sleevi
2016/07/18 23:38:01
Documentation :)
Eran Messeri
2016/09/21 21:10:43
Done.
| |
| 58 scoped_refptr<SignedCertificateTimestamp> sct; | |
| 59 LogEntry log_entry; | |
| 60 }; | |
| 61 | |
| 17 SingleTreeTracker::SingleTreeTracker( | 62 SingleTreeTracker::SingleTreeTracker( |
| 18 scoped_refptr<const net::CTLogVerifier> ct_log) | 63 scoped_refptr<const net::CTLogVerifier> ct_log) |
| 19 : ct_log_(std::move(ct_log)) {} | 64 : ct_log_(std::move(ct_log)) {} |
| 20 | 65 |
| 21 SingleTreeTracker::~SingleTreeTracker() {} | 66 SingleTreeTracker::~SingleTreeTracker() {} |
| 22 | 67 |
| 23 void SingleTreeTracker::OnSCTVerified( | 68 void SingleTreeTracker::OnSCTVerified( |
| 24 net::X509Certificate* cert, | 69 net::X509Certificate* cert, |
| 25 const net::ct::SignedCertificateTimestamp* sct) { | 70 const net::ct::SignedCertificateTimestamp* sct) { |
| 26 DCHECK_EQ(ct_log_->key_id(), sct->log_id); | 71 DCHECK_EQ(ct_log_->key_id(), sct->log_id); |
| 27 | 72 |
| 73 EntryToAudit entry; | |
| 74 if (!GetLogEntry(cert, sct, &entry.log_entry)) | |
| 75 return; | |
| 76 entry.sct = new SignedCertificateTimestamp(); | |
| 77 ManuallyCopySignedCertificateTimestamp(sct, entry.sct); | |
| 78 | |
| 28 // SCT was previously observed, so its status should not be changed. | 79 // SCT was previously observed, so its status should not be changed. |
| 29 if (entries_status_.find(sct->timestamp) != entries_status_.end()) | 80 if (EntryAlreadyEncountered(entry)) |
| 30 return; | 81 return; |
| 31 | 82 |
| 83 SCTInclusionStatus state = SCT_NOT_OBSERVED; | |
| 84 const base::TimeDelta kMaximumMergeDelay = base::TimeDelta::FromHours(24); | |
|
Ryan Sleevi
2016/07/18 23:38:01
you can make this static const (TimeDelta is const
Eran Messeri
2016/09/21 21:10:43
Done.
| |
| 32 // If there isn't a valid STH or the STH is not fresh enough to check | 85 // 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. | 86 // inclusion against, store the SCT for future checking and return. |
| 34 if (verified_sth_.timestamp.is_null() || | 87 if (verified_sth_.timestamp.is_null() || |
| 35 (verified_sth_.timestamp < | 88 (verified_sth_.timestamp < (sct->timestamp + kMaximumMergeDelay))) { |
| 36 (sct->timestamp + base::TimeDelta::FromHours(24)))) { | |
| 37 // TODO(eranm): UMA - how often SCTs have to wait for a newer STH for | 89 // TODO(eranm): UMA - how often SCTs have to wait for a newer STH for |
| 38 // inclusion check. | 90 // inclusion check. |
| 39 entries_status_.insert( | 91 state = SCT_PENDING_NEWER_STH; |
| 40 std::make_pair(sct->timestamp, SCT_PENDING_NEWER_STH)); | 92 } else { |
| 41 return; | 93 // TODO(eranm): UMA - how often inclusion can be checked immediately. |
|
Ryan Sleevi
2016/07/18 23:38:01
Why? Is this just a proxy metric for something els
Eran Messeri
2016/09/21 21:10:43
Obsolete - I've simply added the UMA logging.
This
| |
| 94 state = SCT_PENDING_INCLUSION_CHECK; | |
| 42 } | 95 } |
| 43 | 96 |
| 44 // TODO(eranm): Check inclusion here. | 97 // TODO(eranm): Check inclusion here, see https://crbug.com/624894 |
| 45 // TODO(eranm): UMA - how often inclusion can be checked immediately. | 98 observed_entries_.insert(std::make_pair(std::move(entry), state)); |
| 46 entries_status_.insert( | |
| 47 std::make_pair(sct->timestamp, SCT_PENDING_INCLUSION_CHECK)); | |
| 48 } | 99 } |
| 49 | 100 |
| 50 void SingleTreeTracker::NewSTHObserved(const SignedTreeHead& sth) { | 101 void SingleTreeTracker::NewSTHObserved(const SignedTreeHead& sth) { |
| 51 DCHECK_EQ(ct_log_->key_id(), sth.log_id); | 102 DCHECK_EQ(ct_log_->key_id(), sth.log_id); |
| 52 | 103 |
| 53 if (!ct_log_->VerifySignedTreeHead(sth)) { | 104 if (!ct_log_->VerifySignedTreeHead(sth)) { |
| 54 // Sanity check the STH; the caller should have done this | 105 // Sanity check the STH; the caller should have done this |
| 55 // already, but being paranoid here. | 106 // already, but being paranoid here. |
| 56 // NOTE(eranm): Right now there's no way to get rid of this check here | 107 // 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 | 108 // as this is the first object in the chain that has an instance of |
| 58 // a CTLogVerifier to verify the STH. | 109 // a CTLogVerifier to verify the STH. |
| 59 return; | 110 return; |
| 60 } | 111 } |
| 61 | 112 |
| 62 // In order to avoid updating |verified_sth_| to an older STH in case | 113 // 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 | 114 // 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 | 115 // a larger tree size or that it is for the same tree size but has |
| 65 // a newer timestamp. | 116 // a newer timestamp. |
| 66 const bool sths_for_same_tree = verified_sth_.tree_size == sth.tree_size; | 117 const bool sths_for_same_tree = verified_sth_.tree_size == sth.tree_size; |
| 67 const bool received_sth_is_for_larger_tree = | 118 const bool received_sth_is_for_larger_tree = |
| 68 (verified_sth_.tree_size > sth.tree_size); | 119 (verified_sth_.tree_size > sth.tree_size); |
| 69 const bool received_sth_is_newer = (sth.timestamp > verified_sth_.timestamp); | 120 const bool received_sth_is_newer = (sth.timestamp > verified_sth_.timestamp); |
| 70 | 121 |
| 71 if (verified_sth_.timestamp.is_null() || received_sth_is_for_larger_tree || | 122 if (verified_sth_.timestamp.is_null() || received_sth_is_for_larger_tree || |
| 72 (sths_for_same_tree && received_sth_is_newer)) { | 123 (sths_for_same_tree && received_sth_is_newer)) { |
| 73 verified_sth_ = sth; | 124 verified_sth_ = sth; |
| 74 } | 125 } |
| 75 | 126 |
| 76 // Find out which SCTs can now be checked for inclusion. | 127 // Find out which SCTs can now be checked for inclusion. |
| 77 // TODO(eranm): Keep two maps of MerkleTreeLeaf instances, one for leaves | 128 auto first_entry_not_included = std::find_if( |
|
Ryan Sleevi
2016/07/18 23:38:01
if |observed_entries_| is sorted on timestamp, sho
Eran Messeri
2016/09/21 21:10:43
What I'd like to do is process entries whose times
| |
| 78 // pending inclusion checks and one for leaves pending a new STH. | 129 observed_entries_.begin(), observed_entries_.end(), |
| 79 // The comparison function between MerkleTreeLeaf instances should use the | 130 [&sth](const std::pair<const EntryToAudit&, const SCTInclusionStatus&> |
| 80 // timestamp to determine sorting order, so that bulk moving from one | 131 entry) { return entry.first.sct->timestamp > sth.timestamp; }); |
| 81 // map to the other can happen. | 132 for (auto it = observed_entries_.begin(); it != first_entry_not_included; |
| 82 auto entry = entries_status_.begin(); | 133 ++it) { |
| 83 while (entry != entries_status_.end() && | 134 it->second = SCT_PENDING_INCLUSION_CHECK; |
| 84 entry->first < verified_sth_.timestamp) { | |
| 85 entry->second = SCT_PENDING_INCLUSION_CHECK; | |
| 86 ++entry; | |
| 87 // TODO(eranm): Check inclusion here. | |
| 88 } | 135 } |
| 136 | |
| 137 // TODO(eranm): Check inclusion here of entries that can now be checked. | |
| 138 // See https://crbug.com/624894 | |
| 89 } | 139 } |
| 90 | 140 |
| 91 SingleTreeTracker::SCTInclusionStatus | 141 SingleTreeTracker::SCTInclusionStatus |
| 92 SingleTreeTracker::GetLogEntryInclusionStatus( | 142 SingleTreeTracker::GetLogEntryInclusionStatus( |
| 93 net::X509Certificate* cert, | 143 net::X509Certificate* cert, |
| 94 const net::ct::SignedCertificateTimestamp* sct) { | 144 const net::ct::SignedCertificateTimestamp* sct) { |
| 95 auto it = entries_status_.find(sct->timestamp); | 145 EntryToAudit entry; |
| 146 if (!GetLogEntry(cert, sct, &entry.log_entry)) | |
| 147 return SCT_NOT_OBSERVED; | |
| 148 entry.sct = new SignedCertificateTimestamp(); | |
| 149 ManuallyCopySignedCertificateTimestamp(sct, entry.sct); | |
| 96 | 150 |
| 97 return it == entries_status_.end() ? SCT_NOT_OBSERVED : it->second; | 151 auto entry_iterator = observed_entries_.find(entry); |
|
Ryan Sleevi
2016/07/18 23:38:01
Do you actually need to reconstitute the LogEntry
Eran Messeri
2016/09/21 21:10:43
Obsolete - The SCT is no longer stored, only the h
| |
| 152 if (entry_iterator == observed_entries_.end()) | |
| 153 return SCT_NOT_OBSERVED; | |
| 154 | |
| 155 return entry_iterator->second; | |
| 156 } | |
| 157 | |
| 158 bool SingleTreeTracker::OrderByTimestamp::operator()( | |
| 159 const EntryToAudit& lhs, | |
| 160 const EntryToAudit& rhs) const { | |
| 161 if (lhs.sct->timestamp != rhs.sct->timestamp) | |
| 162 return lhs.sct->timestamp < rhs.sct->timestamp; | |
| 163 | |
| 164 const LogEntry& lhs_entry = lhs.log_entry; | |
| 165 const LogEntry& rhs_entry = rhs.log_entry; | |
| 166 if (lhs_entry.type != rhs_entry.type) | |
| 167 return lhs_entry.type < rhs_entry.type; | |
| 168 | |
| 169 if (lhs_entry.type == LogEntry::LOG_ENTRY_TYPE_X509) | |
| 170 return lhs_entry.leaf_certificate < rhs_entry.leaf_certificate; | |
| 171 | |
| 172 // lhs_entry.type == LOG_ENTRY_TYPE_PRECERT | |
| 173 return lhs_entry.tbs_certificate < rhs_entry.tbs_certificate && | |
| 174 net::SHA256HashValueLessThan()(lhs_entry.issuer_key_hash, | |
| 175 rhs_entry.issuer_key_hash); | |
| 176 } | |
| 177 | |
| 178 bool SingleTreeTracker::EntryAlreadyEncountered(const EntryToAudit& entry) { | |
| 179 return observed_entries_.find(entry) != observed_entries_.end(); | |
|
Ryan Sleevi
2016/07/18 23:38:01
This doesn't seem like it should be a member funct
Eran Messeri
2016/09/21 21:10:43
Obsolete - removed.
| |
| 98 } | 180 } |
| 99 | 181 |
| 100 } // namespace certificate_transparency | 182 } // namespace certificate_transparency |
| OLD | NEW |