OLD | NEW |
---|---|
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 "net/ssl/channel_id_service.h" | 5 #include "net/ssl/channel_id_service.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <limits> | 8 #include <limits> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
81 base::TimeDelta::FromMinutes(5), | 81 base::TimeDelta::FromMinutes(5), |
82 50); | 82 50); |
83 } | 83 } |
84 | 84 |
85 // On success, returns a ChannelID object and sets |*error| to OK. | 85 // On success, returns a ChannelID object and sets |*error| to OK. |
86 // Otherwise, returns NULL, and |*error| will be set to a net error code. | 86 // Otherwise, returns NULL, and |*error| will be set to a net error code. |
87 // |serial_number| is passed in because base::RandInt cannot be called from an | 87 // |serial_number| is passed in because base::RandInt cannot be called from an |
88 // unjoined thread, due to relying on a non-leaked LazyInstance | 88 // unjoined thread, due to relying on a non-leaked LazyInstance |
89 scoped_ptr<ChannelIDStore::ChannelID> GenerateChannelID( | 89 scoped_ptr<ChannelIDStore::ChannelID> GenerateChannelID( |
90 const std::string& server_identifier, | 90 const std::string& server_identifier, |
91 uint32 serial_number, | |
92 int* error) { | 91 int* error) { |
93 scoped_ptr<ChannelIDStore::ChannelID> result; | 92 scoped_ptr<ChannelIDStore::ChannelID> result; |
94 | 93 |
95 base::TimeTicks start = base::TimeTicks::Now(); | 94 base::TimeTicks start = base::TimeTicks::Now(); |
96 base::Time not_valid_before = base::Time::Now(); | 95 base::Time not_valid_before = base::Time::Now(); |
97 base::Time not_valid_after = | 96 scoped_ptr<crypto::ECPrivateKey> key(crypto::ECPrivateKey::Create()); |
98 not_valid_before + base::TimeDelta::FromDays(kValidityPeriodInDays); | 97 |
99 std::string der_cert; | 98 if (!key) { |
100 std::vector<uint8> private_key_info; | 99 DLOG(ERROR) << "Unable to create channel ID key pair"; |
101 scoped_ptr<crypto::ECPrivateKey> key; | 100 *error = ERR_KEY_GENERATION_FAILED; |
102 if (!x509_util::CreateKeyAndChannelIDEC(server_identifier, | |
103 serial_number, | |
104 not_valid_before, | |
105 not_valid_after, | |
106 &key, | |
107 &der_cert)) { | |
108 DLOG(ERROR) << "Unable to create x509 cert for client"; | |
109 *error = ERR_ORIGIN_BOUND_CERT_GENERATION_FAILED; | |
110 return result.Pass(); | 101 return result.Pass(); |
111 } | 102 } |
112 | 103 |
113 if (!key->ExportEncryptedPrivateKey(ChannelIDService::kEPKIPassword, | 104 std::string private_key_out; |
114 1, &private_key_info)) { | 105 std::string public_key_out; |
115 DLOG(ERROR) << "Unable to export private key"; | 106 *error = ExportKeypair(key, &public_key_out, &private_key_out); |
116 *error = ERR_PRIVATE_KEY_EXPORT_FAILED; | 107 if (*error != OK) |
117 return result.Pass(); | 108 return result.Pass(); |
118 } | |
119 | |
120 // TODO(rkn): Perhaps ExportPrivateKey should be changed to output a | |
121 // std::string* to prevent this copying. | |
122 std::string key_out(private_key_info.begin(), private_key_info.end()); | |
123 | 109 |
124 result.reset(new ChannelIDStore::ChannelID( | 110 result.reset(new ChannelIDStore::ChannelID( |
125 server_identifier, | 111 server_identifier, not_valid_before, private_key_out, public_key_out)); |
126 not_valid_before, | |
127 not_valid_after, | |
128 key_out, | |
129 der_cert)); | |
130 UMA_HISTOGRAM_CUSTOM_TIMES("DomainBoundCerts.GenerateCertTime", | 112 UMA_HISTOGRAM_CUSTOM_TIMES("DomainBoundCerts.GenerateCertTime", |
131 base::TimeTicks::Now() - start, | 113 base::TimeTicks::Now() - start, |
132 base::TimeDelta::FromMilliseconds(1), | 114 base::TimeDelta::FromMilliseconds(1), |
133 base::TimeDelta::FromMinutes(5), | 115 base::TimeDelta::FromMinutes(5), |
134 50); | 116 50); |
135 *error = OK; | 117 *error = OK; |
136 return result.Pass(); | 118 return result.Pass(); |
137 } | 119 } |
138 | 120 |
139 } // namespace | 121 } // namespace |
140 | 122 |
123 int CreateECPrivateKeyFromSerializedKey( | |
124 const std::string& public_key, | |
125 const std::string& private_key, | |
126 scoped_ptr<crypto::ECPrivateKey>* key_out) { | |
127 if (public_key.empty() || private_key.empty()) | |
128 return ERR_UNEXPECTED; | |
129 | |
130 std::vector<uint8> public_key_vector(public_key.size()); | |
131 memcpy(&public_key_vector[0], public_key.data(), public_key.size()); | |
Ryan Sleevi
2015/04/15 22:50:02
DANGER: What if |public_key| is .empty()? This wou
nharper
2015/04/25 02:59:19
If public_key or private_key is .empty(), then thi
| |
132 std::vector<uint8> private_key_vector(private_key.size()); | |
133 memcpy(&private_key_vector[0], private_key.data(), private_key.size()); | |
134 crypto::ECPrivateKey* key = | |
135 crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( | |
136 ChannelIDService::kEPKIPassword, private_key_vector, | |
137 public_key_vector); | |
138 if (key == nullptr) | |
139 return ERR_UNEXPECTED; | |
140 key_out->reset(key); | |
141 return OK; | |
142 } | |
143 | |
144 int ExportKeypair(scoped_ptr<crypto::ECPrivateKey>& key, | |
Ryan Sleevi
2015/04/15 22:50:02
pass as const-ref
But really, you should be sendi
nharper
2015/04/25 02:59:19
Changed to const-ref. Does the style guide have in
| |
145 std::string* public_key, | |
146 std::string* private_key) { | |
147 std::vector<uint8> public_key_vector; | |
148 std::vector<uint8> private_key_vector; | |
149 | |
150 if (!key) | |
151 return ERR_UNEXPECTED; | |
152 | |
153 if (!key->ExportEncryptedPrivateKey(ChannelIDService::kEPKIPassword, 1, | |
154 &private_key_vector) || | |
155 !key->ExportPublicKey(&public_key_vector)) { | |
156 DLOG(ERROR) << "Unable to export key"; | |
157 return ERR_PRIVATE_KEY_EXPORT_FAILED; | |
158 } | |
159 | |
160 // TODO(rkn): Perhaps ExportPrivateKey should be changed to output a | |
161 // std::string* to prevent this copying. | |
162 *public_key = std::string(public_key_vector.begin(), public_key_vector.end()); | |
Ryan Sleevi
2015/04/15 22:50:02
Does this work across platforms without throwing c
nharper
2015/04/25 02:59:20
This code was copied from the lhs of this diff (se
| |
163 *private_key = | |
164 std::string(private_key_vector.begin(), private_key_vector.end()); | |
165 return OK; | |
166 } | |
Ryan Sleevi
2015/04/15 22:50:02
Should match order in .h
nharper
2015/04/25 02:59:19
Done.
| |
167 | |
141 // Represents the output and result callback of a request. | 168 // Represents the output and result callback of a request. |
142 class ChannelIDServiceRequest { | 169 class ChannelIDServiceRequest { |
143 public: | 170 public: |
144 ChannelIDServiceRequest(base::TimeTicks request_start, | 171 ChannelIDServiceRequest(base::TimeTicks request_start, |
145 const CompletionCallback& callback, | 172 const CompletionCallback& callback, |
146 std::string* private_key, | 173 scoped_ptr<crypto::ECPrivateKey>* key) |
147 std::string* cert) | 174 : request_start_(request_start), callback_(callback), key_(key) {} |
148 : request_start_(request_start), | |
149 callback_(callback), | |
150 private_key_(private_key), | |
151 cert_(cert) { | |
152 } | |
153 | 175 |
154 // Ensures that the result callback will never be made. | 176 // Ensures that the result callback will never be made. |
155 void Cancel() { | 177 void Cancel() { |
156 RecordGetChannelIDResult(ASYNC_CANCELLED); | 178 RecordGetChannelIDResult(ASYNC_CANCELLED); |
157 callback_.Reset(); | 179 callback_.Reset(); |
158 private_key_ = NULL; | 180 key_->reset(); |
159 cert_ = NULL; | |
160 } | 181 } |
161 | 182 |
162 // Copies the contents of |private_key| and |cert| to the caller's output | 183 // Copies the contents of |private_key| and |cert| to the caller's output |
163 // arguments and calls the callback. | 184 // arguments and calls the callback. |
164 void Post(int error, | 185 void Post(int error, |
165 const std::string& private_key, | 186 const std::string& private_key, |
166 const std::string& cert) { | 187 const std::string& public_key) { |
167 switch (error) { | 188 switch (error) { |
168 case OK: { | 189 case OK: { |
169 base::TimeDelta request_time = base::TimeTicks::Now() - request_start_; | 190 base::TimeDelta request_time = base::TimeTicks::Now() - request_start_; |
170 UMA_HISTOGRAM_CUSTOM_TIMES("DomainBoundCerts.GetCertTimeAsync", | 191 UMA_HISTOGRAM_CUSTOM_TIMES("DomainBoundCerts.GetCertTimeAsync", |
171 request_time, | 192 request_time, |
172 base::TimeDelta::FromMilliseconds(1), | 193 base::TimeDelta::FromMilliseconds(1), |
173 base::TimeDelta::FromMinutes(5), | 194 base::TimeDelta::FromMinutes(5), |
174 50); | 195 50); |
175 RecordGetChannelIDTime(request_time); | 196 RecordGetChannelIDTime(request_time); |
176 RecordGetChannelIDResult(ASYNC_SUCCESS); | 197 RecordGetChannelIDResult(ASYNC_SUCCESS); |
177 break; | 198 break; |
178 } | 199 } |
179 case ERR_KEY_GENERATION_FAILED: | 200 case ERR_KEY_GENERATION_FAILED: |
180 RecordGetChannelIDResult(ASYNC_FAILURE_KEYGEN); | 201 RecordGetChannelIDResult(ASYNC_FAILURE_KEYGEN); |
181 break; | 202 break; |
182 case ERR_ORIGIN_BOUND_CERT_GENERATION_FAILED: | 203 case ERR_ORIGIN_BOUND_CERT_GENERATION_FAILED: |
183 RecordGetChannelIDResult(ASYNC_FAILURE_CREATE_CERT); | 204 RecordGetChannelIDResult(ASYNC_FAILURE_CREATE_CERT); |
184 break; | 205 break; |
185 case ERR_PRIVATE_KEY_EXPORT_FAILED: | 206 case ERR_PRIVATE_KEY_EXPORT_FAILED: |
186 RecordGetChannelIDResult(ASYNC_FAILURE_EXPORT_KEY); | 207 RecordGetChannelIDResult(ASYNC_FAILURE_EXPORT_KEY); |
187 break; | 208 break; |
188 case ERR_INSUFFICIENT_RESOURCES: | 209 case ERR_INSUFFICIENT_RESOURCES: |
189 RecordGetChannelIDResult(WORKER_FAILURE); | 210 RecordGetChannelIDResult(WORKER_FAILURE); |
190 break; | 211 break; |
191 default: | 212 default: |
192 RecordGetChannelIDResult(ASYNC_FAILURE_UNKNOWN); | 213 RecordGetChannelIDResult(ASYNC_FAILURE_UNKNOWN); |
193 break; | 214 break; |
194 } | 215 } |
195 if (!callback_.is_null()) { | 216 if (!callback_.is_null()) { |
196 *private_key_ = private_key; | 217 int err = |
197 *cert_ = cert; | 218 CreateECPrivateKeyFromSerializedKey(public_key, private_key, key_); |
219 if (error == OK && err != OK) | |
220 error = err; | |
198 callback_.Run(error); | 221 callback_.Run(error); |
199 } | 222 } |
200 delete this; | 223 delete this; |
201 } | 224 } |
202 | 225 |
203 bool canceled() const { return callback_.is_null(); } | 226 bool canceled() const { return callback_.is_null(); } |
204 | 227 |
205 private: | 228 private: |
206 base::TimeTicks request_start_; | 229 base::TimeTicks request_start_; |
207 CompletionCallback callback_; | 230 CompletionCallback callback_; |
208 std::string* private_key_; | 231 scoped_ptr<crypto::ECPrivateKey>* key_; |
209 std::string* cert_; | |
210 }; | 232 }; |
211 | 233 |
212 // ChannelIDServiceWorker runs on a worker thread and takes care of the | 234 // ChannelIDServiceWorker runs on a worker thread and takes care of the |
213 // blocking process of performing key generation. Will take care of deleting | 235 // blocking process of performing key generation. Will take care of deleting |
214 // itself once Start() is called. | 236 // itself once Start() is called. |
215 class ChannelIDServiceWorker { | 237 class ChannelIDServiceWorker { |
216 public: | 238 public: |
217 typedef base::Callback<void( | 239 typedef base::Callback<void( |
218 const std::string&, | 240 const std::string&, |
219 int, | 241 int, |
220 scoped_ptr<ChannelIDStore::ChannelID>)> WorkerDoneCallback; | 242 scoped_ptr<ChannelIDStore::ChannelID>)> WorkerDoneCallback; |
221 | 243 |
222 ChannelIDServiceWorker( | 244 ChannelIDServiceWorker( |
223 const std::string& server_identifier, | 245 const std::string& server_identifier, |
224 const WorkerDoneCallback& callback) | 246 const WorkerDoneCallback& callback) |
225 : server_identifier_(server_identifier), | 247 : server_identifier_(server_identifier), |
226 serial_number_(base::RandInt(0, std::numeric_limits<int>::max())), | |
227 origin_loop_(base::MessageLoopProxy::current()), | 248 origin_loop_(base::MessageLoopProxy::current()), |
228 callback_(callback) { | 249 callback_(callback) { |
229 } | 250 } |
230 | 251 |
231 // Starts the worker on |task_runner|. If the worker fails to start, such as | 252 // Starts the worker on |task_runner|. If the worker fails to start, such as |
232 // if the task runner is shutting down, then it will take care of deleting | 253 // if the task runner is shutting down, then it will take care of deleting |
233 // itself. | 254 // itself. |
234 bool Start(const scoped_refptr<base::TaskRunner>& task_runner) { | 255 bool Start(const scoped_refptr<base::TaskRunner>& task_runner) { |
235 DCHECK(origin_loop_->RunsTasksOnCurrentThread()); | 256 DCHECK(origin_loop_->RunsTasksOnCurrentThread()); |
236 | 257 |
237 return task_runner->PostTask( | 258 return task_runner->PostTask( |
238 FROM_HERE, | 259 FROM_HERE, |
239 base::Bind(&ChannelIDServiceWorker::Run, base::Owned(this))); | 260 base::Bind(&ChannelIDServiceWorker::Run, base::Owned(this))); |
240 } | 261 } |
241 | 262 |
242 private: | 263 private: |
243 void Run() { | 264 void Run() { |
244 // Runs on a worker thread. | 265 // Runs on a worker thread. |
245 int error = ERR_FAILED; | 266 int error = ERR_FAILED; |
246 scoped_ptr<ChannelIDStore::ChannelID> cert = | 267 scoped_ptr<ChannelIDStore::ChannelID> cert = |
247 GenerateChannelID(server_identifier_, serial_number_, &error); | 268 GenerateChannelID(server_identifier_, &error); |
248 DVLOG(1) << "GenerateCert " << server_identifier_ << " returned " << error; | 269 DVLOG(1) << "GenerateCert " << server_identifier_ << " returned " << error; |
249 #if defined(USE_NSS) | 270 #if defined(USE_NSS) |
250 // Detach the thread from NSPR. | 271 // Detach the thread from NSPR. |
251 // Calling NSS functions attaches the thread to NSPR, which stores | 272 // Calling NSS functions attaches the thread to NSPR, which stores |
252 // the NSPR thread ID in thread-specific data. | 273 // the NSPR thread ID in thread-specific data. |
253 // The threads in our thread pool terminate after we have called | 274 // The threads in our thread pool terminate after we have called |
254 // PR_Cleanup. Unless we detach them from NSPR, net_unittests gets | 275 // PR_Cleanup. Unless we detach them from NSPR, net_unittests gets |
255 // segfaults on shutdown when the threads' thread-specific data | 276 // segfaults on shutdown when the threads' thread-specific data |
256 // destructors run. | 277 // destructors run. |
257 PR_DetachThread(); | 278 PR_DetachThread(); |
258 #endif | 279 #endif |
259 origin_loop_->PostTask(FROM_HERE, | 280 origin_loop_->PostTask(FROM_HERE, |
260 base::Bind(callback_, server_identifier_, error, | 281 base::Bind(callback_, server_identifier_, error, |
261 base::Passed(&cert))); | 282 base::Passed(&cert))); |
262 } | 283 } |
263 | 284 |
264 const std::string server_identifier_; | 285 const std::string server_identifier_; |
265 // Note that serial_number_ must be initialized on a non-worker thread | |
266 // (see documentation for GenerateCert). | |
267 uint32 serial_number_; | |
268 scoped_refptr<base::SequencedTaskRunner> origin_loop_; | 286 scoped_refptr<base::SequencedTaskRunner> origin_loop_; |
269 WorkerDoneCallback callback_; | 287 WorkerDoneCallback callback_; |
270 | 288 |
271 DISALLOW_COPY_AND_ASSIGN(ChannelIDServiceWorker); | 289 DISALLOW_COPY_AND_ASSIGN(ChannelIDServiceWorker); |
272 }; | 290 }; |
273 | 291 |
274 // A ChannelIDServiceJob is a one-to-one counterpart of an | 292 // A ChannelIDServiceJob is a one-to-one counterpart of an |
275 // ChannelIDServiceWorker. It lives only on the ChannelIDService's | 293 // ChannelIDServiceWorker. It lives only on the ChannelIDService's |
276 // origin message loop. | 294 // origin message loop. |
277 class ChannelIDServiceJob { | 295 class ChannelIDServiceJob { |
278 public: | 296 public: |
279 ChannelIDServiceJob(bool create_if_missing) | 297 ChannelIDServiceJob(bool create_if_missing) |
280 : create_if_missing_(create_if_missing) { | 298 : create_if_missing_(create_if_missing) { |
281 } | 299 } |
282 | 300 |
283 ~ChannelIDServiceJob() { | 301 ~ChannelIDServiceJob() { |
284 if (!requests_.empty()) | 302 if (!requests_.empty()) |
285 DeleteAllCanceled(); | 303 DeleteAllCanceled(); |
286 } | 304 } |
287 | 305 |
288 void AddRequest(ChannelIDServiceRequest* request, | 306 void AddRequest(ChannelIDServiceRequest* request, |
289 bool create_if_missing = false) { | 307 bool create_if_missing = false) { |
290 create_if_missing_ |= create_if_missing; | 308 create_if_missing_ |= create_if_missing; |
291 requests_.push_back(request); | 309 requests_.push_back(request); |
292 } | 310 } |
293 | 311 |
294 void HandleResult(int error, | 312 void HandleResult(int error, |
295 const std::string& private_key, | 313 const std::string& private_key, |
296 const std::string& cert) { | 314 const std::string& public_key) { |
297 PostAll(error, private_key, cert); | 315 PostAll(error, private_key, public_key); |
298 } | 316 } |
299 | 317 |
300 bool CreateIfMissing() const { return create_if_missing_; } | 318 bool CreateIfMissing() const { return create_if_missing_; } |
301 | 319 |
302 private: | 320 private: |
303 void PostAll(int error, | 321 void PostAll(int error, |
304 const std::string& private_key, | 322 const std::string& private_key, |
305 const std::string& cert) { | 323 const std::string& public_key) { |
306 std::vector<ChannelIDServiceRequest*> requests; | 324 std::vector<ChannelIDServiceRequest*> requests; |
307 requests_.swap(requests); | 325 requests_.swap(requests); |
308 | 326 |
309 for (std::vector<ChannelIDServiceRequest*>::iterator | 327 for (std::vector<ChannelIDServiceRequest*>::iterator |
310 i = requests.begin(); i != requests.end(); i++) { | 328 i = requests.begin(); i != requests.end(); i++) { |
311 (*i)->Post(error, private_key, cert); | 329 (*i)->Post(error, private_key, public_key); |
312 // Post() causes the ChannelIDServiceRequest to delete itself. | 330 // Post() causes the ChannelIDServiceRequest to delete itself. |
313 } | 331 } |
314 } | 332 } |
315 | 333 |
316 void DeleteAllCanceled() { | 334 void DeleteAllCanceled() { |
317 for (std::vector<ChannelIDServiceRequest*>::iterator | 335 for (std::vector<ChannelIDServiceRequest*>::iterator |
318 i = requests_.begin(); i != requests_.end(); i++) { | 336 i = requests_.begin(); i != requests_.end(); i++) { |
319 if ((*i)->canceled()) { | 337 if ((*i)->canceled()) { |
320 delete *i; | 338 delete *i; |
321 } else { | 339 } else { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
363 // members afterwards. Reset callback_ first. | 381 // members afterwards. Reset callback_ first. |
364 base::ResetAndReturn(&callback_).Run(result); | 382 base::ResetAndReturn(&callback_).Run(result); |
365 } | 383 } |
366 | 384 |
367 ChannelIDService::ChannelIDService( | 385 ChannelIDService::ChannelIDService( |
368 ChannelIDStore* channel_id_store, | 386 ChannelIDStore* channel_id_store, |
369 const scoped_refptr<base::TaskRunner>& task_runner) | 387 const scoped_refptr<base::TaskRunner>& task_runner) |
370 : channel_id_store_(channel_id_store), | 388 : channel_id_store_(channel_id_store), |
371 task_runner_(task_runner), | 389 task_runner_(task_runner), |
372 requests_(0), | 390 requests_(0), |
373 cert_store_hits_(0), | 391 key_store_hits_(0), |
374 inflight_joins_(0), | 392 inflight_joins_(0), |
375 workers_created_(0), | 393 workers_created_(0), |
376 weak_ptr_factory_(this) { | 394 weak_ptr_factory_(this) { |
377 base::Time start = base::Time::Now(); | 395 base::Time start = base::Time::Now(); |
378 base::Time end = start + base::TimeDelta::FromDays( | 396 base::Time end = start + base::TimeDelta::FromDays( |
379 kValidityPeriodInDays + kSystemTimeValidityBufferInDays); | 397 kValidityPeriodInDays + kSystemTimeValidityBufferInDays); |
380 is_system_time_valid_ = x509_util::IsSupportedValidityRange(start, end); | 398 is_system_time_valid_ = x509_util::IsSupportedValidityRange(start, end); |
Ryan Sleevi
2015/04/15 22:50:02
None of this is needed, is it, now that we're no l
nharper
2015/04/25 02:59:19
Done.
| |
381 } | 399 } |
382 | 400 |
383 ChannelIDService::~ChannelIDService() { | 401 ChannelIDService::~ChannelIDService() { |
384 STLDeleteValues(&inflight_); | 402 STLDeleteValues(&inflight_); |
385 } | 403 } |
386 | 404 |
387 //static | 405 //static |
388 std::string ChannelIDService::GetDomainForHost(const std::string& host) { | 406 std::string ChannelIDService::GetDomainForHost(const std::string& host) { |
389 std::string domain = | 407 std::string domain = |
390 registry_controlled_domains::GetDomainAndRegistry( | 408 registry_controlled_domains::GetDomainAndRegistry( |
391 host, registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); | 409 host, registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); |
392 if (domain.empty()) | 410 if (domain.empty()) |
393 return host; | 411 return host; |
394 return domain; | 412 return domain; |
395 } | 413 } |
396 | 414 |
397 int ChannelIDService::GetOrCreateChannelID( | 415 int ChannelIDService::GetOrCreateChannelID( |
398 const std::string& host, | 416 const std::string& host, |
399 std::string* private_key, | 417 scoped_ptr<crypto::ECPrivateKey>* key, |
400 std::string* cert, | |
401 const CompletionCallback& callback, | 418 const CompletionCallback& callback, |
402 RequestHandle* out_req) { | 419 RequestHandle* out_req) { |
403 DVLOG(1) << __FUNCTION__ << " " << host; | 420 DVLOG(1) << __FUNCTION__ << " " << host; |
404 DCHECK(CalledOnValidThread()); | 421 DCHECK(CalledOnValidThread()); |
405 base::TimeTicks request_start = base::TimeTicks::Now(); | 422 base::TimeTicks request_start = base::TimeTicks::Now(); |
406 | 423 |
407 if (callback.is_null() || !private_key || !cert || host.empty()) { | 424 if (callback.is_null() || !key || host.empty()) { |
408 RecordGetChannelIDResult(INVALID_ARGUMENT); | 425 RecordGetChannelIDResult(INVALID_ARGUMENT); |
409 return ERR_INVALID_ARGUMENT; | 426 return ERR_INVALID_ARGUMENT; |
410 } | 427 } |
411 | 428 |
412 std::string domain = GetDomainForHost(host); | 429 std::string domain = GetDomainForHost(host); |
413 if (domain.empty()) { | 430 if (domain.empty()) { |
414 RecordGetChannelIDResult(INVALID_ARGUMENT); | 431 RecordGetChannelIDResult(INVALID_ARGUMENT); |
415 return ERR_INVALID_ARGUMENT; | 432 return ERR_INVALID_ARGUMENT; |
416 } | 433 } |
417 | 434 |
418 requests_++; | 435 requests_++; |
419 | 436 |
437 std::string public_key; | |
438 std::string private_key; | |
Ryan Sleevi
2015/04/15 22:50:02
These two are unused, right?
nharper
2015/04/25 02:59:19
Correct. Removed.
| |
439 | |
420 // See if a request for the same domain is currently in flight. | 440 // See if a request for the same domain is currently in flight. |
421 bool create_if_missing = true; | 441 bool create_if_missing = true; |
422 if (JoinToInFlightRequest(request_start, domain, private_key, cert, | 442 if (JoinToInFlightRequest(request_start, domain, key, create_if_missing, |
423 create_if_missing, callback, out_req)) { | 443 callback, out_req)) { |
424 return ERR_IO_PENDING; | 444 return ERR_IO_PENDING; |
425 } | 445 } |
426 | 446 |
427 int err = LookupChannelID(request_start, domain, private_key, cert, | 447 int err = LookupChannelID(request_start, domain, key, create_if_missing, |
428 create_if_missing, callback, out_req); | 448 callback, out_req); |
429 if (err == ERR_FILE_NOT_FOUND) { | 449 if (err == ERR_FILE_NOT_FOUND) { |
430 // Sync lookup did not find a valid cert. Start generating a new one. | 450 // Sync lookup did not find a valid cert. Start generating a new one. |
431 workers_created_++; | 451 workers_created_++; |
432 ChannelIDServiceWorker* worker = new ChannelIDServiceWorker( | 452 ChannelIDServiceWorker* worker = new ChannelIDServiceWorker( |
433 domain, | 453 domain, |
434 base::Bind(&ChannelIDService::GeneratedChannelID, | 454 base::Bind(&ChannelIDService::GeneratedChannelID, |
435 weak_ptr_factory_.GetWeakPtr())); | 455 weak_ptr_factory_.GetWeakPtr())); |
436 if (!worker->Start(task_runner_)) { | 456 if (!worker->Start(task_runner_)) { |
437 // TODO(rkn): Log to the NetLog. | 457 // TODO(rkn): Log to the NetLog. |
438 LOG(ERROR) << "ChannelIDServiceWorker couldn't be started."; | 458 LOG(ERROR) << "ChannelIDServiceWorker couldn't be started."; |
439 RecordGetChannelIDResult(WORKER_FAILURE); | 459 RecordGetChannelIDResult(WORKER_FAILURE); |
440 return ERR_INSUFFICIENT_RESOURCES; | 460 return ERR_INSUFFICIENT_RESOURCES; |
441 } | 461 } |
442 // We are waiting for cert generation. Create a job & request to track it. | 462 // We are waiting for cert generation. Create a job & request to track it. |
443 ChannelIDServiceJob* job = new ChannelIDServiceJob(create_if_missing); | 463 ChannelIDServiceJob* job = new ChannelIDServiceJob(create_if_missing); |
444 inflight_[domain] = job; | 464 inflight_[domain] = job; |
445 | 465 |
446 ChannelIDServiceRequest* request = new ChannelIDServiceRequest( | 466 ChannelIDServiceRequest* request = new ChannelIDServiceRequest( |
447 request_start, | 467 request_start, base::Bind(&RequestHandle::OnRequestComplete, |
448 base::Bind(&RequestHandle::OnRequestComplete, | 468 base::Unretained(out_req)), |
449 base::Unretained(out_req)), | 469 key); |
450 private_key, | |
451 cert); | |
452 job->AddRequest(request); | 470 job->AddRequest(request); |
453 out_req->RequestStarted(this, request, callback); | 471 out_req->RequestStarted(this, request, callback); |
454 return ERR_IO_PENDING; | 472 return ERR_IO_PENDING; |
455 } | 473 } |
456 | 474 |
457 return err; | 475 return err; |
458 } | 476 } |
459 | 477 |
460 int ChannelIDService::GetChannelID( | 478 int ChannelIDService::GetChannelID(const std::string& host, |
461 const std::string& host, | 479 scoped_ptr<crypto::ECPrivateKey>* key, |
462 std::string* private_key, | 480 const CompletionCallback& callback, |
463 std::string* cert, | 481 RequestHandle* out_req) { |
464 const CompletionCallback& callback, | |
465 RequestHandle* out_req) { | |
466 DVLOG(1) << __FUNCTION__ << " " << host; | 482 DVLOG(1) << __FUNCTION__ << " " << host; |
467 DCHECK(CalledOnValidThread()); | 483 DCHECK(CalledOnValidThread()); |
468 base::TimeTicks request_start = base::TimeTicks::Now(); | 484 base::TimeTicks request_start = base::TimeTicks::Now(); |
469 | 485 |
470 if (callback.is_null() || !private_key || !cert || host.empty()) { | 486 if (callback.is_null() || !key || host.empty()) { |
471 RecordGetChannelIDResult(INVALID_ARGUMENT); | 487 RecordGetChannelIDResult(INVALID_ARGUMENT); |
472 return ERR_INVALID_ARGUMENT; | 488 return ERR_INVALID_ARGUMENT; |
473 } | 489 } |
474 | 490 |
475 std::string domain = GetDomainForHost(host); | 491 std::string domain = GetDomainForHost(host); |
476 if (domain.empty()) { | 492 if (domain.empty()) { |
477 RecordGetChannelIDResult(INVALID_ARGUMENT); | 493 RecordGetChannelIDResult(INVALID_ARGUMENT); |
478 return ERR_INVALID_ARGUMENT; | 494 return ERR_INVALID_ARGUMENT; |
479 } | 495 } |
480 | 496 |
481 requests_++; | 497 requests_++; |
482 | 498 |
483 // See if a request for the same domain currently in flight. | 499 // See if a request for the same domain currently in flight. |
484 bool create_if_missing = false; | 500 bool create_if_missing = false; |
485 if (JoinToInFlightRequest(request_start, domain, private_key, cert, | 501 if (JoinToInFlightRequest(request_start, domain, key, create_if_missing, |
486 create_if_missing, callback, out_req)) { | 502 callback, out_req)) { |
487 return ERR_IO_PENDING; | 503 return ERR_IO_PENDING; |
488 } | 504 } |
489 | 505 |
490 int err = LookupChannelID(request_start, domain, private_key, cert, | 506 int err = LookupChannelID(request_start, domain, key, create_if_missing, |
491 create_if_missing, callback, out_req); | 507 callback, out_req); |
492 return err; | 508 return err; |
493 } | 509 } |
494 | 510 |
495 void ChannelIDService::GotChannelID( | 511 void ChannelIDService::GotChannelID(int err, |
496 int err, | 512 const std::string& server_identifier, |
497 const std::string& server_identifier, | 513 const std::string& private_key, |
498 base::Time expiration_time, | 514 const std::string& public_key) { |
499 const std::string& key, | |
500 const std::string& cert) { | |
501 DCHECK(CalledOnValidThread()); | 515 DCHECK(CalledOnValidThread()); |
502 | 516 |
503 std::map<std::string, ChannelIDServiceJob*>::iterator j; | 517 std::map<std::string, ChannelIDServiceJob*>::iterator j; |
504 j = inflight_.find(server_identifier); | 518 j = inflight_.find(server_identifier); |
505 if (j == inflight_.end()) { | 519 if (j == inflight_.end()) { |
506 NOTREACHED(); | 520 NOTREACHED(); |
507 return; | 521 return; |
508 } | 522 } |
509 | 523 |
510 if (err == OK) { | 524 if (err == OK) { |
511 // Async DB lookup found a valid cert. | 525 // Async DB lookup found a valid cert. |
512 DVLOG(1) << "Cert store had valid cert for " << server_identifier; | 526 DVLOG(1) << "Cert store had valid key for " << server_identifier; |
513 cert_store_hits_++; | 527 key_store_hits_++; |
514 // ChannelIDServiceRequest::Post will do the histograms and stuff. | 528 // ChannelIDServiceRequest::Post will do the histograms and stuff. |
515 HandleResult(OK, server_identifier, key, cert); | 529 HandleResult(OK, server_identifier, private_key, public_key); |
516 return; | 530 return; |
517 } | 531 } |
518 // Async lookup failed or the certificate was missing. Return the error | 532 // Async lookup failed or the certificate was missing. Return the error |
519 // directly, unless the certificate was missing and a request asked to create | 533 // directly, unless the certificate was missing and a request asked to create |
520 // one. | 534 // one. |
521 if (err != ERR_FILE_NOT_FOUND || !j->second->CreateIfMissing()) { | 535 if (err != ERR_FILE_NOT_FOUND || !j->second->CreateIfMissing()) { |
522 HandleResult(err, server_identifier, key, cert); | 536 HandleResult(err, server_identifier, private_key, public_key); |
523 return; | 537 return; |
524 } | 538 } |
525 // At least one request asked to create a cert => start generating a new one. | 539 // At least one request asked to create a cert => start generating a new one. |
526 workers_created_++; | 540 workers_created_++; |
527 ChannelIDServiceWorker* worker = new ChannelIDServiceWorker( | 541 ChannelIDServiceWorker* worker = new ChannelIDServiceWorker( |
528 server_identifier, | 542 server_identifier, |
529 base::Bind(&ChannelIDService::GeneratedChannelID, | 543 base::Bind(&ChannelIDService::GeneratedChannelID, |
530 weak_ptr_factory_.GetWeakPtr())); | 544 weak_ptr_factory_.GetWeakPtr())); |
531 if (!worker->Start(task_runner_)) { | 545 if (!worker->Start(task_runner_)) { |
532 // TODO(rkn): Log to the NetLog. | 546 // TODO(rkn): Log to the NetLog. |
(...skipping 16 matching lines...) Expand all Loading... | |
549 | 563 |
550 void ChannelIDService::GeneratedChannelID( | 564 void ChannelIDService::GeneratedChannelID( |
551 const std::string& server_identifier, | 565 const std::string& server_identifier, |
552 int error, | 566 int error, |
553 scoped_ptr<ChannelIDStore::ChannelID> cert) { | 567 scoped_ptr<ChannelIDStore::ChannelID> cert) { |
554 DCHECK(CalledOnValidThread()); | 568 DCHECK(CalledOnValidThread()); |
555 | 569 |
556 if (error == OK) { | 570 if (error == OK) { |
557 // TODO(mattm): we should just Pass() the cert object to | 571 // TODO(mattm): we should just Pass() the cert object to |
558 // SetChannelID(). | 572 // SetChannelID(). |
559 channel_id_store_->SetChannelID( | 573 channel_id_store_->SetChannelID(cert->server_identifier(), |
560 cert->server_identifier(), | 574 cert->creation_time(), cert->private_key(), |
561 cert->creation_time(), | 575 cert->public_key()); |
562 cert->expiration_time(), | |
563 cert->private_key(), | |
564 cert->cert()); | |
565 | 576 |
566 HandleResult(error, server_identifier, cert->private_key(), cert->cert()); | 577 HandleResult(error, server_identifier, cert->private_key(), |
578 cert->public_key()); | |
567 } else { | 579 } else { |
568 HandleResult(error, server_identifier, std::string(), std::string()); | 580 HandleResult(error, server_identifier, std::string(), std::string()); |
569 } | 581 } |
570 } | 582 } |
571 | 583 |
572 void ChannelIDService::HandleResult( | 584 void ChannelIDService::HandleResult(int error, |
573 int error, | 585 const std::string& server_identifier, |
574 const std::string& server_identifier, | 586 const std::string& private_key, |
575 const std::string& private_key, | 587 const std::string& public_key) { |
576 const std::string& cert) { | |
577 DCHECK(CalledOnValidThread()); | 588 DCHECK(CalledOnValidThread()); |
578 | 589 |
579 std::map<std::string, ChannelIDServiceJob*>::iterator j; | 590 std::map<std::string, ChannelIDServiceJob*>::iterator j; |
580 j = inflight_.find(server_identifier); | 591 j = inflight_.find(server_identifier); |
581 if (j == inflight_.end()) { | 592 if (j == inflight_.end()) { |
582 NOTREACHED(); | 593 NOTREACHED(); |
583 return; | 594 return; |
584 } | 595 } |
585 ChannelIDServiceJob* job = j->second; | 596 ChannelIDServiceJob* job = j->second; |
586 inflight_.erase(j); | 597 inflight_.erase(j); |
587 | 598 |
588 job->HandleResult(error, private_key, cert); | 599 job->HandleResult(error, private_key, public_key); |
589 delete job; | 600 delete job; |
590 } | 601 } |
591 | 602 |
592 bool ChannelIDService::JoinToInFlightRequest( | 603 bool ChannelIDService::JoinToInFlightRequest( |
593 const base::TimeTicks& request_start, | 604 const base::TimeTicks& request_start, |
594 const std::string& domain, | 605 const std::string& domain, |
595 std::string* private_key, | 606 scoped_ptr<crypto::ECPrivateKey>* key, |
596 std::string* cert, | |
597 bool create_if_missing, | 607 bool create_if_missing, |
598 const CompletionCallback& callback, | 608 const CompletionCallback& callback, |
599 RequestHandle* out_req) { | 609 RequestHandle* out_req) { |
600 ChannelIDServiceJob* job = NULL; | 610 ChannelIDServiceJob* job = NULL; |
601 std::map<std::string, ChannelIDServiceJob*>::const_iterator j = | 611 std::map<std::string, ChannelIDServiceJob*>::const_iterator j = |
602 inflight_.find(domain); | 612 inflight_.find(domain); |
603 if (j != inflight_.end()) { | 613 if (j != inflight_.end()) { |
604 // A request for the same domain is in flight already. We'll attach our | 614 // A request for the same domain is in flight already. We'll attach our |
605 // callback, but we'll also mark it as requiring a cert if one's mising. | 615 // callback, but we'll also mark it as requiring a cert if one's mising. |
606 job = j->second; | 616 job = j->second; |
607 inflight_joins_++; | 617 inflight_joins_++; |
608 | 618 |
609 ChannelIDServiceRequest* request = new ChannelIDServiceRequest( | 619 ChannelIDServiceRequest* request = new ChannelIDServiceRequest( |
610 request_start, | 620 request_start, base::Bind(&RequestHandle::OnRequestComplete, |
611 base::Bind(&RequestHandle::OnRequestComplete, | 621 base::Unretained(out_req)), |
612 base::Unretained(out_req)), | 622 key); |
613 private_key, | |
614 cert); | |
615 job->AddRequest(request, create_if_missing); | 623 job->AddRequest(request, create_if_missing); |
616 out_req->RequestStarted(this, request, callback); | 624 out_req->RequestStarted(this, request, callback); |
617 return true; | 625 return true; |
618 } | 626 } |
619 return false; | 627 return false; |
620 } | 628 } |
621 | 629 |
622 int ChannelIDService::LookupChannelID( | 630 int ChannelIDService::LookupChannelID(const base::TimeTicks& request_start, |
623 const base::TimeTicks& request_start, | 631 const std::string& domain, |
624 const std::string& domain, | 632 scoped_ptr<crypto::ECPrivateKey>* key, |
625 std::string* private_key, | 633 bool create_if_missing, |
626 std::string* cert, | 634 const CompletionCallback& callback, |
627 bool create_if_missing, | 635 RequestHandle* out_req) { |
628 const CompletionCallback& callback, | |
629 RequestHandle* out_req) { | |
630 // Check if a domain bound cert already exists for this domain. Note that | 636 // Check if a domain bound cert already exists for this domain. Note that |
631 // |expiration_time| is ignored, and expired certs are considered valid. | 637 // |expiration_time| is ignored, and expired certs are considered valid. |
632 base::Time expiration_time; | 638 std::string public_key; |
639 std::string private_key; | |
633 int err = channel_id_store_->GetChannelID( | 640 int err = channel_id_store_->GetChannelID( |
634 domain, | 641 domain, &private_key, &public_key, |
635 &expiration_time /* ignored */, | |
636 private_key, | |
637 cert, | |
638 base::Bind(&ChannelIDService::GotChannelID, | 642 base::Bind(&ChannelIDService::GotChannelID, |
639 weak_ptr_factory_.GetWeakPtr())); | 643 weak_ptr_factory_.GetWeakPtr())); |
640 | 644 |
641 if (err == OK) { | 645 if (err == OK) { |
646 err = CreateECPrivateKeyFromSerializedKey(public_key, private_key, key); | |
647 if (err != OK) { | |
648 return err; | |
649 } | |
642 // Sync lookup found a valid cert. | 650 // Sync lookup found a valid cert. |
643 DVLOG(1) << "Cert store had valid cert for " << domain; | 651 DVLOG(1) << "Cert store had valid key for " << domain; |
644 cert_store_hits_++; | 652 key_store_hits_++; |
645 RecordGetChannelIDResult(SYNC_SUCCESS); | 653 RecordGetChannelIDResult(SYNC_SUCCESS); |
646 base::TimeDelta request_time = base::TimeTicks::Now() - request_start; | 654 base::TimeDelta request_time = base::TimeTicks::Now() - request_start; |
647 UMA_HISTOGRAM_TIMES("DomainBoundCerts.GetCertTimeSync", request_time); | 655 UMA_HISTOGRAM_TIMES("DomainBoundCerts.GetCertTimeSync", request_time); |
648 RecordGetChannelIDTime(request_time); | 656 RecordGetChannelIDTime(request_time); |
649 return OK; | 657 return OK; |
650 } | 658 } |
651 | 659 |
652 if (err == ERR_IO_PENDING) { | 660 if (err == ERR_IO_PENDING) { |
653 // We are waiting for async DB lookup. Create a job & request to track it. | 661 // We are waiting for async DB lookup. Create a job & request to track it. |
654 ChannelIDServiceJob* job = new ChannelIDServiceJob(create_if_missing); | 662 ChannelIDServiceJob* job = new ChannelIDServiceJob(create_if_missing); |
655 inflight_[domain] = job; | 663 inflight_[domain] = job; |
656 | 664 |
657 ChannelIDServiceRequest* request = new ChannelIDServiceRequest( | 665 ChannelIDServiceRequest* request = new ChannelIDServiceRequest( |
658 request_start, | 666 request_start, base::Bind(&RequestHandle::OnRequestComplete, |
659 base::Bind(&RequestHandle::OnRequestComplete, | 667 base::Unretained(out_req)), |
660 base::Unretained(out_req)), | 668 key); |
661 private_key, | |
662 cert); | |
663 job->AddRequest(request); | 669 job->AddRequest(request); |
664 out_req->RequestStarted(this, request, callback); | 670 out_req->RequestStarted(this, request, callback); |
665 return ERR_IO_PENDING; | 671 return ERR_IO_PENDING; |
666 } | 672 } |
667 | 673 |
668 return err; | 674 return err; |
669 } | 675 } |
670 | 676 |
671 int ChannelIDService::cert_count() { | 677 int ChannelIDService::cert_count() { |
672 return channel_id_store_->GetChannelIDCount(); | 678 return channel_id_store_->GetChannelIDCount(); |
673 } | 679 } |
674 | 680 |
675 } // namespace net | 681 } // namespace net |
OLD | NEW |