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_ec.h>
16#include <botan/internal/ffi_oid.h>
17#include <botan/internal/ffi_pkey.h>
18#include <botan/internal/ffi_rng.h>
19#include <botan/internal/ffi_util.h>
21#if defined(BOTAN_HAS_HASH_ID)
22 #include <botan/internal/hash_id.h>
30 const char* algo_name,
31 const char* algo_params,
34 if(key_obj ==
nullptr) {
39 if(rng_obj ==
nullptr) {
44 std::unique_ptr<Botan::Private_Key> key(
48 *key_obj =
new botan_privkey_struct(std::move(key));
57 const char* algo_name,
61 if(key_obj ==
nullptr) {
68 std::unique_ptr<Botan::Private_Key> key(
72 *key_obj =
new botan_privkey_struct(std::move(key));
89 std::unique_ptr<Botan::Private_Key> pkcs8;
91 if(password ==
nullptr) {
98 *key =
new botan_privkey_struct(std::move(pkcs8));
116 if(pubkey ==
nullptr) {
120 *key =
new botan_pubkey_struct(std::move(pubkey));
131 auto public_key =
safe_get(key_obj).public_key();
132 *pubout =
new botan_pubkey_struct(std::move(public_key));
174 key, [=](
const auto& k) ->
int {
return invoke_view_callback(view, ctx, k.subject_public_key()); });
184 key, [=](
const auto& k) ->
int {
return invoke_view_callback(view, ctx, k.raw_public_key_bits()); });
211 key, [=](
const auto& k) ->
int {
return invoke_view_callback(view, ctx, k.raw_private_key_bits()); });
228 const char* passphrase,
230 size_t* pbkdf_iters_out,
232 const char* pbkdf_hash,
234 if(pbkdf_iters_out) {
235 *pbkdf_iters_out = 0;
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 :
"");
277 const char* passphrase,
278 const char* maybe_cipher,
279 const char* maybe_pbkdf_algo,
280 size_t pbkdf_runtime_msec,
283 if(passphrase ==
nullptr) {
288 const std::chrono::milliseconds pbkdf_time(pbkdf_runtime_msec);
291 const std::string cipher = (maybe_cipher ? maybe_cipher :
"");
292 const std::string pbkdf_algo = (maybe_pbkdf_algo ? maybe_pbkdf_algo :
"");
305 const char* passphrase,
308 const char* pbkdf_algo,
323 const char* passphrase,
324 const char* maybe_cipher,
325 const char* maybe_pbkdf_algo,
326 size_t maybe_pbkdf_iterations,
329 if(passphrase ==
nullptr) {
336 const std::string cipher = (maybe_cipher ? maybe_cipher :
"");
337 const std::string pbkdf_algo = (maybe_pbkdf_algo ? maybe_pbkdf_algo :
"");
338 const size_t pbkdf_iter = (maybe_pbkdf_iterations ? maybe_pbkdf_iterations : 100000);
348 const char* passphrase,
349 const char* maybe_cipher,
350 const char* maybe_pbkdf_algo,
351 size_t maybe_pbkdf_iterations,
354 if(passphrase ==
nullptr) {
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);
377 auto oid_ptr = std::make_unique<Botan::OID>(k.object_identifier());
378 *oid =
new botan_asn1_oid_struct(std::move(oid_ptr));
390 auto oid_ptr = std::make_unique<Botan::OID>(k.object_identifier());
391 *oid =
new botan_asn1_oid_struct(std::move(oid_ptr));
403 if(k.stateful_operation()) {
418 if(
auto remaining = k.remaining_operations()) {
419 *out = remaining.value();
428 return BOTAN_FFI_VISIT(key, [=](
const auto& k) { *estimate = k.estimated_strength(); });
439#if defined(BOTAN_HAS_HASH_ID)
442 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
int botan_privkey_view_encrypted_pem_timed(botan_privkey_t key, botan_rng_t rng, const char *passphrase, const char *cipher_algo, const char *pbkdf_algo, size_t pbkdf_runtime_msec, botan_view_ctx ctx, botan_view_str_fn view)
struct botan_asn1_oid_struct * botan_asn1_oid_t
struct botan_privkey_struct * botan_privkey_t
int botan_privkey_view_encrypted_der(botan_privkey_t key, botan_rng_t rng, const char *passphrase, const char *cipher_algo, const char *pbkdf_algo, size_t pbkdf_iterations, botan_view_ctx ctx, botan_view_bin_fn view)
#define BOTAN_PRIVKEY_EXPORT_FLAG_RAW
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_privkey_view_encrypted_der_timed(botan_privkey_t key, botan_rng_t rng, const char *passphrase, const char *cipher_algo, const char *pbkdf_algo, size_t pbkdf_runtime_msec, 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)
struct botan_ec_group_struct * botan_ec_group_t
int botan_privkey_view_encrypted_pem(botan_privkey_t key, botan_rng_t rng, const char *passphrase, const char *cipher_algo, const char *pbkdf_algo, size_t pbkdf_iterations, botan_view_ctx ctx, botan_view_str_fn view)
int(* botan_view_bin_fn)(botan_view_ctx view_ctx, const uint8_t *data, size_t len)
#define BOTAN_PRIVKEY_EXPORT_FLAG_PEM
int botan_pubkey_view_raw(botan_pubkey_t key, botan_view_ctx ctx, botan_view_bin_fn view)
#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
@ BOTAN_FFI_ERROR_NO_VALUE
int(* botan_view_str_fn)(botan_view_ctx view_ctx, const char *str, size_t len)
int botan_privkey_view_pem(botan_privkey_t key, botan_view_ctx ctx, botan_view_str_fn view)
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_pubkey_oid(botan_asn1_oid_t *oid, botan_pubkey_t key)
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_stateful_operation(botan_privkey_t key, int *out)
int botan_privkey_view_raw(botan_privkey_t key, botan_view_ctx ctx, botan_view_bin_fn view)
int botan_ec_privkey_create(botan_privkey_t *key_obj, const char *algo_name, botan_ec_group_t ec_group_obj, botan_rng_t rng_obj)
int botan_pubkey_view_der(botan_pubkey_t key, botan_view_ctx ctx, botan_view_bin_fn view)
int botan_privkey_remaining_operations(botan_privkey_t key, uint64_t *out)
int botan_pubkey_export(botan_pubkey_t key, uint8_t out[], size_t *out_len, uint32_t flags)
int botan_privkey_oid(botan_asn1_oid_t *oid, botan_privkey_t key)
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::unique_ptr< Public_Key > load_key(DataSource &source)
std::string PEM_encode(const Public_Key &key)
int invoke_view_callback(botan_view_bin_fn view, botan_view_ctx ctx, std::span< const uint8_t > buf)
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 copy_view_str(uint8_t out[], size_t *out_len, Fn fn, Args... args)
int ffi_guard_thunk(const char *func_name, T thunk)
int write_vec_output(uint8_t out[], size_t *out_len, std::span< const uint8_t > 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)
std::unique_ptr< Private_Key > create_ec_private_key(std::string_view alg_name, const EC_Group &ec_group, RandomNumberGenerator &rng)