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

randombit / botan / 5230455705

10 Jun 2023 02:30PM UTC coverage: 91.715% (-0.03%) from 91.746%
5230455705

push

github

randombit
Merge GH #3584 Change clang-format AllowShortFunctionsOnASingleLine config from All to Inline

77182 of 84154 relevant lines covered (91.72%)

11975295.43 hits per line

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

95.5
/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/data_src.h>
10
#include <botan/hash.h>
11
#include <botan/pk_algs.h>
12
#include <botan/pk_keys.h>
13
#include <botan/pkcs8.h>
14
#include <botan/x509_key.h>
15
#include <botan/internal/ffi_pkey.h>
16
#include <botan/internal/ffi_rng.h>
17
#include <botan/internal/ffi_util.h>
18

19
#if defined(BOTAN_HAS_HASH_ID)
20
   #include <botan/internal/hash_id.h>
21
#endif
22

23
extern "C" {
24

25
using namespace Botan_FFI;
26

27
int botan_privkey_create(botan_privkey_t* key_obj,
23✔
28
                         const char* algo_name,
29
                         const char* algo_params,
30
                         botan_rng_t rng_obj) {
31
   return ffi_guard_thunk(__func__, [=]() -> int {
23✔
32
      if(key_obj == nullptr) {
46✔
33
         return BOTAN_FFI_ERROR_NULL_POINTER;
34
      }
35

36
      *key_obj = nullptr;
23✔
37
      if(rng_obj == nullptr) {
23✔
38
         return BOTAN_FFI_ERROR_NULL_POINTER;
39
      }
40

41
      Botan::RandomNumberGenerator& rng = safe_get(rng_obj);
23✔
42
      std::unique_ptr<Botan::Private_Key> key(
23✔
43
         Botan::create_private_key(algo_name ? algo_name : "RSA", rng, algo_params ? algo_params : ""));
69✔
44

45
      if(key) {
23✔
46
         *key_obj = new botan_privkey_struct(std::move(key));
23✔
47
         return BOTAN_FFI_SUCCESS;
23✔
48
      } else {
49
         return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
50
      }
51
   });
46✔
52
}
53

54
int botan_privkey_load(
51✔
55
   botan_privkey_t* key, botan_rng_t rng_obj, const uint8_t bits[], size_t len, const char* password) {
56
   BOTAN_UNUSED(rng_obj);
51✔
57

58
   *key = nullptr;
51✔
59

60
   return ffi_guard_thunk(__func__, [=]() -> int {
51✔
61
      Botan::DataSource_Memory src(bits, len);
51✔
62

63
      std::unique_ptr<Botan::Private_Key> pkcs8;
51✔
64

65
      if(password == nullptr) {
51✔
66
         pkcs8 = Botan::PKCS8::load_key(src);
22✔
67
      } else {
68
         pkcs8 = Botan::PKCS8::load_key(src, std::string(password));
35✔
69
      }
70

71
      if(pkcs8) {
51✔
72
         *key = new botan_privkey_struct(std::move(pkcs8));
51✔
73
         return BOTAN_FFI_SUCCESS;
51✔
74
      }
75
      return BOTAN_FFI_ERROR_UNKNOWN_ERROR;
76
   });
153✔
77
}
78

79
int botan_privkey_destroy(botan_privkey_t key) {
99✔
80
   return BOTAN_FFI_CHECKED_DELETE(key);
99✔
81
}
82

83
int botan_pubkey_load(botan_pubkey_t* key, const uint8_t bits[], size_t bits_len) {
12✔
84
   *key = nullptr;
12✔
85

86
   return ffi_guard_thunk(__func__, [=]() -> int {
12✔
87
      Botan::DataSource_Memory src(bits, bits_len);
12✔
88
      std::unique_ptr<Botan::Public_Key> pubkey(Botan::X509::load_key(src));
12✔
89

90
      if(pubkey == nullptr) {
12✔
91
         return BOTAN_FFI_ERROR_UNKNOWN_ERROR;
92
      }
93

94
      *key = new botan_pubkey_struct(std::move(pubkey));
12✔
95
      return BOTAN_FFI_SUCCESS;
12✔
96
   });
36✔
97
}
98

99
int botan_pubkey_destroy(botan_pubkey_t key) {
65✔
100
   return BOTAN_FFI_CHECKED_DELETE(key);
65✔
101
}
102

103
int botan_privkey_export_pubkey(botan_pubkey_t* pubout, botan_privkey_t key_obj) {
31✔
104
   return ffi_guard_thunk(__func__, [=]() -> int {
31✔
105
      auto public_key = safe_get(key_obj).public_key();
31✔
106
      *pubout = new botan_pubkey_struct(std::move(public_key));
31✔
107
      return BOTAN_FFI_SUCCESS;
31✔
108
   });
62✔
109
}
110

111
int botan_privkey_algo_name(botan_privkey_t key, char out[], size_t* out_len) {
2✔
112
   return BOTAN_FFI_VISIT(key, [=](const auto& k) { return write_str_output(out, out_len, k.algo_name()); });
4✔
113
}
114

115
int botan_pubkey_algo_name(botan_pubkey_t key, char out[], size_t* out_len) {
7✔
116
   return BOTAN_FFI_VISIT(key, [=](const auto& k) { return write_str_output(out, out_len, k.algo_name()); });
14✔
117
}
118

119
int botan_pubkey_check_key(botan_pubkey_t key, botan_rng_t rng, uint32_t flags) {
25✔
120
   const bool strong = (flags & BOTAN_CHECK_KEY_EXPENSIVE_TESTS);
25✔
121

122
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
50✔
123
      return (k.check_key(safe_get(rng), strong) == true) ? 0 : BOTAN_FFI_ERROR_INVALID_INPUT;
124
   });
125
}
126

127
int botan_privkey_check_key(botan_privkey_t key, botan_rng_t rng, uint32_t flags) {
14✔
128
   const bool strong = (flags & BOTAN_CHECK_KEY_EXPENSIVE_TESTS);
14✔
129
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
28✔
130
      return (k.check_key(safe_get(rng), strong) == true) ? 0 : BOTAN_FFI_ERROR_INVALID_INPUT;
131
   });
132
}
133

134
int botan_pubkey_export(botan_pubkey_t key, uint8_t out[], size_t* out_len, uint32_t flags) {
44✔
135
   if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_DER) {
44✔
136
      return copy_view_bin(out, out_len, botan_pubkey_view_der, key);
22✔
137
   } else if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_PEM) {
22✔
138
      return copy_view_str(out, out_len, botan_pubkey_view_pem, key);
22✔
139
   } else {
140
      return BOTAN_FFI_ERROR_BAD_FLAG;
141
   }
142
}
143

144
int botan_pubkey_view_der(botan_pubkey_t key, botan_view_ctx ctx, botan_view_bin_fn view) {
23✔
145
   return BOTAN_FFI_VISIT(
69✔
146
      key, [=](const auto& k) -> int { return invoke_view_callback(view, ctx, Botan::X509::BER_encode(k)); });
147
}
148

149
int botan_pubkey_view_pem(botan_pubkey_t key, botan_view_ctx ctx, botan_view_str_fn view) {
26✔
150
   return BOTAN_FFI_VISIT(
78✔
151
      key, [=](const auto& k) -> int { return invoke_view_callback(view, ctx, Botan::X509::PEM_encode(k)); });
152
}
153

154
int botan_privkey_export(botan_privkey_t key, uint8_t out[], size_t* out_len, uint32_t flags) {
44✔
155
   if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_DER) {
44✔
156
      return copy_view_bin(out, out_len, botan_privkey_view_der, key);
22✔
157
   } else if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_PEM) {
22✔
158
      return copy_view_str(out, out_len, botan_privkey_view_pem, key);
22✔
159
   } else {
160
      return BOTAN_FFI_ERROR_BAD_FLAG;
161
   }
162
}
163

164
int botan_privkey_view_der(botan_privkey_t key, botan_view_ctx ctx, botan_view_bin_fn view) {
27✔
165
   return BOTAN_FFI_VISIT(
81✔
166
      key, [=](const auto& k) -> int { return invoke_view_callback(view, ctx, Botan::PKCS8::BER_encode(k)); });
167
}
168

169
int botan_privkey_view_pem(botan_privkey_t key, botan_view_ctx ctx, botan_view_str_fn view) {
34✔
170
   return BOTAN_FFI_VISIT(
102✔
171
      key, [=](const auto& k) -> int { return invoke_view_callback(view, ctx, Botan::PKCS8::PEM_encode(k)); });
172
}
173

174
int botan_privkey_export_encrypted(botan_privkey_t key,
×
175
                                   uint8_t out[],
176
                                   size_t* out_len,
177
                                   botan_rng_t rng_obj,
178
                                   const char* pass,
179
                                   const char* /*ignored - pbe*/,
180
                                   uint32_t flags) {
181
   return botan_privkey_export_encrypted_pbkdf_iter(key, out, out_len, rng_obj, pass, 100000, nullptr, nullptr, flags);
×
182
}
183

184
int botan_privkey_export_encrypted_pbkdf_msec(botan_privkey_t key,
11✔
185
                                              uint8_t out[],
186
                                              size_t* out_len,
187
                                              botan_rng_t rng,
188
                                              const char* passphrase,
189
                                              uint32_t pbkdf_msec,
190
                                              size_t* pbkdf_iters_out,
191
                                              const char* cipher,
192
                                              const char* pbkdf_hash,
193
                                              uint32_t flags) {
194
   if(pbkdf_iters_out) {
11✔
195
      *pbkdf_iters_out = 0;
11✔
196
   }
197

198
   if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_DER) {
11✔
199
      return copy_view_bin(
11✔
200
         out, out_len, botan_privkey_view_encrypted_der_timed, key, rng, passphrase, cipher, pbkdf_hash, pbkdf_msec);
11✔
201
   } else if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_PEM) {
×
202
      return copy_view_str(
×
203
         out, out_len, botan_privkey_view_encrypted_pem_timed, key, rng, passphrase, cipher, pbkdf_hash, pbkdf_msec);
×
204
   } else {
205
      return BOTAN_FFI_ERROR_BAD_FLAG;
206
   }
207
}
208

209
int botan_privkey_view_encrypted_der_timed(botan_privkey_t key,
14✔
210
                                           botan_rng_t rng_obj,
211
                                           const char* passphrase,
212
                                           const char* maybe_cipher,
213
                                           const char* maybe_pbkdf_algo,
214
                                           size_t pbkdf_runtime_msec,
215
                                           botan_view_ctx ctx,
216
                                           botan_view_bin_fn view) {
217
   if(passphrase == nullptr) {
14✔
218
      return BOTAN_FFI_ERROR_NULL_POINTER;
219
   }
220

221
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
67✔
222
      const std::chrono::milliseconds pbkdf_time(pbkdf_runtime_msec);
223
      Botan::RandomNumberGenerator& rng = safe_get(rng_obj);
224

225
      const std::string cipher = (maybe_cipher ? maybe_cipher : "");
226
      const std::string pbkdf_algo = (maybe_pbkdf_algo ? maybe_pbkdf_algo : "");
227

228
      auto pkcs8 =
229
         Botan::PKCS8::BER_encode_encrypted_pbkdf_msec(k, rng, passphrase, pbkdf_time, nullptr, cipher, pbkdf_algo);
230

231
      return invoke_view_callback(view, ctx, pkcs8);
232
   });
233
}
234

235
int botan_privkey_view_encrypted_pem_timed(botan_privkey_t key,
3✔
236
                                           botan_rng_t rng_obj,
237
                                           const char* passphrase,
238
                                           const char* maybe_cipher,
239
                                           const char* maybe_pbkdf_algo,
240
                                           size_t pbkdf_runtime_msec,
241
                                           botan_view_ctx ctx,
242
                                           botan_view_str_fn view) {
243
   if(passphrase == nullptr) {
3✔
244
      return BOTAN_FFI_ERROR_NULL_POINTER;
245
   }
246

247
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
12✔
248
      const std::chrono::milliseconds pbkdf_time(pbkdf_runtime_msec);
249
      Botan::RandomNumberGenerator& rng = safe_get(rng_obj);
250

251
      const std::string cipher = (maybe_cipher ? maybe_cipher : "");
252
      const std::string pbkdf_algo = (maybe_pbkdf_algo ? maybe_pbkdf_algo : "");
253

254
      auto pkcs8 =
255
         Botan::PKCS8::PEM_encode_encrypted_pbkdf_msec(k, rng, passphrase, pbkdf_time, nullptr, cipher, pbkdf_algo);
256

257
      return invoke_view_callback(view, ctx, pkcs8);
258
   });
259
}
260

261
int botan_privkey_export_encrypted_pbkdf_iter(botan_privkey_t key,
44✔
262
                                              uint8_t out[],
263
                                              size_t* out_len,
264
                                              botan_rng_t rng,
265
                                              const char* passphrase,
266
                                              size_t pbkdf_iter,
267
                                              const char* cipher,
268
                                              const char* pbkdf_algo,
269
                                              uint32_t flags) {
270
   if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_DER) {
44✔
271
      return copy_view_bin(
22✔
272
         out, out_len, botan_privkey_view_encrypted_der, key, rng, passphrase, cipher, pbkdf_algo, pbkdf_iter);
22✔
273
   } else if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_PEM) {
22✔
274
      return copy_view_str(
22✔
275
         out, out_len, botan_privkey_view_encrypted_pem, key, rng, passphrase, cipher, pbkdf_algo, pbkdf_iter);
22✔
276
   } else {
277
      return BOTAN_FFI_ERROR_BAD_FLAG;
278
   }
279
}
280

281
int botan_privkey_view_encrypted_der(botan_privkey_t key,
22✔
282
                                     botan_rng_t rng_obj,
283
                                     const char* passphrase,
284
                                     const char* maybe_cipher,
285
                                     const char* maybe_pbkdf_algo,
286
                                     size_t maybe_pbkdf_iterations,
287
                                     botan_view_ctx ctx,
288
                                     botan_view_bin_fn view) {
289
   if(passphrase == nullptr) {
22✔
290
      return BOTAN_FFI_ERROR_NULL_POINTER;
291
   }
292

293
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
132✔
294
      Botan::RandomNumberGenerator& rng = safe_get(rng_obj);
295

296
      const std::string cipher = (maybe_cipher ? maybe_cipher : "");
297
      const std::string pbkdf_algo = (maybe_pbkdf_algo ? maybe_pbkdf_algo : "");
298
      const size_t pbkdf_iter = (maybe_pbkdf_iterations ? maybe_pbkdf_iterations : 100000);
299

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

302
      return invoke_view_callback(view, ctx, pkcs8);
303
   });
304
}
305

306
int botan_privkey_view_encrypted_pem(botan_privkey_t key,
22✔
307
                                     botan_rng_t rng_obj,
308
                                     const char* passphrase,
309
                                     const char* maybe_cipher,
310
                                     const char* maybe_pbkdf_algo,
311
                                     size_t maybe_pbkdf_iterations,
312
                                     botan_view_ctx ctx,
313
                                     botan_view_str_fn view) {
314
   if(passphrase == nullptr) {
22✔
315
      return BOTAN_FFI_ERROR_NULL_POINTER;
316
   }
317

318
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
132✔
319
      Botan::RandomNumberGenerator& rng = safe_get(rng_obj);
320

321
      const std::string cipher = (maybe_cipher ? maybe_cipher : "");
322
      const std::string pbkdf_algo = (maybe_pbkdf_algo ? maybe_pbkdf_algo : "");
323
      const size_t pbkdf_iter = (maybe_pbkdf_iterations ? maybe_pbkdf_iterations : 100000);
324

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

327
      return invoke_view_callback(view, ctx, pkcs8);
328
   });
329
}
330

331
int botan_pubkey_estimated_strength(botan_pubkey_t key, size_t* estimate) {
14✔
332
   return BOTAN_FFI_VISIT(key, [=](const auto& k) { *estimate = k.estimated_strength(); });
28✔
333
}
334

335
int botan_pubkey_fingerprint(botan_pubkey_t key, const char* hash_fn, uint8_t out[], size_t* out_len) {
22✔
336
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
132✔
337
      auto h = Botan::HashFunction::create_or_throw(hash_fn);
338
      return write_vec_output(out, out_len, h->process(k.public_key_bits()));
339
   });
340
}
341

342
int botan_pkcs_hash_id(const char* hash_name, uint8_t pkcs_id[], size_t* pkcs_id_len) {
2✔
343
#if defined(BOTAN_HAS_HASH_ID)
344
   return ffi_guard_thunk(__func__, [=]() -> int {
2✔
345
      const std::vector<uint8_t> hash_id = Botan::pkcs_hash_id(hash_name);
2✔
346
      return write_output(pkcs_id, pkcs_id_len, hash_id.data(), hash_id.size());
2✔
347
   });
4✔
348
#else
349
   BOTAN_UNUSED(hash_name, pkcs_id, pkcs_id_len);
350
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
351
#endif
352
}
353
}
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