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