Botan 3.14.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/assert.h>
10#include <botan/base64.h>
11#include <botan/hex.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>
16#include <cstdio>
17#include <cstring>
18#include <type_traits>
19
20#if defined(BOTAN_HAS_OS_UTILS)
21 #include <botan/internal/os_utils.h>
22#endif
23
24namespace Botan_FFI {
25
26namespace {
27
28constexpr size_t FFI_ERROR_BUFFER_SIZE = 512;
29// NOLINTNEXTLINE(*-avoid-non-const-global-variables)
30thread_local char g_last_exception_what[FFI_ERROR_BUFFER_SIZE] = {};
31
32// This object must remain writable even after destruction of global objects
33static_assert(std::is_trivially_destructible_v<decltype(g_last_exception_what)>);
34
35int ffi_map_error_type(Botan::ErrorType err) {
36 switch(err) {
39
49
52
66
70
73
78
85 }
86
88}
89
90} // namespace
91
93 g_last_exception_what[0] = 0;
94}
95
96int ffi_error_exception_thrown(const char* func_name, const char* exn, int rc) {
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);
101 } else {
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));
105 }
106
107#if defined(BOTAN_HAS_OS_UTILS)
108 std::string val;
109 if(Botan::OS::read_env_variable(val, "BOTAN_FFI_PRINT_EXCEPTIONS") && !val.empty()) {
110 // NOLINTNEXTLINE(*-vararg)
111 static_cast<void>(std::fprintf(stderr, "in %s exception '%s' returning %d\n", func_name, exn, rc));
112 }
113#endif
114
115 return rc;
116}
117
118int ffi_error_exception_thrown(const char* func_name, const char* exn, Botan::ErrorType err) {
119 return ffi_error_exception_thrown(func_name, exn, ffi_map_error_type(err));
120}
121
122int botan_view_str_bounce_fn(botan_view_ctx vctx, const char* str, size_t len) {
123 return botan_view_bin_bounce_fn(vctx, reinterpret_cast<const uint8_t*>(str), len);
124}
125
126int botan_view_bin_bounce_fn(botan_view_ctx vctx, const uint8_t* buf, size_t len) {
127 if(any_null_pointers(vctx, buf)) {
129 }
130
131 const botan_view_bounce_struct* ctx = static_cast<botan_view_bounce_struct*>(vctx);
132
133 if(ctx->out_len == nullptr) {
135 }
136
137 const size_t avail = *ctx->out_len;
138 *ctx->out_len = len;
139
140 if(avail < len || ctx->out_ptr == nullptr) {
141 if(ctx->out_ptr != nullptr) {
142 Botan::clear_mem(ctx->out_ptr, avail);
143 }
145 } else {
146 Botan::copy_mem(ctx->out_ptr, buf, len);
147 return BOTAN_FFI_SUCCESS;
148 }
149}
150
151} // namespace Botan_FFI
152
153extern "C" {
154
155using namespace Botan_FFI;
156
158 return g_last_exception_what;
159}
160
161const char* botan_error_description(int err) {
162 switch(err) {
164 return "OK";
165
167 return "Invalid verifier";
168
170 return "Invalid input";
171
173 return "Invalid authentication code";
174
176 return "No value available";
177
179 return "Insufficient buffer space";
180
182 return "String conversion error";
183
185 return "Exception thrown";
186
188 return "Out of memory";
189
191 return "Error while calling system API";
192
194 return "Internal error";
195
197 return "Bad flag";
198
200 return "Null pointer argument";
201
203 return "Bad parameter";
204
206 return "Key not set on object";
207
209 return "Invalid key length";
210
212 return "Invalid object state";
213
215 return "Index out of range";
216
218 return "Not implemented";
219
221 return "Invalid object handle";
222
224 return "TLS error";
225
227 return "HTTP error";
228
230 default:
231 return "Unknown error";
232 }
233}
234
235/*
236* Versioning
237*/
239 return BOTAN_HAS_FFI;
240}
241
242int botan_ffi_supports_api(uint32_t api_version) {
243 // This is the API introduced in 3.14.0 (not advertised until 3.14.1)
244 if(api_version == 20261104) {
245 return BOTAN_FFI_SUCCESS;
246 }
247
248 // This is the API introduced in 3.13
249 if(api_version == 20260811) {
250 return BOTAN_FFI_SUCCESS;
251 }
252
253 // This is the API introduced in 3.12
254 if(api_version == 20260506) {
255 return BOTAN_FFI_SUCCESS;
256 }
257
258 // This is the API introduced in 3.11
259 if(api_version == 20260303) {
260 return BOTAN_FFI_SUCCESS;
261 }
262
263 // This is the API introduced in 3.10
264 if(api_version == 20250829) {
265 return BOTAN_FFI_SUCCESS;
266 }
267
268 // This is the API introduced in 3.8
269 if(api_version == 20250506) {
270 return BOTAN_FFI_SUCCESS;
271 }
272
273 // This is the API introduced in 3.4
274 if(api_version == 20240408) {
275 return BOTAN_FFI_SUCCESS;
276 }
277
278 // This is the API introduced in 3.2
279 if(api_version == 20231009) {
280 return BOTAN_FFI_SUCCESS;
281 }
282
283 // This is the API introduced in 3.1
284 if(api_version == 20230711) {
285 return BOTAN_FFI_SUCCESS;
286 }
287
288 // This is the API introduced in 3.0
289 if(api_version == 20230403) {
290 return BOTAN_FFI_SUCCESS;
291 }
292
293 // This is the API introduced in 2.18
294 if(api_version == 20210220) {
295 return BOTAN_FFI_SUCCESS;
296 }
297
298 // This is the API introduced in 2.13
299 if(api_version == 20191214) {
300 return BOTAN_FFI_SUCCESS;
301 }
302
303 // This is the API introduced in 2.8
304 if(api_version == 20180713) {
305 return BOTAN_FFI_SUCCESS;
306 }
307
308 // This is the API introduced in 2.3
309 if(api_version == 20170815) {
310 return BOTAN_FFI_SUCCESS;
311 }
312
313 // This is the API introduced in 2.1
314 if(api_version == 20170327) {
315 return BOTAN_FFI_SUCCESS;
316 }
317
318 // This is the API introduced in 2.0
319 if(api_version == 20150515) {
320 return BOTAN_FFI_SUCCESS;
321 }
322
323 // Something else:
324 return -1;
325}
326
328#if defined(BOTAN_HAS_FFI_TLS)
329 return BOTAN_HAS_FFI_TLS;
330#else
331 return 0;
332#endif
333}
334
335int botan_ffi_tls_supports_api(uint32_t api_version) {
336#if defined(BOTAN_HAS_FFI_TLS)
337 if(api_version == BOTAN_HAS_FFI_TLS) {
338 return BOTAN_FFI_SUCCESS;
339 }
340#else
341 BOTAN_UNUSED(api_version);
342#endif
343
344 return -1;
345}
346
347const char* botan_version_string() {
348 return Botan::version_cstr();
349}
350
352 return Botan::version_major();
353}
354
356 return Botan::version_minor();
357}
358
360 return Botan::version_patch();
361}
362
365}
366
367int botan_constant_time_compare(const uint8_t* x, const uint8_t* y, size_t len) {
368 if(len > 0 && any_null_pointers(x, y)) {
370 }
371 const auto same = Botan::CT::is_equal(x, y, len);
372 // Return 0 if same or -1 otherwise
373 return static_cast<int>(same.select(1, 0)) - 1;
374}
375
376int botan_same_mem(const uint8_t* x, const uint8_t* y, size_t len) {
377 return botan_constant_time_compare(x, y, len);
378}
379
380int botan_scrub_mem(void* mem, size_t bytes) {
381 if(bytes > 0 && mem == nullptr) {
383 }
384 Botan::secure_scrub_memory(mem, bytes);
385 return BOTAN_FFI_SUCCESS;
386}
387
388int botan_hex_encode(const uint8_t* in, size_t len, char* out, uint32_t flags) {
389 if(len > 0 && (in == nullptr || out == nullptr)) {
391 }
392 return ffi_guard_thunk(__func__, [=]() -> int {
393 const bool uppercase = (flags & BOTAN_FFI_HEX_LOWER_CASE) == 0;
394 Botan::hex_encode(out, in, len, uppercase);
395 return BOTAN_FFI_SUCCESS;
396 });
397}
398
399int botan_hex_decode(const char* hex_str, size_t in_len, uint8_t* out, size_t* out_len) {
400 if(any_null_pointers(hex_str, out_len)) {
402 }
403 return ffi_guard_thunk(__func__, [=]() -> int {
404 const std::vector<uint8_t> bin = Botan::hex_decode(hex_str, in_len);
405 return Botan_FFI::write_vec_output(out, out_len, bin);
406 });
407}
408
409int botan_base64_encode(const uint8_t* in, size_t len, char* out, size_t* out_len) {
410 if(len > 0 && in == nullptr) {
412 }
413 return ffi_guard_thunk(__func__, [=]() -> int {
414 const std::string base64 = Botan::base64_encode(in, len);
415 return Botan_FFI::write_str_output(out, out_len, base64);
416 });
417}
418
419int botan_base64_decode(const char* base64_str, size_t in_len, uint8_t* out, size_t* out_len) {
420 if(any_null_pointers(out, out_len, base64_str)) {
422 }
423
424 return ffi_guard_thunk(__func__, [=]() -> int {
425 if(*out_len < Botan::base64_decode_max_output(in_len)) {
426 *out_len = Botan::base64_decode_max_output(in_len);
428 }
429
430 *out_len = Botan::base64_decode(out, std::string(base64_str, in_len));
431 return BOTAN_FFI_SUCCESS;
432 });
433}
434
435int botan_is_shared(void* handle) {
436 if(any_null_pointers(handle)) {
438 }
439
440 return ffi_guard_thunk(__func__, [=]() -> int {
441 const auto* obj = static_cast<const Botan_FFI::botan_ffi_obj_base*>(handle);
442 if(!obj->appears_to_be_valid_ffi_handle()) {
444 }
445
446 return obj->is_shared() ? 1 : 0;
447 });
448}
449
450int botan_use_count(void* handle, size_t* out_use_count) {
451 if(out_use_count != nullptr) {
452 // Set the out-param to an implausible value, in case any of the error
453 // paths below return early.
454 *out_use_count = 0;
455 }
456
457 if(any_null_pointers(handle, out_use_count)) {
459 }
460
461 return ffi_guard_thunk(__func__, [=]() -> int {
462 const auto* obj = static_cast<const Botan_FFI::botan_ffi_obj_base*>(handle);
463 if(!obj->appears_to_be_valid_ffi_handle()) {
465 }
466
467 *out_use_count = obj->use_count();
468 return BOTAN_FFI_SUCCESS;
469 });
470}
471}
#define BOTAN_UNUSED
Definition assert.h:151
uint32_t botan_version_datestamp()
Definition ffi.cpp:363
int botan_use_count(void *handle, size_t *out_use_count)
Definition ffi.cpp:450
int botan_same_mem(const uint8_t *x, const uint8_t *y, size_t len)
Definition ffi.cpp:376
const char * botan_version_string()
Definition ffi.cpp:347
int botan_base64_decode(const char *base64_str, size_t in_len, uint8_t *out, size_t *out_len)
Definition ffi.cpp:419
uint32_t botan_version_patch()
Definition ffi.cpp:359
int botan_is_shared(void *handle)
Definition ffi.cpp:435
int botan_base64_encode(const uint8_t *in, size_t len, char *out, size_t *out_len)
Definition ffi.cpp:409
int botan_scrub_mem(void *mem, size_t bytes)
Definition ffi.cpp:380
uint32_t botan_ffi_tls_api_version()
Definition ffi.cpp:327
int botan_hex_encode(const uint8_t *in, size_t len, char *out, uint32_t flags)
Definition ffi.cpp:388
uint32_t botan_version_major()
Definition ffi.cpp:351
uint32_t botan_ffi_api_version()
Definition ffi.cpp:238
int botan_ffi_supports_api(uint32_t api_version)
Definition ffi.cpp:242
const char * botan_error_description(int err)
Definition ffi.cpp:161
uint32_t botan_version_minor()
Definition ffi.cpp:355
int botan_constant_time_compare(const uint8_t *x, const uint8_t *y, size_t len)
Definition ffi.cpp:367
int botan_hex_decode(const char *hex_str, size_t in_len, uint8_t *out, size_t *out_len)
Definition ffi.cpp:399
int botan_ffi_tls_supports_api(uint32_t api_version)
Definition ffi.cpp:335
const char * botan_error_last_exception_message()
Definition ffi.cpp:157
#define BOTAN_FFI_HEX_LOWER_CASE
Definition ffi.h:263
void * botan_view_ctx
Definition ffi.h:152
@ BOTAN_FFI_ERROR_TPM_ERROR
Definition ffi.h:144
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition ffi.h:138
@ BOTAN_FFI_ERROR_INVALID_KEY_LENGTH
Definition ffi.h:134
@ BOTAN_FFI_ERROR_KEY_NOT_SET
Definition ffi.h:133
@ BOTAN_FFI_ERROR_TLS_ERROR
Definition ffi.h:141
@ BOTAN_FFI_ERROR_EXCEPTION_THROWN
Definition ffi.h:125
@ BOTAN_FFI_ERROR_OUT_OF_MEMORY
Definition ffi.h:126
@ BOTAN_FFI_ERROR_OUT_OF_RANGE
Definition ffi.h:136
@ BOTAN_FFI_ERROR_INTERNAL_ERROR
Definition ffi.h:128
@ BOTAN_FFI_INVALID_VERIFIER
Definition ffi.h:116
@ BOTAN_FFI_ERROR_INVALID_OBJECT
Definition ffi.h:139
@ BOTAN_FFI_ERROR_UNKNOWN_ERROR
Definition ffi.h:146
@ BOTAN_FFI_ERROR_HTTP_ERROR
Definition ffi.h:142
@ BOTAN_FFI_ERROR_BAD_FLAG
Definition ffi.h:130
@ BOTAN_FFI_ERROR_INVALID_INPUT
Definition ffi.h:118
@ BOTAN_FFI_ERROR_STRING_CONVERSION_ERROR
Definition ffi.h:123
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:131
@ BOTAN_FFI_SUCCESS
Definition ffi.h:114
@ BOTAN_FFI_ERROR_SYSTEM_ERROR
Definition ffi.h:127
@ BOTAN_FFI_ERROR_ROUGHTIME_ERROR
Definition ffi.h:143
@ BOTAN_FFI_ERROR_NO_VALUE
Definition ffi.h:120
@ BOTAN_FFI_ERROR_INVALID_OBJECT_STATE
Definition ffi.h:135
@ BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE
Definition ffi.h:122
@ BOTAN_FFI_ERROR_BAD_MAC
Definition ffi.h:119
@ BOTAN_FFI_ERROR_BAD_PARAMETER
Definition ffi.h:132
#define BOTAN_HAS_FFI
Definition build.h:203
constexpr CT::Mask< T > is_equal(const T x[], const T y[], size_t len)
Definition ct_utils.h:803
bool read_env_variable(std::string &value_out, std::string_view var_name)
Definition os_utils.cpp:471
void ffi_clear_last_exception()
Definition ffi.cpp:92
int ffi_error_exception_thrown(const char *func_name, const char *exn, int rc)
Definition ffi.cpp:96
int botan_view_bin_bounce_fn(botan_view_ctx vctx, const uint8_t *buf, size_t len)
Definition ffi.cpp:126
int botan_view_str_bounce_fn(botan_view_ctx vctx, const char *str, size_t len)
Definition ffi.cpp:122
int ffi_guard_thunk(const char *func_name, T thunk)
Definition ffi_util.h:229
int write_vec_output(uint8_t out[], size_t *out_len, std::span< const uint8_t > buf)
Definition ffi_util.h:436
bool any_null_pointers(Ptrs... ptr)
Definition mem_utils.h:54
int write_str_output(char out[], size_t *out_len, const std::string &str)
Definition ffi_util.h:440
uint32_t version_minor()
Definition version.cpp:59
uint32_t version_major()
Definition version.cpp:55
const char * version_cstr()
Definition version.cpp:20
size_t base64_encode(char out[], const uint8_t in[], size_t input_length, size_t &input_consumed, bool final_inputs)
Definition base64.cpp:167
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:144
uint32_t version_datestamp()
Definition version.cpp:32
void secure_scrub_memory(void *ptr, size_t n)
Definition mem_utils.cpp:24
uint32_t version_patch()
Definition version.cpp:63
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:203
void hex_encode(char output[], const uint8_t input[], size_t input_length, bool uppercase)
Definition hex.cpp:34
size_t base64_decode_max_output(size_t input_length)
Definition base64.cpp:273
size_t hex_decode(uint8_t output[], const char input[], size_t input_length, size_t &input_consumed, bool ignore_ws)
Definition hex.cpp:75
ErrorType
Definition exceptn.h:21
constexpr void clear_mem(T *ptr, size_t n)
Definition mem_ops.h:118
virtual size_t use_count() const =0
virtual bool is_shared() const =0