Botan 3.8.1
Crypto and TLS for C&
ffi_pkey.cpp File Reference
#include <botan/ffi.h>
#include <botan/data_src.h>
#include <botan/hash.h>
#include <botan/pk_algs.h>
#include <botan/pk_keys.h>
#include <botan/pkcs8.h>
#include <botan/x509_key.h>
#include <botan/internal/ffi_ec.h>
#include <botan/internal/ffi_oid.h>
#include <botan/internal/ffi_pkey.h>
#include <botan/internal/ffi_rng.h>
#include <botan/internal/ffi_util.h>

Go to the source code of this file.

Functions

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_pkcs_hash_id (const char *hash_name, uint8_t pkcs_id[], size_t *pkcs_id_len)
 
int botan_privkey_algo_name (botan_privkey_t key, char out[], size_t *out_len)
 
int botan_privkey_check_key (botan_privkey_t key, botan_rng_t rng, uint32_t flags)
 
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_destroy (botan_privkey_t key)
 
int botan_privkey_export (botan_privkey_t key, uint8_t out[], size_t *out_len, uint32_t flags)
 
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_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_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_export_pubkey (botan_pubkey_t *pubout, botan_privkey_t key_obj)
 
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_privkey_oid (botan_asn1_oid_t *oid, botan_privkey_t key)
 
int botan_privkey_remaining_operations (botan_privkey_t key, uint64_t *out)
 
int botan_privkey_stateful_operation (botan_privkey_t key, int *out)
 
int botan_privkey_view_der (botan_privkey_t key, botan_view_ctx ctx, botan_view_bin_fn view)
 
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_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_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)
 
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_view_pem (botan_privkey_t key, botan_view_ctx ctx, botan_view_str_fn view)
 
int botan_privkey_view_raw (botan_privkey_t key, botan_view_ctx ctx, botan_view_bin_fn view)
 
int botan_pubkey_algo_name (botan_pubkey_t key, char out[], size_t *out_len)
 
int botan_pubkey_check_key (botan_pubkey_t key, botan_rng_t rng, uint32_t flags)
 
int botan_pubkey_destroy (botan_pubkey_t key)
 
int botan_pubkey_estimated_strength (botan_pubkey_t key, size_t *estimate)
 
int botan_pubkey_export (botan_pubkey_t key, uint8_t out[], size_t *out_len, uint32_t flags)
 
int botan_pubkey_fingerprint (botan_pubkey_t key, const char *hash_fn, uint8_t out[], size_t *out_len)
 
int botan_pubkey_load (botan_pubkey_t *key, const uint8_t bits[], size_t bits_len)
 
int botan_pubkey_oid (botan_asn1_oid_t *oid, botan_pubkey_t key)
 
int botan_pubkey_view_der (botan_pubkey_t key, botan_view_ctx ctx, botan_view_bin_fn view)
 
int botan_pubkey_view_pem (botan_pubkey_t key, botan_view_ctx ctx, botan_view_str_fn view)
 
int botan_pubkey_view_raw (botan_pubkey_t key, botan_view_ctx ctx, botan_view_bin_fn view)
 

Function Documentation

◆ botan_ec_privkey_create()

int botan_ec_privkey_create ( botan_privkey_t * key,
const char * algo_name,
botan_ec_group_t ec_group,
botan_rng_t rng )

Create a new ec private key

Parameters
keythe new object will be placed here
algo_namesomething like "ECDSA" or "ECDH"
ec_groupa (possibly application specific) elliptic curve
rnga random number generator

Definition at line 56 of file ffi_pkey.cpp.

59 {
60 return ffi_guard_thunk(__func__, [=]() -> int {
61 if(key_obj == nullptr) {
63 }
64 *key_obj = nullptr;
65
66 Botan::EC_Group ec_group = safe_get(ec_group_obj);
68 std::unique_ptr<Botan::Private_Key> key(
69 Botan::create_ec_private_key(algo_name ? algo_name : "ECDSA", ec_group, rng));
70
71 if(key) {
72 *key_obj = new botan_privkey_struct(std::move(key));
73 return BOTAN_FFI_SUCCESS;
74 } else {
76 }
77 });
78}
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition ffi.h:136
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:130
@ BOTAN_FFI_SUCCESS
Definition ffi.h:113
T & safe_get(botan_struct< T, M > *p)
Definition ffi_util.h:67
int ffi_guard_thunk(const char *func_name, T thunk)
Definition ffi_util.h:83
std::unique_ptr< Private_Key > create_ec_private_key(std::string_view alg_name, const EC_Group &ec_group, RandomNumberGenerator &rng)
Definition pk_algs.cpp:442

References BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_SUCCESS, Botan::create_ec_private_key(), Botan_FFI::ffi_guard_thunk(), and Botan_FFI::safe_get().

◆ botan_pkcs_hash_id()

int botan_pkcs_hash_id ( const char * hash_name,
uint8_t pkcs_id[],
size_t * pkcs_id_len )

Signature Scheme Utility Functions

Definition at line 438 of file ffi_pkey.cpp.

438 {
439#if defined(BOTAN_HAS_HASH_ID)
440 return ffi_guard_thunk(__func__, [=]() -> int {
441 const std::vector<uint8_t> hash_id = Botan::pkcs_hash_id(hash_name);
442 return write_output(pkcs_id, pkcs_id_len, hash_id.data(), hash_id.size());
443 });
444#else
445 BOTAN_UNUSED(hash_name, pkcs_id, pkcs_id_len);
447#endif
448}
#define BOTAN_UNUSED
Definition assert.h:120
int write_output(uint8_t out[], size_t *out_len, const uint8_t buf[], size_t buf_len)
Definition ffi_util.h:207
std::vector< uint8_t > pkcs_hash_id(std::string_view name)
Definition hash_id.cpp:78

References BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_UNUSED, Botan_FFI::ffi_guard_thunk(), Botan::pkcs_hash_id(), and Botan_FFI::write_output().

◆ botan_privkey_algo_name()

int botan_privkey_algo_name ( botan_privkey_t key,
char out[],
size_t * out_len )

Definition at line 137 of file ffi_pkey.cpp.

137 {
138 return BOTAN_FFI_VISIT(key, [=](const auto& k) { return write_str_output(out, out_len, k.algo_name()); });
139}
#define BOTAN_FFI_VISIT(obj, lambda)
Definition ffi_util.h:145
int write_str_output(uint8_t out[], size_t *out_len, std::string_view str)
Definition ffi_util.h:230

References BOTAN_FFI_VISIT, and Botan_FFI::write_str_output().

◆ botan_privkey_check_key()

int botan_privkey_check_key ( botan_privkey_t key,
botan_rng_t rng,
uint32_t flags )

Definition at line 153 of file ffi_pkey.cpp.

153 {
154 const bool strong = (flags & BOTAN_CHECK_KEY_EXPENSIVE_TESTS);
155 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
156 return (k.check_key(safe_get(rng), strong) == true) ? 0 : BOTAN_FFI_ERROR_INVALID_INPUT;
157 });
158}
#define BOTAN_CHECK_KEY_EXPENSIVE_TESTS
Definition ffi.h:1323
@ BOTAN_FFI_ERROR_INVALID_INPUT
Definition ffi.h:117
Flags flags(Flag flags)
Definition p11.h:838

References BOTAN_CHECK_KEY_EXPENSIVE_TESTS, BOTAN_FFI_ERROR_INVALID_INPUT, BOTAN_FFI_VISIT, and Botan_FFI::safe_get().

◆ botan_privkey_create()

int botan_privkey_create ( botan_privkey_t * key,
const char * algo_name,
const char * algo_params,
botan_rng_t rng )

Create a new private key

Parameters
keythe new object will be placed here
algo_namesomething like "RSA" or "ECDSA"
algo_paramsis specific to the algorithm. For RSA, specifies the modulus bit length. For ECC is the name of the curve.
rnga random number generator

Definition at line 29 of file ffi_pkey.cpp.

32 {
33 return ffi_guard_thunk(__func__, [=]() -> int {
34 if(key_obj == nullptr) {
36 }
37
38 *key_obj = nullptr;
39 if(rng_obj == nullptr) {
41 }
42
44 std::unique_ptr<Botan::Private_Key> key(
45 Botan::create_private_key(algo_name ? algo_name : "RSA", rng, algo_params ? algo_params : ""));
46
47 if(key) {
48 *key_obj = new botan_privkey_struct(std::move(key));
49 return BOTAN_FFI_SUCCESS;
50 } else {
52 }
53 });
54}
std::unique_ptr< Private_Key > create_private_key(std::string_view alg_name, RandomNumberGenerator &rng, std::string_view params, std::string_view provider)
Definition pk_algs.cpp:487

References BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_SUCCESS, Botan::create_private_key(), Botan_FFI::ffi_guard_thunk(), and Botan_FFI::safe_get().

Referenced by botan_privkey_create_dh(), botan_privkey_create_ecdh(), botan_privkey_create_ecdsa(), botan_privkey_create_mceliece(), and botan_privkey_create_rsa().

◆ botan_privkey_destroy()

int botan_privkey_destroy ( botan_privkey_t key)
Returns
0 if success, error if invalid object handle

Definition at line 105 of file ffi_pkey.cpp.

105 {
106 return BOTAN_FFI_CHECKED_DELETE(key);
107}
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition ffi_util.h:164

References BOTAN_FFI_CHECKED_DELETE.

◆ botan_privkey_export()

int botan_privkey_export ( botan_privkey_t key,
uint8_t out[],
size_t * out_len,
uint32_t flags )

On input *out_len is number of bytes in out[] On output *out_len is number of bytes written (or required) If out is not big enough no output is written, *out_len is set and 1 is returned Returns 0 on success and sets If some other error occurs a negative integer is returned.

Definition at line 187 of file ffi_pkey.cpp.

187 {
188 if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_DER) {
189 return copy_view_bin(out, out_len, botan_privkey_view_der, key);
190 } else if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_PEM) {
191 return copy_view_str(out, out_len, botan_privkey_view_pem, key);
192 } else if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_RAW) {
193 return copy_view_bin(out, out_len, botan_privkey_view_raw, key);
194 } else {
196 }
197}
#define BOTAN_PRIVKEY_EXPORT_FLAG_RAW
Definition ffi.h:1395
int botan_privkey_view_der(botan_privkey_t key, botan_view_ctx ctx, botan_view_bin_fn view)
Definition ffi_pkey.cpp:199
int botan_privkey_view_raw(botan_privkey_t key, botan_view_ctx ctx, botan_view_bin_fn view)
Definition ffi_pkey.cpp:209
#define BOTAN_PRIVKEY_EXPORT_FLAG_PEM
Definition ffi.h:1394
#define BOTAN_PRIVKEY_EXPORT_FLAG_DER
Definition ffi.h:1393
@ BOTAN_FFI_ERROR_BAD_FLAG
Definition ffi.h:129
int botan_privkey_view_pem(botan_privkey_t key, botan_view_ctx ctx, botan_view_str_fn view)
Definition ffi_pkey.cpp:204
int copy_view_bin(uint8_t out[], size_t *out_len, Fn fn, Args... args)
Definition ffi_util.h:189
int copy_view_str(uint8_t out[], size_t *out_len, Fn fn, Args... args)
Definition ffi_util.h:197

References BOTAN_FFI_ERROR_BAD_FLAG, BOTAN_PRIVKEY_EXPORT_FLAG_DER, BOTAN_PRIVKEY_EXPORT_FLAG_PEM, BOTAN_PRIVKEY_EXPORT_FLAG_RAW, botan_privkey_view_der(), botan_privkey_view_pem(), botan_privkey_view_raw(), Botan_FFI::copy_view_bin(), and Botan_FFI::copy_view_str().

◆ botan_privkey_export_encrypted()

int botan_privkey_export_encrypted ( botan_privkey_t key,
uint8_t out[],
size_t * out_len,
botan_rng_t rng,
const char * passphrase,
const char * encryption_algo,
uint32_t flags )

Set encryption_algo to NULL or "" to have the library choose a default (recommended)

Definition at line 214 of file ffi_pkey.cpp.

220 {
221 return botan_privkey_export_encrypted_pbkdf_iter(key, out, out_len, rng_obj, pass, 100000, nullptr, nullptr, flags);
222}
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)
Definition ffi_pkey.cpp:301

References botan_privkey_export_encrypted_pbkdf_iter().

◆ botan_privkey_export_encrypted_pbkdf_iter()

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_iterations,
const char * cipher_algo,
const char * pbkdf_algo,
uint32_t flags )

Export a private key using the specified number of iterations.

Definition at line 301 of file ffi_pkey.cpp.

309 {
310 if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_DER) {
311 return copy_view_bin(
312 out, out_len, botan_privkey_view_encrypted_der, key, rng, passphrase, cipher, pbkdf_algo, pbkdf_iter);
313 } else if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_PEM) {
314 return copy_view_str(
315 out, out_len, botan_privkey_view_encrypted_pem, key, rng, passphrase, cipher, pbkdf_algo, pbkdf_iter);
316 } else {
318 }
319}
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)
Definition ffi_pkey.cpp:321
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)
Definition ffi_pkey.cpp:346

References BOTAN_FFI_ERROR_BAD_FLAG, BOTAN_PRIVKEY_EXPORT_FLAG_DER, BOTAN_PRIVKEY_EXPORT_FLAG_PEM, botan_privkey_view_encrypted_der(), botan_privkey_view_encrypted_pem(), Botan_FFI::copy_view_bin(), and Botan_FFI::copy_view_str().

Referenced by botan_privkey_export_encrypted().

◆ botan_privkey_export_encrypted_pbkdf_msec()

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 )

Definition at line 224 of file ffi_pkey.cpp.

233 {
234 if(pbkdf_iters_out) {
235 *pbkdf_iters_out = 0;
236 }
237
238 if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_DER) {
239 return copy_view_bin(
240 out, out_len, botan_privkey_view_encrypted_der_timed, key, rng, passphrase, cipher, pbkdf_hash, pbkdf_msec);
241 } else if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_PEM) {
242 return copy_view_str(
243 out, out_len, botan_privkey_view_encrypted_pem_timed, key, rng, passphrase, cipher, pbkdf_hash, pbkdf_msec);
244 } else {
246 }
247}
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)
Definition ffi_pkey.cpp:275
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)
Definition ffi_pkey.cpp:249

References BOTAN_FFI_ERROR_BAD_FLAG, BOTAN_PRIVKEY_EXPORT_FLAG_DER, BOTAN_PRIVKEY_EXPORT_FLAG_PEM, botan_privkey_view_encrypted_der_timed(), botan_privkey_view_encrypted_pem_timed(), Botan_FFI::copy_view_bin(), and Botan_FFI::copy_view_str().

◆ botan_privkey_export_pubkey()

int botan_privkey_export_pubkey ( botan_pubkey_t * pubout,
botan_privkey_t key_obj )

Definition at line 129 of file ffi_pkey.cpp.

129 {
130 return ffi_guard_thunk(__func__, [=]() -> int {
131 auto public_key = safe_get(key_obj).public_key();
132 *pubout = new botan_pubkey_struct(std::move(public_key));
133 return BOTAN_FFI_SUCCESS;
134 });
135}

References BOTAN_FFI_SUCCESS, Botan_FFI::ffi_guard_thunk(), and Botan_FFI::safe_get().

◆ botan_privkey_load()

int botan_privkey_load ( botan_privkey_t * key,
botan_rng_t rng,
const uint8_t bits[],
size_t len,
const char * password )

Input currently assumed to be PKCS #8 structure; Set password to NULL to indicate no encryption expected Starting in 2.8.0, the rng parameter is unused and may be set to null

Definition at line 80 of file ffi_pkey.cpp.

81 {
82 BOTAN_UNUSED(rng_obj);
83
84 *key = nullptr;
85
86 return ffi_guard_thunk(__func__, [=]() -> int {
87 Botan::DataSource_Memory src(bits, len);
88
89 std::unique_ptr<Botan::Private_Key> pkcs8;
90
91 if(password == nullptr) {
92 pkcs8 = Botan::PKCS8::load_key(src);
93 } else {
94 pkcs8 = Botan::PKCS8::load_key(src, std::string(password));
95 }
96
97 if(pkcs8) {
98 *key = new botan_privkey_struct(std::move(pkcs8));
99 return BOTAN_FFI_SUCCESS;
100 }
102 });
103}
@ BOTAN_FFI_ERROR_UNKNOWN_ERROR
Definition ffi.h:144
std::unique_ptr< Private_Key > load_key(DataSource &source, const std::function< std::string()> &get_pass)
Definition pkcs8.cpp:317

References BOTAN_FFI_ERROR_UNKNOWN_ERROR, BOTAN_FFI_SUCCESS, BOTAN_UNUSED, Botan_FFI::ffi_guard_thunk(), and Botan::PKCS8::load_key().

◆ botan_privkey_oid()

int botan_privkey_oid ( botan_asn1_oid_t * oid,
botan_privkey_t key )

Definition at line 384 of file ffi_pkey.cpp.

384 {
385 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
386 if(oid == nullptr) {
388 }
389
390 auto oid_ptr = std::make_unique<Botan::OID>(k.object_identifier());
391 *oid = new botan_asn1_oid_struct(std::move(oid_ptr));
392
393 return BOTAN_FFI_SUCCESS;
394 });
395}

References BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_SUCCESS, and BOTAN_FFI_VISIT.

◆ botan_privkey_remaining_operations()

int botan_privkey_remaining_operations ( botan_privkey_t key,
uint64_t * out )

Gets information on many operations a (stateful) key has remaining and sets

Parameters
outto that value
Returns
0 on success, a negative value on failure or if the key is not stateful

Definition at line 412 of file ffi_pkey.cpp.

412 {
413 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
414 if(out == nullptr) {
416 }
417
418 if(auto remaining = k.remaining_operations()) {
419 *out = remaining.value();
420 return BOTAN_FFI_SUCCESS;
421 } else {
423 }
424 });
425}
@ BOTAN_FFI_ERROR_NO_VALUE
Definition ffi.h:119

References BOTAN_FFI_ERROR_NO_VALUE, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_SUCCESS, and BOTAN_FFI_VISIT.

◆ botan_privkey_stateful_operation()

int botan_privkey_stateful_operation ( botan_privkey_t key,
int * out )

Checks whether a key is stateful and sets

Parameters
outto 1 if it is, or 0 if the key is not stateful
Returns
0 on success, a negative value on failure

Definition at line 397 of file ffi_pkey.cpp.

397 {
398 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
399 if(out == nullptr) {
401 }
402
403 if(k.stateful_operation()) {
404 *out = 1;
405 } else {
406 *out = 0;
407 }
408 return BOTAN_FFI_SUCCESS;
409 });
410}

References BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_SUCCESS, and BOTAN_FFI_VISIT.

◆ botan_privkey_view_der()

int botan_privkey_view_der ( botan_privkey_t key,
botan_view_ctx ctx,
botan_view_bin_fn view )

View the private key's DER encoding

Definition at line 199 of file ffi_pkey.cpp.

199 {
200 return BOTAN_FFI_VISIT(
201 key, [=](const auto& k) -> int { return invoke_view_callback(view, ctx, Botan::PKCS8::BER_encode(k)); });
202}
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)
Definition pkcs8.cpp:164
int invoke_view_callback(botan_view_bin_fn view, botan_view_ctx ctx, std::span< const uint8_t > buf)
Definition ffi_util.h:166

References Botan::PKCS8::BER_encode(), BOTAN_FFI_VISIT, and Botan_FFI::invoke_view_callback().

Referenced by botan_privkey_export().

◆ botan_privkey_view_encrypted_der()

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 )

View the encryption of a private key (binary DER encoding)

Set cipher_algo, pbkdf_algo to NULL to use defaults Set pbkdf_iterations to 0 to use defaults

Definition at line 321 of file ffi_pkey.cpp.

328 {
329 if(passphrase == nullptr) {
331 }
332
333 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
335
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);
339
340 auto pkcs8 = Botan::PKCS8::BER_encode_encrypted_pbkdf_iter(k, rng, passphrase, pbkdf_iter, cipher, pbkdf_algo);
341
342 return invoke_view_callback(view, ctx, pkcs8);
343 });
344}
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)
Definition pkcs8.cpp:204

References Botan::PKCS8::BER_encode_encrypted_pbkdf_iter(), BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_VISIT, Botan_FFI::invoke_view_callback(), and Botan_FFI::safe_get().

Referenced by botan_privkey_export_encrypted_pbkdf_iter().

◆ botan_privkey_view_encrypted_der_timed()

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 )

View the encryption of a private key (binary DER encoding)

Set cipher_algo, pbkdf_algo to NULL to use defaults

Definition at line 249 of file ffi_pkey.cpp.

256 {
257 if(passphrase == nullptr) {
259 }
260
261 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
262 const std::chrono::milliseconds pbkdf_time(pbkdf_runtime_msec);
264
265 const std::string cipher = (maybe_cipher ? maybe_cipher : "");
266 const std::string pbkdf_algo = (maybe_pbkdf_algo ? maybe_pbkdf_algo : "");
267
268 auto pkcs8 =
269 Botan::PKCS8::BER_encode_encrypted_pbkdf_msec(k, rng, passphrase, pbkdf_time, nullptr, cipher, pbkdf_algo);
270
271 return invoke_view_callback(view, ctx, pkcs8);
272 });
273}
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)
Definition pkcs8.cpp:247

References Botan::PKCS8::BER_encode_encrypted_pbkdf_msec(), BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_VISIT, Botan_FFI::invoke_view_callback(), and Botan_FFI::safe_get().

Referenced by botan_privkey_export_encrypted_pbkdf_msec().

◆ botan_privkey_view_encrypted_pem()

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 )

View the encryption of a private key (PEM encoding)

Set cipher_algo, pbkdf_algo to NULL to use defaults Set pbkdf_iterations to 0 to use defaults

Definition at line 346 of file ffi_pkey.cpp.

353 {
354 if(passphrase == nullptr) {
356 }
357
358 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
360
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);
364
365 auto pkcs8 = Botan::PKCS8::PEM_encode_encrypted_pbkdf_iter(k, rng, passphrase, pbkdf_iter, cipher, pbkdf_algo);
366
367 return invoke_view_callback(view, ctx, pkcs8);
368 });
369}
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)
Definition pkcs8.cpp:234

References BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_VISIT, Botan_FFI::invoke_view_callback(), Botan::PKCS8::PEM_encode_encrypted_pbkdf_iter(), and Botan_FFI::safe_get().

Referenced by botan_privkey_export_encrypted_pbkdf_iter().

◆ botan_privkey_view_encrypted_pem_timed()

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 )

View the encryption of a private key (PEM encoding)

Set cipher_algo, pbkdf_algo to NULL to use defaults

Definition at line 275 of file ffi_pkey.cpp.

282 {
283 if(passphrase == nullptr) {
285 }
286
287 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
288 const std::chrono::milliseconds pbkdf_time(pbkdf_runtime_msec);
290
291 const std::string cipher = (maybe_cipher ? maybe_cipher : "");
292 const std::string pbkdf_algo = (maybe_pbkdf_algo ? maybe_pbkdf_algo : "");
293
294 auto pkcs8 =
295 Botan::PKCS8::PEM_encode_encrypted_pbkdf_msec(k, rng, passphrase, pbkdf_time, nullptr, cipher, pbkdf_algo);
296
297 return invoke_view_callback(view, ctx, pkcs8);
298 });
299}
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)
Definition pkcs8.cpp:281

References BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_VISIT, Botan_FFI::invoke_view_callback(), Botan::PKCS8::PEM_encode_encrypted_pbkdf_msec(), and Botan_FFI::safe_get().

Referenced by botan_privkey_export_encrypted_pbkdf_msec().

◆ botan_privkey_view_pem()

int botan_privkey_view_pem ( botan_privkey_t key,
botan_view_ctx ctx,
botan_view_str_fn view )

View the private key's PEM encoding

Definition at line 204 of file ffi_pkey.cpp.

204 {
205 return BOTAN_FFI_VISIT(
206 key, [=](const auto& k) -> int { return invoke_view_callback(view, ctx, Botan::PKCS8::PEM_encode(k)); });
207}
std::string PEM_encode(const Private_Key &key)
Definition pkcs8.cpp:119

References BOTAN_FFI_VISIT, Botan_FFI::invoke_view_callback(), and Botan::PKCS8::PEM_encode().

Referenced by botan_privkey_export().

◆ botan_privkey_view_raw()

int botan_privkey_view_raw ( botan_privkey_t key,
botan_view_ctx ctx,
botan_view_bin_fn view )

View the private key's raw encoding

Definition at line 209 of file ffi_pkey.cpp.

209 {
210 return BOTAN_FFI_VISIT(
211 key, [=](const auto& k) -> int { return invoke_view_callback(view, ctx, k.raw_private_key_bits()); });
212}

References BOTAN_FFI_VISIT, and Botan_FFI::invoke_view_callback().

Referenced by botan_privkey_export().

◆ botan_pubkey_algo_name()

int botan_pubkey_algo_name ( botan_pubkey_t key,
char out[],
size_t * out_len )

Definition at line 141 of file ffi_pkey.cpp.

141 {
142 return BOTAN_FFI_VISIT(key, [=](const auto& k) { return write_str_output(out, out_len, k.algo_name()); });
143}

References BOTAN_FFI_VISIT, and Botan_FFI::write_str_output().

◆ botan_pubkey_check_key()

int botan_pubkey_check_key ( botan_pubkey_t key,
botan_rng_t rng,
uint32_t flags )

Returns 0 if key is valid, negative if invalid key or some other error

Definition at line 145 of file ffi_pkey.cpp.

145 {
146 const bool strong = (flags & BOTAN_CHECK_KEY_EXPENSIVE_TESTS);
147
148 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
149 return (k.check_key(safe_get(rng), strong) == true) ? 0 : BOTAN_FFI_ERROR_INVALID_INPUT;
150 });
151}

References BOTAN_CHECK_KEY_EXPENSIVE_TESTS, BOTAN_FFI_ERROR_INVALID_INPUT, BOTAN_FFI_VISIT, and Botan_FFI::safe_get().

◆ botan_pubkey_destroy()

int botan_pubkey_destroy ( botan_pubkey_t key)
Returns
0 if success, error if invalid object handle

Definition at line 125 of file ffi_pkey.cpp.

125 {
126 return BOTAN_FFI_CHECKED_DELETE(key);
127}

References BOTAN_FFI_CHECKED_DELETE.

◆ botan_pubkey_estimated_strength()

int botan_pubkey_estimated_strength ( botan_pubkey_t key,
size_t * estimate )

Definition at line 427 of file ffi_pkey.cpp.

427 {
428 return BOTAN_FFI_VISIT(key, [=](const auto& k) { *estimate = k.estimated_strength(); });
429}

References BOTAN_FFI_VISIT.

◆ botan_pubkey_export()

int botan_pubkey_export ( botan_pubkey_t key,
uint8_t out[],
size_t * out_len,
uint32_t flags )

Definition at line 160 of file ffi_pkey.cpp.

160 {
161 if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_DER) {
162 return copy_view_bin(out, out_len, botan_pubkey_view_der, key);
163 } else if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_PEM) {
164 return copy_view_str(out, out_len, botan_pubkey_view_pem, key);
165 } else if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_RAW) {
166 return copy_view_bin(out, out_len, botan_pubkey_view_raw, key);
167 } else {
169 }
170}
int botan_pubkey_view_pem(botan_pubkey_t key, botan_view_ctx ctx, botan_view_str_fn view)
Definition ffi_pkey.cpp:177
int botan_pubkey_view_der(botan_pubkey_t key, botan_view_ctx ctx, botan_view_bin_fn view)
Definition ffi_pkey.cpp:172
int botan_pubkey_view_raw(botan_pubkey_t key, botan_view_ctx ctx, botan_view_bin_fn view)
Definition ffi_pkey.cpp:182

References BOTAN_FFI_ERROR_BAD_FLAG, BOTAN_PRIVKEY_EXPORT_FLAG_DER, BOTAN_PRIVKEY_EXPORT_FLAG_PEM, BOTAN_PRIVKEY_EXPORT_FLAG_RAW, botan_pubkey_view_der(), botan_pubkey_view_pem(), botan_pubkey_view_raw(), Botan_FFI::copy_view_bin(), and Botan_FFI::copy_view_str().

◆ botan_pubkey_fingerprint()

int botan_pubkey_fingerprint ( botan_pubkey_t key,
const char * hash_fn,
uint8_t out[],
size_t * out_len )

Definition at line 431 of file ffi_pkey.cpp.

431 {
432 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
433 auto h = Botan::HashFunction::create_or_throw(hash_fn);
434 return write_vec_output(out, out_len, h->process(k.public_key_bits()));
435 });
436}
static std::unique_ptr< HashFunction > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition hash.cpp:298
int write_vec_output(uint8_t out[], size_t *out_len, std::span< const uint8_t > buf)
Definition ffi_util.h:226

References BOTAN_FFI_VISIT, Botan::HashFunction::create_or_throw(), and Botan_FFI::write_vec_output().

◆ botan_pubkey_load()

int botan_pubkey_load ( botan_pubkey_t * key,
const uint8_t bits[],
size_t bits_len )

Definition at line 109 of file ffi_pkey.cpp.

109 {
110 *key = nullptr;
111
112 return ffi_guard_thunk(__func__, [=]() -> int {
113 Botan::DataSource_Memory src(bits, bits_len);
114 std::unique_ptr<Botan::Public_Key> pubkey(Botan::X509::load_key(src));
115
116 if(pubkey == nullptr) {
118 }
119
120 *key = new botan_pubkey_struct(std::move(pubkey));
121 return BOTAN_FFI_SUCCESS;
122 });
123}
std::unique_ptr< Public_Key > load_key(DataSource &source)
Definition x509_key.cpp:28

References BOTAN_FFI_ERROR_UNKNOWN_ERROR, BOTAN_FFI_SUCCESS, Botan_FFI::ffi_guard_thunk(), and Botan::X509::load_key().

◆ botan_pubkey_oid()

int botan_pubkey_oid ( botan_asn1_oid_t * oid,
botan_pubkey_t key )

Definition at line 371 of file ffi_pkey.cpp.

371 {
372 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
373 if(oid == nullptr) {
375 }
376
377 auto oid_ptr = std::make_unique<Botan::OID>(k.object_identifier());
378 *oid = new botan_asn1_oid_struct(std::move(oid_ptr));
379
380 return BOTAN_FFI_SUCCESS;
381 });
382}

References BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_SUCCESS, and BOTAN_FFI_VISIT.

◆ botan_pubkey_view_der()

int botan_pubkey_view_der ( botan_pubkey_t key,
botan_view_ctx ctx,
botan_view_bin_fn view )

View the public key's DER encoding

Definition at line 172 of file ffi_pkey.cpp.

172 {
173 return BOTAN_FFI_VISIT(
174 key, [=](const auto& k) -> int { return invoke_view_callback(view, ctx, k.subject_public_key()); });
175}

References BOTAN_FFI_VISIT, and Botan_FFI::invoke_view_callback().

Referenced by botan_pubkey_export().

◆ botan_pubkey_view_pem()

int botan_pubkey_view_pem ( botan_pubkey_t key,
botan_view_ctx ctx,
botan_view_str_fn view )

View the public key's PEM encoding

Definition at line 177 of file ffi_pkey.cpp.

177 {
178 return BOTAN_FFI_VISIT(
179 key, [=](const auto& k) -> int { return invoke_view_callback(view, ctx, Botan::X509::PEM_encode(k)); });
180}
std::string PEM_encode(const Public_Key &key)
Definition x509_key.cpp:21

References BOTAN_FFI_VISIT, Botan_FFI::invoke_view_callback(), and Botan::X509::PEM_encode().

Referenced by botan_pubkey_export().

◆ botan_pubkey_view_raw()

int botan_pubkey_view_raw ( botan_pubkey_t key,
botan_view_ctx ctx,
botan_view_bin_fn view )

View the public key's raw encoding

Definition at line 182 of file ffi_pkey.cpp.

182 {
183 return BOTAN_FFI_VISIT(
184 key, [=](const auto& k) -> int { return invoke_view_callback(view, ctx, k.raw_public_key_bits()); });
185}

References BOTAN_FFI_VISIT, and Botan_FFI::invoke_view_callback().

Referenced by botan_pubkey_export().