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

randombit / botan / 25147314492

30 Apr 2026 04:17AM UTC coverage: 89.359% (-0.01%) from 89.37%
25147314492

push

github

randombit
Avoid using BOTAN_ASSERT for input validation

107055 of 119803 relevant lines covered (89.36%)

11298138.36 hits per line

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

93.06
/src/lib/ffi/ffi_pkey.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/assert.h>
10
#include <botan/data_src.h>
11
#include <botan/hash.h>
12
#include <botan/pk_algs.h>
13
#include <botan/pk_keys.h>
14
#include <botan/pkcs8.h>
15
#include <botan/x509_key.h>
16
#include <botan/internal/ffi_ec.h>
17
#include <botan/internal/ffi_oid.h>
18
#include <botan/internal/ffi_pkey.h>
19
#include <botan/internal/ffi_rng.h>
20
#include <botan/internal/ffi_util.h>
21

22
#if defined(BOTAN_HAS_HASH_ID)
23
   #include <botan/internal/hash_id.h>
24
#endif
25

26
extern "C" {
27

28
using namespace Botan_FFI;
29

30
int botan_privkey_create(botan_privkey_t* key_obj,
86✔
31
                         const char* algo_name,
32
                         const char* algo_params,
33
                         botan_rng_t rng_obj) {
34
   // TODO(Botan4) remove this implicit algorithm choice and reject nullptr algo_name
35
   if(algo_name == nullptr) {
86✔
36
      return botan_privkey_create(key_obj, "RSA", algo_params, rng_obj);
37
   }
38

39
   return ffi_guard_thunk(__func__, [=]() -> int {
86✔
40
      if(key_obj == nullptr) {
86✔
41
         return BOTAN_FFI_ERROR_NULL_POINTER;
42
      }
43

44
      *key_obj = nullptr;
86✔
45
      if(rng_obj == nullptr) {
86✔
46
         return BOTAN_FFI_ERROR_NULL_POINTER;
47
      }
48

49
      const std::string params(algo_params != nullptr ? algo_params : "");
172✔
50

51
      Botan::RandomNumberGenerator& rng = safe_get(rng_obj);
86✔
52

53
      if(auto key = Botan::create_private_key(algo_name, rng, params)) {
86✔
54
         return ffi_new_object(key_obj, std::move(key));
172✔
55
      } else {
56
         return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
57
      }
86✔
58
   });
86✔
59
}
60

61
int botan_ec_privkey_create(botan_privkey_t* key_obj,
3✔
62
                            const char* algo_name,
63
                            botan_ec_group_t ec_group_obj,
64
                            botan_rng_t rng_obj) {
65
   // TODO(Botan4) remove this implicit algorithm choice and reject nullptr algo_name
66
   if(algo_name == nullptr) {
3✔
67
      return botan_ec_privkey_create(key_obj, "ECDSA", ec_group_obj, rng_obj);
68
   }
69

70
   return ffi_guard_thunk(__func__, [=]() -> int {
3✔
71
      if(key_obj == nullptr) {
3✔
72
         return BOTAN_FFI_ERROR_NULL_POINTER;
73
      }
74
      *key_obj = nullptr;
3✔
75

76
      const Botan::EC_Group ec_group = safe_get(ec_group_obj);
3✔
77
      Botan::RandomNumberGenerator& rng = safe_get(rng_obj);
3✔
78

79
      if(auto key = Botan::create_ec_private_key(algo_name, ec_group, rng)) {
3✔
80
         return ffi_new_object(key_obj, std::move(key));
6✔
81
      } else {
82
         return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
83
      }
×
84
   });
3✔
85
}
86

87
int botan_privkey_load(
59✔
88
   botan_privkey_t* key, botan_rng_t rng_obj, const uint8_t bits[], size_t len, const char* password) {
89
   BOTAN_UNUSED(rng_obj);
59✔
90

91
   if(key == nullptr) {
59✔
92
      return BOTAN_FFI_ERROR_NULL_POINTER;
93
   }
94

95
   *key = nullptr;
59✔
96

97
   if(bits == nullptr && len > 0) {
59✔
98
      return BOTAN_FFI_ERROR_NULL_POINTER;
99
   }
100

101
   return ffi_guard_thunk(__func__, [=]() -> int {
59✔
102
      Botan::DataSource_Memory src(bits, len);
59✔
103

104
      std::unique_ptr<Botan::Private_Key> pkcs8;
59✔
105

106
      if(password == nullptr) {
59✔
107
         pkcs8 = Botan::PKCS8::load_key(src);
29✔
108
      } else {
109
         pkcs8 = Botan::PKCS8::load_key(src, std::string(password));
30✔
110
      }
111

112
      if(pkcs8) {
59✔
113
         ffi_new_object(key, std::move(pkcs8));
59✔
114
         return BOTAN_FFI_SUCCESS;
59✔
115
      }
116
      return BOTAN_FFI_ERROR_UNKNOWN_ERROR;
117
   });
177✔
118
}
119

120
int botan_privkey_destroy(botan_privkey_t key) {
225✔
121
   return BOTAN_FFI_CHECKED_DELETE(key);
225✔
122
}
123

124
int botan_pubkey_load(botan_pubkey_t* key, const uint8_t bits[], size_t bits_len) {
15✔
125
   if(key == nullptr) {
15✔
126
      return BOTAN_FFI_ERROR_NULL_POINTER;
127
   }
128

129
   *key = nullptr;
15✔
130

131
   if(bits == nullptr && bits_len > 0) {
15✔
132
      return BOTAN_FFI_ERROR_NULL_POINTER;
133
   }
134

135
   return ffi_guard_thunk(__func__, [=]() -> int {
15✔
136
      Botan::DataSource_Memory src(bits, bits_len);
15✔
137
      std::unique_ptr<Botan::Public_Key> pubkey(Botan::X509::load_key(src));
15✔
138

139
      if(pubkey == nullptr) {
15✔
140
         return BOTAN_FFI_ERROR_UNKNOWN_ERROR;
141
      }
142

143
      ffi_new_object(key, std::move(pubkey));
30✔
144
      return BOTAN_FFI_SUCCESS;
145
   });
45✔
146
}
147

148
int botan_pubkey_destroy(botan_pubkey_t key) {
212✔
149
   return BOTAN_FFI_CHECKED_DELETE(key);
212✔
150
}
151

152
int botan_privkey_export_pubkey(botan_pubkey_t* pubout, botan_privkey_t key_obj) {
107✔
153
   if(pubout == nullptr) {
107✔
154
      return BOTAN_FFI_ERROR_NULL_POINTER;
155
   }
156
   return ffi_guard_thunk(__func__, [=]() -> int {
107✔
157
      auto public_key = safe_get(key_obj).public_key();
107✔
158
      return ffi_new_object(pubout, std::move(public_key));
214✔
159
   });
214✔
160
}
161

162
int botan_privkey_algo_name(botan_privkey_t key, char out[], size_t* out_len) {
4✔
163
   return BOTAN_FFI_VISIT(key, [=](const auto& k) { return write_str_output(out, out_len, k.algo_name()); });
8✔
164
}
165

166
int botan_pubkey_algo_name(botan_pubkey_t key, char out[], size_t* out_len) {
7✔
167
   return BOTAN_FFI_VISIT(key, [=](const auto& k) { return write_str_output(out, out_len, k.algo_name()); });
14✔
168
}
169

170
int botan_pubkey_check_key(botan_pubkey_t key, botan_rng_t rng, uint32_t flags) {
30✔
171
   const bool strong = (flags & BOTAN_CHECK_KEY_EXPENSIVE_TESTS) != 0;
30✔
172

173
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
60✔
174
      return (k.check_key(safe_get(rng), strong) == true) ? 0 : BOTAN_FFI_ERROR_INVALID_INPUT;
175
   });
176
}
177

178
int botan_privkey_check_key(botan_privkey_t key, botan_rng_t rng, uint32_t flags) {
15✔
179
   const bool strong = (flags & BOTAN_CHECK_KEY_EXPENSIVE_TESTS) != 0;
15✔
180
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
30✔
181
      return (k.check_key(safe_get(rng), strong) == true) ? 0 : BOTAN_FFI_ERROR_INVALID_INPUT;
182
   });
183
}
184

185
int botan_pubkey_export(botan_pubkey_t key, uint8_t out[], size_t* out_len, uint32_t flags) {
48✔
186
   if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_DER) {
48✔
187
      return copy_view_bin(out, out_len, botan_pubkey_view_der, key);
24✔
188
   } else if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_PEM) {
24✔
189
      return copy_view_str(out, out_len, botan_pubkey_view_pem, key);
24✔
190
   } else if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_RAW) {
×
191
      return copy_view_bin(out, out_len, botan_pubkey_view_raw, key);
×
192
   } else {
193
      return BOTAN_FFI_ERROR_BAD_FLAG;
194
   }
195
}
196

197
int botan_pubkey_view_der(botan_pubkey_t key, botan_view_ctx ctx, botan_view_bin_fn view) {
25✔
198
   return BOTAN_FFI_VISIT(
100✔
199
      key, [=](const auto& k) -> int { return invoke_view_callback(view, ctx, k.subject_public_key()); });
200
}
201

202
int botan_pubkey_view_pem(botan_pubkey_t key, botan_view_ctx ctx, botan_view_str_fn view) {
28✔
203
   return BOTAN_FFI_VISIT(
84✔
204
      key, [=](const auto& k) -> int { return invoke_view_callback(view, ctx, Botan::X509::PEM_encode(k)); });
205
}
206

207
int botan_pubkey_view_raw(botan_pubkey_t key, botan_view_ctx ctx, botan_view_bin_fn view) {
131✔
208
   return BOTAN_FFI_VISIT(
522✔
209
      key, [=](const auto& k) -> int { return invoke_view_callback(view, ctx, k.raw_public_key_bits()); });
210
}
211

212
int botan_privkey_export(botan_privkey_t key, uint8_t out[], size_t* out_len, uint32_t flags) {
48✔
213
   if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_DER) {
48✔
214
      return copy_view_bin(out, out_len, botan_privkey_view_der, key);
24✔
215
   } else if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_PEM) {
24✔
216
      return copy_view_str(out, out_len, botan_privkey_view_pem, key);
24✔
217
   } else if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_RAW) {
×
218
      return copy_view_bin(out, out_len, botan_privkey_view_raw, key);
×
219
   } else {
220
      return BOTAN_FFI_ERROR_BAD_FLAG;
221
   }
222
}
223

224
int botan_privkey_view_der(botan_privkey_t key, botan_view_ctx ctx, botan_view_bin_fn view) {
30✔
225
   return BOTAN_FFI_VISIT(key,
120✔
226
                          [=](const auto& k) -> int { return invoke_view_callback(view, ctx, k.private_key_info()); });
227
}
228

229
int botan_privkey_view_pem(botan_privkey_t key, botan_view_ctx ctx, botan_view_str_fn view) {
39✔
230
   return BOTAN_FFI_VISIT(
117✔
231
      key, [=](const auto& k) -> int { return invoke_view_callback(view, ctx, Botan::PKCS8::PEM_encode(k)); });
232
}
233

234
int botan_privkey_view_raw(botan_privkey_t key, botan_view_ctx ctx, botan_view_bin_fn view) {
110✔
235
   return BOTAN_FFI_VISIT(
438✔
236
      key, [=](const auto& k) -> int { return invoke_view_callback(view, ctx, k.raw_private_key_bits()); });
237
}
238

239
int botan_privkey_export_encrypted(botan_privkey_t key,
×
240
                                   uint8_t out[],
241
                                   size_t* out_len,
242
                                   botan_rng_t rng_obj,
243
                                   const char* pass,
244
                                   const char* /*ignored - pbe*/,
245
                                   uint32_t flags) {
246
   return botan_privkey_export_encrypted_pbkdf_iter(key, out, out_len, rng_obj, pass, 100000, nullptr, nullptr, flags);
×
247
}
248

249
int botan_privkey_export_encrypted_pbkdf_msec(botan_privkey_t key,
12✔
250
                                              uint8_t out[],
251
                                              size_t* out_len,
252
                                              botan_rng_t rng,
253
                                              const char* passphrase,
254
                                              uint32_t pbkdf_msec,
255
                                              size_t* pbkdf_iters_out,
256
                                              const char* cipher,
257
                                              const char* pbkdf_hash,
258
                                              uint32_t flags) {
259
   if(pbkdf_iters_out != nullptr) {
12✔
260
      *pbkdf_iters_out = 0;
12✔
261
   }
262

263
   if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_DER) {
12✔
264
      return copy_view_bin(
12✔
265
         out, out_len, botan_privkey_view_encrypted_der_timed, key, rng, passphrase, cipher, pbkdf_hash, pbkdf_msec);
12✔
266
   } else if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_PEM) {
×
267
      return copy_view_str(
×
268
         out, out_len, botan_privkey_view_encrypted_pem_timed, key, rng, passphrase, cipher, pbkdf_hash, pbkdf_msec);
×
269
   } else {
270
      return BOTAN_FFI_ERROR_BAD_FLAG;
271
   }
272
}
273

274
int botan_privkey_view_encrypted_der_timed(botan_privkey_t key,
15✔
275
                                           botan_rng_t rng_obj,
276
                                           const char* passphrase,
277
                                           const char* maybe_cipher,
278
                                           const char* maybe_pbkdf_algo,
279
                                           size_t pbkdf_runtime_msec,
280
                                           botan_view_ctx ctx,
281
                                           botan_view_bin_fn view) {
282
   if(passphrase == nullptr) {
15✔
283
      return BOTAN_FFI_ERROR_NULL_POINTER;
284
   }
285

286
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
102✔
287
      const std::chrono::milliseconds pbkdf_time(pbkdf_runtime_msec);
288
      Botan::RandomNumberGenerator& rng = safe_get(rng_obj);
289

290
      const std::string cipher = (maybe_cipher ? maybe_cipher : "");
291
      const std::string pbkdf_algo = (maybe_pbkdf_algo ? maybe_pbkdf_algo : "");
292

293
      auto pkcs8 =
294
         Botan::PKCS8::BER_encode_encrypted_pbkdf_msec(k, rng, passphrase, pbkdf_time, nullptr, cipher, pbkdf_algo);
295

296
      return invoke_view_callback(view, ctx, pkcs8);
297
   });
298
}
299

300
int botan_privkey_view_encrypted_pem_timed(botan_privkey_t key,
3✔
301
                                           botan_rng_t rng_obj,
302
                                           const char* passphrase,
303
                                           const char* maybe_cipher,
304
                                           const char* maybe_pbkdf_algo,
305
                                           size_t pbkdf_runtime_msec,
306
                                           botan_view_ctx ctx,
307
                                           botan_view_str_fn view) {
308
   if(passphrase == nullptr) {
3✔
309
      return BOTAN_FFI_ERROR_NULL_POINTER;
310
   }
311

312
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
15✔
313
      const std::chrono::milliseconds pbkdf_time(pbkdf_runtime_msec);
314
      Botan::RandomNumberGenerator& rng = safe_get(rng_obj);
315

316
      const std::string cipher = (maybe_cipher ? maybe_cipher : "");
317
      const std::string pbkdf_algo = (maybe_pbkdf_algo ? maybe_pbkdf_algo : "");
318

319
      auto pkcs8 =
320
         Botan::PKCS8::PEM_encode_encrypted_pbkdf_msec(k, rng, passphrase, pbkdf_time, nullptr, cipher, pbkdf_algo);
321

322
      return invoke_view_callback(view, ctx, pkcs8);
323
   });
324
}
325

326
int botan_privkey_export_encrypted_pbkdf_iter(botan_privkey_t key,
48✔
327
                                              uint8_t out[],
328
                                              size_t* out_len,
329
                                              botan_rng_t rng,
330
                                              const char* passphrase,
331
                                              size_t pbkdf_iter,
332
                                              const char* cipher,
333
                                              const char* pbkdf_algo,
334
                                              uint32_t flags) {
335
   if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_DER) {
48✔
336
      return copy_view_bin(
24✔
337
         out, out_len, botan_privkey_view_encrypted_der, key, rng, passphrase, cipher, pbkdf_algo, pbkdf_iter);
24✔
338
   } else if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_PEM) {
24✔
339
      return copy_view_str(
24✔
340
         out, out_len, botan_privkey_view_encrypted_pem, key, rng, passphrase, cipher, pbkdf_algo, pbkdf_iter);
24✔
341
   } else {
342
      return BOTAN_FFI_ERROR_BAD_FLAG;
343
   }
344
}
345

346
int botan_privkey_view_encrypted_der(botan_privkey_t key,
24✔
347
                                     botan_rng_t rng_obj,
348
                                     const char* passphrase,
349
                                     const char* maybe_cipher,
350
                                     const char* maybe_pbkdf_algo,
351
                                     size_t maybe_pbkdf_iterations,
352
                                     botan_view_ctx ctx,
353
                                     botan_view_bin_fn view) {
354
   if(passphrase == nullptr) {
24✔
355
      return BOTAN_FFI_ERROR_NULL_POINTER;
356
   }
357

358
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
192✔
359
      Botan::RandomNumberGenerator& rng = safe_get(rng_obj);
360

361
      const std::string cipher = (maybe_cipher ? maybe_cipher : "");
362
      const std::string pbkdf_algo = (maybe_pbkdf_algo ? maybe_pbkdf_algo : "");
363
      const size_t pbkdf_iter = (maybe_pbkdf_iterations ? maybe_pbkdf_iterations : 100000);
364

365
      auto pkcs8 = Botan::PKCS8::BER_encode_encrypted_pbkdf_iter(k, rng, passphrase, pbkdf_iter, cipher, pbkdf_algo);
366

367
      return invoke_view_callback(view, ctx, pkcs8);
368
   });
369
}
370

371
int botan_privkey_view_encrypted_pem(botan_privkey_t key,
24✔
372
                                     botan_rng_t rng_obj,
373
                                     const char* passphrase,
374
                                     const char* maybe_cipher,
375
                                     const char* maybe_pbkdf_algo,
376
                                     size_t maybe_pbkdf_iterations,
377
                                     botan_view_ctx ctx,
378
                                     botan_view_str_fn view) {
379
   if(passphrase == nullptr) {
24✔
380
      return BOTAN_FFI_ERROR_NULL_POINTER;
381
   }
382

383
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
168✔
384
      Botan::RandomNumberGenerator& rng = safe_get(rng_obj);
385

386
      const std::string cipher = (maybe_cipher ? maybe_cipher : "");
387
      const std::string pbkdf_algo = (maybe_pbkdf_algo ? maybe_pbkdf_algo : "");
388
      const size_t pbkdf_iter = (maybe_pbkdf_iterations ? maybe_pbkdf_iterations : 100000);
389

390
      auto pkcs8 = Botan::PKCS8::PEM_encode_encrypted_pbkdf_iter(k, rng, passphrase, pbkdf_iter, cipher, pbkdf_algo);
391

392
      return invoke_view_callback(view, ctx, pkcs8);
393
   });
394
}
395

396
int botan_pubkey_oid(botan_asn1_oid_t* oid, botan_pubkey_t key) {
2✔
397
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
4✔
398
      if(oid == nullptr) {
399
         return BOTAN_FFI_ERROR_NULL_POINTER;
400
      }
401

402
      auto oid_ptr = std::make_unique<Botan::OID>(k.object_identifier());
403
      ffi_new_object(oid, std::move(oid_ptr));
404

405
      return BOTAN_FFI_SUCCESS;
406
   });
407
}
408

409
int botan_privkey_oid(botan_asn1_oid_t* oid, botan_privkey_t key) {
2✔
410
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
4✔
411
      if(oid == nullptr) {
412
         return BOTAN_FFI_ERROR_NULL_POINTER;
413
      }
414

415
      auto oid_ptr = std::make_unique<Botan::OID>(k.object_identifier());
416
      ffi_new_object(oid, std::move(oid_ptr));
417

418
      return BOTAN_FFI_SUCCESS;
419
   });
420
}
421

422
int botan_privkey_stateful_operation(botan_privkey_t key, int* out) {
5✔
423
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
10✔
424
      if(out == nullptr) {
425
         return BOTAN_FFI_ERROR_NULL_POINTER;
426
      }
427

428
      if(k.stateful_operation()) {
429
         *out = 1;
430
      } else {
431
         *out = 0;
432
      }
433
      return BOTAN_FFI_SUCCESS;
434
   });
435
}
436

437
int botan_privkey_remaining_operations(botan_privkey_t key, uint64_t* out) {
5✔
438
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
10✔
439
      if(out == nullptr) {
440
         return BOTAN_FFI_ERROR_NULL_POINTER;
441
      }
442

443
      if(auto remaining = k.remaining_operations()) {
444
         *out = remaining.value();
445
         return BOTAN_FFI_SUCCESS;
446
      } else {
447
         return BOTAN_FFI_ERROR_NO_VALUE;
448
      }
449
   });
450
}
451

452
int botan_pubkey_estimated_strength(botan_pubkey_t key, size_t* estimate) {
15✔
453
   if(estimate == nullptr) {
15✔
454
      return BOTAN_FFI_ERROR_NULL_POINTER;
455
   }
456
   return BOTAN_FFI_VISIT(key, [=](const auto& k) { *estimate = k.estimated_strength(); });
30✔
457
}
458

459
int botan_pubkey_fingerprint(botan_pubkey_t key, const char* hash_fn, uint8_t out[], size_t* out_len) {
24✔
460
   if(hash_fn == nullptr) {
24✔
461
      return BOTAN_FFI_ERROR_NULL_POINTER;
462
   }
463
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
120✔
464
      auto h = Botan::HashFunction::create_or_throw(hash_fn);
465
      return write_vec_output(out, out_len, h->process(k.public_key_bits()));
466
   });
467
}
468

469
int botan_pkcs_hash_id(const char* hash_name, uint8_t pkcs_id[], size_t* pkcs_id_len) {
2✔
470
   if(hash_name == nullptr) {
2✔
471
      return BOTAN_FFI_ERROR_NULL_POINTER;
472
   }
473
#if defined(BOTAN_HAS_HASH_ID)
474
   return ffi_guard_thunk(__func__, [=]() -> int {
2✔
475
      const std::vector<uint8_t> hash_id = Botan::pkcs_hash_id(hash_name);
2✔
476
      return write_output(pkcs_id, pkcs_id_len, hash_id.data(), hash_id.size());
2✔
477
   });
2✔
478
#else
479
   BOTAN_UNUSED(hash_name, pkcs_id, pkcs_id_len);
480
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
481
#endif
482
}
483
}
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