2016-09-02 12:01:33 +02:00
|
|
|
// Copyright (c) 2016 The Chromium Embedded Framework Authors. All rights
|
|
|
|
// reserved. Use of this source code is governed by a BSD-style license that
|
|
|
|
// can be found in the LICENSE file.
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
#include "libcef/browser/x509_certificate_impl.h"
|
2017-07-27 01:19:27 +02:00
|
|
|
|
2017-05-19 11:06:00 +02:00
|
|
|
#include "libcef/browser/x509_cert_principal_impl.h"
|
2016-09-02 12:01:33 +02:00
|
|
|
#include "libcef/common/time_util.h"
|
|
|
|
|
2017-07-27 01:19:27 +02:00
|
|
|
#include "net/ssl/ssl_private_key.h"
|
|
|
|
|
2016-09-02 12:01:33 +02:00
|
|
|
namespace {
|
|
|
|
|
2016-10-27 19:57:12 +02:00
|
|
|
CefRefPtr<CefBinaryValue> EncodeCertificate(
|
2017-05-17 11:29:28 +02:00
|
|
|
const net::X509Certificate::OSCertHandle& os_handle,
|
|
|
|
bool der) {
|
2016-10-27 19:57:12 +02:00
|
|
|
CefRefPtr<CefBinaryValue> bin_encoded;
|
2016-09-02 12:01:33 +02:00
|
|
|
std::string encoded;
|
2016-10-27 19:57:12 +02:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
if ((der && net::X509Certificate::GetDEREncoded(os_handle, &encoded)) ||
|
2016-10-27 19:57:12 +02:00
|
|
|
(!der && net::X509Certificate::GetPEMEncoded(os_handle, &encoded))) {
|
|
|
|
bin_encoded = CefBinaryValue::Create(encoded.c_str(), encoded.size());
|
2016-09-02 12:01:33 +02:00
|
|
|
}
|
2016-10-27 19:57:12 +02:00
|
|
|
|
|
|
|
return bin_encoded;
|
2016-09-02 12:01:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2017-07-27 01:19:27 +02:00
|
|
|
CefX509CertificateImpl::CefX509CertificateImpl(
|
|
|
|
std::unique_ptr<net::ClientCertIdentity> identity)
|
|
|
|
: identity_(std::move(identity)), cert_(identity_->certificate()) {}
|
|
|
|
|
2016-09-02 12:01:33 +02:00
|
|
|
CefX509CertificateImpl::CefX509CertificateImpl(
|
2016-10-27 19:57:12 +02:00
|
|
|
scoped_refptr<net::X509Certificate> cert)
|
2017-05-17 11:29:28 +02:00
|
|
|
: cert_(cert) {}
|
2016-09-02 12:01:33 +02:00
|
|
|
|
|
|
|
CefRefPtr<CefX509CertPrincipal> CefX509CertificateImpl::GetSubject() {
|
2016-10-27 19:57:12 +02:00
|
|
|
if (cert_)
|
|
|
|
return new CefX509CertPrincipalImpl(cert_->subject());
|
|
|
|
return nullptr;
|
2016-09-02 12:01:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefX509CertPrincipal> CefX509CertificateImpl::GetIssuer() {
|
2016-10-27 19:57:12 +02:00
|
|
|
if (cert_)
|
|
|
|
return new CefX509CertPrincipalImpl(cert_->issuer());
|
|
|
|
return nullptr;
|
2016-09-02 12:01:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefBinaryValue> CefX509CertificateImpl::GetSerialNumber() {
|
2016-10-27 19:57:12 +02:00
|
|
|
if (cert_) {
|
|
|
|
const std::string& serial = cert_->serial_number();
|
|
|
|
return CefBinaryValue::Create(serial.c_str(), serial.size());
|
|
|
|
}
|
|
|
|
return nullptr;
|
2016-09-02 12:01:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefTime CefX509CertificateImpl::GetValidStart() {
|
2016-10-27 19:57:12 +02:00
|
|
|
CefTime validity;
|
|
|
|
if (cert_) {
|
|
|
|
const base::Time& valid_time = cert_->valid_start();
|
|
|
|
if (!valid_time.is_null())
|
|
|
|
cef_time_from_basetime(valid_time, validity);
|
|
|
|
}
|
|
|
|
return validity;
|
2016-09-02 12:01:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefTime CefX509CertificateImpl::GetValidExpiry() {
|
2016-10-27 19:57:12 +02:00
|
|
|
CefTime validity;
|
|
|
|
if (cert_) {
|
|
|
|
const base::Time& valid_time = cert_->valid_expiry();
|
|
|
|
if (!valid_time.is_null())
|
|
|
|
cef_time_from_basetime(valid_time, validity);
|
|
|
|
}
|
|
|
|
return validity;
|
2016-09-02 12:01:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefBinaryValue> CefX509CertificateImpl::GetDEREncoded() {
|
2016-10-27 19:57:12 +02:00
|
|
|
if (cert_) {
|
|
|
|
net::X509Certificate::OSCertHandle os_handle = cert_->os_cert_handle();
|
|
|
|
if (os_handle)
|
|
|
|
return EncodeCertificate(os_handle, true);
|
|
|
|
}
|
|
|
|
return nullptr;
|
2016-09-02 12:01:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefBinaryValue> CefX509CertificateImpl::GetPEMEncoded() {
|
2016-10-27 19:57:12 +02:00
|
|
|
if (cert_) {
|
|
|
|
net::X509Certificate::OSCertHandle os_handle = cert_->os_cert_handle();
|
|
|
|
if (os_handle)
|
|
|
|
return EncodeCertificate(os_handle, false);
|
|
|
|
}
|
|
|
|
return nullptr;
|
2016-09-02 12:01:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t CefX509CertificateImpl::GetIssuerChainSize() {
|
2016-10-27 19:57:12 +02:00
|
|
|
if (cert_)
|
|
|
|
return cert_->GetIntermediateCertificates().size();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-07-27 01:19:27 +02:00
|
|
|
void CefX509CertificateImpl::AcquirePrivateKey(
|
|
|
|
const base::Callback<void(scoped_refptr<net::SSLPrivateKey>)>&
|
|
|
|
private_key_callback) {
|
|
|
|
if (identity_)
|
|
|
|
identity_->AcquirePrivateKey(private_key_callback);
|
|
|
|
else
|
|
|
|
private_key_callback.Run(nullptr);
|
|
|
|
}
|
|
|
|
|
2016-10-27 19:57:12 +02:00
|
|
|
void CefX509CertificateImpl::GetEncodedIssuerChain(
|
2017-05-17 11:29:28 +02:00
|
|
|
CefX509Certificate::IssuerChainBinaryList& chain,
|
|
|
|
bool der) {
|
2016-10-27 19:57:12 +02:00
|
|
|
chain.clear();
|
|
|
|
if (cert_) {
|
|
|
|
const net::X509Certificate::OSCertHandles& handles =
|
|
|
|
cert_->GetIntermediateCertificates();
|
|
|
|
for (net::X509Certificate::OSCertHandles::const_iterator it =
|
2017-05-17 11:29:28 +02:00
|
|
|
handles.begin();
|
|
|
|
it != handles.end(); it++) {
|
2016-10-27 19:57:12 +02:00
|
|
|
// Add each to the chain, even if one conversion unexpectedly failed.
|
|
|
|
// GetIssuerChainSize depends on these being the same length.
|
|
|
|
chain.push_back(EncodeCertificate(*it, der));
|
|
|
|
}
|
|
|
|
}
|
2016-09-02 12:01:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefX509CertificateImpl::GetDEREncodedIssuerChain(
|
|
|
|
CefX509Certificate::IssuerChainBinaryList& chain) {
|
2016-10-27 19:57:12 +02:00
|
|
|
if (der_encoded_issuer_chain_.empty())
|
|
|
|
GetEncodedIssuerChain(der_encoded_issuer_chain_, true);
|
2016-09-02 12:01:33 +02:00
|
|
|
chain = der_encoded_issuer_chain_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefX509CertificateImpl::GetPEMEncodedIssuerChain(
|
|
|
|
CefX509Certificate::IssuerChainBinaryList& chain) {
|
2016-10-27 19:57:12 +02:00
|
|
|
if (pem_encoded_issuer_chain_.empty())
|
|
|
|
GetEncodedIssuerChain(pem_encoded_issuer_chain_, false);
|
2016-09-02 12:01:33 +02:00
|
|
|
chain = pem_encoded_issuer_chain_;
|
|
|
|
}
|