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

Side by Side Diff: components/network_time/network_time_tracker.cc

Issue 1835823002: network_time_tracker: add temporary time protocol. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rsleevi review 1 Created 4 years, 7 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/network_time/network_time_tracker.h" 5 #include "components/network_time/network_time_tracker.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/i18n/time_formatting.h" 10 #include "base/i18n/time_formatting.h"
11 #include "base/json/json_reader.h"
11 #include "base/logging.h" 12 #include "base/logging.h"
12 #include "base/strings/utf_string_conversions.h" 13 #include "base/strings/utf_string_conversions.h"
13 #include "base/time/tick_clock.h" 14 #include "base/time/tick_clock.h"
14 #include "build/build_config.h" 15 #include "build/build_config.h"
16 #include "components/client_update_protocol/ecdsa.h"
15 #include "components/network_time/network_time_pref_names.h" 17 #include "components/network_time/network_time_pref_names.h"
16 #include "components/prefs/pref_registry_simple.h" 18 #include "components/prefs/pref_registry_simple.h"
17 #include "components/prefs/pref_service.h" 19 #include "components/prefs/pref_service.h"
20 #include "net/base/load_flags.h"
21 #include "net/http/http_response_headers.h"
22 #include "net/url_request/url_fetcher.h"
23 #include "url/gurl.h"
18 24
19 namespace network_time { 25 namespace network_time {
20 26
21 namespace { 27 namespace {
22 28
29 // Minimum number of minutes between time queries.
30 const uint32_t kMinimumQueryDelayMinutes = 60;
31
23 // Number of time measurements performed in a given network time calculation. 32 // Number of time measurements performed in a given network time calculation.
24 const uint32_t kNumTimeMeasurements = 7; 33 const uint32_t kNumTimeMeasurements = 7;
25 34
26 // Amount of divergence allowed between wall clock and tick clock. 35 // Amount of divergence allowed between wall clock and tick clock.
27 const uint32_t kClockDivergenceSeconds = 60; 36 const uint32_t kClockDivergenceSeconds = 60;
28 37
29 // Maximum time lapse before deserialized data are considered stale. 38 // Maximum time lapse before deserialized data are considered stale.
30 const uint32_t kSerializedDataMaxAgeDays = 7; 39 const uint32_t kSerializedDataMaxAgeDays = 7;
31 40
32 // Name of a pref that stores the wall clock time, via |ToJsTime|. 41 // Name of a pref that stores the wall clock time, via |ToJsTime|.
33 const char kPrefTime[] = "local"; 42 const char kPrefTime[] = "local";
34 43
35 // Name of a pref that stores the tick clock time, via |ToInternalValue|. 44 // Name of a pref that stores the tick clock time, via |ToInternalValue|.
36 const char kPrefTicks[] = "ticks"; 45 const char kPrefTicks[] = "ticks";
37 46
38 // Name of a pref that stores the time uncertainty, via |ToInternalValue|. 47 // Name of a pref that stores the time uncertainty, via |ToInternalValue|.
39 const char kPrefUncertainty[] = "uncertainty"; 48 const char kPrefUncertainty[] = "uncertainty";
40 49
41 // Name of a pref that stores the network time via |ToJsTime|. 50 // Name of a pref that stores the network time via |ToJsTime|.
42 const char kPrefNetworkTime[] = "network"; 51 const char kPrefNetworkTime[] = "network";
43 52
53 // Time server's maximum allowable clock skew, in seconds.
54 const uint32_t kTimeServerMaxSkewSeconds = 10;
55
56 const char kTimeServiceURL[] = "http://clients2.google.com/time/1/current";
mmenke 2016/04/28 15:17:09 I thought there was a plan to move most google dom
mab 2016/04/29 19:42:07 I'll have to defer to waffles on the HSTS question
waffles 2016/04/29 19:51:22 mmenke: Please contact me (internally) with any ne
mmenke 2016/04/29 19:59:44 You're probably more knowledgeable here than I am.
57
58 // This is an ECDSA prime256v1 named-curve key.
59 const int kKeyVersion = 1;
60 const uint8_t kKeyPubBytes[] = {
61 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
62 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03,
63 0x42, 0x00, 0x04, 0xeb, 0xd8, 0xad, 0x0b, 0x8f, 0x75, 0xe8, 0x84, 0x36,
64 0x23, 0x48, 0x14, 0x24, 0xd3, 0x93, 0x42, 0x25, 0x43, 0xc1, 0xde, 0x36,
65 0x29, 0xc6, 0x95, 0xca, 0xeb, 0x28, 0x85, 0xff, 0x09, 0xdc, 0x08, 0xec,
66 0x45, 0x74, 0x6e, 0x4b, 0xc3, 0xa5, 0xfd, 0x8a, 0x2f, 0x02, 0xa0, 0x4b,
67 0xc3, 0xc6, 0xa4, 0x7b, 0xa4, 0x41, 0xfc, 0xa7, 0x02, 0x54, 0xab, 0xe3,
68 0xe4, 0xb1, 0x00, 0xf5, 0xd5, 0x09, 0x11};
69
70 std::string GetServerProof(const net::URLFetcher* source) {
71 const net::HttpResponseHeaders* response_headers =
72 source->GetResponseHeaders();
73 if (!response_headers) {
74 return std::string();
75 }
76 std::string proof;
77 return response_headers->EnumerateHeader(nullptr, "x-cup-server-proof",
78 &proof)
79 ? proof
80 : std::string();
81 }
82
44 } // namespace 83 } // namespace
45 84
46 // static 85 // static
47 void NetworkTimeTracker::RegisterPrefs(PrefRegistrySimple* registry) { 86 void NetworkTimeTracker::RegisterPrefs(PrefRegistrySimple* registry) {
48 registry->RegisterDictionaryPref(prefs::kNetworkTimeMapping, 87 registry->RegisterDictionaryPref(prefs::kNetworkTimeMapping,
49 new base::DictionaryValue()); 88 new base::DictionaryValue());
50 } 89 }
51 90
52 NetworkTimeTracker::NetworkTimeTracker( 91 NetworkTimeTracker::NetworkTimeTracker(
53 std::unique_ptr<base::Clock> clock, 92 std::unique_ptr<base::Clock> clock,
54 std::unique_ptr<base::TickClock> tick_clock, 93 std::unique_ptr<base::TickClock> tick_clock,
55 PrefService* pref_service) 94 PrefService* pref_service,
56 : clock_(std::move(clock)), 95 scoped_refptr<net::URLRequestContextGetter>& getter)
96 : getter_(getter),
97 clock_(std::move(clock)),
57 tick_clock_(std::move(tick_clock)), 98 tick_clock_(std::move(tick_clock)),
58 pref_service_(pref_service) { 99 pref_service_(pref_service) {
59 const base::DictionaryValue* time_mapping = 100 const base::DictionaryValue* time_mapping =
60 pref_service_->GetDictionary(prefs::kNetworkTimeMapping); 101 pref_service_->GetDictionary(prefs::kNetworkTimeMapping);
61 double time_js = 0; 102 double time_js = 0;
62 double ticks_js = 0; 103 double ticks_js = 0;
63 double network_time_js = 0; 104 double network_time_js = 0;
64 double uncertainty_js = 0; 105 double uncertainty_js = 0;
65 if (time_mapping->GetDouble(kPrefTime, &time_js) && 106 if (time_mapping->GetDouble(kPrefTime, &time_js) &&
66 time_mapping->GetDouble(kPrefTicks, &ticks_js) && 107 time_mapping->GetDouble(kPrefTicks, &ticks_js) &&
67 time_mapping->GetDouble(kPrefUncertainty, &uncertainty_js) && 108 time_mapping->GetDouble(kPrefUncertainty, &uncertainty_js) &&
68 time_mapping->GetDouble(kPrefNetworkTime, &network_time_js)) { 109 time_mapping->GetDouble(kPrefNetworkTime, &network_time_js)) {
69 time_at_last_measurement_ = base::Time::FromJsTime(time_js); 110 time_at_last_measurement_ = base::Time::FromJsTime(time_js);
70 ticks_at_last_measurement_ = base::TimeTicks::FromInternalValue( 111 ticks_at_last_measurement_ = base::TimeTicks::FromInternalValue(
71 static_cast<int64_t>(ticks_js)); 112 static_cast<int64_t>(ticks_js));
72 network_time_uncertainty_ = base::TimeDelta::FromInternalValue( 113 network_time_uncertainty_ = base::TimeDelta::FromInternalValue(
73 static_cast<int64_t>(uncertainty_js)); 114 static_cast<int64_t>(uncertainty_js));
74 network_time_at_last_measurement_ = base::Time::FromJsTime(network_time_js); 115 network_time_at_last_measurement_ = base::Time::FromJsTime(network_time_js);
75 } 116 }
76 base::Time now = clock_->Now(); 117 base::Time now = clock_->Now();
77 if (ticks_at_last_measurement_ > tick_clock_->NowTicks() || 118 if (ticks_at_last_measurement_ > tick_clock_->NowTicks() ||
78 time_at_last_measurement_ > now || 119 time_at_last_measurement_ > now ||
79 now - time_at_last_measurement_ > 120 now - time_at_last_measurement_ >
80 base::TimeDelta::FromDays(kSerializedDataMaxAgeDays)) { 121 base::TimeDelta::FromDays(kSerializedDataMaxAgeDays)) {
81 // Drop saved mapping if either clock has run backward, or the data are too 122 // Drop saved mapping if either clock has run backward, or the data are too
82 // old. 123 // old.
83 pref_service_->ClearPref(prefs::kNetworkTimeMapping); 124 pref_service_->ClearPref(prefs::kNetworkTimeMapping);
84 network_time_at_last_measurement_ = base::Time(); // Reset. 125 network_time_at_last_measurement_ = base::Time(); // Reset.
85 } 126 }
127
128 base::StringPiece public_key = {reinterpret_cast<const char*>(kKeyPubBytes),
129 sizeof(kKeyPubBytes)};
130
131 if (getter_) {
132 query_signer_ =
133 client_update_protocol::Ecdsa::Create(kKeyVersion, public_key);
134 base::TimeDelta period =
135 base::TimeDelta::FromMinutes(kMinimumQueryDelayMinutes);
136 query_timer_.Start(FROM_HERE, period, this,
137 &NetworkTimeTracker::QueryTimeService);
138 }
86 } 139 }
87 140
88 NetworkTimeTracker::~NetworkTimeTracker() { 141 NetworkTimeTracker::~NetworkTimeTracker() {
89 DCHECK(thread_checker_.CalledOnValidThread()); 142 DCHECK(thread_checker_.CalledOnValidThread());
90 } 143 }
91 144
145 void NetworkTimeTracker::QueryTimeService() {
146 DCHECK(thread_checker_.CalledOnValidThread());
147
148 // If GetNetworkTime() returns true, the NetworkTimeTracker thinks it is in
149 // sync, so there is no need to query.
150 base::Time network_time;
151 if (GetNetworkTime(&network_time, nullptr)) {
152 return;
153 }
154
155 std::string query_string;
156 query_signer_->SignRequest(nullptr, &query_string);
157 GURL url(kTimeServiceURL);
158 GURL::Replacements replacements;
159 replacements.SetQueryStr(query_string);
160 url = url.ReplaceComponents(replacements);
161
162 // This cancels any outstanding fetch.
163 time_fetcher_ = net::URLFetcher::Create(url, net::URLFetcher::GET, this);
164 if (!time_fetcher_) {
165 DVLOG(1) << "tried to make fetch happen; failed";
166 return;
167 }
168 time_fetcher_->SetRequestContext(getter_.get());
169 // Not expecting any cookies, but just in case.
170 time_fetcher_->SetLoadFlags(net::LOAD_BYPASS_CACHE | net::LOAD_DISABLE_CACHE |
171 net::LOAD_DO_NOT_SAVE_COOKIES |
172 net::LOAD_DO_NOT_SEND_COOKIES |
173 net::LOAD_DO_NOT_SEND_AUTH_DATA);
174 time_fetcher_->Start();
175 fetch_started_ = tick_clock_->NowTicks();
176 }
177
178 void NetworkTimeTracker::OnURLFetchComplete(const net::URLFetcher* source) {
179 DCHECK(thread_checker_.CalledOnValidThread());
180 DCHECK(source);
181 if (source->GetStatus().status() != net::URLRequestStatus::SUCCESS &&
182 source->GetResponseCode() != 200) {
183 DVLOG(1) << "fetch failed, status=" << source->GetStatus().status()
184 << ",code=" << source->GetResponseCode();
185 return;
186 }
187
188 std::string response_body;
189 if (!source->GetResponseAsString(&response_body)) {
190 DVLOG(1) << "failed to get response";
191 return;
192 }
193 DCHECK(query_signer_);
194 if (!query_signer_->ValidateResponse(response_body, GetServerProof(source))) {
195 DVLOG(1) << "invalid signature";
196 return;
197 }
198 response_body = response_body.substr(5); // Skips leading )]}'\n
199 base::JSONReader reader;
200 std::unique_ptr<base::Value> value = reader.Read(response_body);
201 if (!value) {
202 DVLOG(1) << "bad JSON";
203 return;
204 }
205 const base::DictionaryValue* dict;
206 if (!value->GetAsDictionary(&dict)) {
207 DVLOG(1) << "not a dictionary";
208 return;
209 }
210 double current_time_millis;
211 if (!dict->GetDouble("current_time_millis", &current_time_millis)) {
212 DVLOG(1) << "no current_time_millis";
213 return;
214 }
215 // There is a "server_nonce" key here too, but it serves no purpose other than
216 // to make the server's response unpredictable.
217 base::Time current_time = base::Time::FromJsTime(current_time_millis);
218 // The extra 10 seconds comes from a property of the time server that we
219 // happen to know, which is its maximum allowable clock skew. It's unlikely
220 // that it would ever be that badly wrong, but all the same it's included here
221 // to document the very rough nature of the time service provided by this
222 // class.
223 base::TimeDelta resolution =
224 base::TimeDelta::FromMilliseconds(1) +
225 base::TimeDelta::FromSeconds(kTimeServerMaxSkewSeconds);
226 base::TimeDelta latency = tick_clock_->NowTicks() - fetch_started_;
227 UpdateNetworkTime(current_time, resolution, latency, tick_clock_->NowTicks());
228 }
229
92 void NetworkTimeTracker::UpdateNetworkTime(base::Time network_time, 230 void NetworkTimeTracker::UpdateNetworkTime(base::Time network_time,
93 base::TimeDelta resolution, 231 base::TimeDelta resolution,
94 base::TimeDelta latency, 232 base::TimeDelta latency,
95 base::TimeTicks post_time) { 233 base::TimeTicks post_time) {
96 DCHECK(thread_checker_.CalledOnValidThread()); 234 DCHECK(thread_checker_.CalledOnValidThread());
97 DVLOG(1) << "Network time updating to " 235 DVLOG(1) << "Network time updating to "
98 << base::UTF16ToUTF8( 236 << base::UTF16ToUTF8(
99 base::TimeFormatFriendlyDateAndTime(network_time)); 237 base::TimeFormatFriendlyDateAndTime(network_time));
100 // Update network time on every request to limit dependency on ticks lag. 238 // Update network time on every request to limit dependency on ticks lag.
101 // TODO(mad): Find a heuristic to avoid augmenting the 239 // TODO(mad): Find a heuristic to avoid augmenting the
(...skipping 23 matching lines...) Expand all
125 263
126 base::DictionaryValue time_mapping; 264 base::DictionaryValue time_mapping;
127 time_mapping.SetDouble(kPrefTime, time_at_last_measurement_.ToJsTime()); 265 time_mapping.SetDouble(kPrefTime, time_at_last_measurement_.ToJsTime());
128 time_mapping.SetDouble(kPrefTicks, static_cast<double>( 266 time_mapping.SetDouble(kPrefTicks, static_cast<double>(
129 ticks_at_last_measurement_.ToInternalValue())); 267 ticks_at_last_measurement_.ToInternalValue()));
130 time_mapping.SetDouble(kPrefUncertainty, static_cast<double>( 268 time_mapping.SetDouble(kPrefUncertainty, static_cast<double>(
131 network_time_uncertainty_.ToInternalValue())); 269 network_time_uncertainty_.ToInternalValue()));
132 time_mapping.SetDouble(kPrefNetworkTime, 270 time_mapping.SetDouble(kPrefNetworkTime,
133 network_time_at_last_measurement_.ToJsTime()); 271 network_time_at_last_measurement_.ToJsTime());
134 pref_service_->Set(prefs::kNetworkTimeMapping, time_mapping); 272 pref_service_->Set(prefs::kNetworkTimeMapping, time_mapping);
273
274 if (getter_) {
275 query_timer_.Reset();
276 }
135 } 277 }
136 278
137 bool NetworkTimeTracker::GetNetworkTime(base::Time* network_time, 279 bool NetworkTimeTracker::GetNetworkTime(base::Time* network_time,
138 base::TimeDelta* uncertainty) const { 280 base::TimeDelta* uncertainty) const {
139 DCHECK(thread_checker_.CalledOnValidThread()); 281 DCHECK(thread_checker_.CalledOnValidThread());
140 DCHECK(network_time); 282 DCHECK(network_time);
141 if (network_time_at_last_measurement_.is_null()) { 283 if (network_time_at_last_measurement_.is_null()) {
142 return false; 284 return false;
143 } 285 }
144 DCHECK(!ticks_at_last_measurement_.is_null()); 286 DCHECK(!ticks_at_last_measurement_.is_null());
(...skipping 16 matching lines...) Expand all
161 return false; 303 return false;
162 } 304 }
163 *network_time = network_time_at_last_measurement_ + tick_delta; 305 *network_time = network_time_at_last_measurement_ + tick_delta;
164 if (uncertainty) { 306 if (uncertainty) {
165 *uncertainty = network_time_uncertainty_ + divergence; 307 *uncertainty = network_time_uncertainty_ + divergence;
166 } 308 }
167 return true; 309 return true;
168 } 310 }
169 311
170 } // namespace network_time 312 } // namespace network_time
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698