Botan 3.0.0
Crypto and TLS for C&
Functions
ffi_pkey.cpp File Reference
#include <botan/ffi.h>
#include <botan/internal/ffi_util.h>
#include <botan/internal/ffi_pkey.h>
#include <botan/internal/ffi_rng.h>
#include <botan/data_src.h>
#include <botan/hash.h>
#include <botan/pkcs8.h>
#include <botan/pk_keys.h>
#include <botan/x509_key.h>
#include <botan/pk_algs.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 389 of file ffi_pkey.cpp.

390 {
391#if defined(BOTAN_HAS_HASH_ID)
392 return ffi_guard_thunk(__func__, [=]() -> int {
393 const std::vector<uint8_t> hash_id = Botan::pkcs_hash_id(hash_name);
394 return write_output(pkcs_id, pkcs_id_len, hash_id.data(), hash_id.size());
395 });
396#else
397 BOTAN_UNUSED(hash_name, pkcs_id, pkcs_id_len);
399#endif
400 }
#define BOTAN_UNUSED(...)
Definition: assert.h:141
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition: ffi.h:91
int ffi_guard_thunk(const char *func_name, const std::function< int()> &thunk)
Definition: ffi.cpp:120
int write_output(uint8_t out[], size_t *out_len, const uint8_t buf[], size_t buf_len)
Definition: ffi_util.h:190
std::vector< uint8_t > pkcs_hash_id(std::string_view name)
Definition: hash_id.cpp:73

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

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

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

144 {
145 const bool strong = (flags & BOTAN_CHECK_KEY_EXPENSIVE_TESTS);
146 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
147 return (k.check_key(safe_get(rng), strong) == true) ? 0 : BOTAN_FFI_ERROR_INVALID_INPUT;
148 });
149 }
#define BOTAN_CHECK_KEY_EXPENSIVE_TESTS
Definition: ffi.h:1043
@ BOTAN_FFI_ERROR_INVALID_INPUT
Definition: ffi.h:73
Flags flags(Flag flags)
Definition: p11.h:860
T & safe_get(botan_struct< T, M > *p)
Definition: ffi_util.h:69

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

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

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

89 {
90 return BOTAN_FFI_CHECKED_DELETE(key);
91 }
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition: ffi_util.h:145

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

178 {
180 return copy_view_bin(out, out_len, botan_privkey_view_der, key);
181 else if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_PEM)
182 return copy_view_str(out, out_len, botan_privkey_view_pem, key);
183 else
185 }
#define BOTAN_PRIVKEY_EXPORT_FLAG_PEM
Definition: ffi.h:1123
#define BOTAN_PRIVKEY_EXPORT_FLAG_DER
Definition: ffi.h:1122
@ BOTAN_FFI_ERROR_BAD_FLAG
Definition: ffi.h:84
int botan_privkey_view_der(botan_privkey_t key, botan_view_ctx ctx, botan_view_bin_fn view)
Definition: ffi_pkey.cpp:187
int botan_privkey_view_pem(botan_privkey_t key, botan_view_ctx ctx, botan_view_str_fn view)
Definition: ffi_pkey.cpp:195
int copy_view_bin(uint8_t out[], size_t *out_len, Fn fn, Args... args)
Definition: ffi_util.h:171
int copy_view_str(uint8_t out[], size_t *out_len, Fn fn, Args... args)
Definition: ffi_util.h:180

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

209 {
210 return botan_privkey_export_encrypted_pbkdf_iter(key, out, out_len, rng_obj, pass, 100000, nullptr, nullptr, flags);
211 }
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:298

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

306 {
308 {
310 key, rng, passphrase, cipher, pbkdf_algo, pbkdf_iter);
311 }
312 else if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_PEM)
313 {
315 key, rng, passphrase, cipher, pbkdf_algo, pbkdf_iter);
316 }
317 else
318 {
320 }
321 }
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:323
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:349

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

222 {
223 if(pbkdf_iters_out)
224 *pbkdf_iters_out = 0;
225
227 {
229 key, rng, passphrase, cipher, pbkdf_hash, pbkdf_msec);
230 }
231 else if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_PEM)
232 {
234 key, rng, passphrase, cipher, pbkdf_hash, pbkdf_msec);
235 }
236 else
237 {
239 }
240 }
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:242
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:270

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

116 {
117 return ffi_guard_thunk(__func__, [=]() -> int {
118 auto public_key = safe_get(key_obj).public_key();
119 *pubout = new botan_pubkey_struct(std::move(public_key));
120 return BOTAN_FFI_SUCCESS;
121 });
122 }

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

60 {
61 BOTAN_UNUSED(rng_obj);
62
63 *key = nullptr;
64
65 return ffi_guard_thunk(__func__, [=]() -> int {
66 Botan::DataSource_Memory src(bits, len);
67
68 std::unique_ptr<Botan::Private_Key> pkcs8;
69
70 if(password == nullptr)
71 {
72 pkcs8 = Botan::PKCS8::load_key(src);
73 }
74 else
75 {
76 pkcs8 = Botan::PKCS8::load_key(src, std::string(password));
77 }
78
79 if(pkcs8)
80 {
81 *key = new botan_privkey_struct(std::move(pkcs8));
82 return BOTAN_FFI_SUCCESS;
83 }
85 });
86 }
@ BOTAN_FFI_ERROR_UNKNOWN_ERROR
Definition: ffi.h:98
std::unique_ptr< Private_Key > load_key(DataSource &source, const std::function< std::string()> &get_pass)
Definition: pkcs8.cpp:361

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

188 {
189 return BOTAN_FFI_VISIT(key, [=](const auto& k) -> int
190 {
192 });
193 }
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:194
int invoke_view_callback(botan_view_bin_fn view, botan_view_ctx ctx, const std::vector< uint8_t, Alloc > &buf)
Definition: ffi_util.h:148

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

332 {
333 if(passphrase == nullptr)
335
336 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
338
339 const std::string cipher = (maybe_cipher ? maybe_cipher : "");
340 const std::string pbkdf_algo = (maybe_pbkdf_algo ? maybe_pbkdf_algo : "");
341 const size_t pbkdf_iter = (maybe_pbkdf_iterations ? maybe_pbkdf_iterations : 100000);
342
343 auto pkcs8 = Botan::PKCS8::BER_encode_encrypted_pbkdf_iter(k, rng, passphrase, pbkdf_iter, cipher, pbkdf_algo);
344
345 return invoke_view_callback(view, ctx, pkcs8);
346 });
347 }
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:240

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

251 {
252 if(passphrase == nullptr)
254
255 return BOTAN_FFI_VISIT(key, [=](const auto& k)
256 {
257 const std::chrono::milliseconds pbkdf_time(pbkdf_runtime_msec);
259
260 const std::string cipher = (maybe_cipher ? maybe_cipher : "");
261 const std::string pbkdf_algo = (maybe_pbkdf_algo ? maybe_pbkdf_algo : "");
262
264 k, rng, passphrase, pbkdf_time, nullptr, cipher, pbkdf_algo);
265
266 return invoke_view_callback(view, ctx, pkcs8);
267 });
268 }
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:288

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

358 {
359 if(passphrase == nullptr)
361
362 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
364
365 const std::string cipher = (maybe_cipher ? maybe_cipher : "");
366 const std::string pbkdf_algo = (maybe_pbkdf_algo ? maybe_pbkdf_algo : "");
367 const size_t pbkdf_iter = (maybe_pbkdf_iterations ? maybe_pbkdf_iterations : 100000);
368
369 auto pkcs8 = Botan::PKCS8::PEM_encode_encrypted_pbkdf_iter(k, rng, passphrase, pbkdf_iter, cipher, pbkdf_algo);
370
371 return invoke_view_callback(view, ctx, pkcs8);
372 });
373 }
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:273

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

279 {
280 if(passphrase == nullptr)
282
283 return BOTAN_FFI_VISIT(key, [=](const auto& k)
284 {
285 const std::chrono::milliseconds pbkdf_time(pbkdf_runtime_msec);
287
288 const std::string cipher = (maybe_cipher ? maybe_cipher : "");
289 const std::string pbkdf_algo = (maybe_pbkdf_algo ? maybe_pbkdf_algo : "");
290
292 k, rng, passphrase, pbkdf_time, nullptr, cipher, pbkdf_algo);
293
294 return invoke_view_callback(view, ctx, pkcs8);
295 });
296 }
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:321

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

196 {
197 return BOTAN_FFI_VISIT(key, [=](const auto& k) -> int
198 {
200 });
201 }
std::string PEM_encode(const Private_Key &key)
Definition: pkcs8.cpp:142

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

130 {
131 return BOTAN_FFI_VISIT(key, [=](const auto& k) { return write_str_output(out, out_len, k.algo_name()); });
132 }

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

135 {
136 const bool strong = (flags & BOTAN_CHECK_KEY_EXPENSIVE_TESTS);
137
138 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
139 return (k.check_key(safe_get(rng), strong) == true) ? 0 : BOTAN_FFI_ERROR_INVALID_INPUT;
140 });
141 }

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

111 {
112 return BOTAN_FFI_CHECKED_DELETE(key);
113 }

References BOTAN_FFI_CHECKED_DELETE.

◆ botan_pubkey_estimated_strength()

int botan_pubkey_estimated_strength ( botan_pubkey_t  key,
size_t *  estimate 
)

Definition at line 375 of file ffi_pkey.cpp.

376 {
377 return BOTAN_FFI_VISIT(key, [=](const auto& k) { *estimate = k.estimated_strength(); });
378 }

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

152 {
154 return copy_view_bin(out, out_len, botan_pubkey_view_der, key);
155 else if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_PEM)
156 return copy_view_str(out, out_len, botan_pubkey_view_pem, key);
157 else
159 }
int botan_pubkey_view_pem(botan_pubkey_t key, botan_view_ctx ctx, botan_view_str_fn view)
Definition: ffi_pkey.cpp:169
int botan_pubkey_view_der(botan_pubkey_t key, botan_view_ctx ctx, botan_view_bin_fn view)
Definition: ffi_pkey.cpp:161

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

382 {
383 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
384 auto h = Botan::HashFunction::create_or_throw(hash_fn);
385 return write_vec_output(out, out_len, h->process(k.public_key_bits()));
386 });
387 }
static std::unique_ptr< HashFunction > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition: hash.cpp:320
int write_vec_output(uint8_t out[], size_t *out_len, const std::vector< uint8_t, Alloc > &buf)
Definition: ffi_util.h:214

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

95 {
96 *key = nullptr;
97
98 return ffi_guard_thunk(__func__, [=]() -> int {
99 Botan::DataSource_Memory src(bits, bits_len);
100 std::unique_ptr<Botan::Public_Key> pubkey(Botan::X509::load_key(src));
101
102 if(pubkey == nullptr)
104
105 *key = new botan_pubkey_struct(std::move(pubkey));
106 return BOTAN_FFI_SUCCESS;
107 });
108 }
std::unique_ptr< Public_Key > load_key(DataSource &source)
Definition: x509_key.cpp:29

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

162 {
163 return BOTAN_FFI_VISIT(key, [=](const auto& k) -> int
164 {
165 return invoke_view_callback(view, ctx, Botan::X509::BER_encode(k));
166 });
167 }
std::vector< uint8_t > BER_encode(const Public_Key &key)
Definition: x509_key.h:28

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

170 {
171 return BOTAN_FFI_VISIT(key, [=](const auto& k) -> int
172 {
173 return invoke_view_callback(view, ctx, Botan::X509::PEM_encode(k));
174 });
175 }
std::string PEM_encode(const Public_Key &key)
Definition: x509_key.cpp:20

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

Referenced by botan_pubkey_export().