Botan  2.8.0
Crypto and TLS for C++11
Classes | Functions
Botan_FFI Namespace Reference

Classes

struct  botan_struct
 
class  FFI_Error
 

Functions

template<typename T , uint32_t M, typename F >
int apply_fn (botan_struct< T, M > *o, const char *func_name, F func)
 
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, std::function< int()> thunk)
 
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 (uint8_t out[], size_t *out_len, const std::string &str)
 
int write_str_output (char out[], size_t *out_len, const std::string &str)
 
int write_str_output (char out[], size_t *out_len, const std::vector< uint8_t > &str_vec)
 
template<typename Alloc >
int write_vec_output (uint8_t out[], size_t *out_len, const std::vector< uint8_t, Alloc > &buf)
 

Function Documentation

◆ apply_fn()

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

Definition at line 75 of file ffi_util.h.

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

76  {
77  if(!o)
79 
80  if(o->magic_ok() == false)
82 
83  return ffi_guard_thunk(func_name, [&]() { return func(*o->unsafe_get()); });
84  }
int ffi_guard_thunk(const char *func_name, std::function< int()> thunk)
Definition: ffi.cpp:28

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

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

92  {
93  try
94  {
95  if(obj == nullptr)
96  return BOTAN_FFI_SUCCESS; // ignore delete of null objects
97 
98  if(obj->magic_ok() == false)
100 
101  delete obj;
102  return BOTAN_FFI_SUCCESS;
103  }
104  catch(std::exception& e)
105  {
106  return ffi_error_exception_thrown(func_name, e.what());
107  }
108  catch(...)
109  {
110  return ffi_error_exception_thrown(func_name, "unknown exception");
111  }
112  }
int ffi_error_exception_thrown(const char *func_name, const char *exn, int rc)
Definition: ffi.cpp:19

◆ ffi_error_exception_thrown()

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

Definition at line 19 of file ffi.cpp.

References Botan::OS::running_in_privileged_state().

Referenced by ffi_delete_object(), and ffi_guard_thunk().

20  {
21  if(Botan::OS::running_in_privileged_state() == false && std::getenv("BOTAN_FFI_PRINT_EXCEPTIONS") != nullptr)
22  {
23  std::fprintf(stderr, "in %s exception '%s' returning %d\n", func_name, exn, rc);
24  }
25  return rc;
26  }
bool running_in_privileged_state()
Definition: os_utils.cpp:83

◆ ffi_guard_thunk()

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

Definition at line 28 of file ffi.cpp.

References BOTAN_FFI_ERROR_BAD_PARAMETER, BOTAN_FFI_ERROR_INVALID_KEY_LENGTH, BOTAN_FFI_ERROR_KEY_NOT_SET, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_OUT_OF_MEMORY, BOTAN_FFI_ERROR_UNKNOWN_ERROR, Botan_FFI::FFI_Error::error_code(), ffi_error_exception_thrown(), and Botan::Exception::what().

Referenced by apply_fn(), 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_fpe_decrypt(), botan_fpe_encrypt(), botan_fpe_fe1_init(), botan_hash_init(), botan_hex_decode(), botan_hex_encode(), botan_hotp_init(), botan_kdf(), botan_key_unwrap3394(), botan_key_wrap3394(), botan_mac_init(), botan_mceies_decrypt(), botan_mceies_encrypt(), botan_mp_init(), botan_pk_op_decrypt_create(), botan_pk_op_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_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_rsa(), botan_pubkey_load_sm2(), botan_pubkey_load_x25519(), botan_pubkey_sm2_compute_za(), botan_pwdhash(), botan_pwdhash_timed(), botan_rng_init(), botan_totp_init(), botan_x509_cert_dup(), botan_x509_cert_get_public_key(), botan_x509_cert_load(), botan_x509_cert_load_file(), and botan_x509_cert_verify().

29  {
30  try
31  {
32  return thunk();
33  }
34  catch(std::bad_alloc&)
35  {
36  return ffi_error_exception_thrown(func_name, "bad_alloc", BOTAN_FFI_ERROR_OUT_OF_MEMORY);
37  }
38  catch(Botan_FFI::FFI_Error& e)
39  {
40  return ffi_error_exception_thrown(func_name, e.what(), e.error_code());
41  }
42  catch(Botan::Lookup_Error& e)
43  {
45  }
47  {
49  }
50  catch(Botan::Key_Not_Set& e)
51  {
53  }
54  catch(Botan::Invalid_Argument& e)
55  {
57  }
58  catch(Botan::Not_Implemented& e)
59  {
61  }
62  catch(std::exception& e)
63  {
64  return ffi_error_exception_thrown(func_name, e.what());
65  }
66  catch(...)
67  {
68  return ffi_error_exception_thrown(func_name, "unknown exception");
69  }
70 
72  }
int ffi_error_exception_thrown(const char *func_name, const char *exn, int rc)
Definition: ffi.cpp:19
int error_code() const
Definition: ffi_util.h:27
const char * what() const BOTAN_NOEXCEPT override
Definition: exceptn.h:25

◆ safe_get()

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

Definition at line 59 of file ffi_util.h.

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_mceies_decrypt(), botan_mceies_encrypt(), 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_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_encrypted_pbkdf_iter(), botan_privkey_export_encrypted_pbkdf_msec(), 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_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_x509_cert_dup(), botan_x509_cert_get_public_key(), and botan_x509_cert_verify().

60  {
61  if(!p)
62  throw FFI_Error("Null pointer argument", BOTAN_FFI_ERROR_NULL_POINTER);
63  if(p->magic_ok() == false)
64  throw FFI_Error("Bad magic in ffi object", BOTAN_FFI_ERROR_INVALID_OBJECT);
65 
66  if(T* t = p->unsafe_get())
67  return *t;
68 
69  throw FFI_Error("Invalid object pointer", BOTAN_FFI_ERROR_INVALID_OBJECT);
70  }
fe T
Definition: ge.cpp:37

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

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

117  {
118  if(out_len == nullptr)
120 
121  const size_t avail = *out_len;
122  *out_len = buf_len;
123 
124  if((avail >= buf_len) && (out != nullptr))
125  {
126  Botan::copy_mem(out, buf, buf_len);
127  return BOTAN_FFI_SUCCESS;
128  }
129  else
130  {
131  if(out != nullptr)
132  {
133  Botan::clear_mem(out, avail);
134  }
136  }
137  }
void clear_mem(T *ptr, size_t n)
Definition: mem_ops.h:97
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:108

◆ write_str_output() [1/3]

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

◆ write_str_output() [2/3]

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

Definition at line 152 of file ffi_util.h.

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

153  {
154  return write_str_output(Botan::cast_char_ptr_to_uint8(out), out_len, str);
155  }
int write_str_output(char out[], size_t *out_len, const std::vector< uint8_t > &str_vec)
Definition: ffi_util.h:157
const uint8_t * cast_char_ptr_to_uint8(const char *s)
Definition: mem_ops.h:131

◆ write_str_output() [3/3]

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

Definition at line 157 of file ffi_util.h.

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

158  {
160  out_len,
161  str_vec.data(),
162  str_vec.size());
163  }
const uint8_t * cast_char_ptr_to_uint8(const char *s)
Definition: mem_ops.h:131
int write_output(uint8_t out[], size_t *out_len, const uint8_t buf[], size_t buf_len)
Definition: ffi_util.h:116

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