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