1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/cert/test_root_certs.h"
#include <string>
#include <string_view>
#include <utility>
#include "net/cert/x509_certificate.h"
#include "net/cert/x509_util.h"
#include "third_party/boringssl/src/include/openssl/pool.h"
#include "third_party/boringssl/src/pki/cert_errors.h"
#include "third_party/boringssl/src/pki/trust_store.h"
namespace net {
namespace {
bool g_has_instance = false;
base::LazyInstance<TestRootCerts>::Leaky
g_test_root_certs = LAZY_INSTANCE_INITIALIZER;
} // namespace
bool ThreadSafeTrustStoreInMemory::IsEmpty() const {
base::AutoLock lock(lock_);
return impl_.IsEmpty();
}
void ThreadSafeTrustStoreInMemory::Clear() {
base::AutoLock lock(lock_);
impl_.Clear();
}
void ThreadSafeTrustStoreInMemory::AddCertificate(
std::shared_ptr<const bssl::ParsedCertificate> cert,
const bssl::CertificateTrust& trust) {
base::AutoLock lock(lock_);
impl_.AddCertificate(std::move(cert), trust);
}
void ThreadSafeTrustStoreInMemory::SyncGetIssuersOf(
const bssl::ParsedCertificate* cert,
bssl::ParsedCertificateList* issuers) {
base::AutoLock lock(lock_);
impl_.SyncGetIssuersOf(cert, issuers);
}
bssl::CertificateTrust ThreadSafeTrustStoreInMemory::GetTrust(
const bssl::ParsedCertificate* cert) {
base::AutoLock lock(lock_);
return impl_.GetTrust(cert);
}
// static
TestRootCerts* TestRootCerts::GetInstance() {
return g_test_root_certs.Pointer();
}
bool TestRootCerts::HasInstance() {
return g_has_instance;
}
bool TestRootCerts::Add(X509Certificate* certificate,
bssl::CertificateTrust trust) {
base::AutoLock lock(lock_);
bssl::CertErrors errors;
std::shared_ptr<const bssl::ParsedCertificate> parsed =
bssl::ParsedCertificate::Create(
bssl::UpRef(certificate->cert_buffer()),
x509_util::DefaultParseCertificateOptions(), &errors);
if (!parsed) {
return false;
}
test_trust_store_.AddCertificate(std::move(parsed), trust);
if (trust.HasUnspecifiedTrust() || trust.IsDistrusted()) {
// TestRootCerts doesn't support passing the specific trust settings into
// the OS implementations in any case, but in the case of unspecified trust
// or explicit distrust, simply not passing the certs to the OS
// implementation is better than nothing.
return true;
}
return AddImpl(certificate);
}
void TestRootCerts::AddKnownRoot(base::span<const uint8_t> der_cert) {
base::AutoLock lock(lock_);
test_known_roots_.insert(std::string(
reinterpret_cast<const char*>(der_cert.data()), der_cert.size()));
}
void TestRootCerts::Clear() {
base::AutoLock lock(lock_);
ClearImpl();
test_trust_store_.Clear();
test_known_roots_.clear();
}
bool TestRootCerts::IsEmpty() const {
return test_trust_store_.IsEmpty();
}
bool TestRootCerts::IsKnownRoot(base::span<const uint8_t> der_cert) const {
base::AutoLock lock(lock_);
return test_known_roots_.find(
std::string_view(reinterpret_cast<const char*>(der_cert.data()),
der_cert.size())) != test_known_roots_.end();
}
TestRootCerts::TestRootCerts() {
Init();
g_has_instance = true;
}
ScopedTestRoot::ScopedTestRoot() = default;
ScopedTestRoot::ScopedTestRoot(scoped_refptr<X509Certificate> cert,
bssl::CertificateTrust trust) {
Reset({std::move(cert)}, trust);
}
ScopedTestRoot::ScopedTestRoot(CertificateList certs,
bssl::CertificateTrust trust) {
Reset(std::move(certs), trust);
}
ScopedTestRoot::ScopedTestRoot(ScopedTestRoot&& other) {
*this = std::move(other);
}
ScopedTestRoot& ScopedTestRoot::operator=(ScopedTestRoot&& other) {
CertificateList tmp_certs;
tmp_certs.swap(other.certs_);
Reset(std::move(tmp_certs));
return *this;
}
ScopedTestRoot::~ScopedTestRoot() {
Reset({});
}
void ScopedTestRoot::Reset(CertificateList certs,
bssl::CertificateTrust trust) {
if (!certs_.empty())
TestRootCerts::GetInstance()->Clear();
for (const auto& cert : certs)
TestRootCerts::GetInstance()->Add(cert.get(), trust);
certs_ = std::move(certs);
}
ScopedTestKnownRoot::ScopedTestKnownRoot() = default;
ScopedTestKnownRoot::ScopedTestKnownRoot(X509Certificate* cert) {
TestRootCerts::GetInstance()->AddKnownRoot(
x509_util::CryptoBufferAsSpan(cert->cert_buffer()));
}
ScopedTestKnownRoot::~ScopedTestKnownRoot() {
TestRootCerts::GetInstance()->Clear();
}
} // namespace net
|