9#include <botan/assert.h>
10#include <botan/data_src.h>
11#include <botan/hash.h>
12#include <botan/pk_algs.h>
13#include <botan/pk_keys.h>
14#include <botan/pkcs8.h>
15#include <botan/x509_key.h>
16#include <botan/internal/ffi_ec.h>
17#include <botan/internal/ffi_oid.h>
18#include <botan/internal/ffi_pkey.h>
19#include <botan/internal/ffi_rng.h>
20#include <botan/internal/ffi_util.h>
22#if defined(BOTAN_HAS_HASH_ID)
23 #include <botan/internal/hash_id.h>
31 const char* algo_name,
32 const char* algo_params,
35 if(algo_name ==
nullptr) {
40 if(key_obj ==
nullptr) {
45 if(rng_obj ==
nullptr) {
49 const std::string params(algo_params !=
nullptr ? algo_params :
"");
62 const char* algo_name,
66 if(algo_name ==
nullptr) {
71 if(key_obj ==
nullptr) {
97 if(bits ==
nullptr && len > 0) {
104 std::unique_ptr<Botan::Private_Key> pkcs8;
106 if(password ==
nullptr) {
131 if(bits ==
nullptr && bits_len > 0) {
139 if(pubkey ==
nullptr) {
153 if(pubout ==
nullptr) {
157 auto public_key =
safe_get(key_obj).public_key();
199 key, [=](
const auto& k) ->
int {
return invoke_view_callback(view, ctx, k.subject_public_key()); });
209 key, [=](
const auto& k) ->
int {
return invoke_view_callback(view, ctx, k.raw_public_key_bits()); });
236 key, [=](
const auto& k) ->
int {
return invoke_view_callback(view, ctx, k.raw_private_key_bits()); });
253 const char* passphrase,
255 size_t* pbkdf_iters_out,
257 const char* pbkdf_hash,
259 if(pbkdf_iters_out !=
nullptr) {
260 *pbkdf_iters_out = 0;
276 const char* passphrase,
277 const char* maybe_cipher,
278 const char* maybe_pbkdf_algo,
279 size_t pbkdf_runtime_msec,
282 if(passphrase ==
nullptr) {
287 const std::chrono::milliseconds pbkdf_time(pbkdf_runtime_msec);
290 const std::string cipher = (maybe_cipher ? maybe_cipher :
"");
291 const std::string pbkdf_algo = (maybe_pbkdf_algo ? maybe_pbkdf_algo :
"");
302 const char* passphrase,
303 const char* maybe_cipher,
304 const char* maybe_pbkdf_algo,
305 size_t pbkdf_runtime_msec,
308 if(passphrase ==
nullptr) {
313 const std::chrono::milliseconds pbkdf_time(pbkdf_runtime_msec);
316 const std::string cipher = (maybe_cipher ? maybe_cipher :
"");
317 const std::string pbkdf_algo = (maybe_pbkdf_algo ? maybe_pbkdf_algo :
"");
330 const char* passphrase,
333 const char* pbkdf_algo,
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);
373 const char* passphrase,
374 const char* maybe_cipher,
375 const char* maybe_pbkdf_algo,
376 size_t maybe_pbkdf_iterations,
379 if(passphrase ==
nullptr) {
386 const std::string cipher = (maybe_cipher ? maybe_cipher :
"");
387 const std::string pbkdf_algo = (maybe_pbkdf_algo ? maybe_pbkdf_algo :
"");
388 const size_t pbkdf_iter = (maybe_pbkdf_iterations ? maybe_pbkdf_iterations : 100000);
402 auto oid_ptr = std::make_unique<Botan::OID>(k.object_identifier());
415 auto oid_ptr = std::make_unique<Botan::OID>(k.object_identifier());
428 if(k.stateful_operation()) {
443 if(
auto remaining = k.remaining_operations()) {
444 *out = remaining.value();
453 if(estimate ==
nullptr) {
456 return BOTAN_FFI_VISIT(key, [=](
const auto& k) { *estimate = k.estimated_strength(); });
460 if(hash_fn ==
nullptr) {
470 if(hash_name ==
nullptr) {
473#if defined(BOTAN_HAS_HASH_ID)
476 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::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)
T & safe_get(botan_struct< T, M > *p)
BOTAN_FFI_ERROR ffi_new_object(T *obj, Args &&... args)
int copy_view_str(uint8_t out[], size_t *out_len, Fn fn, Args... args)
int write_output(T out[], size_t *out_len, const T buf[], size_t buf_len)
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_str_output(char out[], size_t *out_len, const std::string &str)
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)