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
|
|
|
|
|
|
|
#include "atom/browser/browser.h"
|
|
|
|
#include "atom/common/native_mate_converters/net_converter.h"
|
2015-11-12 19:55:23 +00:00
|
|
|
#include "base/sha1.h"
|
|
|
|
#include "base/stl_util.h"
|
2015-11-05 14:06:36 +00:00
|
|
|
#include "content/public/browser/browser_thread.h"
|
|
|
|
#include "net/base/net_errors.h"
|
|
|
|
|
|
|
|
using content::BrowserThread;
|
|
|
|
|
|
|
|
namespace atom {
|
|
|
|
|
2015-11-12 19:55:23 +00:00
|
|
|
AtomCertVerifier::RequestParams::RequestParams(
|
|
|
|
const net::SHA1HashValue cert_fingerprint,
|
|
|
|
const net::SHA1HashValue ca_fingerprint,
|
|
|
|
const std::string& hostname_arg,
|
|
|
|
const std::string& ocsp_response_arg,
|
|
|
|
int flags_arg)
|
|
|
|
: hostname(hostname_arg),
|
|
|
|
ocsp_response(ocsp_response_arg),
|
|
|
|
flags(flags_arg) {
|
|
|
|
hash_values.reserve(3);
|
|
|
|
net::SHA1HashValue ocsp_hash;
|
|
|
|
base::SHA1HashBytes(
|
|
|
|
reinterpret_cast<const unsigned char*>(ocsp_response.data()),
|
|
|
|
ocsp_response.size(), ocsp_hash.data);
|
|
|
|
hash_values.push_back(ocsp_hash);
|
|
|
|
hash_values.push_back(cert_fingerprint);
|
|
|
|
hash_values.push_back(ca_fingerprint);
|
|
|
|
}
|
2015-11-05 14:06:36 +00:00
|
|
|
|
2015-11-12 19:55:23 +00:00
|
|
|
bool AtomCertVerifier::RequestParams::operator<(
|
|
|
|
const RequestParams& other) const {
|
|
|
|
if (flags != other.flags)
|
|
|
|
return flags < other.flags;
|
|
|
|
if (hostname != other.hostname)
|
|
|
|
return hostname < other.hostname;
|
|
|
|
return std::lexicographical_compare(
|
|
|
|
hash_values.begin(),
|
|
|
|
hash_values.end(),
|
|
|
|
other.hash_values.begin(),
|
|
|
|
other.hash_values.end(),
|
|
|
|
net::SHA1HashValueLessThan());
|
|
|
|
}
|
|
|
|
|
|
|
|
void AtomCertVerifier::CertVerifyRequest::RunResult(int result) {
|
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
|
|
|
|
|
|
|
for (auto& callback : callbacks_)
|
|
|
|
callback.Run(result);
|
|
|
|
cert_verifier_->RemoveRequest(this);
|
|
|
|
}
|
2015-11-05 14:06:36 +00:00
|
|
|
|
2015-11-12 19:55:23 +00:00
|
|
|
void AtomCertVerifier::CertVerifyRequest::DelegateToDefaultVerifier() {
|
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
2015-11-05 14:06:36 +00:00
|
|
|
|
2015-11-12 19:55:23 +00:00
|
|
|
int rv = cert_verifier_->default_cert_verifier()->Verify(
|
|
|
|
certificate_.get(),
|
|
|
|
key_.hostname,
|
|
|
|
key_.ocsp_response,
|
|
|
|
key_.flags,
|
|
|
|
crl_set_.get(),
|
|
|
|
verify_result_,
|
2015-11-17 11:00:33 +00:00
|
|
|
base::Bind(&CertVerifyRequest::RunResult, this),
|
2015-11-16 13:28:37 +00:00
|
|
|
out_req_,
|
2015-11-12 19:55:23 +00:00
|
|
|
net_log_);
|
|
|
|
|
2015-11-16 13:28:37 +00:00
|
|
|
if (rv != net::ERR_IO_PENDING)
|
|
|
|
RunResult(rv);
|
2015-11-05 14:06:36 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 19:55:23 +00:00
|
|
|
void AtomCertVerifier::CertVerifyRequest::ContinueWithResult(int result) {
|
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
|
|
|
|
|
|
|
if (handled_)
|
|
|
|
return;
|
|
|
|
|
|
|
|
handled_ = true;
|
|
|
|
|
|
|
|
if (result != net::ERR_IO_PENDING) {
|
|
|
|
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
|
|
|
|
base::Bind(&CertVerifyRequest::RunResult,
|
2015-11-17 11:00:33 +00:00
|
|
|
this,
|
2015-11-12 19:55:23 +00:00
|
|
|
result));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
BrowserThread::PostTask(
|
|
|
|
BrowserThread::IO, FROM_HERE,
|
2015-11-17 11:00:33 +00:00
|
|
|
base::Bind(&CertVerifyRequest::DelegateToDefaultVerifier, this));
|
2015-11-12 19:55:23 +00:00
|
|
|
}
|
2015-11-05 14:06:36 +00:00
|
|
|
|
2015-11-16 13:28:37 +00:00
|
|
|
AtomCertVerifier::AtomCertVerifier()
|
|
|
|
: delegate_(nullptr) {
|
2015-11-05 14:06:36 +00:00
|
|
|
default_cert_verifier_.reset(net::CertVerifier::CreateDefault());
|
|
|
|
}
|
|
|
|
|
|
|
|
AtomCertVerifier::~AtomCertVerifier() {
|
|
|
|
}
|
|
|
|
|
|
|
|
int AtomCertVerifier::Verify(
|
|
|
|
net::X509Certificate* cert,
|
|
|
|
const std::string& hostname,
|
|
|
|
const std::string& ocsp_response,
|
|
|
|
int flags,
|
|
|
|
net::CRLSet* crl_set,
|
|
|
|
net::CertVerifyResult* verify_result,
|
|
|
|
const net::CompletionCallback& callback,
|
|
|
|
scoped_ptr<Request>* out_req,
|
|
|
|
const net::BoundNetLog& net_log) {
|
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
|
|
|
|
2015-11-16 13:28:37 +00:00
|
|
|
if (callback.is_null() || !verify_result || hostname.empty() || !delegate_)
|
2015-11-05 14:06:36 +00:00
|
|
|
return net::ERR_INVALID_ARGUMENT;
|
|
|
|
|
2015-11-12 19:55:23 +00:00
|
|
|
const RequestParams key(cert->fingerprint(),
|
|
|
|
cert->ca_fingerprint(),
|
|
|
|
hostname,
|
|
|
|
ocsp_response,
|
|
|
|
flags);
|
|
|
|
|
|
|
|
CertVerifyRequest* request = FindRequest(key);
|
|
|
|
|
|
|
|
if (!request) {
|
|
|
|
request = new CertVerifyRequest(this,
|
|
|
|
key,
|
|
|
|
cert,
|
|
|
|
crl_set,
|
|
|
|
verify_result,
|
|
|
|
out_req,
|
|
|
|
net_log);
|
|
|
|
requests_.insert(make_scoped_refptr(request));
|
|
|
|
|
2015-11-05 14:06:36 +00:00
|
|
|
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
|
2015-11-16 13:28:37 +00:00
|
|
|
base::Bind(&Delegate::RequestCertVerification,
|
|
|
|
base::Unretained(delegate_),
|
2015-11-12 19:55:23 +00:00
|
|
|
make_scoped_refptr(request)));
|
2015-11-05 14:06:36 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 19:55:23 +00:00
|
|
|
request->AddCompletionCallback(callback);
|
|
|
|
|
|
|
|
return net::ERR_IO_PENDING;
|
2015-11-05 14:06:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AtomCertVerifier::SupportsOCSPStapling() {
|
2015-11-12 19:55:23 +00:00
|
|
|
return true;
|
2015-11-05 14:06:36 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 19:55:23 +00:00
|
|
|
AtomCertVerifier::CertVerifyRequest* AtomCertVerifier::FindRequest(
|
|
|
|
const RequestParams& key) {
|
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
|
|
|
|
|
|
|
auto it = std::lower_bound(requests_.begin(),
|
|
|
|
requests_.end(),
|
|
|
|
key,
|
|
|
|
CertVerifyRequestToRequestParamsComparator());
|
|
|
|
if (it != requests_.end() && !(key < (*it)->key()))
|
|
|
|
return (*it).get();
|
|
|
|
return nullptr;
|
2015-11-05 14:06:36 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 19:55:23 +00:00
|
|
|
void AtomCertVerifier::RemoveRequest(CertVerifyRequest* request) {
|
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
|
|
|
|
|
|
|
bool erased = requests_.erase(request) == 1;
|
|
|
|
DCHECK(erased);
|
2015-11-05 14:06:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace atom
|