8#include <botan/pkcs8.h>
10#include <botan/asn1_obj.h>
11#include <botan/assert.h>
12#include <botan/ber_dec.h>
13#include <botan/der_enc.h>
15#include <botan/pk_algs.h>
17#include <botan/internal/algorithm_spec.h>
18#include <botan/internal/fmt.h>
20#if defined(BOTAN_HAS_PKCS5_PBES2)
21 #include <botan/internal/pbes2.h>
48 const std::function<std::string()>& get_passphrase,
58 key_data = PKCS8_extract(source, pbe_alg_id);
62 key_data.push_back(*b);
70 if(label ==
"PRIVATE KEY") {
72 }
else if(label ==
"ENCRYPTED PRIVATE KEY") {
74 key_data = PKCS8_extract(key_source, pbe_alg_id);
80 if(key_data.empty()) {
93#if defined(BOTAN_HAS_PKCS5_PBES2)
97 throw Decoding_Error(
"Private key is encrypted but PBES2 was disabled in build");
111 }
catch(std::exception& e) {
126#if defined(BOTAN_HAS_PKCS5_PBES2)
130std::pair<std::string, std::string> choose_pbe_params(std::string_view pbe_algo, std::string_view key_algo) {
131 if(pbe_algo.empty()) {
137 const bool nonstandard_pk = (key_algo ==
"McEliece" || key_algo ==
"XMSS");
140 #if defined(BOTAN_HAS_AEAD_SIV) && defined(BOTAN_HAS_SHA2_64)
141 return std::make_pair(
"AES-256/SIV",
"SHA-512");
142 #elif defined(BOTAN_HAS_AEAD_GCM) && defined(BOTAN_HAS_SHA2_64)
143 return std::make_pair(
"AES-256/GCM",
"SHA-512");
148 return std::make_pair(
"AES-256/CBC",
"SHA-256");
151 const AlgorithmSpec request(pbe_algo);
153 const auto m = request.match(
"PBE-PKCS5v20|PBES2({cipher},{hash})");
155 throw Invalid_Argument(
fmt(
"Unsupported PBE '{}'", pbe_algo));
158 return std::make_pair(std::string(m->str(
"cipher")), std::string(m->str(
"hash")));
170 std::string_view pass,
171 std::chrono::milliseconds msec,
172 std::string_view pbe_algo) {
173#if defined(BOTAN_HAS_PKCS5_PBES2)
174 const auto pbe_params = choose_pbe_params(pbe_algo, key.
algo_name());
176 const std::pair<AlgorithmIdentifier, std::vector<uint8_t>> pbe_info =
179 std::vector<uint8_t> output;
186 throw Encoding_Error(
"PKCS8::BER_encode cannot encrypt because PBES2 was disabled in build");
195 std::string_view pass,
196 std::chrono::milliseconds msec,
197 std::string_view pbe_algo) {
210 std::string_view pass,
211 size_t pbkdf_iterations,
212 std::string_view cipher,
213 std::string_view pbkdf_hash) {
214#if defined(BOTAN_HAS_PKCS5_PBES2)
215 const std::pair<AlgorithmIdentifier, std::vector<uint8_t>> pbe_info =
219 cipher.empty() ?
"AES-256/CBC" : cipher,
220 pbkdf_hash.empty() ?
"SHA-256" : pbkdf_hash,
223 std::vector<uint8_t> output;
230 BOTAN_UNUSED(key, rng, pass, pbkdf_iterations, cipher, pbkdf_hash);
231 throw Encoding_Error(
"PKCS8::BER_encode_encrypted_pbkdf_iter cannot encrypt because PBES2 disabled in build");
240 std::string_view pass,
241 size_t pbkdf_iterations,
242 std::string_view cipher,
243 std::string_view pbkdf_hash) {
245 "ENCRYPTED PRIVATE KEY");
253 std::string_view pass,
254 std::chrono::milliseconds pbkdf_msec,
255 size_t* pbkdf_iterations,
256 std::string_view cipher,
257 std::string_view pbkdf_hash) {
258#if defined(BOTAN_HAS_PKCS5_PBES2)
259 const std::pair<AlgorithmIdentifier, std::vector<uint8_t>> pbe_info =
264 cipher.empty() ?
"AES-256/CBC" : cipher,
265 pbkdf_hash.empty() ?
"SHA-256" : pbkdf_hash,
268 std::vector<uint8_t> output;
277 BOTAN_UNUSED(key, rng, pass, pbkdf_msec, pbkdf_iterations, cipher, pbkdf_hash);
278 throw Encoding_Error(
"BER_encode_encrypted_pbkdf_msec cannot encrypt because PBES2 disabled in build");
287 std::string_view pass,
288 std::chrono::milliseconds pbkdf_msec,
289 size_t* pbkdf_iterations,
290 std::string_view cipher,
291 std::string_view pbkdf_hash) {
294 "ENCRYPTED PRIVATE KEY");
303 const std::function<std::string()>& get_pass,
311 throw PKCS8_Exception(
fmt(
"Unknown algorithm OID {}", alg_id.
oid()));
320std::unique_ptr<Private_Key>
load_key(
DataSource& source,
const std::function<std::string()>& get_pass) {
321 return load_key(source, get_pass,
true);
324std::unique_ptr<Private_Key>
load_key(std::span<const uint8_t> source,
325 const std::function<std::string()>& get_passphrase) {
327 return load_key(ds, get_passphrase);
330std::unique_ptr<Private_Key>
load_key(std::span<const uint8_t> source, std::string_view pass) {
335std::unique_ptr<Private_Key>
load_key(std::span<const uint8_t> source) {
345 source, [pass]() {
return std::string(pass); },
true);
352 const auto fail_fn = []() -> std::string {
353 throw PKCS8_Exception(
"Internal error: Attempt to read password for unencrypted key");
356 return load_key(source, fail_fn,
false);
const std::vector< uint8_t > & parameters() const
virtual std::string algo_name() const =0
BER_Decoder & decode(bool &out)
BER_Decoder & verify_end()
BER_Decoder start_sequence()
BER_Decoder & decode_and_check(const T &expected, std::string_view error_msg)
DER_Encoder & start_sequence()
DER_Encoder & encode(bool b)
size_t read_byte(uint8_t &out)
std::optional< std::string > registered_name() const
secure_vector< uint8_t > private_key_info() const
bool maybe_BER(DataSource &source)
std::string encode(const uint8_t der[], size_t length, std::string_view label, size_t width)
bool matches(DataSource &source, std::string_view extra, size_t search_range)
secure_vector< uint8_t > decode(DataSource &source, std::string &label)
std::vector< uint8_t > BER_encode(const Private_Key &key, RandomNumberGenerator &rng, std::string_view pass, std::chrono::milliseconds msec, std::string_view pbe_algo)
std::string PEM_encode_encrypted_pbkdf_iter(const Private_Key &key, RandomNumberGenerator &rng, std::string_view pass, size_t pbkdf_iterations, std::string_view cipher, std::string_view pbkdf_hash)
std::string PEM_encode(const Private_Key &key)
std::string PEM_encode_encrypted_pbkdf_msec(const Private_Key &key, RandomNumberGenerator &rng, std::string_view pass, std::chrono::milliseconds pbkdf_msec, size_t *pbkdf_iterations, std::string_view cipher, std::string_view pbkdf_hash)
std::vector< uint8_t > BER_encode_encrypted_pbkdf_iter(const Private_Key &key, RandomNumberGenerator &rng, std::string_view pass, size_t pbkdf_iterations, std::string_view cipher, std::string_view pbkdf_hash)
std::vector< uint8_t > BER_encode_encrypted_pbkdf_msec(const Private_Key &key, RandomNumberGenerator &rng, std::string_view pass, std::chrono::milliseconds pbkdf_msec, size_t *pbkdf_iterations, std::string_view cipher, std::string_view pbkdf_hash)
std::unique_ptr< Private_Key > load_key(DataSource &source, const std::function< std::string()> &get_pass)
std::string fmt(std::string_view format, const T &... args)
std::pair< AlgorithmIdentifier, std::vector< uint8_t > > pbes2_encrypt_iter(std::span< const uint8_t > key_bits, std::string_view passphrase, size_t pbkdf_iter, std::string_view cipher, std::string_view digest, RandomNumberGenerator &rng)
secure_vector< uint8_t > pbes2_decrypt(std::span< const uint8_t > key_bits, std::string_view passphrase, const std::vector< uint8_t > ¶ms)
std::pair< AlgorithmIdentifier, std::vector< uint8_t > > pbes2_encrypt_msec(std::span< const uint8_t > key_bits, std::string_view passphrase, std::chrono::milliseconds msec, size_t *out_iterations_if_nonnull, std::string_view cipher, std::string_view digest, RandomNumberGenerator &rng)
std::vector< T, secure_allocator< T > > secure_vector
std::unique_ptr< Private_Key > load_private_key(const AlgorithmIdentifier &alg_id, std::span< const uint8_t > key_bits)