OLD | NEW |
1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2008 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/cert_verifier.h" | 5 #include "net/base/cert_verifier.h" |
6 | 6 |
7 #if defined(USE_NSS) | 7 #if defined(USE_NSS) |
8 #include <private/pprthred.h> // PR_DetatchThread | 8 #include <private/pprthred.h> // PR_DetatchThread |
9 #endif | 9 #endif |
10 | 10 |
11 #include "base/message_loop.h" | 11 #include "base/lock.h" |
| 12 #include "base/message_loop_proxy.h" |
| 13 #include "base/scoped_ptr.h" |
12 #include "base/worker_pool.h" | 14 #include "base/worker_pool.h" |
13 #include "net/base/cert_verify_result.h" | 15 #include "net/base/cert_verify_result.h" |
14 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
15 #include "net/base/x509_certificate.h" | 17 #include "net/base/x509_certificate.h" |
16 | 18 |
17 namespace net { | 19 namespace net { |
18 | 20 |
19 class CertVerifier::Request : | 21 class CertVerifier::Request : |
20 public base::RefCountedThreadSafe<CertVerifier::Request> { | 22 public base::RefCountedThreadSafe<CertVerifier::Request> { |
21 public: | 23 public: |
22 Request(CertVerifier* verifier, | 24 Request(CertVerifier* verifier, |
23 X509Certificate* cert, | 25 X509Certificate* cert, |
24 const std::string& hostname, | 26 const std::string& hostname, |
25 int flags, | 27 int flags, |
26 CertVerifyResult* verify_result, | 28 CertVerifyResult* verify_result, |
27 CompletionCallback* callback) | 29 CompletionCallback* callback) |
28 : cert_(cert), | 30 : cert_(cert), |
29 hostname_(hostname), | 31 hostname_(hostname), |
30 flags_(flags), | 32 flags_(flags), |
31 verifier_(verifier), | 33 verifier_(verifier), |
32 verify_result_(verify_result), | 34 verify_result_(verify_result), |
33 callback_(callback), | 35 callback_(callback), |
34 origin_loop_(MessageLoop::current()), | 36 origin_loop_proxy_(base::MessageLoopProxy::CreateForCurrentThread()), |
35 error_(OK) { | 37 error_(OK) { |
36 } | 38 } |
37 | 39 |
38 void DoVerify() { | 40 void DoVerify() { |
39 // Running on the worker thread | 41 // Running on the worker thread |
40 error_ = cert_->Verify(hostname_, flags_, &result_); | 42 error_ = cert_->Verify(hostname_, flags_, &result_); |
41 #if defined(USE_NSS) | 43 #if defined(USE_NSS) |
42 // Detach the thread from NSPR. | 44 // Detach the thread from NSPR. |
43 // Calling NSS functions attaches the thread to NSPR, which stores | 45 // Calling NSS functions attaches the thread to NSPR, which stores |
44 // the NSPR thread ID in thread-specific data. | 46 // the NSPR thread ID in thread-specific data. |
45 // The threads in our thread pool terminate after we have called | 47 // The threads in our thread pool terminate after we have called |
46 // PR_Cleanup. Unless we detach them from NSPR, net_unittests gets | 48 // PR_Cleanup. Unless we detach them from NSPR, net_unittests gets |
47 // segfaults on shutdown when the threads' thread-specific data | 49 // segfaults on shutdown when the threads' thread-specific data |
48 // destructors run. | 50 // destructors run. |
49 PR_DetachThread(); | 51 PR_DetachThread(); |
50 #endif | 52 #endif |
51 | 53 |
52 Task* reply = NewRunnableMethod(this, &Request::DoCallback); | 54 scoped_ptr<Task> reply(NewRunnableMethod(this, &Request::DoCallback)); |
53 | 55 |
54 // The origin loop could go away while we are trying to post to it, so we | 56 // The origin loop could go away while we are trying to post to it, so we |
55 // need to call its PostTask method inside a lock. See ~CertVerifier. | 57 // need to call its PostTask method inside a lock. See ~CertVerifier. |
56 { | 58 AutoLock locked(origin_loop_proxy_lock_); |
57 AutoLock locked(origin_loop_lock_); | 59 if (origin_loop_proxy_) { |
58 if (origin_loop_) { | 60 bool posted = origin_loop_proxy_->PostTask(FROM_HERE, reply.release()); |
59 origin_loop_->PostTask(FROM_HERE, reply); | 61 // TODO(willchan): Fix leaks and then change this to a DCHECK. |
60 reply = NULL; | 62 LOG_IF(ERROR, !posted) << "Leaked CertVerifier!"; |
61 } | |
62 } | 63 } |
63 | |
64 // Does nothing if it got posted. | |
65 delete reply; | |
66 } | 64 } |
67 | 65 |
68 void DoCallback() { | 66 void DoCallback() { |
69 // Running on the origin thread. | 67 // Running on the origin thread. |
70 | 68 |
71 // We may have been cancelled! | 69 // We may have been cancelled! |
72 if (!verifier_) | 70 if (!verifier_) |
73 return; | 71 return; |
74 | 72 |
75 *verify_result_ = result_; | 73 *verify_result_ = result_; |
76 | 74 |
77 // Drop the verifier's reference to us. Do this before running the | 75 // Drop the verifier's reference to us. Do this before running the |
78 // callback since the callback might result in the verifier being | 76 // callback since the callback might result in the verifier being |
79 // destroyed. | 77 // destroyed. |
80 verifier_->request_ = NULL; | 78 verifier_->request_ = NULL; |
81 | 79 |
82 callback_->Run(error_); | 80 callback_->Run(error_); |
83 } | 81 } |
84 | 82 |
85 void Cancel() { | 83 void Cancel() { |
86 verifier_ = NULL; | 84 verifier_ = NULL; |
87 | 85 |
88 AutoLock locked(origin_loop_lock_); | 86 AutoLock locked(origin_loop_proxy_lock_); |
89 origin_loop_ = NULL; | 87 origin_loop_proxy_ = NULL; |
90 } | 88 } |
91 | 89 |
92 private: | 90 private: |
93 friend class base::RefCountedThreadSafe<CertVerifier::Request>; | 91 friend class base::RefCountedThreadSafe<CertVerifier::Request>; |
94 | 92 |
95 ~Request() {} | 93 ~Request() {} |
96 | 94 |
97 // Set on the origin thread, read on the worker thread. | 95 // Set on the origin thread, read on the worker thread. |
98 scoped_refptr<X509Certificate> cert_; | 96 scoped_refptr<X509Certificate> cert_; |
99 std::string hostname_; | 97 std::string hostname_; |
100 // bitwise OR'd of X509Certificate::VerifyFlags. | 98 // bitwise OR'd of X509Certificate::VerifyFlags. |
101 int flags_; | 99 int flags_; |
102 | 100 |
103 // Only used on the origin thread (where Verify was called). | 101 // Only used on the origin thread (where Verify was called). |
104 CertVerifier* verifier_; | 102 CertVerifier* verifier_; |
105 CertVerifyResult* verify_result_; | 103 CertVerifyResult* verify_result_; |
106 CompletionCallback* callback_; | 104 CompletionCallback* callback_; |
107 | 105 |
108 // Used to post ourselves onto the origin thread. | 106 // Used to post ourselves onto the origin thread. |
109 Lock origin_loop_lock_; | 107 Lock origin_loop_proxy_lock_; |
110 MessageLoop* origin_loop_; | 108 // Use a MessageLoopProxy in case the owner of the CertVerifier is leaked, so |
| 109 // this code won't crash: http://crbug.com/42275. If this is leaked, then it |
| 110 // doesn't get Cancel()'d, so |origin_loop_proxy_| doesn't get NULL'd out. If |
| 111 // the MessageLoop goes away, then if we had used a MessageLoop, this would |
| 112 // crash. |
| 113 scoped_refptr<base::MessageLoopProxy> origin_loop_proxy_; |
111 | 114 |
112 // Assigned on the worker thread, read on the origin thread. | 115 // Assigned on the worker thread, read on the origin thread. |
113 int error_; | 116 int error_; |
114 CertVerifyResult result_; | 117 CertVerifyResult result_; |
115 }; | 118 }; |
116 | 119 |
117 //----------------------------------------------------------------------------- | 120 //----------------------------------------------------------------------------- |
118 | 121 |
119 CertVerifier::CertVerifier() { | 122 CertVerifier::CertVerifier() { |
120 } | 123 } |
(...skipping 25 matching lines...) Expand all Loading... |
146 NewRunnableMethod(request_.get(), &Request::DoVerify), true)) { | 149 NewRunnableMethod(request_.get(), &Request::DoVerify), true)) { |
147 NOTREACHED(); | 150 NOTREACHED(); |
148 request_ = NULL; | 151 request_ = NULL; |
149 return ERR_FAILED; | 152 return ERR_FAILED; |
150 } | 153 } |
151 | 154 |
152 return ERR_IO_PENDING; | 155 return ERR_IO_PENDING; |
153 } | 156 } |
154 | 157 |
155 } // namespace net | 158 } // namespace net |
OLD | NEW |