Botan 3.11.0
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
17#if defined(BOTAN_HAS_OS_UTILS)
18 #include <botan/internal/os_utils.h>
19#endif
20
21namespace Botan_FFI {
22
23namespace {
24
25// NOLINTNEXTLINE(*-avoid-non-const-global-variables)
26thread_local std::string g_last_exception_what;
27
28int ffi_map_error_type(Botan::ErrorType err) {
29 switch(err) {
32
42
45
59
63
66
71
78 }
79
81}
82
83} // namespace
84
86 g_last_exception_what.clear();
87}
88
89int ffi_error_exception_thrown(const char* func_name, const char* exn, int rc) {
90 g_last_exception_what.assign(exn);
91
92#if defined(BOTAN_HAS_OS_UTILS)
93 std::string val;
94 if(Botan::OS::read_env_variable(val, "BOTAN_FFI_PRINT_EXCEPTIONS") && !val.empty()) {
95 // NOLINTNEXTLINE(*-vararg)
96 static_cast<void>(std::fprintf(stderr, "in %s exception '%s' returning %d\n", func_name, exn, rc));
97 }
98#endif
99
100 return rc;
101}
102
103int ffi_error_exception_thrown(const char* func_name, const char* exn, Botan::ErrorType err) {
104 return ffi_error_exception_thrown(func_name, exn, ffi_map_error_type(err));
105}
106
107int botan_view_str_bounce_fn(botan_view_ctx vctx, const char* str, size_t len) {
108 return botan_view_bin_bounce_fn(vctx, reinterpret_cast<const uint8_t*>(str), len);
109}
110
111int botan_view_bin_bounce_fn(botan_view_ctx vctx, const uint8_t* buf, size_t len) {
112 if(vctx == nullptr || buf == nullptr) {
114 }
115
116 const botan_view_bounce_struct* ctx = static_cast<botan_view_bounce_struct*>(vctx);
117
118 const size_t avail = *ctx->out_len;
119 *ctx->out_len = len;
120
121 if(avail < len || ctx->out_ptr == nullptr) {
122 if(ctx->out_ptr != nullptr) {
123 Botan::clear_mem(ctx->out_ptr, avail);
124 }
126 } else {
127 Botan::copy_mem(ctx->out_ptr, buf, len);
128 return BOTAN_FFI_SUCCESS;
129 }
130}
131
132} // namespace Botan_FFI
133
134extern "C" {
135
136using namespace Botan_FFI;
137
139 return g_last_exception_what.c_str();
140}
141
142const char* botan_error_description(int err) {
143 switch(err) {
145 return "OK";
146
148 return "Invalid verifier";
149
151 return "Invalid input";
152
154 return "Invalid authentication code";
155
157 return "No value available";
158
160 return "Insufficient buffer space";
161
163 return "String conversion error";
164
166 return "Exception thrown";
167
169 return "Out of memory";
170
172 return "Error while calling system API";
173
175 return "Internal error";
176
178 return "Bad flag";
179
181 return "Null pointer argument";
182
184 return "Bad parameter";
185
187 return "Key not set on object";
188
190 return "Invalid key length";
191
193 return "Invalid object state";
194
196 return "Index out of range";
197
199 return "Not implemented";
200
202 return "Invalid object handle";
203
205 return "TLS error";
206
208 return "HTTP error";
209
211 default:
212 return "Unknown error";
213 }
214}
215
216/*
217* Versioning
218*/
220 return BOTAN_HAS_FFI;
221}
222
223int botan_ffi_supports_api(uint32_t api_version) {
224 // This is the API introduced in 3.11
225 if(api_version == 20260303) {
226 return BOTAN_FFI_SUCCESS;
227 }
228
229 // This is the API introduced in 3.10
230 if(api_version == 20250829) {
231 return BOTAN_FFI_SUCCESS;
232 }
233
234 // This is the API introduced in 3.8
235 if(api_version == 20250506) {
236 return BOTAN_FFI_SUCCESS;
237 }
238
239 // This is the API introduced in 3.4
240 if(api_version == 20240408) {
241 return BOTAN_FFI_SUCCESS;
242 }
243
244 // This is the API introduced in 3.2
245 if(api_version == 20231009) {
246 return BOTAN_FFI_SUCCESS;
247 }
248
249 // This is the API introduced in 3.1
250 if(api_version == 20230711) {
251 return BOTAN_FFI_SUCCESS;
252 }
253
254 // This is the API introduced in 3.0
255 if(api_version == 20230403) {
256 return BOTAN_FFI_SUCCESS;
257 }
258
259 // This is the API introduced in 2.18
260 if(api_version == 20210220) {
261 return BOTAN_FFI_SUCCESS;
262 }
263
264 // This is the API introduced in 2.13
265 if(api_version == 20191214) {
266 return BOTAN_FFI_SUCCESS;
267 }
268
269 // This is the API introduced in 2.8
270 if(api_version == 20180713) {
271 return BOTAN_FFI_SUCCESS;
272 }
273
274 // This is the API introduced in 2.3
275 if(api_version == 20170815) {
276 return BOTAN_FFI_SUCCESS;
277 }
278
279 // This is the API introduced in 2.1
280 if(api_version == 20170327) {
281 return BOTAN_FFI_SUCCESS;
282 }
283
284 // This is the API introduced in 2.0
285 if(api_version == 20150515) {
286 return BOTAN_FFI_SUCCESS;
287 }
288
289 // Something else:
290 return -1;
291}
292
293const char* botan_version_string() {
294 return Botan::version_cstr();
295}
296
298 return Botan::version_major();
299}
300
302 return Botan::version_minor();
303}
304
306 return Botan::version_patch();
307}
308
311}
312
313int botan_constant_time_compare(const uint8_t* x, const uint8_t* y, size_t len) {
314 auto same = Botan::CT::is_equal(x, y, len);
315 // Return 0 if same or -1 otherwise
316 return static_cast<int>(same.select(1, 0)) - 1;
317}
318
319int botan_same_mem(const uint8_t* x, const uint8_t* y, size_t len) {
320 return botan_constant_time_compare(x, y, len);
321}
322
323int botan_scrub_mem(void* mem, size_t bytes) {
324 Botan::secure_scrub_memory(mem, bytes);
325 return BOTAN_FFI_SUCCESS;
326}
327
328int botan_hex_encode(const uint8_t* in, size_t len, char* out, uint32_t flags) {
329 return ffi_guard_thunk(__func__, [=]() -> int {
330 const bool uppercase = (flags & BOTAN_FFI_HEX_LOWER_CASE) == 0;
331 Botan::hex_encode(out, in, len, uppercase);
332 return BOTAN_FFI_SUCCESS;
333 });
334}
335
336int botan_hex_decode(const char* hex_str, size_t in_len, uint8_t* out, size_t* out_len) {
337 return ffi_guard_thunk(__func__, [=]() -> int {
338 const std::vector<uint8_t> bin = Botan::hex_decode(hex_str, in_len);
339 return Botan_FFI::write_vec_output(out, out_len, bin);
340 });
341}
342
343int botan_base64_encode(const uint8_t* in, size_t len, char* out, size_t* out_len) {
344 return ffi_guard_thunk(__func__, [=]() -> int {
345 const std::string base64 = Botan::base64_encode(in, len);
346 return Botan_FFI::write_str_output(out, out_len, base64);
347 });
348}
349
350int botan_base64_decode(const char* base64_str, size_t in_len, uint8_t* out, size_t* out_len) {
351 return ffi_guard_thunk(__func__, [=]() -> int {
352 if(*out_len < Botan::base64_decode_max_output(in_len)) {
353 *out_len = Botan::base64_decode_max_output(in_len);
355 }
356
357 *out_len = Botan::base64_decode(out, std::string(base64_str, in_len));
358 return BOTAN_FFI_SUCCESS;
359 });
360}
361}
uint32_t botan_version_datestamp()
Definition ffi.cpp:309
int botan_same_mem(const uint8_t *x, const uint8_t *y, size_t len)
Definition ffi.cpp:319
const char * botan_version_string()
Definition ffi.cpp:293
int botan_base64_decode(const char *base64_str, size_t in_len, uint8_t *out, size_t *out_len)
Definition ffi.cpp:350
uint32_t botan_version_patch()
Definition ffi.cpp:305
int botan_base64_encode(const uint8_t *in, size_t len, char *out, size_t *out_len)
Definition ffi.cpp:343
int botan_scrub_mem(void *mem, size_t bytes)
Definition ffi.cpp:323
int botan_hex_encode(const uint8_t *in, size_t len, char *out, uint32_t flags)
Definition ffi.cpp:328
uint32_t botan_version_major()
Definition ffi.cpp:297
uint32_t botan_ffi_api_version()
Definition ffi.cpp:219
int botan_ffi_supports_api(uint32_t api_version)
Definition ffi.cpp:223
const char * botan_error_description(int err)
Definition ffi.cpp:142
uint32_t botan_version_minor()
Definition ffi.cpp:301
int botan_constant_time_compare(const uint8_t *x, const uint8_t *y, size_t len)
Definition ffi.cpp:313
int botan_hex_decode(const char *hex_str, size_t in_len, uint8_t *out, size_t *out_len)
Definition ffi.cpp:336
const char * botan_error_last_exception_message()
Definition ffi.cpp:138
#define BOTAN_FFI_HEX_LOWER_CASE
Definition ffi.h:249
void * botan_view_ctx
Definition ffi.h:154
@ BOTAN_FFI_ERROR_TPM_ERROR
Definition ffi.h:146
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition ffi.h:140
@ BOTAN_FFI_ERROR_INVALID_KEY_LENGTH
Definition ffi.h:136
@ BOTAN_FFI_ERROR_KEY_NOT_SET
Definition ffi.h:135
@ BOTAN_FFI_ERROR_TLS_ERROR
Definition ffi.h:143
@ BOTAN_FFI_ERROR_EXCEPTION_THROWN
Definition ffi.h:127
@ BOTAN_FFI_ERROR_OUT_OF_MEMORY
Definition ffi.h:128
@ BOTAN_FFI_ERROR_OUT_OF_RANGE
Definition ffi.h:138
@ BOTAN_FFI_ERROR_INTERNAL_ERROR
Definition ffi.h:130
@ BOTAN_FFI_INVALID_VERIFIER
Definition ffi.h:118
@ BOTAN_FFI_ERROR_INVALID_OBJECT
Definition ffi.h:141
@ BOTAN_FFI_ERROR_UNKNOWN_ERROR
Definition ffi.h:148
@ BOTAN_FFI_ERROR_HTTP_ERROR
Definition ffi.h:144
@ BOTAN_FFI_ERROR_BAD_FLAG
Definition ffi.h:132
@ BOTAN_FFI_ERROR_INVALID_INPUT
Definition ffi.h:120
@ BOTAN_FFI_ERROR_STRING_CONVERSION_ERROR
Definition ffi.h:125
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:133
@ BOTAN_FFI_SUCCESS
Definition ffi.h:116
@ BOTAN_FFI_ERROR_SYSTEM_ERROR
Definition ffi.h:129
@ BOTAN_FFI_ERROR_ROUGHTIME_ERROR
Definition ffi.h:145
@ BOTAN_FFI_ERROR_NO_VALUE
Definition ffi.h:122
@ BOTAN_FFI_ERROR_INVALID_OBJECT_STATE
Definition ffi.h:137
@ BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE
Definition ffi.h:124
@ BOTAN_FFI_ERROR_BAD_MAC
Definition ffi.h:121
@ BOTAN_FFI_ERROR_BAD_PARAMETER
Definition ffi.h:134
#define BOTAN_HAS_FFI
Definition build.h:199
constexpr CT::Mask< T > is_equal(const T x[], const T y[], size_t len)
Definition ct_utils.h:798
bool read_env_variable(std::string &value_out, std::string_view var_name)
Definition os_utils.cpp:448
void ffi_clear_last_exception()
Definition ffi.cpp:85
int ffi_error_exception_thrown(const char *func_name, const char *exn, int rc)
Definition ffi.cpp:89
int botan_view_bin_bounce_fn(botan_view_ctx vctx, const uint8_t *buf, size_t len)
Definition ffi.cpp:111
int botan_view_str_bounce_fn(botan_view_ctx vctx, const char *str, size_t len)
Definition ffi.cpp:107
int ffi_guard_thunk(const char *func_name, T thunk)
Definition ffi_util.h:95
int write_vec_output(uint8_t out[], size_t *out_len, std::span< const uint8_t > buf)
Definition ffi_util.h:261
int write_str_output(char out[], size_t *out_len, const std::string &str)
Definition ffi_util.h:265
uint32_t version_minor()
Definition version.cpp:59
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:144
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:159
uint32_t version_datestamp()
Definition version.cpp:32
void secure_scrub_memory(void *ptr, size_t n)
Definition mem_utils.cpp:25
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:167
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:199
size_t hex_decode(uint8_t output[], const char input[], size_t input_length, size_t &input_consumed, bool ignore_ws)
Definition hex.cpp:72
ErrorType
Definition exceptn.h:21
constexpr void clear_mem(T *ptr, size_t n)
Definition mem_ops.h:118