forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
botan_operations.cpp
349 lines (308 loc) · 11 KB
/
botan_operations.cpp
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/**
* @file
*
* @brief cryptographic interface using the Botan library
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*
*/
#include <botan/auto_rng.h>
#include <botan/filters.h>
#include <botan/hmac.h>
#include <botan/init.h>
#include <botan/lookup.h>
#include <botan/pbkdf2.h>
#include <botan/pipe.h>
#include <botan/sha2_32.h>
#include <botan/symkey.h>
#include <kdbplugin.h>
#include <memory>
using namespace ckdb;
using namespace Botan;
using std::unique_ptr;
extern "C" {
#include "botan_operations.h"
#include "crypto.h"
#include "gpg.h"
#include "helper.h"
#include <kdbassert.h>
#include <kdberrors.h>
#include <string.h>
/**
* @brief derive the cryptographic key and IV for a given (Elektra) Key k
* @param config KeySet holding the plugin/backend configuration
* @param errorKey holds an error description in case of failure
* @param masterKey holds the decrypted master password from the plugin configuration
* @param k the (Elektra)-Key to be encrypted
* @param cKey holds a unique pointer to an allocated SymmetricKey.
* @param cIv holds a unique pointer to an allocated InitializationVector.
* @retval -1 on failure. errorKey holds the error description.
* @retval 1 on success
*/
static int getKeyIvForEncryption (KeySet * config, Key * errorKey, Key * masterKey, Key * k, unique_ptr<SymmetricKey> & cKey,
unique_ptr<InitializationVector> & cIv)
{
byte salt[ELEKTRA_CRYPTO_DEFAULT_SALT_LEN];
char * saltHexString = NULL;
const size_t requiredKeyBytes = ELEKTRA_CRYPTO_BOTAN_KEYSIZE + ELEKTRA_CRYPTO_BOTAN_BLOCKSIZE;
ELEKTRA_ASSERT (masterKey != NULL, "Parameter `masterKey` must not be NULL");
try
{
// generate the salt
AutoSeeded_RNG rng;
rng.randomize (salt, sizeof (salt));
const int encodingResult = ELEKTRA_PLUGIN_FUNCTION (base64Encode) (errorKey, salt, sizeof (salt), &saltHexString);
if (encodingResult < 0)
{
// error in libinvoke - errorKey has been set by base64Encode
return -1;
}
if (!saltHexString)
{
ELEKTRA_SET_ERROR (87, errorKey, "Memory allocation failed");
return -1;
}
keySetMeta (k, ELEKTRA_CRYPTO_META_SALT, saltHexString);
elektraFree (saltHexString);
// read iteration count
const kdb_unsigned_long_t iterations = ELEKTRA_PLUGIN_FUNCTION (getIterationCount) (errorKey, config);
// generate/derive the cryptographic key and the IV
PKCS5_PBKDF2 pbkdf (new HMAC (new SHA_256));
OctetString derived = pbkdf.derive_key (
requiredKeyBytes, std::string (reinterpret_cast<const char *> (keyValue (masterKey)), keyGetValueSize (masterKey)),
salt, sizeof (salt), iterations);
cKey = unique_ptr<SymmetricKey> (new SymmetricKey (derived.begin (), ELEKTRA_CRYPTO_BOTAN_KEYSIZE));
cIv = unique_ptr<InitializationVector> (
new InitializationVector (derived.begin () + ELEKTRA_CRYPTO_BOTAN_KEYSIZE, ELEKTRA_CRYPTO_BOTAN_BLOCKSIZE));
return 1;
}
catch (std::exception const & e)
{
ELEKTRA_SET_ERRORF (ELEKTRA_ERROR_CRYPTO_INIT, errorKey, "Failed to create a cryptographic key for encryption because: %s",
e.what ());
return -1;
}
}
/**
* @brief derive the cryptographic key and IV for a given (Elektra) Key k
* @param config KeySet holding the plugin/backend configuration
* @param errorKey holds an error description in case of failure
* @param masterKey holds the decrypted master password from the plugin configuration
* @param k the (Elektra)-Key to be encrypted
* @param cKey holds a unique pointer to an allocated SymmetricKey.
* @param cIv holds a unique pointer to an allocated InitializationVector.
* @retval -1 on failure. errorKey holds the error description.
* @retval 1 on success
*/
static int getKeyIvForDecryption (KeySet * config, Key * errorKey, Key * masterKey, Key * k, unique_ptr<SymmetricKey> & cKey,
unique_ptr<InitializationVector> & cIv)
{
const size_t requiredKeyBytes = ELEKTRA_CRYPTO_BOTAN_KEYSIZE + ELEKTRA_CRYPTO_BOTAN_BLOCKSIZE;
kdb_octet_t * saltBuffer;
kdb_unsigned_long_t saltBufferLen = 0;
ELEKTRA_ASSERT (masterKey != NULL, "Parameter `masterKey` must not be NULL");
// get the salt
if (ELEKTRA_PLUGIN_FUNCTION (getSaltFromPayload) (errorKey, k, &saltBuffer, &saltBufferLen) != 1)
{
return -1; // error set by ELEKTRA_PLUGIN_FUNCTION(getSaltFromPayload)()
}
// get the iteration count
const kdb_unsigned_long_t iterations = ELEKTRA_PLUGIN_FUNCTION (getIterationCount) (errorKey, config);
try
{
// derive the cryptographic key and the IV
PKCS5_PBKDF2 pbkdf (new HMAC (new SHA_256));
OctetString derived = pbkdf.derive_key (
requiredKeyBytes, std::string (reinterpret_cast<const char *> (keyValue (masterKey)), keyGetValueSize (masterKey)),
saltBuffer, saltBufferLen, iterations);
cKey = unique_ptr<SymmetricKey> (new SymmetricKey (derived.begin (), ELEKTRA_CRYPTO_BOTAN_KEYSIZE));
cIv = unique_ptr<InitializationVector> (
new InitializationVector (derived.begin () + ELEKTRA_CRYPTO_BOTAN_KEYSIZE, ELEKTRA_CRYPTO_BOTAN_BLOCKSIZE));
return 1;
}
catch (std::exception const & e)
{
ELEKTRA_SET_ERRORF (ELEKTRA_ERROR_CRYPTO_INIT, errorKey,
"Failed to restore the cryptographic key for decryption because: %s", e.what ());
return -1;
}
}
int elektraCryptoBotanInit (Key * errorKey)
{
try
{
LibraryInitializer::initialize ();
}
catch (std::exception const & e)
{
ELEKTRA_SET_ERRORF (ELEKTRA_ERROR_CRYPTO_INIT, errorKey, "Botan initialization failed: %s", e.what ());
return -1; // failure
}
return 1; // success
}
int elektraCryptoBotanEncrypt (KeySet * pluginConfig, Key * k, Key * errorKey, Key * masterKey)
{
// get cryptographic material
unique_ptr<SymmetricKey> cryptoKey;
unique_ptr<InitializationVector> cryptoIv;
if (getKeyIvForEncryption (pluginConfig, errorKey, masterKey, k, cryptoKey, cryptoIv) != 1)
{
return -1;
}
// prepare the salt for payload output
kdb_unsigned_long_t saltLen = 0;
kdb_octet_t * salt = NULL;
if (ELEKTRA_PLUGIN_FUNCTION (getSaltFromMetakey) (errorKey, k, &salt, &saltLen) != 1)
{
return -1; // error set by ELEKTRA_PLUGIN_FUNCTION(getSaltFromMetakey)()
}
// remove salt as metakey because it will be encoded into the crypto payload
keySetMeta (k, ELEKTRA_CRYPTO_META_SALT, NULL);
try
{
// setup pipe and crypto filter
Pipe encryptor (get_cipher (ELEKTRA_CRYPTO_BOTAN_ALGORITHM, *cryptoKey, *cryptoIv, ENCRYPTION));
kdb_octet_t flags;
switch (keyIsString (k))
{
case 1: // string
flags = ELEKTRA_CRYPTO_FLAG_STRING;
break;
case -1: // NULL pointer
flags = ELEKTRA_CRYPTO_FLAG_NULL;
break;
default: // binary
flags = ELEKTRA_CRYPTO_FLAG_NONE;
break;
}
// encryption process
encryptor.start_msg ();
encryptor.write (static_cast<const byte *> (&flags), sizeof (kdb_octet_t));
if (flags == ELEKTRA_CRYPTO_FLAG_STRING)
{
const std::string stringVal (keyString (k));
encryptor.write (stringVal);
}
else if (flags == ELEKTRA_CRYPTO_FLAG_NONE && keyGetValueSize (k) > 0)
{
encryptor.write (static_cast<const byte *> (keyValue (k)), keyGetValueSize (k));
}
encryptor.end_msg ();
// write the salt and the encrypted data back to the Key
const size_t msgLength = encryptor.remaining ();
if (msgLength > 0)
{
auto buffer = unique_ptr<byte[]>{
new byte[msgLength + ELEKTRA_CRYPTO_MAGIC_NUMBER_LEN + sizeof (kdb_unsigned_long_t) + saltLen]
};
size_t bufferIndex = 0;
memcpy (&buffer[bufferIndex], ELEKTRA_CRYPTO_MAGIC_NUMBER, ELEKTRA_CRYPTO_MAGIC_NUMBER_LEN);
bufferIndex += ELEKTRA_CRYPTO_MAGIC_NUMBER_LEN;
memcpy (&buffer[bufferIndex], &saltLen, sizeof (kdb_unsigned_long_t));
bufferIndex += sizeof (kdb_unsigned_long_t);
memcpy (&buffer[bufferIndex], salt, saltLen);
bufferIndex += saltLen;
const size_t buffered = encryptor.read (&buffer[bufferIndex], msgLength);
keySetBinary (k, &buffer[0], buffered + bufferIndex);
}
}
catch (std::exception const & e)
{
ELEKTRA_SET_ERRORF (ELEKTRA_ERROR_CRYPTO_ENCRYPT_FAIL, errorKey, "Encryption failed because: %s", e.what ());
elektraFree (salt);
return -1; // failure
}
elektraFree (salt);
return 1; // success
}
int elektraCryptoBotanDecrypt (KeySet * pluginConfig, Key * k, Key * errorKey, Key * masterKey)
{
// get cryptographic material
unique_ptr<SymmetricKey> cryptoKey;
unique_ptr<InitializationVector> cryptoIv;
if (getKeyIvForDecryption (pluginConfig, errorKey, masterKey, k, cryptoKey, cryptoIv) != 1)
{
return -1;
}
// parse salt length from crypto payload
kdb_unsigned_long_t saltLen = 0;
if (ELEKTRA_PLUGIN_FUNCTION (getSaltFromPayload) (errorKey, k, NULL, &saltLen) != 1)
{
return -1; // error set by ELEKTRA_PLUGIN_FUNCTION(getSaltFromPayload)()
}
saltLen += sizeof (kdb_unsigned_long_t);
// set payload pointer
const byte * payload = reinterpret_cast<const byte *> (keyValue (k)) + saltLen + ELEKTRA_CRYPTO_MAGIC_NUMBER_LEN;
const size_t payloadLen = keyGetValueSize (k) - saltLen - ELEKTRA_CRYPTO_MAGIC_NUMBER_LEN;
try
{
// setup pipe and crypto filter
Pipe decryptor (get_cipher (ELEKTRA_CRYPTO_BOTAN_ALGORITHM, *cryptoKey, *cryptoIv, DECRYPTION));
kdb_octet_t flags = ELEKTRA_CRYPTO_FLAG_NONE;
// decrypt the content
decryptor.process_msg (payload, payloadLen);
if (decryptor.remaining () > 0)
{
// decode the "header" flags
const size_t flagLength = decryptor.read (static_cast<byte *> (&flags), sizeof (kdb_octet_t));
if (flagLength != sizeof (kdb_octet_t))
{
throw std::length_error ("Failed to restore the original data type of the value.");
}
}
const size_t msgLength = decryptor.remaining ();
if (msgLength > 0)
{
if (flags == ELEKTRA_CRYPTO_FLAG_STRING)
{
const std::string stringVal = decryptor.read_all_as_string ();
keySetString (k, stringVal.c_str ());
}
else
{
auto buffer = unique_ptr<byte[]>{ new byte[msgLength] };
const size_t buffered = decryptor.read (&buffer[0], msgLength);
keySetBinary (k, &buffer[0], buffered);
}
}
else
{
keySetBinary (k, NULL, 0);
}
}
catch (std::exception const & e)
{
ELEKTRA_SET_ERRORF (ELEKTRA_ERROR_CRYPTO_DECRYPT_FAIL, errorKey, "Decryption failed because: %s", e.what ());
return -1; // failure
}
return 1; // success
}
/**
* @brief create a random sequence of characters with given length.
* @param errorKey holds an error description in case of failure.
* @param length the number of random bytes to be generated.
* @returns allocated buffer holding a hex-encoded random string or NULL in case of error. Must be freed by the caller.
*/
char * elektraCryptoBotanCreateRandomString (Key * errorKey, const kdb_unsigned_short_t length)
{
try
{
char * hexString = NULL;
auto buffer = unique_ptr<kdb_octet_t[]>{ new kdb_octet_t[length] };
AutoSeeded_RNG rng;
rng.randomize (&buffer[0], length);
if (ELEKTRA_PLUGIN_FUNCTION (base64Encode) (errorKey, &buffer[0], length, &hexString) < 0)
{
// error in libinvoke - errorKey has been set by base64Encode
return 0;
}
return hexString;
}
catch (std::exception const & e)
{
ELEKTRA_SET_ERRORF (ELEKTRA_ERROR_CRYPTO_INTERNAL_ERROR, errorKey, "Failed to generate random string because: %s",
e.what ());
return 0;
}
}
} // extern "C"