Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(466)

Side by Side Diff: ios/web/net/cert_verifier_block_adapter.cc

Issue 1230033005: WKWebView: Added cert verification API to web controller. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Updated comment (s/used/user); Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 web {
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 |net::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,
23 // Verification request. Must be alive until verification is completed, 23 net::NetLog* net_log)
24 // otherwise it will be cancelled. 24 : request(nullptr),
25 scoped_ptr<CertVerifier::Request> request; 25 cert(cert.Pass()),
26 net_log(net::BoundNetLog::Make(
27 net_log,
28 net::NetLog::SOURCE_IOS_WEB_VIEW_CERT_VERIFIER)) {}
29 // Unowned verification request.
30 net::CertVerifier::Request* request;
26 // The result of certificate verification. 31 // The result of certificate verification.
27 CertVerifyResult result; 32 net::CertVerifyResult result;
28 // Certificate being verificated. 33 // Certificate being verified.
29 scoped_refptr<net::X509Certificate> cert; 34 scoped_refptr<net::X509Certificate> cert;
30 35 // BoundNetLog required by CertVerifier.
31 // Copies CertVerifyResult and wraps it into a scoped_ptr. 36 net::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 37
38 private: 38 private:
39 friend class base::RefCountedThreadSafe<VerificationContext>;
39 VerificationContext() = delete; 40 VerificationContext() = delete;
40 // Required by base::RefCounted.
41 friend class base::RefCounted<VerificationContext>;
42 ~VerificationContext() {} 41 ~VerificationContext() {}
43 }; 42 };
44 } 43 }
45 44
46 CertVerifierBlockAdapter::CertVerifierBlockAdapter()
47 : CertVerifierBlockAdapter(
48 scoped_ptr<CertVerifier>(CertVerifier::CreateDefault())) {
49 }
50
51 CertVerifierBlockAdapter::CertVerifierBlockAdapter( 45 CertVerifierBlockAdapter::CertVerifierBlockAdapter(
52 scoped_ptr<CertVerifier> cert_verifier) 46 net::CertVerifier* cert_verifier,
53 : cert_verifier_(cert_verifier.Pass()) { 47 net::NetLog* net_log)
48 : cert_verifier_(cert_verifier), net_log_(net_log) {
54 DCHECK(cert_verifier_); 49 DCHECK(cert_verifier_);
50 DCHECK(net_log_);
55 } 51 }
56 52
57 CertVerifierBlockAdapter::~CertVerifierBlockAdapter() { 53 CertVerifierBlockAdapter::~CertVerifierBlockAdapter() {
54 DCHECK(thread_checker_.CalledOnValidThread());
58 } 55 }
59 56
60 CertVerifierBlockAdapter::Params::Params(scoped_refptr<X509Certificate> cert, 57 CertVerifierBlockAdapter::Params::Params(
61 const std::string& hostname) 58 const scoped_refptr<net::X509Certificate>& cert,
62 : cert(cert), 59 const std::string& hostname)
63 hostname(hostname), 60 : cert(cert), hostname(hostname), flags(0) {}
64 flags(static_cast<CertVerifier::VerifyFlags>(0)) {
65 }
66 61
67 CertVerifierBlockAdapter::Params::~Params() { 62 CertVerifierBlockAdapter::Params::~Params() {
68 } 63 }
69 64
70 void CertVerifierBlockAdapter::Verify( 65 void CertVerifierBlockAdapter::Verify(
71 const Params& params, 66 const Params& params,
72 void (^completion_handler)(scoped_ptr<CertVerifyResult>, int)) { 67 void (^completion_handler)(net::CertVerifyResult, int)) {
68 DCHECK(thread_checker_.CalledOnValidThread());
73 DCHECK(completion_handler); 69 DCHECK(completion_handler);
70 if (!params.cert || params.hostname.empty()) {
71 completion_handler(net::CertVerifyResult(), net::ERR_INVALID_ARGUMENT);
72 return;
73 }
74 74
75 scoped_refptr<VerificationContext> context( 75 scoped_refptr<VerificationContext> context(
76 new VerificationContext(params.cert)); 76 new VerificationContext(params.cert, net_log_));
77 CompletionCallback callback = base::BindBlock(^(int) { 77 net::CompletionCallback callback = base::BindBlock(^(int error) {
78 completion_handler(context->scoped_result(), 0); 78 // Remove pending request.
79 auto request_iterator = std::find(
80 pending_requests_.begin(), pending_requests_.end(), context->request);
81 DCHECK(pending_requests_.end() != request_iterator);
82 pending_requests_.erase(request_iterator);
83
84 completion_handler(context->result, error);
79 }); 85 });
80 int status = cert_verifier_->Verify(params.cert.get(), params.hostname, 86 scoped_ptr<net::CertVerifier::Request> request;
81 params.ocsp_response, params.flags, 87 int error = cert_verifier_->Verify(params.cert.get(), params.hostname,
82 params.crl_set.get(), &(context->result), 88 params.ocsp_response, params.flags,
83 callback, &(context->request), net_log_); 89 params.crl_set.get(), &(context->result),
84 90 callback, &request, context->net_log);
85 if (status == ERR_IO_PENDING) { 91 if (error == net::ERR_IO_PENDING) {
92 // Make sure that |net::CertVerifier::Request| is alive until either
93 // verification is completed or CertVerifierBlockAdapter is destroyed.
94 pending_requests_.push_back(request.Pass());
95 context->request = pending_requests_.back();
86 // Completion handler will be called from |callback| when verification 96 // Completion handler will be called from |callback| when verification
87 // request is completed. 97 // request is completed.
88 return; 98 return;
89 } 99 }
90 100
91 // Verification has either failed or result was retrieved from the cache. 101 // Verification has either failed or result was retrieved from the cache.
92 completion_handler(status ? nullptr : context->scoped_result(), status); 102 completion_handler(context->result, error);
93 } 103 }
94 104
95 } // net 105 } // namespace web
OLDNEW
« no previous file with comments | « ios/web/net/cert_verifier_block_adapter.h ('k') | ios/web/net/cert_verifier_block_adapter_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698