Botan 3.4.0
Crypto and TLS for C&
Functions
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_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_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_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_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_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)
 

Function Documentation

◆ 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 342 of file ffi_pkey.cpp.

342 {
343#if defined(BOTAN_HAS_HASH_ID)
344 return ffi_guard_thunk(__func__, [=]() -> int {
345 const std::vector<uint8_t> hash_id = Botan::pkcs_hash_id(hash_name);
346 return write_output(pkcs_id, pkcs_id_len, hash_id.data(), hash_id.size());
347 });
348#else
349 BOTAN_UNUSED(hash_name, pkcs_id, pkcs_id_len);
351#endif
352}
#define BOTAN_UNUSED
Definition assert.h:118
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition ffi.h:124
int ffi_guard_thunk(const char *func_name, const std::function< int()> &thunk)
Definition ffi.cpp:116
int write_output(uint8_t out[], size_t *out_len, const uint8_t buf[], size_t buf_len)
Definition ffi_util.h:181
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 111 of file ffi_pkey.cpp.

111 {
112 return BOTAN_FFI_VISIT(key, [=](const auto& k) { return write_str_output(out, out_len, k.algo_name()); });
113}
#define BOTAN_FFI_VISIT(obj, lambda)
Definition ffi_util.h:124
int write_str_output(uint8_t out[], size_t *out_len, std::string_view str)
Definition ffi_util.h:205

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 127 of file ffi_pkey.cpp.

127 {
128 const bool strong = (flags & BOTAN_CHECK_KEY_EXPENSIVE_TESTS);
129 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
130 return (k.check_key(safe_get(rng), strong) == true) ? 0 : BOTAN_FFI_ERROR_INVALID_INPUT;
131 });
132}
#define BOTAN_CHECK_KEY_EXPENSIVE_TESTS
Definition ffi.h:1079
@ BOTAN_FFI_ERROR_INVALID_INPUT
Definition ffi.h:106
Flags flags(Flag flags)
Definition p11.h:836
T & safe_get(botan_struct< T, M > *p)
Definition ffi_util.h:63

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 27 of file ffi_pkey.cpp.

30 {
31 return ffi_guard_thunk(__func__, [=]() -> int {
32 if(key_obj == nullptr) {
34 }
35
36 *key_obj = nullptr;
37 if(rng_obj == nullptr) {
39 }
40
42 std::unique_ptr<Botan::Private_Key> key(
43 Botan::create_private_key(algo_name ? algo_name : "RSA", rng, algo_params ? algo_params : ""));
44
45 if(key) {
46 *key_obj = new botan_privkey_struct(std::move(key));
47 return BOTAN_FFI_SUCCESS;
48 } else {
50 }
51 });
52}
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:118
@ BOTAN_FFI_SUCCESS
Definition ffi.h:103
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:406

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 79 of file ffi_pkey.cpp.

79 {
80 return BOTAN_FFI_CHECKED_DELETE(key);
81}
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition ffi_util.h:143

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 154 of file ffi_pkey.cpp.

154 {
155 if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_DER) {
156 return copy_view_bin(out, out_len, botan_privkey_view_der, key);
157 } else if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_PEM) {
158 return copy_view_str(out, out_len, botan_privkey_view_pem, key);
159 } else {
161 }
162}
#define BOTAN_PRIVKEY_EXPORT_FLAG_PEM
Definition ffi.h:1150
#define BOTAN_PRIVKEY_EXPORT_FLAG_DER
Definition ffi.h:1149
@ BOTAN_FFI_ERROR_BAD_FLAG
Definition ffi.h:117
int botan_privkey_view_der(botan_privkey_t key, botan_view_ctx ctx, botan_view_bin_fn view)
Definition ffi_pkey.cpp:164
int botan_privkey_view_pem(botan_privkey_t key, botan_view_ctx ctx, botan_view_str_fn view)
Definition ffi_pkey.cpp:169
int copy_view_bin(uint8_t out[], size_t *out_len, Fn fn, Args... args)
Definition ffi_util.h:163
int copy_view_str(uint8_t out[], size_t *out_len, Fn fn, Args... args)
Definition ffi_util.h:171

References BOTAN_FFI_ERROR_BAD_FLAG, BOTAN_PRIVKEY_EXPORT_FLAG_DER, BOTAN_PRIVKEY_EXPORT_FLAG_PEM, botan_privkey_view_der(), botan_privkey_view_pem(), 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 174 of file ffi_pkey.cpp.

180 {
181 return botan_privkey_export_encrypted_pbkdf_iter(key, out, out_len, rng_obj, pass, 100000, nullptr, nullptr, flags);
182}
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:261

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 261 of file ffi_pkey.cpp.

269 {
270 if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_DER) {
271 return copy_view_bin(
272 out, out_len, botan_privkey_view_encrypted_der, key, rng, passphrase, cipher, pbkdf_algo, pbkdf_iter);
273 } else if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_PEM) {
274 return copy_view_str(
275 out, out_len, botan_privkey_view_encrypted_pem, key, rng, passphrase, cipher, pbkdf_algo, pbkdf_iter);
276 } else {
278 }
279}
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)
Definition ffi_pkey.cpp:281
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)
Definition ffi_pkey.cpp:306

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 184 of file ffi_pkey.cpp.

193 {
194 if(pbkdf_iters_out) {
195 *pbkdf_iters_out = 0;
196 }
197
198 if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_DER) {
199 return copy_view_bin(
200 out, out_len, botan_privkey_view_encrypted_der_timed, key, rng, passphrase, cipher, pbkdf_hash, pbkdf_msec);
201 } else if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_PEM) {
202 return copy_view_str(
203 out, out_len, botan_privkey_view_encrypted_pem_timed, key, rng, passphrase, cipher, pbkdf_hash, pbkdf_msec);
204 } else {
206 }
207}
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)
Definition ffi_pkey.cpp:209
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)
Definition ffi_pkey.cpp:235

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 103 of file ffi_pkey.cpp.

103 {
104 return ffi_guard_thunk(__func__, [=]() -> int {
105 auto public_key = safe_get(key_obj).public_key();
106 *pubout = new botan_pubkey_struct(std::move(public_key));
107 return BOTAN_FFI_SUCCESS;
108 });
109}

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 54 of file ffi_pkey.cpp.

55 {
56 BOTAN_UNUSED(rng_obj);
57
58 *key = nullptr;
59
60 return ffi_guard_thunk(__func__, [=]() -> int {
61 Botan::DataSource_Memory src(bits, len);
62
63 std::unique_ptr<Botan::Private_Key> pkcs8;
64
65 if(password == nullptr) {
66 pkcs8 = Botan::PKCS8::load_key(src);
67 } else {
68 pkcs8 = Botan::PKCS8::load_key(src, std::string(password));
69 }
70
71 if(pkcs8) {
72 *key = new botan_privkey_struct(std::move(pkcs8));
73 return BOTAN_FFI_SUCCESS;
74 }
76 });
77}
@ BOTAN_FFI_ERROR_UNKNOWN_ERROR
Definition ffi.h:131
std::unique_ptr< Private_Key > load_key(DataSource &source, const std::function< std::string()> &get_pass)
Definition pkcs8.cpp:316

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

◆ 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 164 of file ffi_pkey.cpp.

164 {
165 return BOTAN_FFI_VISIT(
166 key, [=](const auto& k) -> int { return invoke_view_callback(view, ctx, Botan::PKCS8::BER_encode(k)); });
167}
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:163
int invoke_view_callback(botan_view_bin_fn view, botan_view_ctx ctx, const std::vector< uint8_t, Alloc > &buf)
Definition ffi_util.h:146

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 281 of file ffi_pkey.cpp.

288 {
289 if(passphrase == nullptr) {
291 }
292
293 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
295
296 const std::string cipher = (maybe_cipher ? maybe_cipher : "");
297 const std::string pbkdf_algo = (maybe_pbkdf_algo ? maybe_pbkdf_algo : "");
298 const size_t pbkdf_iter = (maybe_pbkdf_iterations ? maybe_pbkdf_iterations : 100000);
299
300 auto pkcs8 = Botan::PKCS8::BER_encode_encrypted_pbkdf_iter(k, rng, passphrase, pbkdf_iter, cipher, pbkdf_algo);
301
302 return invoke_view_callback(view, ctx, pkcs8);
303 });
304}
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:203

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 209 of file ffi_pkey.cpp.

216 {
217 if(passphrase == nullptr) {
219 }
220
221 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
222 const std::chrono::milliseconds pbkdf_time(pbkdf_runtime_msec);
224
225 const std::string cipher = (maybe_cipher ? maybe_cipher : "");
226 const std::string pbkdf_algo = (maybe_pbkdf_algo ? maybe_pbkdf_algo : "");
227
228 auto pkcs8 =
229 Botan::PKCS8::BER_encode_encrypted_pbkdf_msec(k, rng, passphrase, pbkdf_time, nullptr, cipher, pbkdf_algo);
230
231 return invoke_view_callback(view, ctx, pkcs8);
232 });
233}
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:246

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 306 of file ffi_pkey.cpp.

313 {
314 if(passphrase == nullptr) {
316 }
317
318 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
320
321 const std::string cipher = (maybe_cipher ? maybe_cipher : "");
322 const std::string pbkdf_algo = (maybe_pbkdf_algo ? maybe_pbkdf_algo : "");
323 const size_t pbkdf_iter = (maybe_pbkdf_iterations ? maybe_pbkdf_iterations : 100000);
324
325 auto pkcs8 = Botan::PKCS8::PEM_encode_encrypted_pbkdf_iter(k, rng, passphrase, pbkdf_iter, cipher, pbkdf_algo);
326
327 return invoke_view_callback(view, ctx, pkcs8);
328 });
329}
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:233

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 235 of file ffi_pkey.cpp.

242 {
243 if(passphrase == nullptr) {
245 }
246
247 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
248 const std::chrono::milliseconds pbkdf_time(pbkdf_runtime_msec);
250
251 const std::string cipher = (maybe_cipher ? maybe_cipher : "");
252 const std::string pbkdf_algo = (maybe_pbkdf_algo ? maybe_pbkdf_algo : "");
253
254 auto pkcs8 =
255 Botan::PKCS8::PEM_encode_encrypted_pbkdf_msec(k, rng, passphrase, pbkdf_time, nullptr, cipher, pbkdf_algo);
256
257 return invoke_view_callback(view, ctx, pkcs8);
258 });
259}
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:280

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 169 of file ffi_pkey.cpp.

169 {
170 return BOTAN_FFI_VISIT(
171 key, [=](const auto& k) -> int { return invoke_view_callback(view, ctx, Botan::PKCS8::PEM_encode(k)); });
172}
std::string PEM_encode(const Private_Key &key)
Definition pkcs8.cpp:118

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

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 115 of file ffi_pkey.cpp.

115 {
116 return BOTAN_FFI_VISIT(key, [=](const auto& k) { return write_str_output(out, out_len, k.algo_name()); });
117}

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 119 of file ffi_pkey.cpp.

119 {
120 const bool strong = (flags & BOTAN_CHECK_KEY_EXPENSIVE_TESTS);
121
122 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
123 return (k.check_key(safe_get(rng), strong) == true) ? 0 : BOTAN_FFI_ERROR_INVALID_INPUT;
124 });
125}

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 99 of file ffi_pkey.cpp.

99 {
100 return BOTAN_FFI_CHECKED_DELETE(key);
101}

References BOTAN_FFI_CHECKED_DELETE.

◆ botan_pubkey_estimated_strength()

int botan_pubkey_estimated_strength ( botan_pubkey_t key,
size_t * estimate )

Definition at line 331 of file ffi_pkey.cpp.

331 {
332 return BOTAN_FFI_VISIT(key, [=](const auto& k) { *estimate = k.estimated_strength(); });
333}

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 134 of file ffi_pkey.cpp.

134 {
135 if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_DER) {
136 return copy_view_bin(out, out_len, botan_pubkey_view_der, key);
137 } else if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_PEM) {
138 return copy_view_str(out, out_len, botan_pubkey_view_pem, key);
139 } else {
141 }
142}
int botan_pubkey_view_pem(botan_pubkey_t key, botan_view_ctx ctx, botan_view_str_fn view)
Definition ffi_pkey.cpp:149
int botan_pubkey_view_der(botan_pubkey_t key, botan_view_ctx ctx, botan_view_bin_fn view)
Definition ffi_pkey.cpp:144

References BOTAN_FFI_ERROR_BAD_FLAG, BOTAN_PRIVKEY_EXPORT_FLAG_DER, BOTAN_PRIVKEY_EXPORT_FLAG_PEM, botan_pubkey_view_der(), botan_pubkey_view_pem(), 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 335 of file ffi_pkey.cpp.

335 {
336 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
337 auto h = Botan::HashFunction::create_or_throw(hash_fn);
338 return write_vec_output(out, out_len, h->process(k.public_key_bits()));
339 });
340}
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, const std::vector< uint8_t, Alloc > &buf)
Definition ffi_util.h:201

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 83 of file ffi_pkey.cpp.

83 {
84 *key = nullptr;
85
86 return ffi_guard_thunk(__func__, [=]() -> int {
87 Botan::DataSource_Memory src(bits, bits_len);
88 std::unique_ptr<Botan::Public_Key> pubkey(Botan::X509::load_key(src));
89
90 if(pubkey == nullptr) {
92 }
93
94 *key = new botan_pubkey_struct(std::move(pubkey));
95 return BOTAN_FFI_SUCCESS;
96 });
97}
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_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 144 of file ffi_pkey.cpp.

144 {
145 return BOTAN_FFI_VISIT(
146 key, [=](const auto& k) -> int { return invoke_view_callback(view, ctx, Botan::X509::BER_encode(k)); });
147}
std::vector< uint8_t > BER_encode(const Public_Key &key)
Definition x509_key.h:23

References Botan::X509::BER_encode(), 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 149 of file ffi_pkey.cpp.

149 {
150 return BOTAN_FFI_VISIT(
151 key, [=](const auto& k) -> int { return invoke_view_callback(view, ctx, Botan::X509::PEM_encode(k)); });
152}
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().