Botan 3.9.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#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") && !val.empty()) {
96 // NOLINTNEXTLINE(*-vararg)
97 static_cast<void>(std::fprintf(stderr, "in %s exception '%s' returning %d\n", func_name, exn, rc));
98 }
99#endif
100
101 return rc;
102}
103
104int ffi_error_exception_thrown(const char* func_name, const char* exn, Botan::ErrorType err) {
105 return ffi_error_exception_thrown(func_name, exn, ffi_map_error_type(err));
106}
107
108int botan_view_str_bounce_fn(botan_view_ctx vctx, const char* str, size_t len) {
109 return botan_view_bin_bounce_fn(vctx, reinterpret_cast<const uint8_t*>(str), len);
110}
111
112int botan_view_bin_bounce_fn(botan_view_ctx vctx, const uint8_t* buf, size_t len) {
113 if(vctx == nullptr || buf == nullptr) {
115 }
116
117 botan_view_bounce_struct* ctx = static_cast<botan_view_bounce_struct*>(vctx);
118
119 const size_t avail = *ctx->out_len;
120 *ctx->out_len = len;
121
122 if(avail < len || ctx->out_ptr == nullptr) {
123 if(ctx->out_ptr != nullptr) {
124 Botan::clear_mem(ctx->out_ptr, avail);
125 }
127 } else {
128 Botan::copy_mem(ctx->out_ptr, buf, len);
129 return BOTAN_FFI_SUCCESS;
130 }
131}
132
133} // namespace Botan_FFI
134
135extern "C" {
136
137using namespace Botan_FFI;
138
140 return g_last_exception_what.c_str();
141}
142
143const char* botan_error_description(int err) {
144 switch(err) {
146 return "OK";
147
149 return "Invalid verifier";
150
152 return "Invalid input";
153
155 return "Invalid authentication code";
156
158 return "No value available";
159
161 return "Insufficient buffer space";
162
164 return "String conversion error";
165
167 return "Exception thrown";
168
170 return "Out of memory";
171
173 return "Error while calling system API";
174
176 return "Internal error";
177
179 return "Bad flag";
180
182 return "Null pointer argument";
183
185 return "Bad parameter";
186
188 return "Key not set on object";
189
191 return "Invalid key length";
192
194 return "Invalid object state";
195
197 return "Not implemented";
198
200 return "Invalid object handle";
201
203 return "TLS error";
204
206 return "HTTP error";
207
209 default:
210 return "Unknown error";
211 }
212}
213
214/*
215* Versioning
216*/
218 return BOTAN_HAS_FFI;
219}
220
221int botan_ffi_supports_api(uint32_t api_version) {
222 // This is the API introduced in 3.8
223 if(api_version == 20250506) {
224 return BOTAN_FFI_SUCCESS;
225 }
226
227 // This is the API introduced in 3.4
228 if(api_version == 20240408) {
229 return BOTAN_FFI_SUCCESS;
230 }
231
232 // This is the API introduced in 3.2
233 if(api_version == 20231009) {
234 return BOTAN_FFI_SUCCESS;
235 }
236
237 // This is the API introduced in 3.1
238 if(api_version == 20230711) {
239 return BOTAN_FFI_SUCCESS;
240 }
241
242 // This is the API introduced in 3.0
243 if(api_version == 20230403) {
244 return BOTAN_FFI_SUCCESS;
245 }
246
247 // This is the API introduced in 2.18
248 if(api_version == 20210220) {
249 return BOTAN_FFI_SUCCESS;
250 }
251
252 // This is the API introduced in 2.13
253 if(api_version == 20191214) {
254 return BOTAN_FFI_SUCCESS;
255 }
256
257 // This is the API introduced in 2.8
258 if(api_version == 20180713) {
259 return BOTAN_FFI_SUCCESS;
260 }
261
262 // This is the API introduced in 2.3
263 if(api_version == 20170815) {
264 return BOTAN_FFI_SUCCESS;
265 }
266
267 // This is the API introduced in 2.1
268 if(api_version == 20170327) {
269 return BOTAN_FFI_SUCCESS;
270 }
271
272 // This is the API introduced in 2.0
273 if(api_version == 20150515) {
274 return BOTAN_FFI_SUCCESS;
275 }
276
277 // Something else:
278 return -1;
279}
280
281const char* botan_version_string() {
282 return Botan::version_cstr();
283}
284
286 return Botan::version_major();
287}
288
290 return Botan::version_minor();
291}
292
294 return Botan::version_patch();
295}
296
299}
300
301int botan_constant_time_compare(const uint8_t* x, const uint8_t* y, size_t len) {
302 auto same = Botan::CT::is_equal(x, y, len);
303 // Return 0 if same or -1 otherwise
304 return static_cast<int>(same.select(1, 0)) - 1;
305}
306
307int botan_same_mem(const uint8_t* x, const uint8_t* y, size_t len) {
308 return botan_constant_time_compare(x, y, len);
309}
310
311int botan_scrub_mem(void* mem, size_t bytes) {
312 Botan::secure_scrub_memory(mem, bytes);
313 return BOTAN_FFI_SUCCESS;
314}
315
316int botan_hex_encode(const uint8_t* in, size_t len, char* out, uint32_t flags) {
317 return ffi_guard_thunk(__func__, [=]() -> int {
318 const bool uppercase = (flags & BOTAN_FFI_HEX_LOWER_CASE) == 0;
319 Botan::hex_encode(out, in, len, uppercase);
320 return BOTAN_FFI_SUCCESS;
321 });
322}
323
324int botan_hex_decode(const char* hex_str, size_t in_len, uint8_t* out, size_t* out_len) {
325 return ffi_guard_thunk(__func__, [=]() -> int {
326 const std::vector<uint8_t> bin = Botan::hex_decode(hex_str, in_len);
327 return Botan_FFI::write_vec_output(out, out_len, bin);
328 });
329}
330
331int botan_base64_encode(const uint8_t* in, size_t len, char* out, size_t* out_len) {
332 return ffi_guard_thunk(__func__, [=]() -> int {
333 const std::string base64 = Botan::base64_encode(in, len);
334 return Botan_FFI::write_str_output(out, out_len, base64);
335 });
336}
337
338int botan_base64_decode(const char* base64_str, size_t in_len, uint8_t* out, size_t* out_len) {
339 return ffi_guard_thunk(__func__, [=]() -> int {
340 if(*out_len < Botan::base64_decode_max_output(in_len)) {
341 *out_len = Botan::base64_decode_max_output(in_len);
343 }
344
345 *out_len = Botan::base64_decode(out, std::string(base64_str, in_len));
346 return BOTAN_FFI_SUCCESS;
347 });
348}
349}
uint32_t botan_version_datestamp()
Definition ffi.cpp:297
int botan_same_mem(const uint8_t *x, const uint8_t *y, size_t len)
Definition ffi.cpp:307
const char * botan_version_string()
Definition ffi.cpp:281
int botan_base64_decode(const char *base64_str, size_t in_len, uint8_t *out, size_t *out_len)
Definition ffi.cpp:338
uint32_t botan_version_patch()
Definition ffi.cpp:293
int botan_base64_encode(const uint8_t *in, size_t len, char *out, size_t *out_len)
Definition ffi.cpp:331
int botan_scrub_mem(void *mem, size_t bytes)
Definition ffi.cpp:311
int botan_hex_encode(const uint8_t *in, size_t len, char *out, uint32_t flags)
Definition ffi.cpp:316
uint32_t botan_version_major()
Definition ffi.cpp:285
uint32_t botan_ffi_api_version()
Definition ffi.cpp:217
int botan_ffi_supports_api(uint32_t api_version)
Definition ffi.cpp:221
const char * botan_error_description(int err)
Definition ffi.cpp:143
uint32_t botan_version_minor()
Definition ffi.cpp:289
int botan_constant_time_compare(const uint8_t *x, const uint8_t *y, size_t len)
Definition ffi.cpp:301
int botan_hex_decode(const char *hex_str, size_t in_len, uint8_t *out, size_t *out_len)
Definition ffi.cpp:324
const char * botan_error_last_exception_message()
Definition ffi.cpp:139
#define BOTAN_FFI_HEX_LOWER_CASE
Definition ffi.h:247
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:135
@ BOTAN_FFI_ERROR_KEY_NOT_SET
Definition ffi.h:134
@ BOTAN_FFI_ERROR_TLS_ERROR
Definition ffi.h:141
@ BOTAN_FFI_ERROR_EXCEPTION_THROWN
Definition ffi.h:126
@ BOTAN_FFI_ERROR_OUT_OF_MEMORY
Definition ffi.h:127
@ BOTAN_FFI_ERROR_INTERNAL_ERROR
Definition ffi.h:129
@ BOTAN_FFI_INVALID_VERIFIER
Definition ffi.h:117
@ 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:131
@ BOTAN_FFI_ERROR_INVALID_INPUT
Definition ffi.h:119
@ BOTAN_FFI_ERROR_STRING_CONVERSION_ERROR
Definition ffi.h:124
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:132
@ BOTAN_FFI_SUCCESS
Definition ffi.h:115
@ BOTAN_FFI_ERROR_SYSTEM_ERROR
Definition ffi.h:128
@ BOTAN_FFI_ERROR_ROUGHTIME_ERROR
Definition ffi.h:143
@ BOTAN_FFI_ERROR_NO_VALUE
Definition ffi.h:121
@ BOTAN_FFI_ERROR_INVALID_OBJECT_STATE
Definition ffi.h:136
@ BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE
Definition ffi.h:123
@ BOTAN_FFI_ERROR_BAD_MAC
Definition ffi.h:120
@ BOTAN_FFI_ERROR_BAD_PARAMETER
Definition ffi.h:133
#define BOTAN_HAS_FFI
Definition build.h:196
constexpr CT::Mask< T > is_equal(const T x[], const T y[], size_t len)
Definition ct_utils.h:826
bool read_env_variable(std::string &value_out, std::string_view var_name)
Definition os_utils.cpp:445
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:112
int botan_view_str_bounce_fn(botan_view_ctx vctx, const char *str, size_t len)
Definition ffi.cpp:108
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:247
int write_str_output(char out[], size_t *out_len, const std::string &str)
Definition ffi_util.h:251
uint32_t version_minor()
Definition version.cpp:59
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:145
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
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
ErrorType
Definition exceptn.h:20
constexpr void clear_mem(T *ptr, size_t n)
Definition mem_ops.h:119