Botan 3.14.1
Crypto and TLS for C&
Botan_FFI Namespace Reference

Namespaces

namespace  detail

Classes

struct  botan_ffi_obj
struct  botan_ffi_obj_base
struct  botan_view_bounce_struct
class  FFI_Error
class  FFI_TLS_Credentials

Concepts

concept  ffi_object
concept  ffi_object_smart_pointer
concept  ffi_raw_visitor_fn
concept  ffi_sp_visitor_fn
concept  ffi_visitor_fn

Functions

template<typename... Ptrs>
bool any_null_pointers (Ptrs... ptr)
template<ffi_object HandleT, ffi_visitor_fn< HandleT > VisitorFnT>
int botan_ffi_visit (HandleT *o, VisitorFnT func, const char *func_name)
int botan_view_bin_bounce_fn (botan_view_ctx vctx, const uint8_t *buf, size_t len)
int botan_view_str_bounce_fn (botan_view_ctx vctx, const char *str, size_t len)
template<std::integral T>
int check_and_prepare_output_space (T out[], size_t *out_len, size_t required_len)
template<typename Fn, typename... Args>
int copy_view_bin (uint8_t out[], size_t *out_len, Fn fn, Args... args)
template<typename Fn, typename... Args>
int copy_view_str (uint8_t out[], size_t *out_len, Fn fn, Args... args)
void ffi_clear_last_exception ()
template<ffi_object T>
int ffi_delete_object (T *obj, const char *func_name)
int ffi_error_exception_thrown (const char *func_name, const char *exn, Botan::ErrorType err)
int ffi_error_exception_thrown (const char *func_name, const char *exn, int rc)
template<std::invocable T>
int ffi_guard_thunk (const char *func_name, T thunk)
template<typename T, typename... Args>
BOTAN_FFI_ERROR ffi_new_object (T *obj, Args &&... args)
int invoke_view_callback (botan_view_bin_fn view, botan_view_ctx ctx, std::span< const uint8_t > buf)
int invoke_view_callback (botan_view_str_fn view, botan_view_ctx ctx, const std::string &str)
template<ffi_object T>
auto safe_get (T *p) -> typename T::element_type &
template<ffi_object T>
auto safe_get_sp (T *p) -> const typename T::smart_pointer_type &
template<std::integral T>
int write_output (T out[], size_t *out_len, const T buf[], size_t buf_len)
int write_str_output (char out[], size_t *out_len, const std::string &str)
int write_vec_output (uint8_t out[], size_t *out_len, std::span< const uint8_t > buf)

Function Documentation

◆ any_null_pointers()

template<typename... Ptrs>
bool Botan::any_null_pointers ( Ptrs... ptr)

Return true if any of the provided arguments are null

Definition at line 54 of file mem_utils.h.

54 {
55 static_assert((... && std::is_pointer_v<Ptrs>), "All arguments must be pointers");
56 return (... || (ptr == nullptr));
57}

Referenced by botan_base64_decode(), botan_bcrypt_generate(), botan_bcrypt_is_valid(), botan_block_cipher_decrypt_blocks(), botan_block_cipher_encrypt_blocks(), botan_cipher_init(), botan_cipher_update(), botan_constant_time_compare(), botan_fpe_fe1_init(), botan_hex_decode(), botan_hotp_generate(), botan_hotp_init(), botan_is_shared(), botan_mac_init(), botan_nist_kw_dec(), botan_nist_kw_enc(), botan_oid_from_string(), botan_privkey_load_ed25519(), botan_privkey_load_ed448(), botan_privkey_load_x25519(), botan_privkey_load_x448(), botan_pubkey_load_ed25519(), botan_pubkey_load_ed448(), botan_pubkey_load_x25519(), botan_pubkey_load_x448(), botan_pwdhash(), botan_pwdhash_timed(), botan_rng_init_drbg(), botan_spake2p_derive_secret(), botan_spake2p_params_confirmation_size(), botan_spake2p_params_init(), botan_spake2p_params_init_custom(), botan_spake2p_prover_init(), botan_spake2p_prover_process_message(), botan_spake2p_registration_record(), botan_spake2p_verifier_init(), botan_spake2p_verifier_process_message(), botan_srp6_client_agree(), botan_srp6_generate_verifier(), botan_srp6_group_size(), botan_srp6_server_session_step1(), botan_tls_credentials_add_cert_chain(), botan_tls_credentials_add_trusted_dir(), botan_tls_credentials_init(), botan_tls_credentials_use_system_store(), botan_tls_policy_init(), botan_tls_policy_init_from_text(), botan_tls_session_manager_init_memory(), botan_tls_session_manager_init_noop(), botan_totp_generate(), botan_totp_init(), botan_tpm2_ctx_from_esys(), botan_tpm2_enable_crypto_backend(), botan_use_count(), botan_view_bin_bounce_fn(), and copy_view_str().

◆ botan_ffi_visit()

template<ffi_object HandleT, ffi_visitor_fn< HandleT > VisitorFnT>
int Botan_FFI::botan_ffi_visit ( HandleT * o,
VisitorFnT func,
const char * func_name )

Visit the underlying object wrapped in the FFI handle o with the visitor function func. The visitor function can either take a reference to the underlying object or a reference to the smart pointer of the underlying object and must return either void, BOTAN_FFI_ERROR or int.

Note that a visitor lambda function with a generic parameter (e.g. auto&) will receive a reference to the underlying object, not the smart pointer.

Definition at line 276 of file ffi_util.h.

276 {
277 if(!o) {
279 }
280
281 if(!o->magic_ok()) {
283 }
284
285 const auto& p = o->unsafe_get_sp();
286 if(p == nullptr) {
288 }
289
290 auto call_visitor = [&](auto&& ref) -> int {
291 if constexpr(std::is_void_v<decltype(func(ref))>) {
292 return ffi_guard_thunk(func_name, [&] {
293 func(ref);
294 return BOTAN_FFI_SUCCESS;
295 });
296 } else {
297 return ffi_guard_thunk(func_name, [&] { return func(ref); });
298 }
299 };
300
301 // Call the visitor either with a reference to the visitee or its inner smart
302 // pointer depending on what the visitor expects. If it could handle both,
303 // the reference is preferred.
305 return call_visitor(*p);
306 } else {
307 return call_visitor(p);
308 }
309}
@ BOTAN_FFI_ERROR_INVALID_OBJECT
Definition ffi.h:139
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:131
@ BOTAN_FFI_SUCCESS
Definition ffi.h:114
int ffi_guard_thunk(const char *func_name, T thunk)
Definition ffi_util.h:229

References BOTAN_FFI_ERROR_INVALID_OBJECT, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_SUCCESS, and ffi_guard_thunk().

◆ botan_view_bin_bounce_fn()

int Botan_FFI::botan_view_bin_bounce_fn ( botan_view_ctx vctx,
const uint8_t * buf,
size_t len )

Definition at line 126 of file ffi.cpp.

126 {
127 if(any_null_pointers(vctx, buf)) {
129 }
130
131 const botan_view_bounce_struct* ctx = static_cast<botan_view_bounce_struct*>(vctx);
132
133 if(ctx->out_len == nullptr) {
135 }
136
137 const size_t avail = *ctx->out_len;
138 *ctx->out_len = len;
139
140 if(avail < len || ctx->out_ptr == nullptr) {
141 if(ctx->out_ptr != nullptr) {
142 Botan::clear_mem(ctx->out_ptr, avail);
143 }
145 } else {
146 Botan::copy_mem(ctx->out_ptr, buf, len);
147 return BOTAN_FFI_SUCCESS;
148 }
149}
@ BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE
Definition ffi.h:122
bool any_null_pointers(Ptrs... ptr)
Definition mem_utils.h:54
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:144
constexpr void clear_mem(T *ptr, size_t n)
Definition mem_ops.h:118

References any_null_pointers(), BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_SUCCESS, Botan::clear_mem(), Botan::copy_mem(), Botan_FFI::botan_view_bounce_struct::out_len, and Botan_FFI::botan_view_bounce_struct::out_ptr.

Referenced by botan_view_str_bounce_fn(), and copy_view_bin().

◆ botan_view_str_bounce_fn()

int Botan_FFI::botan_view_str_bounce_fn ( botan_view_ctx vctx,
const char * str,
size_t len )

Definition at line 122 of file ffi.cpp.

122 {
123 return botan_view_bin_bounce_fn(vctx, reinterpret_cast<const uint8_t*>(str), len);
124}
int botan_view_bin_bounce_fn(botan_view_ctx vctx, const uint8_t *buf, size_t len)
Definition ffi.cpp:126

References botan_view_bin_bounce_fn().

Referenced by copy_view_str().

◆ check_and_prepare_output_space()

template<std::integral T>
int Botan_FFI::check_and_prepare_output_space ( T out[],
size_t * out_len,
size_t required_len )
inline

Definition at line 402 of file ffi_util.h.

402 {
403 if(out_len == nullptr) {
405 }
406
407 const size_t avail = *out_len;
408 *out_len = required_len;
409
410 if(avail < required_len || out == nullptr) {
411 if(out != nullptr) {
412 std::memset(out, 0, sizeof(T) * avail);
413 }
415 } else {
416 return BOTAN_FFI_SUCCESS;
417 }
418}

References BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE, BOTAN_FFI_ERROR_NULL_POINTER, and BOTAN_FFI_SUCCESS.

Referenced by botan_pk_op_sign_finish(), botan_srp6_server_session_step1(), and write_output().

◆ copy_view_bin()

template<typename Fn, typename... Args>
int Botan_FFI::copy_view_bin ( uint8_t out[],
size_t * out_len,
Fn fn,
Args... args )

Definition at line 383 of file ffi_util.h.

383 {
384 if(out_len == nullptr) {
386 }
387 botan_view_bounce_struct ctx{out, out_len};
388 return fn(args..., &ctx, botan_view_bin_bounce_fn);
389}

References BOTAN_FFI_ERROR_NULL_POINTER, and botan_view_bin_bounce_fn().

Referenced by botan_pk_op_key_agreement_export_public(), botan_privkey_export(), botan_privkey_export_encrypted_pbkdf_iter(), botan_privkey_export_encrypted_pbkdf_msec(), botan_pubkey_export(), and botan_x509_cert_get_public_key_bits().

◆ copy_view_str()

template<typename Fn, typename... Args>
int Botan_FFI::copy_view_str ( uint8_t out[],
size_t * out_len,
Fn fn,
Args... args )

Definition at line 392 of file ffi_util.h.

392 {
393 if(any_null_pointers(fn, out_len)) {
395 }
396 botan_view_bounce_struct ctx{out, out_len};
397 return fn(args..., &ctx, botan_view_str_bounce_fn);
398}
int botan_view_str_bounce_fn(botan_view_ctx vctx, const char *str, size_t len)
Definition ffi.cpp:122

References any_null_pointers(), BOTAN_FFI_ERROR_NULL_POINTER, and botan_view_str_bounce_fn().

Referenced by botan_privkey_export(), botan_privkey_export_encrypted_pbkdf_iter(), botan_privkey_export_encrypted_pbkdf_msec(), botan_pubkey_export(), and botan_x509_cert_to_string().

◆ ffi_clear_last_exception()

void Botan_FFI::ffi_clear_last_exception ( )

Definition at line 92 of file ffi.cpp.

92 {
93 g_last_exception_what[0] = 0;
94}

Referenced by ffi_guard_thunk().

◆ ffi_delete_object()

template<ffi_object T>
int Botan_FFI::ffi_delete_object ( T * obj,
const char * func_name )

Definition at line 330 of file ffi_util.h.

330 {
331 return ffi_guard_thunk(func_name, [=]() -> int {
332 // ignore delete of null objects
333 if(obj == nullptr) {
334 return BOTAN_FFI_SUCCESS;
335 }
336
337 if(!obj->magic_ok()) {
339 }
340
341 delete obj; // NOLINT(*-owning-memory)
342 return BOTAN_FFI_SUCCESS;
343 });
344}

References BOTAN_FFI_ERROR_INVALID_OBJECT, BOTAN_FFI_SUCCESS, and ffi_guard_thunk().

◆ ffi_error_exception_thrown() [1/2]

int Botan_FFI::ffi_error_exception_thrown ( const char * func_name,
const char * exn,
Botan::ErrorType err )

Definition at line 118 of file ffi.cpp.

118 {
119 return ffi_error_exception_thrown(func_name, exn, ffi_map_error_type(err));
120}
int ffi_error_exception_thrown(const char *func_name, const char *exn, int rc)
Definition ffi.cpp:96

References ffi_error_exception_thrown().

◆ ffi_error_exception_thrown() [2/2]

int Botan_FFI::ffi_error_exception_thrown ( const char * func_name,
const char * exn,
int rc )

Definition at line 96 of file ffi.cpp.

96 {
97 constexpr char truncated[] = "...[truncated]";
98 const size_t exn_len = std::strlen(exn);
99 if(exn_len < FFI_ERROR_BUFFER_SIZE) {
100 std::memcpy(g_last_exception_what, exn, exn_len + 1);
101 } else {
102 constexpr size_t prefix_len = FFI_ERROR_BUFFER_SIZE - sizeof(truncated);
103 std::memcpy(g_last_exception_what, exn, prefix_len);
104 std::memcpy(g_last_exception_what + prefix_len, truncated, sizeof(truncated));
105 }
106
107#if defined(BOTAN_HAS_OS_UTILS)
108 std::string val;
109 if(Botan::OS::read_env_variable(val, "BOTAN_FFI_PRINT_EXCEPTIONS") && !val.empty()) {
110 // NOLINTNEXTLINE(*-vararg)
111 static_cast<void>(std::fprintf(stderr, "in %s exception '%s' returning %d\n", func_name, exn, rc));
112 }
113#endif
114
115 return rc;
116}
bool read_env_variable(std::string &value_out, std::string_view var_name)
Definition os_utils.cpp:471

References Botan::OS::read_env_variable().

Referenced by ffi_error_exception_thrown(), and ffi_guard_thunk().

◆ ffi_guard_thunk()

template<std::invocable T>
int Botan_FFI::ffi_guard_thunk ( const char * func_name,
T thunk )

Definition at line 229 of file ffi_util.h.

229 {
231
232 try {
233 return thunk();
234 } catch(std::bad_alloc&) {
235 return ffi_error_exception_thrown(func_name, "bad_alloc", BOTAN_FFI_ERROR_OUT_OF_MEMORY);
236 } catch(Botan_FFI::FFI_Error& e) {
237 return ffi_error_exception_thrown(func_name, e.what(), e.error_code());
238 } catch(Botan::Exception& e) {
239 return ffi_error_exception_thrown(func_name, e.what(), e.error_type());
240 } catch(std::exception& e) {
242 } catch(...) {
243 return ffi_error_exception_thrown(func_name, "unknown exception", BOTAN_FFI_ERROR_EXCEPTION_THROWN);
244 }
245}
const char * what() const noexcept override
Definition exceptn.h:94
virtual ErrorType error_type() const noexcept
Definition exceptn.h:99
int error_code() const noexcept override
Definition ffi_util.h:28
@ BOTAN_FFI_ERROR_EXCEPTION_THROWN
Definition ffi.h:125
@ BOTAN_FFI_ERROR_OUT_OF_MEMORY
Definition ffi.h:126
void ffi_clear_last_exception()
Definition ffi.cpp:92

References BOTAN_FFI_ERROR_EXCEPTION_THROWN, BOTAN_FFI_ERROR_OUT_OF_MEMORY, Botan_FFI::FFI_Error::error_code(), Botan_FFI::FFI_Error::error_type(), ffi_clear_last_exception(), ffi_error_exception_thrown(), and Botan::Exception::what().

Referenced by botan_base64_decode(), botan_base64_encode(), botan_bcrypt_generate(), botan_bcrypt_is_valid(), botan_block_cipher_init(), botan_cipher_init(), botan_cipher_start(), botan_cipher_update(), botan_ec_group_from_ber(), botan_ec_group_from_name(), botan_ec_group_from_oid(), botan_ec_group_from_params(), botan_ec_group_from_pem(), botan_ec_group_supports_named_group(), botan_ec_point_from_xy(), botan_ec_privkey_create(), botan_ec_privkey_get_group(), botan_ec_privkey_get_private_key(), botan_ec_pubkey_get_group(), botan_ffi_visit(), botan_fpe_decrypt(), botan_fpe_encrypt(), botan_fpe_fe1_init(), botan_hash_init(), botan_hex_decode(), botan_hex_encode(), botan_hotp_init(), botan_is_shared(), botan_kdf(), botan_mac_init(), botan_mp_init(), botan_nist_kw_dec(), botan_nist_kw_enc(), botan_oid_from_string(), botan_pk_op_decrypt_create(), botan_pk_op_encrypt_create(), botan_pk_op_kem_decrypt_create(), botan_pk_op_kem_encrypt_create(), botan_pk_op_key_agreement_create(), botan_pk_op_sign_create(), botan_pk_op_verify_create(), botan_pkcs_hash_id(), botan_privkey_create(), botan_privkey_create_dsa(), botan_privkey_create_elgamal(), botan_privkey_export_pubkey(), botan_privkey_load(), botan_privkey_load_classic_mceliece(), botan_privkey_load_dh(), botan_privkey_load_dsa(), botan_privkey_load_ecdh(), botan_privkey_load_ecdsa(), botan_privkey_load_ed25519(), botan_privkey_load_ed448(), botan_privkey_load_elgamal(), botan_privkey_load_frodokem(), botan_privkey_load_kyber(), botan_privkey_load_ml_dsa(), botan_privkey_load_ml_kem(), botan_privkey_load_rsa(), botan_privkey_load_rsa_pkcs1(), botan_privkey_load_slh_dsa(), botan_privkey_load_sm2(), botan_privkey_load_x25519(), botan_privkey_load_x448(), botan_pubkey_ecc_key_used_explicit_encoding(), botan_pubkey_load(), botan_pubkey_load_classic_mceliece(), botan_pubkey_load_dh(), botan_pubkey_load_dsa(), botan_pubkey_load_ecdh(), botan_pubkey_load_ecdh_sec1(), botan_pubkey_load_ecdsa(), botan_pubkey_load_ecdsa_sec1(), botan_pubkey_load_ed25519(), botan_pubkey_load_ed448(), botan_pubkey_load_elgamal(), botan_pubkey_load_frodokem(), botan_pubkey_load_kyber(), botan_pubkey_load_ml_dsa(), botan_pubkey_load_ml_kem(), botan_pubkey_load_rsa(), botan_pubkey_load_rsa_pkcs1(), botan_pubkey_load_slh_dsa(), botan_pubkey_load_sm2(), botan_pubkey_load_sm2_sec1(), botan_pubkey_load_x25519(), botan_pubkey_load_x448(), botan_pubkey_sm2_compute_za(), botan_pwdhash(), botan_pwdhash_timed(), botan_rng_init(), botan_rng_init_custom(), botan_rng_init_drbg(), botan_spake2p_derive_secret(), botan_spake2p_params_init(), botan_spake2p_params_init_custom(), botan_spake2p_prover_init(), botan_spake2p_registration_record(), botan_spake2p_verifier_init(), botan_srp6_client_agree(), botan_srp6_generate_verifier(), botan_srp6_group_size(), botan_srp6_server_session_init(), botan_system_rng_get(), botan_tls_credentials_init(), botan_tls_policy_init(), botan_tls_policy_init_from_text(), botan_tls_session_manager_init_memory(), botan_tls_session_manager_init_noop(), botan_totp_init(), botan_tpm2_ctx_from_esys(), botan_tpm2_ctx_init(), botan_tpm2_ctx_init_ex(), botan_tpm2_enable_crypto_backend(), botan_use_count(), botan_x509_cert_dup(), botan_x509_cert_get_public_key(), botan_x509_cert_load(), botan_x509_cert_load_file(), botan_x509_cert_verify(), botan_x509_cert_verify_with_crl(), botan_x509_crl_create(), botan_x509_crl_entry_create(), botan_x509_crl_load(), botan_x509_crl_load_file(), botan_x509_crl_update(), botan_x509_ext_as_blocks_get_entry_at(), botan_x509_ext_as_blocks_get_info(), botan_x509_ext_ip_addr_blocks_get_address(), botan_x509_ext_ip_addr_blocks_get_counts(), botan_x509_ext_ip_addr_blocks_get_family(), botan_xof_init(), botan_zfec_decode(), botan_zfec_encode(), and ffi_delete_object().

◆ ffi_new_object()

template<typename T, typename... Args>
BOTAN_FFI_ERROR Botan_FFI::ffi_new_object ( T * obj,
Args &&... args )

Definition at line 347 of file ffi_util.h.

347 {
348 if(obj == nullptr) {
350 }
351 // NOLINTNEXTLINE(*-owning-memory)
352 *obj = new std::remove_pointer_t<T>(std::forward<Args>(args)...);
353 return BOTAN_FFI_SUCCESS;
354}

References BOTAN_FFI_ERROR_NULL_POINTER, and BOTAN_FFI_SUCCESS.

Referenced by botan_block_cipher_init(), botan_cipher_init(), botan_ec_group_from_ber(), botan_ec_group_from_name(), botan_ec_group_from_oid(), botan_ec_group_from_params(), botan_ec_group_from_pem(), botan_ec_group_get_curve_oid(), botan_ec_point_add(), botan_ec_point_from_bytes(), botan_ec_point_from_xy(), botan_ec_point_generator(), botan_ec_point_get_group(), botan_ec_point_identity(), botan_ec_point_mul(), botan_ec_point_negate(), botan_ec_point_to_mp_xy(), botan_ec_privkey_create(), botan_ec_privkey_get_group(), botan_ec_privkey_get_private_key(), botan_ec_pubkey_get_group(), botan_ec_scalar_from_mp(), botan_ec_scalar_get_group(), botan_ec_scalar_random(), botan_ec_scalar_to_mp(), botan_fpe_fe1_init(), botan_hash_copy_state(), botan_hash_init(), botan_hotp_init(), botan_mac_init(), botan_mp_init(), botan_oid_from_string(), botan_pk_op_decrypt_create(), botan_pk_op_encrypt_create(), botan_pk_op_kem_decrypt_create(), botan_pk_op_kem_encrypt_create(), botan_pk_op_key_agreement_create(), botan_pk_op_sign_create(), botan_pk_op_verify_create(), botan_privkey_create(), botan_privkey_create_dsa(), botan_privkey_create_elgamal(), botan_privkey_export_pubkey(), botan_privkey_load(), botan_privkey_load_classic_mceliece(), botan_privkey_load_dh(), botan_privkey_load_dsa(), botan_privkey_load_ecdh(), botan_privkey_load_ecdsa(), botan_privkey_load_ed25519(), botan_privkey_load_ed448(), botan_privkey_load_elgamal(), botan_privkey_load_frodokem(), botan_privkey_load_kyber(), botan_privkey_load_ml_dsa(), botan_privkey_load_ml_kem(), botan_privkey_load_rsa(), botan_privkey_load_rsa_pkcs1(), botan_privkey_load_slh_dsa(), botan_privkey_load_sm2(), botan_privkey_load_x25519(), botan_privkey_load_x448(), botan_privkey_oid(), botan_pubkey_load(), botan_pubkey_load_classic_mceliece(), botan_pubkey_load_dh(), botan_pubkey_load_dsa(), botan_pubkey_load_ecdh(), botan_pubkey_load_ecdh_sec1(), botan_pubkey_load_ecdsa(), botan_pubkey_load_ecdsa_sec1(), botan_pubkey_load_ed25519(), botan_pubkey_load_ed448(), botan_pubkey_load_elgamal(), botan_pubkey_load_frodokem(), botan_pubkey_load_kyber(), botan_pubkey_load_ml_dsa(), botan_pubkey_load_ml_kem(), botan_pubkey_load_rsa(), botan_pubkey_load_rsa_pkcs1(), botan_pubkey_load_slh_dsa(), botan_pubkey_load_sm2(), botan_pubkey_load_sm2_sec1(), botan_pubkey_load_x25519(), botan_pubkey_load_x448(), botan_pubkey_oid(), botan_rng_init(), botan_rng_init_custom(), botan_rng_init_drbg(), botan_spake2p_params_init(), botan_spake2p_params_init_custom(), botan_spake2p_prover_init(), botan_spake2p_verifier_init(), botan_srp6_server_session_init(), botan_tls_credentials_init(), botan_tls_policy_init(), botan_tls_policy_init_from_text(), botan_tls_session_manager_init_memory(), botan_tls_session_manager_init_noop(), botan_totp_init(), botan_tpm2_ctx_from_esys(), botan_tpm2_ctx_init(), botan_tpm2_ctx_init_ex(), botan_tpm2_enable_crypto_backend(), botan_tpm2_rng_init(), botan_tpm2_unauthenticated_session_init(), botan_x509_cert_dup(), botan_x509_cert_excluded_name_constraints(), botan_x509_cert_get_public_key(), botan_x509_cert_issuer_alternative_names(), botan_x509_cert_load(), botan_x509_cert_load_file(), botan_x509_cert_permitted_name_constraints(), botan_x509_cert_serial_number(), botan_x509_cert_subject_alternative_names(), botan_x509_crl_create(), botan_x509_crl_entries(), botan_x509_crl_entry_create(), botan_x509_crl_entry_serial_number(), botan_x509_crl_load(), botan_x509_crl_load_file(), botan_x509_crl_update(), botan_xof_copy_state(), and botan_xof_init().

◆ invoke_view_callback() [1/2]

int Botan_FFI::invoke_view_callback ( botan_view_bin_fn view,
botan_view_ctx ctx,
std::span< const uint8_t > buf )
inline

Definition at line 359 of file ffi_util.h.

359 {
360 if(view == nullptr) {
362 }
363 return view(ctx, buf.data(), buf.size());
364}

References BOTAN_FFI_ERROR_NULL_POINTER.

Referenced by botan_ec_group_view_der(), botan_ec_group_view_pem(), botan_ec_point_view_compressed(), botan_ec_point_view_uncompressed(), botan_ec_point_view_x_bytes(), botan_ec_point_view_xy_bytes(), botan_ec_point_view_y_bytes(), botan_mp_view_bin(), botan_mp_view_hex(), botan_mp_view_str(), botan_oid_view_name(), botan_oid_view_string(), botan_pk_op_key_agreement_view_public(), botan_privkey_view_der(), botan_privkey_view_encrypted_der(), botan_privkey_view_encrypted_der_timed(), botan_privkey_view_encrypted_pem(), botan_privkey_view_encrypted_pem_timed(), botan_privkey_view_kyber_raw_key(), botan_privkey_view_pem(), botan_privkey_view_raw(), botan_pubkey_view_der(), botan_pubkey_view_ec_public_point(), botan_pubkey_view_kyber_raw_key(), botan_pubkey_view_pem(), botan_pubkey_view_raw(), botan_spake2p_derive_secret(), botan_spake2p_prover_generate_message(), botan_spake2p_prover_process_message(), botan_spake2p_prover_shared_secret(), botan_spake2p_registration_record(), botan_spake2p_verifier_process_message(), botan_spake2p_verifier_shared_secret(), botan_tls_policy_view_text(), botan_x509_cert_view_as_string(), botan_x509_cert_view_binary_values(), botan_x509_cert_view_public_key_bits(), botan_x509_cert_view_string_values(), botan_x509_crl_entry_view_serial_number(), botan_x509_crl_view_binary_values(), botan_x509_crl_view_issuer_dn(), botan_x509_general_name_view_binary_value(), and botan_x509_general_name_view_string_value().

◆ invoke_view_callback() [2/2]

int Botan_FFI::invoke_view_callback ( botan_view_str_fn view,
botan_view_ctx ctx,
const std::string & str )
inline

Definition at line 367 of file ffi_util.h.

367 {
368 if(view == nullptr) {
370 }
371 return view(ctx, str.data(), str.size() + 1);
372}

References BOTAN_FFI_ERROR_NULL_POINTER.

◆ safe_get()

template<ffi_object T>
auto Botan_FFI::safe_get ( T * p) -> typename T::element_type &

Checked access to a reference of the underlying object wrapped in the FFI handle p.

Definition at line 191 of file ffi_util.h.

191 {
192 if(!p) {
193 throw FFI_Error("Null pointer argument", BOTAN_FFI_ERROR_NULL_POINTER);
194 }
195 if(!p->magic_ok()) {
196 throw FFI_Error("Bad magic in ffi object", BOTAN_FFI_ERROR_INVALID_OBJECT);
197 }
198
199 if(auto* t = p->unsafe_get()) {
200 return *t;
201 }
202
203 throw FFI_Error("Invalid object pointer", BOTAN_FFI_ERROR_INVALID_OBJECT);
204}

References BOTAN_FFI_ERROR_INVALID_OBJECT, and BOTAN_FFI_ERROR_NULL_POINTER.

Referenced by botan_bcrypt_generate(), botan_cipher_start(), botan_cipher_update(), botan_ec_group_equal(), botan_ec_group_from_oid(), botan_ec_group_from_params(), botan_ec_point_add(), botan_ec_point_equal(), botan_ec_point_from_xy(), botan_ec_point_mul(), botan_ec_privkey_create(), botan_ec_privkey_get_group(), botan_ec_privkey_get_private_key(), botan_ec_pubkey_get_group(), botan_ec_scalar_from_mp(), botan_ec_scalar_random(), botan_fpe_decrypt(), botan_fpe_encrypt(), botan_fpe_fe1_init(), botan_mp_add(), botan_mp_add_u32(), botan_mp_cmp(), botan_mp_div(), botan_mp_equal(), botan_mp_gcd(), botan_mp_is_prime(), botan_mp_lshift(), botan_mp_mod_inverse(), botan_mp_mod_mul(), botan_mp_mul(), botan_mp_powmod(), botan_mp_rand_bits(), botan_mp_rand_range(), botan_mp_rshift(), botan_mp_set_from_mp(), botan_mp_sub(), botan_mp_sub_u32(), botan_mp_swap(), botan_oid_cmp(), botan_oid_equal(), botan_pk_op_decrypt_create(), botan_pk_op_encrypt(), botan_pk_op_encrypt_create(), botan_pk_op_kem_decrypt_create(), botan_pk_op_kem_encrypt_create(), botan_pk_op_kem_encrypt_create_shared_key(), botan_pk_op_key_agreement_create(), botan_pk_op_sign_create(), botan_pk_op_sign_finish(), botan_pk_op_verify_create(), botan_privkey_check_key(), botan_privkey_create(), botan_privkey_create_dsa(), botan_privkey_create_elgamal(), botan_privkey_export_pubkey(), botan_privkey_get_field(), botan_privkey_load_dh(), botan_privkey_load_dsa(), botan_privkey_load_ecdh(), botan_privkey_load_ecdsa(), botan_privkey_load_elgamal(), botan_privkey_load_rsa(), botan_privkey_load_sm2(), botan_privkey_view_encrypted_der(), botan_privkey_view_encrypted_der_timed(), botan_privkey_view_encrypted_pem(), botan_privkey_view_encrypted_pem_timed(), botan_pubkey_check_key(), botan_pubkey_ecc_key_used_explicit_encoding(), botan_pubkey_get_field(), botan_pubkey_load_dh(), botan_pubkey_load_dsa(), botan_pubkey_load_ecdh(), botan_pubkey_load_ecdsa(), botan_pubkey_load_elgamal(), botan_pubkey_load_rsa(), botan_pubkey_load_sm2(), botan_pubkey_sm2_compute_za(), botan_rng_reseed_from_rng(), botan_spake2p_derive_secret(), botan_spake2p_params_init_custom(), botan_spake2p_prover_generate_message(), botan_spake2p_prover_init(), botan_spake2p_prover_process_message(), botan_spake2p_registration_record(), botan_spake2p_verifier_init(), botan_spake2p_verifier_process_message(), botan_srp6_client_agree(), botan_srp6_server_session_step1(), botan_tls_credentials_add_cert_chain(), botan_tls_credentials_add_crl(), botan_tls_credentials_add_trusted_cert(), botan_x509_cert_allowed_extended_usage_oid(), botan_x509_cert_dup(), botan_x509_cert_get_public_key(), botan_x509_cert_verify(), botan_x509_cert_verify_with_crl(), botan_x509_crl_create(), botan_x509_crl_entry_create(), botan_x509_crl_update(), botan_x509_crl_verify_signature(), botan_x509_ext_as_blocks_get_entry_at(), botan_x509_ext_as_blocks_get_info(), botan_x509_ext_ip_addr_blocks_get_address(), botan_x509_ext_ip_addr_blocks_get_counts(), botan_x509_ext_ip_addr_blocks_get_family(), and botan_x509_is_revoked().

◆ safe_get_sp()

template<ffi_object T>
auto Botan_FFI::safe_get_sp ( T * p) -> const typename T::smart_pointer_type &

Checked access to a reference of the smart pointer of the underlying object wrapped in the FFI handle p. Note that the smart pointer is returned by reference, so that the caller can copy it (if it is a std::shared_ptr).

Definition at line 212 of file ffi_util.h.

212 {
213 if(!p) {
214 throw FFI_Error("Null pointer argument", BOTAN_FFI_ERROR_NULL_POINTER);
215 }
216 if(!p->magic_ok()) {
217 throw FFI_Error("Bad magic in ffi object", BOTAN_FFI_ERROR_INVALID_OBJECT);
218 }
219
220 auto& sp = p->unsafe_get_sp();
221 if(sp == nullptr) {
222 throw FFI_Error("Invalid object pointer", BOTAN_FFI_ERROR_INVALID_OBJECT);
223 }
224
225 return sp;
226}

References BOTAN_FFI_ERROR_INVALID_OBJECT, and BOTAN_FFI_ERROR_NULL_POINTER.

Referenced by botan_tls_credentials_add_cert_chain(), botan_tls_session_manager_init_memory(), botan_tpm2_ctx_enable_crypto_backend(), and botan_tpm2_enable_crypto_backend().

◆ write_output()

template<std::integral T>
int Botan_FFI::write_output ( T out[],
size_t * out_len,
const T buf[],
size_t buf_len )
inline

Definition at line 421 of file ffi_util.h.

421 {
422 static_assert(sizeof(T) == 1, "T should be either uint8_t or char");
423
424 const auto rc = check_and_prepare_output_space(out, out_len, buf_len);
425 if(rc != BOTAN_FFI_SUCCESS) {
426 return rc;
427 }
428
429 if(out != nullptr) {
430 std::memcpy(out, buf, sizeof(T) * buf_len);
431 }
432
433 return BOTAN_FFI_SUCCESS;
434}
int check_and_prepare_output_space(T out[], size_t *out_len, size_t required_len)
Definition ffi_util.h:402

References BOTAN_FFI_SUCCESS, and check_and_prepare_output_space().

Referenced by botan_pkcs_hash_id(), write_str_output(), and write_vec_output().

◆ write_str_output()

int Botan_FFI::write_str_output ( char out[],
size_t * out_len,
const std::string & str )
inline

◆ write_vec_output()