Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 #import "ios/web/net/crw_cert_verification_controller.h" | 5 #import "ios/web/net/crw_cert_verification_controller.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/mac/bind_objc_block.h" | 8 #include "base/mac/bind_objc_block.h" |
| 9 #import "base/memory/ref_counted.h" | 9 #import "base/memory/ref_counted.h" |
| 10 #import "base/memory/scoped_ptr.h" | 10 #import "base/memory/scoped_ptr.h" |
| 11 #include "base/strings/sys_string_conversions.h" | 11 #include "base/strings/sys_string_conversions.h" |
| 12 #include "base/threading/worker_pool.h" | |
| 12 #include "ios/web/net/cert_verifier_block_adapter.h" | 13 #include "ios/web/net/cert_verifier_block_adapter.h" |
| 13 #include "ios/web/public/browser_state.h" | 14 #include "ios/web/public/browser_state.h" |
| 14 #include "ios/web/public/web_thread.h" | 15 #include "ios/web/public/web_thread.h" |
| 16 #import "ios/web/web_state/wk_web_view_security_util.h" | |
| 15 #include "net/cert/cert_verify_result.h" | 17 #include "net/cert/cert_verify_result.h" |
| 16 #include "net/ssl/ssl_config_service.h" | 18 #include "net/ssl/ssl_config_service.h" |
| 17 #include "net/url_request/url_request_context.h" | 19 #include "net/url_request/url_request_context.h" |
| 18 #include "net/url_request/url_request_context_getter.h" | 20 #include "net/url_request/url_request_context_getter.h" |
| 19 | 21 |
| 20 namespace { | 22 namespace { |
| 21 | 23 |
| 22 // This class takes ownership of block and releases it on UI thread, even if | 24 // This class takes ownership of block and releases it on UI thread, even if |
| 23 // |BlockHolder| is destructed on a background thread. | 25 // |BlockHolder| is destructed on a background thread. |
| 24 template <class T> | 26 template <class T> |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 66 // URLRequestContextGetter for obtaining net layer objects. | 68 // URLRequestContextGetter for obtaining net layer objects. |
| 67 net::URLRequestContextGetter* _contextGetter; | 69 net::URLRequestContextGetter* _contextGetter; |
| 68 } | 70 } |
| 69 | 71 |
| 70 // Cert verification flags. Must be used on IO Thread. | 72 // Cert verification flags. Must be used on IO Thread. |
| 71 @property(nonatomic, readonly) int certVerifyFlags; | 73 @property(nonatomic, readonly) int certVerifyFlags; |
| 72 | 74 |
| 73 // Creates _certVerifier object on IO thread. | 75 // Creates _certVerifier object on IO thread. |
| 74 - (void)createCertVerifier; | 76 - (void)createCertVerifier; |
| 75 | 77 |
| 76 // Verifies the given |cert| for the given |host| and calls |completionHandler| | 78 // Verifies the given |cert| for the given |host| using |net::CertVerifier| and |
| 77 // on completion. |completionHandler| cannot be null and will be called | 79 // calls |completionHandler| on completion. |completionHandler| cannot be null |
| 78 // synchronously or asynchronously on IO thread. | 80 // and will be called asynchronously on IO thread. |
|
davidben
2015/10/07 20:16:29
Nit: If you're sticking with the callbacks as they
Eugene But (OOO till 7-30)
2015/10/08 16:53:32
This very callback can be called only on IO thread
davidben
2015/10/12 23:21:49
I mean verifyCert itself may be called on any thre
Eugene But (OOO till 7-30)
2015/10/13 20:40:33
Thanks for clarification. Done.
| |
| 79 - (void)verifyCert:(const scoped_refptr<net::X509Certificate>&)cert | 81 - (void)verifyCert:(const scoped_refptr<net::X509Certificate>&)cert |
| 80 forHost:(NSString*)host | 82 forHost:(NSString*)host |
| 81 completionHandler:(void (^)(net::CertVerifyResult, int))completionHandler; | 83 completionHandler:(void (^)(net::CertVerifyResult, int))completionHandler; |
| 82 | 84 |
| 85 // Verifies the given |trust| using SecTrustRef API. |completionHandler| cannot | |
| 86 // be null and will be either called asynchronously on Worker thread or | |
| 87 // synchronously on current thread if the worker task can't start (in this | |
| 88 // case |success| argument will be NO). | |
|
davidben
2015/10/07 20:16:29
We could avoid some of the two-thread weirdness by
Eugene But (OOO till 7-30)
2015/10/08 16:53:32
I thought about it. But it would be an anti patter
davidben
2015/10/12 23:21:49
Mm. I would have thought, in Objective-C, if there
Eugene But (OOO till 7-30)
2015/10/13 20:40:33
Ideally yes, completion handler should be called o
| |
| 89 - (void)verifyTrust:(base::ScopedCFTypeRef<SecTrustRef>)trust | |
| 90 completionHandler:(void (^)(SecTrustResultType, BOOL success))handler; | |
| 91 | |
| 83 @end | 92 @end |
| 84 | 93 |
| 85 @implementation CRWCertVerificationController | 94 @implementation CRWCertVerificationController |
| 86 | 95 |
| 87 #pragma mark - Superclass | 96 #pragma mark - Superclass |
| 88 | 97 |
| 89 - (void)dealloc { | 98 - (void)dealloc { |
| 90 DCHECK(!_certVerifier); | 99 DCHECK(!_certVerifier); |
| 91 [super dealloc]; | 100 [super dealloc]; |
| 92 } | 101 } |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 105 if (self) { | 114 if (self) { |
| 106 _contextGetter = browserState->GetRequestContext(); | 115 _contextGetter = browserState->GetRequestContext(); |
| 107 DCHECK(_contextGetter); | 116 DCHECK(_contextGetter); |
| 108 [self createCertVerifier]; | 117 [self createCertVerifier]; |
| 109 } | 118 } |
| 110 return self; | 119 return self; |
| 111 } | 120 } |
| 112 | 121 |
| 113 - (void)decidePolicyForCert:(const scoped_refptr<net::X509Certificate>&)cert | 122 - (void)decidePolicyForCert:(const scoped_refptr<net::X509Certificate>&)cert |
| 114 host:(NSString*)host | 123 host:(NSString*)host |
| 115 completionHandler:(web::PolicyDecisionHandler)handler { | 124 completionHandler:(web::PolicyDecisionHandler)completionHandler { |
| 116 DCHECK_CURRENTLY_ON_WEB_THREAD(web::WebThread::UI); | 125 DCHECK_CURRENTLY_ON_WEB_THREAD(web::WebThread::UI); |
| 117 // completionHandler of |verifyCert:forHost:completionHandler:| is called on | 126 // completionHandler of |verifyCert:forHost:completionHandler:| is called on |
| 118 // IO thread and then bounces back to UI thread. As a result all objects | 127 // IO thread and then bounces back to UI thread. As a result all objects |
| 119 // captured by completionHandler may be released on either UI or IO thread. | 128 // captured by completionHandler may be released on either UI or IO thread. |
| 120 // Since |handler| can potentially capture multiple thread unsafe objects | 129 // Since |completionHandler| can potentially capture multiple thread unsafe |
| 121 // (like Web Controller) |handler| itself should never be released on | 130 // objects (like Web Controller) |completionHandler| itself should never be |
| 122 // background thread and |BlockHolder| ensures that. | 131 // released on background thread and |BlockHolder| ensures that. |
| 123 __block scoped_refptr<BlockHolder<web::PolicyDecisionHandler>> handlerHolder( | 132 __block scoped_refptr<BlockHolder<web::PolicyDecisionHandler>> handlerHolder( |
| 124 new BlockHolder<web::PolicyDecisionHandler>(handler)); | 133 new BlockHolder<web::PolicyDecisionHandler>(completionHandler)); |
| 125 [self verifyCert:cert | 134 [self verifyCert:cert |
| 126 forHost:host | 135 forHost:host |
| 127 completionHandler:^(net::CertVerifyResult result, int error) { | 136 completionHandler:^(net::CertVerifyResult result, int error) { |
| 128 web::CertAcceptPolicy policy = | 137 web::CertAcceptPolicy policy = |
| 129 web::CERT_ACCEPT_POLICY_NON_RECOVERABLE_ERROR; | 138 web::CERT_ACCEPT_POLICY_NON_RECOVERABLE_ERROR; |
| 130 if (error == net::OK) { | 139 if (error == net::OK) { |
| 131 policy = web::CERT_ACCEPT_POLICY_ALLOW; | 140 policy = web::CERT_ACCEPT_POLICY_ALLOW; |
| 132 } else if (net::IsCertStatusError(result.cert_status)) { | 141 } else if (net::IsCertStatusError(result.cert_status)) { |
| 133 policy = net::IsCertStatusMinorError(result.cert_status) | 142 policy = net::IsCertStatusMinorError(result.cert_status) |
| 134 ? web::CERT_ACCEPT_POLICY_ALLOW | 143 ? web::CERT_ACCEPT_POLICY_ALLOW |
| 135 : web::CERT_ACCEPT_POLICY_RECOVERABLE_ERROR; | 144 : web::CERT_ACCEPT_POLICY_RECOVERABLE_ERROR; |
| 136 } | 145 } |
| 137 | 146 |
| 138 dispatch_async(dispatch_get_main_queue(), ^{ | 147 dispatch_async(dispatch_get_main_queue(), ^{ |
| 139 handlerHolder->call(policy, result.cert_status); | 148 handlerHolder->call(policy, result.cert_status); |
| 140 }); | 149 }); |
| 141 }]; | 150 }]; |
| 142 } | 151 } |
| 143 | 152 |
| 153 - (void)querySSLStatusForTrust:(base::ScopedCFTypeRef<SecTrustRef>)trust | |
| 154 host:(NSString*)host | |
| 155 completionHandler:(web::StatusQueryHandler)completionHandler { | |
|
davidben
2015/10/07 20:16:29
This callback currently almost always runs asynchr
stuartmorgan
2015/10/07 22:30:13
Alternately, how about we push that down the stack
Eugene But (OOO till 7-30)
2015/10/08 16:53:32
Done.
| |
| 156 DCHECK_CURRENTLY_ON_WEB_THREAD(web::WebThread::UI); | |
| 157 | |
| 158 // The completion handlers of |verifyCert:forHost:completionHandler:| and | |
| 159 // |verifyTrust:completionHandler:| will be deallocated on background thread. | |
| 160 // |completionHandler| itself should never be released on background thread | |
| 161 // and |BlockHolder| ensures that. | |
| 162 __block scoped_refptr<BlockHolder<web::StatusQueryHandler>> handlerHolder( | |
| 163 new BlockHolder<web::StatusQueryHandler>(completionHandler)); | |
| 164 [self verifyTrust:trust | |
| 165 completionHandler:^(SecTrustResultType trustResult, BOOL success) { | |
| 166 if (!success) { | |
| 167 // |verifyTrust:completionHandler:| was not able to start WorkerThread | |
| 168 // task, so |completionHandler| is called on UI thread. | |
| 169 DCHECK_CURRENTLY_ON_WEB_THREAD(web::WebThread::UI); | |
|
stuartmorgan
2015/10/07 22:30:14
Instead of this, how about we dispatch_async(dispa
| |
| 170 handlerHolder->call(web::SECURITY_STYLE_UNKNOWN, net::CertStatus()); | |
| 171 return; | |
| 172 } | |
| 173 | |
| 174 web::SecurityStyle securityStyle = | |
| 175 web::GetSecurityStyleFromTrustResult(trustResult); | |
| 176 if (securityStyle == web::SECURITY_STYLE_AUTHENTICATED) { | |
| 177 // SecTrust API considers this cert as valid. | |
| 178 dispatch_async(dispatch_get_main_queue(), ^{ | |
| 179 handlerHolder->call(securityStyle, net::CertStatus()); | |
| 180 }); | |
| 181 return; | |
| 182 } | |
| 183 | |
| 184 // Retrieve the net::CertStatus for invalid certificates to determine | |
| 185 // the rejection reason. | |
| 186 // TODO(eugenebut): Add UMA for CertVerifier and SecTrust verification | |
| 187 // mismatch (crbug.com/535699). | |
|
davidben
2015/10/07 20:16:29
This is very possible as their implementations hav
Eugene But (OOO till 7-30)
2015/10/08 16:53:32
We know that mismatch will happen, but we (includi
davidben
2015/10/12 23:21:49
I meant you might want to turn those into a differ
Eugene But (OOO till 7-30)
2015/10/13 20:40:33
I see your point. Introducing a new security style
davidben
2015/10/15 17:46:12
We mentioned this in the meeting, but UNKNOWN vs a
Eugene But (OOO till 7-30)
2015/10/15 22:59:34
Thank you for confirmation.
| |
| 188 scoped_refptr<net::X509Certificate> cert( | |
| 189 web::CreateCertFromTrust(trust)); | |
| 190 [self verifyCert:cert | |
| 191 forHost:host | |
| 192 completionHandler:^(net::CertVerifyResult certVerifierResult, int) { | |
| 193 dispatch_async(dispatch_get_main_queue(), ^{ | |
| 194 handlerHolder->call(securityStyle, | |
| 195 certVerifierResult.cert_status); | |
| 196 }); | |
| 197 }]; | |
| 198 }]; | |
| 199 } | |
| 200 | |
| 144 - (void)shutDown { | 201 - (void)shutDown { |
| 145 DCHECK_CURRENTLY_ON_WEB_THREAD(web::WebThread::UI); | 202 DCHECK_CURRENTLY_ON_WEB_THREAD(web::WebThread::UI); |
| 146 web::WebThread::PostTask(web::WebThread::IO, FROM_HERE, base::BindBlock(^{ | 203 web::WebThread::PostTask(web::WebThread::IO, FROM_HERE, base::BindBlock(^{ |
| 147 // This block captures |self| delaying its deallocation and causing dealloc | 204 // This block captures |self| delaying its deallocation and causing dealloc |
| 148 // to happen on either IO or UI thread (which is fine for this class). | 205 // to happen on either IO or UI thread (which is fine for this class). |
| 149 _certVerifier.reset(); | 206 _certVerifier.reset(); |
| 150 })); | 207 })); |
| 151 } | 208 } |
| 152 | 209 |
| 153 #pragma mark - Private | 210 #pragma mark - Private |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 190 | 247 |
| 191 web::CertVerifierBlockAdapter::Params params( | 248 web::CertVerifierBlockAdapter::Params params( |
| 192 blockCert.Pass(), base::SysNSStringToUTF8(host)); | 249 blockCert.Pass(), base::SysNSStringToUTF8(host)); |
| 193 params.flags = self.certVerifyFlags; | 250 params.flags = self.certVerifyFlags; |
| 194 params.crl_set = net::SSLConfigService::GetCRLSet(); | 251 params.crl_set = net::SSLConfigService::GetCRLSet(); |
| 195 // OCSP response is not provided by iOS API. | 252 // OCSP response is not provided by iOS API. |
| 196 _certVerifier->Verify(params, completionHandler); | 253 _certVerifier->Verify(params, completionHandler); |
| 197 })); | 254 })); |
| 198 } | 255 } |
| 199 | 256 |
| 257 - (void)verifyTrust:(base::ScopedCFTypeRef<SecTrustRef>)trust | |
| 258 completionHandler:(void (^)(SecTrustResultType, BOOL))completionHandler { | |
| 259 DCHECK(completionHandler); | |
| 260 // SecTrustEvaluate performs trust evaluation synchronously, possibly making | |
| 261 // network requests. The IO thread should not be blocked by that operation. | |
|
davidben
2015/10/07 20:16:29
IO -> UI? This code I believe runs on the UI threa
Eugene But (OOO till 7-30)
2015/10/08 16:53:32
A few patches ago this method was called on IO thr
| |
| 262 bool result = base::WorkerPool::PostTask(FROM_HERE, base::BindBlock(^{ | |
| 263 SecTrustResultType trustResult = kSecTrustResultInvalid; | |
| 264 if (SecTrustEvaluate(trust.get(), &trustResult) != errSecSuccess) { | |
| 265 trustResult = kSecTrustResultInvalid; | |
| 266 } | |
| 267 completionHandler(trustResult, YES); | |
| 268 }), false /* task_is_slow */); | |
| 269 | |
| 270 if (!result) { | |
| 271 completionHandler(kSecTrustResultInvalid, NO); | |
| 272 } | |
| 273 } | |
| 274 | |
| 200 @end | 275 @end |
| OLD | NEW |