-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCrypto.cc
398 lines (339 loc) · 9.88 KB
/
Crypto.cc
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
// vim: ts=8 sw=2 smarttab
/*
* Ceph - scalable distributed file system
*
* Copyright (C) 2004-2009 Sage Weil <[email protected]>
*
* This is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software
* Foundation. See file COPYING.
*
*/
#include <sstream>
#include "Crypto.h"
#ifdef USE_CRYPTOPP
# include <cryptopp/modes.h>
# include <cryptopp/aes.h>
# include <cryptopp/filters.h>
#elif USE_NSS
# include <nspr.h>
# include <nss.h>
# include <pk11pub.h>
#endif
#include "include/assert.h"
#include "common/Clock.h"
#include "common/armor.h"
#include "common/ceph_crypto.h"
#include "common/config.h"
#include "common/debug.h"
#include "common/hex.h"
#include "common/safe_io.h"
#include "include/ceph_fs.h"
#include "include/compat.h"
#include <errno.h>
int get_random_bytes(char *buf, int len)
{
int fd = TEMP_FAILURE_RETRY(::open("/dev/urandom", O_RDONLY));
if (fd < 0)
return -errno;
int ret = safe_read_exact(fd, buf, len);
TEMP_FAILURE_RETRY(::close(fd));
return ret;
}
static int get_random_bytes(int len, bufferlist& bl)
{
char buf[len];
get_random_bytes(buf, len);
bl.append(buf, len);
return 0;
}
uint64_t get_random(uint64_t min_val, uint64_t max_val)
{
uint64_t r;
get_random_bytes((char *)&r, sizeof(r));
r = min_val + r % (max_val - min_val + 1);
return r;
}
// ---------------------------------------------------
int CryptoNone::create(bufferptr& secret)
{
return 0;
}
int CryptoNone::validate_secret(bufferptr& secret)
{
return 0;
}
void CryptoNone::encrypt(const bufferptr& secret, const bufferlist& in,
bufferlist& out, std::string &error) const
{
out = in;
}
void CryptoNone::decrypt(const bufferptr& secret, const bufferlist& in,
bufferlist& out, std::string &error) const
{
out = in;
}
// ---------------------------------------------------
#ifdef USE_CRYPTOPP
# define AES_KEY_LEN ((size_t)CryptoPP::AES::DEFAULT_KEYLENGTH)
# define AES_BLOCK_LEN ((size_t)CryptoPP::AES::BLOCKSIZE)
#elif USE_NSS
// when we say AES, we mean AES-128
# define AES_KEY_LEN 16
# define AES_BLOCK_LEN 16
static void nss_aes_operation(CK_ATTRIBUTE_TYPE op, const bufferptr& secret,
const bufferlist& in, bufferlist& out, std::string &error)
{
const CK_MECHANISM_TYPE mechanism = CKM_AES_CBC_PAD;
// sample source said this has to be at least size of input + 8,
// but i see 15 still fail with SEC_ERROR_OUTPUT_LEN
bufferptr out_tmp(in.length()+16);
PK11SlotInfo *slot;
slot = PK11_GetBestSlot(mechanism, NULL);
if (!slot) {
ostringstream oss;
oss << "cannot find NSS slot to use: " << PR_GetError();
error = oss.str();
goto err;
}
SECItem keyItem;
keyItem.type = siBuffer;
keyItem.data = (unsigned char*)secret.c_str();
keyItem.len = secret.length();
PK11SymKey *key;
key = PK11_ImportSymKey(slot, mechanism, PK11_OriginUnwrap, CKA_ENCRYPT,
&keyItem, NULL);
if (!key) {
ostringstream oss;
oss << "cannot convert AES key for NSS: " << PR_GetError();
error = oss.str();
goto err_slot;
}
SECItem ivItem;
ivItem.type = siBuffer;
// losing constness due to SECItem.data; IV should never be
// modified, regardless
ivItem.data = (unsigned char*)CEPH_AES_IV;
ivItem.len = sizeof(CEPH_AES_IV);
SECItem *param;
param = PK11_ParamFromIV(mechanism, &ivItem);
if (!param) {
ostringstream oss;
oss << "cannot set NSS IV param: " << PR_GetError();
error = oss.str();
goto err_key;
}
PK11Context *ctx;
ctx = PK11_CreateContextBySymKey(mechanism, op, key, param);
if (!ctx) {
ostringstream oss;
oss << "cannot create NSS context: " << PR_GetError();
error = oss.str();
goto err_param;
}
SECStatus ret;
int written;
// in is const, and PK11_CipherOp is not; C++ makes this hard to cheat,
// so just copy it to a temp buffer, at least for now
unsigned in_len;
unsigned char *in_buf;
in_len = in.length();
in_buf = (unsigned char*)malloc(in_len);
if (!in_buf)
throw std::bad_alloc();
in.copy(0, in_len, (char*)in_buf);
ret = PK11_CipherOp(ctx, (unsigned char*)out_tmp.c_str(), &written, out_tmp.length(),
in_buf, in.length());
free(in_buf);
if (ret != SECSuccess) {
ostringstream oss;
oss << "NSS AES failed: " << PR_GetError();
error = oss.str();
goto err_op;
}
unsigned int written2;
ret = PK11_DigestFinal(ctx, (unsigned char*)out_tmp.c_str()+written, &written2,
out_tmp.length()-written);
if (ret != SECSuccess) {
ostringstream oss;
oss << "NSS AES final round failed: " << PR_GetError();
error = oss.str();
goto err_op;
}
out_tmp.set_length(written + written2);
out.append(out_tmp);
PK11_DestroyContext(ctx, PR_TRUE);
SECITEM_FreeItem(param, PR_TRUE);
PK11_FreeSymKey(key);
PK11_FreeSlot(slot);
return;
err_op:
PK11_DestroyContext(ctx, PR_TRUE);
err_param:
SECITEM_FreeItem(param, PR_TRUE);
err_key:
PK11_FreeSymKey(key);
err_slot:
PK11_FreeSlot(slot);
err:
;
}
#else
# error "No supported crypto implementation found."
#endif
int CryptoAES::create(bufferptr& secret)
{
bufferlist bl;
int r = get_random_bytes(AES_KEY_LEN, bl);
if (r < 0)
return r;
secret = buffer::ptr(bl.c_str(), bl.length());
return 0;
}
int CryptoAES::validate_secret(bufferptr& secret)
{
if (secret.length() < (size_t)AES_KEY_LEN) {
return -EINVAL;
}
return 0;
}
void CryptoAES::encrypt(const bufferptr& secret, const bufferlist& in, bufferlist& out,
std::string &error) const
{
if (secret.length() < AES_KEY_LEN) {
error = "key is too short";
return;
}
#ifdef USE_CRYPTOPP
{
const unsigned char *key = (const unsigned char *)secret.c_str();
string ciphertext;
CryptoPP::AES::Encryption aesEncryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption( aesEncryption, (const byte*)CEPH_AES_IV );
CryptoPP::StringSink *sink = new CryptoPP::StringSink(ciphertext);
CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, sink);
for (std::list<bufferptr>::const_iterator it = in.buffers().begin();
it != in.buffers().end(); ++it) {
const unsigned char *in_buf = (const unsigned char *)it->c_str();
stfEncryptor.Put(in_buf, it->length());
}
try {
stfEncryptor.MessageEnd();
} catch (CryptoPP::Exception& e) {
ostringstream oss;
oss << "encryptor.MessageEnd::Exception: " << e.GetWhat();
error = oss.str();
return;
}
out.append((const char *)ciphertext.c_str(), ciphertext.length());
}
#elif USE_NSS
nss_aes_operation(CKA_ENCRYPT, secret, in, out, error);
#else
# error "No supported crypto implementation found."
#endif
}
void CryptoAES::decrypt(const bufferptr& secret, const bufferlist& in,
bufferlist& out, std::string &error) const
{
#ifdef USE_CRYPTOPP
const unsigned char *key = (const unsigned char *)secret.c_str();
CryptoPP::AES::Decryption aesDecryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption( aesDecryption, (const byte*)CEPH_AES_IV );
string decryptedtext;
CryptoPP::StringSink *sink = new CryptoPP::StringSink(decryptedtext);
CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, sink);
for (std::list<bufferptr>::const_iterator it = in.buffers().begin();
it != in.buffers().end(); ++it) {
const unsigned char *in_buf = (const unsigned char *)it->c_str();
stfDecryptor.Put(in_buf, it->length());
}
try {
stfDecryptor.MessageEnd();
} catch (CryptoPP::Exception& e) {
ostringstream oss;
oss << "decryptor.MessageEnd::Exception: " << e.GetWhat();
error = oss.str();
return;
}
out.append((const char *)decryptedtext.c_str(), decryptedtext.length());
#elif USE_NSS
nss_aes_operation(CKA_DECRYPT, secret, in, out, error);
#else
# error "No supported crypto implementation found."
#endif
}
// ---------------------------------------------------
int CryptoKey::set_secret(CephContext *cct, int type, bufferptr& s)
{
this->type = type;
created = ceph_clock_now(cct);
CryptoHandler *h = cct->get_crypto_handler(type);
if (!h) {
lderr(cct) << "ERROR: cct->get_crypto_handler(type=" << type << ") returned NULL" << dendl;
return -EOPNOTSUPP;
}
int ret = h->validate_secret(s);
if (ret < 0)
return ret;
secret = s;
return 0;
}
int CryptoKey::create(CephContext *cct, int t)
{
type = t;
created = ceph_clock_now(cct);
CryptoHandler *h = cct->get_crypto_handler(type);
if (!h) {
lderr(cct) << "ERROR: cct->get_crypto_handler(type=" << type << ") returned NULL" << dendl;
return -EOPNOTSUPP;
}
return h->create(secret);
}
void CryptoKey::encrypt(CephContext *cct, const bufferlist& in, bufferlist& out, std::string &error) const
{
if (!ch || ch->get_type() != type) {
ch = cct->get_crypto_handler(type);
if (!ch) {
ostringstream oss;
oss << "CryptoKey::encrypt: key type " << type << " not supported.";
return;
}
}
ch->encrypt(this->secret, in, out, error);
}
void CryptoKey::decrypt(CephContext *cct, const bufferlist& in, bufferlist& out, std::string &error) const
{
if (!ch || ch->get_type() != type) {
ch = cct->get_crypto_handler(type);
if (!ch) {
ostringstream oss;
oss << "CryptoKey::decrypt: key type " << type << " not supported.";
return;
}
}
ch->decrypt(this->secret, in, out, error);
}
void CryptoKey::print(std::ostream &out) const
{
out << encode_base64();
}
void CryptoKey::to_str(std::string& s) const
{
int len = secret.length() * 4;
char buf[len];
hex2str(secret.c_str(), secret.length(), buf, len);
s = buf;
}
void CryptoKey::encode_formatted(string label, Formatter *f, bufferlist &bl)
{
f->open_object_section(label.c_str());
f->dump_string("key", encode_base64());
f->close_section();
f->flush(bl);
}
void CryptoKey::encode_plaintext(bufferlist &bl)
{
bl.append(encode_base64());
}