Move PKCS#12 key derivation to a separate file
[strongswan.git] / src / libstrongswan / crypto / pkcs5.c
1 /*
2 * Copyright (C) 2012-2013 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "pkcs5.h"
17
18 #include <utils/debug.h>
19 #include <asn1/oid.h>
20 #include <asn1/asn1.h>
21 #include <asn1/asn1_parser.h>
22 #include <credentials/containers/pkcs12.h>
23
24 typedef struct private_pkcs5_t private_pkcs5_t;
25
26 /**
27 * Private data of a pkcs5_t object
28 */
29 struct private_pkcs5_t {
30
31 /**
32 * Implements pkcs5_t.
33 */
34 pkcs5_t public;
35
36 /**
37 * Salt used during encryption
38 */
39 chunk_t salt;
40
41 /**
42 * Iterations for key derivation
43 */
44 u_int64_t iterations;
45
46 /**
47 * Encryption algorithm
48 */
49 encryption_algorithm_t encr;
50
51 /**
52 * Encryption key length
53 */
54 size_t keylen;
55
56 /**
57 * Crypter
58 */
59 crypter_t *crypter;
60
61
62 /**
63 * The encryption scheme
64 */
65 enum {
66 PKCS5_SCHEME_PBES1,
67 PKCS5_SCHEME_PBES2,
68 PKCS5_SCHEME_PKCS12,
69 } scheme;
70
71 /**
72 * Data used for individual schemes
73 */
74 union {
75 struct {
76 /**
77 * Hash algorithm
78 */
79 hash_algorithm_t hash;
80
81 /**
82 * Hasher
83 */
84 hasher_t *hasher;
85
86 } pbes1;
87 struct {
88 /**
89 * PRF algorithm
90 */
91 pseudo_random_function_t prf_alg;
92
93 /**
94 * PRF
95 */
96 prf_t * prf;
97
98 /**
99 * IV
100 */
101 chunk_t iv;
102
103 } pbes2;
104 } data;
105 };
106
107 /**
108 * Verify padding of decrypted blob.
109 * Length of blob is adjusted accordingly.
110 */
111 static bool verify_padding(chunk_t *blob)
112 {
113 u_int8_t padding, count;
114
115 padding = count = blob->ptr[blob->len - 1];
116
117 if (padding > 8)
118 {
119 return FALSE;
120 }
121 for (; blob->len && count; --blob->len, --count)
122 {
123 if (blob->ptr[blob->len - 1] != padding)
124 {
125 return FALSE;
126 }
127 }
128 return TRUE;
129 }
130
131 /**
132 * Prototype for key derivation functions.
133 */
134 typedef bool (*kdf_t)(private_pkcs5_t *this, chunk_t password, chunk_t key);
135
136 /**
137 * Try to decrypt the given data with the given password using the given
138 * key derivation function. keymat is where the kdf function writes the key
139 * to, key and iv point to the actual keys and initialization vectors resp.
140 */
141 static bool decrypt_generic(private_pkcs5_t *this, chunk_t password,
142 chunk_t data, chunk_t *decrypted, kdf_t kdf,
143 chunk_t keymat, chunk_t key, chunk_t iv)
144 {
145 if (!kdf(this, password, keymat))
146 {
147 return FALSE;
148 }
149 if (!this->crypter->set_key(this->crypter, key) ||
150 !this->crypter->decrypt(this->crypter, data, iv, decrypted))
151 {
152 memwipe(keymat.ptr, keymat.len);
153 return FALSE;
154 }
155 memwipe(keymat.ptr, keymat.len);
156 if (verify_padding(decrypted))
157 {
158 return TRUE;
159 }
160 chunk_free(decrypted);
161 return FALSE;
162 }
163
164 /**
165 * KDF as used by PKCS#12
166 */
167 static bool pkcs12_kdf(private_pkcs5_t *this, chunk_t password, chunk_t keymat)
168 {
169 chunk_t key, iv;
170
171 key = chunk_create(keymat.ptr, this->keylen);
172 iv = chunk_create(keymat.ptr + this->keylen, keymat.len - this->keylen);
173
174 return pkcs12_derive_key(this->data.pbes1.hash, password, this->salt,
175 this->iterations, PKCS12_KEY_ENCRYPTION, key) &&
176 pkcs12_derive_key(this->data.pbes1.hash, password, this->salt,
177 this->iterations, PKCS12_KEY_IV, iv);
178 }
179
180 /**
181 * Function F of PBKDF2
182 */
183 static bool pbkdf2_f(chunk_t block, prf_t *prf, chunk_t seed,
184 u_int64_t iterations)
185 {
186 chunk_t u;
187 u_int64_t i;
188
189 u = chunk_alloca(prf->get_block_size(prf));
190 if (!prf->get_bytes(prf, seed, u.ptr))
191 {
192 return FALSE;
193 }
194 memcpy(block.ptr, u.ptr, block.len);
195
196 for (i = 1; i < iterations; i++)
197 {
198 if (!prf->get_bytes(prf, u, u.ptr))
199 {
200 return FALSE;
201 }
202 memxor(block.ptr, u.ptr, block.len);
203 }
204 return TRUE;
205 }
206
207 /**
208 * PBKDF2 key derivation function for PBES2, key must be allocated
209 */
210 static bool pbkdf2(private_pkcs5_t *this, chunk_t password, chunk_t key)
211 {
212 prf_t *prf;
213 chunk_t keymat, block, seed;
214 size_t blocks;
215 u_int32_t i = 0;
216
217 prf = this->data.pbes2.prf;
218
219 if (!prf->set_key(prf, password))
220 {
221 return FALSE;
222 }
223
224 block.len = prf->get_block_size(prf);
225 blocks = (key.len - 1) / block.len + 1;
226 keymat = chunk_alloca(blocks * block.len);
227
228 seed = chunk_cata("cc", this->salt, chunk_from_thing(i));
229
230 for (; i < blocks; i++)
231 {
232 htoun32(seed.ptr + this->salt.len, i + 1);
233 block.ptr = keymat.ptr + (i * block.len);
234 if (!pbkdf2_f(block, prf, seed, this->iterations))
235 {
236 return FALSE;
237 }
238 }
239 memcpy(key.ptr, keymat.ptr, key.len);
240 return TRUE;
241 }
242
243 /**
244 * PBKDF1 key derivation function for PBES1, key must be allocated
245 */
246 static bool pbkdf1(private_pkcs5_t *this, chunk_t password, chunk_t key)
247 {
248 hasher_t *hasher;
249 chunk_t hash;
250 u_int64_t i;
251
252 hasher = this->data.pbes1.hasher;
253
254 hash = chunk_alloca(hasher->get_hash_size(hasher));
255 if (!hasher->get_hash(hasher, password, NULL) ||
256 !hasher->get_hash(hasher, this->salt, hash.ptr))
257 {
258 return FALSE;
259 }
260
261 for (i = 1; i < this->iterations; i++)
262 {
263 if (!hasher->get_hash(hasher, hash, hash.ptr))
264 {
265 return FALSE;
266 }
267 }
268 memcpy(key.ptr, hash.ptr, key.len);
269 return TRUE;
270 }
271
272 static bool ensure_crypto_primitives(private_pkcs5_t *this, chunk_t data)
273 {
274 if (!this->crypter)
275 {
276 this->crypter = lib->crypto->create_crypter(lib->crypto, this->encr,
277 this->keylen);
278 if (!this->crypter)
279 {
280 DBG1(DBG_ASN, " %N encryption algorithm not available",
281 encryption_algorithm_names, this->encr);
282 return FALSE;
283 }
284 }
285 if (data.len % this->crypter->get_block_size(this->crypter))
286 {
287 DBG1(DBG_ASN, " data size is not a multiple of block size");
288 return FALSE;
289 }
290 switch (this->scheme)
291 {
292 case PKCS5_SCHEME_PBES1:
293 {
294 if (!this->data.pbes1.hasher)
295 {
296 hasher_t *hasher;
297
298 hasher = lib->crypto->create_hasher(lib->crypto,
299 this->data.pbes1.hash);
300 if (!hasher)
301 {
302 DBG1(DBG_ASN, " %N hash algorithm not available",
303 hash_algorithm_names, this->data.pbes1.hash);
304 return FALSE;
305 }
306 if (hasher->get_hash_size(hasher) < this->keylen)
307 {
308 hasher->destroy(hasher);
309 return FALSE;
310 }
311 this->data.pbes1.hasher = hasher;
312 }
313 }
314 case PKCS5_SCHEME_PBES2:
315 {
316 if (!this->data.pbes2.prf)
317 {
318 prf_t *prf;
319
320 prf = lib->crypto->create_prf(lib->crypto,
321 this->data.pbes2.prf_alg);
322 if (!prf)
323 {
324 DBG1(DBG_ASN, " %N prf algorithm not available",
325 pseudo_random_function_names,
326 this->data.pbes2.prf_alg);
327 return FALSE;
328 }
329 this->data.pbes2.prf = prf;
330 }
331 }
332 case PKCS5_SCHEME_PKCS12:
333 break;
334 }
335 return TRUE;
336 }
337
338 METHOD(pkcs5_t, decrypt, bool,
339 private_pkcs5_t *this, chunk_t password, chunk_t data, chunk_t *decrypted)
340 {
341 chunk_t keymat, key, iv;
342 kdf_t kdf;
343
344 if (!ensure_crypto_primitives(this, data) || !decrypted)
345 {
346 return FALSE;
347 }
348 kdf = pbkdf1;
349 switch (this->scheme)
350 {
351 case PKCS5_SCHEME_PKCS12:
352 kdf = pkcs12_kdf;
353 /* fall-through */
354 case PKCS5_SCHEME_PBES1:
355 keymat = chunk_alloca(this->keylen +
356 this->crypter->get_iv_size(this->crypter));
357 key = chunk_create(keymat.ptr, this->keylen);
358 iv = chunk_create(keymat.ptr + this->keylen,
359 keymat.len - this->keylen);
360 break;
361 case PKCS5_SCHEME_PBES2:
362 kdf = pbkdf2;
363 keymat = chunk_alloca(this->keylen);
364 key = keymat;
365 iv = this->data.pbes2.iv;
366 break;
367 default:
368 return FALSE;
369 }
370 return decrypt_generic(this, password, data, decrypted, kdf,
371 keymat, key, iv);
372 }
373
374 /**
375 * ASN.1 definition of a PBEParameter structure
376 */
377 static const asn1Object_t pbeParameterObjects[] = {
378 { 0, "PBEParameter", ASN1_SEQUENCE, ASN1_NONE }, /* 0 */
379 { 1, "salt", ASN1_OCTET_STRING, ASN1_BODY }, /* 1 */
380 { 1, "iterationCount", ASN1_INTEGER, ASN1_BODY }, /* 2 */
381 { 0, "exit", ASN1_EOC, ASN1_EXIT }
382 };
383 #define PBEPARAM_SALT 1
384 #define PBEPARAM_ITERATION_COUNT 2
385
386 /**
387 * Parse a PBEParameter structure
388 */
389 static bool parse_pbes1_params(private_pkcs5_t *this, chunk_t blob, int level0)
390 {
391 asn1_parser_t *parser;
392 chunk_t object;
393 int objectID;
394 bool success;
395
396 parser = asn1_parser_create(pbeParameterObjects, blob);
397 parser->set_top_level(parser, level0);
398
399 while (parser->iterate(parser, &objectID, &object))
400 {
401 switch (objectID)
402 {
403 case PBEPARAM_SALT:
404 {
405 this->salt = chunk_clone(object);
406 break;
407 }
408 case PBEPARAM_ITERATION_COUNT:
409 {
410 this->iterations = asn1_parse_integer_uint64(object);
411 break;
412 }
413 }
414 }
415 success = parser->success(parser);
416 parser->destroy(parser);
417 return success;
418 }
419
420 /**
421 * ASN.1 definition of a PBKDF2-params structure
422 * The salt is actually a CHOICE and could be an AlgorithmIdentifier from
423 * PBKDF2-SaltSources (but as per RFC 2898 that's for future versions).
424 */
425 static const asn1Object_t pbkdf2ParamsObjects[] = {
426 { 0, "PBKDF2-params", ASN1_SEQUENCE, ASN1_NONE }, /* 0 */
427 { 1, "salt", ASN1_OCTET_STRING, ASN1_BODY }, /* 1 */
428 { 1, "iterationCount",ASN1_INTEGER, ASN1_BODY }, /* 2 */
429 { 1, "keyLength", ASN1_INTEGER, ASN1_OPT|ASN1_BODY }, /* 3 */
430 { 1, "end opt", ASN1_EOC, ASN1_END }, /* 4 */
431 { 1, "prf", ASN1_EOC, ASN1_DEF|ASN1_RAW }, /* 5 */
432 { 0, "exit", ASN1_EOC, ASN1_EXIT }
433 };
434 #define PBKDF2_SALT 1
435 #define PBKDF2_ITERATION_COUNT 2
436 #define PBKDF2_KEYLENGTH 3
437 #define PBKDF2_PRF 5
438
439 /**
440 * Parse a PBKDF2-params structure
441 */
442 static bool parse_pbkdf2_params(private_pkcs5_t *this, chunk_t blob, int level0)
443 {
444 asn1_parser_t *parser;
445 chunk_t object;
446 int objectID;
447 bool success;
448
449 parser = asn1_parser_create(pbkdf2ParamsObjects, blob);
450 parser->set_top_level(parser, level0);
451
452 /* keylen is optional */
453 this->keylen = 0;
454
455 while (parser->iterate(parser, &objectID, &object))
456 {
457 switch (objectID)
458 {
459 case PBKDF2_SALT:
460 {
461 this->salt = chunk_clone(object);
462 break;
463 }
464 case PBKDF2_ITERATION_COUNT:
465 {
466 this->iterations = asn1_parse_integer_uint64(object);
467 break;
468 }
469 case PBKDF2_KEYLENGTH:
470 {
471 this->keylen = (size_t)asn1_parse_integer_uint64(object);
472 break;
473 }
474 case PBKDF2_PRF:
475 { /* defaults to id-hmacWithSHA1, no other is currently defined */
476 this->data.pbes2.prf_alg = PRF_HMAC_SHA1;
477 break;
478 }
479 }
480 }
481 success = parser->success(parser);
482 parser->destroy(parser);
483 return success;
484 }
485
486 /**
487 * ASN.1 definition of a PBES2-params structure
488 */
489 static const asn1Object_t pbes2ParamsObjects[] = {
490 { 0, "PBES2-params", ASN1_SEQUENCE, ASN1_NONE }, /* 0 */
491 { 1, "keyDerivationFunc", ASN1_EOC, ASN1_RAW }, /* 1 */
492 { 1, "encryptionScheme", ASN1_EOC, ASN1_RAW }, /* 2 */
493 { 0, "exit", ASN1_EOC, ASN1_EXIT }
494 };
495 #define PBES2PARAMS_KEY_DERIVATION_FUNC 1
496 #define PBES2PARAMS_ENCRYPTION_SCHEME 2
497
498 /**
499 * Parse a PBES2-params structure
500 */
501 static bool parse_pbes2_params(private_pkcs5_t *this, chunk_t blob, int level0)
502 {
503 asn1_parser_t *parser;
504 chunk_t object, params;
505 int objectID;
506 bool success = FALSE;
507
508 parser = asn1_parser_create(pbes2ParamsObjects, blob);
509 parser->set_top_level(parser, level0);
510
511 while (parser->iterate(parser, &objectID, &object))
512 {
513 switch (objectID)
514 {
515 case PBES2PARAMS_KEY_DERIVATION_FUNC:
516 {
517 int oid = asn1_parse_algorithmIdentifier(object,
518 parser->get_level(parser) + 1, &params);
519 if (oid != OID_PBKDF2)
520 { /* unsupported key derivation function */
521 goto end;
522 }
523 if (!parse_pbkdf2_params(this, params,
524 parser->get_level(parser) + 1))
525 {
526 goto end;
527 }
528 break;
529 }
530 case PBES2PARAMS_ENCRYPTION_SCHEME:
531 {
532 int oid = asn1_parse_algorithmIdentifier(object,
533 parser->get_level(parser) + 1, &params);
534 if (oid != OID_3DES_EDE_CBC)
535 { /* unsupported encryption scheme */
536 goto end;
537 }
538 if (this->keylen <= 0)
539 { /* default key length for DES-EDE3-CBC-Pad */
540 this->keylen = 24;
541 }
542 if (!asn1_parse_simple_object(&params, ASN1_OCTET_STRING,
543 parser->get_level(parser) + 1, "IV"))
544 {
545 goto end;
546 }
547 this->encr = ENCR_3DES;
548 this->data.pbes2.iv = chunk_clone(params);
549 break;
550 }
551 }
552 }
553 success = parser->success(parser);
554 end:
555 parser->destroy(parser);
556 return success;
557 }
558
559 METHOD(pkcs5_t, destroy, void,
560 private_pkcs5_t *this)
561 {
562 DESTROY_IF(this->crypter);
563 chunk_free(&this->salt);
564 switch (this->scheme)
565 {
566 case PKCS5_SCHEME_PBES1:
567 DESTROY_IF(this->data.pbes1.hasher);
568 break;
569 case PKCS5_SCHEME_PBES2:
570 DESTROY_IF(this->data.pbes2.prf);
571 chunk_free(&this->data.pbes2.iv);
572 break;
573 case PKCS5_SCHEME_PKCS12:
574 break;
575 }
576 free(this);
577 }
578
579 /*
580 * Described in header
581 */
582 pkcs5_t *pkcs5_from_algorithmIdentifier(chunk_t blob, int level0)
583 {
584 private_pkcs5_t *this;
585 chunk_t params;
586 int oid;
587
588 INIT(this,
589 .public = {
590 .decrypt = _decrypt,
591 .destroy = _destroy,
592 },
593 .scheme = PKCS5_SCHEME_PBES1,
594 .keylen = 8,
595 );
596
597 oid = asn1_parse_algorithmIdentifier(blob, level0, &params);
598
599 switch (oid)
600 {
601 case OID_PBE_MD5_DES_CBC:
602 this->encr = ENCR_DES;
603 this->data.pbes1.hash = HASH_MD5;
604 break;
605 case OID_PBE_SHA1_DES_CBC:
606 this->encr = ENCR_DES;
607 this->data.pbes1.hash = HASH_SHA1;
608 break;
609 case OID_PBE_SHA1_RC2_CBC_40:
610 this->scheme = PKCS5_SCHEME_PKCS12;
611 this->keylen = 5;
612 this->encr = ENCR_RC2_CBC;
613 this->data.pbes1.hash = HASH_SHA1;
614 break;
615 case OID_PBES2:
616 this->scheme = PKCS5_SCHEME_PBES2;
617 break;
618 default:
619 /* encryption scheme not supported */
620 goto failure;
621 }
622
623 switch (this->scheme)
624 {
625 case PKCS5_SCHEME_PBES1:
626 case PKCS5_SCHEME_PKCS12:
627 if (!parse_pbes1_params(this, params, level0))
628 {
629 goto failure;
630 }
631 break;
632 case PKCS5_SCHEME_PBES2:
633 if (!parse_pbes2_params(this, params, level0))
634 {
635 goto failure;
636 }
637 break;
638 }
639 return &this->public;
640
641 failure:
642 destroy(this);
643 return NULL;
644 }