Botan  2.10.0
Crypto and TLS for C++11
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 #include <botan/internal/ffi_util.h>
9 #include <botan/internal/os_utils.h>
10 #include <botan/version.h>
11 #include <botan/mem_ops.h>
12 #include <botan/hex.h>
13 #include <botan/base64.h>
14 #include <cstdio>
15 #include <cstdlib>
16 
17 namespace Botan_FFI {
18 
19 int ffi_error_exception_thrown(const char* func_name, const char* exn, int rc)
20  {
21  if(Botan::OS::read_env_variable("BOTAN_FFI_PRINT_EXCEPTIONS") != nullptr)
22  {
23  std::fprintf(stderr, "in %s exception '%s' returning %d\n", func_name, exn, rc);
24  }
25  return rc;
26  }
27 
28 namespace {
29 
30 int ffi_map_error_type(Botan::ErrorType err)
31  {
32  switch(err)
33  {
46 
60 
64 
67 
72 
77  }
78 
80  }
81 
82 }
83 
84 int ffi_guard_thunk(const char* func_name, std::function<int ()> thunk)
85  {
86  try
87  {
88  return thunk();
89  }
90  catch(std::bad_alloc&)
91  {
92  return ffi_error_exception_thrown(func_name, "bad_alloc", BOTAN_FFI_ERROR_OUT_OF_MEMORY);
93  }
94  catch(Botan_FFI::FFI_Error& e)
95  {
96  return ffi_error_exception_thrown(func_name, e.what(), e.error_code());
97  }
98  catch(Botan::Exception& e)
99  {
100  return ffi_error_exception_thrown(func_name, e.what(), ffi_map_error_type(e.error_type()));
101  }
102  catch(std::exception& e)
103  {
104  return ffi_error_exception_thrown(func_name, e.what());
105  }
106  catch(...)
107  {
108  return ffi_error_exception_thrown(func_name, "unknown exception");
109  }
110 
112  }
113 
114 }
115 
116 extern "C" {
117 
118 using namespace Botan_FFI;
119 
120 const char* botan_error_description(int err)
121  {
122  switch(err)
123  {
124  case BOTAN_FFI_SUCCESS:
125  return "OK";
126 
128  return "Invalid verifier";
129 
131  return "Invalid input";
132 
134  return "Invalid authentication code";
135 
137  return "Insufficient buffer space";
138 
140  return "Exception thrown";
141 
143  return "Out of memory";
144 
146  return "Error while calling system API";
147 
149  return "Internal error";
150 
152  return "Bad flag";
153 
155  return "Null pointer argument";
156 
158  return "Bad parameter";
159 
161  return "Key not set on object";
162 
164  return "Invalid key length";
165 
167  return "Invalid object state";
168 
170  return "Not implemented";
171 
173  return "Invalid object handle";
174 
176  return "TLS error";
177 
179  return "HTTP error";
180 
182  return "Unknown error";
183  }
184 
185  return "Unknown error";
186  }
187 
188 /*
189 * Versioning
190 */
192  {
193  return BOTAN_HAS_FFI;
194  }
195 
196 int botan_ffi_supports_api(uint32_t api_version)
197  {
198  // This is the API introduced in 2.8
199  if(api_version == 20180713)
200  return BOTAN_FFI_SUCCESS;
201 
202  // This is the API introduced in 2.3
203  if(api_version == 20170815)
204  return BOTAN_FFI_SUCCESS;
205 
206  // This is the API introduced in 2.1
207  if(api_version == 20170327)
208  return BOTAN_FFI_SUCCESS;
209 
210  // This is the API introduced in 2.0
211  if(api_version == 20150515)
212  return BOTAN_FFI_SUCCESS;
213 
214  // Something else:
215  return -1;
216  }
217 
218 const char* botan_version_string()
219  {
220  return Botan::version_cstr();
221  }
222 
227 
228 int botan_constant_time_compare(const uint8_t* x, const uint8_t* y, size_t len)
229  {
230  return Botan::constant_time_compare(x, y, len) ? 0 : -1;
231  }
232 
233 int botan_same_mem(const uint8_t* x, const uint8_t* y, size_t len)
234  {
235  return botan_constant_time_compare(x, y, len);
236  }
237 
238 int botan_scrub_mem(void* mem, size_t bytes)
239  {
240  Botan::secure_scrub_memory(mem, bytes);
241  return BOTAN_FFI_SUCCESS;
242  }
243 
244 int botan_hex_encode(const uint8_t* in, size_t len, char* out, uint32_t flags)
245  {
246  return ffi_guard_thunk(__func__, [=]() -> int {
247  const bool uppercase = (flags & BOTAN_FFI_HEX_LOWER_CASE) == 0;
248  Botan::hex_encode(out, in, len, uppercase);
249  return BOTAN_FFI_SUCCESS;
250  });
251  }
252 
253 int botan_hex_decode(const char* hex_str, size_t in_len, uint8_t* out, size_t* out_len)
254  {
255  return ffi_guard_thunk(__func__, [=]() -> int {
256  const std::vector<uint8_t> bin = Botan::hex_decode(hex_str, in_len);
257  return Botan_FFI::write_vec_output(out, out_len, bin);
258  });
259  }
260 
261 int botan_base64_encode(const uint8_t* in, size_t len, char* out, size_t* out_len)
262  {
263  return ffi_guard_thunk(__func__, [=]() -> int {
264  const std::string base64 = Botan::base64_encode(in, len);
265  return Botan_FFI::write_str_output(out, out_len, base64);
266  });
267  }
268 
269 int botan_base64_decode(const char* base64_str, size_t in_len,
270  uint8_t* out, size_t* out_len)
271  {
272  return ffi_guard_thunk(__func__, [=]() -> int {
273  if(*out_len < Botan::base64_decode_max_output(in_len))
274  {
275  *out_len = Botan::base64_decode_max_output(in_len);
277  }
278 
279  *out_len = Botan::base64_decode(out, std::string(base64_str, in_len));
280  return BOTAN_FFI_SUCCESS;
281  });
282  }
283 
284 }
int ffi_guard_thunk(const char *func_name, std::function< int()> thunk)
Definition: ffi.cpp:84
void hex_encode(char output[], const uint8_t input[], size_t input_length, bool uppercase)
Definition: hex.cpp:14
int botan_hex_encode(const uint8_t *in, size_t len, char *out, uint32_t flags)
Definition: ffi.cpp:244
int ffi_error_exception_thrown(const char *func_name, const char *exn, int rc)
Definition: ffi.cpp:19
size_t base64_decode_max_output(size_t input_length)
Definition: base64.cpp:224
virtual ErrorType error_type() const noexcept
Definition: exceptn.h:94
Flags flags(Flag flags)
Definition: p11.h:858
bool constant_time_compare(const uint8_t x[], const uint8_t y[], size_t len)
Definition: mem_ops.h:81
int write_vec_output(uint8_t out[], size_t *out_len, const std::vector< uint8_t, Alloc > &buf)
Definition: ffi_util.h:142
#define BOTAN_FFI_HEX_LOWER_CASE
Definition: ffi.h:152
int error_code() const noexcept override
Definition: ffi_util.h:27
const char * what() const noexcept override
Definition: exceptn.h:89
int botan_base64_encode(const uint8_t *in, size_t len, char *out, size_t *out_len)
Definition: ffi.cpp:261
uint32_t botan_version_patch()
Definition: ffi.cpp:225
int botan_scrub_mem(void *mem, size_t bytes)
Definition: ffi.cpp:238
uint32_t version_major()
Definition: version.cpp:72
int write_str_output(uint8_t out[], size_t *out_len, const std::string &str)
Definition: ffi_util.h:147
size_t base64_encode(char out[], const uint8_t in[], size_t input_length, size_t &input_consumed, bool final_inputs)
Definition: base64.cpp:166
uint32_t botan_ffi_api_version()
Definition: ffi.cpp:191
uint32_t version_patch()
Definition: version.cpp:74
ErrorType
Definition: exceptn.h:20
uint32_t botan_version_major()
Definition: ffi.cpp:223
int botan_same_mem(const uint8_t *x, const uint8_t *y, size_t len)
Definition: ffi.cpp:233
uint32_t version_minor()
Definition: version.cpp:73
size_t hex_decode(uint8_t output[], const char input[], size_t input_length, size_t &input_consumed, bool ignore_ws)
Definition: hex.cpp:49
const char * botan_error_description(int err)
Definition: ffi.cpp:120
uint32_t botan_version_minor()
Definition: ffi.cpp:224
uint32_t version_datestamp()
Definition: version.cpp:67
int botan_base64_decode(const char *base64_str, size_t in_len, uint8_t *out, size_t *out_len)
Definition: ffi.cpp:269
void secure_scrub_memory(void *ptr, size_t n)
Definition: os_utils.cpp:51
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:181
uint32_t botan_version_datestamp()
Definition: ffi.cpp:226
int botan_hex_decode(const char *hex_str, size_t in_len, uint8_t *out, size_t *out_len)
Definition: ffi.cpp:253
int botan_ffi_supports_api(uint32_t api_version)
Definition: ffi.cpp:196
int botan_constant_time_compare(const uint8_t *x, const uint8_t *y, size_t len)
Definition: ffi.cpp:228
const char * version_cstr()
Definition: version.cpp:29
const char * read_env_variable(const std::string &var_name)
Definition: os_utils.cpp:311
const char * botan_version_string()
Definition: ffi.cpp:218