2015-11-05 17:06:36 +03:00
|
|
|
// Copyright (c) 2015 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2015-11-17 14:03:09 +03:00
|
|
|
#include "atom/browser/net/atom_cert_verifier.h"
|
2015-11-05 17:06:36 +03:00
|
|
|
|
|
|
|
#include "atom/browser/browser.h"
|
|
|
|
#include "atom/common/native_mate_converters/net_converter.h"
|
2016-11-09 16:05:46 +03:00
|
|
|
#include "base/containers/linked_list.h"
|
|
|
|
#include "base/memory/ptr_util.h"
|
|
|
|
#include "base/memory/weak_ptr.h"
|
2017-11-15 16:09:22 +03:00
|
|
|
#include "brightray/browser/net/require_ct_delegate.h"
|
2015-11-05 17:06:36 +03:00
|
|
|
#include "content/public/browser/browser_thread.h"
|
|
|
|
#include "net/base/net_errors.h"
|
2016-10-17 13:33:24 +03:00
|
|
|
#include "net/cert/cert_verify_result.h"
|
2015-11-17 14:44:55 +03:00
|
|
|
#include "net/cert/crl_set.h"
|
|
|
|
#include "net/cert/x509_certificate.h"
|
2015-11-05 17:06:36 +03:00
|
|
|
|
|
|
|
using content::BrowserThread;
|
|
|
|
|
|
|
|
namespace atom {
|
|
|
|
|
2015-11-18 06:17:08 +03:00
|
|
|
namespace {
|
2015-11-12 22:55:23 +03:00
|
|
|
|
2016-11-09 16:05:46 +03:00
|
|
|
class Response : public base::LinkNode<Response> {
|
|
|
|
public:
|
|
|
|
Response(net::CertVerifyResult* verify_result,
|
|
|
|
const net::CompletionCallback& callback)
|
|
|
|
: verify_result_(verify_result), callback_(callback) {}
|
|
|
|
net::CertVerifyResult* verify_result() { return verify_result_; }
|
|
|
|
net::CompletionCallback callback() { return callback_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
net::CertVerifyResult* verify_result_;
|
|
|
|
net::CompletionCallback callback_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(Response);
|
|
|
|
};
|
2015-11-17 14:44:55 +03:00
|
|
|
|
2015-11-18 06:17:08 +03:00
|
|
|
} // namespace
|
|
|
|
|
2016-11-09 16:05:46 +03:00
|
|
|
class CertVerifierRequest : public AtomCertVerifier::Request {
|
|
|
|
public:
|
|
|
|
CertVerifierRequest(const AtomCertVerifier::RequestParams& params,
|
|
|
|
AtomCertVerifier* cert_verifier)
|
|
|
|
: params_(params),
|
|
|
|
cert_verifier_(cert_verifier),
|
|
|
|
error_(net::ERR_IO_PENDING),
|
|
|
|
custom_response_(net::ERR_IO_PENDING),
|
|
|
|
first_response_(true),
|
|
|
|
weak_ptr_factory_(this) {}
|
|
|
|
|
2017-03-31 00:13:23 +03:00
|
|
|
~CertVerifierRequest() override {
|
2016-11-09 16:05:46 +03:00
|
|
|
cert_verifier_->RemoveRequest(params_);
|
|
|
|
default_verifier_request_.reset();
|
|
|
|
while (!response_list_.empty() && !first_response_) {
|
|
|
|
base::LinkNode<Response>* response_node = response_list_.head();
|
|
|
|
response_node->RemoveFromList();
|
|
|
|
Response* response = response_node->value();
|
|
|
|
RunResponse(response);
|
|
|
|
}
|
|
|
|
cert_verifier_ = nullptr;
|
|
|
|
weak_ptr_factory_.InvalidateWeakPtrs();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RunResponse(Response* response) {
|
|
|
|
if (custom_response_ == net::ERR_ABORTED) {
|
|
|
|
*(response->verify_result()) = result_;
|
|
|
|
response->callback().Run(error_);
|
|
|
|
} else {
|
|
|
|
response->verify_result()->Reset();
|
|
|
|
response->verify_result()->verified_cert = params_.certificate();
|
|
|
|
cert_verifier_->ct_delegate()->AddCTExcludedHost(params_.hostname());
|
|
|
|
response->callback().Run(custom_response_);
|
|
|
|
}
|
|
|
|
delete response;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Start(net::CRLSet* crl_set,
|
2017-02-08 00:58:05 +03:00
|
|
|
const net::NetLogWithSource& net_log) {
|
2016-11-09 16:05:46 +03:00
|
|
|
int error = cert_verifier_->default_verifier()->Verify(
|
|
|
|
params_, crl_set, &result_,
|
|
|
|
base::Bind(&CertVerifierRequest::OnDefaultVerificationDone,
|
|
|
|
weak_ptr_factory_.GetWeakPtr()),
|
|
|
|
&default_verifier_request_, net_log);
|
|
|
|
if (error != net::ERR_IO_PENDING)
|
|
|
|
OnDefaultVerificationDone(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnDefaultVerificationDone(int error) {
|
|
|
|
error_ = error;
|
2017-02-08 21:34:07 +03:00
|
|
|
std::unique_ptr<VerifyRequestParams> request(new VerifyRequestParams());
|
|
|
|
request->hostname = params_.hostname();
|
|
|
|
request->default_result = net::ErrorToString(error);
|
2017-10-16 12:17:21 +03:00
|
|
|
request->error_code = error;
|
2017-02-08 21:34:07 +03:00
|
|
|
request->certificate = params_.certificate();
|
2017-12-10 14:30:43 +03:00
|
|
|
auto response_callback = base::Bind(&CertVerifierRequest::OnResponseInUI,
|
|
|
|
weak_ptr_factory_.GetWeakPtr());
|
2016-11-09 16:05:46 +03:00
|
|
|
BrowserThread::PostTask(
|
|
|
|
BrowserThread::UI, FROM_HERE,
|
2017-02-08 21:34:07 +03:00
|
|
|
base::Bind(&CertVerifierRequest::OnVerifyRequestInUI,
|
2017-12-10 14:30:43 +03:00
|
|
|
cert_verifier_->verify_proc(), base::Passed(&request),
|
|
|
|
response_callback));
|
2017-02-08 21:34:07 +03:00
|
|
|
}
|
|
|
|
|
2017-12-10 14:30:43 +03:00
|
|
|
static void OnVerifyRequestInUI(
|
|
|
|
const AtomCertVerifier::VerifyProc& verify_proc,
|
|
|
|
std::unique_ptr<VerifyRequestParams> request,
|
|
|
|
const base::Callback<void(int)>& response_callback) {
|
|
|
|
verify_proc.Run(*(request.get()), response_callback);
|
2016-11-09 16:05:46 +03:00
|
|
|
}
|
|
|
|
|
2017-12-10 14:30:43 +03:00
|
|
|
static void OnResponseInUI(base::WeakPtr<CertVerifierRequest> self,
|
|
|
|
int result) {
|
|
|
|
BrowserThread::PostTask(
|
|
|
|
BrowserThread::IO, FROM_HERE,
|
|
|
|
base::Bind(&CertVerifierRequest::NotifyResponseInIO, self, result));
|
2016-11-09 16:05:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void NotifyResponseInIO(int result) {
|
|
|
|
custom_response_ = result;
|
|
|
|
first_response_ = false;
|
|
|
|
// Responding to first request in the list will initiate destruction of
|
|
|
|
// the class, respond to others in the list inside destructor.
|
|
|
|
base::LinkNode<Response>* response_node = response_list_.head();
|
|
|
|
response_node->RemoveFromList();
|
|
|
|
Response* response = response_node->value();
|
|
|
|
RunResponse(response);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddResponseListener(net::CertVerifyResult* verify_result,
|
|
|
|
const net::CompletionCallback& callback) {
|
|
|
|
response_list_.Append(new Response(verify_result, callback));
|
|
|
|
}
|
|
|
|
|
|
|
|
const AtomCertVerifier::RequestParams& params() const { return params_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
using ResponseList = base::LinkedList<Response>;
|
|
|
|
|
|
|
|
const AtomCertVerifier::RequestParams params_;
|
|
|
|
AtomCertVerifier* cert_verifier_;
|
|
|
|
int error_;
|
|
|
|
int custom_response_;
|
|
|
|
bool first_response_;
|
|
|
|
ResponseList response_list_;
|
|
|
|
net::CertVerifyResult result_;
|
|
|
|
std::unique_ptr<AtomCertVerifier::Request> default_verifier_request_;
|
|
|
|
base::WeakPtrFactory<CertVerifierRequest> weak_ptr_factory_;
|
|
|
|
};
|
|
|
|
|
2017-11-15 16:09:22 +03:00
|
|
|
AtomCertVerifier::AtomCertVerifier(brightray::RequireCTDelegate* ct_delegate)
|
2016-10-17 13:33:24 +03:00
|
|
|
: default_cert_verifier_(net::CertVerifier::CreateDefault()),
|
|
|
|
ct_delegate_(ct_delegate) {}
|
2015-11-05 17:06:36 +03:00
|
|
|
|
2016-10-17 13:33:24 +03:00
|
|
|
AtomCertVerifier::~AtomCertVerifier() {}
|
2015-11-05 17:06:36 +03:00
|
|
|
|
2015-11-18 06:17:08 +03:00
|
|
|
void AtomCertVerifier::SetVerifyProc(const VerifyProc& proc) {
|
|
|
|
verify_proc_ = proc;
|
|
|
|
}
|
|
|
|
|
2015-11-05 17:06:36 +03:00
|
|
|
int AtomCertVerifier::Verify(
|
2016-09-06 11:24:37 +03:00
|
|
|
const RequestParams& params,
|
2015-11-05 17:06:36 +03:00
|
|
|
net::CRLSet* crl_set,
|
|
|
|
net::CertVerifyResult* verify_result,
|
|
|
|
const net::CompletionCallback& callback,
|
2016-05-23 04:59:39 +03:00
|
|
|
std::unique_ptr<Request>* out_req,
|
2017-01-24 08:17:43 +03:00
|
|
|
const net::NetLogWithSource& net_log) {
|
2015-11-05 17:06:36 +03:00
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
|
|
|
|
2016-10-17 13:33:24 +03:00
|
|
|
if (verify_proc_.is_null()) {
|
|
|
|
ct_delegate_->ClearCTExcludedHostsList();
|
2016-11-09 16:05:46 +03:00
|
|
|
return default_cert_verifier_->Verify(params, crl_set, verify_result,
|
|
|
|
callback, out_req, net_log);
|
|
|
|
} else {
|
|
|
|
CertVerifierRequest* request = FindRequest(params);
|
|
|
|
if (!request) {
|
|
|
|
out_req->reset();
|
|
|
|
std::unique_ptr<CertVerifierRequest> new_request =
|
|
|
|
base::MakeUnique<CertVerifierRequest>(params, this);
|
|
|
|
new_request->Start(crl_set, net_log);
|
|
|
|
request = new_request.get();
|
|
|
|
*out_req = std::move(new_request);
|
|
|
|
inflight_requests_[params] = request;
|
|
|
|
}
|
|
|
|
request->AddResponseListener(verify_result, callback);
|
|
|
|
|
|
|
|
return net::ERR_IO_PENDING;
|
2016-10-17 13:33:24 +03:00
|
|
|
}
|
2016-11-09 16:05:46 +03:00
|
|
|
}
|
2016-10-17 13:33:24 +03:00
|
|
|
|
2016-11-09 16:05:46 +03:00
|
|
|
bool AtomCertVerifier::SupportsOCSPStapling() {
|
|
|
|
if (verify_proc_.is_null())
|
|
|
|
return default_cert_verifier_->SupportsOCSPStapling();
|
|
|
|
return false;
|
|
|
|
}
|
2015-11-18 06:17:08 +03:00
|
|
|
|
2016-11-09 16:05:46 +03:00
|
|
|
void AtomCertVerifier::RemoveRequest(const RequestParams& params) {
|
|
|
|
auto it = inflight_requests_.find(params);
|
|
|
|
if (it != inflight_requests_.end())
|
|
|
|
inflight_requests_.erase(it);
|
2015-11-17 16:36:36 +03:00
|
|
|
}
|
|
|
|
|
2016-11-09 16:05:46 +03:00
|
|
|
CertVerifierRequest* AtomCertVerifier::FindRequest(
|
|
|
|
const RequestParams& params) {
|
|
|
|
auto it = inflight_requests_.find(params);
|
|
|
|
if (it != inflight_requests_.end())
|
|
|
|
return it->second;
|
|
|
|
return nullptr;
|
2015-11-17 16:36:36 +03:00
|
|
|
}
|
|
|
|
|
2015-11-05 17:06:36 +03:00
|
|
|
} // namespace atom
|