Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Side by Side Diff: components/certificate_transparency/single_tree_tracker.cc

Issue 2017563002: Add Certificate Transparency logs auditing (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressing all comments Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
10 #include "net/cert/signed_certificate_timestamp.h" 12 #include "net/cert/signed_certificate_timestamp.h"
11 #include "net/cert/x509_certificate.h" 13 #include "net/cert/x509_certificate.h"
12 14
15 using net::ct::LogEntry;
16 using net::ct::MerkleTreeLeaf;
13 using net::ct::SignedTreeHead; 17 using net::ct::SignedTreeHead;
14 18
15 namespace certificate_transparency { 19 namespace certificate_transparency {
20 bool OrderByTimestamp::operator()(const ObservedLeaf& lhs,
Ryan Sleevi 2016/06/30 22:48:19 Should have had a line break after the namespace
Eran Messeri 2016/07/01 13:24:00 Done.
21 const ObservedLeaf& rhs) {
22 const MerkleTreeLeaf& lhs_leaf = lhs.GetLeaf();
23 const MerkleTreeLeaf& rhs_leaf = rhs.GetLeaf();
24 // This comparator should only used for containers where leaves from
25 // different logs are not mixed.
26 DCHECK(lhs_leaf.log_id == rhs_leaf.log_id);
Ryan Sleevi 2016/06/30 22:48:19 DCHECK_EQ
Eran Messeri 2016/07/01 13:24:00 Done.
27
28 if (lhs_leaf.timestamp != rhs_leaf.timestamp)
29 return lhs_leaf.timestamp < rhs_leaf.timestamp;
30
31 // If the condition above is false, then |lhs| and |rhs| are either two
32 // leaves with the same timestamp or the same leaf. Compare the actual
33 // LogEntries to find out.
Ryan Sleevi 2016/06/30 22:48:18 Grammatical comments about position tend to read w
Eran Messeri 2016/07/01 13:24:00 Removed the comment, since, as you say, it was not
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 == 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()(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
28 // SCT was previously observed, so its status should not be changed. 59 MerkleTreeLeaf leaf;
29 if (entries_status_.find(sct->timestamp) != entries_status_.end()) 60 if (!GetMerkleTreeLeaf(cert, sct, &leaf))
30 return; 61 return;
31 62
63 ObservedLeaf timestamped_leaf(std::move(leaf), base::Time::Now());
64
65 // SCT was previously observed, so its status should not be changed.
66 if (EntryPendingNewSTH(timestamped_leaf) ||
67 EntryPendingInclusionProof(timestamped_leaf))
68 return;
69
70 const base::TimeDelta kMaximumMergeDelay = base::TimeDelta::FromHours(24);
32 // If there isn't a valid STH or the STH is not fresh enough to check 71 // 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. 72 // inclusion against, store the SCT for future checking and return.
34 if (verified_sth_.timestamp.is_null() || 73 if (verified_sth_.timestamp.is_null() ||
35 (verified_sth_.timestamp < 74 (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 75 // TODO(eranm): UMA - how often SCTs have to wait for a newer STH for
38 // inclusion check. 76 // inclusion check.
39 entries_status_.insert( 77 pending_new_sth_.insert(std::move(timestamped_leaf));
40 std::make_pair(sct->timestamp, SCT_PENDING_NEWER_STH));
41 return; 78 return;
42 } 79 }
43 80
44 // TODO(eranm): Check inclusion here. 81 // TODO(eranm): Check inclusion here, see https://crbug.com/624894
45 // TODO(eranm): UMA - how often inclusion can be checked immediately. 82 // TODO(eranm): UMA - how often inclusion can be checked immediately.
46 entries_status_.insert( 83 pending_inclusion_check_.insert(std::move(timestamped_leaf));
47 std::make_pair(sct->timestamp, SCT_PENDING_INCLUSION_CHECK));
48 } 84 }
49 85
50 void SingleTreeTracker::NewSTHObserved(const SignedTreeHead& sth) { 86 void SingleTreeTracker::NewSTHObserved(const SignedTreeHead& sth) {
51 DCHECK_EQ(ct_log_->key_id(), sth.log_id); 87 DCHECK_EQ(ct_log_->key_id(), sth.log_id);
52 88
53 if (!ct_log_->VerifySignedTreeHead(sth)) { 89 if (!ct_log_->VerifySignedTreeHead(sth)) {
54 // Sanity check the STH; the caller should have done this 90 // Sanity check the STH; the caller should have done this
55 // already, but being paranoid here. 91 // already, but being paranoid here.
56 // NOTE(eranm): Right now there's no way to get rid of this check here 92 // 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 93 // as this is the first object in the chain that has an instance of
58 // a CTLogVerifier to verify the STH. 94 // a CTLogVerifier to verify the STH.
59 return; 95 return;
60 } 96 }
61 97
62 // In order to avoid updating |verified_sth_| to an older STH in case 98 // 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 99 // 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 100 // a larger tree size or that it is for the same tree size but has
65 // a newer timestamp. 101 // a newer timestamp.
66 const bool sths_for_same_tree = verified_sth_.tree_size == sth.tree_size; 102 const bool sths_for_same_tree = verified_sth_.tree_size == sth.tree_size;
67 const bool received_sth_is_for_larger_tree = 103 const bool received_sth_is_for_larger_tree =
68 (verified_sth_.tree_size > sth.tree_size); 104 (verified_sth_.tree_size > sth.tree_size);
69 const bool received_sth_is_newer = (sth.timestamp > verified_sth_.timestamp); 105 const bool received_sth_is_newer = (sth.timestamp > verified_sth_.timestamp);
70 106
71 if (verified_sth_.timestamp.is_null() || received_sth_is_for_larger_tree || 107 if (verified_sth_.timestamp.is_null() || received_sth_is_for_larger_tree ||
72 (sths_for_same_tree && received_sth_is_newer)) { 108 (sths_for_same_tree && received_sth_is_newer)) {
73 verified_sth_ = sth; 109 verified_sth_ = sth;
74 } 110 }
75 111
112 if (pending_new_sth_.empty())
113 return;
76 // Find out which SCTs can now be checked for inclusion. 114 // Find out which SCTs can now be checked for inclusion.
Ryan Sleevi 2016/06/30 22:48:19 Use vertical whitespace
Eran Messeri 2016/07/01 13:24:00 Done.
77 // TODO(eranm): Keep two maps of MerkleTreeLeaf instances, one for leaves 115 auto sth_timestamp_compare = [&sth](const ObservedLeaf& leaf) {
78 // pending inclusion checks and one for leaves pending a new STH. 116 return leaf.GetLeaf().timestamp > sth.timestamp;
79 // The comparison function between MerkleTreeLeaf instances should use the 117 };
Ryan Sleevi 2016/06/30 22:48:18 Don't create a temporary variable for this one cal
Eran Messeri 2016/07/01 13:24:00 Done.
80 // timestamp to determine sorting order, so that bulk moving from one 118 auto first_entry_not_included = std::find_if(
81 // map to the other can happen. 119 pending_new_sth_.begin(), pending_new_sth_.end(), sth_timestamp_compare);
82 auto entry = entries_status_.begin(); 120 // Move all entries up to the first entry not covered by this STH to
83 while (entry != entries_status_.end() && 121 // the set of entries pending inclusion check.
Ryan Sleevi 2016/06/30 22:48:19 Grammatically, this reads weird. "move all entries
Eran Messeri 2016/07/01 13:24:00 Correct, but that piece of code is gone.
84 entry->first < verified_sth_.timestamp) { 122 std::move(pending_new_sth_.begin(), first_entry_not_included,
85 entry->second = SCT_PENDING_INCLUSION_CHECK; 123 std::inserter(pending_inclusion_check_,
86 ++entry; 124 pending_inclusion_check_.begin()));
87 // TODO(eranm): Check inclusion here. 125 // Clear moved entries.
Ryan Sleevi 2016/06/30 22:48:19 Doesn't add value with the above rewrite
Eran Messeri 2016/07/01 13:24:00 Done.
88 } 126 pending_new_sth_.erase(pending_new_sth_.begin(), first_entry_not_included);
127
128 // TODO(eranm): Check inclusion here of entries that can now be checked.
129 // See https://crbug.com/624894
89 } 130 }
90 131
91 SingleTreeTracker::SCTInclusionStatus 132 SingleTreeTracker::SCTInclusionStatus
92 SingleTreeTracker::GetLogEntryInclusionStatus( 133 SingleTreeTracker::GetLogEntryInclusionStatus(
93 net::X509Certificate* cert, 134 net::X509Certificate* cert,
94 const net::ct::SignedCertificateTimestamp* sct) { 135 const net::ct::SignedCertificateTimestamp* sct) {
95 auto it = entries_status_.find(sct->timestamp); 136 MerkleTreeLeaf leaf;
137 if (!GetMerkleTreeLeaf(cert, sct, &leaf))
138 return SCT_NOT_OBSERVED;
96 139
97 return it == entries_status_.end() ? SCT_NOT_OBSERVED : it->second; 140 // The observation time for this leaf is not relevant as the comparison
141 // function does not take it into account.
142 ObservedLeaf observed_leaf(std::move(leaf), base::Time::UnixEpoch());
143 if (EntryPendingNewSTH(observed_leaf))
144 return SCT_PENDING_NEWER_STH;
145
146 if (EntryPendingInclusionProof(observed_leaf))
147 return SCT_PENDING_INCLUSION_CHECK;
148
149 // TODO(eranm): Look up the result of inclusion check for this leaf.
150
151 return SCT_NOT_OBSERVED;
152 }
153
154 bool SingleTreeTracker::EntryPendingNewSTH(const ObservedLeaf& leaf) {
155 return pending_new_sth_.find(leaf) != pending_new_sth_.end();
156 }
157
158 bool SingleTreeTracker::EntryPendingInclusionProof(const ObservedLeaf& leaf) {
159 return pending_inclusion_check_.find(leaf) != pending_inclusion_check_.end();
98 } 160 }
99 161
100 } // namespace certificate_transparency 162 } // namespace certificate_transparency
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698