8#include <botan/pkcs8.h>
10#include <botan/asn1_obj.h>
11#include <botan/ber_dec.h>
12#include <botan/der_enc.h>
14#include <botan/pk_algs.h>
16#include <botan/internal/fmt.h>
17#include <botan/internal/scan_name.h>
19#if defined(BOTAN_HAS_PKCS5_PBES2)
20 #include <botan/internal/pbes2.h>
42 const std::function<std::string()>& get_passphrase,
51 key_data = PKCS8_extract(source, pbe_alg_id);
58 key_data.push_back(
b);
67 if(label ==
"PRIVATE KEY") {
69 }
else if(label ==
"ENCRYPTED PRIVATE KEY") {
71 key_data = PKCS8_extract(key_source, pbe_alg_id);
77 if(key_data.empty()) {
90#if defined(BOTAN_HAS_PKCS5_PBES2)
94 throw Decoding_Error(
"Private key is encrypted but PBES2 was disabled in build");
107 }
catch(std::exception& e) {
122#if defined(BOTAN_HAS_PKCS5_PBES2)
126std::pair<std::string, std::string> choose_pbe_params(std::string_view pbe_algo, std::string_view key_algo) {
127 if(pbe_algo.empty()) {
133 const bool nonstandard_pk = (key_algo ==
"McEliece" || key_algo ==
"XMSS");
136 #if defined(BOTAN_HAS_AEAD_SIV) && defined(BOTAN_HAS_SHA2_64)
137 return std::make_pair(
"AES-256/SIV",
"SHA-512");
138 #elif defined(BOTAN_HAS_AEAD_GCM) && defined(BOTAN_HAS_SHA2_64)
139 return std::make_pair(
"AES-256/GCM",
"SHA-512");
144 return std::make_pair(
"AES-256/CBC",
"SHA-256");
147 SCAN_Name request(pbe_algo);
149 if(request.arg_count() != 2 || (request.algo_name() !=
"PBE-PKCS5v20" && request.algo_name() !=
"PBES2")) {
150 throw Invalid_Argument(
fmt(
"Unsupported PBE '{}'", pbe_algo));
153 return std::make_pair(request.arg(0), request.arg(1));
165 std::string_view pass,
166 std::chrono::milliseconds msec,
167 std::string_view pbe_algo) {
168#if defined(BOTAN_HAS_PKCS5_PBES2)
169 const auto pbe_params = choose_pbe_params(pbe_algo, key.
algo_name());
171 const std::pair<AlgorithmIdentifier, std::vector<uint8_t>> pbe_info =
174 std::vector<uint8_t> output;
181 throw Encoding_Error(
"PKCS8::BER_encode cannot encrypt because PBES2 was disabled in build");
190 std::string_view pass,
191 std::chrono::milliseconds msec,
192 std::string_view pbe_algo) {
205 std::string_view pass,
206 size_t pbkdf_iterations,
207 std::string_view cipher,
208 std::string_view pbkdf_hash) {
209#if defined(BOTAN_HAS_PKCS5_PBES2)
210 const std::pair<AlgorithmIdentifier, std::vector<uint8_t>> pbe_info =
214 cipher.empty() ?
"AES-256/CBC" : cipher,
215 pbkdf_hash.empty() ?
"SHA-256" : pbkdf_hash,
218 std::vector<uint8_t> output;
225 BOTAN_UNUSED(key, rng, pass, pbkdf_iterations, cipher, pbkdf_hash);
226 throw Encoding_Error(
"PKCS8::BER_encode_encrypted_pbkdf_iter cannot encrypt because PBES2 disabled in build");
235 std::string_view pass,
236 size_t pbkdf_iterations,
237 std::string_view cipher,
238 std::string_view pbkdf_hash) {
240 "ENCRYPTED PRIVATE KEY");
248 std::string_view pass,
249 std::chrono::milliseconds pbkdf_msec,
250 size_t* pbkdf_iterations,
251 std::string_view cipher,
252 std::string_view pbkdf_hash) {
253#if defined(BOTAN_HAS_PKCS5_PBES2)
254 const std::pair<AlgorithmIdentifier, std::vector<uint8_t>> pbe_info =
259 cipher.empty() ?
"AES-256/CBC" : cipher,
260 pbkdf_hash.empty() ?
"SHA-256" : pbkdf_hash,
263 std::vector<uint8_t> output;
272 BOTAN_UNUSED(key, rng, pass, pbkdf_msec, pbkdf_iterations, cipher, pbkdf_hash);
273 throw Encoding_Error(
"BER_encode_encrypted_pbkdf_msec cannot encrypt because PBES2 disabled in build");
282 std::string_view pass,
283 std::chrono::milliseconds pbkdf_msec,
284 size_t* pbkdf_iterations,
285 std::string_view cipher,
286 std::string_view pbkdf_hash) {
289 "ENCRYPTED PRIVATE KEY");
298 const std::function<std::string()>& get_pass,
304 if(alg_name.empty()) {
316std::unique_ptr<Private_Key>
load_key(
DataSource& source,
const std::function<std::string()>& get_pass) {
317 return load_key(source, get_pass,
true);
320std::unique_ptr<Private_Key>
load_key(std::span<const uint8_t> source,
321 const std::function<std::string()>& get_passphrase) {
323 return load_key(ds, get_passphrase);
326std::unique_ptr<Private_Key>
load_key(std::span<const uint8_t> source, std::string_view pass) {
331std::unique_ptr<Private_Key>
load_key(std::span<const uint8_t> source) {
341 source, [pass]() {
return std::string(pass); },
true);
348 auto fail_fn = []() -> std::string {
349 throw PKCS8_Exception(
"Internal error: Attempt to read password for unencrypted key");
352 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)
virtual bool end_of_data() const =0
std::string to_formatted_string() const
std::string human_name_or_empty() 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)