| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/cert/multi_threaded_cert_verifier.h" | 5 #include "net/cert/multi_threaded_cert_verifier.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 // destructors run. | 251 // destructors run. |
| 252 PR_DetachThread(); | 252 PR_DetachThread(); |
| 253 #endif | 253 #endif |
| 254 } | 254 } |
| 255 | 255 |
| 256 // CertVerifierJob lives only on the verifier's origin message loop. | 256 // CertVerifierJob lives only on the verifier's origin message loop. |
| 257 class CertVerifierJob { | 257 class CertVerifierJob { |
| 258 public: | 258 public: |
| 259 CertVerifierJob(const CertVerifier::RequestParams& key, | 259 CertVerifierJob(const CertVerifier::RequestParams& key, |
| 260 NetLog* net_log, | 260 NetLog* net_log, |
| 261 X509Certificate* cert, | |
| 262 MultiThreadedCertVerifier* cert_verifier) | 261 MultiThreadedCertVerifier* cert_verifier) |
| 263 : key_(key), | 262 : key_(key), |
| 264 start_time_(base::TimeTicks::Now()), | 263 start_time_(base::TimeTicks::Now()), |
| 265 wall_start_time_(base::Time::Now()), | 264 wall_start_time_(base::Time::Now()), |
| 266 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_CERT_VERIFIER_JOB)), | 265 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_CERT_VERIFIER_JOB)), |
| 267 cert_verifier_(cert_verifier), | 266 cert_verifier_(cert_verifier), |
| 268 is_first_job_(false), | 267 is_first_job_(false), |
| 269 weak_ptr_factory_(this) { | 268 weak_ptr_factory_(this) { |
| 270 net_log_.BeginEvent( | 269 net_log_.BeginEvent(NetLog::TYPE_CERT_VERIFIER_JOB, |
| 271 NetLog::TYPE_CERT_VERIFIER_JOB, | 270 base::Bind(&NetLogX509CertificateCallback, |
| 272 base::Bind(&NetLogX509CertificateCallback, base::Unretained(cert))); | 271 base::Unretained(key.certificate().get()))); |
| 273 } | 272 } |
| 274 | 273 |
| 275 // Indicates whether this was the first job started by the CertVerifier. This | 274 // Indicates whether this was the first job started by the CertVerifier. This |
| 276 // is only used for logging certain UMA stats. | 275 // is only used for logging certain UMA stats. |
| 277 void set_is_first_job(bool is_first_job) { is_first_job_ = is_first_job; } | 276 void set_is_first_job(bool is_first_job) { is_first_job_ = is_first_job; } |
| 278 | 277 |
| 279 const CertVerifier::RequestParams& key() const { return key_; } | 278 const CertVerifier::RequestParams& key() const { return key_; } |
| 280 | 279 |
| 281 // Posts a task to the worker pool to do the verification. Once the | 280 // Posts a task to the worker pool to do the verification. Once the |
| 282 // verification has completed on the worker thread, it will call | 281 // verification has completed on the worker thread, it will call |
| 283 // OnJobCompleted() on the origin thread. | 282 // OnJobCompleted() on the origin thread. |
| 284 bool Start(const scoped_refptr<CertVerifyProc>& verify_proc, | 283 bool Start(const scoped_refptr<CertVerifyProc>& verify_proc, |
| 285 const scoped_refptr<X509Certificate>& cert, | 284 const scoped_refptr<CRLSet>& crl_set) { |
| 286 const std::string& hostname, | |
| 287 const std::string& ocsp_response, | |
| 288 int flags, | |
| 289 const scoped_refptr<CRLSet>& crl_set, | |
| 290 const CertificateList& additional_trust_anchors) { | |
| 291 // Owned by the bound reply callback. | 285 // Owned by the bound reply callback. |
| 292 std::unique_ptr<MultiThreadedCertVerifier::CachedResult> owned_result( | 286 std::unique_ptr<MultiThreadedCertVerifier::CachedResult> owned_result( |
| 293 new MultiThreadedCertVerifier::CachedResult()); | 287 new MultiThreadedCertVerifier::CachedResult()); |
| 294 | 288 |
| 295 // Parameter evaluation order is undefined in C++. Ensure the pointer value | 289 // Parameter evaluation order is undefined in C++. Ensure the pointer value |
| 296 // is gotten before calling base::Passed(). | 290 // is gotten before calling base::Passed(). |
| 297 auto result = owned_result.get(); | 291 auto result = owned_result.get(); |
| 298 | 292 |
| 299 return base::WorkerPool::PostTaskAndReply( | 293 return base::WorkerPool::PostTaskAndReply( |
| 300 FROM_HERE, | 294 FROM_HERE, |
| 301 base::Bind(&DoVerifyOnWorkerThread, verify_proc, cert, hostname, | 295 base::Bind(&DoVerifyOnWorkerThread, verify_proc, key_.certificate(), |
| 302 ocsp_response, flags, crl_set, additional_trust_anchors, | 296 key_.hostname(), key_.ocsp_response(), key_.flags(), crl_set, |
| 303 &result->error, &result->result), | 297 key_.additional_trust_anchors(), &result->error, |
| 298 &result->result), |
| 304 base::Bind(&CertVerifierJob::OnJobCompleted, | 299 base::Bind(&CertVerifierJob::OnJobCompleted, |
| 305 weak_ptr_factory_.GetWeakPtr(), base::Passed(&owned_result)), | 300 weak_ptr_factory_.GetWeakPtr(), base::Passed(&owned_result)), |
| 306 true /* task is slow */); | 301 true /* task is slow */); |
| 307 } | 302 } |
| 308 | 303 |
| 309 ~CertVerifierJob() { | 304 ~CertVerifierJob() { |
| 310 // If the job is in progress, cancel it. | 305 // If the job is in progress, cancel it. |
| 311 if (cert_verifier_) { | 306 if (cert_verifier_) { |
| 312 cert_verifier_ = nullptr; | 307 cert_verifier_ = nullptr; |
| 313 | 308 |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 416 STLDeleteElements(&inflight_); | 411 STLDeleteElements(&inflight_); |
| 417 CertDatabase::GetInstance()->RemoveObserver(this); | 412 CertDatabase::GetInstance()->RemoveObserver(this); |
| 418 } | 413 } |
| 419 | 414 |
| 420 void MultiThreadedCertVerifier::SetCertTrustAnchorProvider( | 415 void MultiThreadedCertVerifier::SetCertTrustAnchorProvider( |
| 421 CertTrustAnchorProvider* trust_anchor_provider) { | 416 CertTrustAnchorProvider* trust_anchor_provider) { |
| 422 DCHECK(CalledOnValidThread()); | 417 DCHECK(CalledOnValidThread()); |
| 423 trust_anchor_provider_ = trust_anchor_provider; | 418 trust_anchor_provider_ = trust_anchor_provider; |
| 424 } | 419 } |
| 425 | 420 |
| 426 int MultiThreadedCertVerifier::Verify(X509Certificate* cert, | 421 int MultiThreadedCertVerifier::Verify(const RequestParams& params, |
| 427 const std::string& hostname, | |
| 428 const std::string& ocsp_response, | |
| 429 int flags, | |
| 430 CRLSet* crl_set, | 422 CRLSet* crl_set, |
| 431 CertVerifyResult* verify_result, | 423 CertVerifyResult* verify_result, |
| 432 const CompletionCallback& callback, | 424 const CompletionCallback& callback, |
| 433 std::unique_ptr<Request>* out_req, | 425 std::unique_ptr<Request>* out_req, |
| 434 const BoundNetLog& net_log) { | 426 const BoundNetLog& net_log) { |
| 435 out_req->reset(); | 427 out_req->reset(); |
| 436 | 428 |
| 437 DCHECK(CalledOnValidThread()); | 429 DCHECK(CalledOnValidThread()); |
| 438 | 430 |
| 439 if (callback.is_null() || !verify_result || hostname.empty()) | 431 if (callback.is_null() || !verify_result || params.hostname().empty()) |
| 440 return ERR_INVALID_ARGUMENT; | 432 return ERR_INVALID_ARGUMENT; |
| 441 | 433 |
| 442 requests_++; | 434 requests_++; |
| 443 | 435 |
| 444 const CertificateList empty_cert_list; | 436 CertificateList new_trust_anchors(params.additional_trust_anchors()); |
| 445 const CertificateList& additional_trust_anchors = | 437 if (trust_anchor_provider_) { |
| 446 trust_anchor_provider_ ? | 438 const CertificateList& trust_anchors = |
| 447 trust_anchor_provider_->GetAdditionalTrustAnchors() : empty_cert_list; | 439 trust_anchor_provider_->GetAdditionalTrustAnchors(); |
| 440 new_trust_anchors.insert(new_trust_anchors.end(), trust_anchors.begin(), |
| 441 trust_anchors.end()); |
| 442 } |
| 448 | 443 |
| 449 const CertVerifier::RequestParams key(cert, hostname, flags, ocsp_response, | 444 const RequestParams key(params.certificate(), params.hostname(), |
| 450 additional_trust_anchors); | 445 params.flags(), params.ocsp_response(), |
| 446 new_trust_anchors); |
| 451 const CertVerifierCache::value_type* cached_entry = | 447 const CertVerifierCache::value_type* cached_entry = |
| 452 cache_.Get(key, CacheValidityPeriod(base::Time::Now())); | 448 cache_.Get(key, CacheValidityPeriod(base::Time::Now())); |
| 453 if (cached_entry) { | 449 if (cached_entry) { |
| 454 ++cache_hits_; | 450 ++cache_hits_; |
| 455 *verify_result = cached_entry->result; | 451 *verify_result = cached_entry->result; |
| 456 return cached_entry->error; | 452 return cached_entry->error; |
| 457 } | 453 } |
| 458 | 454 |
| 459 // No cache hit. See if an identical request is currently in flight. | 455 // No cache hit. See if an identical request is currently in flight. |
| 460 CertVerifierJob* job = FindJob(key); | 456 CertVerifierJob* job = FindJob(key); |
| 461 if (job) { | 457 if (job) { |
| 462 // An identical request is in flight already. We'll just attach our | 458 // An identical request is in flight already. We'll just attach our |
| 463 // callback. | 459 // callback. |
| 464 inflight_joins_++; | 460 inflight_joins_++; |
| 465 } else { | 461 } else { |
| 466 // Need to make a new job. | 462 // Need to make a new job. |
| 467 std::unique_ptr<CertVerifierJob> new_job( | 463 std::unique_ptr<CertVerifierJob> new_job( |
| 468 new CertVerifierJob(key, net_log.net_log(), cert, this)); | 464 new CertVerifierJob(key, net_log.net_log(), this)); |
| 469 | 465 |
| 470 if (!new_job->Start(verify_proc_, cert, hostname, ocsp_response, flags, | 466 if (!new_job->Start(verify_proc_, crl_set)) { |
| 471 crl_set, additional_trust_anchors)) { | |
| 472 // TODO(wtc): log to the NetLog. | 467 // TODO(wtc): log to the NetLog. |
| 473 LOG(ERROR) << "CertVerifierJob couldn't be started."; | 468 LOG(ERROR) << "CertVerifierJob couldn't be started."; |
| 474 return ERR_INSUFFICIENT_RESOURCES; // Just a guess. | 469 return ERR_INSUFFICIENT_RESOURCES; // Just a guess. |
| 475 } | 470 } |
| 476 | 471 |
| 477 job = new_job.release(); | 472 job = new_job.release(); |
| 478 inflight_.insert(job); | 473 inflight_.insert(job); |
| 479 | 474 |
| 480 if (requests_ == 1) | 475 if (requests_ == 1) |
| 481 job->set_is_first_job(true); | 476 job->set_is_first_job(true); |
| 482 } | 477 } |
| 483 | 478 |
| 484 std::unique_ptr<CertVerifierRequest> request = | 479 std::unique_ptr<CertVerifierRequest> request = |
| 485 job->CreateRequest(callback, verify_result, net_log); | 480 job->CreateRequest(callback, verify_result, net_log); |
| 486 *out_req = std::move(request); | 481 *out_req = std::move(request); |
| 487 return ERR_IO_PENDING; | 482 return ERR_IO_PENDING; |
| 488 } | 483 } |
| 489 | 484 |
| 490 bool MultiThreadedCertVerifier::SupportsOCSPStapling() { | 485 bool MultiThreadedCertVerifier::SupportsOCSPStapling() { |
| 491 return verify_proc_->SupportsOCSPStapling(); | 486 return verify_proc_->SupportsOCSPStapling(); |
| 492 } | 487 } |
| 493 | 488 |
| 494 bool MultiThreadedCertVerifier::JobComparator::operator()( | 489 bool MultiThreadedCertVerifier::JobComparator::operator()( |
| 495 const CertVerifierJob* job1, | 490 const CertVerifierJob* job1, |
| 496 const CertVerifierJob* job2) const { | 491 const CertVerifierJob* job2) const { |
| 497 return job1->key() < job2->key(); | 492 return job1->key() < job2->key(); |
| 498 } | 493 } |
| 499 | 494 |
| 500 void MultiThreadedCertVerifier::SaveResultToCache( | 495 void MultiThreadedCertVerifier::SaveResultToCache(const RequestParams& key, |
| 501 const CertVerifier::RequestParams& key, | 496 const base::Time& start_time, |
| 502 const base::Time& start_time, | 497 const CachedResult& result) { |
| 503 const CachedResult& result) { | |
| 504 DCHECK(CalledOnValidThread()); | 498 DCHECK(CalledOnValidThread()); |
| 505 | 499 |
| 506 // When caching, this uses the time that validation started as the | 500 // When caching, this uses the time that validation started as the |
| 507 // beginning of the validity, rather than the time that it ended (aka | 501 // beginning of the validity, rather than the time that it ended (aka |
| 508 // base::Time::Now()), to account for the fact that during validation, | 502 // base::Time::Now()), to account for the fact that during validation, |
| 509 // the clock may have changed. | 503 // the clock may have changed. |
| 510 // | 504 // |
| 511 // If the clock has changed significantly, then this result will ideally | 505 // If the clock has changed significantly, then this result will ideally |
| 512 // be evicted and the next time the certificate is encountered, it will | 506 // be evicted and the next time the certificate is encountered, it will |
| 513 // be revalidated. | 507 // be revalidated. |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 547 ClearCache(); | 541 ClearCache(); |
| 548 } | 542 } |
| 549 | 543 |
| 550 struct MultiThreadedCertVerifier::JobToRequestParamsComparator { | 544 struct MultiThreadedCertVerifier::JobToRequestParamsComparator { |
| 551 bool operator()(const CertVerifierJob* job, | 545 bool operator()(const CertVerifierJob* job, |
| 552 const CertVerifier::RequestParams& value) const { | 546 const CertVerifier::RequestParams& value) const { |
| 553 return job->key() < value; | 547 return job->key() < value; |
| 554 } | 548 } |
| 555 }; | 549 }; |
| 556 | 550 |
| 557 CertVerifierJob* MultiThreadedCertVerifier::FindJob( | 551 CertVerifierJob* MultiThreadedCertVerifier::FindJob(const RequestParams& key) { |
| 558 const CertVerifier::RequestParams& key) { | |
| 559 DCHECK(CalledOnValidThread()); | 552 DCHECK(CalledOnValidThread()); |
| 560 | 553 |
| 561 // The JobSet is kept in sorted order so items can be found using binary | 554 // The JobSet is kept in sorted order so items can be found using binary |
| 562 // search. | 555 // search. |
| 563 auto it = std::lower_bound(inflight_.begin(), inflight_.end(), key, | 556 auto it = std::lower_bound(inflight_.begin(), inflight_.end(), key, |
| 564 JobToRequestParamsComparator()); | 557 JobToRequestParamsComparator()); |
| 565 if (it != inflight_.end() && !(key < (*it)->key())) | 558 if (it != inflight_.end() && !(key < (*it)->key())) |
| 566 return *it; | 559 return *it; |
| 567 return nullptr; | 560 return nullptr; |
| 568 } | 561 } |
| 569 | 562 |
| 570 } // namespace net | 563 } // namespace net |
| OLD | NEW |