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>
19#if defined(BOTAN_HAS_HASH_ID)
20 #include <botan/internal/hash_id.h>
28 const char* algo_name,
29 const char* algo_params,
32 if(key_obj ==
nullptr) {
37 if(rng_obj ==
nullptr) {
42 std::unique_ptr<Botan::Private_Key> key(
46 *key_obj =
new botan_privkey_struct(std::move(key));
63 std::unique_ptr<Botan::Private_Key> pkcs8;
65 if(password ==
nullptr) {
72 *key =
new botan_privkey_struct(std::move(pkcs8));
90 if(pubkey ==
nullptr) {
94 *key =
new botan_pubkey_struct(std::move(pubkey));
105 auto public_key =
safe_get(key_obj).public_key();
106 *pubout =
new botan_pubkey_struct(std::move(public_key));
158 key, [=](
const auto& k) ->
int {
return invoke_view_callback(view, ctx, k.raw_public_key_bits()); });
185 key, [=](
const auto& k) ->
int {
return invoke_view_callback(view, ctx, k.raw_private_key_bits()); });
202 const char* passphrase,
204 size_t* pbkdf_iters_out,
206 const char* pbkdf_hash,
208 if(pbkdf_iters_out) {
209 *pbkdf_iters_out = 0;
225 const char* passphrase,
226 const char* maybe_cipher,
227 const char* maybe_pbkdf_algo,
228 size_t pbkdf_runtime_msec,
231 if(passphrase ==
nullptr) {
236 const std::chrono::milliseconds pbkdf_time(pbkdf_runtime_msec);
239 const std::string cipher = (maybe_cipher ? maybe_cipher :
"");
240 const std::string pbkdf_algo = (maybe_pbkdf_algo ? maybe_pbkdf_algo :
"");
251 const char* passphrase,
252 const char* maybe_cipher,
253 const char* maybe_pbkdf_algo,
254 size_t pbkdf_runtime_msec,
257 if(passphrase ==
nullptr) {
262 const std::chrono::milliseconds pbkdf_time(pbkdf_runtime_msec);
265 const std::string cipher = (maybe_cipher ? maybe_cipher :
"");
266 const std::string pbkdf_algo = (maybe_pbkdf_algo ? maybe_pbkdf_algo :
"");
279 const char* passphrase,
282 const char* pbkdf_algo,
297 const char* passphrase,
298 const char* maybe_cipher,
299 const char* maybe_pbkdf_algo,
300 size_t maybe_pbkdf_iterations,
303 if(passphrase ==
nullptr) {
310 const std::string cipher = (maybe_cipher ? maybe_cipher :
"");
311 const std::string pbkdf_algo = (maybe_pbkdf_algo ? maybe_pbkdf_algo :
"");
312 const size_t pbkdf_iter = (maybe_pbkdf_iterations ? maybe_pbkdf_iterations : 100000);
322 const char* passphrase,
323 const char* maybe_cipher,
324 const char* maybe_pbkdf_algo,
325 size_t maybe_pbkdf_iterations,
328 if(passphrase ==
nullptr) {
335 const std::string cipher = (maybe_cipher ? maybe_cipher :
"");
336 const std::string pbkdf_algo = (maybe_pbkdf_algo ? maybe_pbkdf_algo :
"");
337 const size_t pbkdf_iter = (maybe_pbkdf_iterations ? maybe_pbkdf_iterations : 100000);
346 return BOTAN_FFI_VISIT(key, [=](
const auto& k) { *estimate = k.estimated_strength(); });
357#if defined(BOTAN_HAS_HASH_ID)
360 return write_output(pkcs_id, pkcs_id_len, hash_id.data(), hash_id.size());
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
#define BOTAN_PRIVKEY_EXPORT_FLAG_RAW
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_privkey_view_raw(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_pubkey_view_raw(botan_pubkey_t key, botan_view_ctx ctx, botan_view_bin_fn view)
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)