123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678 |
- /*
- * Copyright (C) 2005-2009 Martin Willi
- * Copyright (C) 2005 Jan Hutter
- * HSR Hochschule fuer Technik Rapperswil
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the
- * Free Software Foundation; either version 2 of the License, or (at your
- * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
- * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
- * for more details.
- */
- #include <stdlib.h>
- #include <string.h>
- #include "parser.h"
- #include <library.h>
- #include <daemon.h>
- #include <collections/linked_list.h>
- #include <encoding/payloads/encodings.h>
- #include <encoding/payloads/payload.h>
- #include <encoding/payloads/sa_payload.h>
- #include <encoding/payloads/proposal_substructure.h>
- #include <encoding/payloads/transform_substructure.h>
- #include <encoding/payloads/transform_attribute.h>
- #include <encoding/payloads/ke_payload.h>
- #include <encoding/payloads/nonce_payload.h>
- #include <encoding/payloads/id_payload.h>
- #include <encoding/payloads/notify_payload.h>
- #include <encoding/payloads/encrypted_payload.h>
- #include <encoding/payloads/auth_payload.h>
- #include <encoding/payloads/cert_payload.h>
- #include <encoding/payloads/certreq_payload.h>
- #include <encoding/payloads/ts_payload.h>
- #include <encoding/payloads/delete_payload.h>
- #include <encoding/payloads/vendor_id_payload.h>
- #include <encoding/payloads/cp_payload.h>
- #include <encoding/payloads/configuration_attribute.h>
- #include <encoding/payloads/eap_payload.h>
- #include <encoding/payloads/unknown_payload.h>
- typedef struct private_parser_t private_parser_t;
- /**
- * Private data stored in a context.
- *
- * Contains pointers and counters to store current state.
- */
- struct private_parser_t {
- /**
- * Public members, see parser_t.
- */
- parser_t public;
- /**
- * major IKE version
- */
- uint8_t major_version;
- /**
- * Current bit for reading in input data.
- */
- uint8_t bit_pos;
- /**
- * Current byte for reading in input data.
- */
- uint8_t *byte_pos;
- /**
- * Input data to parse.
- */
- uint8_t *input;
- /**
- * Roof of input, used for length-checking.
- */
- uint8_t *input_roof;
- /**
- * Set of encoding rules for this parsing session.
- */
- encoding_rule_t *rules;
- };
- /**
- * Log invalid length error
- */
- static bool short_input(private_parser_t *this, int number)
- {
- DBG1(DBG_ENC, " not enough input to parse rule %d %N",
- number, encoding_type_names, this->rules[number].type);
- return FALSE;
- }
- /**
- * Log unaligned rules
- */
- static bool bad_bitpos(private_parser_t *this, int number)
- {
- DBG1(DBG_ENC, " found rule %d %N on bitpos %d",
- number, encoding_type_names, this->rules[number].type, this->bit_pos);
- return FALSE;
- }
- /**
- * Parse a 4-Bit unsigned integer from the current parsing position.
- */
- static bool parse_uint4(private_parser_t *this, int rule_number,
- uint8_t *output_pos)
- {
- if (this->byte_pos + sizeof(uint8_t) > this->input_roof)
- {
- return short_input(this, rule_number);
- }
- switch (this->bit_pos)
- {
- case 0:
- if (output_pos)
- {
- *output_pos = *(this->byte_pos) >> 4;
- }
- this->bit_pos = 4;
- break;
- case 4:
- if (output_pos)
- {
- *output_pos = *(this->byte_pos) & 0x0F;
- }
- this->bit_pos = 0;
- this->byte_pos++;
- break;
- default:
- return bad_bitpos(this, rule_number);
- }
- if (output_pos)
- {
- DBG3(DBG_ENC, " => %hhu", *output_pos);
- }
- return TRUE;
- }
- /**
- * Parse a 8-Bit unsigned integer from the current parsing position.
- */
- static bool parse_uint8(private_parser_t *this, int rule_number,
- uint8_t *output_pos)
- {
- if (this->byte_pos + sizeof(uint8_t) > this->input_roof)
- {
- return short_input(this, rule_number);
- }
- if (this->bit_pos)
- {
- return bad_bitpos(this, rule_number);
- }
- if (output_pos)
- {
- *output_pos = *(this->byte_pos);
- DBG3(DBG_ENC, " => %hhu", *output_pos);
- }
- this->byte_pos++;
- return TRUE;
- }
- /**
- * Parse a 15-Bit unsigned integer from the current parsing position.
- */
- static bool parse_uint15(private_parser_t *this, int rule_number,
- uint16_t *output_pos)
- {
- if (this->byte_pos + sizeof(uint16_t) > this->input_roof)
- {
- return short_input(this, rule_number);
- }
- if (this->bit_pos != 1)
- {
- return bad_bitpos(this, rule_number);
- }
- if (output_pos)
- {
- memcpy(output_pos, this->byte_pos, sizeof(uint16_t));
- *output_pos = ntohs(*output_pos) & ~0x8000;
- DBG3(DBG_ENC, " => %hu", *output_pos);
- }
- this->byte_pos += sizeof(uint16_t);
- this->bit_pos = 0;
- return TRUE;
- }
- /**
- * Parse a 16-Bit unsigned integer from the current parsing position.
- */
- static bool parse_uint16(private_parser_t *this, int rule_number,
- uint16_t *output_pos)
- {
- if (this->byte_pos + sizeof(uint16_t) > this->input_roof)
- {
- return short_input(this, rule_number);
- }
- if (this->bit_pos)
- {
- return bad_bitpos(this, rule_number);
- }
- if (output_pos)
- {
- memcpy(output_pos, this->byte_pos, sizeof(uint16_t));
- *output_pos = ntohs(*output_pos);
- DBG3(DBG_ENC, " => %hu", *output_pos);
- }
- this->byte_pos += sizeof(uint16_t);
- return TRUE;
- }
- /**
- * Parse a 32-Bit unsigned integer from the current parsing position.
- */
- static bool parse_uint32(private_parser_t *this, int rule_number,
- uint32_t *output_pos)
- {
- if (this->byte_pos + sizeof(uint32_t) > this->input_roof)
- {
- return short_input(this, rule_number);
- }
- if (this->bit_pos)
- {
- return bad_bitpos(this, rule_number);
- }
- if (output_pos)
- {
- memcpy(output_pos, this->byte_pos, sizeof(uint32_t));
- *output_pos = ntohl(*output_pos);
- DBG3(DBG_ENC, " => %u", *output_pos);
- }
- this->byte_pos += sizeof(uint32_t);
- return TRUE;
- }
- /**
- * Parse a given amount of bytes and writes them to a specific location
- */
- static bool parse_bytes(private_parser_t *this, int rule_number,
- uint8_t *output_pos, int bytes)
- {
- if (this->byte_pos + bytes > this->input_roof)
- {
- return short_input(this, rule_number);
- }
- if (this->bit_pos)
- {
- return bad_bitpos(this, rule_number);
- }
- if (output_pos)
- {
- memcpy(output_pos, this->byte_pos, bytes);
- DBG3(DBG_ENC, " %b", output_pos, bytes);
- }
- this->byte_pos += bytes;
- return TRUE;
- }
- /**
- * Parse a single Bit from the current parsing position
- */
- static bool parse_bit(private_parser_t *this, int rule_number,
- bool *output_pos)
- {
- if (this->byte_pos + sizeof(uint8_t) > this->input_roof)
- {
- return short_input(this, rule_number);
- }
- if (output_pos)
- {
- uint8_t mask;
- mask = 0x01 << (7 - this->bit_pos);
- *output_pos = *this->byte_pos & mask;
- if (*output_pos)
- { /* set to a "clean", comparable true */
- *output_pos = TRUE;
- }
- DBG3(DBG_ENC, " => %d", *output_pos);
- }
- this->bit_pos = (this->bit_pos + 1) % 8;
- if (this->bit_pos == 0)
- {
- this->byte_pos++;
- }
- return TRUE;
- }
- /**
- * Parse substructures in a list.
- */
- static bool parse_list(private_parser_t *this, int rule_number,
- linked_list_t **output_pos, payload_type_t payload_type, int length)
- {
- linked_list_t *list = *output_pos;
- if (length < 0)
- {
- return short_input(this, rule_number);
- }
- if (this->bit_pos)
- {
- return bad_bitpos(this, rule_number);
- }
- while (length > 0)
- {
- uint8_t *pos_before = this->byte_pos;
- payload_t *payload;
- DBG2(DBG_ENC, " %d bytes left, parsing recursively %N",
- length, payload_type_names, payload_type);
- if (this->public.parse_payload(&this->public, payload_type,
- &payload) != SUCCESS)
- {
- DBG1(DBG_ENC, " parsing of a %N substructure failed",
- payload_type_names, payload_type);
- return FALSE;
- }
- list->insert_last(list, payload);
- length -= this->byte_pos - pos_before;
- }
- if (length != 0)
- { /* must yield exactly to zero */
- DBG1(DBG_ENC, " length of %N substructure list invalid",
- payload_type_names, payload_type);
- return FALSE;
- }
- *output_pos = list;
- return TRUE;
- }
- /**
- * Parse data from current parsing position in a chunk.
- */
- static bool parse_chunk(private_parser_t *this, int rule_number,
- chunk_t *output_pos, int length)
- {
- if (this->byte_pos + length > this->input_roof)
- {
- return short_input(this, rule_number);
- }
- if (this->bit_pos)
- {
- return bad_bitpos(this, rule_number);
- }
- if (output_pos)
- {
- *output_pos = chunk_alloc(length);
- memcpy(output_pos->ptr, this->byte_pos, length);
- DBG3(DBG_ENC, " %b", output_pos->ptr, length);
- }
- this->byte_pos += length;
- return TRUE;
- }
- METHOD(parser_t, parse_payload, status_t,
- private_parser_t *this, payload_type_t payload_type, payload_t **payload)
- {
- payload_t *pld;
- void *output;
- int payload_length = 0, spi_size = 0, attribute_length = 0, header_length;
- uint16_t ts_type = 0;
- bool attribute_format = FALSE;
- int rule_number, rule_count;
- encoding_rule_t *rule;
- /* create instance of the payload to parse */
- if (payload_is_known(payload_type, this->major_version))
- {
- pld = payload_create(payload_type);
- }
- else
- {
- pld = (payload_t*)unknown_payload_create(payload_type);
- }
- DBG2(DBG_ENC, "parsing %N payload, %d bytes left",
- payload_type_names, payload_type, this->input_roof - this->byte_pos);
- DBG3(DBG_ENC, "parsing payload from %b",
- this->byte_pos, (u_int)(this->input_roof - this->byte_pos));
- /* base pointer for output, avoids casting in every rule */
- output = pld;
- /* parse the payload with its own rulse */
- rule_count = pld->get_encoding_rules(pld, &this->rules);
- for (rule_number = 0; rule_number < rule_count; rule_number++)
- {
- /* update header length for each rule, as it is dynamic (SPIs) */
- header_length = pld->get_header_length(pld);
- rule = &(this->rules[rule_number]);
- DBG2(DBG_ENC, " parsing rule %d %N",
- rule_number, encoding_type_names, rule->type);
- switch ((int)rule->type)
- {
- case U_INT_4:
- {
- if (!parse_uint4(this, rule_number, output + rule->offset))
- {
- pld->destroy(pld);
- return PARSE_ERROR;
- }
- break;
- }
- case U_INT_8:
- case RESERVED_BYTE:
- {
- if (!parse_uint8(this, rule_number, output + rule->offset))
- {
- pld->destroy(pld);
- return PARSE_ERROR;
- }
- break;
- }
- case U_INT_16:
- {
- if (!parse_uint16(this, rule_number, output + rule->offset))
- {
- pld->destroy(pld);
- return PARSE_ERROR;
- }
- break;
- }
- case U_INT_32:
- case HEADER_LENGTH:
- {
- if (!parse_uint32(this, rule_number, output + rule->offset))
- {
- pld->destroy(pld);
- return PARSE_ERROR;
- }
- break;
- }
- case IKE_SPI:
- {
- if (!parse_bytes(this, rule_number, output + rule->offset, 8))
- {
- pld->destroy(pld);
- return PARSE_ERROR;
- }
- break;
- }
- case RESERVED_BIT:
- case FLAG:
- {
- if (!parse_bit(this, rule_number, output + rule->offset))
- {
- pld->destroy(pld);
- return PARSE_ERROR;
- }
- break;
- }
- case PAYLOAD_LENGTH:
- {
- if (!parse_uint16(this, rule_number, output + rule->offset))
- {
- pld->destroy(pld);
- return PARSE_ERROR;
- }
- /* parsed u_int16 should be aligned */
- payload_length = *(uint16_t*)(output + rule->offset);
- /* all payloads must have at least 4 bytes header */
- if (payload_length < 4)
- {
- pld->destroy(pld);
- return PARSE_ERROR;
- }
- break;
- }
- case SPI_SIZE:
- {
- if (!parse_uint8(this, rule_number, output + rule->offset))
- {
- pld->destroy(pld);
- return PARSE_ERROR;
- }
- spi_size = *(uint8_t*)(output + rule->offset);
- break;
- }
- case SPI:
- {
- if (!parse_chunk(this, rule_number, output + rule->offset,
- spi_size))
- {
- pld->destroy(pld);
- return PARSE_ERROR;
- }
- break;
- }
- case PAYLOAD_LIST + PLV2_PROPOSAL_SUBSTRUCTURE:
- case PAYLOAD_LIST + PLV1_PROPOSAL_SUBSTRUCTURE:
- case PAYLOAD_LIST + PLV2_TRANSFORM_SUBSTRUCTURE:
- case PAYLOAD_LIST + PLV1_TRANSFORM_SUBSTRUCTURE:
- case PAYLOAD_LIST + PLV2_TRANSFORM_ATTRIBUTE:
- case PAYLOAD_LIST + PLV1_TRANSFORM_ATTRIBUTE:
- case PAYLOAD_LIST + PLV2_CONFIGURATION_ATTRIBUTE:
- case PAYLOAD_LIST + PLV1_CONFIGURATION_ATTRIBUTE:
- case PAYLOAD_LIST + PLV2_TRAFFIC_SELECTOR_SUBSTRUCTURE:
- {
- if (payload_length < header_length ||
- !parse_list(this, rule_number, output + rule->offset,
- rule->type - PAYLOAD_LIST,
- payload_length - header_length))
- {
- pld->destroy(pld);
- return PARSE_ERROR;
- }
- break;
- }
- case CHUNK_DATA:
- {
- if (payload_length < header_length ||
- !parse_chunk(this, rule_number, output + rule->offset,
- payload_length - header_length))
- {
- pld->destroy(pld);
- return PARSE_ERROR;
- }
- break;
- }
- case ENCRYPTED_DATA:
- {
- if (!parse_chunk(this, rule_number, output + rule->offset,
- this->input_roof - this->byte_pos))
- {
- pld->destroy(pld);
- return PARSE_ERROR;
- }
- break;
- }
- case ATTRIBUTE_FORMAT:
- {
- if (!parse_bit(this, rule_number, output + rule->offset))
- {
- pld->destroy(pld);
- return PARSE_ERROR;
- }
- attribute_format = *(bool*)(output + rule->offset);
- break;
- }
- case ATTRIBUTE_TYPE:
- {
- if (!parse_uint15(this, rule_number, output + rule->offset))
- {
- pld->destroy(pld);
- return PARSE_ERROR;
- }
- break;
- }
- case ATTRIBUTE_LENGTH:
- {
- if (!parse_uint16(this, rule_number, output + rule->offset))
- {
- pld->destroy(pld);
- return PARSE_ERROR;
- }
- attribute_length = *(uint16_t*)(output + rule->offset);
- break;
- }
- case ATTRIBUTE_LENGTH_OR_VALUE:
- {
- if (!parse_uint16(this, rule_number, output + rule->offset))
- {
- pld->destroy(pld);
- return PARSE_ERROR;
- }
- attribute_length = *(uint16_t*)(output + rule->offset);
- break;
- }
- case ATTRIBUTE_VALUE:
- {
- if (attribute_format == FALSE &&
- !parse_chunk(this, rule_number, output + rule->offset,
- attribute_length))
- {
- pld->destroy(pld);
- return PARSE_ERROR;
- }
- break;
- }
- case TS_TYPE:
- {
- if (!parse_uint8(this, rule_number, output + rule->offset))
- {
- pld->destroy(pld);
- return PARSE_ERROR;
- }
- ts_type = *(uint8_t*)(output + rule->offset);
- break;
- }
- case ADDRESS:
- {
- int address_length = (ts_type == TS_IPV4_ADDR_RANGE) ? 4 : 16;
- if (!parse_chunk(this, rule_number, output + rule->offset,
- address_length))
- {
- pld->destroy(pld);
- return PARSE_ERROR;
- }
- break;
- }
- default:
- {
- DBG1(DBG_ENC, " no rule to parse rule %d %N",
- rule_number, encoding_type_names, rule->type);
- pld->destroy(pld);
- return PARSE_ERROR;
- }
- }
- /* process next rulue */
- rule++;
- }
- *payload = pld;
- DBG2(DBG_ENC, "parsing %N payload finished",
- payload_type_names, payload_type);
- return SUCCESS;
- }
- METHOD(parser_t, get_remaining_byte_count, int,
- private_parser_t *this)
- {
- return this->input_roof - this->byte_pos;
- }
- METHOD(parser_t, reset_context, void,
- private_parser_t *this)
- {
- this->byte_pos = this->input;
- this->bit_pos = 0;
- }
- METHOD(parser_t, set_major_version, void,
- private_parser_t *this, uint8_t major_version)
- {
- this->major_version = major_version;
- }
- METHOD(parser_t, destroy, void,
- private_parser_t *this)
- {
- free(this);
- }
- /*
- * Described in header.
- */
- parser_t *parser_create(chunk_t data)
- {
- private_parser_t *this;
- INIT(this,
- .public = {
- .parse_payload = _parse_payload,
- .reset_context = _reset_context,
- .set_major_version = _set_major_version,
- .get_remaining_byte_count = _get_remaining_byte_count,
- .destroy = _destroy,
- },
- .input = data.ptr,
- .byte_pos = data.ptr,
- .input_roof = data.ptr + data.len,
- );
- return &this->public;
- }
|