Botan 3.14.1
Crypto and TLS for C&
ffi_tls.cpp
Go to the documentation of this file.
1/*
2* (C) 2026 Moritz Schmitt
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#include <botan/ffi_tls.h>
8
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>
23#include <algorithm>
24#include <memory>
25#include <string>
26#include <string_view>
27#include <utility>
28#include <vector>
29
30#if defined(BOTAN_HAS_CERTSTOR_SYSTEM)
31 #include <botan/certstor_system.h>
32#endif
33
34namespace Botan_FFI {
35
37 m_trusted.add_certificate(cert);
38}
39
41 m_trusted.add_crl(crl);
42}
43
44void FFI_TLS_Credentials::add_store(std::shared_ptr<Botan::Certificate_Store> store) {
45 m_extra_stores.push_back(std::move(store));
46}
47
48void FFI_TLS_Credentials::add_cert_chain(std::vector<Botan::X509_Certificate> chain,
49 std::shared_ptr<Botan::Private_Key> key) {
50 BOTAN_ARG_CHECK(!chain.empty(), "Certificate chain must not be empty");
51 BOTAN_ARG_CHECK(key != nullptr, "Private key must not be null");
52 m_chains.push_back({
53 .certs = std::move(chain),
54 .key = std::move(key),
55 });
56}
57
58std::vector<Botan::Certificate_Store*> FFI_TLS_Credentials::trusted_certificate_authorities(
59 const std::string& /*type*/, const std::string& /*context*/) {
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());
64 }
65 return stores;
66}
67
68std::vector<Botan::X509_Certificate> FFI_TLS_Credentials::find_cert_chain(
69 const std::vector<std::string>& cert_key_types,
70 const std::vector<Botan::AlgorithmIdentifier>& /*cert_signature_schemes*/,
71 const std::vector<Botan::X509_DN>& acceptable_CAs,
72 const std::string& type,
73 const std::string& context) {
74 // A chain is usable if the peer accepts the type of its key. On a server,
75 // if any chain (of whatever key type) has a leaf certificate matching the
76 // server name the client asked for, only such chains are offered; the
77 // TLS 1.2 server asks once per key type, so a chain for another name must
78 // not be offered under a different key type when a matching one exists.
79 // Among the usable chains the first one issued by a certificate authority
80 // the peer named wins, otherwise the first usable chain is used, since the
81 // peer may still accept it. The names the peer sends may be those of trust
82 // anchors as well as of subordinate CAs (RFC 8446 4.2.4); a Botan server for
83 // instance sends the subjects of its trust stores. A root's name matches the
84 // issuer of the last certificate of a chain, not the issuer of the leaf, so
85 // every certificate of the chain is checked.
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);
89 });
90
91 std::vector<Botan::X509_Certificate> result;
92
93 for(const auto& chain : m_chains) {
94 if(!cert_key_types.empty() && !Botan::value_exists(cert_key_types, chain.key->algo_name())) {
95 continue;
96 }
97
98 if(restrict_to_hostname && !chain.certs.front().matches_dns_name(context)) {
99 continue;
100 }
101
102 bool preferred = true;
103
104 if(!acceptable_CAs.empty()) {
105 preferred = std::any_of(chain.certs.begin(), chain.certs.end(), [&](const Botan::X509_Certificate& cert) {
106 return Botan::value_exists(acceptable_CAs, cert.issuer_dn());
107 });
108 }
109
110 if(preferred) {
111 result = chain.certs;
112 break;
113 }
114
115 if(result.empty()) {
116 result = chain.certs;
117 }
118 }
119
120 return result;
121}
122
123std::shared_ptr<Botan::Private_Key> FFI_TLS_Credentials::private_key_for(const Botan::X509_Certificate& cert,
124 const std::string& /*type*/,
125 const std::string& /*context*/) {
126 for(const auto& chain : m_chains) {
127 if(chain.certs.front() == cert) {
128 return chain.key;
129 }
130 }
131 return nullptr;
132}
133
134} // namespace Botan_FFI
135
136extern "C" {
137
138using namespace Botan_FFI;
139
140/*
141* Policies
142*/
143
145 if(any_null_pointers(policy)) {
147 }
148
149 *policy = nullptr;
150
151 return ffi_guard_thunk(__func__, [=]() -> int {
152 const std::string policy_name(name != nullptr ? name : "default");
153
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>());
160 }
161
163 });
164}
165
167 if(any_null_pointers(policy, text)) {
169 }
170
171 *policy = nullptr;
172
173 return ffi_guard_thunk(__func__, [=]() -> int {
174 return ffi_new_object(policy, std::make_shared<Botan::TLS::Text_Policy>(std::string_view(text)));
175 });
176}
177
179 return BOTAN_FFI_VISIT(policy, [=](const auto& p) { return invoke_view_callback(view, ctx, p.to_string()); });
180}
181
185
186/*
187* Credentials
188*/
189
191 if(any_null_pointers(creds)) {
193 }
194
195 *creds = nullptr;
196
197 return ffi_guard_thunk(__func__,
198 [=]() -> int { return ffi_new_object(creds, std::make_shared<FFI_TLS_Credentials>()); });
199}
200
202 return BOTAN_FFI_VISIT(creds, [=](auto& c) { c.add_trusted_cert(safe_get(cert)); });
203}
204
206 return BOTAN_FFI_VISIT(creds, [=](auto& c) { c.add_crl(safe_get(crl)); });
207}
208
210 if(any_null_pointers(creds, path)) {
212 }
213
214#if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
215 return BOTAN_FFI_VISIT(creds, [=](auto& c) -> int {
216 // The directory constructor silently yields an empty store for a
217 // missing or unreadable directory, which would make every verification
218 // fail without any hint why; report that as an error instead.
219 auto store = std::make_shared<Botan::Certificate_Store_In_Memory>(std::string_view(path));
220 if(store->all_subjects().empty()) {
222 }
223 c.add_store(std::move(store));
224 return BOTAN_FFI_SUCCESS;
225 });
226#else
228#endif
229}
230
232 if(any_null_pointers(creds)) {
234 }
235
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>()); });
238#else
240#endif
241}
242
244 const botan_x509_cert_t* chain,
245 size_t chain_len,
246 botan_privkey_t key) {
247 if(any_null_pointers(creds, chain, key)) {
249 }
250
251 if(chain_len == 0) {
253 }
254
255 return BOTAN_FFI_VISIT(creds, [=](auto& c) -> int {
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]));
260 }
261
262 std::shared_ptr<Botan::Private_Key> key_ptr = safe_get_sp(key);
263
264 if(key_ptr->public_key()->fingerprint_public() != certs.front().subject_public_key()->fingerprint_public()) {
266 }
267
268 c.add_cert_chain(std::move(certs), std::move(key_ptr));
269 return BOTAN_FFI_SUCCESS;
270 });
271}
272
276
277/*
278* Session managers
279*/
280
282 if(any_null_pointers(mgr)) {
284 }
285
286 *mgr = nullptr;
287
288 return ffi_guard_thunk(__func__, [=]() -> int {
289 const auto rng_ptr = [&]() -> std::shared_ptr<Botan::RandomNumberGenerator> {
290 if(rng != nullptr) {
291 return safe_get_sp(rng);
292 } else {
293 return std::make_shared<Botan::System_RNG>();
294 }
295 }();
296
297 return ffi_new_object(mgr, std::make_shared<Botan::TLS::Session_Manager_In_Memory>(rng_ptr, max_sessions));
298 });
299}
300
302 if(any_null_pointers(mgr)) {
304 }
305
306 *mgr = nullptr;
307
308 return ffi_guard_thunk(
309 __func__, [=]() -> int { return ffi_new_object(mgr, std::make_shared<Botan::TLS::Session_Manager_Noop>()); });
310}
311
315
316} // extern "C"
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33
std::shared_ptr< Botan::Private_Key > private_key_for(const Botan::X509_Certificate &cert, const std::string &type, const std::string &context) override
Definition ffi_tls.cpp:123
std::vector< Botan::Certificate_Store * > trusted_certificate_authorities(const std::string &type, const std::string &context) override
Definition ffi_tls.cpp:58
void add_crl(const Botan::X509_CRL &crl)
Definition ffi_tls.cpp:40
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
Definition ffi_tls.cpp:68
void add_store(std::shared_ptr< Botan::Certificate_Store > store)
Definition ffi_tls.cpp:44
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
Definition ffi_tls.cpp:48
void add_trusted_cert(const Botan::X509_Certificate &cert)
Definition ffi_tls.cpp:36
struct botan_privkey_struct * botan_privkey_t
Definition ffi.h:1874
struct botan_x509_crl_struct * botan_x509_crl_t
Definition ffi.h:4211
struct botan_x509_cert_struct * botan_x509_cert_t
Definition ffi.h:3608
void * botan_view_ctx
Definition ffi.h:152
struct botan_rng_struct * botan_rng_t
Definition ffi.h:343
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition ffi.h:138
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:131
@ BOTAN_FFI_SUCCESS
Definition ffi.h:114
@ BOTAN_FFI_ERROR_BAD_PARAMETER
Definition ffi.h:132
int(* botan_view_str_fn)(botan_view_ctx view_ctx, const char *str, size_t len)
Definition ffi.h:170
int botan_tls_credentials_use_system_store(botan_tls_credentials_t creds)
Definition ffi_tls.cpp:231
int botan_tls_credentials_add_crl(botan_tls_credentials_t creds, botan_x509_crl_t crl)
Definition ffi_tls.cpp:205
int botan_tls_session_manager_init_noop(botan_tls_session_manager_t *mgr)
Definition ffi_tls.cpp:301
int botan_tls_policy_init(botan_tls_policy_t *policy, const char *name)
Definition ffi_tls.cpp:144
int botan_tls_credentials_init(botan_tls_credentials_t *creds)
Definition ffi_tls.cpp:190
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)
Definition ffi_tls.cpp:243
int botan_tls_credentials_add_trusted_dir(botan_tls_credentials_t creds, const char *path)
Definition ffi_tls.cpp:209
int botan_tls_policy_init_from_text(botan_tls_policy_t *policy, const char *text)
Definition ffi_tls.cpp:166
int botan_tls_session_manager_init_memory(botan_tls_session_manager_t *mgr, botan_rng_t rng, size_t max_sessions)
Definition ffi_tls.cpp:281
int botan_tls_session_manager_destroy(botan_tls_session_manager_t mgr)
Definition ffi_tls.cpp:312
int botan_tls_credentials_add_trusted_cert(botan_tls_credentials_t creds, botan_x509_cert_t cert)
Definition ffi_tls.cpp:201
int botan_tls_policy_view_text(botan_tls_policy_t policy, botan_view_ctx ctx, botan_view_str_fn view)
Definition ffi_tls.cpp:178
int botan_tls_credentials_destroy(botan_tls_credentials_t creds)
Definition ffi_tls.cpp:273
int botan_tls_policy_destroy(botan_tls_policy_t policy)
Definition ffi_tls.cpp:182
const char * text
Definition ffi_tls.h:75
struct botan_tls_policy_struct * botan_tls_policy_t
Definition ffi_tls.h:48
const char * path
Definition ffi_tls.h:139
botan_x509_crl_t crl
Definition ffi_tls.h:127
struct botan_tls_session_manager_struct * botan_tls_session_manager_t
Definition ffi_tls.h:183
const char * name
Definition ffi_tls.h:59
struct botan_tls_credentials_struct * botan_tls_credentials_t
Definition ffi_tls.h:106
#define BOTAN_FFI_VISIT(obj, lambda)
Definition ffi_util.h:327
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition ffi_util.h:357
int invoke_view_callback(botan_view_bin_fn view, botan_view_ctx ctx, std::span< const uint8_t > buf)
Definition ffi_util.h:359
auto safe_get_sp(T *p) -> const typename T::smart_pointer_type &
Definition ffi_util.h:212
auto safe_get(T *p) -> typename T::element_type &
Definition ffi_util.h:191
BOTAN_FFI_ERROR ffi_new_object(T *obj, Args &&... args)
Definition ffi_util.h:347
int ffi_guard_thunk(const char *func_name, T thunk)
Definition ffi_util.h:229
bool any_null_pointers(Ptrs... ptr)
Definition mem_utils.h:54
bool value_exists(const std::vector< T > &vec, const V &val)
Definition stl_util.h:44