esp_packet.c 11 KB

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