aboutsummaryrefslogtreecommitdiffstats
path: root/src/libcharon/plugins/eap_tls/tls/tls_crypto.c
blob: f8894629fb7196941d200ca8b9f5f30a103b79a9 (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
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
/*
 * Copyright (C) 2010 Martin Willi
 * Copyright (C) 2010 revosec AG
 *
 * 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 "tls_crypto.h"

#include <daemon.h>

typedef struct private_tls_crypto_t private_tls_crypto_t;

/**
 * Private data of an tls_crypto_t object.
 */
struct private_tls_crypto_t {

	/**
	 * Public tls_crypto_t interface.
	 */
	tls_crypto_t public;

	/**
	 * Protection layer
	 */
	tls_protection_t *protection;

	/**
	 * List of supported/acceptable cipher suites
	 */
	tls_cipher_suite_t *suites;

	/**
	 * Number of supported suites
	 */
	int suite_count;

	/**
	 * Selected cipher suite
	 */
	tls_cipher_suite_t suite;

	/**
	 * TLS context
	 */
	tls_t *tls;

	/**
	 * All handshake data concatentated
	 */
	chunk_t handshake;

	/**
	 * Connection state TLS PRF
	 */
	tls_prf_t *prf;

	/**
	 * Signer instance for inbound traffic
	 */
	signer_t *signer_in;

	/**
	 * Signer instance for outbound traffic
	 */
	signer_t *signer_out;

	/**
	 * Crypter instance for inbound traffic
	 */
	crypter_t *crypter_in;

	/**
	 * Crypter instance for outbound traffic
	 */
	crypter_t *crypter_out;

	/**
	 * IV for input decryption, if < TLSv1.2
	 */
	chunk_t iv_in;

	/**
	 * IV for output decryption, if < TLSv1.2
	 */
	chunk_t iv_out;

	/**
	 * EAP-TLS MSK
	 */
	chunk_t msk;
};

typedef struct {
	tls_cipher_suite_t suite;
	hash_algorithm_t hash;
	pseudo_random_function_t prf;
	integrity_algorithm_t mac;
	encryption_algorithm_t encr;
	size_t encr_size;
} suite_algs_t;

/**
 * Mapping suites to a set of algorithms
 */
static suite_algs_t suite_algs[] = {
	{ TLS_RSA_WITH_NULL_MD5,
		HASH_MD5,
		PRF_HMAC_MD5,
		AUTH_HMAC_MD5_128,
		ENCR_NULL, 0
	},
	{ TLS_RSA_WITH_NULL_SHA,
		HASH_SHA1,
		PRF_HMAC_SHA1,
		AUTH_HMAC_SHA1_160,
		ENCR_NULL, 0
	},
	{ TLS_RSA_WITH_NULL_SHA256,
		HASH_SHA256,
		PRF_HMAC_SHA2_256,
		AUTH_HMAC_SHA2_256_256,
		ENCR_NULL, 0
	},
	{ TLS_RSA_WITH_AES_128_CBC_SHA,
		HASH_SHA1,
		PRF_HMAC_SHA1,
		AUTH_HMAC_SHA1_160,
		ENCR_AES_CBC, 16
	},
	{ TLS_RSA_WITH_AES_256_CBC_SHA,
		HASH_SHA1,
		PRF_HMAC_SHA1,
		AUTH_HMAC_SHA1_160,
		ENCR_AES_CBC, 32
	},
	{ TLS_RSA_WITH_3DES_EDE_CBC_SHA,
		HASH_SHA1,
		PRF_HMAC_SHA1,
		AUTH_HMAC_SHA1_160,
		ENCR_3DES, 0
	},
	{ TLS_RSA_WITH_AES_128_CBC_SHA256,
		HASH_SHA256,
		PRF_HMAC_SHA2_256,
		AUTH_HMAC_SHA2_256_256,
		ENCR_AES_CBC, 16
	},
};

/**
 * Look up algoritms by a suite
 */
static suite_algs_t *find_suite(tls_cipher_suite_t suite)
{
	int i;

	for (i = 0; i < countof(suite_algs); i++)
	{
		if (suite_algs[i].suite == suite)
		{
			return &suite_algs[i];
		}
	}
	return NULL;
}

/**
 * Initialize the cipher suite list
 */
static void build_cipher_suite_list(private_tls_crypto_t *this)
{
	encryption_algorithm_t encr;
	integrity_algorithm_t mac;
	enumerator_t *encrs, *macs;
	tls_cipher_suite_t supported[64], unique[64];
	int count = 0, i, j;

	/* we assume that we support RSA, but no DHE yet */
	macs = lib->crypto->create_signer_enumerator(lib->crypto);
	while (macs->enumerate(macs, &mac))
	{
		switch (mac)
		{
			case AUTH_HMAC_SHA1_160:
				supported[count++] = TLS_RSA_WITH_NULL_SHA;
				break;
			case AUTH_HMAC_SHA2_256_256:
				supported[count++] = TLS_RSA_WITH_NULL_SHA256;
				break;
			case AUTH_HMAC_MD5_128:
				supported[count++] = TLS_RSA_WITH_NULL_MD5;
				break;
			default:
				break;
		}
		encrs = lib->crypto->create_crypter_enumerator(lib->crypto);
		while (encrs->enumerate(encrs, &encr))
		{
			switch (encr)
			{
				case ENCR_AES_CBC:
					switch (mac)
					{
						case AUTH_HMAC_SHA1_160:
							supported[count++] = TLS_RSA_WITH_AES_128_CBC_SHA;
							supported[count++] = TLS_RSA_WITH_AES_256_CBC_SHA;
							break;
						case AUTH_HMAC_SHA2_256_256:
							supported[count++] = TLS_RSA_WITH_AES_128_CBC_SHA256;
							supported[count++] = TLS_RSA_WITH_AES_128_CBC_SHA256;
							break;
						default:
							break;
					}
					break;
				case ENCR_3DES:
					switch (mac)
					{
						case AUTH_HMAC_SHA1_160:
							supported[count++] = TLS_RSA_WITH_3DES_EDE_CBC_SHA;
							break;
						default:
							break;
					}
					break;
				default:
					break;
			}
		}
		encrs->destroy(encrs);
	}
	macs->destroy(macs);

	/* remove duplicates */
	this->suite_count = 0;
	for (i = 0; i < count; i++)
	{
		bool match = FALSE;

		for (j = 0; j < this->suite_count; j++)
		{
			if (supported[i] == unique[j])
			{
				match = TRUE;
				break;
			}
		}
		if (!match)
		{
			unique[this->suite_count++] = supported[i];
		}
	}
	free(this->suites);
	this->suites = malloc(sizeof(tls_cipher_suite_t) * this->suite_count);
	memcpy(this->suites, unique, sizeof(tls_cipher_suite_t) * this->suite_count);
}

METHOD(tls_crypto_t, get_cipher_suites, int,
	private_tls_crypto_t *this, tls_cipher_suite_t **suites)
{
	*suites = this->suites;
	return this->suite_count;
}

/**
 * Create crypto primitives
 */
static bool create_ciphers(private_tls_crypto_t *this, tls_cipher_suite_t suite)
{
	suite_algs_t *algs;

	algs = find_suite(suite);
	if (!algs)
	{
		DBG1(DBG_IKE, "selected TLS suite not supported");
		return FALSE;
	}

	DESTROY_IF(this->prf);
	if (this->tls->get_version(this->tls) < TLS_1_2)
	{
		this->prf = tls_prf_create_10();
	}
	else
	{
		this->prf = tls_prf_create_12(algs->prf);
	}
	if (!this->prf)
	{
		DBG1(DBG_IKE, "selected TLS PRF not supported");
		return FALSE;
	}

	DESTROY_IF(this->signer_in);
	DESTROY_IF(this->signer_out);
	this->signer_in = lib->crypto->create_signer(lib->crypto, algs->mac);
	this->signer_out = lib->crypto->create_signer(lib->crypto, algs->mac);
	if (!this->signer_in || !this->signer_out)
	{
		DBG1(DBG_IKE, "selected TLS MAC %N not supported",
			 integrity_algorithm_names, algs->mac);
		return FALSE;
	}

	DESTROY_IF(this->crypter_in);
	DESTROY_IF(this->crypter_out);
	if (algs->encr == ENCR_NULL)
	{
		this->crypter_in = this->crypter_out = NULL;
	}
	else
	{
		this->crypter_in = lib->crypto->create_crypter(lib->crypto,
												algs->encr, algs->encr_size);
		this->crypter_out = lib->crypto->create_crypter(lib->crypto,
												algs->encr, algs->encr_size);
		if (!this->crypter_in || !this->crypter_out)
		{
			DBG1(DBG_IKE, "selected TLS crypter %N not supported",
				 encryption_algorithm_names, algs->encr);
			return FALSE;
		}
	}
	return TRUE;
}

METHOD(tls_crypto_t, select_cipher_suite, tls_cipher_suite_t,
	private_tls_crypto_t *this, tls_cipher_suite_t *suites, int count)
{
	int i, j;

	for (i = 0; i < this->suite_count; i++)
	{
		for (j = 0; j < count; j++)
		{
			if (this->suites[i] == suites[j])
			{
				if (create_ciphers(this, this->suites[i]))
				{
					this->suite = this->suites[i];
					return this->suite;
				}
			}
		}
	}
	return 0;
}

METHOD(tls_crypto_t, set_protection, void,
	private_tls_crypto_t *this, tls_protection_t *protection)
{
	this->protection = protection;
}

METHOD(tls_crypto_t, append_handshake, void,
	private_tls_crypto_t *this, tls_handshake_type_t type, chunk_t data)
{
	u_int32_t header;

	/* reconstruct handshake header */
	header = htonl(data.len | (type << 24));
	this->handshake = chunk_cat("mcc", this->handshake,
								chunk_from_thing(header), data);
}

/**
 * Create a hash of the stored handshake data
 */
static bool hash_handshake(private_tls_crypto_t *this, chunk_t *hash)
{
	if (this->tls->get_version(this->tls) >= TLS_1_2)
	{
		hasher_t *hasher;
		suite_algs_t *alg;

		alg = find_suite(this->suite);
		if (!alg)
		{
			return FALSE;
		}
		hasher = lib->crypto->create_hasher(lib->crypto, alg->hash);
		if (!hasher)
		{
			DBG1(DBG_IKE, "%N not supported", hash_algorithm_names, alg->hash);
			return FALSE;
		}
		hasher->allocate_hash(hasher, this->handshake, hash);
		hasher->destroy(hasher);
	}
	else
	{
		hasher_t *md5, *sha1;
		char buf[HASH_SIZE_MD5 + HASH_SIZE_SHA1];

		md5 = lib->crypto->create_hasher(lib->crypto, HASH_MD5);
		if (!md5)
		{
			DBG1(DBG_IKE, "%N not supported", hash_algorithm_names, HASH_MD5);
			return FALSE;
		}
		md5->get_hash(md5, this->handshake, buf);
		md5->destroy(md5);
		sha1 = lib->crypto->create_hasher(lib->crypto, HASH_SHA1);
		if (!sha1)
		{
			DBG1(DBG_IKE, "%N not supported", hash_algorithm_names, HASH_SHA1);
			return FALSE;
		}
		sha1->get_hash(sha1, this->handshake, buf + HASH_SIZE_MD5);
		sha1->destroy(sha1);

		*hash = chunk_clone(chunk_from_thing(buf));
	}
	return TRUE;
}

METHOD(tls_crypto_t, sign_handshake, bool,
	private_tls_crypto_t *this, private_key_t *key, tls_writer_t *writer)
{
	chunk_t sig, hash;

	if (this->tls->get_version(this->tls) >= TLS_1_2)
	{
		/* TODO: use supported algorithms instead of fixed SHA1/RSA */
		if (!key->sign(key, SIGN_RSA_EMSA_PKCS1_SHA1, this->handshake, &sig))
		{
			return FALSE;
		}
		writer->write_uint8(writer, 2);
		writer->write_uint8(writer, 1);
		writer->write_data16(writer, sig);
		free(sig.ptr);
	}
	else
	{
		if (!hash_handshake(this, &hash))
		{
			return FALSE;
		}
		if (!key->sign(key, SIGN_RSA_EMSA_PKCS1_NULL, hash, &sig))
		{
			free(hash.ptr);
			return FALSE;
		}
		writer->write_data16(writer, sig);
		free(hash.ptr);
		free(sig.ptr);
	}
	return TRUE;
}

METHOD(tls_crypto_t, verify_handshake, bool,
	private_tls_crypto_t *this, public_key_t *key, tls_reader_t *reader)
{
	if (this->tls->get_version(this->tls) >= TLS_1_2)
	{
		u_int8_t hash, alg;
		chunk_t sig;

		if (!reader->read_uint8(reader, &hash) ||
			!reader->read_uint8(reader, &alg) ||
			!reader->read_data16(reader, &sig))
		{
			DBG1(DBG_IKE, "received invalid Certificate Verify");
			return FALSE;
		}
		/* TODO: map received hash/sig alg to signature scheme */
		if (hash != 2 || alg != 1 ||
			!key->verify(key, SIGN_RSA_EMSA_PKCS1_SHA1, this->handshake, sig))
		{
			return FALSE;
		}
	}
	else
	{
		chunk_t sig, hash;

		if (!reader->read_data16(reader, &sig))
		{
			DBG1(DBG_IKE, "received invalid Certificate Verify");
			return FALSE;
		}
		if (!hash_handshake(this, &hash))
		{
			return FALSE;
		}
		if (!key->verify(key, SIGN_RSA_EMSA_PKCS1_NULL, hash, sig))
		{
			free(hash.ptr);
			return FALSE;
		}
		free(hash.ptr);
	}
	return TRUE;
}

METHOD(tls_crypto_t, calculate_finished, bool,
	private_tls_crypto_t *this, char *label, char out[12])
{
	chunk_t seed;

	if (!this->prf)
	{
		return FALSE;
	}
	if (!hash_handshake(this, &seed))
	{
		return FALSE;
	}
	this->prf->get_bytes(this->prf, label, seed, 12, out);
	free(seed.ptr);
	return TRUE;
}

METHOD(tls_crypto_t, derive_secrets, void,
	private_tls_crypto_t *this, chunk_t premaster,
	chunk_t client_random, chunk_t server_random)
{
	char master[48];
	chunk_t seed, block, client_write, server_write;
	int mks, eks = 0, ivs = 0;

	/* derive master secret */
	seed = chunk_cata("cc", client_random, server_random);
	this->prf->set_key(this->prf, premaster);
	this->prf->get_bytes(this->prf, "master secret", seed,
						 sizeof(master), master);

	this->prf->set_key(this->prf, chunk_from_thing(master));
	memset(master, 0, sizeof(master));

	/* derive key block for key expansion */
	mks = this->signer_out->get_key_size(this->signer_out);
	if (this->crypter_out)
	{
		eks = this->crypter_out->get_key_size(this->crypter_out);
		if (this->tls->get_version(this->tls) < TLS_1_1)
		{
			ivs = this->crypter_out->get_block_size(this->crypter_out);
		}
	}
	seed = chunk_cata("cc", server_random, client_random);
	block = chunk_alloca((mks + eks + ivs) * 2);
	this->prf->get_bytes(this->prf, "key expansion", seed, block.len, block.ptr);

	/* signer keys */
	client_write = chunk_create(block.ptr, mks);
	block = chunk_skip(block, mks);
	server_write = chunk_create(block.ptr, mks);
	block = chunk_skip(block, mks);
	if (this->tls->is_server(this->tls))
	{
		this->signer_in->set_key(this->signer_in, client_write);
		this->signer_out->set_key(this->signer_out, server_write);
	}
	else
	{
		this->signer_out->set_key(this->signer_out, client_write);
		this->signer_in->set_key(this->signer_in, server_write);
	}

	/* crypter keys, and IVs if < TLSv1.2 */
	if (this->crypter_out && this->crypter_in)
	{
		client_write = chunk_create(block.ptr, eks);
		block = chunk_skip(block, eks);
		server_write = chunk_create(block.ptr, eks);
		block = chunk_skip(block, eks);

		if (this->tls->is_server(this->tls))
		{
			this->crypter_in->set_key(this->crypter_in, client_write);
			this->crypter_out->set_key(this->crypter_out, server_write);
		}
		else
		{
			this->crypter_out->set_key(this->crypter_out, client_write);
			this->crypter_in->set_key(this->crypter_in, server_write);
		}
		if (ivs)
		{
			client_write = chunk_create(block.ptr, ivs);
			block = chunk_skip(block, ivs);
			server_write = chunk_create(block.ptr, ivs);
			block = chunk_skip(block, ivs);

			if (this->tls->is_server(this->tls))
			{
				this->iv_in = chunk_clone(client_write);
				this->iv_out = chunk_clone(server_write);
			}
			else
			{
				this->iv_out = chunk_clone(client_write);
				this->iv_in = chunk_clone(server_write);
			}
		}
	}
}

METHOD(tls_crypto_t, change_cipher, void,
	private_tls_crypto_t *this, bool inbound)
{
	if (this->protection)
	{
		if (inbound)
		{
			this->protection->set_cipher(this->protection, TRUE,
							this->signer_in, this->crypter_in, this->iv_in);
		}
		else
		{
			this->protection->set_cipher(this->protection, FALSE,
							this->signer_out, this->crypter_out, this->iv_out);
		}
	}
}

METHOD(tls_crypto_t, derive_eap_msk, void,
	private_tls_crypto_t *this, chunk_t client_random, chunk_t server_random)
{
	chunk_t seed;

	seed = chunk_cata("cc", client_random, server_random);
	free(this->msk.ptr);
	this->msk = chunk_alloc(64);
	this->prf->get_bytes(this->prf, "client EAP encryption", seed,
						 this->msk.len, this->msk.ptr);
}

METHOD(tls_crypto_t, get_eap_msk, chunk_t,
	private_tls_crypto_t *this)
{
	return this->msk;
}

METHOD(tls_crypto_t, destroy, void,
	private_tls_crypto_t *this)
{
	DESTROY_IF(this->signer_in);
	DESTROY_IF(this->signer_out);
	DESTROY_IF(this->crypter_in);
	DESTROY_IF(this->crypter_out);
	free(this->iv_in.ptr);
	free(this->iv_out.ptr);
	free(this->handshake.ptr);
	free(this->msk.ptr);
	DESTROY_IF(this->prf);
	free(this->suites);
	free(this);
}

/**
 * See header
 */
tls_crypto_t *tls_crypto_create(tls_t *tls)
{
	private_tls_crypto_t *this;

	INIT(this,
		.public = {
			.get_cipher_suites = _get_cipher_suites,
			.select_cipher_suite = _select_cipher_suite,
			.set_protection = _set_protection,
			.append_handshake = _append_handshake,
			.sign_handshake = _sign_handshake,
			.verify_handshake = _verify_handshake,
			.calculate_finished = _calculate_finished,
			.derive_secrets = _derive_secrets,
			.change_cipher = _change_cipher,
			.derive_eap_msk = _derive_eap_msk,
			.get_eap_msk = _get_eap_msk,
			.destroy = _destroy,
		},
		.tls = tls,
	);

	build_cipher_suite_list(this);

	return &this->public;
}