cef/libcef/browser/x509_certificate_impl.cc

140 lines
3.8 KiB
C++
Raw Normal View History

// 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.
#include "cef/libcef/browser/x509_certificate_impl.h"
#include "cef/libcef/browser/x509_cert_principal_impl.h"
#include "cef/libcef/common/time_util.h"
#include "net/cert/x509_util.h"
#include "net/ssl/ssl_private_key.h"
namespace {
CefRefPtr<CefBinaryValue> EncodeCertificate(const CRYPTO_BUFFER* cert_buffer,
bool der) {
std::string encoded;
if (der) {
encoded =
std::string(net::x509_util::CryptoBufferAsStringPiece(cert_buffer));
} else if (!net::X509Certificate::GetPEMEncoded(cert_buffer, &encoded)) {
return nullptr;
}
2023-01-02 23:59:03 +01:00
if (encoded.empty()) {
return nullptr;
2023-01-02 23:59:03 +01:00
}
return CefBinaryValue::Create(encoded.c_str(), encoded.size());
}
} // namespace
CefX509CertificateImpl::CefX509CertificateImpl(
std::unique_ptr<net::ClientCertIdentity> identity)
: identity_(std::move(identity)), cert_(identity_->certificate()) {}
CefX509CertificateImpl::CefX509CertificateImpl(
scoped_refptr<net::X509Certificate> cert)
: cert_(cert) {}
CefRefPtr<CefX509CertPrincipal> CefX509CertificateImpl::GetSubject() {
2023-01-02 23:59:03 +01:00
if (cert_) {
return new CefX509CertPrincipalImpl(cert_->subject());
2023-01-02 23:59:03 +01:00
}
return nullptr;
}
CefRefPtr<CefX509CertPrincipal> CefX509CertificateImpl::GetIssuer() {
2023-01-02 23:59:03 +01:00
if (cert_) {
return new CefX509CertPrincipalImpl(cert_->issuer());
2023-01-02 23:59:03 +01:00
}
return nullptr;
}
CefRefPtr<CefBinaryValue> CefX509CertificateImpl::GetSerialNumber() {
if (cert_) {
const std::string& serial = cert_->serial_number();
return CefBinaryValue::Create(serial.c_str(), serial.size());
}
return nullptr;
}
CefBaseTime CefX509CertificateImpl::GetValidStart() {
if (cert_) {
return cert_->valid_start();
}
return CefBaseTime();
}
CefBaseTime CefX509CertificateImpl::GetValidExpiry() {
if (cert_) {
return cert_->valid_expiry();
}
return CefBaseTime();
}
CefRefPtr<CefBinaryValue> CefX509CertificateImpl::GetDEREncoded() {
if (cert_) {
const CRYPTO_BUFFER* cert_buffer = cert_->cert_buffer();
2023-01-02 23:59:03 +01:00
if (cert_buffer) {
return EncodeCertificate(cert_buffer, true);
2023-01-02 23:59:03 +01:00
}
}
return nullptr;
}
CefRefPtr<CefBinaryValue> CefX509CertificateImpl::GetPEMEncoded() {
if (cert_) {
const CRYPTO_BUFFER* cert_buffer = cert_->cert_buffer();
2023-01-02 23:59:03 +01:00
if (cert_buffer) {
return EncodeCertificate(cert_buffer, false);
2023-01-02 23:59:03 +01:00
}
}
return nullptr;
}
size_t CefX509CertificateImpl::GetIssuerChainSize() {
2023-01-02 23:59:03 +01:00
if (cert_) {
return cert_->intermediate_buffers().size();
2023-01-02 23:59:03 +01:00
}
return 0;
}
void CefX509CertificateImpl::AcquirePrivateKey(
base::OnceCallback<void(scoped_refptr<net::SSLPrivateKey>)>
private_key_callback) {
2023-01-02 23:59:03 +01:00
if (identity_) {
identity_->AcquirePrivateKey(std::move(private_key_callback));
2023-01-02 23:59:03 +01:00
} else {
std::move(private_key_callback).Run(nullptr);
2023-01-02 23:59:03 +01:00
}
}
void CefX509CertificateImpl::GetEncodedIssuerChain(
CefX509Certificate::IssuerChainBinaryList& chain,
bool der) {
chain.clear();
if (cert_) {
for (const auto& it : cert_->intermediate_buffers()) {
// Add each to the chain, even if one conversion unexpectedly failed.
// GetIssuerChainSize depends on these being the same length.
chain.push_back(EncodeCertificate(it.get(), der));
}
}
}
void CefX509CertificateImpl::GetDEREncodedIssuerChain(
CefX509Certificate::IssuerChainBinaryList& chain) {
2023-01-02 23:59:03 +01:00
if (der_encoded_issuer_chain_.empty()) {
GetEncodedIssuerChain(der_encoded_issuer_chain_, true);
2023-01-02 23:59:03 +01:00
}
chain = der_encoded_issuer_chain_;
}
void CefX509CertificateImpl::GetPEMEncodedIssuerChain(
CefX509Certificate::IssuerChainBinaryList& chain) {
2023-01-02 23:59:03 +01:00
if (pem_encoded_issuer_chain_.empty()) {
GetEncodedIssuerChain(pem_encoded_issuer_chain_, false);
2023-01-02 23:59:03 +01:00
}
chain = pem_encoded_issuer_chain_;
}