parser.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. /*
  2. * Copyright (C) 2005-2009 Martin Willi
  3. * Copyright (C) 2005 Jan Hutter
  4. * HSR 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. #include <stdlib.h>
  17. #include <string.h>
  18. #include "parser.h"
  19. #include <library.h>
  20. #include <daemon.h>
  21. #include <collections/linked_list.h>
  22. #include <encoding/payloads/encodings.h>
  23. #include <encoding/payloads/payload.h>
  24. #include <encoding/payloads/sa_payload.h>
  25. #include <encoding/payloads/proposal_substructure.h>
  26. #include <encoding/payloads/transform_substructure.h>
  27. #include <encoding/payloads/transform_attribute.h>
  28. #include <encoding/payloads/ke_payload.h>
  29. #include <encoding/payloads/nonce_payload.h>
  30. #include <encoding/payloads/id_payload.h>
  31. #include <encoding/payloads/notify_payload.h>
  32. #include <encoding/payloads/encrypted_payload.h>
  33. #include <encoding/payloads/auth_payload.h>
  34. #include <encoding/payloads/cert_payload.h>
  35. #include <encoding/payloads/certreq_payload.h>
  36. #include <encoding/payloads/ts_payload.h>
  37. #include <encoding/payloads/delete_payload.h>
  38. #include <encoding/payloads/vendor_id_payload.h>
  39. #include <encoding/payloads/cp_payload.h>
  40. #include <encoding/payloads/configuration_attribute.h>
  41. #include <encoding/payloads/eap_payload.h>
  42. #include <encoding/payloads/unknown_payload.h>
  43. typedef struct private_parser_t private_parser_t;
  44. /**
  45. * Private data stored in a context.
  46. *
  47. * Contains pointers and counters to store current state.
  48. */
  49. struct private_parser_t {
  50. /**
  51. * Public members, see parser_t.
  52. */
  53. parser_t public;
  54. /**
  55. * major IKE version
  56. */
  57. uint8_t major_version;
  58. /**
  59. * Current bit for reading in input data.
  60. */
  61. uint8_t bit_pos;
  62. /**
  63. * Current byte for reading in input data.
  64. */
  65. uint8_t *byte_pos;
  66. /**
  67. * Input data to parse.
  68. */
  69. uint8_t *input;
  70. /**
  71. * Roof of input, used for length-checking.
  72. */
  73. uint8_t *input_roof;
  74. /**
  75. * Set of encoding rules for this parsing session.
  76. */
  77. encoding_rule_t *rules;
  78. };
  79. /**
  80. * Log invalid length error
  81. */
  82. static bool short_input(private_parser_t *this, int number)
  83. {
  84. DBG1(DBG_ENC, " not enough input to parse rule %d %N",
  85. number, encoding_type_names, this->rules[number].type);
  86. return FALSE;
  87. }
  88. /**
  89. * Log unaligned rules
  90. */
  91. static bool bad_bitpos(private_parser_t *this, int number)
  92. {
  93. DBG1(DBG_ENC, " found rule %d %N on bitpos %d",
  94. number, encoding_type_names, this->rules[number].type, this->bit_pos);
  95. return FALSE;
  96. }
  97. /**
  98. * Parse a 4-Bit unsigned integer from the current parsing position.
  99. */
  100. static bool parse_uint4(private_parser_t *this, int rule_number,
  101. uint8_t *output_pos)
  102. {
  103. if (this->byte_pos + sizeof(uint8_t) > this->input_roof)
  104. {
  105. return short_input(this, rule_number);
  106. }
  107. switch (this->bit_pos)
  108. {
  109. case 0:
  110. if (output_pos)
  111. {
  112. *output_pos = *(this->byte_pos) >> 4;
  113. }
  114. this->bit_pos = 4;
  115. break;
  116. case 4:
  117. if (output_pos)
  118. {
  119. *output_pos = *(this->byte_pos) & 0x0F;
  120. }
  121. this->bit_pos = 0;
  122. this->byte_pos++;
  123. break;
  124. default:
  125. return bad_bitpos(this, rule_number);
  126. }
  127. if (output_pos)
  128. {
  129. DBG3(DBG_ENC, " => %hhu", *output_pos);
  130. }
  131. return TRUE;
  132. }
  133. /**
  134. * Parse a 8-Bit unsigned integer from the current parsing position.
  135. */
  136. static bool parse_uint8(private_parser_t *this, int rule_number,
  137. uint8_t *output_pos)
  138. {
  139. if (this->byte_pos + sizeof(uint8_t) > this->input_roof)
  140. {
  141. return short_input(this, rule_number);
  142. }
  143. if (this->bit_pos)
  144. {
  145. return bad_bitpos(this, rule_number);
  146. }
  147. if (output_pos)
  148. {
  149. *output_pos = *(this->byte_pos);
  150. DBG3(DBG_ENC, " => %hhu", *output_pos);
  151. }
  152. this->byte_pos++;
  153. return TRUE;
  154. }
  155. /**
  156. * Parse a 15-Bit unsigned integer from the current parsing position.
  157. */
  158. static bool parse_uint15(private_parser_t *this, int rule_number,
  159. uint16_t *output_pos)
  160. {
  161. if (this->byte_pos + sizeof(uint16_t) > this->input_roof)
  162. {
  163. return short_input(this, rule_number);
  164. }
  165. if (this->bit_pos != 1)
  166. {
  167. return bad_bitpos(this, rule_number);
  168. }
  169. if (output_pos)
  170. {
  171. memcpy(output_pos, this->byte_pos, sizeof(uint16_t));
  172. *output_pos = ntohs(*output_pos) & ~0x8000;
  173. DBG3(DBG_ENC, " => %hu", *output_pos);
  174. }
  175. this->byte_pos += sizeof(uint16_t);
  176. this->bit_pos = 0;
  177. return TRUE;
  178. }
  179. /**
  180. * Parse a 16-Bit unsigned integer from the current parsing position.
  181. */
  182. static bool parse_uint16(private_parser_t *this, int rule_number,
  183. uint16_t *output_pos)
  184. {
  185. if (this->byte_pos + sizeof(uint16_t) > this->input_roof)
  186. {
  187. return short_input(this, rule_number);
  188. }
  189. if (this->bit_pos)
  190. {
  191. return bad_bitpos(this, rule_number);
  192. }
  193. if (output_pos)
  194. {
  195. memcpy(output_pos, this->byte_pos, sizeof(uint16_t));
  196. *output_pos = ntohs(*output_pos);
  197. DBG3(DBG_ENC, " => %hu", *output_pos);
  198. }
  199. this->byte_pos += sizeof(uint16_t);
  200. return TRUE;
  201. }
  202. /**
  203. * Parse a 32-Bit unsigned integer from the current parsing position.
  204. */
  205. static bool parse_uint32(private_parser_t *this, int rule_number,
  206. uint32_t *output_pos)
  207. {
  208. if (this->byte_pos + sizeof(uint32_t) > this->input_roof)
  209. {
  210. return short_input(this, rule_number);
  211. }
  212. if (this->bit_pos)
  213. {
  214. return bad_bitpos(this, rule_number);
  215. }
  216. if (output_pos)
  217. {
  218. memcpy(output_pos, this->byte_pos, sizeof(uint32_t));
  219. *output_pos = ntohl(*output_pos);
  220. DBG3(DBG_ENC, " => %u", *output_pos);
  221. }
  222. this->byte_pos += sizeof(uint32_t);
  223. return TRUE;
  224. }
  225. /**
  226. * Parse a given amount of bytes and writes them to a specific location
  227. */
  228. static bool parse_bytes(private_parser_t *this, int rule_number,
  229. uint8_t *output_pos, int bytes)
  230. {
  231. if (this->byte_pos + bytes > this->input_roof)
  232. {
  233. return short_input(this, rule_number);
  234. }
  235. if (this->bit_pos)
  236. {
  237. return bad_bitpos(this, rule_number);
  238. }
  239. if (output_pos)
  240. {
  241. memcpy(output_pos, this->byte_pos, bytes);
  242. DBG3(DBG_ENC, " %b", output_pos, bytes);
  243. }
  244. this->byte_pos += bytes;
  245. return TRUE;
  246. }
  247. /**
  248. * Parse a single Bit from the current parsing position
  249. */
  250. static bool parse_bit(private_parser_t *this, int rule_number,
  251. bool *output_pos)
  252. {
  253. if (this->byte_pos + sizeof(uint8_t) > this->input_roof)
  254. {
  255. return short_input(this, rule_number);
  256. }
  257. if (output_pos)
  258. {
  259. uint8_t mask;
  260. mask = 0x01 << (7 - this->bit_pos);
  261. *output_pos = *this->byte_pos & mask;
  262. if (*output_pos)
  263. { /* set to a "clean", comparable true */
  264. *output_pos = TRUE;
  265. }
  266. DBG3(DBG_ENC, " => %d", *output_pos);
  267. }
  268. this->bit_pos = (this->bit_pos + 1) % 8;
  269. if (this->bit_pos == 0)
  270. {
  271. this->byte_pos++;
  272. }
  273. return TRUE;
  274. }
  275. /**
  276. * Parse substructures in a list.
  277. */
  278. static bool parse_list(private_parser_t *this, int rule_number,
  279. linked_list_t **output_pos, payload_type_t payload_type, int length)
  280. {
  281. linked_list_t *list = *output_pos;
  282. if (length < 0)
  283. {
  284. return short_input(this, rule_number);
  285. }
  286. if (this->bit_pos)
  287. {
  288. return bad_bitpos(this, rule_number);
  289. }
  290. while (length > 0)
  291. {
  292. uint8_t *pos_before = this->byte_pos;
  293. payload_t *payload;
  294. DBG2(DBG_ENC, " %d bytes left, parsing recursively %N",
  295. length, payload_type_names, payload_type);
  296. if (this->public.parse_payload(&this->public, payload_type,
  297. &payload) != SUCCESS)
  298. {
  299. DBG1(DBG_ENC, " parsing of a %N substructure failed",
  300. payload_type_names, payload_type);
  301. return FALSE;
  302. }
  303. list->insert_last(list, payload);
  304. length -= this->byte_pos - pos_before;
  305. }
  306. if (length != 0)
  307. { /* must yield exactly to zero */
  308. DBG1(DBG_ENC, " length of %N substructure list invalid",
  309. payload_type_names, payload_type);
  310. return FALSE;
  311. }
  312. *output_pos = list;
  313. return TRUE;
  314. }
  315. /**
  316. * Parse data from current parsing position in a chunk.
  317. */
  318. static bool parse_chunk(private_parser_t *this, int rule_number,
  319. chunk_t *output_pos, int length)
  320. {
  321. if (this->byte_pos + length > this->input_roof)
  322. {
  323. return short_input(this, rule_number);
  324. }
  325. if (this->bit_pos)
  326. {
  327. return bad_bitpos(this, rule_number);
  328. }
  329. if (output_pos)
  330. {
  331. *output_pos = chunk_alloc(length);
  332. memcpy(output_pos->ptr, this->byte_pos, length);
  333. DBG3(DBG_ENC, " %b", output_pos->ptr, length);
  334. }
  335. this->byte_pos += length;
  336. return TRUE;
  337. }
  338. METHOD(parser_t, parse_payload, status_t,
  339. private_parser_t *this, payload_type_t payload_type, payload_t **payload)
  340. {
  341. payload_t *pld;
  342. void *output;
  343. int payload_length = 0, spi_size = 0, attribute_length = 0, header_length;
  344. uint16_t ts_type = 0;
  345. bool attribute_format = FALSE;
  346. int rule_number, rule_count;
  347. encoding_rule_t *rule;
  348. /* create instance of the payload to parse */
  349. if (payload_is_known(payload_type, this->major_version))
  350. {
  351. pld = payload_create(payload_type);
  352. }
  353. else
  354. {
  355. pld = (payload_t*)unknown_payload_create(payload_type);
  356. }
  357. DBG2(DBG_ENC, "parsing %N payload, %d bytes left",
  358. payload_type_names, payload_type, this->input_roof - this->byte_pos);
  359. DBG3(DBG_ENC, "parsing payload from %b",
  360. this->byte_pos, (u_int)(this->input_roof - this->byte_pos));
  361. /* base pointer for output, avoids casting in every rule */
  362. output = pld;
  363. /* parse the payload with its own rulse */
  364. rule_count = pld->get_encoding_rules(pld, &this->rules);
  365. for (rule_number = 0; rule_number < rule_count; rule_number++)
  366. {
  367. /* update header length for each rule, as it is dynamic (SPIs) */
  368. header_length = pld->get_header_length(pld);
  369. rule = &(this->rules[rule_number]);
  370. DBG2(DBG_ENC, " parsing rule %d %N",
  371. rule_number, encoding_type_names, rule->type);
  372. switch ((int)rule->type)
  373. {
  374. case U_INT_4:
  375. {
  376. if (!parse_uint4(this, rule_number, output + rule->offset))
  377. {
  378. pld->destroy(pld);
  379. return PARSE_ERROR;
  380. }
  381. break;
  382. }
  383. case U_INT_8:
  384. case RESERVED_BYTE:
  385. {
  386. if (!parse_uint8(this, rule_number, output + rule->offset))
  387. {
  388. pld->destroy(pld);
  389. return PARSE_ERROR;
  390. }
  391. break;
  392. }
  393. case U_INT_16:
  394. {
  395. if (!parse_uint16(this, rule_number, output + rule->offset))
  396. {
  397. pld->destroy(pld);
  398. return PARSE_ERROR;
  399. }
  400. break;
  401. }
  402. case U_INT_32:
  403. case HEADER_LENGTH:
  404. {
  405. if (!parse_uint32(this, rule_number, output + rule->offset))
  406. {
  407. pld->destroy(pld);
  408. return PARSE_ERROR;
  409. }
  410. break;
  411. }
  412. case IKE_SPI:
  413. {
  414. if (!parse_bytes(this, rule_number, output + rule->offset, 8))
  415. {
  416. pld->destroy(pld);
  417. return PARSE_ERROR;
  418. }
  419. break;
  420. }
  421. case RESERVED_BIT:
  422. case FLAG:
  423. {
  424. if (!parse_bit(this, rule_number, output + rule->offset))
  425. {
  426. pld->destroy(pld);
  427. return PARSE_ERROR;
  428. }
  429. break;
  430. }
  431. case PAYLOAD_LENGTH:
  432. {
  433. if (!parse_uint16(this, rule_number, output + rule->offset))
  434. {
  435. pld->destroy(pld);
  436. return PARSE_ERROR;
  437. }
  438. /* parsed u_int16 should be aligned */
  439. payload_length = *(uint16_t*)(output + rule->offset);
  440. /* all payloads must have at least 4 bytes header */
  441. if (payload_length < 4)
  442. {
  443. pld->destroy(pld);
  444. return PARSE_ERROR;
  445. }
  446. break;
  447. }
  448. case SPI_SIZE:
  449. {
  450. if (!parse_uint8(this, rule_number, output + rule->offset))
  451. {
  452. pld->destroy(pld);
  453. return PARSE_ERROR;
  454. }
  455. spi_size = *(uint8_t*)(output + rule->offset);
  456. break;
  457. }
  458. case SPI:
  459. {
  460. if (!parse_chunk(this, rule_number, output + rule->offset,
  461. spi_size))
  462. {
  463. pld->destroy(pld);
  464. return PARSE_ERROR;
  465. }
  466. break;
  467. }
  468. case PAYLOAD_LIST + PLV2_PROPOSAL_SUBSTRUCTURE:
  469. case PAYLOAD_LIST + PLV1_PROPOSAL_SUBSTRUCTURE:
  470. case PAYLOAD_LIST + PLV2_TRANSFORM_SUBSTRUCTURE:
  471. case PAYLOAD_LIST + PLV1_TRANSFORM_SUBSTRUCTURE:
  472. case PAYLOAD_LIST + PLV2_TRANSFORM_ATTRIBUTE:
  473. case PAYLOAD_LIST + PLV1_TRANSFORM_ATTRIBUTE:
  474. case PAYLOAD_LIST + PLV2_CONFIGURATION_ATTRIBUTE:
  475. case PAYLOAD_LIST + PLV1_CONFIGURATION_ATTRIBUTE:
  476. case PAYLOAD_LIST + PLV2_TRAFFIC_SELECTOR_SUBSTRUCTURE:
  477. {
  478. if (payload_length < header_length ||
  479. !parse_list(this, rule_number, output + rule->offset,
  480. rule->type - PAYLOAD_LIST,
  481. payload_length - header_length))
  482. {
  483. pld->destroy(pld);
  484. return PARSE_ERROR;
  485. }
  486. break;
  487. }
  488. case CHUNK_DATA:
  489. {
  490. if (payload_length < header_length ||
  491. !parse_chunk(this, rule_number, output + rule->offset,
  492. payload_length - header_length))
  493. {
  494. pld->destroy(pld);
  495. return PARSE_ERROR;
  496. }
  497. break;
  498. }
  499. case ENCRYPTED_DATA:
  500. {
  501. if (!parse_chunk(this, rule_number, output + rule->offset,
  502. this->input_roof - this->byte_pos))
  503. {
  504. pld->destroy(pld);
  505. return PARSE_ERROR;
  506. }
  507. break;
  508. }
  509. case ATTRIBUTE_FORMAT:
  510. {
  511. if (!parse_bit(this, rule_number, output + rule->offset))
  512. {
  513. pld->destroy(pld);
  514. return PARSE_ERROR;
  515. }
  516. attribute_format = *(bool*)(output + rule->offset);
  517. break;
  518. }
  519. case ATTRIBUTE_TYPE:
  520. {
  521. if (!parse_uint15(this, rule_number, output + rule->offset))
  522. {
  523. pld->destroy(pld);
  524. return PARSE_ERROR;
  525. }
  526. break;
  527. }
  528. case ATTRIBUTE_LENGTH:
  529. {
  530. if (!parse_uint16(this, rule_number, output + rule->offset))
  531. {
  532. pld->destroy(pld);
  533. return PARSE_ERROR;
  534. }
  535. attribute_length = *(uint16_t*)(output + rule->offset);
  536. break;
  537. }
  538. case ATTRIBUTE_LENGTH_OR_VALUE:
  539. {
  540. if (!parse_uint16(this, rule_number, output + rule->offset))
  541. {
  542. pld->destroy(pld);
  543. return PARSE_ERROR;
  544. }
  545. attribute_length = *(uint16_t*)(output + rule->offset);
  546. break;
  547. }
  548. case ATTRIBUTE_VALUE:
  549. {
  550. if (attribute_format == FALSE &&
  551. !parse_chunk(this, rule_number, output + rule->offset,
  552. attribute_length))
  553. {
  554. pld->destroy(pld);
  555. return PARSE_ERROR;
  556. }
  557. break;
  558. }
  559. case TS_TYPE:
  560. {
  561. if (!parse_uint8(this, rule_number, output + rule->offset))
  562. {
  563. pld->destroy(pld);
  564. return PARSE_ERROR;
  565. }
  566. ts_type = *(uint8_t*)(output + rule->offset);
  567. break;
  568. }
  569. case ADDRESS:
  570. {
  571. int address_length = (ts_type == TS_IPV4_ADDR_RANGE) ? 4 : 16;
  572. if (!parse_chunk(this, rule_number, output + rule->offset,
  573. address_length))
  574. {
  575. pld->destroy(pld);
  576. return PARSE_ERROR;
  577. }
  578. break;
  579. }
  580. default:
  581. {
  582. DBG1(DBG_ENC, " no rule to parse rule %d %N",
  583. rule_number, encoding_type_names, rule->type);
  584. pld->destroy(pld);
  585. return PARSE_ERROR;
  586. }
  587. }
  588. /* process next rulue */
  589. rule++;
  590. }
  591. *payload = pld;
  592. DBG2(DBG_ENC, "parsing %N payload finished",
  593. payload_type_names, payload_type);
  594. return SUCCESS;
  595. }
  596. METHOD(parser_t, get_remaining_byte_count, int,
  597. private_parser_t *this)
  598. {
  599. return this->input_roof - this->byte_pos;
  600. }
  601. METHOD(parser_t, reset_context, void,
  602. private_parser_t *this)
  603. {
  604. this->byte_pos = this->input;
  605. this->bit_pos = 0;
  606. }
  607. METHOD(parser_t, set_major_version, void,
  608. private_parser_t *this, uint8_t major_version)
  609. {
  610. this->major_version = major_version;
  611. }
  612. METHOD(parser_t, destroy, void,
  613. private_parser_t *this)
  614. {
  615. free(this);
  616. }
  617. /*
  618. * Described in header.
  619. */
  620. parser_t *parser_create(chunk_t data)
  621. {
  622. private_parser_t *this;
  623. INIT(this,
  624. .public = {
  625. .parse_payload = _parse_payload,
  626. .reset_context = _reset_context,
  627. .set_major_version = _set_major_version,
  628. .get_remaining_byte_count = _get_remaining_byte_count,
  629. .destroy = _destroy,
  630. },
  631. .input = data.ptr,
  632. .byte_pos = data.ptr,
  633. .input_roof = data.ptr + data.len,
  634. );
  635. return &this->public;
  636. }