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
3 // found in the LICENSE file.
5 #include "ios/web/net/cert_verifier_block_adapter.h"
7 #include "base/mac/bind_objc_block.h"
8 #include "net/base/net_errors.h"
9 #include "net/cert/cert_verify_result.h"
10 #include "net/cert/crl_set.h"
11 #include "net/cert/x509_certificate.h"
17 // Resource manager which keeps CertVerifier::Request, CertVerifyResult and
18 // X509Certificate alive until verification is completed.
19 struct VerificationContext
: public base::RefCounted
<VerificationContext
> {
20 VerificationContext(scoped_refptr
<net::X509Certificate
> cert
) : cert(cert
) {
21 result
.cert_status
= CERT_STATUS_INVALID
;
23 // Verification request. Must be alive until verification is completed,
24 // otherwise it will be cancelled.
25 scoped_ptr
<CertVerifier::Request
> request
;
26 // The result of certificate verification.
27 CertVerifyResult result
;
28 // Certificate being verificated.
29 scoped_refptr
<net::X509Certificate
> cert
;
31 // Copies CertVerifyResult and wraps it into a scoped_ptr.
32 scoped_ptr
<CertVerifyResult
> scoped_result() {
33 scoped_ptr
<CertVerifyResult
> scoped_result(new CertVerifyResult());
34 scoped_result
->CopyFrom(result
);
35 return scoped_result
.Pass();
39 VerificationContext() = delete;
40 // Required by base::RefCounted.
41 friend class base::RefCounted
<VerificationContext
>;
42 ~VerificationContext() {}
46 CertVerifierBlockAdapter::CertVerifierBlockAdapter()
47 : CertVerifierBlockAdapter(
48 scoped_ptr
<CertVerifier
>(CertVerifier::CreateDefault())) {
51 CertVerifierBlockAdapter::CertVerifierBlockAdapter(
52 scoped_ptr
<CertVerifier
> cert_verifier
)
53 : cert_verifier_(cert_verifier
.Pass()) {
54 DCHECK(cert_verifier_
);
57 CertVerifierBlockAdapter::~CertVerifierBlockAdapter() {
60 CertVerifierBlockAdapter::Params::Params(scoped_refptr
<X509Certificate
> cert
,
61 const std::string
& hostname
)
64 flags(static_cast<CertVerifier::VerifyFlags
>(0)) {
67 CertVerifierBlockAdapter::Params::~Params() {
70 void CertVerifierBlockAdapter::Verify(
72 void (^completion_handler
)(scoped_ptr
<CertVerifyResult
>, int)) {
73 DCHECK(completion_handler
);
75 scoped_refptr
<VerificationContext
> context(
76 new VerificationContext(params
.cert
));
77 CompletionCallback callback
= base::BindBlock(^(int) {
78 completion_handler(context
->scoped_result(), 0);
80 int status
= cert_verifier_
->Verify(params
.cert
.get(), params
.hostname
,
81 params
.ocsp_response
, params
.flags
,
82 params
.crl_set
.get(), &(context
->result
),
83 callback
, &(context
->request
), net_log_
);
85 if (status
== ERR_IO_PENDING
) {
86 // Completion handler will be called from |callback| when verification
87 // request is completed.
91 // Verification has either failed or result was retrieved from the cache.
92 completion_handler(status
? nullptr : context
->scoped_result(), status
);