Botan 3.7.1
Crypto and TLS for C&
ffi.cpp
Go to the documentation of this file.
1/*
2* (C) 2015,2017 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#include <botan/ffi.h>
8
9#include <botan/base64.h>
10#include <botan/hex.h>
11#include <botan/mem_ops.h>
12#include <botan/version.h>
13#include <botan/internal/ct_utils.h>
14#include <botan/internal/ffi_util.h>
15#include <cstdio>
16#include <cstdlib>
17
18#if defined(BOTAN_HAS_OS_UTILS)
19 #include <botan/internal/os_utils.h>
20#endif
21
22namespace Botan_FFI {
23
24namespace {
25
26// NOLINTNEXTLINE(*-avoid-non-const-global-variables)
27thread_local std::string g_last_exception_what;
28
29} // namespace
30
31int ffi_error_exception_thrown(const char* func_name, const char* exn, int rc) {
32 g_last_exception_what.assign(exn);
33
34#if defined(BOTAN_HAS_OS_UTILS)
35 std::string val;
36 if(Botan::OS::read_env_variable(val, "BOTAN_FFI_PRINT_EXCEPTIONS") == true && !val.empty()) {
37 static_cast<void>(std::fprintf(stderr, "in %s exception '%s' returning %d\n", func_name, exn, rc));
38 }
39#endif
40
41 return rc;
42}
43
44int botan_view_str_bounce_fn(botan_view_ctx vctx, const char* str, size_t len) {
45 return botan_view_bin_bounce_fn(vctx, reinterpret_cast<const uint8_t*>(str), len);
46}
47
48int botan_view_bin_bounce_fn(botan_view_ctx vctx, const uint8_t* buf, size_t len) {
49 if(vctx == nullptr || buf == nullptr) {
51 }
52
53 botan_view_bounce_struct* ctx = static_cast<botan_view_bounce_struct*>(vctx);
54
55 const size_t avail = *ctx->out_len;
56 *ctx->out_len = len;
57
58 if(avail < len || ctx->out_ptr == nullptr) {
59 if(ctx->out_ptr) {
60 Botan::clear_mem(ctx->out_ptr, avail);
61 }
63 } else {
64 Botan::copy_mem(ctx->out_ptr, buf, len);
65 return BOTAN_FFI_SUCCESS;
66 }
67}
68
69namespace {
70
71int ffi_map_error_type(Botan::ErrorType err) {
72 switch(err) {
75
85
88
102
106
109
114
121 }
122
124}
125
126} // namespace
127
128int ffi_guard_thunk(const char* func_name, const std::function<int()>& thunk) {
129 g_last_exception_what.clear();
130
131 try {
132 return thunk();
133 } catch(std::bad_alloc&) {
134 return ffi_error_exception_thrown(func_name, "bad_alloc", BOTAN_FFI_ERROR_OUT_OF_MEMORY);
135 } catch(Botan_FFI::FFI_Error& e) {
136 return ffi_error_exception_thrown(func_name, e.what(), e.error_code());
137 } catch(Botan::Exception& e) {
138 return ffi_error_exception_thrown(func_name, e.what(), ffi_map_error_type(e.error_type()));
139 } catch(std::exception& e) {
140 return ffi_error_exception_thrown(func_name, e.what());
141 } catch(...) {
142 return ffi_error_exception_thrown(func_name, "unknown exception");
143 }
144}
145
146} // namespace Botan_FFI
147
148extern "C" {
149
150using namespace Botan_FFI;
151
153 return g_last_exception_what.c_str();
154}
155
156const char* botan_error_description(int err) {
157 switch(err) {
159 return "OK";
160
162 return "Invalid verifier";
163
165 return "Invalid input";
166
168 return "Invalid authentication code";
169
171 return "Insufficient buffer space";
172
174 return "String conversion error";
175
177 return "Exception thrown";
178
180 return "Out of memory";
181
183 return "Error while calling system API";
184
186 return "Internal error";
187
189 return "Bad flag";
190
192 return "Null pointer argument";
193
195 return "Bad parameter";
196
198 return "Key not set on object";
199
201 return "Invalid key length";
202
204 return "Invalid object state";
205
207 return "Not implemented";
208
210 return "Invalid object handle";
211
213 return "TLS error";
214
216 return "HTTP error";
217
219 return "Unknown error";
220
221 default:
222 return "Unknown error";
223 }
224}
225
226/*
227* Versioning
228*/
230 return BOTAN_HAS_FFI;
231}
232
233int botan_ffi_supports_api(uint32_t api_version) {
234 // This is the API introduced in 3.4
235 if(api_version == 20240408) {
236 return BOTAN_FFI_SUCCESS;
237 }
238
239 // This is the API introduced in 3.2
240 if(api_version == 20231009) {
241 return BOTAN_FFI_SUCCESS;
242 }
243
244 // This is the API introduced in 3.1
245 if(api_version == 20230711) {
246 return BOTAN_FFI_SUCCESS;
247 }
248
249 // This is the API introduced in 3.0
250 if(api_version == 20230403) {
251 return BOTAN_FFI_SUCCESS;
252 }
253
254 // This is the API introduced in 2.18
255 if(api_version == 20210220) {
256 return BOTAN_FFI_SUCCESS;
257 }
258
259 // This is the API introduced in 2.13
260 if(api_version == 20191214) {
261 return BOTAN_FFI_SUCCESS;
262 }
263
264 // This is the API introduced in 2.8
265 if(api_version == 20180713) {
266 return BOTAN_FFI_SUCCESS;
267 }
268
269 // This is the API introduced in 2.3
270 if(api_version == 20170815) {
271 return BOTAN_FFI_SUCCESS;
272 }
273
274 // This is the API introduced in 2.1
275 if(api_version == 20170327) {
276 return BOTAN_FFI_SUCCESS;
277 }
278
279 // This is the API introduced in 2.0
280 if(api_version == 20150515) {
281 return BOTAN_FFI_SUCCESS;
282 }
283
284 // Something else:
285 return -1;
286}
287
288const char* botan_version_string() {
289 return Botan::version_cstr();
290}
291
293 return Botan::version_major();
294}
295
297 return Botan::version_minor();
298}
299
301 return Botan::version_patch();
302}
303
306}
307
308int botan_constant_time_compare(const uint8_t* x, const uint8_t* y, size_t len) {
309 auto same = Botan::CT::is_equal(x, y, len);
310 // Return 0 if same or -1 otherwise
311 return static_cast<int>(same.select(1, 0)) - 1;
312}
313
314int botan_same_mem(const uint8_t* x, const uint8_t* y, size_t len) {
315 return botan_constant_time_compare(x, y, len);
316}
317
318int botan_scrub_mem(void* mem, size_t bytes) {
319 Botan::secure_scrub_memory(mem, bytes);
320 return BOTAN_FFI_SUCCESS;
321}
322
323int botan_hex_encode(const uint8_t* in, size_t len, char* out, uint32_t flags) {
324 return ffi_guard_thunk(__func__, [=]() -> int {
325 const bool uppercase = (flags & BOTAN_FFI_HEX_LOWER_CASE) == 0;
326 Botan::hex_encode(out, in, len, uppercase);
327 return BOTAN_FFI_SUCCESS;
328 });
329}
330
331int botan_hex_decode(const char* hex_str, size_t in_len, uint8_t* out, size_t* out_len) {
332 return ffi_guard_thunk(__func__, [=]() -> int {
333 const std::vector<uint8_t> bin = Botan::hex_decode(hex_str, in_len);
334 return Botan_FFI::write_vec_output(out, out_len, bin);
335 });
336}
337
338int botan_base64_encode(const uint8_t* in, size_t len, char* out, size_t* out_len) {
339 return ffi_guard_thunk(__func__, [=]() -> int {
340 const std::string base64 = Botan::base64_encode(in, len);
341 return Botan_FFI::write_str_output(out, out_len, base64);
342 });
343}
344
345int botan_base64_decode(const char* base64_str, size_t in_len, uint8_t* out, size_t* out_len) {
346 return ffi_guard_thunk(__func__, [=]() -> int {
347 if(*out_len < Botan::base64_decode_max_output(in_len)) {
348 *out_len = Botan::base64_decode_max_output(in_len);
350 }
351
352 *out_len = Botan::base64_decode(out, std::string(base64_str, in_len));
353 return BOTAN_FFI_SUCCESS;
354 });
355}
356}
const char * what() const noexcept override
Definition exceptn.h:93
Botan::ErrorType error_type() const noexcept override
Definition ffi_util.h:26
int error_code() const noexcept override
Definition ffi_util.h:24
uint32_t botan_version_datestamp()
Definition ffi.cpp:304
int botan_same_mem(const uint8_t *x, const uint8_t *y, size_t len)
Definition ffi.cpp:314
const char * botan_version_string()
Definition ffi.cpp:288
int botan_base64_decode(const char *base64_str, size_t in_len, uint8_t *out, size_t *out_len)
Definition ffi.cpp:345
uint32_t botan_version_patch()
Definition ffi.cpp:300
int botan_base64_encode(const uint8_t *in, size_t len, char *out, size_t *out_len)
Definition ffi.cpp:338
int botan_scrub_mem(void *mem, size_t bytes)
Definition ffi.cpp:318
int botan_hex_encode(const uint8_t *in, size_t len, char *out, uint32_t flags)
Definition ffi.cpp:323
uint32_t botan_version_major()
Definition ffi.cpp:292
uint32_t botan_ffi_api_version()
Definition ffi.cpp:229
int botan_ffi_supports_api(uint32_t api_version)
Definition ffi.cpp:233
const char * botan_error_description(int err)
Definition ffi.cpp:156
uint32_t botan_version_minor()
Definition ffi.cpp:296
int botan_constant_time_compare(const uint8_t *x, const uint8_t *y, size_t len)
Definition ffi.cpp:308
int botan_hex_decode(const char *hex_str, size_t in_len, uint8_t *out, size_t *out_len)
Definition ffi.cpp:331
const char * botan_error_last_exception_message()
Definition ffi.cpp:152
#define BOTAN_FFI_HEX_LOWER_CASE
Definition ffi.h:237
void * botan_view_ctx
Definition ffi.h:146
@ BOTAN_FFI_ERROR_TPM_ERROR
Definition ffi.h:141
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition ffi.h:135
@ BOTAN_FFI_ERROR_INVALID_KEY_LENGTH
Definition ffi.h:132
@ BOTAN_FFI_ERROR_KEY_NOT_SET
Definition ffi.h:131
@ BOTAN_FFI_ERROR_TLS_ERROR
Definition ffi.h:138
@ BOTAN_FFI_ERROR_EXCEPTION_THROWN
Definition ffi.h:123
@ BOTAN_FFI_ERROR_OUT_OF_MEMORY
Definition ffi.h:124
@ BOTAN_FFI_ERROR_INTERNAL_ERROR
Definition ffi.h:126
@ BOTAN_FFI_INVALID_VERIFIER
Definition ffi.h:115
@ BOTAN_FFI_ERROR_INVALID_OBJECT
Definition ffi.h:136
@ BOTAN_FFI_ERROR_UNKNOWN_ERROR
Definition ffi.h:143
@ BOTAN_FFI_ERROR_HTTP_ERROR
Definition ffi.h:139
@ BOTAN_FFI_ERROR_BAD_FLAG
Definition ffi.h:128
@ BOTAN_FFI_ERROR_INVALID_INPUT
Definition ffi.h:117
@ BOTAN_FFI_ERROR_STRING_CONVERSION_ERROR
Definition ffi.h:121
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:129
@ BOTAN_FFI_SUCCESS
Definition ffi.h:114
@ BOTAN_FFI_ERROR_SYSTEM_ERROR
Definition ffi.h:125
@ BOTAN_FFI_ERROR_ROUGHTIME_ERROR
Definition ffi.h:140
@ BOTAN_FFI_ERROR_INVALID_OBJECT_STATE
Definition ffi.h:133
@ BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE
Definition ffi.h:120
@ BOTAN_FFI_ERROR_BAD_MAC
Definition ffi.h:118
@ BOTAN_FFI_ERROR_BAD_PARAMETER
Definition ffi.h:130
#define BOTAN_HAS_FFI
Definition build.h:253
constexpr CT::Mask< T > is_equal(const T x[], const T y[], size_t len)
Definition ct_utils.h:788
bool read_env_variable(std::string &value_out, std::string_view var_name)
Definition os_utils.cpp:443
int ffi_error_exception_thrown(const char *func_name, const char *exn, int rc)
Definition ffi.cpp:31
int botan_view_bin_bounce_fn(botan_view_ctx vctx, const uint8_t *buf, size_t len)
Definition ffi.cpp:48
int botan_view_str_bounce_fn(botan_view_ctx vctx, const char *str, size_t len)
Definition ffi.cpp:44
int write_str_output(uint8_t out[], size_t *out_len, std::string_view str)
Definition ffi_util.h:205
int ffi_guard_thunk(const char *func_name, const std::function< int()> &thunk)
Definition ffi.cpp:128
int write_vec_output(uint8_t out[], size_t *out_len, const std::vector< uint8_t, Alloc > &buf)
Definition ffi_util.h:201
uint32_t version_minor()
Definition version.cpp:86
uint32_t version_major()
Definition version.cpp:82
const char * version_cstr()
Definition version.cpp:33
size_t base64_encode(char out[], const uint8_t in[], size_t input_length, size_t &input_consumed, bool final_inputs)
Definition base64.cpp:160
uint32_t version_datestamp()
Definition version.cpp:75
void secure_scrub_memory(void *ptr, size_t n)
Definition mem_utils.cpp:19
uint32_t version_patch()
Definition version.cpp:90
size_t base64_decode(uint8_t out[], const char in[], size_t input_length, size_t &input_consumed, bool final_inputs, bool ignore_ws)
Definition base64.cpp:168
ErrorType
Definition exceptn.h:20
void hex_encode(char output[], const uint8_t input[], size_t input_length, bool uppercase)
Definition hex.cpp:35
size_t base64_decode_max_output(size_t input_length)
Definition base64.cpp:200
size_t hex_decode(uint8_t output[], const char input[], size_t input_length, size_t &input_consumed, bool ignore_ws)
Definition hex.cpp:73
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:147
constexpr void clear_mem(T *ptr, size_t n)
Definition mem_ops.h:121