4 * @brief Generic parser class used to parse IKEv2-Header and Payload
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 #include <arpa/inet.h>
29 #include <definitions.h>
31 #include <utils/allocator.h>
32 #include <utils/logger.h>
33 #include <utils/linked_list.h>
34 #include <encoding/payloads/encodings.h>
35 #include <encoding/payloads/payload.h>
36 #include <encoding/payloads/sa_payload.h>
37 #include <encoding/payloads/proposal_substructure.h>
38 #include <encoding/payloads/transform_substructure.h>
39 #include <encoding/payloads/transform_attribute.h>
40 #include <encoding/payloads/ke_payload.h>
41 #include <encoding/payloads/nonce_payload.h>
42 #include <encoding/payloads/notify_payload.h>
47 * @private data stored in a context
49 * contains pointers and counters to store current state
51 typedef struct private_parser_s private_parser_t
;
53 struct private_parser_s
{
55 * Public members, see parser_t
60 * @brief parse a 4-Bit unsigned integer from the current parsing position.
62 * @param this parser object
63 * @param rule_number number of current rule
64 * @param[out] output_pos pointer where to write the parsed result
67 * - PARSE_ERROR when not successful
69 status_t (*parse_uint4
) (private_parser_t
*this, int rule_number
, u_int8_t
*output_pos
);
72 * @brief parse a 8-Bit unsigned integer from the current parsing position.
74 * @param this parser object
75 * @param rule_number number of current rule
76 * @param[out] output_pos pointer where to write the parsed result
79 * - PARSE_ERROR when not successful
81 status_t (*parse_uint8
) (private_parser_t
*this, int rule_number
, u_int8_t
*output_pos
);
84 * @brief parse a 15-Bit unsigned integer from the current parsing position.
86 * This is a special case used for ATTRIBUTE_TYPE.
87 * Big-/Little-endian conversion is done here.
89 * @param this parser object
90 * @param rule_number number of current rule
91 * @param[out] output_pos pointer where to write the parsed result
94 * - PARSE_ERROR when not successful
96 status_t (*parse_uint15
) (private_parser_t
*this, int rule_number
, u_int16_t
*output_pos
);
99 * @brief parse a 16-Bit unsigned integer from the current parsing position.
101 * Big-/Little-endian conversion is done here.
103 * @param this parser object
104 * @param rule_number number of current rule
105 * @param[out] output_pos pointer where to write the parsed result
108 * - PARSE_ERROR when not successful
110 status_t (*parse_uint16
) (private_parser_t
*this, int rule_number
, u_int16_t
*output_pos
);
113 * @brief parse a 32-Bit unsigned integer from the current parsing position.
115 * Big-/Little-endian conversion is done here.
117 * @param this parser object
118 * @param rule_number number of current rule
119 * @param[out] output_pos pointer where to write the parsed result
122 * - PARSE_ERROR when not successful
124 status_t (*parse_uint32
) (private_parser_t
*this, int rule_number
, u_int32_t
*output_pos
);
127 * @brief parse a 64-Bit unsigned integer from the current parsing position.
129 * @todo add support for big-endian machines.
131 * @param this parser object
132 * @param rule_number number of current rule
133 * @param[out] output_pos pointer where to write the parsed result
136 * - PARSE_ERROR when not successful
138 status_t (*parse_uint64
) (private_parser_t
*this, int rule_number
, u_int64_t
*output_pos
);
141 * @brief parse a given amount of bytes and writes them to a specific location
143 * @param this parser object
144 * @param rule_number number of current rule
145 * @param[out] output_pos pointer where to write the parsed result
146 * @param bytes number of bytes to parse
149 * - PARSE_ERROR when not successful
151 status_t (*parse_bytes
) (private_parser_t
*this, int rule_number
, u_int8_t
*output_pos
,size_t bytes
);
154 * @brief parse a single Bit from the current parsing position
156 * @param this parser object
157 * @param rule_number number of current rule
158 * @param[out] output_pos pointer where to write the parsed result
161 * - PARSE_ERROR when not successful
163 status_t (*parse_bit
) (private_parser_t
*this, int rule_number
, bool *output_pos
);
166 * @brief parse substructures in a list
168 * This function calls the parser recursivly to parse contained substructures
169 * in a linked_list_t. The list must already be created. Payload defines
170 * the type of the substructures. parsing is continued until the specified length
171 * is completely parsed.
173 * @param this parser object
174 * @param rule_number number of current rule
175 * @param[out] output_pos pointer of a linked_list where substructures are added
176 * @param payload_type type of the contained substructures to parse
177 * @param length number of bytes to parse in this list
180 * - PARSE_ERROR when not successful
182 status_t (*parse_list
) (private_parser_t
*this, int rule_number
, linked_list_t
**output_pos
, payload_type_t payload_ype
, size_t length
);
185 * @brief parse data from current parsing position in a chunk.
187 * This function clones length number of bytes to output_pos, without
188 * modifiyng them. Space will be allocated and must be freed by caller.
190 * @param this parser object
191 * @param rule_number number of current rule
192 * @param[out] output_pos pointer of a chunk which will point to the allocated data
193 * @param length number of bytes to clone
196 * - PARSE_ERROR when not successful
198 status_t (*parse_chunk
) (private_parser_t
*this, int rule_number
, chunk_t
*output_pos
, size_t length
);
201 * Current bit for reading in input data
206 * Current byte for reading in input data
211 * input data to parse
216 * roof of input, used for length-checking
218 u_int8_t
*input_roof
;
221 * set of encoding rules for this parsing session
223 encoding_rule_t
*rules
;
232 * implementation of private_parser_t.parse_uint4
234 static status_t
parse_uint4(private_parser_t
*this, int rule_number
, u_int8_t
*output_pos
)
236 if (this->byte_pos
+ sizeof(u_int8_t
) > this->input_roof
)
238 this->logger
->log(this->logger
, ERROR
, " not enough input to parse rule %d %s",
239 rule_number
, mapping_find(encoding_type_m
,
240 this->rules
[rule_number
].type
));
243 switch (this->bit_pos
)
246 /* caller interested in result ? */
247 if (output_pos
!= NULL
)
249 *output_pos
= *(this->byte_pos
) >> 4;
254 /* caller interested in result ? */
255 if (output_pos
!= NULL
)
257 *output_pos
= *(this->byte_pos
) & 0x0F;
263 this->logger
->log(this->logger
, ERROR
, " found rule %d %s on bitpos %d",
264 rule_number
, mapping_find(encoding_type_m
,
265 this->rules
[rule_number
].type
), this->bit_pos
);
269 if (output_pos
!= NULL
)
271 this->logger
->log(this->logger
, RAW
|MOST
, " => %d", *output_pos
);
279 * implementation of private_parser_t.parse_uint8
281 static status_t
parse_uint8(private_parser_t
*this, int rule_number
, u_int8_t
*output_pos
)
283 if (this->byte_pos
+ sizeof(u_int8_t
) > this->input_roof
)
285 this->logger
->log(this->logger
, ERROR
, " not enough input to parse rule %d %s",
286 rule_number
, mapping_find(encoding_type_m
,
287 this->rules
[rule_number
].type
));
292 this->logger
->log(this->logger
, ERROR
, " found rule %d %s on bitpos %d",
293 rule_number
, mapping_find(encoding_type_m
,
294 this->rules
[rule_number
].type
), this->bit_pos
);
298 /* caller interested in result ? */
299 if (output_pos
!= NULL
)
301 *output_pos
= *(this->byte_pos
);
302 this->logger
->log(this->logger
, RAW
|MOST
, " => %d", *output_pos
);
312 * implementation of private_parser_t.parse_uint15
314 static status_t
parse_uint15(private_parser_t
*this, int rule_number
, u_int16_t
*output_pos
)
316 if (this->byte_pos
+ sizeof(u_int16_t
) > this->input_roof
)
318 this->logger
->log(this->logger
, ERROR
, " not enough input to parse rule %d %s",
319 rule_number
, mapping_find(encoding_type_m
,
320 this->rules
[rule_number
].type
));
323 if (this->bit_pos
!= 1)
325 this->logger
->log(this->logger
, ERROR
, " found rule %d %s on bitpos %d",
326 rule_number
, mapping_find(encoding_type_m
, this->rules
[rule_number
].type
),
330 /* caller interested in result ? */
331 if (output_pos
!= NULL
)
333 *output_pos
= ntohs(*((u_int16_t
*)this->byte_pos
)) & ~0x8000;
334 this->logger
->log(this->logger
, RAW
|MOST
, " => %d", *output_pos
);
345 * implementation of private_parser_t.parse_uint16
347 static status_t
parse_uint16(private_parser_t
*this, int rule_number
, u_int16_t
*output_pos
)
349 if (this->byte_pos
+ sizeof(u_int16_t
) > this->input_roof
)
351 this->logger
->log(this->logger
, ERROR
, " not enough input to parse rule %d %s",
352 rule_number
, mapping_find(encoding_type_m
, this->rules
[rule_number
].type
));
357 this->logger
->log(this->logger
, ERROR
, " found rule %d %s on bitpos %d",
358 rule_number
, mapping_find(encoding_type_m
, this->rules
[rule_number
].type
),
362 /* caller interested in result ? */
363 if (output_pos
!= NULL
)
365 *output_pos
= ntohs(*((u_int16_t
*)this->byte_pos
));
367 this->logger
->log(this->logger
, RAW
|MOST
, " => %d", *output_pos
);
375 * implementation of private_parser_t.parse_uint32
377 static status_t
parse_uint32(private_parser_t
*this, int rule_number
, u_int32_t
*output_pos
)
379 if (this->byte_pos
+ sizeof(u_int32_t
) > this->input_roof
)
381 this->logger
->log(this->logger
, ERROR
, " not enough input to parse rule %d %s",
382 rule_number
, mapping_find(encoding_type_m
, this->rules
[rule_number
].type
));
387 this->logger
->log(this->logger
, ERROR
, " found rule %d %s on bitpos %d",
388 rule_number
, mapping_find(encoding_type_m
, this->rules
[rule_number
].type
),
392 /* caller interested in result ? */
393 if (output_pos
!= NULL
)
395 *output_pos
= ntohl(*((u_int32_t
*)this->byte_pos
));
397 this->logger
->log(this->logger
, RAW
|MOST
, " => %d", *output_pos
);
406 * implementation of private_parser_t.parse_uint64
408 static status_t
parse_uint64(private_parser_t
*this, int rule_number
, u_int64_t
*output_pos
)
410 if (this->byte_pos
+ sizeof(u_int64_t
) > this->input_roof
)
412 this->logger
->log(this->logger
, ERROR
, " not enough input to parse rule %d %s",
413 rule_number
, mapping_find(encoding_type_m
, this->rules
[rule_number
].type
));
418 this->logger
->log(this->logger
, ERROR
, " found rule %d %s on bitpos %d",
419 rule_number
, mapping_find(encoding_type_m
, this->rules
[rule_number
].type
),
423 /* caller interested in result ? */
424 if (output_pos
!= NULL
)
426 /* assuming little endian host order */
427 *(output_pos
+ 1) = ntohl(*((u_int32_t
*)this->byte_pos
));
428 *output_pos
= ntohl(*(((u_int32_t
*)this->byte_pos
) + 1));
430 this->logger
->log_bytes(this->logger
, RAW
|MOST
, " =>", (void*)output_pos
, 8);
439 static status_t
parse_bytes (private_parser_t
*this, int rule_number
, u_int8_t
*output_pos
,size_t bytes
)
441 if (this->byte_pos
+ bytes
> this->input_roof
)
443 this->logger
->log(this->logger
, ERROR
, " not enough input to parse rule %d %s",
444 rule_number
, mapping_find(encoding_type_m
, this->rules
[rule_number
].type
));
449 this->logger
->log(this->logger
, ERROR
, " found rule %d %s on bitpos %d",
450 rule_number
, mapping_find(encoding_type_m
, this->rules
[rule_number
].type
),
455 /* caller interested in result ? */
456 if (output_pos
!= NULL
)
458 memcpy(output_pos
,this->byte_pos
,bytes
);
460 this->logger
->log_bytes(this->logger
, RAW
|MOST
, " =>", (void*)output_pos
, bytes
);
462 this->byte_pos
+= bytes
;
468 * implementation of private_parser_t.parse_bit
470 static status_t
parse_bit(private_parser_t
*this, int rule_number
, bool *output_pos
)
472 if (this->byte_pos
+ sizeof(u_int8_t
) > this->input_roof
)
474 this->logger
->log(this->logger
, ERROR
, " not enough input to parse rule %d %s",
475 rule_number
, mapping_find(encoding_type_m
, this->rules
[rule_number
].type
));
478 /* caller interested in result ? */
479 if (output_pos
!= NULL
)
482 mask
= 0x01 << (7 - this->bit_pos
);
483 *output_pos
= *this->byte_pos
& mask
;
487 /* set to a "clean", comparable true */
491 this->logger
->log(this->logger
, RAW
|MOST
, " => %d", *output_pos
);
493 this->bit_pos
= (this->bit_pos
+ 1) % 8;
494 if (this->bit_pos
== 0)
504 * implementation of private_parser_t.parse_list
506 static status_t
parse_list(private_parser_t
*this, int rule_number
, linked_list_t
**output_pos
, payload_type_t payload_type
, size_t length
)
508 linked_list_t
* list
= *output_pos
;
512 this->logger
->log(this->logger
, ERROR
, " invalid length for rule %d %s",
513 rule_number
, mapping_find(encoding_type_m
, this->rules
[rule_number
].type
));
519 this->logger
->log(this->logger
, ERROR
, " found rule %d %s on bitpos %d",
520 rule_number
, mapping_find(encoding_type_m
, this->rules
[rule_number
].type
), this->bit_pos
);
526 u_int8_t
*pos_before
= this->byte_pos
;
529 this->logger
->log(this->logger
, CONTROL
|MORE
, " %d bytes left, parsing recursivly %s",
530 length
, mapping_find(payload_type_m
, payload_type
));
531 status
= this->public.parse_payload((parser_t
*)this, payload_type
, &payload
);
532 if (status
!= SUCCESS
)
534 this->logger
->log(this->logger
, ERROR
, " parsing of a %s substructure failed",
535 mapping_find(payload_type_m
, payload_type
));
538 list
->insert_last(list
, payload
);
539 length
-= this->byte_pos
- pos_before
;
546 * implementation of private_parser_t.parse_chunk
548 static status_t
parse_chunk(private_parser_t
*this, int rule_number
, chunk_t
*output_pos
, size_t length
)
550 if (this->byte_pos
+ length
> this->input_roof
)
552 this->logger
->log(this->logger
, ERROR
, " not enough input (%d bytes) to parse rule %d %s",
553 length
, rule_number
, mapping_find(encoding_type_m
, this->rules
[rule_number
].type
));
558 this->logger
->log(this->logger
, ERROR
, " found rule %d %s on bitpos %d",
559 rule_number
, mapping_find(encoding_type_m
, this->rules
[rule_number
].type
), this->bit_pos
);
562 if (output_pos
!= NULL
)
564 output_pos
->len
= length
;
565 output_pos
->ptr
= allocator_alloc(length
);
566 if (output_pos
->ptr
== NULL
)
568 this->logger
->log(this->logger
, ERROR
, " allocation of chunk (%d bytes) failed", length
);
571 memcpy(output_pos
->ptr
, this->byte_pos
, length
);
573 this->byte_pos
+= length
;
574 this->logger
->log_bytes(this->logger
, RAW
|MOST
, " =>", (void*)output_pos
->ptr
, length
);
580 * implementation of parser_context_t.parse_payload
582 static status_t
parse_payload(private_parser_t
*this, payload_type_t payload_type
, payload_t
**payload
)
586 size_t rule_count
, payload_length
, spi_size
, attribute_length
;
587 bool attribute_format
;
589 encoding_rule_t
*rule
;
591 this->logger
->log(this->logger
, CONTROL
, "parsing %s payload, %d bytes left",
592 mapping_find(payload_type_m
, payload_type
),
593 this->input_roof
-this->byte_pos
);
595 this->logger
->log_bytes(this->logger
, RAW
, "parsing payload from", this->byte_pos
,
596 this->input_roof
-this->byte_pos
);
598 /* ok, do the parsing */
599 pld
= payload_create(payload_type
);
602 this->logger
->log(this->logger
, ERROR
, " payload %s not supported", mapping_find(payload_type_m
, payload_type
));
603 return NOT_SUPPORTED
;
605 /* base pointer for output, avoids casting in every rule */
608 pld
->get_encoding_rules(pld
, &(this->rules
), &rule_count
);
610 for (rule_number
= 0; rule_number
< rule_count
; rule_number
++)
612 rule
= &(this->rules
[rule_number
]);
613 this->logger
->log(this->logger
, CONTROL
|MORE
, " parsing rule %d %s",
614 rule_number
, mapping_find(encoding_type_m
, rule
->type
));
619 if (this->parse_uint4(this, rule_number
, output
+ rule
->offset
) != SUCCESS
)
628 if (this->parse_uint8(this, rule_number
, output
+ rule
->offset
) != SUCCESS
)
637 if (this->parse_uint16(this, rule_number
, output
+ rule
->offset
) != SUCCESS
)
646 if (this->parse_uint32(this, rule_number
, output
+ rule
->offset
) != SUCCESS
)
655 if (this->parse_uint64(this, rule_number
, output
+ rule
->offset
) != SUCCESS
)
664 if (this->parse_bytes(this, rule_number
, output
+ rule
->offset
,8) != SUCCESS
)
673 if (this->parse_bit(this, rule_number
, NULL
) != SUCCESS
)
682 if (this->parse_uint8(this, rule_number
, NULL
) != SUCCESS
)
691 if (this->parse_bit(this, rule_number
, output
+ rule
->offset
) != SUCCESS
)
700 if (this->parse_uint16(this, rule_number
, output
+ rule
->offset
) != SUCCESS
)
705 payload_length
= *(u_int16_t
*)(output
+ rule
->offset
);
710 if (this->parse_uint32(this, rule_number
, output
+ rule
->offset
) != SUCCESS
)
719 if (this->parse_uint8(this, rule_number
, output
+ rule
->offset
) != SUCCESS
)
724 spi_size
= *(u_int8_t
*)(output
+ rule
->offset
);
729 if (this->parse_chunk(this, rule_number
, output
+ rule
->offset
, spi_size
) != SUCCESS
)
738 size_t proposals_length
= payload_length
- SA_PAYLOAD_HEADER_LENGTH
;
739 if (this->parse_list(this, rule_number
, output
+ rule
->offset
, PROPOSAL_SUBSTRUCTURE
, proposals_length
) != SUCCESS
)
748 size_t transforms_length
= payload_length
- spi_size
- PROPOSAL_SUBSTRUCTURE_HEADER_LENGTH
;
749 if (this->parse_list(this, rule_number
, output
+ rule
->offset
, TRANSFORM_SUBSTRUCTURE
, transforms_length
) != SUCCESS
)
756 case TRANSFORM_ATTRIBUTES
:
758 size_t transform_a_length
= payload_length
- TRANSFORM_SUBSTRUCTURE_HEADER_LENGTH
;
759 if (this->parse_list(this, rule_number
, output
+ rule
->offset
, TRANSFORM_ATTRIBUTE
, transform_a_length
) != SUCCESS
)
766 case ATTRIBUTE_FORMAT
:
768 if (this->parse_bit(this, rule_number
, output
+ rule
->offset
) != SUCCESS
)
773 attribute_format
= *(bool*)(output
+ rule
->offset
);
778 if (this->parse_uint15(this, rule_number
, output
+ rule
->offset
) != SUCCESS
)
783 attribute_format
= *(bool*)(output
+ rule
->offset
);
786 case ATTRIBUTE_LENGTH_OR_VALUE
:
788 if (this->parse_uint16(this, rule_number
, output
+ rule
->offset
) != SUCCESS
)
793 attribute_length
= *(u_int16_t
*)(output
+ rule
->offset
);
796 case ATTRIBUTE_VALUE
:
798 if (attribute_format
== FALSE
)
800 if (this->parse_chunk(this, rule_number
, output
+ rule
->offset
, attribute_length
) != SUCCESS
)
810 size_t nonce_length
= payload_length
- NONCE_PAYLOAD_HEADER_LENGTH
;
811 if (this->parse_chunk(this, rule_number
, output
+ rule
->offset
, nonce_length
) != SUCCESS
)
818 case KEY_EXCHANGE_DATA
:
820 size_t keydata_length
= payload_length
- KE_PAYLOAD_HEADER_LENGTH
;
821 if (this->parse_chunk(this, rule_number
, output
+ rule
->offset
, keydata_length
) != SUCCESS
)
828 case NOTIFICATION_DATA
:
830 size_t notify_length
= payload_length
- NOTIFY_PAYLOAD_HEADER_LENGTH
- spi_size
;
831 if (this->parse_chunk(this, rule_number
, output
+ rule
->offset
, notify_length
) != SUCCESS
)
840 this->logger
->log(this->logger
, ERROR
, " no rule to parse rule %d %s (%d)", rule_number
, mapping_find(encoding_type_m
, rule
->type
), rule
->type
);
845 /* process next rulue */
851 this->logger
->log(this->logger
, CONTROL
, "parsing %s successful", mapping_find(payload_type_m
, payload_type
));
856 * implementation of parser_t.reset_context
858 static status_t
reset_context (private_parser_t
*this)
860 this->byte_pos
= this->input
;
866 * implementation of parser_t.destroy
868 static status_t
destroy(private_parser_t
*this)
870 global_logger_manager
->destroy_logger(global_logger_manager
,this->logger
);
871 allocator_free(this);
879 parser_t
*parser_create(chunk_t data
)
881 private_parser_t
*this = allocator_alloc_thing(private_parser_t
);
888 this->logger
= global_logger_manager
->create_logger(global_logger_manager
, PARSER
, NULL
);
889 this->logger
->disable_level(this->logger
, FULL
);
890 this->logger
->enable_level(this->logger
, CONTROL
);
893 if (this->logger
== NULL
)
895 global_logger_manager
->destroy_logger(global_logger_manager
, this->logger
);
896 allocator_free(this);
900 this->public.parse_payload
= (status_t(*)(parser_t
*,payload_type_t
,payload_t
**)) parse_payload
;
901 this->public.reset_context
= (status_t(*)(parser_t
*)) reset_context
;
902 this->public.destroy
= (status_t(*)(parser_t
*)) destroy
;
905 this->parse_uint4
= parse_uint4
;
906 this->parse_uint8
= parse_uint8
;
907 this->parse_uint15
= parse_uint15
;
908 this->parse_uint16
= parse_uint16
;
909 this->parse_uint32
= parse_uint32
;
910 this->parse_uint64
= parse_uint64
;
911 this->parse_bytes
= parse_bytes
;
912 this->parse_bit
= parse_bit
;
913 this->parse_list
= parse_list
;
914 this->parse_chunk
= parse_chunk
;
916 this->input
= data
.ptr
;
917 this->byte_pos
= data
.ptr
;
919 this->input_roof
= data
.ptr
+ data
.len
;
921 return (parser_t
*)this;