1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
/*
* PKCS #8
* (C) 1999-2010,2014 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
#include <botan/pkcs8.h>
#include <botan/der_enc.h>
#include <botan/ber_dec.h>
#include <botan/alg_id.h>
#include <botan/oids.h>
#include <botan/pem.h>
#include <botan/pbes2.h>
#include <botan/scan_name.h>
#include <botan/internal/pk_algs.h>
namespace Botan {
namespace PKCS8 {
namespace {
/*
* Get info from an EncryptedPrivateKeyInfo
*/
secure_vector<byte> PKCS8_extract(DataSource& source,
AlgorithmIdentifier& pbe_alg_id)
{
secure_vector<byte> key_data;
BER_Decoder(source)
.start_cons(SEQUENCE)
.decode(pbe_alg_id)
.decode(key_data, OCTET_STRING)
.verify_end();
return key_data;
}
/*
* PEM decode and/or decrypt a private key
*/
secure_vector<byte> PKCS8_decode(
DataSource& source,
std::function<std::string ()> get_passphrase,
AlgorithmIdentifier& pk_alg_id)
{
AlgorithmIdentifier pbe_alg_id;
secure_vector<byte> key_data, key;
bool is_encrypted = true;
try {
if(ASN1::maybe_BER(source) && !PEM_Code::matches(source))
key_data = PKCS8_extract(source, pbe_alg_id);
else
{
std::string label;
key_data = PEM_Code::decode(source, label);
if(label == "PRIVATE KEY")
is_encrypted = false;
else if(label == "ENCRYPTED PRIVATE KEY")
{
DataSource_Memory key_source(key_data);
key_data = PKCS8_extract(key_source, pbe_alg_id);
}
else
throw PKCS8_Exception("Unknown PEM label " + label);
}
if(key_data.empty())
throw PKCS8_Exception("No key data found");
}
catch(Decoding_Error& e)
{
throw Decoding_Error("PKCS #8 private key decoding failed: " + std::string(e.what()));
}
try
{
if(is_encrypted)
{
if(OIDS::lookup(pbe_alg_id.oid) != "PBE-PKCS5v20")
throw Exception("Unknown PBE type " + pbe_alg_id.oid.as_string());
key = pbes2_decrypt(key_data, get_passphrase(), pbe_alg_id.parameters);
}
else
key = key_data;
BER_Decoder(key)
.start_cons(SEQUENCE)
.decode_and_check<size_t>(0, "Unknown PKCS #8 version number")
.decode(pk_alg_id)
.decode(key, OCTET_STRING)
.discard_remaining()
.end_cons();
}
catch(std::exception& e)
{
throw Decoding_Error("PKCS #8 private key decoding failed: " + std::string(e.what()));
}
return key;
}
}
/*
* BER encode a PKCS #8 private key, unencrypted
*/
secure_vector<byte> BER_encode(const Private_Key& key)
{
const size_t PKCS8_VERSION = 0;
return DER_Encoder()
.start_cons(SEQUENCE)
.encode(PKCS8_VERSION)
.encode(key.pkcs8_algorithm_identifier())
.encode(key.pkcs8_private_key(), OCTET_STRING)
.end_cons()
.get_contents();
}
/*
* PEM encode a PKCS #8 private key, unencrypted
*/
std::string PEM_encode(const Private_Key& key)
{
return PEM_Code::encode(PKCS8::BER_encode(key), "PRIVATE KEY");
}
namespace {
std::pair<std::string, std::string>
choose_pbe_params(const std::string& pbe_algo, const std::string& key_algo)
{
if(pbe_algo == "")
{
// Defaults:
if(key_algo == "Curve25519" || key_algo == "McEliece")
return std::make_pair("AES-256/GCM", "SHA-512");
else // for everything else (RSA, DSA, ECDSA, GOST, ...)
return std::make_pair("AES-256/CBC", "SHA-256");
}
SCAN_Name request(pbe_algo);
if(request.algo_name() != "PBE-PKCS5v20" || request.arg_count() != 2)
throw Exception("Unsupported PBE " + pbe_algo);
return std::make_pair(request.arg(1), request.arg(0));
}
}
/*
* BER encode a PKCS #8 private key, encrypted
*/
std::vector<byte> BER_encode(const Private_Key& key,
RandomNumberGenerator& rng,
const std::string& pass,
std::chrono::milliseconds msec,
const std::string& pbe_algo)
{
const auto pbe_params = choose_pbe_params(pbe_algo, key.algo_name());
const std::pair<AlgorithmIdentifier, std::vector<byte>> pbe_info =
pbes2_encrypt(PKCS8::BER_encode(key), pass, msec,
pbe_params.first, pbe_params.second, rng);
return DER_Encoder()
.start_cons(SEQUENCE)
.encode(pbe_info.first)
.encode(pbe_info.second, OCTET_STRING)
.end_cons()
.get_contents_unlocked();
}
/*
* PEM encode a PKCS #8 private key, encrypted
*/
std::string PEM_encode(const Private_Key& key,
RandomNumberGenerator& rng,
const std::string& pass,
std::chrono::milliseconds msec,
const std::string& pbe_algo)
{
if(pass == "")
return PEM_encode(key);
return PEM_Code::encode(PKCS8::BER_encode(key, rng, pass, msec, pbe_algo),
"ENCRYPTED PRIVATE KEY");
}
/*
* Extract a private key and return it
*/
Private_Key* load_key(DataSource& source,
RandomNumberGenerator& rng,
std::function<std::string ()> get_pass)
{
AlgorithmIdentifier alg_id;
secure_vector<byte> pkcs8_key = PKCS8_decode(source, get_pass, alg_id);
const std::string alg_name = OIDS::lookup(alg_id.oid);
if(alg_name == "" || alg_name == alg_id.oid.as_string())
throw PKCS8_Exception("Unknown algorithm OID: " +
alg_id.oid.as_string());
return make_private_key(alg_id, pkcs8_key, rng);
}
/*
* Extract a private key and return it
*/
Private_Key* load_key(const std::string& fsname,
RandomNumberGenerator& rng,
std::function<std::string ()> get_pass)
{
DataSource_Stream source(fsname, true);
return PKCS8::load_key(source, rng, get_pass);
}
/*
* Extract a private key and return it
*/
Private_Key* load_key(DataSource& source,
RandomNumberGenerator& rng,
const std::string& pass)
{
return PKCS8::load_key(source, rng, [pass]() { return pass; });
}
/*
* Extract a private key and return it
*/
Private_Key* load_key(const std::string& fsname,
RandomNumberGenerator& rng,
const std::string& pass)
{
return PKCS8::load_key(fsname, rng, [pass]() { return pass; });
}
/*
* Make a copy of this private key
*/
Private_Key* copy_key(const Private_Key& key,
RandomNumberGenerator& rng)
{
DataSource_Memory source(PEM_encode(key));
return PKCS8::load_key(source, rng);
}
}
}
|