../svn-commit.tmp
[strongswan.git] / Source / charon / encoding / payloads / encryption_payload.c
1 /**
2 * @file encryption_payload.c
3 *
4 * @brief Implementation of encryption_payload_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 /* offsetof macro */
24 #include <stddef.h>
25
26 #include "encryption_payload.h"
27
28 #include <daemon.h>
29 #include <encoding/payloads/encodings.h>
30 #include <utils/allocator.h>
31 #include <utils/linked_list.h>
32 #include <utils/logger.h>
33 #include <encoding/generator.h>
34 #include <encoding/parser.h>
35 #include <utils/iterator.h>
36 #include <utils/randomizer.h>
37 #include <transforms/signers/signer.h>
38
39
40
41
42 typedef struct private_encryption_payload_t private_encryption_payload_t;
43
44 /**
45 * Private data of an encryption_payload_t' Object.
46 *
47 */
48 struct private_encryption_payload_t {
49
50 /**
51 * Public encryption_payload_t interface.
52 */
53 encryption_payload_t public;
54
55 /**
56 * There is no next payload for an encryption payload,
57 * since encryption payload MUST be the last one.
58 * next_payload means here the first payload of the
59 * contained, encrypted payload.
60 */
61 u_int8_t next_payload;
62
63 /**
64 * Critical flag.
65 */
66 bool critical;
67
68 /**
69 * Length of this payload
70 */
71 u_int16_t payload_length;
72
73 /**
74 * Chunk containing the iv, data, padding,
75 * and (an eventually not calculated) signature.
76 */
77 chunk_t encrypted;
78
79 /**
80 * Chunk containing the data in decrypted (unpadded) form.
81 */
82 chunk_t decrypted;
83
84 /**
85 * Signer set by set_signer.
86 */
87 signer_t *signer;
88
89 /**
90 * Crypter, supplied by encrypt/decrypt
91 */
92 crypter_t *crypter;
93
94 /**
95 * Contained payloads of this encrpytion_payload.
96 */
97 linked_list_t *payloads;
98
99 /**
100 * logger for this payload, uses MESSAGE context
101 */
102 logger_t *logger;
103
104 /**
105 * @brief Computes the length of this payload.
106 *
107 * @param this calling private_encryption_payload_t object
108 */
109 void (*compute_length) (private_encryption_payload_t *this);
110
111 /**
112 * @brief Generate payloads (unencrypted) in chunk decrypted.
113 *
114 * @param this calling private_encryption_payload_t object
115 */
116 void (*generate) (private_encryption_payload_t *this);
117
118 /**
119 * @brief Parse payloads from a (unencrypted) chunk.
120 *
121 * @param this calling private_encryption_payload_t object
122 */
123 status_t (*parse) (private_encryption_payload_t *this);
124 };
125
126 /**
127 * Encoding rules to parse or generate a IKEv2-Encryption Payload.
128 *
129 * The defined offsets are the positions in a object of type
130 * private_encryption_payload_t.
131 *
132 */
133 encoding_rule_t encryption_payload_encodings[] = {
134 /* 1 Byte next payload type, stored in the field next_payload */
135 { U_INT_8, offsetof(private_encryption_payload_t, next_payload) },
136 /* the critical bit */
137 { FLAG, offsetof(private_encryption_payload_t, critical) },
138 /* 7 Bit reserved bits, nowhere stored */
139 { RESERVED_BIT, 0 },
140 { RESERVED_BIT, 0 },
141 { RESERVED_BIT, 0 },
142 { RESERVED_BIT, 0 },
143 { RESERVED_BIT, 0 },
144 { RESERVED_BIT, 0 },
145 { RESERVED_BIT, 0 },
146 /* Length of the whole encryption payload*/
147 { PAYLOAD_LENGTH, offsetof(private_encryption_payload_t, payload_length) },
148 /* encrypted data, stored in a chunk. contains iv, data, padding */
149 { ENCRYPTED_DATA, offsetof(private_encryption_payload_t, encrypted) },
150 };
151
152 /*
153 1 2 3
154 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
155 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
156 ! Next Payload !C! RESERVED ! Payload Length !
157 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
158 ! Initialization Vector !
159 ! (length is block size for encryption algorithm) !
160 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
161 ! Encrypted IKE Payloads !
162 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
163 ! ! Padding (0-255 octets) !
164 +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+
165 ! ! Pad Length !
166 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
167 ~ Integrity Checksum Data ~
168 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
169 */
170
171 /**
172 * Implementation of payload_t.verify.
173 */
174 static status_t verify(private_encryption_payload_t *this)
175 {
176 return SUCCESS;
177 }
178
179 /**
180 * Implementation of payload_t.get_encoding_rules.
181 */
182 static void get_encoding_rules(private_encryption_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
183 {
184 *rules = encryption_payload_encodings;
185 *rule_count = sizeof(encryption_payload_encodings) / sizeof(encoding_rule_t);
186 }
187
188 /**
189 * Implementation of payload_t.get_type.
190 */
191 static payload_type_t get_type(private_encryption_payload_t *this)
192 {
193 return ENCRYPTED;
194 }
195
196 /**
197 * Implementation of payload_t.get_next_type.
198 */
199 static payload_type_t get_next_type(private_encryption_payload_t *this)
200 {
201 /* returns first contained payload here */
202 return (this->next_payload);
203 }
204
205 /**
206 * Implementation of payload_t.set_next_type.
207 */
208 static void set_next_type(private_encryption_payload_t *this, payload_type_t type)
209 {
210 /* set next type is not allowed, since this payload MUST be the last one
211 * and so nothing is done in here*/
212 }
213
214 /**
215 * Implementation of payload_t.get_length.
216 */
217 static size_t get_length(private_encryption_payload_t *this)
218 {
219 this->compute_length(this);
220 return this->payload_length;
221 }
222
223 /**
224 * Implementation of payload_t.create_payload_iterator.
225 */
226 static iterator_t *create_payload_iterator (private_encryption_payload_t *this, bool forward)
227 {
228 return (this->payloads->create_iterator(this->payloads, forward));
229 }
230
231 /**
232 * Implementation of payload_t.add_payload.
233 */
234 static void add_payload(private_encryption_payload_t *this, payload_t *payload)
235 {
236 payload_t *last_payload;
237 if (this->payloads->get_count(this->payloads) > 0)
238 {
239 this->payloads->get_last(this->payloads,(void **) &last_payload);
240 last_payload->set_next_type(last_payload, payload->get_type(payload));
241 }
242 else
243 {
244 this->next_payload = payload->get_type(payload);
245 }
246 payload->set_next_type(payload, NO_PAYLOAD);
247 this->payloads->insert_last(this->payloads, (void*)payload);
248 this->compute_length(this);
249 }
250
251 /**
252 * Implementation of encryption_payload_t.remove_first_payload.
253 */
254 static status_t remove_first_payload(private_encryption_payload_t *this, payload_t **payload)
255 {
256 return this->payloads->remove_first(this->payloads, (void**)payload);
257 }
258
259 /**
260 * Implementation of encryption_payload_t.get_payload_count.
261 */
262 static size_t get_payload_count(private_encryption_payload_t *this)
263 {
264 return this->payloads->get_count(this->payloads);
265 }
266
267
268 /**
269 * Implementation of encryption_payload_t.encrypt.
270 */
271 static status_t encrypt(private_encryption_payload_t *this)
272 {
273 chunk_t iv, padding, to_crypt, result;
274 randomizer_t *randomizer;
275 status_t status;
276 size_t block_size;
277
278 if (this->signer == NULL || this->crypter == NULL)
279 {
280 this->logger->log(this->logger, ERROR, "could not encrypt, signer/crypter not set");
281 return INVALID_STATE;
282 }
283
284 /* for random data in iv and padding */
285 randomizer = randomizer_create();
286
287
288 /* build payload chunk */
289 this->generate(this);
290
291 this->logger->log(this->logger, CONTROL|LEVEL2, "encrypting payloads");
292
293 /* build padding */
294 block_size = this->crypter->get_block_size(this->crypter);
295 padding.len = block_size - ((this->decrypted.len + 1) % block_size);
296 randomizer->allocate_pseudo_random_bytes(randomizer, padding.len, &padding);
297
298 /* concatenate payload data, padding, padding len */
299 to_crypt.len = this->decrypted.len + padding.len + 1;
300 to_crypt.ptr = allocator_alloc(to_crypt.len);
301
302 memcpy(to_crypt.ptr, this->decrypted.ptr, this->decrypted.len);
303 memcpy(to_crypt.ptr + this->decrypted.len, padding.ptr, padding.len);
304 *(to_crypt.ptr + to_crypt.len - 1) = padding.len;
305
306 /* build iv */
307 iv.len = block_size;
308 randomizer->allocate_pseudo_random_bytes(randomizer, iv.len, &iv);
309 randomizer->destroy(randomizer);
310
311 /* encrypt to_crypt chunk */
312 allocator_free(this->encrypted.ptr);
313 status = this->crypter->encrypt(this->crypter, to_crypt, iv, &result);
314 allocator_free(padding.ptr);
315 allocator_free(to_crypt.ptr);
316 if (status != SUCCESS)
317 {
318 this->logger->log(this->logger, ERROR|LEVEL1, "encryption failed");
319 allocator_free(iv.ptr);
320 return status;
321 }
322
323 /* build encrypted result with iv and signature */
324 this->encrypted.len = iv.len + result.len + this->signer->get_block_size(this->signer);
325 allocator_free(this->encrypted.ptr);
326 this->encrypted.ptr = allocator_alloc(this->encrypted.len);
327
328 /* fill in result, signature is left out */
329 memcpy(this->encrypted.ptr, iv.ptr, iv.len);
330 memcpy(this->encrypted.ptr + iv.len, result.ptr, result.len);
331
332 allocator_free(result.ptr);
333 allocator_free(iv.ptr);
334 return SUCCESS;
335 }
336
337 /**
338 * Implementation of encryption_payload_t.encrypt.
339 */
340 static status_t decrypt(private_encryption_payload_t *this)
341 {
342 chunk_t iv, concatenated;
343 u_int8_t padding_length;
344 status_t status;
345
346
347 this->logger->log(this->logger, CONTROL|LEVEL2, "decrypting encryption payload");
348
349 if (this->signer == NULL || this->crypter == NULL)
350 {
351 this->logger->log(this->logger, ERROR, "could not decrypt, no crypter/signer set");
352 return INVALID_STATE;
353 }
354
355 /* get IV */
356 iv.len = this->crypter->get_block_size(this->crypter);
357
358 iv.ptr = this->encrypted.ptr;
359
360 /* point concatenated to data + padding + padding_length*/
361 concatenated.ptr = this->encrypted.ptr + iv.len;
362 concatenated.len = this->encrypted.len - iv.len - this->signer->get_block_size(this->signer);
363
364 /* check the size of input:
365 * concatenated must be at least on block_size of crypter
366 */
367 if (concatenated.len < iv.len)
368 {
369 this->logger->log(this->logger, ERROR|LEVEL1, "could not decrypt, invalid input");
370 return FAILED;
371 }
372
373 /* free previus data, if any */
374 allocator_free(this->decrypted.ptr);
375
376 status = this->crypter->decrypt(this->crypter, concatenated, iv, &(this->decrypted));
377 if (status != SUCCESS)
378 {
379 this->logger->log(this->logger, ERROR|LEVEL1, "could not decrypt, decryption failed");
380 return FAILED;
381 }
382
383 /* get padding length, sits just bevore signature */
384 padding_length = *(this->decrypted.ptr + this->decrypted.len - 1);
385 /* add one byte to the padding length, since the padding_length field is not included */
386 padding_length++;
387 this->decrypted.len -= padding_length;
388
389 /* check size again */
390 if (padding_length > concatenated.len || this->decrypted.len < 0)
391 {
392 this->logger->log(this->logger, ERROR|LEVEL1, "decryption failed, invalid padding length found. Invalid key?");
393 /* decryption failed :-/ */
394 return FAILED;
395 }
396
397 /* free padding */
398 this->decrypted.ptr = allocator_realloc(this->decrypted.ptr, this->decrypted.len);
399 this->logger->log(this->logger, CONTROL|LEVEL2, "decryption successful, trying to parse content");
400 return (this->parse(this));
401 }
402
403 /**
404 * Implementation of encryption_payload_t.set_transforms.
405 */
406 static void set_transforms(private_encryption_payload_t *this, crypter_t* crypter, signer_t* signer)
407 {
408 this->signer = signer;
409 this->crypter = crypter;
410 }
411
412 /**
413 * Implementation of encryption_payload_t.build_signature.
414 */
415 static status_t build_signature(private_encryption_payload_t *this, chunk_t data)
416 {
417 chunk_t data_without_sig = data;
418 chunk_t sig;
419
420 if (this->signer == NULL)
421 {
422 this->logger->log(this->logger, ERROR, "unable to build signature, no signer set");
423 return INVALID_STATE;
424 }
425
426 sig.len = this->signer->get_block_size(this->signer);
427 data_without_sig.len -= sig.len;
428 sig.ptr = data.ptr + data_without_sig.len;
429 this->logger->log(this->logger, CONTROL|LEVEL2, "building signature");
430 this->signer->get_signature(this->signer, data_without_sig, sig.ptr);
431 return SUCCESS;
432 }
433
434 /**
435 * Implementation of encryption_payload_t.verify_signature.
436 */
437 static status_t verify_signature(private_encryption_payload_t *this, chunk_t data)
438 {
439 chunk_t sig, data_without_sig;
440 bool valid;
441
442 if (this->signer == NULL)
443 {
444 this->logger->log(this->logger, ERROR, "unable to verify signature, no signer set");
445 return INVALID_STATE;
446 }
447 /* find signature in data chunk */
448 sig.len = this->signer->get_block_size(this->signer);
449 if (data.len <= sig.len)
450 {
451 this->logger->log(this->logger, ERROR|LEVEL1, "unable to verify signature, invalid input");
452 return FAILED;
453 }
454 sig.ptr = data.ptr + data.len - sig.len;
455
456 /* verify it */
457 data_without_sig.len = data.len - sig.len;
458 data_without_sig.ptr = data.ptr;
459 valid = this->signer->verify_signature(this->signer, data_without_sig, sig);
460
461 if (!valid)
462 {
463 this->logger->log(this->logger, ERROR|LEVEL1, "signature verification failed");
464 return FAILED;
465 }
466
467 this->logger->log(this->logger, CONTROL|LEVEL2, "signature verification successful");
468 return SUCCESS;
469 }
470
471 /**
472 * Implementation of private_encryption_payload_t.generate.
473 */
474 static void generate(private_encryption_payload_t *this)
475 {
476 payload_t *current_payload, *next_payload;
477 generator_t *generator;
478 iterator_t *iterator;
479
480 /* recalculate length before generating */
481 this->compute_length(this);
482
483 /* create iterator */
484 iterator = this->payloads->create_iterator(this->payloads, TRUE);
485
486 /* get first payload */
487 if (iterator->has_next(iterator))
488 {
489 iterator->current(iterator, (void**)&current_payload);
490 this->next_payload = current_payload->get_type(current_payload);
491 }
492 else
493 {
494 /* no paylads? */
495 this->logger->log(this->logger, CONTROL|LEVEL1, "generating contained payloads, but no available");
496 allocator_free(this->decrypted.ptr);
497 this->decrypted = CHUNK_INITIALIZER;
498 iterator->destroy(iterator);
499 return;
500 }
501
502 generator = generator_create();
503
504 /* build all payload, except last */
505 while(iterator->has_next(iterator))
506 {
507 iterator->current(iterator, (void**)&next_payload);
508 current_payload->set_next_type(current_payload, next_payload->get_type(next_payload));
509 generator->generate_payload(generator, current_payload);
510 current_payload = next_payload;
511 }
512 iterator->destroy(iterator);
513
514 /* build last payload */
515 current_payload->set_next_type(current_payload, NO_PAYLOAD);
516 generator->generate_payload(generator, current_payload);
517
518 /* free already generated data */
519 allocator_free(this->decrypted.ptr);
520
521 generator->write_to_chunk(generator, &(this->decrypted));
522 generator->destroy(generator);
523 this->logger->log(this->logger, CONTROL|LEVEL1, "successfully generated content in encrpytion payload");
524 }
525
526 /**
527 * Implementation of private_encryption_payload_t.parse.
528 */
529 static status_t parse(private_encryption_payload_t *this)
530 {
531 parser_t *parser;
532 status_t status;
533 payload_type_t current_payload_type;
534
535 /* check if there is decrypted data */
536 if (this->decrypted.ptr == NULL)
537 {
538 this->logger->log(this->logger, ERROR, "unable to parse, no input!");
539 return INVALID_STATE;
540 }
541
542 /* build a parser on the decrypted data */
543 parser = parser_create(this->decrypted);
544
545 current_payload_type = this->next_payload;
546 /* parse all payloads */
547 while (current_payload_type != NO_PAYLOAD)
548 {
549 payload_t *current_payload;
550
551 status = parser->parse_payload(parser, current_payload_type, (payload_t**)&current_payload);
552 if (status != SUCCESS)
553 {
554 parser->destroy(parser);
555 return PARSE_ERROR;
556 }
557
558
559 status = current_payload->verify(current_payload);
560 if (status != SUCCESS)
561 {
562 this->logger->log(this->logger, ERROR|LEVEL1, "%s verification failed: %s",
563 mapping_find(payload_type_m,current_payload->get_type(current_payload)),
564 mapping_find(status_m, status));
565 current_payload->destroy(current_payload);
566 parser->destroy(parser);
567 return VERIFY_ERROR;
568 }
569
570 /* get next payload type */
571 current_payload_type = current_payload->get_next_type(current_payload);
572
573 this->payloads->insert_last(this->payloads,current_payload);
574 }
575 parser->destroy(parser);
576 this->logger->log(this->logger, CONTROL|LEVEL1, "succesfully parsed content of encryption payload");
577 return SUCCESS;
578 }
579
580 /**
581 * Implementation of private_encryption_payload_t.compute_length.
582 */
583 static void compute_length(private_encryption_payload_t *this)
584 {
585 iterator_t *iterator;
586 size_t block_size, length = 0;
587 iterator = this->payloads->create_iterator(this->payloads, TRUE);
588
589 /* count payload length */
590 while (iterator->has_next(iterator))
591 {
592 payload_t *current_payload;
593 iterator->current(iterator, (void **) &current_payload);
594 length += current_payload->get_length(current_payload);
595 }
596 iterator->destroy(iterator);
597
598 if (this->crypter && this->signer)
599 {
600 /* append one byte for padding length */
601 length++;
602 /* append padding */
603 block_size = this->crypter->get_block_size(this->crypter);
604 length += block_size - length % block_size;
605 /* add iv */
606 length += block_size;
607 /* add signature */
608 length += this->signer->get_block_size(this->signer);
609 }
610 length += ENCRYPTION_PAYLOAD_HEADER_LENGTH;
611 this->payload_length = length;
612 }
613
614
615 /**
616 * Implementation of payload_t.destroy.
617 */
618 static void destroy(private_encryption_payload_t *this)
619 {
620 /* all proposals are getting destroyed */
621 while (this->payloads->get_count(this->payloads) > 0)
622 {
623 payload_t *current_payload;
624 this->payloads->remove_last(this->payloads,(void **)&current_payload);
625 current_payload->destroy(current_payload);
626 }
627 this->payloads->destroy(this->payloads);
628 charon->logger_manager->destroy_logger(charon->logger_manager, this->logger);
629 allocator_free(this->encrypted.ptr);
630 allocator_free(this->decrypted.ptr);
631 allocator_free(this);
632 }
633
634 /*
635 * Described in header
636 */
637 encryption_payload_t *encryption_payload_create()
638 {
639 private_encryption_payload_t *this = allocator_alloc_thing(private_encryption_payload_t);
640
641 /* payload_t interface functions */
642 this->public.payload_interface.verify = (status_t (*) (payload_t *))verify;
643 this->public.payload_interface.get_encoding_rules = (void (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
644 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
645 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
646 this->public.payload_interface.set_next_type = (void (*) (payload_t *,payload_type_t)) set_next_type;
647 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_type;
648 this->public.payload_interface.destroy = (void (*) (payload_t *))destroy;
649
650 /* public functions */
651 this->public.create_payload_iterator = (iterator_t * (*) (encryption_payload_t *,bool)) create_payload_iterator;
652 this->public.add_payload = (void (*) (encryption_payload_t *,payload_t *)) add_payload;
653 this->public.remove_first_payload = (status_t (*)(encryption_payload_t*, payload_t **)) remove_first_payload;
654 this->public.get_payload_count = (size_t (*)(encryption_payload_t*)) get_payload_count;
655
656 this->public.encrypt = (status_t (*) (encryption_payload_t *)) encrypt;
657 this->public.decrypt = (status_t (*) (encryption_payload_t *)) decrypt;
658 this->public.set_transforms = (void (*) (encryption_payload_t*,crypter_t*,signer_t*)) set_transforms;
659 this->public.build_signature = (status_t (*) (encryption_payload_t*, chunk_t)) build_signature;
660 this->public.verify_signature = (status_t (*) (encryption_payload_t*, chunk_t)) verify_signature;
661 this->public.destroy = (void (*) (encryption_payload_t *)) destroy;
662
663 /* private functions */
664 this->compute_length = compute_length;
665 this->generate = generate;
666 this->parse = parse;
667 this->logger = charon->logger_manager->create_logger(charon->logger_manager, ENCRYPTION_PAYLOAD, NULL);
668
669 /* set default values of the fields */
670 this->critical = TRUE;
671 this->next_payload = NO_PAYLOAD;
672 this->payload_length = ENCRYPTION_PAYLOAD_HEADER_LENGTH;
673 this->encrypted = CHUNK_INITIALIZER;
674 this->decrypted = CHUNK_INITIALIZER;
675 this->signer = NULL;
676 this->crypter = NULL;
677 this->payloads = linked_list_create();
678
679 return (&(this->public));
680 }