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 #include "ios/web/net/cert_verifier_block_adapter.h" | 5 #include "ios/web/net/cert_verifier_block_adapter.h" |
6 | 6 |
7 #include "base/mac/bind_objc_block.h" | 7 #include "base/mac/bind_objc_block.h" |
8 #include "net/base/net_errors.h" | 8 #include "net/base/net_errors.h" |
9 #include "net/cert/crl_set.h" | 9 #include "net/cert/crl_set.h" |
10 #include "net/cert/x509_certificate.h" | 10 #include "net/cert/x509_certificate.h" |
11 #include "net/log/net_log.h" | 11 #include "net/log/net_log.h" |
12 | 12 |
13 namespace web { | 13 namespace web { |
14 | 14 |
15 namespace { | 15 namespace { |
16 | 16 |
17 // Resource manager which keeps CertVerifyResult, X509Certificate and | 17 // Resource manager which keeps CertVerifyResult, X509Certificate, |
18 // BoundNetLog alive until verification is completed. Also holds unowned pointer | 18 // CertVerifier::Request and BoundNetLog alive until verification is completed. |
19 // to |net::CertVerifier::Request|. | 19 // This class is refcounted so it can be captured by a block, keeping its |
| 20 // members alive. |
20 struct VerificationContext | 21 struct VerificationContext |
21 : public base::RefCountedThreadSafe<VerificationContext> { | 22 : public base::RefCountedThreadSafe<VerificationContext> { |
22 VerificationContext(scoped_refptr<net::X509Certificate> cert, | 23 VerificationContext(scoped_refptr<net::X509Certificate> cert, |
23 net::NetLog* net_log) | 24 net::NetLog* net_log) |
24 : request(nullptr), | 25 : request(nullptr), |
25 cert(cert.Pass()), | 26 cert(cert.Pass()), |
26 net_log(net::BoundNetLog::Make( | 27 net_log(net::BoundNetLog::Make( |
27 net_log, | 28 net_log, |
28 net::NetLog::SOURCE_IOS_WEB_VIEW_CERT_VERIFIER)) {} | 29 net::NetLog::SOURCE_IOS_WEB_VIEW_CERT_VERIFIER)) {} |
29 // Unowned verification request. | 30 |
30 net::CertVerifier::Request* request; | 31 // Stores the current verification request. The request must outlive the |
| 32 // VerificationContext and the CertVerifierBlockAdapter, so that the |
| 33 // verification request is not cancelled. CertVerifierBlockAdapter::Verify |
| 34 // guarantees its completion handler to be called, which will not happen if |
| 35 // verification request is cancelled. |
| 36 scoped_ptr<net::CertVerifier::Request> request; |
31 // The result of certificate verification. | 37 // The result of certificate verification. |
32 net::CertVerifyResult result; | 38 net::CertVerifyResult result; |
33 // Certificate being verified. | 39 // Certificate being verified. |
34 scoped_refptr<net::X509Certificate> cert; | 40 scoped_refptr<net::X509Certificate> cert; |
35 // BoundNetLog required by CertVerifier. | 41 // BoundNetLog required by CertVerifier. |
36 net::BoundNetLog net_log; | 42 net::BoundNetLog net_log; |
37 | 43 |
38 private: | 44 private: |
39 friend class base::RefCountedThreadSafe<VerificationContext>; | 45 friend class base::RefCountedThreadSafe<VerificationContext>; |
40 VerificationContext() = delete; | 46 VerificationContext() = delete; |
(...skipping 26 matching lines...) Expand all Loading... |
67 DCHECK(thread_checker_.CalledOnValidThread()); | 73 DCHECK(thread_checker_.CalledOnValidThread()); |
68 DCHECK(completion_handler); | 74 DCHECK(completion_handler); |
69 if (!params.cert || params.hostname.empty()) { | 75 if (!params.cert || params.hostname.empty()) { |
70 completion_handler(net::CertVerifyResult(), net::ERR_INVALID_ARGUMENT); | 76 completion_handler(net::CertVerifyResult(), net::ERR_INVALID_ARGUMENT); |
71 return; | 77 return; |
72 } | 78 } |
73 | 79 |
74 scoped_refptr<VerificationContext> context( | 80 scoped_refptr<VerificationContext> context( |
75 new VerificationContext(params.cert, net_log_)); | 81 new VerificationContext(params.cert, net_log_)); |
76 net::CompletionCallback callback = base::BindBlock(^(int error) { | 82 net::CompletionCallback callback = base::BindBlock(^(int error) { |
77 // Remove pending request. | |
78 auto request_iterator = std::find( | |
79 pending_requests_.begin(), pending_requests_.end(), context->request); | |
80 DCHECK(pending_requests_.end() != request_iterator); | |
81 pending_requests_.erase(request_iterator); | |
82 | |
83 completion_handler(context->result, error); | 83 completion_handler(context->result, error); |
84 }); | 84 }); |
85 scoped_ptr<net::CertVerifier::Request> request; | 85 scoped_ptr<net::CertVerifier::Request> request; |
86 int error = cert_verifier_->Verify(params.cert.get(), params.hostname, | 86 int error = cert_verifier_->Verify(params.cert.get(), params.hostname, |
87 params.ocsp_response, params.flags, | 87 params.ocsp_response, params.flags, |
88 params.crl_set.get(), &(context->result), | 88 params.crl_set.get(), &(context->result), |
89 callback, &request, context->net_log); | 89 callback, &request, context->net_log); |
90 if (error == net::ERR_IO_PENDING) { | 90 if (error == net::ERR_IO_PENDING) { |
91 // Make sure that |net::CertVerifier::Request| is alive until either | 91 // Keep the |net::CertVerifier::Request| alive until verification completes. |
92 // verification is completed or CertVerifierBlockAdapter is destroyed. | 92 // Because |context| is kept alive by |callback| (through base::BindBlock), |
93 pending_requests_.push_back(request.Pass()); | 93 // this means that the cert verification request cannot be cancelled. |
94 context->request = pending_requests_.back(); | 94 // However, it guarantees that |callback| - and thus |completion_handler| - |
| 95 // will always be called, which is a necessary part of the API contract of |
| 96 // |CertVerifierBlockAdapter::Verify()|. |
| 97 context->request = request.Pass(); |
95 // Completion handler will be called from |callback| when verification | 98 // Completion handler will be called from |callback| when verification |
96 // request is completed. | 99 // request is completed. |
97 return; | 100 return; |
98 } | 101 } |
99 | 102 |
100 // Verification has either failed or result was retrieved from the cache. | 103 // Verification has either failed or result was retrieved from the cache. |
101 completion_handler(context->result, error); | 104 completion_handler(context->result, error); |
102 } | 105 } |
103 | 106 |
104 } // namespace web | 107 } // namespace web |
OLD | NEW |