• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 20751057581

06 Jan 2026 02:18PM UTC coverage: 90.433% (+0.009%) from 90.424%
20751057581

Pull #5188

github

web-flow
Merge 8cc4c6141 into f536862f0
Pull Request #5188: FFI: Generic getter(s) for X.509 objects

101763 of 112529 relevant lines covered (90.43%)

12998968.06 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.94
/src/lib/ffi/ffi.cpp
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

22
namespace Botan_FFI {
23

24
namespace {
25

26
// NOLINTNEXTLINE(*-avoid-non-const-global-variables)
27
thread_local std::string g_last_exception_what;
97,134✔
28

29
int ffi_map_error_type(Botan::ErrorType err) {
11✔
30
   switch(err) {
11✔
31
      case Botan::ErrorType::Unknown:
32
         return BOTAN_FFI_ERROR_UNKNOWN_ERROR;
33

34
      case Botan::ErrorType::SystemError:
×
35
      case Botan::ErrorType::IoError:
×
36
      case Botan::ErrorType::Pkcs11Error:
×
37
      case Botan::ErrorType::CommonCryptoError:
×
38
      case Botan::ErrorType::ZlibError:
×
39
      case Botan::ErrorType::Bzip2Error:
×
40
      case Botan::ErrorType::LzmaError:
×
41
      case Botan::ErrorType::DatabaseError:
×
42
         return BOTAN_FFI_ERROR_SYSTEM_ERROR;
×
43

44
      case Botan::ErrorType::TPMError:
×
45
         return BOTAN_FFI_ERROR_TPM_ERROR;
×
46

47
      case Botan::ErrorType::NotImplemented:
48
         return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
49
      case Botan::ErrorType::OutOfMemory:
×
50
         return BOTAN_FFI_ERROR_OUT_OF_MEMORY;
×
51
      case Botan::ErrorType::InternalError:
×
52
         return BOTAN_FFI_ERROR_INTERNAL_ERROR;
×
53
      case Botan::ErrorType::InvalidObjectState:
2✔
54
         return BOTAN_FFI_ERROR_INVALID_OBJECT_STATE;
2✔
55
      case Botan::ErrorType::KeyNotSet:
2✔
56
         return BOTAN_FFI_ERROR_KEY_NOT_SET;
2✔
57
      case Botan::ErrorType::InvalidArgument:
4✔
58
      case Botan::ErrorType::InvalidNonceLength:
4✔
59
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
4✔
60

61
      case Botan::ErrorType::EncodingFailure:
1✔
62
      case Botan::ErrorType::DecodingFailure:
1✔
63
         return BOTAN_FFI_ERROR_INVALID_INPUT;
1✔
64

65
      case Botan::ErrorType::InvalidTag:
×
66
         return BOTAN_FFI_ERROR_BAD_MAC;
×
67

68
      case Botan::ErrorType::InvalidKeyLength:
×
69
         return BOTAN_FFI_ERROR_INVALID_KEY_LENGTH;
×
70
      case Botan::ErrorType::LookupError:
71
         return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
72

73
      case Botan::ErrorType::HttpError:
×
74
         return BOTAN_FFI_ERROR_HTTP_ERROR;
×
75
      case Botan::ErrorType::TLSError:
×
76
         return BOTAN_FFI_ERROR_TLS_ERROR;
×
77
      case Botan::ErrorType::RoughtimeError:
×
78
         return BOTAN_FFI_ERROR_ROUGHTIME_ERROR;
×
79
   }
80

81
   return BOTAN_FFI_ERROR_UNKNOWN_ERROR;
82
}
83

84
}  // namespace
85

86
void ffi_clear_last_exception() {
97,112✔
87
   g_last_exception_what.clear();
97,112✔
88
}
97,103✔
89

90
int ffi_error_exception_thrown(const char* func_name, const char* exn, int rc) {
17✔
91
   g_last_exception_what.assign(exn);
17✔
92

93
#if defined(BOTAN_HAS_OS_UTILS)
94
   std::string val;
17✔
95
   if(Botan::OS::read_env_variable(val, "BOTAN_FFI_PRINT_EXCEPTIONS") && !val.empty()) {
17✔
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;
17✔
102
}
17✔
103

104
int ffi_error_exception_thrown(const char* func_name, const char* exn, Botan::ErrorType err) {
11✔
105
   return ffi_error_exception_thrown(func_name, exn, ffi_map_error_type(err));
11✔
106
}
107

108
int botan_view_str_bounce_fn(botan_view_ctx vctx, const char* str, size_t len) {
74✔
109
   return botan_view_bin_bounce_fn(vctx, reinterpret_cast<const uint8_t*>(str), len);
74✔
110
}
111

112
int botan_view_bin_bounce_fn(botan_view_ctx vctx, const uint8_t* buf, size_t len) {
168✔
113
   if(vctx == nullptr || buf == nullptr) {
168✔
114
      return BOTAN_FFI_ERROR_NULL_POINTER;
115
   }
116

117
   const botan_view_bounce_struct* ctx = static_cast<botan_view_bounce_struct*>(vctx);
168✔
118

119
   const size_t avail = *ctx->out_len;
168✔
120
   *ctx->out_len = len;
168✔
121

122
   if(avail < len || ctx->out_ptr == nullptr) {
168✔
123
      if(ctx->out_ptr != nullptr) {
78✔
124
         Botan::clear_mem(ctx->out_ptr, avail);
×
125
      }
126
      return BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE;
78✔
127
   } else {
128
      Botan::copy_mem(ctx->out_ptr, buf, len);
90✔
129
      return BOTAN_FFI_SUCCESS;
90✔
130
   }
131
}
132

133
}  // namespace Botan_FFI
134

135
extern "C" {
136

137
using namespace Botan_FFI;
138

139
const char* botan_error_last_exception_message() {
5✔
140
   return g_last_exception_what.c_str();
5✔
141
}
142

143
const char* botan_error_description(int err) {
155✔
144
   switch(err) {
155✔
145
      case BOTAN_FFI_SUCCESS:
146
         return "OK";
147

148
      case BOTAN_FFI_INVALID_VERIFIER:
1✔
149
         return "Invalid verifier";
1✔
150

151
      case BOTAN_FFI_ERROR_INVALID_INPUT:
2✔
152
         return "Invalid input";
2✔
153

154
      case BOTAN_FFI_ERROR_BAD_MAC:
1✔
155
         return "Invalid authentication code";
1✔
156

157
      case BOTAN_FFI_ERROR_NO_VALUE:
2✔
158
         return "No value available";
2✔
159

160
      case BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE:
1✔
161
         return "Insufficient buffer space";
1✔
162

163
      case BOTAN_FFI_ERROR_STRING_CONVERSION_ERROR:
1✔
164
         return "String conversion error";
1✔
165

166
      case BOTAN_FFI_ERROR_EXCEPTION_THROWN:
1✔
167
         return "Exception thrown";
1✔
168

169
      case BOTAN_FFI_ERROR_OUT_OF_MEMORY:
1✔
170
         return "Out of memory";
1✔
171

172
      case BOTAN_FFI_ERROR_SYSTEM_ERROR:
1✔
173
         return "Error while calling system API";
1✔
174

175
      case BOTAN_FFI_ERROR_INTERNAL_ERROR:
1✔
176
         return "Internal error";
1✔
177

178
      case BOTAN_FFI_ERROR_BAD_FLAG:
1✔
179
         return "Bad flag";
1✔
180

181
      case BOTAN_FFI_ERROR_NULL_POINTER:
1✔
182
         return "Null pointer argument";
1✔
183

184
      case BOTAN_FFI_ERROR_BAD_PARAMETER:
1✔
185
         return "Bad parameter";
1✔
186

187
      case BOTAN_FFI_ERROR_KEY_NOT_SET:
1✔
188
         return "Key not set on object";
1✔
189

190
      case BOTAN_FFI_ERROR_INVALID_KEY_LENGTH:
1✔
191
         return "Invalid key length";
1✔
192

193
      case BOTAN_FFI_ERROR_INVALID_OBJECT_STATE:
1✔
194
         return "Invalid object state";
1✔
195

196
      case BOTAN_FFI_ERROR_OUT_OF_RANGE:
1✔
197
         return "Index out of range";
1✔
198

199
      case BOTAN_FFI_ERROR_NOT_IMPLEMENTED:
4✔
200
         return "Not implemented";
4✔
201

202
      case BOTAN_FFI_ERROR_INVALID_OBJECT:
1✔
203
         return "Invalid object handle";
1✔
204

205
      case BOTAN_FFI_ERROR_TLS_ERROR:
1✔
206
         return "TLS error";
1✔
207

208
      case BOTAN_FFI_ERROR_HTTP_ERROR:
1✔
209
         return "HTTP error";
1✔
210

211
      case BOTAN_FFI_ERROR_UNKNOWN_ERROR:
128✔
212
      default:
128✔
213
         return "Unknown error";
128✔
214
   }
215
}
216

217
/*
218
* Versioning
219
*/
220
uint32_t botan_ffi_api_version() {
4✔
221
   return BOTAN_HAS_FFI;
4✔
222
}
223

224
int botan_ffi_supports_api(uint32_t api_version) {
7✔
225
   // This is the API introduced in 3.11
226
   if(api_version == 20260203) {
7✔
227
      return BOTAN_FFI_SUCCESS;
228
   }
229

230
   // This is the API introduced in 3.10
231
   if(api_version == 20250829) {
6✔
232
      return BOTAN_FFI_SUCCESS;
233
   }
234

235
   // This is the API introduced in 3.8
236
   if(api_version == 20250506) {
5✔
237
      return BOTAN_FFI_SUCCESS;
238
   }
239

240
   // This is the API introduced in 3.4
241
   if(api_version == 20240408) {
5✔
242
      return BOTAN_FFI_SUCCESS;
243
   }
244

245
   // This is the API introduced in 3.2
246
   if(api_version == 20231009) {
5✔
247
      return BOTAN_FFI_SUCCESS;
248
   }
249

250
   // This is the API introduced in 3.1
251
   if(api_version == 20230711) {
5✔
252
      return BOTAN_FFI_SUCCESS;
253
   }
254

255
   // This is the API introduced in 3.0
256
   if(api_version == 20230403) {
5✔
257
      return BOTAN_FFI_SUCCESS;
258
   }
259

260
   // This is the API introduced in 2.18
261
   if(api_version == 20210220) {
5✔
262
      return BOTAN_FFI_SUCCESS;
263
   }
264

265
   // This is the API introduced in 2.13
266
   if(api_version == 20191214) {
5✔
267
      return BOTAN_FFI_SUCCESS;
268
   }
269

270
   // This is the API introduced in 2.8
271
   if(api_version == 20180713) {
5✔
272
      return BOTAN_FFI_SUCCESS;
273
   }
274

275
   // This is the API introduced in 2.3
276
   if(api_version == 20170815) {
4✔
277
      return BOTAN_FFI_SUCCESS;
278
   }
279

280
   // This is the API introduced in 2.1
281
   if(api_version == 20170327) {
3✔
282
      return BOTAN_FFI_SUCCESS;
283
   }
284

285
   // This is the API introduced in 2.0
286
   if(api_version == 20150515) {
2✔
287
      return BOTAN_FFI_SUCCESS;
1✔
288
   }
289

290
   // Something else:
291
   return -1;
292
}
293

294
const char* botan_version_string() {
2✔
295
   return Botan::version_cstr();
2✔
296
}
297

298
uint32_t botan_version_major() {
2✔
299
   return Botan::version_major();
2✔
300
}
301

302
uint32_t botan_version_minor() {
2✔
303
   return Botan::version_minor();
2✔
304
}
305

306
uint32_t botan_version_patch() {
1✔
307
   return Botan::version_patch();
1✔
308
}
309

310
uint32_t botan_version_datestamp() {
1✔
311
   return Botan::version_datestamp();
1✔
312
}
313

314
int botan_constant_time_compare(const uint8_t* x, const uint8_t* y, size_t len) {
28✔
315
   auto same = Botan::CT::is_equal(x, y, len);
28✔
316
   // Return 0 if same or -1 otherwise
317
   return static_cast<int>(same.select(1, 0)) - 1;
28✔
318
}
319

320
int botan_same_mem(const uint8_t* x, const uint8_t* y, size_t len) {
×
321
   return botan_constant_time_compare(x, y, len);
×
322
}
323

324
int botan_scrub_mem(void* mem, size_t bytes) {
1✔
325
   Botan::secure_scrub_memory(mem, bytes);
1✔
326
   return BOTAN_FFI_SUCCESS;
1✔
327
}
328

329
int botan_hex_encode(const uint8_t* in, size_t len, char* out, uint32_t flags) {
3✔
330
   return ffi_guard_thunk(__func__, [=]() -> int {
3✔
331
      const bool uppercase = (flags & BOTAN_FFI_HEX_LOWER_CASE) == 0;
3✔
332
      Botan::hex_encode(out, in, len, uppercase);
3✔
333
      return BOTAN_FFI_SUCCESS;
334
   });
3✔
335
}
336

337
int botan_hex_decode(const char* hex_str, size_t in_len, uint8_t* out, size_t* out_len) {
2✔
338
   return ffi_guard_thunk(__func__, [=]() -> int {
2✔
339
      const std::vector<uint8_t> bin = Botan::hex_decode(hex_str, in_len);
2✔
340
      return Botan_FFI::write_vec_output(out, out_len, bin);
2✔
341
   });
2✔
342
}
343

344
int botan_base64_encode(const uint8_t* in, size_t len, char* out, size_t* out_len) {
2✔
345
   return ffi_guard_thunk(__func__, [=]() -> int {
2✔
346
      const std::string base64 = Botan::base64_encode(in, len);
2✔
347
      return Botan_FFI::write_str_output(out, out_len, base64);
2✔
348
   });
2✔
349
}
350

351
int botan_base64_decode(const char* base64_str, size_t in_len, uint8_t* out, size_t* out_len) {
2✔
352
   return ffi_guard_thunk(__func__, [=]() -> int {
2✔
353
      if(*out_len < Botan::base64_decode_max_output(in_len)) {
2✔
354
         *out_len = Botan::base64_decode_max_output(in_len);
1✔
355
         return BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE;
1✔
356
      }
357

358
      *out_len = Botan::base64_decode(out, std::string(base64_str, in_len));
1✔
359
      return BOTAN_FFI_SUCCESS;
1✔
360
   });
2✔
361
}
362
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc