diff --git a/Source/charon/encoding/generator.c b/Source/charon/encoding/generator.c index a19cd3669..701d64821 100644 --- a/Source/charon/encoding/generator.c +++ b/Source/charon/encoding/generator.c @@ -46,6 +46,7 @@ #include #include #include +#include typedef struct private_generator_t private_generator_t; @@ -742,6 +743,7 @@ static void generate_payload (private_generator_t *this,payload_t *payload) case CERT_DATA: case CERTREQ_DATA: case SPIS: + case VID_DATA: { u_int32_t payload_length_position_offset; u_int16_t length_of_payload; @@ -774,6 +776,9 @@ static void generate_payload (private_generator_t *this,payload_t *payload) case SPIS: header_length = DELETE_PAYLOAD_HEADER_LENGTH; break; + case VID_DATA: + header_length = VENDOR_ID_PAYLOAD_HEADER_LENGTH; + break; default: break; } diff --git a/Source/charon/encoding/parser.c b/Source/charon/encoding/parser.c index b0aefee84..22419c0e2 100644 --- a/Source/charon/encoding/parser.c +++ b/Source/charon/encoding/parser.c @@ -47,6 +47,7 @@ #include #include #include +#include typedef struct private_parser_t private_parser_t; @@ -860,6 +861,16 @@ static status_t parse_payload(private_parser_t *this, payload_type_t payload_typ } break; } + case VID_DATA: + { + size_t data_length = payload_length - VENDOR_ID_PAYLOAD_HEADER_LENGTH; + if (this->parse_chunk(this, rule_number, output + rule->offset, data_length) != SUCCESS) + { + pld->destroy(pld); + return PARSE_ERROR; + } + break; + } case KEY_EXCHANGE_DATA: { size_t keydata_length = payload_length - KE_PAYLOAD_HEADER_LENGTH; diff --git a/Source/charon/encoding/payloads/Makefile.payloads b/Source/charon/encoding/payloads/Makefile.payloads index a85822e37..55215ab81 100644 --- a/Source/charon/encoding/payloads/Makefile.payloads +++ b/Source/charon/encoding/payloads/Makefile.payloads @@ -54,6 +54,9 @@ OBJS+= $(BUILD_DIR)delete_payload.o $(BUILD_DIR)delete_payload.o : $(PAYLOADS_DIR)delete_payload.c $(PAYLOADS_DIR)delete_payload.h $(CC) $(CFLAGS) -c -o $@ $< +OBJS+= $(BUILD_DIR)vendor_id_payload.o +$(BUILD_DIR)vendor_id_payload.o : $(PAYLOADS_DIR)vendor_id_payload.c $(PAYLOADS_DIR)vendor_id_payload.h + $(CC) $(CFLAGS) -c -o $@ $< OBJS+= $(BUILD_DIR)ts_payload.o $(BUILD_DIR)ts_payload.o : $(PAYLOADS_DIR)ts_payload.c $(PAYLOADS_DIR)ts_payload.h diff --git a/Source/charon/encoding/payloads/encodings.c b/Source/charon/encoding/payloads/encodings.c index 2172671d1..efd6d5993 100644 --- a/Source/charon/encoding/payloads/encodings.c +++ b/Source/charon/encoding/payloads/encodings.c @@ -57,5 +57,6 @@ mapping_t encoding_type_m[] = { {CERT_DATA, "CERT_DATA"}, {CERTREQ_DATA, "CERTREQ_DATA"}, {SPIS, "SPIS"}, + {VID_DATA, "VID_DATA"}, {MAPPING_END, NULL} }; diff --git a/Source/charon/encoding/payloads/encodings.h b/Source/charon/encoding/payloads/encodings.h index 79740c339..39c6d31a1 100644 --- a/Source/charon/encoding/payloads/encodings.h +++ b/Source/charon/encoding/payloads/encodings.h @@ -397,6 +397,16 @@ enum encoding_type_t{ * When parsing (Payload Length - 8) bytes are read and written into the chunk pointing to. */ SPIS, + + /** + * Representating the VID DATA field in a VENDOR ID payload. + * + * When generating the content of the chunkt pointing to + * is written. + * + * When parsing (Payload Length - 4) bytes are read and written into the chunk pointing to. + */ + VID_DATA, /** * Representating an IKE_SPI field in an IKEv2 Header. diff --git a/Source/charon/encoding/payloads/payload.c b/Source/charon/encoding/payloads/payload.c index fda596355..ac50198cc 100644 --- a/Source/charon/encoding/payloads/payload.c +++ b/Source/charon/encoding/payloads/payload.c @@ -36,6 +36,7 @@ #include #include #include +#include /* * build the mappings for payload_type_t @@ -107,6 +108,8 @@ payload_t *payload_create(payload_type_t type) return (payload_t*)notify_payload_create(); case DELETE: return (payload_t*)delete_payload_create(); + case VENDOR_ID: + return (payload_t*)vendor_id_payload_create(); case ENCRYPTED: return (payload_t*)encryption_payload_create(); default: diff --git a/Source/charon/encoding/payloads/vendor_id_payload.c b/Source/charon/encoding/payloads/vendor_id_payload.c new file mode 100644 index 000000000..df1ba7ed1 --- /dev/null +++ b/Source/charon/encoding/payloads/vendor_id_payload.c @@ -0,0 +1,234 @@ +/** + * @file vendor_id_payload.c + * + * @brief Implementation of vendor_id_payload_t. + * + */ + +/* + * Copyright (C) 2005 Jan Hutter, Martin Willi + * 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 . + * + * 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 "vendor_id_payload.h" + +#include + + +typedef struct private_vendor_id_payload_t private_vendor_id_payload_t; + +/** + * Private data of an vendor_id_payload_t object. + * + */ +struct private_vendor_id_payload_t { + /** + * Public vendor_id_payload_t interface. + */ + vendor_id_payload_t public; + + /** + * Next payload type. + */ + u_int8_t next_payload; + + /** + * Critical flag. + */ + bool critical; + + /** + * Length of this payload. + */ + u_int16_t payload_length; + + /** + * The contained vendor_id data value. + */ + chunk_t vendor_id_data; +}; + +/** + * Encoding rules to parse or generate a VENDOR ID payload + * + * The defined offsets are the positions in a object of type + * private_vendor_id_payload_t. + * + */ +encoding_rule_t vendor_id_payload_encodings[] = { + /* 1 Byte next payload type, stored in the field next_payload */ + { U_INT_8, offsetof(private_vendor_id_payload_t, next_payload) }, + /* the critical bit */ + { FLAG, offsetof(private_vendor_id_payload_t, critical) }, + /* 7 Bit reserved bits, nowhere stored */ + { RESERVED_BIT, 0 }, + { RESERVED_BIT, 0 }, + { RESERVED_BIT, 0 }, + { RESERVED_BIT, 0 }, + { RESERVED_BIT, 0 }, + { RESERVED_BIT, 0 }, + { RESERVED_BIT, 0 }, + /* Length of the whole payload*/ + { PAYLOAD_LENGTH, offsetof(private_vendor_id_payload_t, payload_length)}, + /* some vendor_id data bytes, length is defined in PAYLOAD_LENGTH */ + { VID_DATA, offsetof(private_vendor_id_payload_t, vendor_id_data) } +}; + +/* + 1 2 3 + 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + ! Next Payload !C! RESERVED ! Payload Length ! + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + ! Cert Encoding ! ! + +-+-+-+-+-+-+-+-+ ! + ~ Certificate Data ~ + ! ! + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +*/ + +/** + * Implementation of payload_t.verify. + */ +static status_t verify(private_vendor_id_payload_t *this) +{ + if (this->critical) + { + /* critical bit is set! */ + return FAILED; + } + + return SUCCESS; +} + +/** + * Implementation of vendor_id_payload_t.get_encoding_rules. + */ +static void get_encoding_rules(private_vendor_id_payload_t *this, encoding_rule_t **rules, size_t *rule_count) +{ + *rules = vendor_id_payload_encodings; + *rule_count = sizeof(vendor_id_payload_encodings) / sizeof(encoding_rule_t); +} + +/** + * Implementation of payload_t.get_type. + */ +static payload_type_t get_payload_type(private_vendor_id_payload_t *this) +{ + return VENDOR_ID; +} + +/** + * Implementation of payload_t.get_next_type. + */ +static payload_type_t get_next_type(private_vendor_id_payload_t *this) +{ + return (this->next_payload); +} + +/** + * Implementation of payload_t.set_next_type. + */ +static void set_next_type(private_vendor_id_payload_t *this,payload_type_t type) +{ + this->next_payload = type; +} + +/** + * Implementation of payload_t.get_length. + */ +static size_t get_length(private_vendor_id_payload_t *this) +{ + return this->payload_length; +} + +/** + * Implementation of vendor_id_payload_t.set_data. + */ +static void set_data (private_vendor_id_payload_t *this, chunk_t data) +{ + if (this->vendor_id_data.ptr != NULL) + { + allocator_free_chunk(&(this->vendor_id_data)); + } + this->vendor_id_data.ptr = allocator_clone_bytes(data.ptr,data.len); + this->vendor_id_data.len = data.len; + this->payload_length = VENDOR_ID_PAYLOAD_HEADER_LENGTH + this->vendor_id_data.len; +} + +/** + * Implementation of vendor_id_payload_t.get_data. + */ +static chunk_t get_data (private_vendor_id_payload_t *this) +{ + return (this->vendor_id_data); +} + +/** + * Implementation of vendor_id_payload_t.get_data_clone. + */ +static chunk_t get_data_clone (private_vendor_id_payload_t *this) +{ + chunk_t cloned_data; + if (this->vendor_id_data.ptr == NULL) + { + return (this->vendor_id_data); + } + cloned_data.ptr = allocator_clone_bytes(this->vendor_id_data.ptr,this->vendor_id_data.len); + cloned_data.len = this->vendor_id_data.len; + return cloned_data; +} + +/** + * Implementation of payload_t.destroy and vendor_id_payload_t.destroy. + */ +static void destroy(private_vendor_id_payload_t *this) +{ + if (this->vendor_id_data.ptr != NULL) + { + allocator_free_chunk(&(this->vendor_id_data)); + } + + allocator_free(this); +} + +/* + * Described in header + */ +vendor_id_payload_t *vendor_id_payload_create() +{ + private_vendor_id_payload_t *this = allocator_alloc_thing(private_vendor_id_payload_t); + + /* interface functions */ + this->public.payload_interface.verify = (status_t (*) (payload_t *))verify; + this->public.payload_interface.get_encoding_rules = (void (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules; + this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length; + this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type; + this->public.payload_interface.set_next_type = (void (*) (payload_t *,payload_type_t)) set_next_type; + this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_payload_type; + this->public.payload_interface.destroy = (void (*) (payload_t *))destroy; + + /* public functions */ + this->public.destroy = (void (*) (vendor_id_payload_t *)) destroy; + this->public.set_data = (void (*) (vendor_id_payload_t *,chunk_t)) set_data; + this->public.get_data_clone = (chunk_t (*) (vendor_id_payload_t *)) get_data_clone; + this->public.get_data = (chunk_t (*) (vendor_id_payload_t *)) get_data; + + /* private variables */ + this->critical = FALSE; + this->next_payload = NO_PAYLOAD; + this->payload_length =VENDOR_ID_PAYLOAD_HEADER_LENGTH; + this->vendor_id_data = CHUNK_INITIALIZER; + + return (&(this->public)); +} diff --git a/Source/charon/encoding/payloads/vendor_id_payload.h b/Source/charon/encoding/payloads/vendor_id_payload.h new file mode 100644 index 000000000..b17092864 --- /dev/null +++ b/Source/charon/encoding/payloads/vendor_id_payload.h @@ -0,0 +1,101 @@ +/** + * @file vendor_id_payload.h + * + * @brief Interface of vendor_id_payload_t. + * + */ + +/* + * Copyright (C) 2005 Jan Hutter, Martin Willi + * 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 . + * + * 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. + */ + +#ifndef _VENDOR_ID_PAYLOAD_H_ +#define _VENDOR_ID_PAYLOAD_H_ + +#include +#include + +/** + * Length of a VENDOR ID payload without the VID data in bytes. + * + * @ingroup payloads + */ +#define VENDOR_ID_PAYLOAD_HEADER_LENGTH 4 + + +typedef struct vendor_id_payload_t vendor_id_payload_t; + +/** + * Object representing an IKEv2 VENDOR ID payload. + * + * The VENDOR ID payload format is described in draft section 3.12. + * + * @ingroup payloads + * + */ +struct vendor_id_payload_t { + /** + * The payload_t interface. + */ + payload_t payload_interface; + + /** + * @brief Set the VID data. + * + * Data are getting cloned. + * + * @param this calling vendor_id_payload_t object + * @param data VID data as chunk_t + */ + void (*set_data) (vendor_id_payload_t *this, chunk_t data); + + /** + * @brief Get the VID data. + * + * Returned data are a copy of the internal one. + * + * @param this calling vendor_id_payload_t object + * @return VID data as chunk_t + */ + chunk_t (*get_data_clone) (vendor_id_payload_t *this); + + /** + * @brief Get the VID data. + * + * Returned data are NOT copied. + * + * @param this calling vendor_id_payload_t object + * @return VID data as chunk_t + */ + chunk_t (*get_data) (vendor_id_payload_t *this); + + /** + * @brief Destroys an vendor_id_payload_t object. + * + * @param this vendor_id_payload_t object to destroy + */ + void (*destroy) (vendor_id_payload_t *this); +}; + +/** + * @brief Creates an empty vendor_id_payload_t object. + * + * @return created vendor_id_payload_t object + * + * @ingroup payloads + */ +vendor_id_payload_t *vendor_id_payload_create(); + + +#endif //_VENDOR_ID_PAYLOAD_H_ diff --git a/Source/charon/testcases/generator_test.c b/Source/charon/testcases/generator_test.c index 75075f2f7..0ca227694 100644 --- a/Source/charon/testcases/generator_test.c +++ b/Source/charon/testcases/generator_test.c @@ -44,6 +44,7 @@ #include #include #include +#include /* * Described in Header @@ -1296,3 +1297,52 @@ void test_generator_with_delete_payload(tester_t *tester) charon->logger_manager->destroy_logger(charon->logger_manager,logger); } + +/* + * Described in header. + */ +void test_generator_with_vendor_id_payload(tester_t *tester) +{ + generator_t *generator; + vendor_id_payload_t *vendor_id_payload; + logger_t *logger; + chunk_t generated_data; + chunk_t data; + + logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"Message with VENDOR ID Payload"); + + /* create generator */ + generator = generator_create(); + tester->assert_true(tester,(generator != NULL), "generator create check"); + + vendor_id_payload = vendor_id_payload_create(); + + + data.ptr = "123456789012"; + data.len = strlen(data.ptr); +; + vendor_id_payload->set_data(vendor_id_payload,data); + generator->generate_payload(generator,(payload_t *)vendor_id_payload); + generator->write_to_chunk(generator,&generated_data); + logger->log_chunk(logger,RAW,"generated payload",&generated_data); + + u_int8_t expected_generation[] = { + /* payload header */ + 0x00,0x00,0x00,0x10, + /* vendor_id data */ + 0x31,0x32,0x33,0x34, + 0x35,0x36,0x37,0x38, + 0x39,0x30,0x31,0x32, + }; + + logger->log_bytes(logger,RAW,"expected payload",expected_generation,sizeof(expected_generation)); + + tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data"); + + allocator_free_chunk(&generated_data); + + vendor_id_payload->destroy(vendor_id_payload); + generator->destroy(generator); + + charon->logger_manager->destroy_logger(charon->logger_manager,logger); +} diff --git a/Source/charon/testcases/generator_test.h b/Source/charon/testcases/generator_test.h index 2f7828b82..31876adee 100644 --- a/Source/charon/testcases/generator_test.h +++ b/Source/charon/testcases/generator_test.h @@ -152,5 +152,13 @@ void test_generator_with_certreq_payload(tester_t *tester); */ void test_generator_with_delete_payload(tester_t *tester); +/** + * @brief Test function used to test the generator with VENDOR ID payload. + * + * @param tester associated tester_t object + * + * @ingroup testcases + */ +void test_generator_with_vendor_id_payload(tester_t *tester); #endif /*GENERATOR_TEST_H_*/ diff --git a/Source/charon/testcases/parser_test.c b/Source/charon/testcases/parser_test.c index 5bf92fa96..af8d786e4 100644 --- a/Source/charon/testcases/parser_test.c +++ b/Source/charon/testcases/parser_test.c @@ -40,6 +40,7 @@ #include #include #include +#include /* @@ -815,3 +816,43 @@ void test_parser_with_delete_payload(tester_t *tester) delete_payload->destroy(delete_payload); } + +/* + * Described in Header + */ +void test_parser_with_vendor_id_payload(tester_t *tester) +{ + parser_t *parser; + vendor_id_payload_t *vendor_id_payload; + status_t status; + chunk_t vendor_id_chunk, result; + + u_int8_t vendor_id_bytes[] = { + 0x00,0x00,0x00,0x10, /* payload header */ + 0x04,0x05,0x06,0x07,/* 12 Byte data */ + 0x08,0x09,0x0A,0x2B, + 0x0C,0x0D,0x0E,0x0F + }; + + vendor_id_chunk.ptr = vendor_id_bytes; + vendor_id_chunk.len = sizeof(vendor_id_bytes); + + parser = parser_create(vendor_id_chunk); + tester->assert_true(tester,(parser != NULL), "parser create check"); + status = parser->parse_payload(parser, VENDOR_ID, (payload_t**)&vendor_id_payload); + tester->assert_true(tester,(status == SUCCESS),"parse_payload call check"); + parser->destroy(parser); + + if (status != SUCCESS) + { + return; + } + result = vendor_id_payload->get_data(vendor_id_payload); + tester->assert_true(tester,(result.len == 12), "parsed data lenght"); + tester->assert_false(tester,(memcmp(vendor_id_bytes + 4, result.ptr, result.len)), "parsed data"); + tester->assert_true(tester,(((payload_t *)vendor_id_payload)->verify((payload_t *)vendor_id_payload) == SUCCESS), "verify check"); + + vendor_id_payload->destroy(vendor_id_payload); +} + + diff --git a/Source/charon/testcases/parser_test.h b/Source/charon/testcases/parser_test.h index 0de800516..33df377e2 100644 --- a/Source/charon/testcases/parser_test.h +++ b/Source/charon/testcases/parser_test.h @@ -135,4 +135,14 @@ void test_parser_with_certreq_payload(tester_t *tester); */ void test_parser_with_delete_payload(tester_t *tester); +/** + * @brief Test function used to test the parser_t functionality when + * parsing a VENDOR ID payload. + * + * @param tester associated tester_t object + * + * @ingroup testcases + */ +void test_parser_with_vendor_id_payload(tester_t *tester); + #endif /*PARSER_TEST_H_*/ diff --git a/Source/charon/testcases/testcases.c b/Source/charon/testcases/testcases.c index 3226126d6..3e3c850a3 100644 --- a/Source/charon/testcases/testcases.c +++ b/Source/charon/testcases/testcases.c @@ -92,6 +92,7 @@ test_t generator_test11 = {test_generator_with_ts_payload,"Generator: TS Payload test_t generator_test12 = {test_generator_with_cert_payload,"Generator: CERT Payload"}; test_t generator_test13 = {test_generator_with_certreq_payload,"Generator: CERTREQ Payload"}; test_t generator_test14 = {test_generator_with_delete_payload,"Generator: DELETE Payload"}; +test_t generator_test15 = {test_generator_with_vendor_id_payload,"Generator: VENDOR ID Payload"}; test_t parser_test1 = {test_parser_with_header_payload, "Parser: header payload"}; test_t parser_test2 = {test_parser_with_sa_payload, "Parser: sa payload"}; test_t parser_test3 = {test_parser_with_nonce_payload, "Parser: nonce payload"}; @@ -103,6 +104,7 @@ test_t parser_test8 = {test_parser_with_ts_payload, "Parser: TS payload"}; test_t parser_test9 = {test_parser_with_cert_payload, "Parser: CERT payload"}; test_t parser_test10 = {test_parser_with_certreq_payload, "Parser: CERTREQ payload"}; test_t parser_test11 = {test_parser_with_delete_payload, "Parser: DELETE payload"}; +test_t parser_test12 = {test_parser_with_vendor_id_payload, "Parser: VENDOR ID payload"}; test_t packet_test = {test_packet,"Packet"}; test_t diffie_hellman_test = {test_diffie_hellman,"Diffie Hellman"}; test_t sha1_hasher_test = {test_sha1_hasher,"SHA1 hasher"}; @@ -195,6 +197,7 @@ int main() &parser_test9, &parser_test10, &parser_test11, + &parser_test12, &generator_test3, &generator_test4, &generator_test5, @@ -207,6 +210,7 @@ int main() &generator_test12, &generator_test13, &generator_test14, + &generator_test15, &ike_sa_manager_test, &packet_test, &diffie_hellman_test, @@ -237,8 +241,8 @@ int main() tester_t *tester = tester_create(test_output, FALSE); -// tester->perform_tests(tester,all_tests); - tester->perform_test(tester,&parser_test11); + tester->perform_tests(tester,all_tests); +// tester->perform_test(tester,&parser_test12); tester->destroy(tester);