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 typedef struct private_parser_t private_parser_t
;
50 * @private data stored in a context
52 * contains pointers and counters to store current state
54 struct private_parser_t
{
56 * Public members, see parser_t
61 * @brief parse a 4-Bit unsigned integer from the current parsing position.
63 * @param this parser object
64 * @param rule_number number of current rule
65 * @param[out] output_pos pointer where to write the parsed result
68 * - PARSE_ERROR when not successful
70 status_t (*parse_uint4
) (private_parser_t
*this, int rule_number
, u_int8_t
*output_pos
);
73 * @brief parse a 8-Bit unsigned integer from the current parsing position.
75 * @param this parser object
76 * @param rule_number number of current rule
77 * @param[out] output_pos pointer where to write the parsed result
80 * - PARSE_ERROR when not successful
82 status_t (*parse_uint8
) (private_parser_t
*this, int rule_number
, u_int8_t
*output_pos
);
85 * @brief parse a 15-Bit unsigned integer from the current parsing position.
87 * This is a special case used for ATTRIBUTE_TYPE.
88 * Big-/Little-endian conversion is done here.
90 * @param this parser object
91 * @param rule_number number of current rule
92 * @param[out] output_pos pointer where to write the parsed result
95 * - PARSE_ERROR when not successful
97 status_t (*parse_uint15
) (private_parser_t
*this, int rule_number
, u_int16_t
*output_pos
);
100 * @brief parse a 16-Bit unsigned integer from the current parsing position.
102 * Big-/Little-endian conversion is done here.
104 * @param this parser object
105 * @param rule_number number of current rule
106 * @param[out] output_pos pointer where to write the parsed result
109 * - PARSE_ERROR when not successful
111 status_t (*parse_uint16
) (private_parser_t
*this, int rule_number
, u_int16_t
*output_pos
);
114 * @brief parse a 32-Bit unsigned integer from the current parsing position.
116 * Big-/Little-endian conversion is done here.
118 * @param this parser object
119 * @param rule_number number of current rule
120 * @param[out] output_pos pointer where to write the parsed result
123 * - PARSE_ERROR when not successful
125 status_t (*parse_uint32
) (private_parser_t
*this, int rule_number
, u_int32_t
*output_pos
);
128 * @brief parse a 64-Bit unsigned integer from the current parsing position.
130 * @todo add support for big-endian machines.
132 * @param this parser object
133 * @param rule_number number of current rule
134 * @param[out] output_pos pointer where to write the parsed result
137 * - PARSE_ERROR when not successful
139 status_t (*parse_uint64
) (private_parser_t
*this, int rule_number
, u_int64_t
*output_pos
);
142 * @brief parse a given amount of bytes and writes them to a specific location
144 * @param this parser object
145 * @param rule_number number of current rule
146 * @param[out] output_pos pointer where to write the parsed result
147 * @param bytes number of bytes to parse
150 * - PARSE_ERROR when not successful
152 status_t (*parse_bytes
) (private_parser_t
*this, int rule_number
, u_int8_t
*output_pos
,size_t bytes
);
155 * @brief parse a single Bit from the current parsing position
157 * @param this parser object
158 * @param rule_number number of current rule
159 * @param[out] output_pos pointer where to write the parsed result
162 * - PARSE_ERROR when not successful
164 status_t (*parse_bit
) (private_parser_t
*this, int rule_number
, bool *output_pos
);
167 * @brief parse substructures in a list
169 * This function calls the parser recursivly to parse contained substructures
170 * in a linked_list_t. The list must already be created. Payload defines
171 * the type of the substructures. parsing is continued until the specified length
172 * is completely parsed.
174 * @param this parser object
175 * @param rule_number number of current rule
176 * @param[out] output_pos pointer of a linked_list where substructures are added
177 * @param payload_type type of the contained substructures to parse
178 * @param length number of bytes to parse in this list
181 * - PARSE_ERROR when not successful
183 status_t (*parse_list
) (private_parser_t
*this, int rule_number
, linked_list_t
**output_pos
, payload_type_t payload_ype
, size_t length
);
186 * @brief parse data from current parsing position in a chunk.
188 * This function clones length number of bytes to output_pos, without
189 * modifiyng them. Space will be allocated and must be freed by caller.
191 * @param this parser object
192 * @param rule_number number of current rule
193 * @param[out] output_pos pointer of a chunk which will point to the allocated data
194 * @param length number of bytes to clone
197 * - PARSE_ERROR when not successful
199 status_t (*parse_chunk
) (private_parser_t
*this, int rule_number
, chunk_t
*output_pos
, size_t length
);
202 * Current bit for reading in input data
207 * Current byte for reading in input data
212 * input data to parse
217 * roof of input, used for length-checking
219 u_int8_t
*input_roof
;
222 * set of encoding rules for this parsing session
224 encoding_rule_t
*rules
;
233 * implementation of private_parser_t.parse_uint4
235 static status_t
parse_uint4(private_parser_t
*this, int rule_number
, u_int8_t
*output_pos
)
237 if (this->byte_pos
+ sizeof(u_int8_t
) > this->input_roof
)
239 this->logger
->log(this->logger
, ERROR
, " not enough input to parse rule %d %s",
240 rule_number
, mapping_find(encoding_type_m
,
241 this->rules
[rule_number
].type
));
244 switch (this->bit_pos
)
247 /* caller interested in result ? */
248 if (output_pos
!= NULL
)
250 *output_pos
= *(this->byte_pos
) >> 4;
255 /* caller interested in result ? */
256 if (output_pos
!= NULL
)
258 *output_pos
= *(this->byte_pos
) & 0x0F;
264 this->logger
->log(this->logger
, ERROR
, " found rule %d %s on bitpos %d",
265 rule_number
, mapping_find(encoding_type_m
,
266 this->rules
[rule_number
].type
), this->bit_pos
);
270 if (output_pos
!= NULL
)
272 this->logger
->log(this->logger
, RAW
|MOST
, " => %d", *output_pos
);
280 * implementation of private_parser_t.parse_uint8
282 static status_t
parse_uint8(private_parser_t
*this, int rule_number
, u_int8_t
*output_pos
)
284 if (this->byte_pos
+ sizeof(u_int8_t
) > this->input_roof
)
286 this->logger
->log(this->logger
, ERROR
, " not enough input to parse rule %d %s",
287 rule_number
, mapping_find(encoding_type_m
,
288 this->rules
[rule_number
].type
));
293 this->logger
->log(this->logger
, ERROR
, " found rule %d %s on bitpos %d",
294 rule_number
, mapping_find(encoding_type_m
,
295 this->rules
[rule_number
].type
), this->bit_pos
);
299 /* caller interested in result ? */
300 if (output_pos
!= NULL
)
302 *output_pos
= *(this->byte_pos
);
303 this->logger
->log(this->logger
, RAW
|MOST
, " => %d", *output_pos
);
313 * implementation of private_parser_t.parse_uint15
315 static status_t
parse_uint15(private_parser_t
*this, int rule_number
, u_int16_t
*output_pos
)
317 if (this->byte_pos
+ sizeof(u_int16_t
) > this->input_roof
)
319 this->logger
->log(this->logger
, ERROR
, " not enough input to parse rule %d %s",
320 rule_number
, mapping_find(encoding_type_m
,
321 this->rules
[rule_number
].type
));
324 if (this->bit_pos
!= 1)
326 this->logger
->log(this->logger
, ERROR
, " found rule %d %s on bitpos %d",
327 rule_number
, mapping_find(encoding_type_m
, this->rules
[rule_number
].type
),
331 /* caller interested in result ? */
332 if (output_pos
!= NULL
)
334 *output_pos
= ntohs(*((u_int16_t
*)this->byte_pos
)) & ~0x8000;
335 this->logger
->log(this->logger
, RAW
|MOST
, " => %d", *output_pos
);
346 * implementation of private_parser_t.parse_uint16
348 static status_t
parse_uint16(private_parser_t
*this, int rule_number
, u_int16_t
*output_pos
)
350 if (this->byte_pos
+ sizeof(u_int16_t
) > this->input_roof
)
352 this->logger
->log(this->logger
, ERROR
, " not enough input to parse rule %d %s",
353 rule_number
, mapping_find(encoding_type_m
, this->rules
[rule_number
].type
));
358 this->logger
->log(this->logger
, ERROR
, " found rule %d %s on bitpos %d",
359 rule_number
, mapping_find(encoding_type_m
, this->rules
[rule_number
].type
),
363 /* caller interested in result ? */
364 if (output_pos
!= NULL
)
366 *output_pos
= ntohs(*((u_int16_t
*)this->byte_pos
));
368 this->logger
->log(this->logger
, RAW
|MOST
, " => %d", *output_pos
);
376 * implementation of private_parser_t.parse_uint32
378 static status_t
parse_uint32(private_parser_t
*this, int rule_number
, u_int32_t
*output_pos
)
380 if (this->byte_pos
+ sizeof(u_int32_t
) > this->input_roof
)
382 this->logger
->log(this->logger
, ERROR
, " not enough input to parse rule %d %s",
383 rule_number
, mapping_find(encoding_type_m
, this->rules
[rule_number
].type
));
388 this->logger
->log(this->logger
, ERROR
, " found rule %d %s on bitpos %d",
389 rule_number
, mapping_find(encoding_type_m
, this->rules
[rule_number
].type
),
393 /* caller interested in result ? */
394 if (output_pos
!= NULL
)
396 *output_pos
= ntohl(*((u_int32_t
*)this->byte_pos
));
398 this->logger
->log(this->logger
, RAW
|MOST
, " => %d", *output_pos
);
407 * implementation of private_parser_t.parse_uint64
409 static status_t
parse_uint64(private_parser_t
*this, int rule_number
, u_int64_t
*output_pos
)
411 if (this->byte_pos
+ sizeof(u_int64_t
) > this->input_roof
)
413 this->logger
->log(this->logger
, ERROR
, " not enough input to parse rule %d %s",
414 rule_number
, mapping_find(encoding_type_m
, this->rules
[rule_number
].type
));
419 this->logger
->log(this->logger
, ERROR
, " found rule %d %s on bitpos %d",
420 rule_number
, mapping_find(encoding_type_m
, this->rules
[rule_number
].type
),
424 /* caller interested in result ? */
425 if (output_pos
!= NULL
)
427 /* assuming little endian host order */
428 *(output_pos
+ 1) = ntohl(*((u_int32_t
*)this->byte_pos
));
429 *output_pos
= ntohl(*(((u_int32_t
*)this->byte_pos
) + 1));
431 this->logger
->log_bytes(this->logger
, RAW
|MOST
, " =>", (void*)output_pos
, 8);
440 static status_t
parse_bytes (private_parser_t
*this, int rule_number
, u_int8_t
*output_pos
,size_t bytes
)
442 if (this->byte_pos
+ bytes
> this->input_roof
)
444 this->logger
->log(this->logger
, ERROR
, " not enough input to parse rule %d %s",
445 rule_number
, mapping_find(encoding_type_m
, this->rules
[rule_number
].type
));
450 this->logger
->log(this->logger
, ERROR
, " found rule %d %s on bitpos %d",
451 rule_number
, mapping_find(encoding_type_m
, this->rules
[rule_number
].type
),
456 /* caller interested in result ? */
457 if (output_pos
!= NULL
)
459 memcpy(output_pos
,this->byte_pos
,bytes
);
461 this->logger
->log_bytes(this->logger
, RAW
|MOST
, " =>", (void*)output_pos
, bytes
);
463 this->byte_pos
+= bytes
;
469 * implementation of private_parser_t.parse_bit
471 static status_t
parse_bit(private_parser_t
*this, int rule_number
, bool *output_pos
)
473 if (this->byte_pos
+ sizeof(u_int8_t
) > this->input_roof
)
475 this->logger
->log(this->logger
, ERROR
, " not enough input to parse rule %d %s",
476 rule_number
, mapping_find(encoding_type_m
, this->rules
[rule_number
].type
));
479 /* caller interested in result ? */
480 if (output_pos
!= NULL
)
483 mask
= 0x01 << (7 - this->bit_pos
);
484 *output_pos
= *this->byte_pos
& mask
;
488 /* set to a "clean", comparable true */
492 this->logger
->log(this->logger
, RAW
|MOST
, " => %d", *output_pos
);
494 this->bit_pos
= (this->bit_pos
+ 1) % 8;
495 if (this->bit_pos
== 0)
505 * implementation of private_parser_t.parse_list
507 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
)
509 linked_list_t
* list
= *output_pos
;
513 this->logger
->log(this->logger
, ERROR
, " invalid length for rule %d %s",
514 rule_number
, mapping_find(encoding_type_m
, this->rules
[rule_number
].type
));
520 this->logger
->log(this->logger
, ERROR
, " found rule %d %s on bitpos %d",
521 rule_number
, mapping_find(encoding_type_m
, this->rules
[rule_number
].type
), this->bit_pos
);
527 u_int8_t
*pos_before
= this->byte_pos
;
530 this->logger
->log(this->logger
, CONTROL
|MORE
, " %d bytes left, parsing recursivly %s",
531 length
, mapping_find(payload_type_m
, payload_type
));
532 status
= this->public.parse_payload((parser_t
*)this, payload_type
, &payload
);
533 if (status
!= SUCCESS
)
535 this->logger
->log(this->logger
, ERROR
, " parsing of a %s substructure failed",
536 mapping_find(payload_type_m
, payload_type
));
539 list
->insert_last(list
, payload
);
540 length
-= this->byte_pos
- pos_before
;
547 * implementation of private_parser_t.parse_chunk
549 static status_t
parse_chunk(private_parser_t
*this, int rule_number
, chunk_t
*output_pos
, size_t length
)
551 if (this->byte_pos
+ length
> this->input_roof
)
553 this->logger
->log(this->logger
, ERROR
, " not enough input (%d bytes) to parse rule %d %s",
554 length
, rule_number
, mapping_find(encoding_type_m
, this->rules
[rule_number
].type
));
559 this->logger
->log(this->logger
, ERROR
, " found rule %d %s on bitpos %d",
560 rule_number
, mapping_find(encoding_type_m
, this->rules
[rule_number
].type
), this->bit_pos
);
563 if (output_pos
!= NULL
)
565 output_pos
->len
= length
;
566 output_pos
->ptr
= allocator_alloc(length
);
567 if (output_pos
->ptr
== NULL
)
569 this->logger
->log(this->logger
, ERROR
, " allocation of chunk (%d bytes) failed", length
);
572 memcpy(output_pos
->ptr
, this->byte_pos
, length
);
574 this->byte_pos
+= length
;
575 this->logger
->log_bytes(this->logger
, RAW
|MOST
, " =>", (void*)output_pos
->ptr
, length
);
581 * implementation of parser_context_t.parse_payload
583 static status_t
parse_payload(private_parser_t
*this, payload_type_t payload_type
, payload_t
**payload
)
587 size_t rule_count
, payload_length
, spi_size
, attribute_length
;
588 bool attribute_format
;
590 encoding_rule_t
*rule
;
592 this->logger
->log(this->logger
, CONTROL
, "parsing %s payload, %d bytes left",
593 mapping_find(payload_type_m
, payload_type
),
594 this->input_roof
-this->byte_pos
);
596 this->logger
->log_bytes(this->logger
, RAW
, "parsing payload from", this->byte_pos
,
597 this->input_roof
-this->byte_pos
);
599 /* ok, do the parsing */
600 pld
= payload_create(payload_type
);
603 this->logger
->log(this->logger
, ERROR
, " payload %s not supported", mapping_find(payload_type_m
, payload_type
));
604 return NOT_SUPPORTED
;
606 /* base pointer for output, avoids casting in every rule */
609 pld
->get_encoding_rules(pld
, &(this->rules
), &rule_count
);
611 for (rule_number
= 0; rule_number
< rule_count
; rule_number
++)
613 rule
= &(this->rules
[rule_number
]);
614 this->logger
->log(this->logger
, CONTROL
|MORE
, " parsing rule %d %s",
615 rule_number
, mapping_find(encoding_type_m
, rule
->type
));
620 if (this->parse_uint4(this, rule_number
, output
+ rule
->offset
) != SUCCESS
)
629 if (this->parse_uint8(this, rule_number
, output
+ rule
->offset
) != SUCCESS
)
638 if (this->parse_uint16(this, rule_number
, output
+ rule
->offset
) != SUCCESS
)
647 if (this->parse_uint32(this, rule_number
, output
+ rule
->offset
) != SUCCESS
)
656 if (this->parse_uint64(this, rule_number
, output
+ rule
->offset
) != SUCCESS
)
665 if (this->parse_bytes(this, rule_number
, output
+ rule
->offset
,8) != SUCCESS
)
674 if (this->parse_bit(this, rule_number
, NULL
) != SUCCESS
)
683 if (this->parse_uint8(this, rule_number
, NULL
) != SUCCESS
)
692 if (this->parse_bit(this, rule_number
, output
+ rule
->offset
) != SUCCESS
)
701 if (this->parse_uint16(this, rule_number
, output
+ rule
->offset
) != SUCCESS
)
706 payload_length
= *(u_int16_t
*)(output
+ rule
->offset
);
711 if (this->parse_uint32(this, rule_number
, output
+ rule
->offset
) != SUCCESS
)
720 if (this->parse_uint8(this, rule_number
, output
+ rule
->offset
) != SUCCESS
)
725 spi_size
= *(u_int8_t
*)(output
+ rule
->offset
);
730 if (this->parse_chunk(this, rule_number
, output
+ rule
->offset
, spi_size
) != SUCCESS
)
739 size_t proposals_length
= payload_length
- SA_PAYLOAD_HEADER_LENGTH
;
740 if (this->parse_list(this, rule_number
, output
+ rule
->offset
, PROPOSAL_SUBSTRUCTURE
, proposals_length
) != SUCCESS
)
749 size_t transforms_length
= payload_length
- spi_size
- PROPOSAL_SUBSTRUCTURE_HEADER_LENGTH
;
750 if (this->parse_list(this, rule_number
, output
+ rule
->offset
, TRANSFORM_SUBSTRUCTURE
, transforms_length
) != SUCCESS
)
757 case TRANSFORM_ATTRIBUTES
:
759 size_t transform_a_length
= payload_length
- TRANSFORM_SUBSTRUCTURE_HEADER_LENGTH
;
760 if (this->parse_list(this, rule_number
, output
+ rule
->offset
, TRANSFORM_ATTRIBUTE
, transform_a_length
) != SUCCESS
)
767 case ATTRIBUTE_FORMAT
:
769 if (this->parse_bit(this, rule_number
, output
+ rule
->offset
) != SUCCESS
)
774 attribute_format
= *(bool*)(output
+ rule
->offset
);
779 if (this->parse_uint15(this, rule_number
, output
+ rule
->offset
) != SUCCESS
)
784 attribute_format
= *(bool*)(output
+ rule
->offset
);
787 case ATTRIBUTE_LENGTH_OR_VALUE
:
789 if (this->parse_uint16(this, rule_number
, output
+ rule
->offset
) != SUCCESS
)
794 attribute_length
= *(u_int16_t
*)(output
+ rule
->offset
);
797 case ATTRIBUTE_VALUE
:
799 if (attribute_format
== FALSE
)
801 if (this->parse_chunk(this, rule_number
, output
+ rule
->offset
, attribute_length
) != SUCCESS
)
811 size_t nonce_length
= payload_length
- NONCE_PAYLOAD_HEADER_LENGTH
;
812 if (this->parse_chunk(this, rule_number
, output
+ rule
->offset
, nonce_length
) != SUCCESS
)
819 case KEY_EXCHANGE_DATA
:
821 size_t keydata_length
= payload_length
- KE_PAYLOAD_HEADER_LENGTH
;
822 if (this->parse_chunk(this, rule_number
, output
+ rule
->offset
, keydata_length
) != SUCCESS
)
829 case NOTIFICATION_DATA
:
831 size_t notify_length
= payload_length
- NOTIFY_PAYLOAD_HEADER_LENGTH
- spi_size
;
832 if (this->parse_chunk(this, rule_number
, output
+ rule
->offset
, notify_length
) != SUCCESS
)
841 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
);
846 /* process next rulue */
852 this->logger
->log(this->logger
, CONTROL
, "parsing %s successful", mapping_find(payload_type_m
, payload_type
));
857 * implementation of parser_t.reset_context
859 static status_t
reset_context (private_parser_t
*this)
861 this->byte_pos
= this->input
;
867 * implementation of parser_t.destroy
869 static status_t
destroy(private_parser_t
*this)
871 global_logger_manager
->destroy_logger(global_logger_manager
,this->logger
);
872 allocator_free(this);
880 parser_t
*parser_create(chunk_t data
)
882 private_parser_t
*this = allocator_alloc_thing(private_parser_t
);
889 this->logger
= global_logger_manager
->create_logger(global_logger_manager
, PARSER
, NULL
);
892 if (this->logger
== NULL
)
894 global_logger_manager
->destroy_logger(global_logger_manager
, this->logger
);
895 allocator_free(this);
899 this->public.parse_payload
= (status_t(*)(parser_t
*,payload_type_t
,payload_t
**)) parse_payload
;
900 this->public.reset_context
= (status_t(*)(parser_t
*)) reset_context
;
901 this->public.destroy
= (status_t(*)(parser_t
*)) destroy
;
904 this->parse_uint4
= parse_uint4
;
905 this->parse_uint8
= parse_uint8
;
906 this->parse_uint15
= parse_uint15
;
907 this->parse_uint16
= parse_uint16
;
908 this->parse_uint32
= parse_uint32
;
909 this->parse_uint64
= parse_uint64
;
910 this->parse_bytes
= parse_bytes
;
911 this->parse_bit
= parse_bit
;
912 this->parse_list
= parse_list
;
913 this->parse_chunk
= parse_chunk
;
915 this->input
= data
.ptr
;
916 this->byte_pos
= data
.ptr
;
918 this->input_roof
= data
.ptr
+ data
.len
;
920 return (parser_t
*)this;