Botan 3.1.1
Crypto and TLS for C&
Classes | Functions | Variables
Botan_FFI Namespace Reference

Classes

struct  botan_struct
 
struct  botan_view_bounce_struct
 
class  FFI_Error
 

Functions

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)
 
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, int rc)
 
int ffi_guard_thunk (const char *func_name, const std::function< int()> &thunk)
 
template<typename Alloc >
int invoke_view_callback (botan_view_bin_fn view, botan_view_ctx ctx, const std::vector< uint8_t, Alloc > &buf)
 
int invoke_view_callback (botan_view_str_fn view, botan_view_ctx ctx, std::string_view str)
 
template<typename T , uint32_t M>
Tsafe_get (botan_struct< T, M > *p)
 
int write_output (uint8_t out[], size_t *out_len, const uint8_t buf[], size_t buf_len)
 
int write_str_output (char out[], size_t *out_len, const std::vector< uint8_t > &str_vec)
 
int write_str_output (char out[], size_t *out_len, std::string_view str)
 
int write_str_output (uint8_t out[], size_t *out_len, std::string_view str)
 
template<typename Alloc >
int write_vec_output (uint8_t out[], size_t *out_len, const std::vector< uint8_t, Alloc > &buf)
 

Variables

thread_local std::string g_last_exception_what
 

Function Documentation

◆ botan_ffi_visit()

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

Definition at line 77 of file ffi_util.h.

77 {
78 using RetT = std::invoke_result_t<F, T&>;
79 static_assert(std::is_void_v<RetT> || std::is_same_v<RetT, BOTAN_FFI_ERROR> || std::is_same_v<RetT, int>,
80 "BOTAN_FFI_DO must be used with a block that returns either nothing, int or BOTAN_FFI_ERROR");
81
82 if(!o)
84
85 if(o->magic_ok() == false)
87
88 T* p = o->unsafe_get();
89 if(p == nullptr)
91
92 if constexpr(std::is_void_v<RetT>) {
93 return ffi_guard_thunk(func_name, [&] {
94 func(*p);
95 return BOTAN_FFI_SUCCESS;
96 });
97 } else {
98 return ffi_guard_thunk(func_name, [&] { return func(*p); });
99 }
100}
@ BOTAN_FFI_ERROR_INVALID_OBJECT
Definition: ffi.h:111
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition: ffi.h:104
@ BOTAN_FFI_SUCCESS
Definition: ffi.h:89
FE_25519 T
Definition: ge.cpp:34
int ffi_guard_thunk(const char *func_name, const std::function< int()> &thunk)
Definition: ffi.cpp:115
T * unsafe_get() const
Definition: ffi_util.h:43
bool magic_ok() const
Definition: ffi_util.h:41

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(), T, 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 37 of file ffi.cpp.

37 {
38 if(vctx == nullptr || buf == nullptr) {
40 }
41
42 botan_view_bounce_struct* ctx = static_cast<botan_view_bounce_struct*>(vctx);
43
44 const size_t avail = *ctx->out_len;
45 *ctx->out_len = len;
46
47 if(avail < len || ctx->out_ptr == nullptr) {
48 if(ctx->out_ptr) {
49 Botan::clear_mem(ctx->out_ptr, avail);
50 }
52 } else {
53 Botan::copy_mem(ctx->out_ptr, buf, len);
54 return BOTAN_FFI_SUCCESS;
55 }
56}
@ BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE
Definition: ffi.h:95
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:120
constexpr void clear_mem(T *ptr, size_t n)
Definition: mem_ops.h:109

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

33 {
34 return botan_view_bin_bounce_fn(vctx, reinterpret_cast<const uint8_t*>(str), len);
35}
int botan_view_bin_bounce_fn(botan_view_ctx vctx, const uint8_t *buf, size_t len)
Definition: ffi.cpp:37

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 
)

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

120 {
121 return ffi_guard_thunk(func_name, [=]() -> int {
122 // ignore delete of null objects
123 if(obj == nullptr)
124 return BOTAN_FFI_SUCCESS;
125
126 if(obj->magic_ok() == false)
128
129 delete obj;
130 return BOTAN_FFI_SUCCESS;
131 });
132}

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

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

Definition at line 23 of file ffi.cpp.

23 {
24 g_last_exception_what.assign(exn);
25
26 std::string val;
27 if(Botan::OS::read_env_variable(val, "BOTAN_FFI_PRINT_EXCEPTIONS") == true && !val.empty()) {
28 static_cast<void>(std::fprintf(stderr, "in %s exception '%s' returning %d\n", func_name, exn, rc));
29 }
30 return rc;
31}
bool read_env_variable(std::string &value_out, std::string_view var_name)
Definition: os_utils.cpp:400
thread_local std::string g_last_exception_what
Definition: ffi.cpp:21

References g_last_exception_what, and Botan::OS::read_env_variable().

Referenced by ffi_guard_thunk().

◆ ffi_guard_thunk()

int Botan_FFI::ffi_guard_thunk ( const char *  func_name,
const std::function< int()> &  thunk 
)

Definition at line 115 of file ffi.cpp.

115 {
116 g_last_exception_what.clear();
117
118 try {
119 return thunk();
120 } catch(std::bad_alloc&) {
121 return ffi_error_exception_thrown(func_name, "bad_alloc", BOTAN_FFI_ERROR_OUT_OF_MEMORY);
122 } catch(Botan_FFI::FFI_Error& e) {
123 return ffi_error_exception_thrown(func_name, e.what(), e.error_code());
124 } catch(Botan::Exception& e) {
125 return ffi_error_exception_thrown(func_name, e.what(), ffi_map_error_type(e.error_type()));
126 } catch(std::exception& e) {
127 return ffi_error_exception_thrown(func_name, e.what());
128 } catch(...) {
129 return ffi_error_exception_thrown(func_name, "unknown exception");
130 }
131
133}
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:23
@ BOTAN_FFI_ERROR_OUT_OF_MEMORY
Definition: ffi.h:99
@ BOTAN_FFI_ERROR_UNKNOWN_ERROR
Definition: ffi.h:117
int ffi_error_exception_thrown(const char *func_name, const char *exn, int rc)
Definition: ffi.cpp:23

References BOTAN_FFI_ERROR_OUT_OF_MEMORY, BOTAN_FFI_ERROR_UNKNOWN_ERROR, Botan_FFI::FFI_Error::error_code(), Botan_FFI::FFI_Error::error_type(), ffi_error_exception_thrown(), g_last_exception_what, 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_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_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_dh(), botan_privkey_load_dsa(), botan_privkey_load_ecdh(), botan_privkey_load_ecdsa(), botan_privkey_load_ed25519(), botan_privkey_load_elgamal(), botan_privkey_load_kyber(), botan_privkey_load_rsa(), botan_privkey_load_rsa_pkcs1(), botan_privkey_load_sm2(), botan_privkey_load_x25519(), botan_pubkey_load(), botan_pubkey_load_dh(), botan_pubkey_load_dsa(), botan_pubkey_load_ecdh(), botan_pubkey_load_ecdsa(), botan_pubkey_load_ed25519(), botan_pubkey_load_elgamal(), botan_pubkey_load_kyber(), botan_pubkey_load_rsa(), botan_pubkey_load_sm2(), botan_pubkey_load_x25519(), 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_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().

◆ invoke_view_callback() [1/2]

template<typename Alloc >
int Botan_FFI::invoke_view_callback ( botan_view_bin_fn  view,
botan_view_ctx  ctx,
const std::vector< uint8_t, Alloc > &  buf 
)
inline

◆ invoke_view_callback() [2/2]

int Botan_FFI::invoke_view_callback ( botan_view_str_fn  view,
botan_view_ctx  ctx,
std::string_view  str 
)
inline

Definition at line 141 of file ffi_util.h.

141 {
142 return view(ctx, str.data(), str.size() + 1);
143}

◆ safe_get()

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

Definition at line 62 of file ffi_util.h.

62 {
63 if(!p)
64 throw FFI_Error("Null pointer argument", BOTAN_FFI_ERROR_NULL_POINTER);
65 if(p->magic_ok() == false)
66 throw FFI_Error("Bad magic in ffi object", BOTAN_FFI_ERROR_INVALID_OBJECT);
67
68 if(T* t = p->unsafe_get())
69 return *t;
70
71 throw FFI_Error("Invalid object pointer", BOTAN_FFI_ERROR_INVALID_OBJECT);
72}

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

Referenced by botan_bcrypt_generate(), botan_cipher_start(), botan_cipher_update(), 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_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_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_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()

int Botan_FFI::write_output ( uint8_t  out[],
size_t *  out_len,
const uint8_t  buf[],
size_t  buf_len 
)
inline

Definition at line 171 of file ffi_util.h.

171 {
172 if(out_len == nullptr)
174
175 const size_t avail = *out_len;
176 *out_len = buf_len;
177
178 if((avail >= buf_len) && (out != nullptr)) {
179 Botan::copy_mem(out, buf, buf_len);
180 return BOTAN_FFI_SUCCESS;
181 } else {
182 if(out != nullptr) {
183 Botan::clear_mem(out, avail);
184 }
186 }
187}

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() [1/3]

int Botan_FFI::write_str_output ( char  out[],
size_t *  out_len,
const std::vector< uint8_t > &  str_vec 
)
inline

Definition at line 202 of file ffi_util.h.

202 {
203 return write_output(Botan::cast_char_ptr_to_uint8(out), out_len, str_vec.data(), str_vec.size());
204}
int write_output(uint8_t out[], size_t *out_len, const uint8_t buf[], size_t buf_len)
Definition: ffi_util.h:171
const uint8_t * cast_char_ptr_to_uint8(const char *s)
Definition: mem_ops.h:177

References Botan::cast_char_ptr_to_uint8(), and write_output().

◆ write_str_output() [2/3]

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

Definition at line 198 of file ffi_util.h.

198 {
199 return write_str_output(Botan::cast_char_ptr_to_uint8(out), out_len, str);
200}
int write_str_output(uint8_t out[], size_t *out_len, std::string_view str)
Definition: ffi_util.h:194

References Botan::cast_char_ptr_to_uint8(), and write_str_output().

◆ write_str_output() [3/3]

int Botan_FFI::write_str_output ( uint8_t  out[],
size_t *  out_len,
std::string_view  str 
)
inline

◆ write_vec_output()

template<typename Alloc >
int Botan_FFI::write_vec_output ( uint8_t  out[],
size_t *  out_len,
const std::vector< uint8_t, Alloc > &  buf 
)

Variable Documentation

◆ g_last_exception_what

thread_local std::string Botan_FFI::g_last_exception_what