8#include <botan/internal/ffi_util.h>
9#include <botan/internal/ffi_pkey.h>
10#include <botan/internal/ffi_rng.h>
11#include <botan/data_src.h>
12#include <botan/hash.h>
13#include <botan/pkcs8.h>
14#include <botan/pk_keys.h>
15#include <botan/x509_key.h>
16#include <botan/pk_algs.h>
18#if defined(BOTAN_HAS_HASH_ID)
19 #include <botan/internal/hash_id.h>
27 const char* algo_name,
28 const char* algo_params,
32 if(key_obj ==
nullptr)
36 if(rng_obj ==
nullptr)
40 std::unique_ptr<Botan::Private_Key> key(
43 algo_params ? algo_params :
""));
47 *key_obj =
new botan_privkey_struct(std::move(key));
58 const uint8_t bits[],
size_t len,
68 std::unique_ptr<Botan::Private_Key> pkcs8;
70 if(password ==
nullptr)
81 *key =
new botan_privkey_struct(std::move(pkcs8));
94 const uint8_t bits[],
size_t bits_len)
102 if(pubkey ==
nullptr)
105 *key =
new botan_pubkey_struct(std::move(pubkey));
118 auto public_key =
safe_get(key_obj).public_key();
119 *pubout =
new botan_pubkey_struct(std::move(public_key));
204 uint8_t out[],
size_t* out_len,
214 uint8_t out[],
size_t* out_len,
216 const char* passphrase,
218 size_t* pbkdf_iters_out,
220 const char* pbkdf_hash,
224 *pbkdf_iters_out = 0;
229 key, rng, passphrase, cipher, pbkdf_hash, pbkdf_msec);
234 key, rng, passphrase, cipher, pbkdf_hash, pbkdf_msec);
245 const char* passphrase,
246 const char* maybe_cipher,
247 const char* maybe_pbkdf_algo,
248 size_t pbkdf_runtime_msec,
252 if(passphrase ==
nullptr)
257 const std::chrono::milliseconds pbkdf_time(pbkdf_runtime_msec);
260 const std::string cipher = (maybe_cipher ? maybe_cipher :
"");
261 const std::string pbkdf_algo = (maybe_pbkdf_algo ? maybe_pbkdf_algo :
"");
264 k, rng, passphrase, pbkdf_time,
nullptr, cipher, pbkdf_algo);
273 const char* passphrase,
274 const char* maybe_cipher,
275 const char* maybe_pbkdf_algo,
276 size_t pbkdf_runtime_msec,
280 if(passphrase ==
nullptr)
285 const std::chrono::milliseconds pbkdf_time(pbkdf_runtime_msec);
288 const std::string cipher = (maybe_cipher ? maybe_cipher :
"");
289 const std::string pbkdf_algo = (maybe_pbkdf_algo ? maybe_pbkdf_algo :
"");
292 k, rng, passphrase, pbkdf_time,
nullptr, cipher, pbkdf_algo);
299 uint8_t out[],
size_t* out_len,
301 const char* passphrase,
304 const char* pbkdf_algo,
310 key, rng, passphrase, cipher, pbkdf_algo, pbkdf_iter);
315 key, rng, passphrase, cipher, pbkdf_algo, pbkdf_iter);
326 const char* passphrase,
327 const char* maybe_cipher,
328 const char* maybe_pbkdf_algo,
329 size_t maybe_pbkdf_iterations,
333 if(passphrase ==
nullptr)
339 const std::string cipher = (maybe_cipher ? maybe_cipher :
"");
340 const std::string pbkdf_algo = (maybe_pbkdf_algo ? maybe_pbkdf_algo :
"");
341 const size_t pbkdf_iter = (maybe_pbkdf_iterations ? maybe_pbkdf_iterations : 100000);
352 const char* passphrase,
353 const char* maybe_cipher,
354 const char* maybe_pbkdf_algo,
355 size_t maybe_pbkdf_iterations,
359 if(passphrase ==
nullptr)
365 const std::string cipher = (maybe_cipher ? maybe_cipher :
"");
366 const std::string pbkdf_algo = (maybe_pbkdf_algo ? maybe_pbkdf_algo :
"");
367 const size_t pbkdf_iter = (maybe_pbkdf_iterations ? maybe_pbkdf_iterations : 100000);
377 return BOTAN_FFI_VISIT(key, [=](
const auto& k) { *estimate = k.estimated_strength(); });
381 uint8_t out[],
size_t* out_len)
391#if defined(BOTAN_HAS_HASH_ID)
394 return write_output(pkcs_id, pkcs_id_len, hash_id.data(), hash_id.size());
#define BOTAN_UNUSED(...)
static std::unique_ptr< HashFunction > create_or_throw(std::string_view algo_spec, std::string_view provider="")
struct botan_pubkey_struct * botan_pubkey_t
struct botan_privkey_struct * botan_privkey_t
int(* botan_view_bin_fn)(botan_view_ctx view_ctx, const uint8_t *data, size_t len)
#define BOTAN_PRIVKEY_EXPORT_FLAG_PEM
#define BOTAN_CHECK_KEY_EXPENSIVE_TESTS
struct botan_rng_struct * botan_rng_t
#define BOTAN_PRIVKEY_EXPORT_FLAG_DER
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
@ BOTAN_FFI_ERROR_UNKNOWN_ERROR
@ BOTAN_FFI_ERROR_BAD_FLAG
@ BOTAN_FFI_ERROR_INVALID_INPUT
@ BOTAN_FFI_ERROR_NULL_POINTER
int(* botan_view_str_fn)(botan_view_ctx view_ctx, const char *str, size_t len)
int botan_privkey_export_encrypted(botan_privkey_t key, uint8_t out[], size_t *out_len, botan_rng_t rng_obj, const char *pass, const char *, uint32_t flags)
int botan_privkey_check_key(botan_privkey_t key, botan_rng_t rng, uint32_t flags)
int botan_pubkey_fingerprint(botan_pubkey_t key, const char *hash_fn, uint8_t out[], size_t *out_len)
int botan_privkey_load(botan_privkey_t *key, botan_rng_t rng_obj, const uint8_t bits[], size_t len, const char *password)
int botan_pubkey_estimated_strength(botan_pubkey_t key, size_t *estimate)
int botan_pkcs_hash_id(const char *hash_name, uint8_t pkcs_id[], size_t *pkcs_id_len)
int botan_privkey_view_encrypted_der_timed(botan_privkey_t key, botan_rng_t rng_obj, const char *passphrase, const char *maybe_cipher, const char *maybe_pbkdf_algo, size_t pbkdf_runtime_msec, botan_view_ctx ctx, botan_view_bin_fn view)
int botan_pubkey_load(botan_pubkey_t *key, const uint8_t bits[], size_t bits_len)
int botan_privkey_export(botan_privkey_t key, uint8_t out[], size_t *out_len, uint32_t flags)
int botan_privkey_view_encrypted_pem_timed(botan_privkey_t key, botan_rng_t rng_obj, const char *passphrase, const char *maybe_cipher, const char *maybe_pbkdf_algo, size_t pbkdf_runtime_msec, botan_view_ctx ctx, botan_view_str_fn view)
int botan_privkey_export_encrypted_pbkdf_msec(botan_privkey_t key, uint8_t out[], size_t *out_len, botan_rng_t rng, const char *passphrase, uint32_t pbkdf_msec, size_t *pbkdf_iters_out, const char *cipher, const char *pbkdf_hash, uint32_t flags)
int botan_privkey_view_encrypted_der(botan_privkey_t key, botan_rng_t rng_obj, const char *passphrase, const char *maybe_cipher, const char *maybe_pbkdf_algo, size_t maybe_pbkdf_iterations, botan_view_ctx ctx, botan_view_bin_fn view)
int botan_privkey_destroy(botan_privkey_t key)
int botan_pubkey_algo_name(botan_pubkey_t key, char out[], size_t *out_len)
int botan_pubkey_view_pem(botan_pubkey_t key, botan_view_ctx ctx, botan_view_str_fn view)
int botan_privkey_view_der(botan_privkey_t key, botan_view_ctx ctx, botan_view_bin_fn view)
int botan_pubkey_view_der(botan_pubkey_t key, botan_view_ctx ctx, botan_view_bin_fn view)
int botan_pubkey_export(botan_pubkey_t key, uint8_t out[], size_t *out_len, uint32_t flags)
int botan_pubkey_destroy(botan_pubkey_t key)
int botan_privkey_algo_name(botan_privkey_t key, char out[], size_t *out_len)
int botan_privkey_create(botan_privkey_t *key_obj, const char *algo_name, const char *algo_params, botan_rng_t rng_obj)
int botan_privkey_export_pubkey(botan_pubkey_t *pubout, botan_privkey_t key_obj)
int botan_pubkey_check_key(botan_pubkey_t key, botan_rng_t rng, uint32_t flags)
int botan_privkey_export_encrypted_pbkdf_iter(botan_privkey_t key, uint8_t out[], size_t *out_len, botan_rng_t rng, const char *passphrase, size_t pbkdf_iter, const char *cipher, const char *pbkdf_algo, uint32_t flags)
int botan_privkey_view_pem(botan_privkey_t key, botan_view_ctx ctx, botan_view_str_fn view)
int botan_privkey_view_encrypted_pem(botan_privkey_t key, botan_rng_t rng_obj, const char *passphrase, const char *maybe_cipher, const char *maybe_pbkdf_algo, size_t maybe_pbkdf_iterations, botan_view_ctx ctx, botan_view_str_fn view)
#define BOTAN_FFI_VISIT(obj, lambda)
#define BOTAN_FFI_CHECKED_DELETE(o)
std::vector< uint8_t > BER_encode(const Private_Key &key, RandomNumberGenerator &rng, std::string_view pass, std::chrono::milliseconds msec, std::string_view pbe_algo)
std::string PEM_encode_encrypted_pbkdf_iter(const Private_Key &key, RandomNumberGenerator &rng, std::string_view pass, size_t pbkdf_iterations, std::string_view cipher, std::string_view pbkdf_hash)
std::string PEM_encode(const Private_Key &key)
std::string PEM_encode_encrypted_pbkdf_msec(const Private_Key &key, RandomNumberGenerator &rng, std::string_view pass, std::chrono::milliseconds pbkdf_msec, size_t *pbkdf_iterations, std::string_view cipher, std::string_view pbkdf_hash)
std::vector< uint8_t > BER_encode_encrypted_pbkdf_iter(const Private_Key &key, RandomNumberGenerator &rng, std::string_view pass, size_t pbkdf_iterations, std::string_view cipher, std::string_view pbkdf_hash)
std::vector< uint8_t > BER_encode_encrypted_pbkdf_msec(const Private_Key &key, RandomNumberGenerator &rng, std::string_view pass, std::chrono::milliseconds pbkdf_msec, size_t *pbkdf_iterations, std::string_view cipher, std::string_view pbkdf_hash)
std::unique_ptr< Private_Key > load_key(DataSource &source, const std::function< std::string()> &get_pass)
std::vector< uint8_t > BER_encode(const Public_Key &key)
std::unique_ptr< Public_Key > load_key(DataSource &source)
std::string PEM_encode(const Public_Key &key)
int copy_view_bin(uint8_t out[], size_t *out_len, Fn fn, Args... args)
int write_str_output(uint8_t out[], size_t *out_len, std::string_view str)
T & safe_get(botan_struct< T, M > *p)
int invoke_view_callback(botan_view_bin_fn view, botan_view_ctx ctx, const std::vector< uint8_t, Alloc > &buf)
int copy_view_str(uint8_t out[], size_t *out_len, Fn fn, Args... args)
int ffi_guard_thunk(const char *func_name, const std::function< int()> &thunk)
int write_vec_output(uint8_t out[], size_t *out_len, const std::vector< uint8_t, Alloc > &buf)
int write_output(uint8_t out[], size_t *out_len, const uint8_t buf[], size_t buf_len)
std::unique_ptr< Private_Key > create_private_key(std::string_view alg_name, RandomNumberGenerator &rng, std::string_view params, std::string_view provider)
std::vector< uint8_t > pkcs_hash_id(std::string_view name)