Botan 3.6.1
Crypto and TLS for C&
pkcs8.h
Go to the documentation of this file.
1/*
2* PKCS #8
3* (C) 1999-2007,2023 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#ifndef BOTAN_PKCS8_H_
9#define BOTAN_PKCS8_H_
10
11#include <botan/data_src.h>
12#include <botan/exceptn.h>
13#include <botan/pk_keys.h>
14#include <botan/secmem.h>
15#include <chrono>
16#include <functional>
17#include <memory>
18#include <span>
19#include <string_view>
20
21namespace Botan {
22
23class RandomNumberGenerator;
24
25/**
26* PKCS #8 General Exception
27*/
29 public:
30 explicit PKCS8_Exception(std::string_view error) : Decoding_Error("PKCS #8", error) {}
31};
32
33/**
34* This namespace contains functions for handling PKCS #8 private keys
35*/
36namespace PKCS8 {
37
38/**
39* BER encode a private key
40* @param key the private key to encode
41* @return BER encoded key
42*/
44 return key.private_key_info();
45}
46
47/**
48* Get a string containing a PEM encoded private key.
49* @param key the key to encode
50* @return encoded key
51*/
52BOTAN_PUBLIC_API(2, 0) std::string PEM_encode(const Private_Key& key);
53
54/**
55* Encrypt a key using PKCS #8 encryption
56* @param key the key to encode
57* @param rng the rng to use
58* @param pass the password to use for encryption
59* @param msec number of milliseconds to run the password derivation
60* @param pbe_algo the name of the desired password-based encryption
61* algorithm; if empty ("") a reasonable (portable/secure)
62* default will be chosen.
63* @return encrypted key in binary BER form
64*/
66std::vector<uint8_t> BER_encode(const Private_Key& key,
68 std::string_view pass,
69 std::chrono::milliseconds msec = std::chrono::milliseconds(300),
70 std::string_view pbe_algo = "");
71
72/**
73* Get a string containing a PEM encoded private key, encrypting it with a
74* password.
75* @param key the key to encode
76* @param rng the rng to use
77* @param pass the password to use for encryption
78* @param msec number of milliseconds to run the password derivation
79* @param pbe_algo the name of the desired password-based encryption
80* algorithm; if empty ("") a reasonable (portable/secure)
81* default will be chosen.
82* @return encrypted key in PEM form
83*/
85std::string PEM_encode(const Private_Key& key,
87 std::string_view pass,
88 std::chrono::milliseconds msec = std::chrono::milliseconds(300),
89 std::string_view pbe_algo = "");
90
91/**
92* Encrypt a key using PKCS #8 encryption and a fixed iteration count
93* @param key the key to encode
94* @param rng the rng to use
95* @param pass the password to use for encryption
96* @param pbkdf_iter number of interations to run PBKDF2
97* @param cipher if non-empty specifies the cipher to use. CBC and GCM modes
98* are supported, for example "AES-128/CBC", "AES-256/GCM", "Serpent/CBC".
99* If empty a suitable default is chosen.
100* @param pbkdf_hash if non-empty specifies the PBKDF hash function to use.
101* For example "SHA-256" or "SHA-384". If empty a suitable default is chosen.
102* @return encrypted key in binary BER form
103*/
105std::vector<uint8_t> BER_encode_encrypted_pbkdf_iter(const Private_Key& key,
107 std::string_view pass,
108 size_t pbkdf_iter,
109 std::string_view cipher = "",
110 std::string_view pbkdf_hash = "");
111
112/**
113* Get a string containing a PEM encoded private key, encrypting it with a
114* password.
115* @param key the key to encode
116* @param rng the rng to use
117* @param pass the password to use for encryption
118* @param pbkdf_iter number of iterations to run PBKDF
119* @param cipher if non-empty specifies the cipher to use. CBC and GCM modes
120* are supported, for example "AES-128/CBC", "AES-256/GCM", "Serpent/CBC".
121* If empty a suitable default is chosen.
122* @param pbkdf_hash if non-empty specifies the PBKDF hash function to use.
123* For example "SHA-256" or "SHA-384". If empty a suitable default is chosen.
124* @return encrypted key in PEM form
125*/
127std::string PEM_encode_encrypted_pbkdf_iter(const Private_Key& key,
129 std::string_view pass,
130 size_t pbkdf_iter,
131 std::string_view cipher = "",
132 std::string_view pbkdf_hash = "");
133
134/**
135* Encrypt a key using PKCS #8 encryption and a variable iteration count
136* @param key the key to encode
137* @param rng the rng to use
138* @param pass the password to use for encryption
139* @param pbkdf_msec how long to run PBKDF2
140* @param pbkdf_iterations if non-null, set to the number of iterations used
141* @param cipher if non-empty specifies the cipher to use. CBC and GCM modes
142* are supported, for example "AES-128/CBC", "AES-256/GCM", "Serpent/CBC".
143* If empty a suitable default is chosen.
144* @param pbkdf_hash if non-empty specifies the PBKDF hash function to use.
145* For example "SHA-256" or "SHA-384". If empty a suitable default is chosen.
146* @return encrypted key in binary BER form
147*/
149std::vector<uint8_t> BER_encode_encrypted_pbkdf_msec(const Private_Key& key,
151 std::string_view pass,
152 std::chrono::milliseconds pbkdf_msec,
153 size_t* pbkdf_iterations,
154 std::string_view cipher = "",
155 std::string_view pbkdf_hash = "");
156
157/**
158* Get a string containing a PEM encoded private key, encrypting it with a
159* password.
160* @param key the key to encode
161* @param rng the rng to use
162* @param pass the password to use for encryption
163* @param pbkdf_msec how long in milliseconds to run PBKDF2
164* @param pbkdf_iterations (output argument) number of iterations of PBKDF
165* that ended up being used
166* @param cipher if non-empty specifies the cipher to use. CBC and GCM modes
167* are supported, for example "AES-128/CBC", "AES-256/GCM", "Serpent/CBC".
168* If empty a suitable default is chosen.
169* @param pbkdf_hash if non-empty specifies the PBKDF hash function to use.
170* For example "SHA-256" or "SHA-384". If empty a suitable default is chosen.
171* @return encrypted key in PEM form
172*/
174std::string PEM_encode_encrypted_pbkdf_msec(const Private_Key& key,
176 std::string_view pass,
177 std::chrono::milliseconds pbkdf_msec,
178 size_t* pbkdf_iterations,
179 std::string_view cipher = "",
180 std::string_view pbkdf_hash = "");
181
182/**
183* Load an encrypted key from a data source.
184* @param source the data source providing the encoded key
185* @param get_passphrase a function that returns passphrases
186* @return loaded private key object
187*/
189std::unique_ptr<Private_Key> load_key(DataSource& source, const std::function<std::string()>& get_passphrase);
190
191/** Load an encrypted key from a data source.
192* @param source the data source providing the encoded key
193* @param pass the passphrase to decrypt the key
194* @return loaded private key object
195*/
197std::unique_ptr<Private_Key> load_key(DataSource& source, std::string_view pass);
198
199/** Load an unencrypted key from a data source.
200* @param source the data source providing the encoded key
201* @return loaded private key object
202*/
204std::unique_ptr<Private_Key> load_key(DataSource& source);
205
206/**
207* Load an encrypted key from memory.
208* @param source the byte buffer containing the encoded key
209* @param get_passphrase a function that returns passphrases
210* @return loaded private key object
211*/
213std::unique_ptr<Private_Key> load_key(std::span<const uint8_t> source,
214 const std::function<std::string()>& get_passphrase);
215
216/** Load an encrypted key from memory.
217* @param source the byte buffer containing the encoded key
218* @param pass the passphrase to decrypt the key
219* @return loaded private key object
220*/
222std::unique_ptr<Private_Key> load_key(std::span<const uint8_t> source, std::string_view pass);
223
224/** Load an unencrypted key from memory.
225* @param source the byte buffer containing the encoded key
226* @return loaded private key object
227*/
229std::unique_ptr<Private_Key> load_key(std::span<const uint8_t> source);
230
231/**
232* Copy an existing encoded key object.
233* @param key the key to copy
234* @return new copy of the key
235*/
236inline std::unique_ptr<Private_Key> copy_key(const Private_Key& key) {
237 DataSource_Memory source(key.private_key_info());
238 return PKCS8::load_key(source);
239}
240
241} // namespace PKCS8
242
243} // namespace Botan
244
245#endif
PKCS8_Exception(std::string_view error)
Definition pkcs8.h:30
secure_vector< uint8_t > private_key_info() const
Definition pk_keys.cpp:60
int(* final)(unsigned char *, CTX *)
#define BOTAN_PUBLIC_API(maj, min)
Definition compiler.h:31
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
std::unique_ptr< Private_Key > copy_key(const Private_Key &key)
Definition pkcs8.h:236
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
std::string PEM_encode(const Private_Key &key)
Definition pkcs8.cpp:118
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
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
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
std::unique_ptr< Private_Key > load_key(DataSource &source, const std::function< std::string()> &get_pass)
Definition pkcs8.cpp:316
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61