testing: Start charon before Apache in tnc/tnccs-20-pdp-pt-tls
[strongswan.git] / src / libipsec / esp_packet.c
1 /*
2 * Copyright (C) 2012-2013 Tobias Brunner
3 * Copyright (C) 2012 Giuliano Grassi
4 * Copyright (C) 2012 Ralf Sager
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
19 #include "esp_packet.h"
20
21 #include <library.h>
22 #include <utils/debug.h>
23 #include <crypto/crypters/crypter.h>
24 #include <crypto/signers/signer.h>
25 #include <bio/bio_reader.h>
26 #include <bio/bio_writer.h>
27
28 #include <netinet/in.h>
29
30 typedef struct private_esp_packet_t private_esp_packet_t;
31
32 /**
33 * Private additions to esp_packet_t.
34 */
35 struct private_esp_packet_t {
36
37 /**
38 * Public members
39 */
40 esp_packet_t public;
41
42 /**
43 * Raw ESP packet
44 */
45 packet_t *packet;
46
47 /**
48 * Payload of this packet
49 */
50 ip_packet_t *payload;
51
52 /**
53 * Next Header info (e.g. IPPROTO_IPIP)
54 */
55 uint8_t next_header;
56
57 };
58
59 /**
60 * Forward declaration for clone()
61 */
62 static private_esp_packet_t *esp_packet_create_internal(packet_t *packet);
63
64 METHOD(packet_t, set_source, void,
65 private_esp_packet_t *this, host_t *src)
66 {
67 return this->packet->set_source(this->packet, src);
68 }
69
70 METHOD2(esp_packet_t, packet_t, get_source, host_t*,
71 private_esp_packet_t *this)
72 {
73 return this->packet->get_source(this->packet);
74 }
75
76 METHOD(packet_t, set_destination, void,
77 private_esp_packet_t *this, host_t *dst)
78 {
79 return this->packet->set_destination(this->packet, dst);
80 }
81
82 METHOD2(esp_packet_t, packet_t, get_destination, host_t*,
83 private_esp_packet_t *this)
84 {
85 return this->packet->get_destination(this->packet);
86 }
87
88 METHOD(packet_t, get_data, chunk_t,
89 private_esp_packet_t *this)
90 {
91 return this->packet->get_data(this->packet);
92 }
93
94 METHOD(packet_t, set_data, void,
95 private_esp_packet_t *this, chunk_t data)
96 {
97 return this->packet->set_data(this->packet, data);
98 }
99
100 METHOD(packet_t, get_dscp, uint8_t,
101 private_esp_packet_t *this)
102 {
103 return this->packet->get_dscp(this->packet);
104 }
105
106 METHOD(packet_t, set_dscp, void,
107 private_esp_packet_t *this, uint8_t value)
108 {
109 this->packet->set_dscp(this->packet, value);
110 }
111
112 METHOD(packet_t, skip_bytes, void,
113 private_esp_packet_t *this, size_t bytes)
114 {
115 return this->packet->skip_bytes(this->packet, bytes);
116 }
117
118 METHOD(packet_t, clone_, packet_t*,
119 private_esp_packet_t *this)
120 {
121 private_esp_packet_t *pkt;
122
123 pkt = esp_packet_create_internal(this->packet->clone(this->packet));
124 pkt->payload = this->payload ? this->payload->clone(this->payload) : NULL;
125 pkt->next_header = this->next_header;
126 return &pkt->public.packet;
127 }
128
129 METHOD(esp_packet_t, parse_header, bool,
130 private_esp_packet_t *this, uint32_t *spi)
131 {
132 bio_reader_t *reader;
133 uint32_t seq;
134
135 reader = bio_reader_create(this->packet->get_data(this->packet));
136 if (!reader->read_uint32(reader, spi) ||
137 !reader->read_uint32(reader, &seq))
138 {
139 DBG1(DBG_ESP, "failed to parse ESP header: invalid length");
140 reader->destroy(reader);
141 return FALSE;
142 }
143 reader->destroy(reader);
144
145 DBG2(DBG_ESP, "parsed ESP header with SPI %.8x [seq %u]", *spi, seq);
146 *spi = htonl(*spi);
147 return TRUE;
148 }
149
150 /**
151 * Check padding as specified in RFC 4303
152 */
153 static bool check_padding(chunk_t padding)
154 {
155 size_t i;
156
157 for (i = 0; i < padding.len; ++i)
158 {
159 if (padding.ptr[i] != (uint8_t)(i + 1))
160 {
161 return FALSE;
162 }
163 }
164 return TRUE;
165 }
166
167 /**
168 * Remove the padding from the payload and set the next header info
169 */
170 static bool remove_padding(private_esp_packet_t *this, chunk_t plaintext)
171 {
172 uint8_t next_header, pad_length;
173 chunk_t padding, payload;
174 bio_reader_t *reader;
175
176 reader = bio_reader_create(plaintext);
177 if (!reader->read_uint8_end(reader, &next_header) ||
178 !reader->read_uint8_end(reader, &pad_length))
179 {
180 DBG1(DBG_ESP, "parsing ESP payload failed: invalid length");
181 goto failed;
182 }
183 if (!reader->read_data_end(reader, pad_length, &padding) ||
184 !check_padding(padding))
185 {
186 DBG1(DBG_ESP, "parsing ESP payload failed: invalid padding");
187 goto failed;
188 }
189 this->payload = ip_packet_create(reader->peek(reader));
190 reader->destroy(reader);
191 if (!this->payload)
192 {
193 DBG1(DBG_ESP, "parsing ESP payload failed: unsupported payload");
194 return FALSE;
195 }
196 this->next_header = next_header;
197 payload = this->payload->get_encoding(this->payload);
198
199 DBG3(DBG_ESP, "ESP payload:\n payload %B\n padding %B\n "
200 "padding length = %hhu, next header = %hhu", &payload, &padding,
201 pad_length, this->next_header);
202 return TRUE;
203
204 failed:
205 reader->destroy(reader);
206 chunk_free(&plaintext);
207 return FALSE;
208 }
209
210 METHOD(esp_packet_t, decrypt, status_t,
211 private_esp_packet_t *this, esp_context_t *esp_context)
212 {
213 bio_reader_t *reader;
214 uint32_t spi, seq;
215 chunk_t data, iv, icv, aad, ciphertext, plaintext;
216 aead_t *aead;
217
218 DESTROY_IF(this->payload);
219 this->payload = NULL;
220
221 data = this->packet->get_data(this->packet);
222 aead = esp_context->get_aead(esp_context);
223
224 reader = bio_reader_create(data);
225 if (!reader->read_uint32(reader, &spi) ||
226 !reader->read_uint32(reader, &seq) ||
227 !reader->read_data(reader, aead->get_iv_size(aead), &iv) ||
228 !reader->read_data_end(reader, aead->get_icv_size(aead), &icv) ||
229 reader->remaining(reader) % aead->get_block_size(aead))
230 {
231 DBG1(DBG_ESP, "ESP decryption failed: invalid length");
232 return PARSE_ERROR;
233 }
234 ciphertext = reader->peek(reader);
235 reader->destroy(reader);
236
237 if (!esp_context->verify_seqno(esp_context, seq))
238 {
239 DBG1(DBG_ESP, "ESP sequence number verification failed:\n "
240 "src %H, dst %H, SPI %.8x [seq %u]",
241 get_source(this), get_destination(this), spi, seq);
242 return VERIFY_ERROR;
243 }
244 DBG3(DBG_ESP, "ESP decryption:\n SPI %.8x [seq %u]\n IV %B\n "
245 "encrypted %B\n ICV %B", spi, seq, &iv, &ciphertext, &icv);
246
247 /* include ICV in ciphertext for decryption/verification */
248 ciphertext.len += icv.len;
249 /* aad = spi + seq */
250 aad = chunk_create(data.ptr, 8);
251
252 if (!aead->decrypt(aead, ciphertext, aad, iv, &plaintext))
253 {
254 DBG1(DBG_ESP, "ESP decryption or ICV verification failed");
255 return FAILED;
256 }
257 esp_context->set_authenticated_seqno(esp_context, seq);
258
259 if (!remove_padding(this, plaintext))
260 {
261 return PARSE_ERROR;
262 }
263 return SUCCESS;
264 }
265
266 /**
267 * Generate the padding as specified in RFC4303
268 */
269 static void generate_padding(chunk_t padding)
270 {
271 size_t i;
272
273 for (i = 0; i < padding.len; ++i)
274 {
275 padding.ptr[i] = (uint8_t)(i + 1);
276 }
277 }
278
279 METHOD(esp_packet_t, encrypt, status_t,
280 private_esp_packet_t *this, esp_context_t *esp_context, uint32_t spi)
281 {
282 chunk_t iv, icv, aad, padding, payload, ciphertext;
283 bio_writer_t *writer;
284 uint32_t next_seqno;
285 size_t blocksize, plainlen;
286 aead_t *aead;
287 iv_gen_t *iv_gen;
288
289 this->packet->set_data(this->packet, chunk_empty);
290
291 if (!esp_context->next_seqno(esp_context, &next_seqno))
292 {
293 DBG1(DBG_ESP, "ESP encapsulation failed: sequence numbers cycled");
294 return FAILED;
295 }
296
297 aead = esp_context->get_aead(esp_context);
298 iv_gen = aead->get_iv_gen(aead);
299 if (!iv_gen)
300 {
301 DBG1(DBG_ESP, "ESP encryption failed: no IV generator");
302 return NOT_FOUND;
303 }
304
305 blocksize = aead->get_block_size(aead);
306 iv.len = aead->get_iv_size(aead);
307 icv.len = aead->get_icv_size(aead);
308
309 /* plaintext = payload, padding, pad_length, next_header */
310 payload = this->payload ? this->payload->get_encoding(this->payload)
311 : chunk_empty;
312 plainlen = payload.len + 2;
313 padding.len = pad_len(plainlen, blocksize);
314 /* ICV must be on a 4-byte boundary */
315 padding.len += pad_len(iv.len + plainlen + padding.len, 4);
316 plainlen += padding.len;
317
318 /* len = spi, seq, IV, plaintext, ICV */
319 writer = bio_writer_create(2 * sizeof(uint32_t) + iv.len + plainlen +
320 icv.len);
321 writer->write_uint32(writer, ntohl(spi));
322 writer->write_uint32(writer, next_seqno);
323
324 iv = writer->skip(writer, iv.len);
325 if (!iv_gen->get_iv(iv_gen, next_seqno, iv.len, iv.ptr))
326 {
327 DBG1(DBG_ESP, "ESP encryption failed: could not generate IV");
328 writer->destroy(writer);
329 return FAILED;
330 }
331
332 /* plain-/ciphertext will start here */
333 ciphertext = writer->get_buf(writer);
334 ciphertext.ptr += ciphertext.len;
335 ciphertext.len = plainlen;
336
337 writer->write_data(writer, payload);
338
339 padding = writer->skip(writer, padding.len);
340 generate_padding(padding);
341
342 writer->write_uint8(writer, padding.len);
343 writer->write_uint8(writer, this->next_header);
344
345 /* aad = spi + seq */
346 aad = writer->get_buf(writer);
347 aad.len = 8;
348 icv = writer->skip(writer, icv.len);
349
350 DBG3(DBG_ESP, "ESP before encryption:\n payload = %B\n padding = %B\n "
351 "padding length = %hhu, next header = %hhu", &payload, &padding,
352 (uint8_t)padding.len, this->next_header);
353
354 /* encrypt/authenticate the content inline */
355 if (!aead->encrypt(aead, ciphertext, aad, iv, NULL))
356 {
357 DBG1(DBG_ESP, "ESP encryption or ICV generation failed");
358 writer->destroy(writer);
359 return FAILED;
360 }
361
362 DBG3(DBG_ESP, "ESP packet:\n SPI %.8x [seq %u]\n IV %B\n "
363 "encrypted %B\n ICV %B", ntohl(spi), next_seqno, &iv,
364 &ciphertext, &icv);
365
366 this->packet->set_data(this->packet, writer->extract_buf(writer));
367 writer->destroy(writer);
368 return SUCCESS;
369 }
370
371 METHOD(esp_packet_t, get_next_header, uint8_t,
372 private_esp_packet_t *this)
373 {
374 return this->next_header;
375 }
376
377 METHOD(esp_packet_t, get_payload, ip_packet_t*,
378 private_esp_packet_t *this)
379 {
380 return this->payload;
381 }
382
383 METHOD(esp_packet_t, extract_payload, ip_packet_t*,
384 private_esp_packet_t *this)
385 {
386 ip_packet_t *payload;
387
388 payload = this->payload;
389 this->payload = NULL;
390 return payload;
391 }
392
393 METHOD2(esp_packet_t, packet_t, destroy, void,
394 private_esp_packet_t *this)
395 {
396 DESTROY_IF(this->payload);
397 this->packet->destroy(this->packet);
398 free(this);
399 }
400
401 static private_esp_packet_t *esp_packet_create_internal(packet_t *packet)
402 {
403 private_esp_packet_t *this;
404
405 INIT(this,
406 .public = {
407 .packet = {
408 .set_source = _set_source,
409 .get_source = _get_source,
410 .set_destination = _set_destination,
411 .get_destination = _get_destination,
412 .get_data = _get_data,
413 .set_data = _set_data,
414 .get_dscp = _get_dscp,
415 .set_dscp = _set_dscp,
416 .skip_bytes = _skip_bytes,
417 .clone = _clone_,
418 .destroy = _destroy,
419 },
420 .get_source = _get_source,
421 .get_destination = _get_destination,
422 .get_next_header = _get_next_header,
423 .parse_header = _parse_header,
424 .decrypt = _decrypt,
425 .encrypt = _encrypt,
426 .get_payload = _get_payload,
427 .extract_payload = _extract_payload,
428 .destroy = _destroy,
429 },
430 .packet = packet,
431 .next_header = IPPROTO_NONE,
432 );
433 return this;
434 }
435
436 /**
437 * Described in header.
438 */
439 esp_packet_t *esp_packet_create_from_packet(packet_t *packet)
440 {
441 private_esp_packet_t *this;
442
443 this = esp_packet_create_internal(packet);
444
445 return &this->public;
446 }
447
448 /**
449 * Described in header.
450 */
451 esp_packet_t *esp_packet_create_from_payload(host_t *src, host_t *dst,
452 ip_packet_t *payload)
453 {
454 private_esp_packet_t *this;
455 packet_t *packet;
456
457 packet = packet_create_from_data(src, dst, chunk_empty);
458 this = esp_packet_create_internal(packet);
459 this->payload = payload;
460 if (payload)
461 {
462 this->next_header = payload->get_version(payload) == 4 ? IPPROTO_IPIP
463 : IPPROTO_IPV6;
464 }
465 else
466 {
467 this->next_header = IPPROTO_NONE;
468 }
469 return &this->public;
470 }