Botan 3.9.0
Crypto and TLS for C&
Botan_FFI Namespace Reference

Classes

struct  botan_struct
struct  botan_view_bounce_struct
class  FFI_Error

Functions

template<typename... Ptrs>
bool any_null_pointers (Ptrs... ptr)
template<typename T, uint32_t M, typename F>
int botan_ffi_visit (botan_struct< T, M > *o, F 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<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<typename T, uint32_t M>
int ffi_delete_object (botan_struct< T, M > *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<typename T, uint32_t M>
T & safe_get (botan_struct< T, M > *p)
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 23 of file mem_utils.h.

23 {
24 static_assert((... && std::is_pointer_v<Ptrs>), "All arguments must be pointers");
25 return (... || (ptr == nullptr));
26}

Referenced by botan_mac_init(), botan_srp6_client_agree(), botan_srp6_generate_verifier(), botan_srp6_group_size(), and botan_srp6_server_session_step1().

◆ botan_ffi_visit()

template<typename T, uint32_t M, typename F>
int Botan_FFI::botan_ffi_visit ( botan_struct< T, M > * o,
F func,
const char * func_name )

Definition at line 114 of file ffi_util.h.

114 {
115 using RetT = std::invoke_result_t<F, T&>;
116 static_assert(std::is_void_v<RetT> || std::is_same_v<RetT, BOTAN_FFI_ERROR> || std::is_same_v<RetT, int>,
117 "BOTAN_FFI_DO must be used with a block that returns either nothing, int or BOTAN_FFI_ERROR");
118
119 if(!o) {
121 }
122
123 if(!o->magic_ok()) {
125 }
126
127 T* p = o->unsafe_get();
128 if(p == nullptr) {
130 }
131
132 if constexpr(std::is_void_v<RetT>) {
133 return ffi_guard_thunk(func_name, [&] {
134 func(*p);
135 return BOTAN_FFI_SUCCESS;
136 });
137 } else {
138 return ffi_guard_thunk(func_name, [&] { return func(*p); });
139 }
140}
@ BOTAN_FFI_ERROR_INVALID_OBJECT
Definition ffi.h:139
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:132
@ BOTAN_FFI_SUCCESS
Definition ffi.h:115
int ffi_guard_thunk(const char *func_name, T thunk)
Definition ffi_util.h:95
T * unsafe_get() const
Definition ffi_util.h:52
bool magic_ok() const
Definition ffi_util.h:50

References BOTAN_FFI_ERROR_INVALID_OBJECT, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_SUCCESS, ffi_guard_thunk(), Botan_FFI::botan_struct< T, MAGIC >::magic_ok(), and Botan_FFI::botan_struct< T, MAGIC >::unsafe_get().

◆ 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 112 of file ffi.cpp.

112 {
113 if(vctx == nullptr || buf == nullptr) {
115 }
116
117 botan_view_bounce_struct* ctx = static_cast<botan_view_bounce_struct*>(vctx);
118
119 const size_t avail = *ctx->out_len;
120 *ctx->out_len = len;
121
122 if(avail < len || ctx->out_ptr == nullptr) {
123 if(ctx->out_ptr != nullptr) {
124 Botan::clear_mem(ctx->out_ptr, avail);
125 }
127 } else {
128 Botan::copy_mem(ctx->out_ptr, buf, len);
129 return BOTAN_FFI_SUCCESS;
130 }
131}
@ BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE
Definition ffi.h:123
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:145
constexpr void clear_mem(T *ptr, size_t n)
Definition mem_ops.h:119

References 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 108 of file ffi.cpp.

108 {
109 return botan_view_bin_bounce_fn(vctx, reinterpret_cast<const uint8_t*>(str), len);
110}
int botan_view_bin_bounce_fn(botan_view_ctx vctx, const uint8_t *buf, size_t len)
Definition ffi.cpp:112

References botan_view_bin_bounce_fn().

Referenced by copy_view_str().

◆ 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 )

◆ 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 217 of file ffi_util.h.

217 {
218 if(fn == nullptr) {
220 }
221 botan_view_bounce_struct ctx{out, out_len};
222 return fn(args..., &ctx, botan_view_str_bounce_fn);
223}
int botan_view_str_bounce_fn(botan_view_ctx vctx, const char *str, size_t len)
Definition ffi.cpp:108

References 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 86 of file ffi.cpp.

86 {
87 g_last_exception_what.clear();
88}

Referenced by ffi_guard_thunk().

◆ ffi_delete_object()

template<typename T, uint32_t M>
int Botan_FFI::ffi_delete_object ( botan_struct< T, M > * obj,
const char * func_name )

Definition at line 161 of file ffi_util.h.

161 {
162 return ffi_guard_thunk(func_name, [=]() -> int {
163 // ignore delete of null objects
164 if(obj == nullptr) {
165 return BOTAN_FFI_SUCCESS;
166 }
167
168 if(!obj->magic_ok()) {
170 }
171
172 delete obj; // NOLINT(*-owning-memory)
173 return BOTAN_FFI_SUCCESS;
174 });
175}

References BOTAN_FFI_ERROR_INVALID_OBJECT, BOTAN_FFI_SUCCESS, ffi_guard_thunk(), and Botan_FFI::botan_struct< T, MAGIC >::magic_ok().

◆ 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 104 of file ffi.cpp.

104 {
105 return ffi_error_exception_thrown(func_name, exn, ffi_map_error_type(err));
106}
int ffi_error_exception_thrown(const char *func_name, const char *exn, int rc)
Definition ffi.cpp:90

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 90 of file ffi.cpp.

90 {
91 g_last_exception_what.assign(exn);
92
93#if defined(BOTAN_HAS_OS_UTILS)
94 std::string val;
95 if(Botan::OS::read_env_variable(val, "BOTAN_FFI_PRINT_EXCEPTIONS") && !val.empty()) {
96 // NOLINTNEXTLINE(*-vararg)
97 static_cast<void>(std::fprintf(stderr, "in %s exception '%s' returning %d\n", func_name, exn, rc));
98 }
99#endif
100
101 return rc;
102}
bool read_env_variable(std::string &value_out, std::string_view var_name)
Definition os_utils.cpp:445

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 95 of file ffi_util.h.

95 {
97
98 try {
99 return thunk();
100 } catch(std::bad_alloc&) {
101 return ffi_error_exception_thrown(func_name, "bad_alloc", BOTAN_FFI_ERROR_OUT_OF_MEMORY);
102 } catch(Botan_FFI::FFI_Error& e) {
103 return ffi_error_exception_thrown(func_name, e.what(), e.error_code());
104 } catch(Botan::Exception& e) {
105 return ffi_error_exception_thrown(func_name, e.what(), e.error_type());
106 } catch(std::exception& e) {
108 } catch(...) {
109 return ffi_error_exception_thrown(func_name, "unknown exception", BOTAN_FFI_ERROR_EXCEPTION_THROWN);
110 }
111}
const char * what() const noexcept override
Definition exceptn.h:93
virtual ErrorType error_type() const noexcept
Definition exceptn.h:98
int error_code() const noexcept override
Definition ffi_util.h:27
@ BOTAN_FFI_ERROR_EXCEPTION_THROWN
Definition ffi.h:126
@ BOTAN_FFI_ERROR_OUT_OF_MEMORY
Definition ffi.h:127
void ffi_clear_last_exception()
Definition ffi.cpp:86

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_privkey_create(), 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_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_ecdsa(), botan_pubkey_load_ed25519(), botan_pubkey_load_ed448(), botan_pubkey_load_elgamal(), botan_pubkey_load_frodokem(), botan_pubkey_load_ml_dsa(), botan_pubkey_load_ml_kem(), botan_pubkey_load_rsa(), botan_pubkey_load_slh_dsa(), botan_pubkey_load_sm2(), 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_srp6_client_agree(), botan_srp6_generate_verifier(), botan_srp6_group_size(), botan_srp6_server_session_init(), botan_system_rng_get(), botan_totp_init(), botan_tpm2_ctx_from_esys(), botan_tpm2_ctx_init(), botan_tpm2_ctx_init_ex(), botan_tpm2_enable_crypto_backend(), 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_load(), botan_x509_crl_load_file(), 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 178 of file ffi_util.h.

178 {
179 // NOLINTNEXTLINE(*-owning-memory)
180 *obj = new std::remove_pointer_t<T>(std::forward<Args>(args)...);
181 return BOTAN_FFI_SUCCESS;
182}

References 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_privkey_create(), 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_ecdsa(), 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_slh_dsa(), botan_pubkey_load_sm2(), botan_pubkey_load_x25519(), botan_pubkey_load_x448(), botan_pubkey_oid(), botan_rng_init(), botan_rng_init_custom(), botan_srp6_server_session_init(), 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_get_public_key(), botan_x509_cert_load(), botan_x509_cert_load_file(), botan_x509_crl_load(), and botan_x509_crl_load_file().

◆ invoke_view_callback() [1/2]

◆ 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 195 of file ffi_util.h.

195 {
196 if(view == nullptr) {
198 }
199 return view(ctx, str.data(), str.size() + 1);
200}

References BOTAN_FFI_ERROR_NULL_POINTER.

◆ safe_get()

template<typename T, uint32_t M>
T & Botan_FFI::safe_get ( botan_struct< T, M > * p)

Definition at line 79 of file ffi_util.h.

79 {
80 if(!p) {
81 throw FFI_Error("Null pointer argument", BOTAN_FFI_ERROR_NULL_POINTER);
82 }
83 if(!p->magic_ok()) {
84 throw FFI_Error("Bad magic in ffi object", BOTAN_FFI_ERROR_INVALID_OBJECT);
85 }
86
87 if(T* t = p->unsafe_get()) {
88 return *t;
89 }
90
91 throw FFI_Error("Invalid object pointer", BOTAN_FFI_ERROR_INVALID_OBJECT);
92}

References BOTAN_FFI_ERROR_INVALID_OBJECT, BOTAN_FFI_ERROR_NULL_POINTER, Botan_FFI::botan_struct< T, MAGIC >::magic_ok(), and Botan_FFI::botan_struct< T, MAGIC >::unsafe_get().

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_privkey_create(), 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_srp6_client_agree(), botan_srp6_server_session_step1(), botan_tpm2_ctx_enable_crypto_backend(), botan_tpm2_enable_crypto_backend(), botan_x509_cert_dup(), botan_x509_cert_get_public_key(), botan_x509_cert_verify(), botan_x509_cert_verify_with_crl(), and botan_x509_is_revoked().

◆ 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 226 of file ffi_util.h.

226 {
227 static_assert(sizeof(T) == 1, "T should be either uint8_t or char");
228
229 if(out_len == nullptr) {
231 }
232
233 const size_t avail = *out_len;
234 *out_len = buf_len;
235
236 if((avail >= buf_len) && (out != nullptr)) {
237 Botan::copy_mem(out, buf, buf_len);
238 return BOTAN_FFI_SUCCESS;
239 } else {
240 if(out != nullptr) {
241 Botan::clear_mem(out, avail);
242 }
244 }
245}

References BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_SUCCESS, Botan::clear_mem(), and Botan::copy_mem().

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()