Use different return values in payload decryption to distinguish between integrity...
[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) 2005 Jan Hutter
5 * Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 #include <stddef.h>
19 #include <string.h>
20
21 #include "encryption_payload.h"
22
23 #include <daemon.h>
24 #include <encoding/payloads/encodings.h>
25 #include <utils/linked_list.h>
26 #include <encoding/generator.h>
27 #include <encoding/parser.h>
28
29 typedef struct private_encryption_payload_t private_encryption_payload_t;
30
31 /**
32 * Private data of an encryption_payload_t' Object.
33 *
34 */
35 struct private_encryption_payload_t {
36
37 /**
38 * Public encryption_payload_t interface.
39 */
40 encryption_payload_t public;
41
42 /**
43 * There is no next payload for an encryption payload,
44 * since encryption payload MUST be the last one.
45 * next_payload means here the first payload of the
46 * contained, encrypted payload.
47 */
48 u_int8_t next_payload;
49
50 /**
51 * Flags, including reserved bits
52 */
53 u_int8_t flags;
54
55 /**
56 * Length of this payload
57 */
58 u_int16_t payload_length;
59
60 /**
61 * Chunk containing the IV, plain, padding and ICV.
62 */
63 chunk_t encrypted;
64
65 /**
66 * AEAD transform to use
67 */
68 aead_t *aead;
69
70 /**
71 * Contained payloads
72 */
73 linked_list_t *payloads;
74 };
75
76 /**
77 * Encoding rules to parse or generate a IKEv2-Encryption Payload.
78 *
79 * The defined offsets are the positions in a object of type
80 * private_encryption_payload_t.
81 */
82 encoding_rule_t encryption_payload_encodings[] = {
83 /* 1 Byte next payload type, stored in the field next_payload */
84 { U_INT_8, offsetof(private_encryption_payload_t, next_payload) },
85 /* Critical and 7 reserved bits, all stored for reconstruction */
86 { U_INT_8, offsetof(private_encryption_payload_t, flags) },
87 /* Length of the whole encryption payload*/
88 { PAYLOAD_LENGTH, offsetof(private_encryption_payload_t, payload_length) },
89 /* encrypted data, stored in a chunk. contains iv, data, padding */
90 { ENCRYPTED_DATA, offsetof(private_encryption_payload_t, encrypted) },
91 };
92
93 /*
94 1 2 3
95 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
96 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
97 ! Next Payload !C! RESERVED ! Payload Length !
98 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
99 ! Initialization Vector !
100 ! (length is block size for encryption algorithm) !
101 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
102 ! Encrypted IKE Payloads !
103 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
104 ! ! Padding (0-255 octets) !
105 +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+
106 ! ! Pad Length !
107 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
108 ~ Integrity Checksum Data ~
109 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
110 */
111
112 METHOD(payload_t, verify, status_t,
113 private_encryption_payload_t *this)
114 {
115 return SUCCESS;
116 }
117
118 METHOD(payload_t, get_encoding_rules, void,
119 private_encryption_payload_t *this, encoding_rule_t **rules,
120 size_t *count)
121 {
122 *rules = encryption_payload_encodings;
123 *count = countof(encryption_payload_encodings);
124 }
125
126 METHOD(payload_t, get_type, payload_type_t,
127 private_encryption_payload_t *this)
128 {
129 return ENCRYPTED;
130 }
131
132 METHOD(payload_t, get_next_type, payload_type_t,
133 private_encryption_payload_t *this)
134 {
135 return this->next_payload;
136 }
137
138 METHOD(payload_t, set_next_type, void,
139 private_encryption_payload_t *this, payload_type_t type)
140 {
141 /* the next payload is set during add */
142 }
143
144 /**
145 * Compute the lenght of the whole payload
146 */
147 static void compute_length(private_encryption_payload_t *this)
148 {
149 enumerator_t *enumerator;
150 payload_t *payload;
151 size_t bs, length = 0;
152
153 if (this->encrypted.len)
154 {
155 length = this->encrypted.len;
156 }
157 else
158 {
159 enumerator = this->payloads->create_enumerator(this->payloads);
160 while (enumerator->enumerate(enumerator, &payload))
161 {
162 length += payload->get_length(payload);
163 }
164 enumerator->destroy(enumerator);
165
166 if (this->aead)
167 {
168 /* append padding */
169 bs = this->aead->get_block_size(this->aead);
170 length += bs - (length % bs);
171 /* add iv */
172 length += this->aead->get_iv_size(this->aead);
173 /* add icv */
174 length += this->aead->get_icv_size(this->aead);
175 }
176 }
177 length += ENCRYPTION_PAYLOAD_HEADER_LENGTH;
178 this->payload_length = length;
179 }
180
181 METHOD2(payload_t, encryption_payload_t, get_length, size_t,
182 private_encryption_payload_t *this)
183 {
184 compute_length(this);
185 return this->payload_length;
186 }
187
188 METHOD(encryption_payload_t, add_payload, void,
189 private_encryption_payload_t *this, payload_t *payload)
190 {
191 payload_t *last_payload;
192
193 if (this->payloads->get_count(this->payloads) > 0)
194 {
195 this->payloads->get_last(this->payloads, (void **)&last_payload);
196 last_payload->set_next_type(last_payload, payload->get_type(payload));
197 }
198 else
199 {
200 this->next_payload = payload->get_type(payload);
201 }
202 payload->set_next_type(payload, NO_PAYLOAD);
203 this->payloads->insert_last(this->payloads, payload);
204 compute_length(this);
205 }
206
207 METHOD(encryption_payload_t, remove_payload, payload_t *,
208 private_encryption_payload_t *this)
209 {
210 payload_t *payload;
211
212 if (this->payloads->remove_first(this->payloads,
213 (void**)&payload) == SUCCESS)
214 {
215 return payload;
216 }
217 return NULL;
218 }
219
220 /**
221 * Generate payload before encryption
222 */
223 static chunk_t generate(private_encryption_payload_t *this,
224 generator_t *generator)
225 {
226 payload_t *current, *next;
227 enumerator_t *enumerator;
228 u_int32_t *lenpos;
229 chunk_t chunk = chunk_empty;
230
231 enumerator = this->payloads->create_enumerator(this->payloads);
232 if (enumerator->enumerate(enumerator, &current))
233 {
234 this->next_payload = current->get_type(current);
235
236 while (enumerator->enumerate(enumerator, &next))
237 {
238 current->set_next_type(current, next->get_type(next));
239 generator->generate_payload(generator, current);
240 current = next;
241 }
242 current->set_next_type(current, NO_PAYLOAD);
243 generator->generate_payload(generator, current);
244
245 chunk = generator->get_chunk(generator, &lenpos);
246 DBG2(DBG_ENC, "generated content in encryption payload");
247 }
248 enumerator->destroy(enumerator);
249 return chunk;
250 }
251
252 /**
253 * Append the encryption payload header to the associated data
254 */
255 static chunk_t append_header(private_encryption_payload_t *this, chunk_t assoc)
256 {
257 struct {
258 u_int8_t next_payload;
259 u_int8_t flags;
260 u_int16_t length;
261 } __attribute__((packed)) header = {
262 .next_payload = this->next_payload,
263 .flags = this->flags,
264 .length = htons(get_length(this)),
265 };
266 return chunk_cat("cc", assoc, chunk_from_thing(header));
267 }
268
269 METHOD(encryption_payload_t, encrypt, bool,
270 private_encryption_payload_t *this, chunk_t assoc)
271 {
272 chunk_t iv, plain, padding, icv, crypt;
273 generator_t *generator;
274 rng_t *rng;
275 size_t bs;
276
277 if (this->aead == NULL)
278 {
279 DBG1(DBG_ENC, "encrypting encryption payload failed, transform missing");
280 return FALSE;
281 }
282
283 rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
284 if (!rng)
285 {
286 DBG1(DBG_ENC, "encrypting encryption payload failed, no RNG found");
287 return FALSE;
288 }
289
290 assoc = append_header(this, assoc);
291
292 generator = generator_create();
293 plain = generate(this, generator);
294 bs = this->aead->get_block_size(this->aead);
295 /* we need at least one byte padding to store the padding length */
296 padding.len = bs - (plain.len % bs);
297 iv.len = this->aead->get_iv_size(this->aead);
298 icv.len = this->aead->get_icv_size(this->aead);
299
300 /* prepare data to authenticate-encrypt:
301 * | IV | plain | padding | ICV |
302 * \____crypt______/ ^
303 * | /
304 * v /
305 * assoc -> + ------->/
306 */
307 free(this->encrypted.ptr);
308 this->encrypted = chunk_alloc(iv.len + plain.len + padding.len + icv.len);
309 iv.ptr = this->encrypted.ptr;
310 memcpy(iv.ptr + iv.len, plain.ptr, plain.len);
311 plain.ptr = iv.ptr + iv.len;
312 padding.ptr = plain.ptr + plain.len;
313 icv.ptr = padding.ptr + padding.len;
314 crypt = chunk_create(plain.ptr, plain.len + padding.len);
315 generator->destroy(generator);
316
317 rng->get_bytes(rng, iv.len, iv.ptr);
318 rng->get_bytes(rng, padding.len - 1, padding.ptr);
319 padding.ptr[padding.len - 1] = padding.len - 1;
320 rng->destroy(rng);
321
322 DBG3(DBG_ENC, "encryption payload encryption:");
323 DBG3(DBG_ENC, "IV %B", &iv);
324 DBG3(DBG_ENC, "plain %B", &plain);
325 DBG3(DBG_ENC, "padding %B", &padding);
326 DBG3(DBG_ENC, "assoc %B", &assoc);
327
328 this->aead->encrypt(this->aead, crypt, assoc, iv, NULL);
329
330 DBG3(DBG_ENC, "encrypted %B", &crypt);
331 DBG3(DBG_ENC, "ICV %B", &icv);
332
333 free(assoc.ptr);
334
335 return TRUE;
336 }
337
338 /**
339 * Parse the payloads after decryption.
340 */
341 static status_t parse(private_encryption_payload_t *this, chunk_t plain)
342 {
343 parser_t *parser;
344 payload_type_t type;
345
346 parser = parser_create(plain);
347 type = this->next_payload;
348 while (type != NO_PAYLOAD)
349 {
350 payload_t *payload;
351
352 if (parser->parse_payload(parser, type, &payload) != SUCCESS)
353 {
354 parser->destroy(parser);
355 return PARSE_ERROR;
356 }
357 if (payload->verify(payload) != SUCCESS)
358 {
359 DBG1(DBG_ENC, "%N verification failed",
360 payload_type_names, payload->get_type(payload));
361 payload->destroy(payload);
362 parser->destroy(parser);
363 return VERIFY_ERROR;
364 }
365 type = payload->get_next_type(payload);
366 this->payloads->insert_last(this->payloads, payload);
367 }
368 parser->destroy(parser);
369 DBG2(DBG_ENC, "parsed content of encryption payload");
370 return SUCCESS;
371 }
372
373 METHOD(encryption_payload_t, decrypt, status_t,
374 private_encryption_payload_t *this, chunk_t assoc)
375 {
376 chunk_t iv, plain, padding, icv, crypt;
377 size_t bs;
378
379 if (this->aead == NULL)
380 {
381 DBG1(DBG_ENC, "decrypting encryption payload failed, transform missing");
382 return INVALID_STATE;
383 }
384
385 /* prepare data to authenticate-decrypt:
386 * | IV | plain | padding | ICV |
387 * \____crypt______/ ^
388 * | /
389 * v /
390 * assoc -> + ------->/
391 */
392
393 bs = this->aead->get_block_size(this->aead);
394 iv.len = this->aead->get_iv_size(this->aead);
395 iv.ptr = this->encrypted.ptr;
396 icv.len = this->aead->get_icv_size(this->aead);
397 icv.ptr = this->encrypted.ptr + this->encrypted.len - icv.len;
398 crypt.ptr = iv.ptr + iv.len;
399 crypt.len = this->encrypted.len - iv.len;
400
401 if (iv.len + icv.len > this->encrypted.len ||
402 (crypt.len - icv.len) % bs)
403 {
404 DBG1(DBG_ENC, "decrypting encryption payload failed, invalid length");
405 return FAILED;
406 }
407
408 assoc = append_header(this, assoc);
409
410 DBG3(DBG_ENC, "encryption payload decryption:");
411 DBG3(DBG_ENC, "IV %B", &iv);
412 DBG3(DBG_ENC, "encrypted %B", &crypt);
413 DBG3(DBG_ENC, "ICV %B", &icv);
414 DBG3(DBG_ENC, "assoc %B", &assoc);
415
416 if (!this->aead->decrypt(this->aead, crypt, assoc, iv, NULL))
417 {
418 DBG1(DBG_ENC, "verifying encryption payload integrity failed");
419 free(assoc.ptr);
420 return FAILED;
421 }
422 free(assoc.ptr);
423
424 plain = chunk_create(crypt.ptr, crypt.len - icv.len);
425 padding.len = plain.ptr[plain.len - 1] + 1;
426 if (padding.len > plain.len)
427 {
428 DBG1(DBG_ENC, "decrypting encryption payload failed, "
429 "padding invalid %B", &crypt);
430 return PARSE_ERROR;
431 }
432 plain.len -= padding.len;
433 padding.ptr = plain.ptr + plain.len;
434
435 DBG3(DBG_ENC, "plain %B", &plain);
436 DBG3(DBG_ENC, "padding %B", &padding);
437
438 return parse(this, plain);
439 }
440
441 METHOD(encryption_payload_t, set_transform, void,
442 private_encryption_payload_t *this, aead_t* aead)
443 {
444 this->aead = aead;
445 }
446
447 METHOD2(payload_t, encryption_payload_t, destroy, void,
448 private_encryption_payload_t *this)
449 {
450 this->payloads->destroy_offset(this->payloads, offsetof(payload_t, destroy));
451 free(this->encrypted.ptr);
452 free(this);
453 }
454
455 /*
456 * Described in header
457 */
458 encryption_payload_t *encryption_payload_create()
459 {
460 private_encryption_payload_t *this;
461
462 INIT(this,
463 .public = {
464 .payload_interface = {
465 .verify = _verify,
466 .get_encoding_rules = _get_encoding_rules,
467 .get_length = _get_length,
468 .get_next_type = _get_next_type,
469 .set_next_type = _set_next_type,
470 .get_type = _get_type,
471 .destroy = _destroy,
472 },
473 .get_length = _get_length,
474 .add_payload = _add_payload,
475 .remove_payload = _remove_payload,
476 .set_transform = _set_transform,
477 .encrypt = _encrypt,
478 .decrypt = _decrypt,
479 .destroy = _destroy,
480 },
481 .next_payload = NO_PAYLOAD,
482 .payload_length = ENCRYPTION_PAYLOAD_HEADER_LENGTH,
483 .payloads = linked_list_create(),
484 );
485
486 return &this->public;
487 }