Botan 3.8.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
29int ffi_map_error_type(Botan::ErrorType err) {
30 switch(err) {
33
43
46
60
64
67
72
79 }
80
82}
83
84} // namespace
85
87 g_last_exception_what.clear();
88}
89
90int ffi_error_exception_thrown(const char* func_name, const char* exn, int rc) {
91 g_last_exception_what.assign(exn);
92
93#if defined(BOTAN_HAS_OS_UTILS)
94 std::string val;
95 if(Botan::OS::read_env_variable(val, "BOTAN_FFI_PRINT_EXCEPTIONS") == true && !val.empty()) {
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 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) {
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 "Not implemented";
197
199 return "Invalid object handle";
200
202 return "TLS error";
203
205 return "HTTP error";
206
208 return "Unknown error";
209
210 default:
211 return "Unknown error";
212 }
213}
214
215/*
216* Versioning
217*/
219 return BOTAN_HAS_FFI;
220}
221
222int botan_ffi_supports_api(uint32_t api_version) {
223 // This is the API introduced in 3.8
224 if(api_version == 20250506) {
225 return BOTAN_FFI_SUCCESS;
226 }
227
228 // This is the API introduced in 3.4
229 if(api_version == 20240408) {
230 return BOTAN_FFI_SUCCESS;
231 }
232
233 // This is the API introduced in 3.2
234 if(api_version == 20231009) {
235 return BOTAN_FFI_SUCCESS;
236 }
237
238 // This is the API introduced in 3.1
239 if(api_version == 20230711) {
240 return BOTAN_FFI_SUCCESS;
241 }
242
243 // This is the API introduced in 3.0
244 if(api_version == 20230403) {
245 return BOTAN_FFI_SUCCESS;
246 }
247
248 // This is the API introduced in 2.18
249 if(api_version == 20210220) {
250 return BOTAN_FFI_SUCCESS;
251 }
252
253 // This is the API introduced in 2.13
254 if(api_version == 20191214) {
255 return BOTAN_FFI_SUCCESS;
256 }
257
258 // This is the API introduced in 2.8
259 if(api_version == 20180713) {
260 return BOTAN_FFI_SUCCESS;
261 }
262
263 // This is the API introduced in 2.3
264 if(api_version == 20170815) {
265 return BOTAN_FFI_SUCCESS;
266 }
267
268 // This is the API introduced in 2.1
269 if(api_version == 20170327) {
270 return BOTAN_FFI_SUCCESS;
271 }
272
273 // This is the API introduced in 2.0
274 if(api_version == 20150515) {
275 return BOTAN_FFI_SUCCESS;
276 }
277
278 // Something else:
279 return -1;
280}
281
282const char* botan_version_string() {
283 return Botan::version_cstr();
284}
285
287 return Botan::version_major();
288}
289
291 return Botan::version_minor();
292}
293
295 return Botan::version_patch();
296}
297
300}
301
302int botan_constant_time_compare(const uint8_t* x, const uint8_t* y, size_t len) {
303 auto same = Botan::CT::is_equal(x, y, len);
304 // Return 0 if same or -1 otherwise
305 return static_cast<int>(same.select(1, 0)) - 1;
306}
307
308int botan_same_mem(const uint8_t* x, const uint8_t* y, size_t len) {
309 return botan_constant_time_compare(x, y, len);
310}
311
312int botan_scrub_mem(void* mem, size_t bytes) {
313 Botan::secure_scrub_memory(mem, bytes);
314 return BOTAN_FFI_SUCCESS;
315}
316
317int botan_hex_encode(const uint8_t* in, size_t len, char* out, uint32_t flags) {
318 return ffi_guard_thunk(__func__, [=]() -> int {
319 const bool uppercase = (flags & BOTAN_FFI_HEX_LOWER_CASE) == 0;
320 Botan::hex_encode(out, in, len, uppercase);
321 return BOTAN_FFI_SUCCESS;
322 });
323}
324
325int botan_hex_decode(const char* hex_str, size_t in_len, uint8_t* out, size_t* out_len) {
326 return ffi_guard_thunk(__func__, [=]() -> int {
327 const std::vector<uint8_t> bin = Botan::hex_decode(hex_str, in_len);
328 return Botan_FFI::write_vec_output(out, out_len, bin);
329 });
330}
331
332int botan_base64_encode(const uint8_t* in, size_t len, char* out, size_t* out_len) {
333 return ffi_guard_thunk(__func__, [=]() -> int {
334 const std::string base64 = Botan::base64_encode(in, len);
335 return Botan_FFI::write_str_output(out, out_len, base64);
336 });
337}
338
339int botan_base64_decode(const char* base64_str, size_t in_len, uint8_t* out, size_t* out_len) {
340 return ffi_guard_thunk(__func__, [=]() -> int {
341 if(*out_len < Botan::base64_decode_max_output(in_len)) {
342 *out_len = Botan::base64_decode_max_output(in_len);
344 }
345
346 *out_len = Botan::base64_decode(out, std::string(base64_str, in_len));
347 return BOTAN_FFI_SUCCESS;
348 });
349}
350}
uint32_t botan_version_datestamp()
Definition ffi.cpp:298
int botan_same_mem(const uint8_t *x, const uint8_t *y, size_t len)
Definition ffi.cpp:308
const char * botan_version_string()
Definition ffi.cpp:282
int botan_base64_decode(const char *base64_str, size_t in_len, uint8_t *out, size_t *out_len)
Definition ffi.cpp:339
uint32_t botan_version_patch()
Definition ffi.cpp:294
int botan_base64_encode(const uint8_t *in, size_t len, char *out, size_t *out_len)
Definition ffi.cpp:332
int botan_scrub_mem(void *mem, size_t bytes)
Definition ffi.cpp:312
int botan_hex_encode(const uint8_t *in, size_t len, char *out, uint32_t flags)
Definition ffi.cpp:317
uint32_t botan_version_major()
Definition ffi.cpp:286
uint32_t botan_ffi_api_version()
Definition ffi.cpp:218
int botan_ffi_supports_api(uint32_t api_version)
Definition ffi.cpp:222
const char * botan_error_description(int err)
Definition ffi.cpp:142
uint32_t botan_version_minor()
Definition ffi.cpp:290
int botan_constant_time_compare(const uint8_t *x, const uint8_t *y, size_t len)
Definition ffi.cpp:302
int botan_hex_decode(const char *hex_str, size_t in_len, uint8_t *out, size_t *out_len)
Definition ffi.cpp:325
const char * botan_error_last_exception_message()
Definition ffi.cpp:138
#define BOTAN_FFI_HEX_LOWER_CASE
Definition ffi.h:245
void * botan_view_ctx
Definition ffi.h:150
@ BOTAN_FFI_ERROR_TPM_ERROR
Definition ffi.h:142
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition ffi.h:136
@ BOTAN_FFI_ERROR_INVALID_KEY_LENGTH
Definition ffi.h:133
@ BOTAN_FFI_ERROR_KEY_NOT_SET
Definition ffi.h:132
@ BOTAN_FFI_ERROR_TLS_ERROR
Definition ffi.h:139
@ BOTAN_FFI_ERROR_EXCEPTION_THROWN
Definition ffi.h:124
@ BOTAN_FFI_ERROR_OUT_OF_MEMORY
Definition ffi.h:125
@ BOTAN_FFI_ERROR_INTERNAL_ERROR
Definition ffi.h:127
@ BOTAN_FFI_INVALID_VERIFIER
Definition ffi.h:115
@ BOTAN_FFI_ERROR_INVALID_OBJECT
Definition ffi.h:137
@ BOTAN_FFI_ERROR_UNKNOWN_ERROR
Definition ffi.h:144
@ BOTAN_FFI_ERROR_HTTP_ERROR
Definition ffi.h:140
@ BOTAN_FFI_ERROR_BAD_FLAG
Definition ffi.h:129
@ BOTAN_FFI_ERROR_INVALID_INPUT
Definition ffi.h:117
@ BOTAN_FFI_ERROR_STRING_CONVERSION_ERROR
Definition ffi.h:122
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:130
@ BOTAN_FFI_SUCCESS
Definition ffi.h:113
@ BOTAN_FFI_ERROR_SYSTEM_ERROR
Definition ffi.h:126
@ BOTAN_FFI_ERROR_ROUGHTIME_ERROR
Definition ffi.h:141
@ BOTAN_FFI_ERROR_NO_VALUE
Definition ffi.h:119
@ BOTAN_FFI_ERROR_INVALID_OBJECT_STATE
Definition ffi.h:134
@ BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE
Definition ffi.h:121
@ BOTAN_FFI_ERROR_BAD_MAC
Definition ffi.h:118
@ BOTAN_FFI_ERROR_BAD_PARAMETER
Definition ffi.h:131
#define BOTAN_HAS_FFI
Definition build.h:211
constexpr CT::Mask< T > is_equal(const T x[], const T y[], size_t len)
Definition ct_utils.h:789
bool read_env_variable(std::string &value_out, std::string_view var_name)
Definition os_utils.cpp:435
void ffi_clear_last_exception()
Definition ffi.cpp:86
int ffi_error_exception_thrown(const char *func_name, const char *exn, int rc)
Definition ffi.cpp:90
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 write_str_output(uint8_t out[], size_t *out_len, std::string_view str)
Definition ffi_util.h:230
int ffi_guard_thunk(const char *func_name, T thunk)
Definition ffi_util.h:83
int write_vec_output(uint8_t out[], size_t *out_len, std::span< const uint8_t > buf)
Definition ffi_util.h:226
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:160
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: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:149
constexpr void clear_mem(T *ptr, size_t n)
Definition mem_ops.h:123