Add a return value to aead_t.encrypt()
[strongswan.git] / src / libcharon / encoding / payloads / encryption_payload.c
1 /*
2 * Copyright (C) 2005-2010 Martin Willi
3 * Copyright (C) 2010 revosec AG
4 * Copyright (C) 2011 Tobias Brunner
5 * Copyright (C) 2005 Jan Hutter
6 * Hochschule fuer Technik Rapperswil
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
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
30 typedef struct private_encryption_payload_t private_encryption_payload_t;
31
32 /**
33 * Private data of an encryption_payload_t' Object.
34 *
35 */
36 struct private_encryption_payload_t {
37
38 /**
39 * Public encryption_payload_t interface.
40 */
41 encryption_payload_t public;
42
43 /**
44 * There is no next payload for an encryption payload,
45 * since encryption payload MUST be the last one.
46 * next_payload means here the first payload of the
47 * contained, encrypted payload.
48 */
49 u_int8_t next_payload;
50
51 /**
52 * Flags, including reserved bits
53 */
54 u_int8_t flags;
55
56 /**
57 * Length of this payload
58 */
59 u_int16_t payload_length;
60
61 /**
62 * Chunk containing the IV, plain, padding and ICV.
63 */
64 chunk_t encrypted;
65
66 /**
67 * AEAD transform to use
68 */
69 aead_t *aead;
70
71 /**
72 * Contained payloads
73 */
74 linked_list_t *payloads;
75
76 /**
77 * Type of payload, ENCRYPTED or ENCRYPTED_V1
78 */
79 payload_type_t type;
80 };
81
82 /**
83 * Encoding rules to parse or generate a IKEv2-Encryption Payload.
84 *
85 * The defined offsets are the positions in a object of type
86 * private_encryption_payload_t.
87 */
88 static encoding_rule_t encodings_v2[] = {
89 /* 1 Byte next payload type, stored in the field next_payload */
90 { U_INT_8, offsetof(private_encryption_payload_t, next_payload) },
91 /* Critical and 7 reserved bits, all stored for reconstruction */
92 { U_INT_8, offsetof(private_encryption_payload_t, flags) },
93 /* Length of the whole encryption payload*/
94 { PAYLOAD_LENGTH, offsetof(private_encryption_payload_t, payload_length) },
95 /* encrypted data, stored in a chunk. contains iv, data, padding */
96 { CHUNK_DATA, offsetof(private_encryption_payload_t, encrypted) },
97 };
98
99 /*
100 1 2 3
101 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
102 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
103 ! Next Payload !C! RESERVED ! Payload Length !
104 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
105 ! Initialization Vector !
106 ! (length is block size for encryption algorithm) !
107 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
108 ! Encrypted IKE Payloads !
109 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
110 ! ! Padding (0-255 octets) !
111 +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+
112 ! ! Pad Length !
113 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
114 ~ Integrity Checksum Data ~
115 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
116 */
117
118 /**
119 * Encoding rules to parse or generate a complete encrypted IKEv1 message.
120 *
121 * The defined offsets are the positions in a object of type
122 * private_encryption_payload_t.
123 */
124 static encoding_rule_t encodings_v1[] = {
125 /* encrypted data, stored in a chunk */
126 { ENCRYPTED_DATA, offsetof(private_encryption_payload_t, encrypted) },
127 };
128
129 /*
130 1 2 3
131 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
132 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
133 ! Encrypted IKE Payloads !
134 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
135 ! ! Padding (0-255 octets) !
136 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
137 */
138
139 METHOD(payload_t, verify, status_t,
140 private_encryption_payload_t *this)
141 {
142 return SUCCESS;
143 }
144
145 METHOD(payload_t, get_encoding_rules, int,
146 private_encryption_payload_t *this, encoding_rule_t **rules)
147 {
148 if (this->type == ENCRYPTED)
149 {
150 *rules = encodings_v2;
151 return countof(encodings_v2);
152 }
153 *rules = encodings_v1;
154 return countof(encodings_v1);
155 }
156
157 METHOD(payload_t, get_header_length, int,
158 private_encryption_payload_t *this)
159 {
160 if (this->type == ENCRYPTED)
161 {
162 return 4;
163 }
164 return 0;
165 }
166
167 METHOD(payload_t, get_type, payload_type_t,
168 private_encryption_payload_t *this)
169 {
170 return this->type;
171 }
172
173 METHOD(payload_t, get_next_type, payload_type_t,
174 private_encryption_payload_t *this)
175 {
176 return this->next_payload;
177 }
178
179 METHOD(payload_t, set_next_type, void,
180 private_encryption_payload_t *this, payload_type_t type)
181 {
182 /* the next payload is set during add, still allow this for IKEv1 */
183 this->next_payload = type;
184 }
185
186 /**
187 * Compute the length of the whole payload
188 */
189 static void compute_length(private_encryption_payload_t *this)
190 {
191 enumerator_t *enumerator;
192 payload_t *payload;
193 size_t bs, length = 0;
194
195 if (this->encrypted.len)
196 {
197 length = this->encrypted.len;
198 }
199 else
200 {
201 enumerator = this->payloads->create_enumerator(this->payloads);
202 while (enumerator->enumerate(enumerator, &payload))
203 {
204 length += payload->get_length(payload);
205 }
206 enumerator->destroy(enumerator);
207
208 if (this->aead)
209 {
210 /* append padding */
211 bs = this->aead->get_block_size(this->aead);
212 length += bs - (length % bs);
213 /* add iv */
214 length += this->aead->get_iv_size(this->aead);
215 /* add icv */
216 length += this->aead->get_icv_size(this->aead);
217 }
218 }
219 length += get_header_length(this);
220 this->payload_length = length;
221 }
222
223 METHOD2(payload_t, encryption_payload_t, get_length, size_t,
224 private_encryption_payload_t *this)
225 {
226 compute_length(this);
227 return this->payload_length;
228 }
229
230 METHOD(encryption_payload_t, add_payload, void,
231 private_encryption_payload_t *this, payload_t *payload)
232 {
233 payload_t *last_payload;
234
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, payload);
246 compute_length(this);
247 }
248
249 METHOD(encryption_payload_t, remove_payload, payload_t *,
250 private_encryption_payload_t *this)
251 {
252 payload_t *payload;
253
254 if (this->payloads->remove_first(this->payloads,
255 (void**)&payload) == SUCCESS)
256 {
257 return payload;
258 }
259 return NULL;
260 }
261
262 /**
263 * Generate payload before encryption
264 */
265 static chunk_t generate(private_encryption_payload_t *this,
266 generator_t *generator)
267 {
268 payload_t *current, *next;
269 enumerator_t *enumerator;
270 u_int32_t *lenpos;
271 chunk_t chunk = chunk_empty;
272
273 enumerator = this->payloads->create_enumerator(this->payloads);
274 if (enumerator->enumerate(enumerator, &current))
275 {
276 this->next_payload = current->get_type(current);
277
278 while (enumerator->enumerate(enumerator, &next))
279 {
280 current->set_next_type(current, next->get_type(next));
281 generator->generate_payload(generator, current);
282 current = next;
283 }
284 current->set_next_type(current, NO_PAYLOAD);
285 generator->generate_payload(generator, current);
286
287 chunk = generator->get_chunk(generator, &lenpos);
288 DBG2(DBG_ENC, "generated content in encryption payload");
289 }
290 enumerator->destroy(enumerator);
291 return chunk;
292 }
293
294 /**
295 * Append the encryption payload header to the associated data
296 */
297 static chunk_t append_header(private_encryption_payload_t *this, chunk_t assoc)
298 {
299 struct {
300 u_int8_t next_payload;
301 u_int8_t flags;
302 u_int16_t length;
303 } __attribute__((packed)) header = {
304 .next_payload = this->next_payload,
305 .flags = this->flags,
306 .length = htons(get_length(this)),
307 };
308 return chunk_cat("cc", assoc, chunk_from_thing(header));
309 }
310
311 METHOD(encryption_payload_t, encrypt, bool,
312 private_encryption_payload_t *this, chunk_t assoc)
313 {
314 chunk_t iv, plain, padding, icv, crypt;
315 generator_t *generator;
316 rng_t *rng;
317 size_t bs;
318
319 if (this->aead == NULL)
320 {
321 DBG1(DBG_ENC, "encrypting encryption payload failed, transform missing");
322 return FALSE;
323 }
324
325 rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
326 if (!rng)
327 {
328 DBG1(DBG_ENC, "encrypting encryption payload failed, no RNG found");
329 return FALSE;
330 }
331
332 assoc = append_header(this, assoc);
333
334 generator = generator_create();
335 plain = generate(this, generator);
336 bs = this->aead->get_block_size(this->aead);
337 /* we need at least one byte padding to store the padding length */
338 padding.len = bs - (plain.len % bs);
339 iv.len = this->aead->get_iv_size(this->aead);
340 icv.len = this->aead->get_icv_size(this->aead);
341
342 /* prepare data to authenticate-encrypt:
343 * | IV | plain | padding | ICV |
344 * \____crypt______/ ^
345 * | /
346 * v /
347 * assoc -> + ------->/
348 */
349 free(this->encrypted.ptr);
350 this->encrypted = chunk_alloc(iv.len + plain.len + padding.len + icv.len);
351 iv.ptr = this->encrypted.ptr;
352 memcpy(iv.ptr + iv.len, plain.ptr, plain.len);
353 plain.ptr = iv.ptr + iv.len;
354 padding.ptr = plain.ptr + plain.len;
355 icv.ptr = padding.ptr + padding.len;
356 crypt = chunk_create(plain.ptr, plain.len + padding.len);
357 generator->destroy(generator);
358
359 rng->get_bytes(rng, iv.len, iv.ptr);
360 rng->get_bytes(rng, padding.len - 1, padding.ptr);
361 padding.ptr[padding.len - 1] = padding.len - 1;
362 rng->destroy(rng);
363
364 DBG3(DBG_ENC, "encryption payload encryption:");
365 DBG3(DBG_ENC, "IV %B", &iv);
366 DBG3(DBG_ENC, "plain %B", &plain);
367 DBG3(DBG_ENC, "padding %B", &padding);
368 DBG3(DBG_ENC, "assoc %B", &assoc);
369
370 if (!this->aead->encrypt(this->aead, crypt, assoc, iv, NULL))
371 {
372 free(assoc.ptr);
373 return FALSE;
374 }
375
376 DBG3(DBG_ENC, "encrypted %B", &crypt);
377 DBG3(DBG_ENC, "ICV %B", &icv);
378
379 free(assoc.ptr);
380
381 return TRUE;
382 }
383
384 METHOD(encryption_payload_t, encrypt_v1, bool,
385 private_encryption_payload_t *this, chunk_t iv)
386 {
387 generator_t *generator;
388 chunk_t plain, padding;
389 size_t bs;
390
391 if (this->aead == NULL)
392 {
393 DBG1(DBG_ENC, "encryption failed, transform missing");
394 chunk_free(&iv);
395 return FALSE;
396 }
397
398 generator = generator_create();
399 plain = generate(this, generator);
400 bs = this->aead->get_block_size(this->aead);
401 padding.len = bs - (plain.len % bs);
402
403 /* prepare data to encrypt:
404 * | plain | padding | */
405 free(this->encrypted.ptr);
406 this->encrypted = chunk_alloc(plain.len + padding.len);
407 memcpy(this->encrypted.ptr, plain.ptr, plain.len);
408 plain.ptr = this->encrypted.ptr;
409 padding.ptr = plain.ptr + plain.len;
410 memset(padding.ptr, 0, padding.len);
411 generator->destroy(generator);
412
413 DBG3(DBG_ENC, "encrypting payloads:");
414 DBG3(DBG_ENC, "plain %B", &plain);
415 DBG3(DBG_ENC, "padding %B", &padding);
416
417 if (!this->aead->encrypt(this->aead, this->encrypted, chunk_empty, iv, NULL))
418 {
419 chunk_free(&iv);
420 return FALSE;
421 }
422 chunk_free(&iv);
423
424 DBG3(DBG_ENC, "encrypted %B", &this->encrypted);
425
426 return TRUE;
427 }
428
429 /**
430 * Parse the payloads after decryption.
431 */
432 static status_t parse(private_encryption_payload_t *this, chunk_t plain)
433 {
434 parser_t *parser;
435 payload_type_t type;
436
437 parser = parser_create(plain);
438 type = this->next_payload;
439 while (type != NO_PAYLOAD)
440 {
441 payload_t *payload;
442
443 if (plain.len < 4 || untoh16(plain.ptr + 2) > plain.len)
444 {
445 DBG1(DBG_ENC, "invalid %N payload length, decryption failed?",
446 payload_type_names, type);
447 parser->destroy(parser);
448 return PARSE_ERROR;
449 }
450 if (parser->parse_payload(parser, type, &payload) != SUCCESS)
451 {
452 parser->destroy(parser);
453 return PARSE_ERROR;
454 }
455 if (payload->verify(payload) != SUCCESS)
456 {
457 DBG1(DBG_ENC, "%N verification failed",
458 payload_type_names, payload->get_type(payload));
459 payload->destroy(payload);
460 parser->destroy(parser);
461 return VERIFY_ERROR;
462 }
463 type = payload->get_next_type(payload);
464 this->payloads->insert_last(this->payloads, payload);
465 }
466 parser->destroy(parser);
467 DBG2(DBG_ENC, "parsed content of encryption payload");
468 return SUCCESS;
469 }
470
471 METHOD(encryption_payload_t, decrypt, status_t,
472 private_encryption_payload_t *this, chunk_t assoc)
473 {
474 chunk_t iv, plain, padding, icv, crypt;
475 size_t bs;
476
477 if (this->aead == NULL)
478 {
479 DBG1(DBG_ENC, "decrypting encryption payload failed, transform missing");
480 return INVALID_STATE;
481 }
482
483 /* prepare data to authenticate-decrypt:
484 * | IV | plain | padding | ICV |
485 * \____crypt______/ ^
486 * | /
487 * v /
488 * assoc -> + ------->/
489 */
490
491 bs = this->aead->get_block_size(this->aead);
492 iv.len = this->aead->get_iv_size(this->aead);
493 iv.ptr = this->encrypted.ptr;
494 icv.len = this->aead->get_icv_size(this->aead);
495 icv.ptr = this->encrypted.ptr + this->encrypted.len - icv.len;
496 crypt.ptr = iv.ptr + iv.len;
497 crypt.len = this->encrypted.len - iv.len;
498
499 if (iv.len + icv.len > this->encrypted.len ||
500 (crypt.len - icv.len) % bs)
501 {
502 DBG1(DBG_ENC, "decrypting encryption payload failed, invalid length");
503 return FAILED;
504 }
505
506 assoc = append_header(this, assoc);
507
508 DBG3(DBG_ENC, "encryption payload decryption:");
509 DBG3(DBG_ENC, "IV %B", &iv);
510 DBG3(DBG_ENC, "encrypted %B", &crypt);
511 DBG3(DBG_ENC, "ICV %B", &icv);
512 DBG3(DBG_ENC, "assoc %B", &assoc);
513
514 if (!this->aead->decrypt(this->aead, crypt, assoc, iv, NULL))
515 {
516 DBG1(DBG_ENC, "verifying encryption payload integrity failed");
517 free(assoc.ptr);
518 return FAILED;
519 }
520 free(assoc.ptr);
521
522 plain = chunk_create(crypt.ptr, crypt.len - icv.len);
523 padding.len = plain.ptr[plain.len - 1] + 1;
524 if (padding.len > plain.len)
525 {
526 DBG1(DBG_ENC, "decrypting encryption payload failed, "
527 "padding invalid %B", &crypt);
528 return PARSE_ERROR;
529 }
530 plain.len -= padding.len;
531 padding.ptr = plain.ptr + plain.len;
532
533 DBG3(DBG_ENC, "plain %B", &plain);
534 DBG3(DBG_ENC, "padding %B", &padding);
535
536 return parse(this, plain);
537 }
538
539 METHOD(encryption_payload_t, decrypt_v1, status_t,
540 private_encryption_payload_t *this, chunk_t iv)
541 {
542 if (this->aead == NULL)
543 {
544 DBG1(DBG_ENC, "decryption failed, transform missing");
545 chunk_free(&iv);
546 return INVALID_STATE;
547 }
548
549 /* data must be a multiple of block size */
550 if (iv.len != this->aead->get_block_size(this->aead) ||
551 this->encrypted.len < iv.len || this->encrypted.len % iv.len)
552 {
553 DBG1(DBG_ENC, "decryption failed, invalid length");
554 chunk_free(&iv);
555 return FAILED;
556 }
557
558 DBG3(DBG_ENC, "decrypting payloads:");
559 DBG3(DBG_ENC, "encrypted %B", &this->encrypted);
560
561 this->aead->decrypt(this->aead, this->encrypted, chunk_empty, iv, NULL);
562 chunk_free(&iv);
563
564 DBG3(DBG_ENC, "plain %B", &this->encrypted);
565
566 return parse(this, this->encrypted);
567 }
568
569 METHOD(encryption_payload_t, set_transform, void,
570 private_encryption_payload_t *this, aead_t* aead)
571 {
572 this->aead = aead;
573 }
574
575 METHOD2(payload_t, encryption_payload_t, destroy, void,
576 private_encryption_payload_t *this)
577 {
578 this->payloads->destroy_offset(this->payloads, offsetof(payload_t, destroy));
579 free(this->encrypted.ptr);
580 free(this);
581 }
582
583 /*
584 * Described in header
585 */
586 encryption_payload_t *encryption_payload_create(payload_type_t type)
587 {
588 private_encryption_payload_t *this;
589
590 INIT(this,
591 .public = {
592 .payload_interface = {
593 .verify = _verify,
594 .get_encoding_rules = _get_encoding_rules,
595 .get_header_length = _get_header_length,
596 .get_length = _get_length,
597 .get_next_type = _get_next_type,
598 .set_next_type = _set_next_type,
599 .get_type = _get_type,
600 .destroy = _destroy,
601 },
602 .get_length = _get_length,
603 .add_payload = _add_payload,
604 .remove_payload = _remove_payload,
605 .set_transform = _set_transform,
606 .encrypt = _encrypt,
607 .decrypt = _decrypt,
608 .destroy = _destroy,
609 },
610 .next_payload = NO_PAYLOAD,
611 .payload_length = get_header_length(this),
612 .payloads = linked_list_create(),
613 .type = type,
614 );
615
616 if (type == ENCRYPTED_V1)
617 {
618 this->public.encrypt = _encrypt_v1;
619 this->public.decrypt = _decrypt_v1;
620 }
621
622 return &this->public;
623 }