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

Side by Side Diff: net/ssl/channel_id_service.cc

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

Powered by Google App Engine
This is Rietveld 408576698