Botan 3.14.1
Crypto and TLS for C&
pkcs8.cpp
Go to the documentation of this file.
1/*
2* PKCS #8
3* (C) 1999-2010,2014,2018 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/pkcs8.h>
9
10#include <botan/asn1_obj.h>
11#include <botan/assert.h>
12#include <botan/ber_dec.h>
13#include <botan/der_enc.h>
14#include <botan/pem.h>
15#include <botan/pk_algs.h>
16#include <botan/rng.h>
17#include <botan/internal/algorithm_spec.h>
18#include <botan/internal/fmt.h>
19
20#if defined(BOTAN_HAS_PKCS5_PBES2)
21 #include <botan/internal/pbes2.h>
22#endif
23
24namespace Botan::PKCS8 {
25
26namespace {
27
28/*
29* Get info from an EncryptedPrivateKeyInfo
30*/
31secure_vector<uint8_t> PKCS8_extract(DataSource& source, AlgorithmIdentifier& pbe_alg_id) {
33
36 .decode(pbe_alg_id)
38 .end_cons()
39 .verify_end();
40
41 return key_data;
42}
43
44/*
45* PEM decode and/or decrypt a private key
46*/
47secure_vector<uint8_t> PKCS8_decode(DataSource& source,
48 const std::function<std::string()>& get_passphrase,
49 AlgorithmIdentifier& pk_alg_id,
50 bool is_encrypted) {
51 AlgorithmIdentifier pbe_alg_id;
54
55 try {
56 if(ASN1::maybe_BER(source) && !PEM_Code::matches(source)) {
57 if(is_encrypted) {
58 key_data = PKCS8_extract(source, pbe_alg_id);
59 } else {
60 // todo read more efficiently
61 while(auto b = source.read_byte()) {
62 key_data.push_back(*b);
63 }
64 }
65 } else {
66 std::string label;
67 key_data = PEM_Code::decode(source, label);
68
69 // todo remove autodetect for pem as well?
70 if(label == "PRIVATE KEY") {
71 is_encrypted = false;
72 } else if(label == "ENCRYPTED PRIVATE KEY") {
73 DataSource_Memory key_source(key_data);
74 key_data = PKCS8_extract(key_source, pbe_alg_id);
75 } else {
76 throw PKCS8_Exception(fmt("Unknown PEM label '{}'", label));
77 }
78 }
79
80 if(key_data.empty()) {
81 throw PKCS8_Exception("No key data found");
82 }
83 } catch(Decoding_Error& e) {
84 throw Decoding_Error("PKCS #8 private key decoding", e);
85 }
86
87 try {
88 if(is_encrypted) {
89 if(pbe_alg_id.oid().registered_name() != "PBE-PKCS5v20") {
90 throw PKCS8_Exception(fmt("Unknown PBE type {}", pbe_alg_id.oid()));
91 }
92
93#if defined(BOTAN_HAS_PKCS5_PBES2)
94 key = pbes2_decrypt(key_data, get_passphrase(), pbe_alg_id.parameters());
95#else
96 BOTAN_UNUSED(get_passphrase);
97 throw Decoding_Error("Private key is encrypted but PBES2 was disabled in build");
98#endif
99 } else {
100 key = key_data;
101 }
102
105 .decode_and_check<size_t>(0, "Unknown PKCS #8 version number")
106 .decode(pk_alg_id)
107 .decode(key, ASN1_Type::OctetString)
108 .discard_remaining()
109 .end_cons()
110 .verify_end();
111 } catch(std::exception& e) {
112 throw Decoding_Error("PKCS #8 private key decoding", e);
113 }
114 return key;
115}
116
117} // namespace
118
119/*
120* PEM encode a PKCS #8 private key, unencrypted
121*/
122std::string PEM_encode(const Private_Key& key) {
123 return PEM_Code::encode(key.private_key_info(), "PRIVATE KEY");
124}
125
126#if defined(BOTAN_HAS_PKCS5_PBES2)
127
128namespace {
129
130std::pair<std::string, std::string> choose_pbe_params(std::string_view pbe_algo, std::string_view key_algo) {
131 if(pbe_algo.empty()) {
132 /*
133 * For algorithms where we are using a non-RFC format anyway, default to
134 * SIV or GCM. For others (RSA, ECDSA, ...) default to something widely
135 * compatible.
136 */
137 const bool nonstandard_pk = (key_algo == "McEliece" || key_algo == "XMSS");
138
139 if(nonstandard_pk) {
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");
144 #endif
145 }
146
147 // Default is something compatible with everyone else
148 return std::make_pair("AES-256/CBC", "SHA-256");
149 }
150
151 const AlgorithmSpec request(pbe_algo);
152
153 const auto m = request.match("PBE-PKCS5v20|PBES2({cipher},{hash})");
154 if(!m) {
155 throw Invalid_Argument(fmt("Unsupported PBE '{}'", pbe_algo));
156 }
157
158 return std::make_pair(std::string(m->str("cipher")), std::string(m->str("hash")));
159}
160
161} // namespace
162
163#endif
164
165/*
166* BER encode a PKCS #8 private key, encrypted
167*/
168std::vector<uint8_t> BER_encode(const Private_Key& key,
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());
175
176 const std::pair<AlgorithmIdentifier, std::vector<uint8_t>> pbe_info =
177 pbes2_encrypt_msec(PKCS8::BER_encode(key), pass, msec, nullptr, pbe_params.first, pbe_params.second, rng);
178
179 std::vector<uint8_t> output;
180 DER_Encoder der(output);
181 der.start_sequence().encode(pbe_info.first).encode(pbe_info.second, ASN1_Type::OctetString).end_cons();
182
183 return output;
184#else
185 BOTAN_UNUSED(key, rng, pass, msec, pbe_algo);
186 throw Encoding_Error("PKCS8::BER_encode cannot encrypt because PBES2 was disabled in build");
187#endif
188}
189
190/*
191* PEM encode a PKCS #8 private key, encrypted
192*/
193std::string PEM_encode(const Private_Key& key,
195 std::string_view pass,
196 std::chrono::milliseconds msec,
197 std::string_view pbe_algo) {
198 if(pass.empty()) {
199 return PEM_encode(key);
200 }
201
202 return PEM_Code::encode(PKCS8::BER_encode(key, rng, pass, msec, pbe_algo), "ENCRYPTED PRIVATE KEY");
203}
204
205/*
206* BER encode a PKCS #8 private key, encrypted
207*/
208std::vector<uint8_t> BER_encode_encrypted_pbkdf_iter(const Private_Key& key,
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 =
217 pass,
218 pbkdf_iterations,
219 cipher.empty() ? "AES-256/CBC" : cipher,
220 pbkdf_hash.empty() ? "SHA-256" : pbkdf_hash,
221 rng);
222
223 std::vector<uint8_t> output;
224 DER_Encoder der(output);
225 der.start_sequence().encode(pbe_info.first).encode(pbe_info.second, ASN1_Type::OctetString).end_cons();
226
227 return output;
228
229#else
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");
232#endif
233}
234
235/*
236* PEM encode a PKCS #8 private key, encrypted
237*/
240 std::string_view pass,
241 size_t pbkdf_iterations,
242 std::string_view cipher,
243 std::string_view pbkdf_hash) {
244 return PEM_Code::encode(PKCS8::BER_encode_encrypted_pbkdf_iter(key, rng, pass, pbkdf_iterations, cipher, pbkdf_hash),
245 "ENCRYPTED PRIVATE KEY");
246}
247
248/*
249* BER encode a PKCS #8 private key, encrypted
250*/
251std::vector<uint8_t> BER_encode_encrypted_pbkdf_msec(const Private_Key& 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 =
261 pass,
262 pbkdf_msec,
263 pbkdf_iterations,
264 cipher.empty() ? "AES-256/CBC" : cipher,
265 pbkdf_hash.empty() ? "SHA-256" : pbkdf_hash,
266 rng);
267
268 std::vector<uint8_t> output;
269 DER_Encoder(output)
271 .encode(pbe_info.first)
272 .encode(pbe_info.second, ASN1_Type::OctetString)
273 .end_cons();
274
275 return output;
276#else
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");
279#endif
280}
281
282/*
283* PEM encode a PKCS #8 private key, encrypted
284*/
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) {
292 return PEM_Code::encode(
293 PKCS8::BER_encode_encrypted_pbkdf_msec(key, rng, pass, pbkdf_msec, pbkdf_iterations, cipher, pbkdf_hash),
294 "ENCRYPTED PRIVATE KEY");
295}
296
297namespace {
298
299/*
300* Extract a private key (encrypted/unencrypted) and return it
301*/
302std::unique_ptr<Private_Key> load_key(DataSource& source,
303 const std::function<std::string()>& get_pass,
304 bool is_encrypted) {
305 AlgorithmIdentifier alg_id;
306 secure_vector<uint8_t> pkcs8_key = PKCS8_decode(source, get_pass, alg_id, is_encrypted);
307
308 if(const auto alg_name = alg_id.oid().registered_name()) {
309 return load_private_key(alg_id, pkcs8_key);
310 } else {
311 throw PKCS8_Exception(fmt("Unknown algorithm OID {}", alg_id.oid()));
312 }
313}
314
315} // namespace
316
317/*
318* Extract an encrypted private key and return it
319*/
320std::unique_ptr<Private_Key> load_key(DataSource& source, const std::function<std::string()>& get_pass) {
321 return load_key(source, get_pass, true);
322}
323
324std::unique_ptr<Private_Key> load_key(std::span<const uint8_t> source,
325 const std::function<std::string()>& get_passphrase) {
326 Botan::DataSource_Memory ds(source);
327 return load_key(ds, get_passphrase);
328}
329
330std::unique_ptr<Private_Key> load_key(std::span<const uint8_t> source, std::string_view pass) {
331 Botan::DataSource_Memory ds(source);
332 return load_key(ds, pass);
333}
334
335std::unique_ptr<Private_Key> load_key(std::span<const uint8_t> source) {
336 Botan::DataSource_Memory ds(source);
337 return load_key(ds);
338}
339
340/*
341* Extract an encrypted private key and return it
342*/
343std::unique_ptr<Private_Key> load_key(DataSource& source, std::string_view pass) {
344 return load_key(
345 source, [pass]() { return std::string(pass); }, true);
346}
347
348/*
349* Extract an unencrypted private key and return it
350*/
351std::unique_ptr<Private_Key> load_key(DataSource& source) {
352 const auto fail_fn = []() -> std::string {
353 throw PKCS8_Exception("Internal error: Attempt to read password for unencrypted key");
354 };
355
356 return load_key(source, fail_fn, false);
357}
358
359} // namespace Botan::PKCS8
#define BOTAN_UNUSED
Definition assert.h:151
const std::vector< uint8_t > & parameters() const
Definition asn1_obj.h:693
const OID & oid() const
Definition asn1_obj.h:688
virtual std::string algo_name() const =0
static Limits DER()
Definition ber_dec.h:44
BER_Decoder & decode(bool &out)
Definition ber_dec.h:361
BER_Decoder & verify_end()
Definition ber_dec.cpp:471
BER_Decoder & end_cons()
Definition ber_dec.cpp:635
BER_Decoder start_sequence()
Definition ber_dec.h:279
BER_Decoder & decode_and_check(const T &expected, std::string_view error_msg)
Definition ber_dec.h:698
DER_Encoder & start_sequence()
Definition der_enc.h:86
DER_Encoder & end_cons()
Definition der_enc.cpp:208
DER_Encoder & encode(bool b)
Definition der_enc.cpp:313
size_t read_byte(uint8_t &out)
Definition data_src.cpp:27
std::optional< std::string > registered_name() const
Definition asn1_oid.cpp:149
secure_vector< uint8_t > private_key_info() const
Definition pk_keys.cpp:77
bool maybe_BER(DataSource &source)
Definition asn1_obj.cpp:231
std::string encode(const uint8_t der[], size_t length, std::string_view label, size_t width)
Definition pem.cpp:41
bool matches(DataSource &source, std::string_view extra, size_t search_range)
Definition pem.cpp:146
secure_vector< uint8_t > decode(DataSource &source, std::string &label)
Definition pem.cpp:64
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:168
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:238
std::string PEM_encode(const Private_Key &key)
Definition pkcs8.cpp:122
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:285
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:208
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:251
std::unique_ptr< Private_Key > load_key(DataSource &source, const std::function< std::string()> &get_pass)
Definition pkcs8.cpp:320
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
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)
Definition pbes2.cpp:414
secure_vector< uint8_t > pbes2_decrypt(std::span< const uint8_t > key_bits, std::string_view passphrase, const std::vector< uint8_t > &params)
Definition pbes2.cpp:423
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)
Definition pbes2.cpp:396
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128
std::unique_ptr< Private_Key > load_private_key(const AlgorithmIdentifier &alg_id, std::span< const uint8_t > key_bits)
Definition pk_algs.cpp:297