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

randombit / botan / 5123321399

30 May 2023 04:06PM UTC coverage: 92.213% (+0.004%) from 92.209%
5123321399

Pull #3558

github

web-flow
Merge dd72f7389 into 057bcbc35
Pull Request #3558: Add braces around all if/else statements

75602 of 81986 relevant lines covered (92.21%)

11859779.3 hits per line

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

95.41
/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) { return BOTAN_FFI_CHECKED_DELETE(key); }
95✔
80

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

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

88
      if(pubkey == nullptr) {
12✔
89
         return BOTAN_FFI_ERROR_UNKNOWN_ERROR;
90
      }
91

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

97
int botan_pubkey_destroy(botan_pubkey_t key) { return BOTAN_FFI_CHECKED_DELETE(key); }
57✔
98

99
int botan_privkey_export_pubkey(botan_pubkey_t* pubout, botan_privkey_t key_obj) {
27✔
100
   return ffi_guard_thunk(__func__, [=]() -> int {
27✔
101
      auto public_key = safe_get(key_obj).public_key();
27✔
102
      *pubout = new botan_pubkey_struct(std::move(public_key));
27✔
103
      return BOTAN_FFI_SUCCESS;
27✔
104
   });
54✔
105
}
106

107
int botan_privkey_algo_name(botan_privkey_t key, char out[], size_t* out_len) {
2✔
108
   return BOTAN_FFI_VISIT(key, [=](const auto& k) { return write_str_output(out, out_len, k.algo_name()); });
4✔
109
}
110

111
int botan_pubkey_algo_name(botan_pubkey_t key, char out[], size_t* out_len) {
7✔
112
   return BOTAN_FFI_VISIT(key, [=](const auto& k) { return write_str_output(out, out_len, k.algo_name()); });
14✔
113
}
114

115
int botan_pubkey_check_key(botan_pubkey_t key, botan_rng_t rng, uint32_t flags) {
25✔
116
   const bool strong = (flags & BOTAN_CHECK_KEY_EXPENSIVE_TESTS);
25✔
117

118
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
50✔
119
      return (k.check_key(safe_get(rng), strong) == true) ? 0 : BOTAN_FFI_ERROR_INVALID_INPUT;
120
   });
121
}
122

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

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

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

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

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

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

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

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

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

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

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

217
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
67✔
218
      const std::chrono::milliseconds pbkdf_time(pbkdf_runtime_msec);
219
      Botan::RandomNumberGenerator& rng = safe_get(rng_obj);
220

221
      const std::string cipher = (maybe_cipher ? maybe_cipher : "");
222
      const std::string pbkdf_algo = (maybe_pbkdf_algo ? maybe_pbkdf_algo : "");
223

224
      auto pkcs8 =
225
         Botan::PKCS8::BER_encode_encrypted_pbkdf_msec(k, rng, passphrase, pbkdf_time, nullptr, cipher, pbkdf_algo);
226

227
      return invoke_view_callback(view, ctx, pkcs8);
228
   });
229
}
230

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

243
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
12✔
244
      const std::chrono::milliseconds pbkdf_time(pbkdf_runtime_msec);
245
      Botan::RandomNumberGenerator& rng = safe_get(rng_obj);
246

247
      const std::string cipher = (maybe_cipher ? maybe_cipher : "");
248
      const std::string pbkdf_algo = (maybe_pbkdf_algo ? maybe_pbkdf_algo : "");
249

250
      auto pkcs8 =
251
         Botan::PKCS8::PEM_encode_encrypted_pbkdf_msec(k, rng, passphrase, pbkdf_time, nullptr, cipher, pbkdf_algo);
252

253
      return invoke_view_callback(view, ctx, pkcs8);
254
   });
255
}
256

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

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

289
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
132✔
290
      Botan::RandomNumberGenerator& rng = safe_get(rng_obj);
291

292
      const std::string cipher = (maybe_cipher ? maybe_cipher : "");
293
      const std::string pbkdf_algo = (maybe_pbkdf_algo ? maybe_pbkdf_algo : "");
294
      const size_t pbkdf_iter = (maybe_pbkdf_iterations ? maybe_pbkdf_iterations : 100000);
295

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

298
      return invoke_view_callback(view, ctx, pkcs8);
299
   });
300
}
301

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

314
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
132✔
315
      Botan::RandomNumberGenerator& rng = safe_get(rng_obj);
316

317
      const std::string cipher = (maybe_cipher ? maybe_cipher : "");
318
      const std::string pbkdf_algo = (maybe_pbkdf_algo ? maybe_pbkdf_algo : "");
319
      const size_t pbkdf_iter = (maybe_pbkdf_iterations ? maybe_pbkdf_iterations : 100000);
320

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

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

327
int botan_pubkey_estimated_strength(botan_pubkey_t key, size_t* estimate) {
14✔
328
   return BOTAN_FFI_VISIT(key, [=](const auto& k) { *estimate = k.estimated_strength(); });
28✔
329
}
330

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

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