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.
|
|
|
|
|
2024-04-30 17:45:07 +02:00
|
|
|
#include "cef/libcef/browser/x509_certificate_impl.h"
|
2016-09-02 12:01:33 +02:00
|
|
|
|
2024-04-30 17:45:07 +02:00
|
|
|
#include "cef/libcef/browser/x509_cert_principal_impl.h"
|
|
|
|
#include "cef/libcef/common/time_util.h"
|
2018-02-15 01:12:09 +01:00
|
|
|
#include "net/cert/x509_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 {
|
|
|
|
|
2018-02-15 01:12:09 +01:00
|
|
|
CefRefPtr<CefBinaryValue> EncodeCertificate(const CRYPTO_BUFFER* cert_buffer,
|
|
|
|
bool der) {
|
2016-09-02 12:01:33 +02:00
|
|
|
std::string encoded;
|
2018-02-15 01:12:09 +01:00
|
|
|
if (der) {
|
|
|
|
encoded =
|
|
|
|
std::string(net::x509_util::CryptoBufferAsStringPiece(cert_buffer));
|
|
|
|
} else if (!net::X509Certificate::GetPEMEncoded(cert_buffer, &encoded)) {
|
|
|
|
return nullptr;
|
2016-09-02 12:01:33 +02:00
|
|
|
}
|
2023-01-02 23:59:03 +01:00
|
|
|
if (encoded.empty()) {
|
2018-02-15 01:12:09 +01:00
|
|
|
return nullptr;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2018-02-15 01:12:09 +01:00
|
|
|
return CefBinaryValue::Create(encoded.c_str(), encoded.size());
|
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() {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (cert_) {
|
2016-10-27 19:57:12 +02:00
|
|
|
return new CefX509CertPrincipalImpl(cert_->subject());
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-10-27 19:57:12 +02:00
|
|
|
return nullptr;
|
2016-09-02 12:01:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefX509CertPrincipal> CefX509CertificateImpl::GetIssuer() {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (cert_) {
|
2016-10-27 19:57:12 +02:00
|
|
|
return new CefX509CertPrincipalImpl(cert_->issuer());
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-10-27 19:57:12 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-05-31 22:28:43 +02:00
|
|
|
CefBaseTime CefX509CertificateImpl::GetValidStart() {
|
2016-10-27 19:57:12 +02:00
|
|
|
if (cert_) {
|
2022-05-31 22:28:43 +02:00
|
|
|
return cert_->valid_start();
|
2016-10-27 19:57:12 +02:00
|
|
|
}
|
2022-05-31 22:28:43 +02:00
|
|
|
return CefBaseTime();
|
2016-09-02 12:01:33 +02:00
|
|
|
}
|
|
|
|
|
2022-05-31 22:28:43 +02:00
|
|
|
CefBaseTime CefX509CertificateImpl::GetValidExpiry() {
|
2016-10-27 19:57:12 +02:00
|
|
|
if (cert_) {
|
2022-05-31 22:28:43 +02:00
|
|
|
return cert_->valid_expiry();
|
2016-10-27 19:57:12 +02:00
|
|
|
}
|
2022-05-31 22:28:43 +02:00
|
|
|
return CefBaseTime();
|
2016-09-02 12:01:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefBinaryValue> CefX509CertificateImpl::GetDEREncoded() {
|
2016-10-27 19:57:12 +02:00
|
|
|
if (cert_) {
|
2018-02-15 01:12:09 +01:00
|
|
|
const CRYPTO_BUFFER* cert_buffer = cert_->cert_buffer();
|
2023-01-02 23:59:03 +01:00
|
|
|
if (cert_buffer) {
|
2018-02-15 01:12:09 +01:00
|
|
|
return EncodeCertificate(cert_buffer, true);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-10-27 19:57:12 +02:00
|
|
|
}
|
|
|
|
return nullptr;
|
2016-09-02 12:01:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefBinaryValue> CefX509CertificateImpl::GetPEMEncoded() {
|
2016-10-27 19:57:12 +02:00
|
|
|
if (cert_) {
|
2018-02-15 01:12:09 +01:00
|
|
|
const CRYPTO_BUFFER* cert_buffer = cert_->cert_buffer();
|
2023-01-02 23:59:03 +01:00
|
|
|
if (cert_buffer) {
|
2018-02-15 01:12:09 +01:00
|
|
|
return EncodeCertificate(cert_buffer, false);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-10-27 19:57:12 +02:00
|
|
|
}
|
|
|
|
return nullptr;
|
2016-09-02 12:01:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t CefX509CertificateImpl::GetIssuerChainSize() {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (cert_) {
|
2018-02-15 01:12:09 +01:00
|
|
|
return cert_->intermediate_buffers().size();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-10-27 19:57:12 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-07-27 01:19:27 +02:00
|
|
|
void CefX509CertificateImpl::AcquirePrivateKey(
|
2020-03-04 01:29:39 +01:00
|
|
|
base::OnceCallback<void(scoped_refptr<net::SSLPrivateKey>)>
|
2017-07-27 01:19:27 +02:00
|
|
|
private_key_callback) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (identity_) {
|
2020-03-04 01:29:39 +01:00
|
|
|
identity_->AcquirePrivateKey(std::move(private_key_callback));
|
2023-01-02 23:59:03 +01:00
|
|
|
} else {
|
2020-03-04 01:29:39 +01:00
|
|
|
std::move(private_key_callback).Run(nullptr);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-07-27 01:19:27 +02:00
|
|
|
}
|
|
|
|
|
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_) {
|
2018-02-15 01:12:09 +01:00
|
|
|
for (const auto& it : cert_->intermediate_buffers()) {
|
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.
|
2018-02-15 01:12:09 +01:00
|
|
|
chain.push_back(EncodeCertificate(it.get(), der));
|
2016-10-27 19:57:12 +02:00
|
|
|
}
|
|
|
|
}
|
2016-09-02 12:01:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefX509CertificateImpl::GetDEREncodedIssuerChain(
|
|
|
|
CefX509Certificate::IssuerChainBinaryList& chain) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (der_encoded_issuer_chain_.empty()) {
|
2016-10-27 19:57:12 +02:00
|
|
|
GetEncodedIssuerChain(der_encoded_issuer_chain_, true);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-09-02 12:01:33 +02: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()) {
|
2016-10-27 19:57:12 +02:00
|
|
|
GetEncodedIssuerChain(pem_encoded_issuer_chain_, false);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-09-02 12:01:33 +02:00
|
|
|
chain = pem_encoded_issuer_chain_;
|
|
|
|
}
|