Botan 3.14.1
Crypto and TLS for C&
#include <botan/ffi_tls.h>
#include <botan/assert.h>
#include <botan/certstor.h>
#include <botan/system_rng.h>
#include <botan/tls_policy.h>
#include <botan/tls_session_manager_memory.h>
#include <botan/tls_session_manager_noop.h>
#include <botan/x509_crl.h>
#include <botan/x509cert.h>
#include <botan/internal/ffi_cert.h>
#include <botan/internal/ffi_pkey.h>
#include <botan/internal/ffi_rng.h>
#include <botan/internal/ffi_tls_impl.h>
#include <botan/internal/ffi_util.h>
#include <botan/internal/stl_util.h>
#include <algorithm>
#include <memory>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

Go to the source code of this file.

Namespaces

namespace  Botan_FFI

Functions

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_crl (botan_tls_credentials_t creds, botan_x509_crl_t crl)
int botan_tls_credentials_add_trusted_cert (botan_tls_credentials_t creds, botan_x509_cert_t cert)
int botan_tls_credentials_add_trusted_dir (botan_tls_credentials_t creds, const char *path)
int botan_tls_credentials_destroy (botan_tls_credentials_t creds)
int botan_tls_credentials_init (botan_tls_credentials_t *creds)
int botan_tls_credentials_use_system_store (botan_tls_credentials_t creds)
int botan_tls_policy_destroy (botan_tls_policy_t policy)
int botan_tls_policy_init (botan_tls_policy_t *policy, const char *name)
int botan_tls_policy_init_from_text (botan_tls_policy_t *policy, const char *text)
int botan_tls_policy_view_text (botan_tls_policy_t policy, botan_view_ctx ctx, botan_view_str_fn view)
int botan_tls_session_manager_destroy (botan_tls_session_manager_t mgr)
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_init_noop (botan_tls_session_manager_t *mgr)

Function Documentation

◆ botan_tls_credentials_add_cert_chain()

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 )

Add a certificate chain, leaf first, together with the private key of the leaf certificate. The chain must contain at least one certificate. The certificates are copied; the key object is shared between the key handle and the credentials, so the handle may be destroyed afterwards and a key that cannot be exported, such as a hardware backed key, can be used.

Returns BOTAN_FFI_ERROR_BAD_PARAMETER if chain_len is 0 or the key does not belong to the leaf certificate.

Several chains may be added, for instance one with an RSA and one with an ECDSA key, or chains for different server names. A channel uses the first chain whose key type the peer accepts. A server that has a chain whose leaf certificate matches the server name the client asked for offers only such chains. If the peer named acceptable certificate authorities, a chain with a certificate issued by one of them is preferred.

Definition at line 243 of file ffi_tls.cpp.

246 {
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}
@ 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
#define BOTAN_FFI_VISIT(obj, lambda)
Definition ffi_util.h:327
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
bool any_null_pointers(Ptrs... ptr)
Definition mem_utils.h:54

References Botan_FFI::any_null_pointers(), BOTAN_FFI_ERROR_BAD_PARAMETER, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_SUCCESS, BOTAN_FFI_VISIT, Botan_FFI::safe_get(), and Botan_FFI::safe_get_sp().

◆ botan_tls_credentials_add_crl()

int botan_tls_credentials_add_crl ( botan_tls_credentials_t creds,
botan_x509_crl_t crl )

Definition at line 205 of file ffi_tls.cpp.

205 {
206 return BOTAN_FFI_VISIT(creds, [=](auto& c) { c.add_crl(safe_get(crl)); });
207}
botan_x509_crl_t crl
Definition ffi_tls.h:127

References BOTAN_FFI_VISIT, crl, and Botan_FFI::safe_get().

◆ botan_tls_credentials_add_trusted_cert()

int botan_tls_credentials_add_trusted_cert ( botan_tls_credentials_t creds,
botan_x509_cert_t cert )

Add a certificate (copied) as a trust anchor for verifying the peer.

Definition at line 201 of file ffi_tls.cpp.

201 {
202 return BOTAN_FFI_VISIT(creds, [=](auto& c) { c.add_trusted_cert(safe_get(cert)); });
203}

References BOTAN_FFI_VISIT, and Botan_FFI::safe_get().

◆ botan_tls_credentials_add_trusted_dir()

int botan_tls_credentials_add_trusted_dir ( botan_tls_credentials_t creds,
const char * path )

Definition at line 209 of file ffi_tls.cpp.

209 {
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}
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition ffi.h:138
const char * path
Definition ffi_tls.h:139

References Botan_FFI::any_null_pointers(), BOTAN_FFI_ERROR_BAD_PARAMETER, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_SUCCESS, BOTAN_FFI_VISIT, and path.

◆ botan_tls_credentials_destroy()

int botan_tls_credentials_destroy ( botan_tls_credentials_t creds)

Definition at line 273 of file ffi_tls.cpp.

273 {
274 return BOTAN_FFI_CHECKED_DELETE(creds);
275}
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition ffi_util.h:357

References BOTAN_FFI_CHECKED_DELETE.

◆ botan_tls_credentials_init()

int botan_tls_credentials_init ( botan_tls_credentials_t * creds)

Definition at line 190 of file ffi_tls.cpp.

190 {
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}
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

References Botan_FFI::any_null_pointers(), BOTAN_FFI_ERROR_NULL_POINTER, Botan_FFI::ffi_guard_thunk(), and Botan_FFI::ffi_new_object().

◆ botan_tls_credentials_use_system_store()

int botan_tls_credentials_use_system_store ( botan_tls_credentials_t creds)

Definition at line 231 of file ffi_tls.cpp.

231 {
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}

References Botan_FFI::any_null_pointers(), BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, and BOTAN_FFI_VISIT.

◆ botan_tls_policy_destroy()

int botan_tls_policy_destroy ( botan_tls_policy_t policy)

Definition at line 182 of file ffi_tls.cpp.

182 {
183 return BOTAN_FFI_CHECKED_DELETE(policy);
184}

References BOTAN_FFI_CHECKED_DELETE.

◆ botan_tls_policy_init()

int botan_tls_policy_init ( botan_tls_policy_t * policy,
const char * name )

Definition at line 144 of file ffi_tls.cpp.

144 {
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}
const char * name
Definition ffi_tls.h:59

References Botan_FFI::any_null_pointers(), BOTAN_FFI_ERROR_BAD_PARAMETER, BOTAN_FFI_ERROR_NULL_POINTER, Botan_FFI::ffi_guard_thunk(), Botan_FFI::ffi_new_object(), and name.

Referenced by BOTAN_FFI_EXPORT().

◆ botan_tls_policy_init_from_text()

int botan_tls_policy_init_from_text ( botan_tls_policy_t * policy,
const char * text )

Definition at line 166 of file ffi_tls.cpp.

166 {
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}
const char * text
Definition ffi_tls.h:75

References Botan_FFI::any_null_pointers(), BOTAN_FFI_ERROR_NULL_POINTER, Botan_FFI::ffi_guard_thunk(), Botan_FFI::ffi_new_object(), and text.

◆ botan_tls_policy_view_text()

int botan_tls_policy_view_text ( botan_tls_policy_t policy,
botan_view_ctx ctx,
botan_view_str_fn view )

View the main settings of the policy as text (Botan::TLS::Policy::to_string), one "key = value" line per setting, using the key names of Text_Policy. Not every setting is included.

Definition at line 178 of file ffi_tls.cpp.

178 {
179 return BOTAN_FFI_VISIT(policy, [=](const auto& p) { return invoke_view_callback(view, ctx, p.to_string()); });
180}
int invoke_view_callback(botan_view_bin_fn view, botan_view_ctx ctx, std::span< const uint8_t > buf)
Definition ffi_util.h:359

References BOTAN_FFI_VISIT, and Botan_FFI::invoke_view_callback().

◆ botan_tls_session_manager_destroy()

int botan_tls_session_manager_destroy ( botan_tls_session_manager_t mgr)

Definition at line 312 of file ffi_tls.cpp.

312 {
313 return BOTAN_FFI_CHECKED_DELETE(mgr);
314}

References BOTAN_FFI_CHECKED_DELETE.

◆ botan_tls_session_manager_init_memory()

int botan_tls_session_manager_init_memory ( botan_tls_session_manager_t * mgr,
botan_rng_t rng,
size_t max_sessions )

Create a session manager that keeps sessions in memory (Botan::TLS::Session_Manager_In_Memory).

Parameters
mgrthe output handle; set to NULL on failure
Random Number Generatorsthe random number generator the manager uses; may be NULL, in which case an internal system RNG is used. The manager keeps its own reference to a non-NULL rng, so the handle may be destroyed after the call; the RNG object then lives on until the manager and every channel using the manager have been destroyed.
max_sessionsthe maximum number of sessions kept, 0 for no limit

Definition at line 281 of file ffi_tls.cpp.

281 {
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}

References Botan_FFI::any_null_pointers(), BOTAN_FFI_ERROR_NULL_POINTER, Botan_FFI::ffi_guard_thunk(), Botan_FFI::ffi_new_object(), and Botan_FFI::safe_get_sp().

◆ botan_tls_session_manager_init_noop()

int botan_tls_session_manager_init_noop ( botan_tls_session_manager_t * mgr)

Definition at line 301 of file ffi_tls.cpp.

301 {
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}

References Botan_FFI::any_null_pointers(), BOTAN_FFI_ERROR_NULL_POINTER, Botan_FFI::ffi_guard_thunk(), and Botan_FFI::ffi_new_object().