9#include <botan/assert.h>
10#include <botan/base64.h>
12#include <botan/mem_ops.h>
13#include <botan/version.h>
14#include <botan/internal/ct_utils.h>
15#include <botan/internal/ffi_util.h>
20#if defined(BOTAN_HAS_OS_UTILS)
21 #include <botan/internal/os_utils.h>
28constexpr size_t FFI_ERROR_BUFFER_SIZE = 512;
30thread_local char g_last_exception_what[FFI_ERROR_BUFFER_SIZE] = {};
33static_assert(std::is_trivially_destructible_v<
decltype(g_last_exception_what)>);
93 g_last_exception_what[0] = 0;
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);
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));
107#if defined(BOTAN_HAS_OS_UTILS)
111 static_cast<void>(std::fprintf(stderr,
"in %s exception '%s' returning %d\n", func_name, exn, rc));
137 const size_t avail = *ctx->
out_len;
140 if(avail < len || ctx->out_ptr ==
nullptr) {
158 return g_last_exception_what;
167 return "Invalid verifier";
170 return "Invalid input";
173 return "Invalid authentication code";
176 return "No value available";
179 return "Insufficient buffer space";
182 return "String conversion error";
185 return "Exception thrown";
188 return "Out of memory";
191 return "Error while calling system API";
194 return "Internal error";
200 return "Null pointer argument";
203 return "Bad parameter";
206 return "Key not set on object";
209 return "Invalid key length";
212 return "Invalid object state";
215 return "Index out of range";
218 return "Not implemented";
221 return "Invalid object handle";
231 return "Unknown error";
244 if(api_version == 20261104) {
249 if(api_version == 20260811) {
254 if(api_version == 20260506) {
259 if(api_version == 20260303) {
264 if(api_version == 20250829) {
269 if(api_version == 20250506) {
274 if(api_version == 20240408) {
279 if(api_version == 20231009) {
284 if(api_version == 20230711) {
289 if(api_version == 20230403) {
294 if(api_version == 20210220) {
299 if(api_version == 20191214) {
304 if(api_version == 20180713) {
309 if(api_version == 20170815) {
314 if(api_version == 20170327) {
319 if(api_version == 20150515) {
328#if defined(BOTAN_HAS_FFI_TLS)
329 return BOTAN_HAS_FFI_TLS;
336#if defined(BOTAN_HAS_FFI_TLS)
337 if(api_version == BOTAN_HAS_FFI_TLS) {
373 return static_cast<int>(same.select(1, 0)) - 1;
381 if(bytes > 0 && mem ==
nullptr) {
389 if(len > 0 && (in ==
nullptr || out ==
nullptr)) {
410 if(len > 0 && in ==
nullptr) {
442 if(!obj->appears_to_be_valid_ffi_handle()) {
451 if(out_use_count !=
nullptr) {
463 if(!obj->appears_to_be_valid_ffi_handle()) {
uint32_t botan_version_datestamp()
int botan_use_count(void *handle, size_t *out_use_count)
int botan_same_mem(const uint8_t *x, const uint8_t *y, size_t len)
const char * botan_version_string()
int botan_base64_decode(const char *base64_str, size_t in_len, uint8_t *out, size_t *out_len)
uint32_t botan_version_patch()
int botan_is_shared(void *handle)
int botan_base64_encode(const uint8_t *in, size_t len, char *out, size_t *out_len)
int botan_scrub_mem(void *mem, size_t bytes)
uint32_t botan_ffi_tls_api_version()
int botan_hex_encode(const uint8_t *in, size_t len, char *out, uint32_t flags)
uint32_t botan_version_major()
uint32_t botan_ffi_api_version()
int botan_ffi_supports_api(uint32_t api_version)
const char * botan_error_description(int err)
uint32_t botan_version_minor()
int botan_constant_time_compare(const uint8_t *x, const uint8_t *y, size_t len)
int botan_hex_decode(const char *hex_str, size_t in_len, uint8_t *out, size_t *out_len)
int botan_ffi_tls_supports_api(uint32_t api_version)
const char * botan_error_last_exception_message()
#define BOTAN_FFI_HEX_LOWER_CASE
@ BOTAN_FFI_ERROR_TPM_ERROR
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
@ BOTAN_FFI_ERROR_INVALID_KEY_LENGTH
@ BOTAN_FFI_ERROR_KEY_NOT_SET
@ BOTAN_FFI_ERROR_TLS_ERROR
@ BOTAN_FFI_ERROR_EXCEPTION_THROWN
@ BOTAN_FFI_ERROR_OUT_OF_MEMORY
@ BOTAN_FFI_ERROR_OUT_OF_RANGE
@ BOTAN_FFI_ERROR_INTERNAL_ERROR
@ BOTAN_FFI_INVALID_VERIFIER
@ BOTAN_FFI_ERROR_INVALID_OBJECT
@ BOTAN_FFI_ERROR_UNKNOWN_ERROR
@ BOTAN_FFI_ERROR_HTTP_ERROR
@ BOTAN_FFI_ERROR_BAD_FLAG
@ BOTAN_FFI_ERROR_INVALID_INPUT
@ BOTAN_FFI_ERROR_STRING_CONVERSION_ERROR
@ BOTAN_FFI_ERROR_NULL_POINTER
@ BOTAN_FFI_ERROR_SYSTEM_ERROR
@ BOTAN_FFI_ERROR_ROUGHTIME_ERROR
@ BOTAN_FFI_ERROR_NO_VALUE
@ BOTAN_FFI_ERROR_INVALID_OBJECT_STATE
@ BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE
@ BOTAN_FFI_ERROR_BAD_MAC
@ BOTAN_FFI_ERROR_BAD_PARAMETER
constexpr CT::Mask< T > is_equal(const T x[], const T y[], size_t len)
bool read_env_variable(std::string &value_out, std::string_view var_name)
void ffi_clear_last_exception()
int ffi_error_exception_thrown(const char *func_name, const char *exn, int rc)
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)
int ffi_guard_thunk(const char *func_name, T thunk)
int write_vec_output(uint8_t out[], size_t *out_len, std::span< const uint8_t > buf)
bool any_null_pointers(Ptrs... ptr)
int write_str_output(char out[], size_t *out_len, const std::string &str)
const char * version_cstr()
size_t base64_encode(char out[], const uint8_t in[], size_t input_length, size_t &input_consumed, bool final_inputs)
constexpr void copy_mem(T *out, const T *in, size_t n)
uint32_t version_datestamp()
void secure_scrub_memory(void *ptr, size_t n)
size_t base64_decode(uint8_t out[], const char in[], size_t input_length, size_t &input_consumed, bool final_inputs, bool ignore_ws)
void hex_encode(char output[], const uint8_t input[], size_t input_length, bool uppercase)
size_t base64_decode_max_output(size_t input_length)
size_t hex_decode(uint8_t output[], const char input[], size_t input_length, size_t &input_consumed, bool ignore_ws)
constexpr void clear_mem(T *ptr, size_t n)
virtual size_t use_count() const =0
virtual bool is_shared() const =0