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/base/multi_threaded_cert_verifier.h" | 5 #include "net/base/multi_threaded_cert_verifier.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
13 #include "base/synchronization/lock.h" | 13 #include "base/synchronization/lock.h" |
14 #include "base/time.h" | 14 #include "base/time.h" |
15 #include "base/threading/worker_pool.h" | 15 #include "base/threading/worker_pool.h" |
| 16 #include "net/base/cert_verify_proc.h" |
16 #include "net/base/crl_set.h" | 17 #include "net/base/crl_set.h" |
17 #include "net/base/net_errors.h" | 18 #include "net/base/net_errors.h" |
18 #include "net/base/net_log.h" | 19 #include "net/base/net_log.h" |
19 #include "net/base/x509_certificate.h" | 20 #include "net/base/x509_certificate.h" |
20 #include "net/base/x509_certificate_net_log_param.h" | 21 #include "net/base/x509_certificate_net_log_param.h" |
21 | 22 |
22 #if defined(USE_NSS) | 23 #if defined(USE_NSS) |
23 #include <private/pprthred.h> // PR_DetachThread | 24 #include <private/pprthred.h> // PR_DetachThread |
24 #endif | 25 #endif |
25 | 26 |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 CertVerifyResult* verify_result_; | 124 CertVerifyResult* verify_result_; |
124 const BoundNetLog net_log_; | 125 const BoundNetLog net_log_; |
125 }; | 126 }; |
126 | 127 |
127 | 128 |
128 // CertVerifierWorker runs on a worker thread and takes care of the blocking | 129 // CertVerifierWorker runs on a worker thread and takes care of the blocking |
129 // process of performing the certificate verification. Deletes itself | 130 // process of performing the certificate verification. Deletes itself |
130 // eventually if Start() succeeds. | 131 // eventually if Start() succeeds. |
131 class CertVerifierWorker { | 132 class CertVerifierWorker { |
132 public: | 133 public: |
133 CertVerifierWorker(X509Certificate* cert, | 134 CertVerifierWorker(CertVerifyProc* verify_proc, |
| 135 X509Certificate* cert, |
134 const std::string& hostname, | 136 const std::string& hostname, |
135 int flags, | 137 int flags, |
136 CRLSet* crl_set, | 138 CRLSet* crl_set, |
137 MultiThreadedCertVerifier* cert_verifier) | 139 MultiThreadedCertVerifier* cert_verifier) |
138 : cert_(cert), | 140 : verify_proc_(verify_proc), |
| 141 cert_(cert), |
139 hostname_(hostname), | 142 hostname_(hostname), |
140 flags_(flags), | 143 flags_(flags), |
141 crl_set_(crl_set), | 144 crl_set_(crl_set), |
142 origin_loop_(MessageLoop::current()), | 145 origin_loop_(MessageLoop::current()), |
143 cert_verifier_(cert_verifier), | 146 cert_verifier_(cert_verifier), |
144 canceled_(false), | 147 canceled_(false), |
145 error_(ERR_FAILED) { | 148 error_(ERR_FAILED) { |
146 } | 149 } |
147 | 150 |
148 // Returns the certificate being verified. May only be called /before/ | 151 // Returns the certificate being verified. May only be called /before/ |
(...skipping 12 matching lines...) Expand all Loading... |
161 // getting deleted. | 164 // getting deleted. |
162 void Cancel() { | 165 void Cancel() { |
163 DCHECK_EQ(MessageLoop::current(), origin_loop_); | 166 DCHECK_EQ(MessageLoop::current(), origin_loop_); |
164 base::AutoLock locked(lock_); | 167 base::AutoLock locked(lock_); |
165 canceled_ = true; | 168 canceled_ = true; |
166 } | 169 } |
167 | 170 |
168 private: | 171 private: |
169 void Run() { | 172 void Run() { |
170 // Runs on a worker thread. | 173 // Runs on a worker thread. |
171 error_ = cert_->Verify(hostname_, flags_, crl_set_, &verify_result_); | 174 error_ = verify_proc_->Verify(cert_, hostname_, flags_, crl_set_, |
| 175 &verify_result_); |
172 #if defined(USE_NSS) | 176 #if defined(USE_NSS) |
173 // Detach the thread from NSPR. | 177 // Detach the thread from NSPR. |
174 // Calling NSS functions attaches the thread to NSPR, which stores | 178 // Calling NSS functions attaches the thread to NSPR, which stores |
175 // the NSPR thread ID in thread-specific data. | 179 // the NSPR thread ID in thread-specific data. |
176 // The threads in our thread pool terminate after we have called | 180 // The threads in our thread pool terminate after we have called |
177 // PR_Cleanup. Unless we detach them from NSPR, net_unittests gets | 181 // PR_Cleanup. Unless we detach them from NSPR, net_unittests gets |
178 // segfaults on shutdown when the threads' thread-specific data | 182 // segfaults on shutdown when the threads' thread-specific data |
179 // destructors run. | 183 // destructors run. |
180 PR_DetachThread(); | 184 PR_DetachThread(); |
181 #endif | 185 #endif |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 origin_loop_->PostTask( | 223 origin_loop_->PostTask( |
220 FROM_HERE, base::Bind( | 224 FROM_HERE, base::Bind( |
221 &CertVerifierWorker::DoReply, base::Unretained(this))); | 225 &CertVerifierWorker::DoReply, base::Unretained(this))); |
222 } | 226 } |
223 } | 227 } |
224 | 228 |
225 if (canceled) | 229 if (canceled) |
226 delete this; | 230 delete this; |
227 } | 231 } |
228 | 232 |
| 233 scoped_refptr<CertVerifyProc> verify_proc_; |
229 scoped_refptr<X509Certificate> cert_; | 234 scoped_refptr<X509Certificate> cert_; |
230 const std::string hostname_; | 235 const std::string hostname_; |
231 const int flags_; | 236 const int flags_; |
232 scoped_refptr<CRLSet> crl_set_; | 237 scoped_refptr<CRLSet> crl_set_; |
233 MessageLoop* const origin_loop_; | 238 MessageLoop* const origin_loop_; |
234 MultiThreadedCertVerifier* const cert_verifier_; | 239 MultiThreadedCertVerifier* const cert_verifier_; |
235 | 240 |
236 // lock_ protects canceled_. | 241 // lock_ protects canceled_. |
237 base::Lock lock_; | 242 base::Lock lock_; |
238 | 243 |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
317 const base::TimeTicks start_time_; | 322 const base::TimeTicks start_time_; |
318 std::vector<CertVerifierRequest*> requests_; | 323 std::vector<CertVerifierRequest*> requests_; |
319 CertVerifierWorker* worker_; | 324 CertVerifierWorker* worker_; |
320 const BoundNetLog net_log_; | 325 const BoundNetLog net_log_; |
321 }; | 326 }; |
322 | 327 |
323 MultiThreadedCertVerifier::MultiThreadedCertVerifier() | 328 MultiThreadedCertVerifier::MultiThreadedCertVerifier() |
324 : cache_(kMaxCacheEntries), | 329 : cache_(kMaxCacheEntries), |
325 requests_(0), | 330 requests_(0), |
326 cache_hits_(0), | 331 cache_hits_(0), |
327 inflight_joins_(0) { | 332 inflight_joins_(0), |
| 333 verify_proc_(CertVerifyProc::CreateDefault()) { |
328 CertDatabase::AddObserver(this); | 334 CertDatabase::AddObserver(this); |
329 } | 335 } |
330 | 336 |
331 MultiThreadedCertVerifier::~MultiThreadedCertVerifier() { | 337 MultiThreadedCertVerifier::~MultiThreadedCertVerifier() { |
332 STLDeleteValues(&inflight_); | 338 STLDeleteValues(&inflight_); |
333 | 339 |
334 CertDatabase::RemoveObserver(this); | 340 CertDatabase::RemoveObserver(this); |
335 } | 341 } |
336 | 342 |
337 int MultiThreadedCertVerifier::Verify(X509Certificate* cert, | 343 int MultiThreadedCertVerifier::Verify(X509Certificate* cert, |
(...skipping 28 matching lines...) Expand all Loading... |
366 CertVerifierJob* job; | 372 CertVerifierJob* job; |
367 std::map<RequestParams, CertVerifierJob*>::const_iterator j; | 373 std::map<RequestParams, CertVerifierJob*>::const_iterator j; |
368 j = inflight_.find(key); | 374 j = inflight_.find(key); |
369 if (j != inflight_.end()) { | 375 if (j != inflight_.end()) { |
370 // An identical request is in flight already. We'll just attach our | 376 // An identical request is in flight already. We'll just attach our |
371 // callback. | 377 // callback. |
372 inflight_joins_++; | 378 inflight_joins_++; |
373 job = j->second; | 379 job = j->second; |
374 } else { | 380 } else { |
375 // Need to make a new request. | 381 // Need to make a new request. |
376 CertVerifierWorker* worker = new CertVerifierWorker(cert, hostname, flags, | 382 CertVerifierWorker* worker = new CertVerifierWorker(verify_proc_, cert, |
| 383 hostname, flags, |
377 crl_set, this); | 384 crl_set, this); |
378 job = new CertVerifierJob( | 385 job = new CertVerifierJob( |
379 worker, | 386 worker, |
380 BoundNetLog::Make(net_log.net_log(), NetLog::SOURCE_CERT_VERIFIER_JOB)); | 387 BoundNetLog::Make(net_log.net_log(), NetLog::SOURCE_CERT_VERIFIER_JOB)); |
381 if (!worker->Start()) { | 388 if (!worker->Start()) { |
382 delete job; | 389 delete job; |
383 delete worker; | 390 delete worker; |
384 *out_req = NULL; | 391 *out_req = NULL; |
385 // TODO(wtc): log to the NetLog. | 392 // TODO(wtc): log to the NetLog. |
386 LOG(ERROR) << "CertVerifierWorker couldn't be started."; | 393 LOG(ERROR) << "CertVerifierWorker couldn't be started."; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
434 delete job; | 441 delete job; |
435 } | 442 } |
436 | 443 |
437 void MultiThreadedCertVerifier::OnCertTrustChanged( | 444 void MultiThreadedCertVerifier::OnCertTrustChanged( |
438 const X509Certificate* cert) { | 445 const X509Certificate* cert) { |
439 DCHECK(CalledOnValidThread()); | 446 DCHECK(CalledOnValidThread()); |
440 | 447 |
441 ClearCache(); | 448 ClearCache(); |
442 } | 449 } |
443 | 450 |
| 451 void MultiThreadedCertVerifier::SetCertVerifyProc(CertVerifyProc* verify_proc) { |
| 452 verify_proc_ = verify_proc; |
| 453 } |
| 454 |
444 } // namespace net | 455 } // namespace net |
OLD | NEW |