aboutsummaryrefslogtreecommitdiffstats
path: root/Source/charon/encoding/payloads/encryption_payload.c
blob: db4b441e506d25a385ce9b6d9bf1e43f7e02df47 (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
/**
 * @file encryption_payload.c
 * 
 * @brief Implementation of encryption_payload_t.
 * 
 */

/*
 * Copyright (C) 2005 Jan Hutter, Martin Willi
 * 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.
 */
 
/* offsetof macro */
#include <stddef.h>

#include "encryption_payload.h"

#include <encoding/payloads/encodings.h>
#include <utils/allocator.h>
#include <utils/linked_list.h>
#include <encoding/generator.h>
#include <encoding/parser.h>
#include <utils/iterator.h>
#include <utils/randomizer.h>
#include <transforms/signers/signer.h>




typedef struct private_encryption_payload_t private_encryption_payload_t;

/**
 * Private data of an encryption_payload_t' Object.
 * 
 */
struct private_encryption_payload_t {
	/**
	 * Public encryption_payload_t interface.
	 */
	encryption_payload_t public;
	
	/**
	 * There is no next payload for an encryption payload, 
	 * since encryption payload MUST be the last one.
	 * next_payload means here the first payload of the 
	 * contained, encrypted payload.
	 */
	u_int8_t next_payload;

	/**
	 * Critical flag.
	 */
	bool critical;
	
	/**
	 * Length of this payload
	 */
	u_int16_t payload_length;
	
	/**
	 * Initialization vector.
	 */
	chunk_t iv;
	
	/**
	 * Integrity checksum.
	 */
	chunk_t checksum;
	
	/**
	 * Chunk containing the iv, data, padding,
	 * and (an eventually not calculated) signature.
	 */
	chunk_t encrypted;
	
	/**
	 * Chunk containing the data in decrypted (unpadded) form.
	 */
	chunk_t decrypted;
	
	/**
	 * Signer set by set_signer.
	 */
	signer_t *signer;
	
	/**
	 * Contained payloads of this encrpytion_payload.
	 */
	linked_list_t *payloads;
	
	/**
	 * @brief Computes the length of this payload.
	 *
	 * @param this 	calling private_encryption_payload_t object
	 */
	void (*compute_length) (private_encryption_payload_t *this);
	
	/**
	 * @brief Generate payloads (unencrypted) in chunk decrypted.
	 * 
	 * @param this 	calling private_encryption_payload_t object
	 */
	void (*generate) (private_encryption_payload_t *this);
	status_t (*parse) (private_encryption_payload_t *this);
};

/**
 * Encoding rules to parse or generate a IKEv2-Encryption Payload.
 * 
 * The defined offsets are the positions in a object of type 
 * private_encryption_payload_t.
 * 
 */
encoding_rule_t encryption_payload_encodings[] = {
	/* 1 Byte next payload type, stored in the field next_payload */
	{ U_INT_8,			offsetof(private_encryption_payload_t, next_payload) 	},
	/* the critical bit */
	{ FLAG,				offsetof(private_encryption_payload_t, critical) 		},
	/* 7 Bit reserved bits, nowhere stored */
	{ RESERVED_BIT,		0 														},
	{ RESERVED_BIT,		0 														},
	{ RESERVED_BIT,		0 														},
	{ RESERVED_BIT,		0 														},
	{ RESERVED_BIT,		0 														},
	{ RESERVED_BIT,		0 														},
	{ RESERVED_BIT,		0 														},
	/* Length of the whole encryption payload*/
	{ PAYLOAD_LENGTH,	offsetof(private_encryption_payload_t, payload_length) 	},
	/* encrypted data, stored in a chunk. contains iv, data, padding */
	{ ENCRYPTED_DATA,	offsetof(private_encryption_payload_t, encrypted)			},
};

/*
                           1                   2                   3
       0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      ! Next Payload  !C!  RESERVED   !         Payload Length        !
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      !                     Initialization Vector                     !
      !         (length is block size for encryption algorithm)       !
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      !                    Encrypted IKE Payloads                     !
      +               +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      !               !             Padding (0-255 octets)            !
      +-+-+-+-+-+-+-+-+                               +-+-+-+-+-+-+-+-+
      !                                               !  Pad Length   !
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      ~                    Integrity Checksum Data                    ~
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/

/**
 * Implementation of payload_t.verify.
 */
static status_t verify(private_encryption_payload_t *this)
{
// 	int proposal_number = 1;
	status_t status;
// 	iterator_t *iterator;
// 	bool first = TRUE;
// 	
// 	if (this->critical)
// 	{
// 		/* critical bit set! */
// 		return FAILED;
// 	}
// 
// 	/* check proposal numbering */		
// 	status = this->proposals->create_iterator(this->proposals,&iterator,TRUE);
// 	if (status != SUCCESS)
// 	{
// 		return status;
// 	}
// 	
// 	while(iterator->has_next(iterator))
// 	{
// 		proposal_substructure_t *current_proposal;
// 		status = iterator->current(iterator,(void **)&current_proposal);
// 		{
// 			break;
// 		}
// 		if (current_proposal->get_proposal_number(current_proposal) > proposal_number)
// 		{
// 			if (first) 
// 			{
// 				/* first number must be 1 */
// 				status = FAILED;
// 				break;
// 			}
// 			
// 			if (current_proposal->get_proposal_number(current_proposal) != (proposal_number + 1))
// 			{
// 				/* must be only one more then previous proposal */
// 				status = FAILED;
// 				break;
// 			}
// 		}
// 		else if (current_proposal->get_proposal_number(current_proposal) < proposal_number)
// 		{
// 			iterator->destroy(iterator);
// 			/* must not be smaller then proceeding one */
// 			status = FAILED;
// 			break;
// 		}
// 		first = FALSE;
// 	}
// 	
// 	iterator->destroy(iterator);
	return status;
}

/**
 * Implementation of payload_t.destroy.
 */
static void destroy(private_encryption_payload_t *this)
{
	/* all proposals are getting destroyed */ 
	while (this->payloads->get_count(this->payloads) > 0)
	{
		payload_t *current_payload;
		this->payloads->remove_last(this->payloads,(void **)&current_payload);
		current_payload->destroy(current_payload);
	}
	this->payloads->destroy(this->payloads);
		
	allocator_free(this->iv.ptr);
	allocator_free(this->encrypted.ptr);
	allocator_free(this->decrypted.ptr);
	allocator_free(this->checksum.ptr);
	allocator_free(this);
}

/**
 * Implementation of payload_t.get_encoding_rules.
 */
static void get_encoding_rules(private_encryption_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
{
	*rules = encryption_payload_encodings;
	*rule_count = sizeof(encryption_payload_encodings) / sizeof(encoding_rule_t);
}

/**
 * Implementation of payload_t.get_type.
 */
static payload_type_t get_type(private_encryption_payload_t *this)
{
	return ENCRYPTED;
}

/**
 * Implementation of payload_t.get_next_type.
 */
static payload_type_t get_next_type(private_encryption_payload_t *this)
{
	/* returns first contained payload here */
	return (this->next_payload);
}

/**
 * Implementation of payload_t.set_next_type.
 */
static void set_next_type(private_encryption_payload_t *this, payload_type_t type)
{
	/* set next type is not allowed, since this payload MUST be the last one 
	 * and so nothing is done in here*/
}

/**
 * Implementation of payload_t.get_length.
 */
static size_t get_length(private_encryption_payload_t *this)
{
	this->compute_length(this);
	return this->payload_length;
}

/**
 * Implementation of payload_t.create_payload_iterator.
 */
static  iterator_t *create_payload_iterator (private_encryption_payload_t *this, bool forward)
{
	return (this->payloads->create_iterator(this->payloads, forward));
}

/**
 * Implementation of payload_t.add_payload.
 */
static void add_payload(private_encryption_payload_t *this, payload_t *payload)
{
	payload_t *last_payload;
	if (this->payloads->get_count(this->payloads) > 0)
	{
		this->payloads->get_last(this->payloads,(void **) &last_payload);
	}
	
	if (this->payloads->get_count(this->payloads) == 1)
	{
		this->next_payload = payload->get_type(payload);
	}
	else
	{
		last_payload->set_next_type(last_payload, payload->get_type(payload));
	}
	payload->set_next_type(payload, NO_PAYLOAD);
	this->compute_length(this);
}

/**
 * Implementation of encryption_payload_t.encrypt.
 */
static status_t encrypt(private_encryption_payload_t *this, crypter_t *crypter)
{
	chunk_t iv, padding, concatenated;
	randomizer_t *randomizer;
	status_t status;
	
	if (this->signer == NULL)
	{
		return INVALID_STATE;
	}
	
	/* for random data in iv and padding */
	randomizer = randomizer_create();

	/* build payload chunk */
	this->generate(this);
	
	/* build padding */
	padding.len = (this->decrypted.len + 1) % crypter->get_block_size(crypter);
	status = randomizer->allocate_pseudo_random_bytes(randomizer, padding.len, &padding);
	if (status != SUCCESS)
	{
		randomizer->destroy(randomizer);
		return status;
	}
	
	/* concatenate payload data, padding, padding len */
	concatenated.len = this->decrypted.len + padding.len + 1;
	concatenated.ptr = allocator_alloc(concatenated.len);

	memcpy(concatenated.ptr, this->decrypted.ptr, this->decrypted.len);
	memcpy(concatenated.ptr + this->decrypted.len, padding.ptr, padding.len);
	*(concatenated.ptr + concatenated.len - 1) = padding.len;

		
	/* build iv */
	iv.len = crypter->get_block_size(crypter);
	randomizer->allocate_pseudo_random_bytes(randomizer, iv.len, &iv);
	randomizer->destroy(randomizer);
		
	/* encrypt concatenated chunk */
	allocator_free(this->encrypted.ptr);
	status = crypter->encrypt(crypter, concatenated, iv, &(this->encrypted));
	allocator_free(padding.ptr);
	allocator_free(concatenated.ptr);
	allocator_free(iv.ptr);
	if (status != SUCCESS)
	{
		return status;
	}
	
	/* append an empty signature */
	this->encrypted.len += this->signer->get_block_size(this->signer);
	allocator_realloc(this->encrypted.ptr, this->encrypted.len);
	return SUCCESS;
}

/**
 * Implementation of encryption_payload_t.encrypt.
 */
static status_t decrypt(private_encryption_payload_t *this, crypter_t *crypter)
{
	chunk_t iv, concatenated;
	u_int8_t padding_length;
	status_t status;
	
	if (this->signer == NULL)
	{
		return INVALID_STATE;
	}
	
	/* get IV */
	iv.len = crypter->get_block_size(crypter);
	iv.ptr = this->encrypted.ptr;
	
	/* point concatenated to data + padding + padding_length*/
	concatenated.ptr = this->encrypted.ptr + iv.len;
	concatenated.len = this->encrypted.len - iv.len - this->signer->get_block_size(this->signer);
	
	/* check the size of input:
	 * concatenated  must be at least on block_size of crypter
	 */
	if (concatenated.len < iv.len)
	{
		return FAILED;
	}
	
	/* free previus data, if any */
	allocator_free(this->decrypted.ptr);
	
	status = crypter->decrypt(crypter, concatenated, iv, &(this->decrypted));
	if (status != SUCCESS)
	{
		return FAILED;
	}
	
	/* get padding length, sits just bevore signature */
	padding_length = *(this->decrypted.ptr + this->decrypted.len - 1);
	this->decrypted.len -= padding_length;
	
	/* check size again */
	if (padding_length > concatenated.len || this->decrypted.len < 0)
	{
		/* decryption failed :-/ */
		return FAILED;
	}
	
	/* free padding */
	this->decrypted.ptr = allocator_realloc(this->decrypted.ptr, this->decrypted.len);
	if (this->decrypted.ptr == NULL)
	{
		return OUT_OF_RES;
	}
	
	return SUCCESS;
}

/**
 * Implementation of encryption_payload_t.set_signer.
 */
static void set_signer(private_encryption_payload_t *this, signer_t* signer)
{
	this->signer = signer;
}

/**
 * Implementation of encryption_payload_t.build_signature.
 */
static status_t build_signature(private_encryption_payload_t *this, chunk_t data)
{
	chunk_t data_without_sig = data;
	chunk_t sig;
	
	if (this->signer == NULL)
	{
		return INVALID_STATE;
	}
	
	sig.len = this->signer->get_block_size(this->signer);
	data_without_sig.len -= sig.len;
	sig.ptr = data.ptr + data_without_sig.len;
	this->signer->get_signature(this->signer, data_without_sig, sig.ptr);
	return SUCCESS;
}

/**
 * Implementation of encryption_payload_t.verify_signature.
 */
static status_t verify_signature(private_encryption_payload_t *this, chunk_t data)
{
	chunk_t sig, data_without_sig;
	bool valid;
	
	if (this->signer == NULL)
	{
		return INVALID_STATE;
	}
	/* find signature in data chunk */
	sig.len = this->signer->get_block_size(this->signer);
	if (data.len <= sig.len)
	{
		return FAILED;
	}
	sig.ptr = data.ptr + data.len - sig.len;
	
	/* verify it */
	data_without_sig.len = data.len - sig.len;
	data_without_sig.ptr = data.ptr;
	this->signer->verify_signature(this->signer, data_without_sig, sig, &valid);
	
	if (!valid)
	{
		return FAILED;
	}
	
	return SUCCESS;
}

/**
 * Implementation of private_encryption_payload_t.generate.
 */
static void generate(private_encryption_payload_t *this)
{
	payload_t *current_payload, *next_payload;
	generator_t *generator;
	iterator_t *iterator;
	
	/* create iterator */
	iterator = this->payloads->create_iterator(this->payloads, TRUE);
	
	/* get first payload */
	if (iterator->has_next(iterator))
	{
		iterator->current(iterator, (void**)&current_payload);
		this->next_payload = current_payload->get_type(current_payload);
	}
	else
	{
		/* no paylads? */
		allocator_free(this->decrypted.ptr);
		this->decrypted = CHUNK_INITIALIZER;
		iterator->destroy(iterator);
		return;
	}
	
	generator = generator_create();
	
	/* build all payload, except last */
	while(iterator->has_next(iterator))
	{
		iterator->current(iterator, (void**)&next_payload);
		current_payload->set_next_type(current_payload, next_payload->get_type(next_payload));
		
		generator->generate_payload(generator, current_payload);
		current_payload = next_payload;
	}
	iterator->destroy(iterator);
	
	/* build last payload */
	current_payload->set_next_type(current_payload, NO_PAYLOAD);
	generator->generate_payload(generator, current_payload);
	
	/* free already generated data */
	allocator_free(this->decrypted.ptr);
	
	generator->write_to_chunk(generator, &(this->decrypted));
	generator->destroy(generator);
}

/**
 * Implementation of private_encryption_payload_t.parse.
 */
static status_t parse(private_encryption_payload_t *this)
{
	parser_t *parser;
	status_t status;
	payload_type_t current_payload_type;
	
	/* check if there is decrypted data */
	if (this->decrypted.ptr == NULL)
	{
		return INVALID_STATE;
	}
	
	/* build a parser on the decrypted data */
	parser = parser_create(this->decrypted);
	
	current_payload_type = this->next_payload;
	/* parse all payloads */
	while (current_payload_type != NO_PAYLOAD)
	{
		payload_t *current_payload;	
		
		status = parser->parse_payload(parser, current_payload_type, (payload_t**)&current_payload);
		if (status != SUCCESS)
		{
			return PARSE_ERROR;
		}
		
		status = current_payload->verify(current_payload);
		if (status != SUCCESS)
		{
			return VERIFY_ERROR;
		}

		/* get next payload type */
		current_payload_type = current_payload->get_next_type(current_payload);
		
		this->payloads->insert_last(this->payloads,current_payload);
	}
	return SUCCESS;
}

/**
 * Implementation of private_encryption_payload_t.compute_length.
 */
static void compute_length(private_encryption_payload_t *this)
{
	iterator_t *iterator;
	size_t length = ENCRYPTION_PAYLOAD_HEADER_LENGTH;
	iterator = this->payloads->create_iterator(this->payloads, TRUE);

	while (iterator->has_next(iterator))
	{
		payload_t *current_payload;
		iterator->current(iterator, (void **) &current_payload);
		length += current_payload->get_length(current_payload);
	}
	iterator->destroy(iterator);
	
	this->payload_length = length;

}

/*
 * Described in header
 */
encryption_payload_t *encryption_payload_create()
{
	private_encryption_payload_t *this = allocator_alloc_thing(private_encryption_payload_t);
	
	/* payload_t interface functions */
	this->public.payload_interface.verify = (status_t (*) (payload_t *))verify;
	this->public.payload_interface.get_encoding_rules = (void (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
	this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
	this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
	this->public.payload_interface.set_next_type = (void (*) (payload_t *,payload_type_t)) set_next_type;
	this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_type;
	this->public.payload_interface.destroy = (void (*) (payload_t *))destroy;
	
	/* public functions */
	this->public.create_payload_iterator = (iterator_t * (*) (encryption_payload_t *,bool)) create_payload_iterator;
	this->public.add_payload = (void (*) (encryption_payload_t *,payload_t *)) add_payload;
	this->public.encrypt = (status_t (*) (encryption_payload_t *, crypter_t*)) encrypt;
	this->public.decrypt = (status_t (*) (encryption_payload_t *, crypter_t*)) decrypt;
	this->public.set_signer = (void (*) (encryption_payload_t *,signer_t*)) set_signer;
	this->public.build_signature = (status_t (*) (encryption_payload_t*, chunk_t)) build_signature;
	this->public.verify_signature = (status_t (*) (encryption_payload_t*, chunk_t)) verify_signature;
	this->public.destroy = (void (*) (encryption_payload_t *)) destroy;
	
	/* private functions */
	this->compute_length = compute_length;
	this->generate = generate;
	this->parse = parse;
	
	/* set default values of the fields */
	this->critical = TRUE;
	this->next_payload = NO_PAYLOAD;
	this->payload_length = ENCRYPTION_PAYLOAD_HEADER_LENGTH;
	this->iv = CHUNK_INITIALIZER;
	this->encrypted = CHUNK_INITIALIZER;
	this->decrypted = CHUNK_INITIALIZER;
	this->checksum = CHUNK_INITIALIZER;
	this->signer = NULL;
	this->payloads = linked_list_create();
	
	return (&(this->public));
}