7#include <botan/ffi_tls.h>
9#include <botan/assert.h>
10#include <botan/certstor.h>
11#include <botan/system_rng.h>
12#include <botan/tls_policy.h>
13#include <botan/tls_session_manager_memory.h>
14#include <botan/tls_session_manager_noop.h>
15#include <botan/x509_crl.h>
16#include <botan/x509cert.h>
17#include <botan/internal/ffi_cert.h>
18#include <botan/internal/ffi_pkey.h>
19#include <botan/internal/ffi_rng.h>
20#include <botan/internal/ffi_tls_impl.h>
21#include <botan/internal/ffi_util.h>
22#include <botan/internal/stl_util.h>
30#if defined(BOTAN_HAS_CERTSTOR_SYSTEM)
31 #include <botan/certstor_system.h>
37 m_trusted.add_certificate(cert);
41 m_trusted.add_crl(
crl);
45 m_extra_stores.push_back(std::move(store));
49 std::shared_ptr<Botan::Private_Key> key) {
53 .certs = std::move(chain),
54 .key = std::move(key),
59 const std::string& ,
const std::string& ) {
60 std::vector<Botan::Certificate_Store*> stores;
61 stores.push_back(&m_trusted);
62 for(
const auto& store : m_extra_stores) {
63 stores.push_back(store.get());
69 const std::vector<std::string>& cert_key_types,
70 const std::vector<Botan::AlgorithmIdentifier>& ,
71 const std::vector<Botan::X509_DN>& acceptable_CAs,
72 const std::string& type,
73 const std::string& context) {
86 const bool restrict_to_hostname = type ==
"tls-server" && !context.empty() &&
87 std::any_of(m_chains.begin(), m_chains.end(), [&](
const Chain& chain) {
88 return chain.certs.front().matches_dns_name(context);
91 std::vector<Botan::X509_Certificate> result;
93 for(
const auto& chain : m_chains) {
94 if(!cert_key_types.empty() && !
Botan::value_exists(cert_key_types, chain.key->algo_name())) {
98 if(restrict_to_hostname && !chain.certs.front().matches_dns_name(context)) {
102 bool preferred =
true;
104 if(!acceptable_CAs.empty()) {
106 return Botan::value_exists(acceptable_CAs, cert.issuer_dn());
111 result = chain.certs;
116 result = chain.certs;
125 const std::string& ) {
126 for(
const auto& chain : m_chains) {
127 if(chain.certs.front() == cert) {
152 const std::string policy_name(
name !=
nullptr ?
name :
"default");
154 if(policy_name ==
"default") {
155 return ffi_new_object(policy, std::make_shared<Botan::TLS::Policy>());
156 }
else if(policy_name ==
"strict") {
157 return ffi_new_object(policy, std::make_shared<Botan::TLS::Strict_Policy>());
158 }
else if(policy_name ==
"bsi_tr_02102_2") {
159 return ffi_new_object(policy, std::make_shared<Botan::TLS::BSI_TR_02102_2>());
174 return ffi_new_object(policy, std::make_shared<Botan::TLS::Text_Policy>(std::string_view(
text)));
198 [=]() ->
int {
return ffi_new_object(creds, std::make_shared<FFI_TLS_Credentials>()); });
214#if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
219 auto store = std::make_shared<Botan::Certificate_Store_In_Memory>(std::string_view(
path));
220 if(store->all_subjects().empty()) {
223 c.add_store(std::move(store));
236#if defined(BOTAN_HAS_CERTSTOR_SYSTEM)
237 return BOTAN_FFI_VISIT(creds, [](
auto& c) { c.add_store(std::make_shared<Botan::System_Certificate_Store>()); });
256 std::vector<Botan::X509_Certificate> certs;
257 certs.reserve(chain_len);
258 for(
size_t i = 0; i != chain_len; ++i) {
259 certs.push_back(
safe_get(chain[i]));
262 std::shared_ptr<Botan::Private_Key> key_ptr =
safe_get_sp(key);
264 if(key_ptr->public_key()->fingerprint_public() != certs.front().subject_public_key()->fingerprint_public()) {
268 c.add_cert_chain(std::move(certs), std::move(key_ptr));
289 const auto rng_ptr = [&]() -> std::shared_ptr<Botan::RandomNumberGenerator> {
293 return std::make_shared<Botan::System_RNG>();
297 return ffi_new_object(mgr, std::make_shared<Botan::TLS::Session_Manager_In_Memory>(rng_ptr, max_sessions));
309 __func__, [=]() ->
int {
return ffi_new_object(mgr, std::make_shared<Botan::TLS::Session_Manager_Noop>()); });
#define BOTAN_ARG_CHECK(expr, msg)
std::shared_ptr< Botan::Private_Key > private_key_for(const Botan::X509_Certificate &cert, const std::string &type, const std::string &context) override
std::vector< Botan::Certificate_Store * > trusted_certificate_authorities(const std::string &type, const std::string &context) override
void add_crl(const Botan::X509_CRL &crl)
std::vector< Botan::X509_Certificate > find_cert_chain(const std::vector< std::string > &cert_key_types, const std::vector< Botan::AlgorithmIdentifier > &cert_signature_schemes, const std::vector< Botan::X509_DN > &acceptable_CAs, const std::string &type, const std::string &context) override
void add_store(std::shared_ptr< Botan::Certificate_Store > store)
void add_cert_chain(std::vector< Botan::X509_Certificate > chain, std::shared_ptr< Botan::Private_Key > key)
chain is leaf first and not empty; key belongs to the leaf
void add_trusted_cert(const Botan::X509_Certificate &cert)
struct botan_privkey_struct * botan_privkey_t
struct botan_x509_crl_struct * botan_x509_crl_t
struct botan_x509_cert_struct * botan_x509_cert_t
struct botan_rng_struct * botan_rng_t
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
@ BOTAN_FFI_ERROR_NULL_POINTER
@ BOTAN_FFI_ERROR_BAD_PARAMETER
int(* botan_view_str_fn)(botan_view_ctx view_ctx, const char *str, size_t len)
int botan_tls_credentials_use_system_store(botan_tls_credentials_t creds)
int botan_tls_credentials_add_crl(botan_tls_credentials_t creds, botan_x509_crl_t crl)
int botan_tls_session_manager_init_noop(botan_tls_session_manager_t *mgr)
int botan_tls_policy_init(botan_tls_policy_t *policy, const char *name)
int botan_tls_credentials_init(botan_tls_credentials_t *creds)
int botan_tls_credentials_add_cert_chain(botan_tls_credentials_t creds, const botan_x509_cert_t *chain, size_t chain_len, botan_privkey_t key)
int botan_tls_credentials_add_trusted_dir(botan_tls_credentials_t creds, const char *path)
int botan_tls_policy_init_from_text(botan_tls_policy_t *policy, const char *text)
int botan_tls_session_manager_init_memory(botan_tls_session_manager_t *mgr, botan_rng_t rng, size_t max_sessions)
int botan_tls_session_manager_destroy(botan_tls_session_manager_t mgr)
int botan_tls_credentials_add_trusted_cert(botan_tls_credentials_t creds, botan_x509_cert_t cert)
int botan_tls_policy_view_text(botan_tls_policy_t policy, botan_view_ctx ctx, botan_view_str_fn view)
int botan_tls_credentials_destroy(botan_tls_credentials_t creds)
int botan_tls_policy_destroy(botan_tls_policy_t policy)
struct botan_tls_policy_struct * botan_tls_policy_t
struct botan_tls_session_manager_struct * botan_tls_session_manager_t
struct botan_tls_credentials_struct * botan_tls_credentials_t
#define BOTAN_FFI_VISIT(obj, lambda)
#define BOTAN_FFI_CHECKED_DELETE(o)
int invoke_view_callback(botan_view_bin_fn view, botan_view_ctx ctx, std::span< const uint8_t > buf)
auto safe_get_sp(T *p) -> const typename T::smart_pointer_type &
auto safe_get(T *p) -> typename T::element_type &
BOTAN_FFI_ERROR ffi_new_object(T *obj, Args &&... args)
int ffi_guard_thunk(const char *func_name, T thunk)
bool any_null_pointers(Ptrs... ptr)
bool value_exists(const std::vector< T > &vec, const V &val)