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/cert_verify_result.h" | |
10 #include "net/cert/crl_set.h" | 9 #include "net/cert/crl_set.h" |
11 #include "net/cert/x509_certificate.h" | 10 #include "net/cert/x509_certificate.h" |
11 #include "net/log/net_log.h" | |
12 | 12 |
13 namespace net { | 13 namespace net { |
14 | 14 |
15 namespace { | 15 namespace { |
16 | 16 |
17 // Resource manager which keeps CertVerifier::Request, CertVerifyResult and | 17 // Resource manager which keeps CertVerifyResult, X509Certificate and |
18 // X509Certificate alive until verification is completed. | 18 // BoundNetLog alive until verification is completed. Also holds unowned pointer |
19 struct VerificationContext : public base::RefCounted<VerificationContext> { | 19 // to |CertVerifier::Request|. |
20 VerificationContext(scoped_refptr<net::X509Certificate> cert) : cert(cert) { | 20 struct VerificationContext |
21 result.cert_status = CERT_STATUS_INVALID; | 21 : public base::RefCountedThreadSafe<VerificationContext> { |
22 } | 22 VerificationContext(scoped_refptr<net::X509Certificate> cert, NetLog* net_log) |
23 // Verification request. Must be alive until verification is completed, | 23 : request(nullptr), |
24 // otherwise it will be cancelled. | 24 cert(cert), |
Ryan Sleevi
2015/08/14 02:29:43
cert.Pass()
Eugene But (OOO till 7-30)
2015/08/14 21:18:19
Done.
| |
25 scoped_ptr<CertVerifier::Request> request; | 25 net_log( |
26 BoundNetLog::Make(net_log, NetLog::IOS_WEB_VIEW_CERT_VERIFIER)) {} | |
27 // Unowned verification request. | |
28 CertVerifier::Request* request; | |
26 // The result of certificate verification. | 29 // The result of certificate verification. |
27 CertVerifyResult result; | 30 CertVerifyResult cert_verify_result; |
28 // Certificate being verificated. | 31 // Certificate being verificated. |
29 scoped_refptr<net::X509Certificate> cert; | 32 scoped_refptr<net::X509Certificate> cert; |
30 | 33 // BoundNetLog required by CertVerifier. |
31 // Copies CertVerifyResult and wraps it into a scoped_ptr. | 34 BoundNetLog net_log; |
32 scoped_ptr<CertVerifyResult> scoped_result() { | |
33 scoped_ptr<CertVerifyResult> scoped_result(new CertVerifyResult()); | |
34 scoped_result->CopyFrom(result); | |
35 return scoped_result.Pass(); | |
36 } | |
37 | 35 |
38 private: | 36 private: |
39 VerificationContext() = delete; | 37 VerificationContext() = delete; |
40 // Required by base::RefCounted. | 38 // Required by base::RefCounted. |
41 friend class base::RefCounted<VerificationContext>; | 39 friend class base::RefCountedThreadSafe<VerificationContext>; |
Ryan Sleevi
2015/08/14 02:29:43
This should be the first (e.g. on line 37, after l
Eugene But (OOO till 7-30)
2015/08/14 21:18:19
Done.
| |
42 ~VerificationContext() {} | 40 ~VerificationContext() {} |
43 }; | 41 }; |
44 } | 42 } |
45 | 43 |
46 CertVerifierBlockAdapter::CertVerifierBlockAdapter() | 44 CertVerifierBlockAdapter::CertVerifierBlockAdapter(CertVerifier* cert_verifier, |
47 : CertVerifierBlockAdapter( | 45 NetLog* net_log) |
48 scoped_ptr<CertVerifier>(CertVerifier::CreateDefault())) { | 46 : cert_verifier_(cert_verifier), net_log_(net_log) { |
49 } | |
50 | |
51 CertVerifierBlockAdapter::CertVerifierBlockAdapter( | |
52 scoped_ptr<CertVerifier> cert_verifier) | |
53 : cert_verifier_(cert_verifier.Pass()) { | |
54 DCHECK(cert_verifier_); | 47 DCHECK(cert_verifier_); |
48 DCHECK(net_log_); | |
55 } | 49 } |
56 | 50 |
57 CertVerifierBlockAdapter::~CertVerifierBlockAdapter() { | 51 CertVerifierBlockAdapter::~CertVerifierBlockAdapter() { |
52 DCHECK(thread_checker_.CalledOnValidThread()); | |
58 } | 53 } |
59 | 54 |
60 CertVerifierBlockAdapter::Params::Params(scoped_refptr<X509Certificate> cert, | 55 CertVerifierBlockAdapter::Params::Params( |
61 const std::string& hostname) | 56 const scoped_refptr<X509Certificate>& cert, |
62 : cert(cert), | 57 const std::string& hostname) |
63 hostname(hostname), | 58 : cert(cert), hostname(hostname), flags(0) {} |
64 flags(static_cast<CertVerifier::VerifyFlags>(0)) { | |
65 } | |
66 | 59 |
67 CertVerifierBlockAdapter::Params::~Params() { | 60 CertVerifierBlockAdapter::Params::~Params() { |
68 } | 61 } |
69 | 62 |
70 void CertVerifierBlockAdapter::Verify( | 63 void CertVerifierBlockAdapter::Verify( |
71 const Params& params, | 64 const Params& params, |
72 void (^completion_handler)(scoped_ptr<CertVerifyResult>, int)) { | 65 void (^completion_handler)(CertVerifyResult, int)) { |
66 DCHECK(thread_checker_.CalledOnValidThread()); | |
73 DCHECK(completion_handler); | 67 DCHECK(completion_handler); |
68 if (!params.cert || params.hostname.empty()) { | |
69 completion_handler(CertVerifyResult(), ERR_INVALID_ARGUMENT); | |
70 return; | |
71 } | |
74 | 72 |
75 scoped_refptr<VerificationContext> context( | 73 scoped_refptr<VerificationContext> context( |
76 new VerificationContext(params.cert)); | 74 new VerificationContext(params.cert, net_log_)); |
77 CompletionCallback callback = base::BindBlock(^(int) { | 75 CompletionCallback callback = base::BindBlock(^(int status_result) { |
78 completion_handler(context->scoped_result(), 0); | 76 // Remove pending request. |
77 auto request_iterator = std::find( | |
78 pending_requests_.begin(), pending_requests_.end(), context->request); | |
79 DCHECK(pending_requests_.end() != request_iterator); | |
80 pending_requests_.erase(request_iterator); | |
81 | |
82 completion_handler(context->cert_verify_result, status_result); | |
79 }); | 83 }); |
80 int status = cert_verifier_->Verify(params.cert.get(), params.hostname, | 84 scoped_ptr<CertVerifier::Request> request(new CertVerifier::Request); |
81 params.ocsp_response, params.flags, | 85 int status_result = cert_verifier_->Verify( |
82 params.crl_set.get(), &(context->result), | 86 params.cert.get(), params.hostname, params.ocsp_response, params.flags, |
83 callback, &(context->request), net_log_); | 87 params.crl_set.get(), &(context->cert_verify_result), callback, &request, |
84 | 88 context->net_log); |
85 if (status == ERR_IO_PENDING) { | 89 if (status_result == ERR_IO_PENDING) { |
90 // Make sure that |CertVerifier::Request| is alive until either verification | |
91 // is completed or CertVerifierBlockAdapter is destroyed. | |
92 pending_requests_.push_back(request.Pass()); | |
93 context->request = pending_requests_.back(); | |
86 // Completion handler will be called from |callback| when verification | 94 // Completion handler will be called from |callback| when verification |
87 // request is completed. | 95 // request is completed. |
88 return; | 96 return; |
89 } | 97 } |
90 | 98 |
91 // Verification has either failed or result was retrieved from the cache. | 99 // Verification has either failed or result was retrieved from the cache. |
92 completion_handler(status ? nullptr : context->scoped_result(), status); | 100 completion_handler(context->cert_verify_result, status_result); |
93 } | 101 } |
94 | 102 |
95 } // net | 103 } // net |
OLD | NEW |