2015-11-05 14:06:36 +00: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 11:03:09 +00:00
|
|
|
#include "atom/browser/net/atom_cert_verifier.h"
|
2015-11-05 14:06:36 +00:00
|
|
|
|
2018-09-13 00:25:56 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2015-11-05 14:06:36 +00:00
|
|
|
#include "atom/browser/browser.h"
|
2018-10-19 18:51:43 +00:00
|
|
|
#include "atom/browser/net/require_ct_delegate.h"
|
2015-11-05 14:06:36 +00:00
|
|
|
#include "atom/common/native_mate_converters/net_converter.h"
|
2016-11-09 13:05:46 +00:00
|
|
|
#include "base/containers/linked_list.h"
|
|
|
|
#include "base/memory/weak_ptr.h"
|
2015-11-05 14:06:36 +00:00
|
|
|
#include "content/public/browser/browser_thread.h"
|
|
|
|
#include "net/base/net_errors.h"
|
2016-10-17 10:33:24 +00:00
|
|
|
#include "net/cert/cert_verify_result.h"
|
2015-11-17 11:44:55 +00:00
|
|
|
#include "net/cert/x509_certificate.h"
|
2015-11-05 14:06:36 +00:00
|
|
|
|
|
|
|
using content::BrowserThread;
|
|
|
|
|
|
|
|
namespace atom {
|
|
|
|
|
2018-04-17 23:37:22 +00:00
|
|
|
VerifyRequestParams::VerifyRequestParams() = default;
|
|
|
|
VerifyRequestParams::~VerifyRequestParams() = default;
|
|
|
|
VerifyRequestParams::VerifyRequestParams(const VerifyRequestParams&) = default;
|
|
|
|
|
2015-11-18 03:17:08 +00:00
|
|
|
namespace {
|
2015-11-12 19:55:23 +00:00
|
|
|
|
2016-11-09 13:05:46 +00:00
|
|
|
class Response : public base::LinkNode<Response> {
|
|
|
|
public:
|
|
|
|
Response(net::CertVerifyResult* verify_result,
|
2018-10-02 21:36:10 +00:00
|
|
|
net::CompletionOnceCallback callback)
|
|
|
|
: verify_result_(verify_result), callback_(std::move(callback)) {}
|
2016-11-09 13:05:46 +00:00
|
|
|
net::CertVerifyResult* verify_result() { return verify_result_; }
|
2018-10-02 21:36:10 +00:00
|
|
|
net::CompletionOnceCallback callback() { return std::move(callback_); }
|
2016-11-09 13:05:46 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
net::CertVerifyResult* verify_result_;
|
2018-10-02 21:36:10 +00:00
|
|
|
net::CompletionOnceCallback callback_;
|
2016-11-09 13:05:46 +00:00
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(Response);
|
|
|
|
};
|
2015-11-17 11:44:55 +00:00
|
|
|
|
2015-11-18 03:17:08 +00:00
|
|
|
} // namespace
|
|
|
|
|
2016-11-09 13:05:46 +00:00
|
|
|
class CertVerifierRequest : public AtomCertVerifier::Request {
|
|
|
|
public:
|
|
|
|
CertVerifierRequest(const AtomCertVerifier::RequestParams& params,
|
|
|
|
AtomCertVerifier* cert_verifier)
|
|
|
|
: params_(params),
|
|
|
|
cert_verifier_(cert_verifier),
|
|
|
|
weak_ptr_factory_(this) {}
|
|
|
|
|
2017-03-30 21:13:23 +00:00
|
|
|
~CertVerifierRequest() override {
|
2016-11-09 13:05:46 +00: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;
|
|
|
|
}
|
|
|
|
|
2018-10-25 16:45:18 +00:00
|
|
|
void Start(const net::NetLogWithSource& net_log) {
|
2016-11-09 13:05:46 +00:00
|
|
|
int error = cert_verifier_->default_verifier()->Verify(
|
2018-10-25 16:45:18 +00:00
|
|
|
params_, &result_,
|
2016-11-09 13:05:46 +00:00
|
|
|
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;
|
2018-06-18 07:32:55 +00:00
|
|
|
auto request = std::make_unique<VerifyRequestParams>();
|
2017-02-08 18:34:07 +00:00
|
|
|
request->hostname = params_.hostname();
|
|
|
|
request->default_result = net::ErrorToString(error);
|
2017-10-16 09:17:21 +00:00
|
|
|
request->error_code = error;
|
2017-02-08 18:34:07 +00:00
|
|
|
request->certificate = params_.certificate();
|
2017-12-10 11:30:43 +00:00
|
|
|
auto response_callback = base::Bind(&CertVerifierRequest::OnResponseInUI,
|
|
|
|
weak_ptr_factory_.GetWeakPtr());
|
2016-11-09 13:05:46 +00:00
|
|
|
BrowserThread::PostTask(
|
|
|
|
BrowserThread::UI, FROM_HERE,
|
2018-04-20 10:55:05 +00:00
|
|
|
base::BindOnce(&CertVerifierRequest::OnVerifyRequestInUI,
|
|
|
|
cert_verifier_->verify_proc(), std::move(request),
|
|
|
|
response_callback));
|
2017-02-08 18:34:07 +00:00
|
|
|
}
|
|
|
|
|
2017-12-10 11:30:43 +00: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 13:05:46 +00:00
|
|
|
}
|
|
|
|
|
2017-12-10 11:30:43 +00:00
|
|
|
static void OnResponseInUI(base::WeakPtr<CertVerifierRequest> self,
|
|
|
|
int result) {
|
|
|
|
BrowserThread::PostTask(
|
|
|
|
BrowserThread::IO, FROM_HERE,
|
2018-04-20 10:55:05 +00:00
|
|
|
base::BindOnce(&CertVerifierRequest::NotifyResponseInIO, self, result));
|
2016-11-09 13:05:46 +00: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,
|
2018-10-02 21:36:10 +00:00
|
|
|
net::CompletionOnceCallback callback) {
|
|
|
|
response_list_.Append(new Response(verify_result, std::move(callback)));
|
2016-11-09 13:05:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const AtomCertVerifier::RequestParams& params() const { return params_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
using ResponseList = base::LinkedList<Response>;
|
|
|
|
|
|
|
|
const AtomCertVerifier::RequestParams params_;
|
|
|
|
AtomCertVerifier* cert_verifier_;
|
2018-05-21 22:18:38 +00:00
|
|
|
int error_ = net::ERR_IO_PENDING;
|
|
|
|
int custom_response_ = net::ERR_IO_PENDING;
|
|
|
|
bool first_response_ = true;
|
2016-11-09 13:05:46 +00:00
|
|
|
ResponseList response_list_;
|
|
|
|
net::CertVerifyResult result_;
|
|
|
|
std::unique_ptr<AtomCertVerifier::Request> default_verifier_request_;
|
|
|
|
base::WeakPtrFactory<CertVerifierRequest> weak_ptr_factory_;
|
|
|
|
};
|
|
|
|
|
2018-10-19 18:51:43 +00:00
|
|
|
AtomCertVerifier::AtomCertVerifier(RequireCTDelegate* ct_delegate)
|
2016-10-17 10:33:24 +00:00
|
|
|
: default_cert_verifier_(net::CertVerifier::CreateDefault()),
|
|
|
|
ct_delegate_(ct_delegate) {}
|
2015-11-05 14:06:36 +00:00
|
|
|
|
2016-10-17 10:33:24 +00:00
|
|
|
AtomCertVerifier::~AtomCertVerifier() {}
|
2015-11-05 14:06:36 +00:00
|
|
|
|
2015-11-18 03:17:08 +00:00
|
|
|
void AtomCertVerifier::SetVerifyProc(const VerifyProc& proc) {
|
|
|
|
verify_proc_ = proc;
|
|
|
|
}
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
int AtomCertVerifier::Verify(const RequestParams& params,
|
|
|
|
net::CertVerifyResult* verify_result,
|
2018-10-02 21:36:10 +00:00
|
|
|
net::CompletionOnceCallback callback,
|
2018-04-18 01:55:30 +00:00
|
|
|
std::unique_ptr<Request>* out_req,
|
|
|
|
const net::NetLogWithSource& net_log) {
|
2015-11-05 14:06:36 +00:00
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
|
|
|
|
2016-10-17 10:33:24 +00:00
|
|
|
if (verify_proc_.is_null()) {
|
|
|
|
ct_delegate_->ClearCTExcludedHostsList();
|
2018-10-02 21:36:10 +00:00
|
|
|
return default_cert_verifier_->Verify(
|
2018-10-25 16:45:18 +00:00
|
|
|
params, verify_result, std::move(callback), out_req, net_log);
|
2016-11-09 13:05:46 +00:00
|
|
|
} else {
|
|
|
|
CertVerifierRequest* request = FindRequest(params);
|
|
|
|
if (!request) {
|
|
|
|
out_req->reset();
|
2018-06-18 07:32:55 +00:00
|
|
|
auto new_request = std::make_unique<CertVerifierRequest>(params, this);
|
2018-10-25 16:45:18 +00:00
|
|
|
new_request->Start(net_log);
|
2016-11-09 13:05:46 +00:00
|
|
|
request = new_request.get();
|
|
|
|
*out_req = std::move(new_request);
|
|
|
|
inflight_requests_[params] = request;
|
|
|
|
}
|
2018-10-02 21:36:10 +00:00
|
|
|
request->AddResponseListener(verify_result, std::move(callback));
|
2016-11-09 13:05:46 +00:00
|
|
|
|
|
|
|
return net::ERR_IO_PENDING;
|
2016-10-17 10:33:24 +00:00
|
|
|
}
|
2016-11-09 13:05:46 +00:00
|
|
|
}
|
2016-10-17 10:33:24 +00:00
|
|
|
|
2016-11-09 13:05:46 +00: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 13:36:36 +00:00
|
|
|
}
|
|
|
|
|
2016-11-09 13:05:46 +00: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 13:36:36 +00:00
|
|
|
}
|
|
|
|
|
2015-11-05 14:06:36 +00:00
|
|
|
} // namespace atom
|