aboutsummaryrefslogtreecommitdiffstats
path: root/src/libstrongswan/crypto/pkcs5.c
blob: 8a145242507b43c1ee0aa25d2278c6d76afc8df7 (plain)
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
/*
 * Copyright (C) 2012-2013 Tobias Brunner
 * Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include "pkcs5.h"

#include <utils/debug.h>
#include <asn1/oid.h>
#include <asn1/asn1.h>
#include <asn1/asn1_parser.h>
#include <credentials/containers/pkcs12.h>

typedef struct private_pkcs5_t private_pkcs5_t;

/**
 * Private data of a pkcs5_t object
 */
struct private_pkcs5_t {

	/**
	 * Implements pkcs5_t.
	 */
	pkcs5_t public;

	/**
	 * Salt used during encryption
	 */
	chunk_t salt;

	/**
	 * Iterations for key derivation
	 */
	uint64_t iterations;

	/**
	 * Encryption algorithm
	 */
	encryption_algorithm_t encr;

	/**
	 * Encryption key length
	 */
	size_t keylen;

	/**
	 * Crypter
	 */
	crypter_t *crypter;


	/**
	 * The encryption scheme
	 */
	enum {
		PKCS5_SCHEME_PBES1,
		PKCS5_SCHEME_PBES2,
		PKCS5_SCHEME_PKCS12,
	} scheme;

	/**
	 * Data used for individual schemes
	 */
	union {
		struct {
			/**
			 * Hash algorithm
			 */
			hash_algorithm_t hash;

			/**
			 * Hasher
			 */
			hasher_t *hasher;

		} pbes1;
		struct {
			/**
			 * PRF algorithm
			 */
			pseudo_random_function_t prf_alg;

			/**
			 * PRF
			 */
			prf_t * prf;

			/**
			 * IV
			 */
			chunk_t iv;

		} pbes2;
	} data;
};

/**
 * Verify padding of decrypted blob.
 * Length of blob is adjusted accordingly.
 */
static bool verify_padding(crypter_t *crypter, chunk_t *blob)
{
	uint8_t padding, count;

	padding = count = blob->ptr[blob->len - 1];

	if (padding > crypter->get_block_size(crypter))
	{
		return FALSE;
	}
	for (; blob->len && count; --blob->len, --count)
	{
		if (blob->ptr[blob->len - 1] != padding)
		{
			return FALSE;
		}
	}
	return TRUE;
}

/**
 * Prototype for key derivation functions.
 */
typedef bool (*kdf_t)(private_pkcs5_t *this, chunk_t password, chunk_t key);

/**
 * Try to decrypt the given data with the given password using the given
 * key derivation function. keymat is where the kdf function writes the key
 * to, key and iv point to the actual keys and initialization vectors resp.
 */
static bool decrypt_generic(private_pkcs5_t *this, chunk_t password,
							chunk_t data, chunk_t *decrypted, kdf_t kdf,
							chunk_t keymat, chunk_t key, chunk_t iv)
{
	if (!kdf(this, password, keymat))
	{
		return FALSE;
	}
	if (!this->crypter->set_key(this->crypter, key) ||
		!this->crypter->decrypt(this->crypter, data, iv, decrypted))
	{
		memwipe(keymat.ptr, keymat.len);
		return FALSE;
	}
	memwipe(keymat.ptr, keymat.len);
	if (verify_padding(this->crypter, decrypted))
	{
		return TRUE;
	}
	chunk_free(decrypted);
	return FALSE;
}

/**
 * KDF as used by PKCS#12
 */
static bool pkcs12_kdf(private_pkcs5_t *this, chunk_t password, chunk_t keymat)
{
	chunk_t key, iv;

	key = chunk_create(keymat.ptr, this->keylen);
	iv = chunk_create(keymat.ptr + this->keylen, keymat.len - this->keylen);

	return pkcs12_derive_key(this->data.pbes1.hash, password, this->salt,
							 this->iterations, PKCS12_KEY_ENCRYPTION, key) &&
		   pkcs12_derive_key(this->data.pbes1.hash, password, this->salt,
							 this->iterations, PKCS12_KEY_IV, iv);
}

/**
 * Function F of PBKDF2
 */
static bool pbkdf2_f(chunk_t block, prf_t *prf, chunk_t seed,
					 uint64_t iterations)
{
	chunk_t u;
	uint64_t i;

	u = chunk_alloca(prf->get_block_size(prf));
	if (!prf->get_bytes(prf, seed, u.ptr))
	{
		return FALSE;
	}
	memcpy(block.ptr, u.ptr, block.len);

	for (i = 1; i < iterations; i++)
	{
		if (!prf->get_bytes(prf, u, u.ptr))
		{
			return FALSE;
		}
		memxor(block.ptr, u.ptr, block.len);
	}
	return TRUE;
}

/**
 * PBKDF2 key derivation function for PBES2, key must be allocated
 */
static bool pbkdf2(private_pkcs5_t *this, chunk_t password, chunk_t key)
{
	prf_t *prf;
	chunk_t keymat, block, seed;
	size_t blocks;
	uint32_t i = 0;

	prf = this->data.pbes2.prf;

	if (!prf->set_key(prf, password))
	{
		return FALSE;
	}

	block.len = prf->get_block_size(prf);
	blocks = (key.len - 1) / block.len + 1;
	keymat = chunk_alloca(blocks * block.len);

	seed = chunk_cata("cc", this->salt, chunk_from_thing(i));

	for (; i < blocks; i++)
	{
		htoun32(seed.ptr + this->salt.len, i + 1);
		block.ptr = keymat.ptr + (i * block.len);
		if (!pbkdf2_f(block, prf, seed, this->iterations))
		{
			return FALSE;
		}
	}
	memcpy(key.ptr, keymat.ptr, key.len);
	return TRUE;
}

/**
 * PBKDF1 key derivation function for PBES1, key must be allocated
 */
static bool pbkdf1(private_pkcs5_t *this, chunk_t password, chunk_t key)
{
	hasher_t *hasher;
	chunk_t hash;
	uint64_t i;

	hasher = this->data.pbes1.hasher;

	hash = chunk_alloca(hasher->get_hash_size(hasher));
	if (!hasher->get_hash(hasher, password, NULL) ||
		!hasher->get_hash(hasher, this->salt, hash.ptr))
	{
		return FALSE;
	}

	for (i = 1; i < this->iterations; i++)
	{
		if (!hasher->get_hash(hasher, hash, hash.ptr))
		{
			return FALSE;
		}
	}
	memcpy(key.ptr, hash.ptr, key.len);
	return TRUE;
}

static bool ensure_crypto_primitives(private_pkcs5_t *this, chunk_t data)
{
	if (!this->crypter)
	{
		this->crypter = lib->crypto->create_crypter(lib->crypto, this->encr,
													this->keylen);
		if (!this->crypter)
		{
			DBG1(DBG_ASN, "  %N encryption algorithm not available",
				 encryption_algorithm_names, this->encr);
			return FALSE;
		}
	}
	if (data.len % this->crypter->get_block_size(this->crypter))
	{
		DBG1(DBG_ASN, "  data size is not a multiple of block size");
		return FALSE;
	}
	switch (this->scheme)
	{
		case PKCS5_SCHEME_PBES1:
		{
			if (!this->data.pbes1.hasher)
			{
				hasher_t *hasher;

				hasher = lib->crypto->create_hasher(lib->crypto,
													this->data.pbes1.hash);
				if (!hasher)
				{
					DBG1(DBG_ASN, "  %N hash algorithm not available",
						 hash_algorithm_names, this->data.pbes1.hash);
					return  FALSE;
				}
				if (hasher->get_hash_size(hasher) < this->keylen)
				{
					hasher->destroy(hasher);
					return FALSE;
				}
				this->data.pbes1.hasher = hasher;
			}
			break;
		}
		case PKCS5_SCHEME_PBES2:
		{
			if (!this->data.pbes2.prf)
			{
				prf_t *prf;

				prf = lib->crypto->create_prf(lib->crypto,
											  this->data.pbes2.prf_alg);
				if (!prf)
				{
					DBG1(DBG_ASN, "  %N prf algorithm not available",
						 pseudo_random_function_names,
						 this->data.pbes2.prf_alg);
					return FALSE;
				}
				this->data.pbes2.prf = prf;
			}
			break;
		}
		case PKCS5_SCHEME_PKCS12:
			break;
	}
	return TRUE;
}

METHOD(pkcs5_t, decrypt, bool,
	private_pkcs5_t *this, chunk_t password, chunk_t data, chunk_t *decrypted)
{
	chunk_t keymat, key, iv;
	kdf_t kdf;

	if (!ensure_crypto_primitives(this, data) || !decrypted)
	{
		return FALSE;
	}
	kdf = pbkdf1;
	switch (this->scheme)
	{
		case PKCS5_SCHEME_PKCS12:
			kdf = pkcs12_kdf;
			/* fall-through */
		case PKCS5_SCHEME_PBES1:
			keymat = chunk_alloca(this->keylen +
								  this->crypter->get_iv_size(this->crypter));
			key = chunk_create(keymat.ptr, this->keylen);
			iv = chunk_create(keymat.ptr + this->keylen,
							  keymat.len - this->keylen);
			break;
		case PKCS5_SCHEME_PBES2:
			kdf = pbkdf2;
			keymat = chunk_alloca(this->keylen);
			key = keymat;
			iv = this->data.pbes2.iv;
			break;
		default:
			return FALSE;
	}
	return decrypt_generic(this, password, data, decrypted, kdf,
						   keymat, key, iv);
}

/**
 * ASN.1 definition of a PBEParameter structure
 */
static const asn1Object_t pbeParameterObjects[] = {
	{ 0, "PBEParameter",		ASN1_SEQUENCE,		ASN1_NONE	}, /* 0 */
	{ 1,   "salt",				ASN1_OCTET_STRING,	ASN1_BODY	}, /* 1 */
	{ 1,   "iterationCount",	ASN1_INTEGER,		ASN1_BODY	}, /* 2 */
	{ 0, "exit",				ASN1_EOC,			ASN1_EXIT	}
};
#define PBEPARAM_SALT					1
#define PBEPARAM_ITERATION_COUNT		2

/**
 * Parse a PBEParameter structure
 */
static bool parse_pbes1_params(private_pkcs5_t *this, chunk_t blob, int level0)
{
	asn1_parser_t *parser;
	chunk_t object;
	int objectID;
	bool success;

	parser = asn1_parser_create(pbeParameterObjects, blob);
	parser->set_top_level(parser, level0);

	while (parser->iterate(parser, &objectID, &object))
	{
		switch (objectID)
		{
			case PBEPARAM_SALT:
			{
				this->salt = chunk_clone(object);
				break;
			}
			case PBEPARAM_ITERATION_COUNT:
			{
				this->iterations = asn1_parse_integer_uint64(object);
				break;
			}
		}
	}
	success = parser->success(parser);
	parser->destroy(parser);
	return success;
}

/**
 * ASN.1 definition of a PBKDF2-params structure
 * The salt is actually a CHOICE and could be an AlgorithmIdentifier from
 * PBKDF2-SaltSources (but as per RFC 2898 that's for future versions).
 */
static const asn1Object_t pbkdf2ParamsObjects[] = {
	{ 0, "PBKDF2-params",	ASN1_SEQUENCE,		ASN1_NONE			}, /* 0 */
	{ 1,   "salt",			ASN1_OCTET_STRING,	ASN1_BODY			}, /* 1 */
	{ 1,   "iterationCount",ASN1_INTEGER,		ASN1_BODY			}, /* 2 */
	{ 1,   "keyLength",		ASN1_INTEGER,		ASN1_OPT|ASN1_BODY	}, /* 3 */
	{ 1,   "end opt",		ASN1_EOC,			ASN1_END			}, /* 4 */
	{ 1,   "prf",			ASN1_EOC,			ASN1_DEF|ASN1_RAW	}, /* 5 */
	{ 0, "exit",			ASN1_EOC,			ASN1_EXIT			}
};
#define PBKDF2_SALT					1
#define PBKDF2_ITERATION_COUNT		2
#define PBKDF2_KEYLENGTH			3
#define PBKDF2_PRF					5

/**
 * Parse a PBKDF2-params structure
 */
static bool parse_pbkdf2_params(private_pkcs5_t *this, chunk_t blob, int level0)
{
	asn1_parser_t *parser;
	chunk_t object;
	int objectID;
	bool success;

	parser = asn1_parser_create(pbkdf2ParamsObjects, blob);
	parser->set_top_level(parser, level0);

 	/* keylen is optional */
	this->keylen = 0;

	while (parser->iterate(parser, &objectID, &object))
	{
		switch (objectID)
		{
			case PBKDF2_SALT:
			{
				this->salt = chunk_clone(object);
				break;
			}
			case PBKDF2_ITERATION_COUNT:
			{
				this->iterations = asn1_parse_integer_uint64(object);
				break;
			}
			case PBKDF2_KEYLENGTH:
			{
				this->keylen = (size_t)asn1_parse_integer_uint64(object);
				break;
			}
			case PBKDF2_PRF:
			{	/* defaults to id-hmacWithSHA1, no other is currently defined */
				this->data.pbes2.prf_alg = PRF_HMAC_SHA1;
				break;
			}
		}
	}
	success = parser->success(parser);
	parser->destroy(parser);
	return success;
}

/**
 * ASN.1 definition of a PBES2-params structure
 */
static const asn1Object_t pbes2ParamsObjects[] = {
	{ 0, "PBES2-params",		ASN1_SEQUENCE,		ASN1_NONE	}, /* 0 */
	{ 1,   "keyDerivationFunc",	ASN1_EOC,			ASN1_RAW	}, /* 1 */
	{ 1,   "encryptionScheme",	ASN1_EOC,			ASN1_RAW	}, /* 2 */
	{ 0, "exit",				ASN1_EOC,			ASN1_EXIT	}
};
#define PBES2PARAMS_KEY_DERIVATION_FUNC		1
#define PBES2PARAMS_ENCRYPTION_SCHEME		2

/**
 * Parse a PBES2-params structure
 */
static bool parse_pbes2_params(private_pkcs5_t *this, chunk_t blob, int level0)
{
	asn1_parser_t *parser;
	chunk_t object, params;
	size_t keylen;
	int objectID;
	bool success = FALSE;

	parser = asn1_parser_create(pbes2ParamsObjects, blob);
	parser->set_top_level(parser, level0);

	while (parser->iterate(parser, &objectID, &object))
	{
		switch (objectID)
		{
			case PBES2PARAMS_KEY_DERIVATION_FUNC:
			{
				int oid = asn1_parse_algorithmIdentifier(object,
									parser->get_level(parser) + 1, &params);
				if (oid != OID_PBKDF2)
				{	/* unsupported key derivation function */
					goto end;
				}
				if (!parse_pbkdf2_params(this, params,
										 parser->get_level(parser) + 1))
				{
					goto end;
				}
				break;
			}
			case PBES2PARAMS_ENCRYPTION_SCHEME:
			{
				int oid = asn1_parse_algorithmIdentifier(object,
									parser->get_level(parser) + 1, &params);
				this->encr = encryption_algorithm_from_oid(oid, &keylen);
				if (this->encr == ENCR_UNDEFINED)
				{	/* unsupported encryption scheme */
					goto end;
				}
				/* prefer encoded key length */
				this->keylen = this->keylen ?: keylen / 8;
				if (!this->keylen)
				{	/* set default key length for known algorithms */
					switch (this->encr)
					{
						case ENCR_DES:
							this->keylen = 8;
							break;
						case ENCR_3DES:
							this->keylen = 24;
							break;
						case ENCR_BLOWFISH:
							this->keylen = 16;
							break;
						default:
							goto end;
					}
				}
				if (!asn1_parse_simple_object(&params, ASN1_OCTET_STRING,
									parser->get_level(parser) + 1, "IV"))
				{
					goto end;
				}
				this->data.pbes2.iv = chunk_clone(params);
				break;
			}
		}
	}
	success = parser->success(parser);
end:
	parser->destroy(parser);
	return success;
}

METHOD(pkcs5_t, destroy, void,
	private_pkcs5_t *this)
{
	DESTROY_IF(this->crypter);
	chunk_free(&this->salt);
	switch (this->scheme)
	{
		case PKCS5_SCHEME_PBES1:
			DESTROY_IF(this->data.pbes1.hasher);
			break;
		case PKCS5_SCHEME_PBES2:
			DESTROY_IF(this->data.pbes2.prf);
			chunk_free(&this->data.pbes2.iv);
			break;
		case PKCS5_SCHEME_PKCS12:
			break;
	}
	free(this);
}

/*
 * Described in header
 */
pkcs5_t *pkcs5_from_algorithmIdentifier(chunk_t blob, int level0)
{
	private_pkcs5_t *this;
	chunk_t params;
	int oid;

	INIT(this,
		.public = {
			.decrypt = _decrypt,
			.destroy = _destroy,
		},
		.scheme = PKCS5_SCHEME_PBES1,
		.keylen = 8,
	);

	oid = asn1_parse_algorithmIdentifier(blob, level0, &params);

	switch (oid)
	{
		case OID_PBE_MD5_DES_CBC:
			this->encr = ENCR_DES;
			this->data.pbes1.hash = HASH_MD5;
			break;
		case OID_PBE_SHA1_DES_CBC:
			this->encr = ENCR_DES;
			this->data.pbes1.hash = HASH_SHA1;
			break;
		case OID_PBE_SHA1_3DES_CBC:
			this->scheme = PKCS5_SCHEME_PKCS12;
			this->keylen = 24;
			this->encr = ENCR_3DES;
			this->data.pbes1.hash = HASH_SHA1;
			break;
		case OID_PBE_SHA1_RC2_CBC_40:
		case OID_PBE_SHA1_RC2_CBC_128:
			this->scheme = PKCS5_SCHEME_PKCS12;
			this->keylen = (oid == OID_PBE_SHA1_RC2_CBC_40) ? 5 : 16;
			this->encr = ENCR_RC2_CBC;
			this->data.pbes1.hash = HASH_SHA1;
			break;
		case OID_PBES2:
			this->scheme = PKCS5_SCHEME_PBES2;
			break;
		default:
			/* encryption scheme not supported */
			goto failure;
	}

	switch (this->scheme)
	{
		case PKCS5_SCHEME_PBES1:
		case PKCS5_SCHEME_PKCS12:
			if (!parse_pbes1_params(this, params, level0))
			{
				goto failure;
			}
			break;
		case PKCS5_SCHEME_PBES2:
			if (!parse_pbes2_params(this, params, level0))
			{
				goto failure;
			}
			break;
	}
	return &this->public;

failure:
	destroy(this);
	return NULL;
}