- implemented and tested cert_payload_t
authorJan Hutter <jhutter@hsr.ch>
Mon, 5 Dec 2005 12:54:49 +0000 (12:54 -0000)
committerJan Hutter <jhutter@hsr.ch>
Mon, 5 Dec 2005 12:54:49 +0000 (12:54 -0000)
Source/charon/encoding/generator.c
Source/charon/encoding/parser.c
Source/charon/encoding/payloads/Makefile.payloads
Source/charon/encoding/payloads/encodings.c
Source/charon/encoding/payloads/encodings.h
Source/charon/encoding/payloads/payload.c
Source/charon/testcases/generator_test.c
Source/charon/testcases/generator_test.h
Source/charon/testcases/parser_test.c
Source/charon/testcases/parser_test.h
Source/charon/testcases/testcases.c

index b50e7ff..1eb2869 100644 (file)
@@ -43,6 +43,7 @@
 #include <encoding/payloads/id_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>
 
 
@@ -744,7 +745,7 @@ static void generate_payload (private_generator_t *this,payload_t *payload)
                                u_int16_t int16_val = htons(length_of_ke_payload);                      
                                this->write_bytes_to_buffer_at_offset(this,&int16_val,sizeof(u_int16_t),payload_length_position_offset);
                                break;
-                       }               
+                       }
                        case NOTIFICATION_DATA:
                        {
                                /* the Notification Data value is generated from chunk */
@@ -800,17 +801,30 @@ static void generate_payload (private_generator_t *this,payload_t *payload)
                        }
                        case CERT_DATA:
                        {
-                               /* the AUTH Data value is generated from chunk */
+                               /* the CERT Data value is generated from chunk */
                                this->generate_from_chunk(this, rules[i].offset);
                                
                                u_int32_t payload_length_position_offset = this->last_payload_length_position_offset;
-                               /* Length of nonce PAYLOAD is calculated */
+                               /* Length of PAYLOAD is calculated */
                                u_int16_t length_of_cert_payload = CERT_PAYLOAD_HEADER_LENGTH + ((chunk_t *)(this->data_struct + rules[i].offset))->len;
                                u_int16_t int16_val = htons(length_of_cert_payload);
 
                                this->write_bytes_to_buffer_at_offset(this,&int16_val,sizeof(u_int16_t),payload_length_position_offset);
                                break;
                        }
+                       case CERTREQ_DATA:
+                       {
+                               /* the CERTREQ Data value is generated from chunk */
+                               this->generate_from_chunk(this, rules[i].offset);
+                               
+                               u_int32_t payload_length_position_offset = this->last_payload_length_position_offset;
+                               /* Length of PAYLOAD is calculated */
+                               u_int16_t length_of_certreq_payload = CERTREQ_PAYLOAD_HEADER_LENGTH + ((chunk_t *)(this->data_struct + rules[i].offset))->len;
+                               u_int16_t int16_val = htons(length_of_certreq_payload);
+
+                               this->write_bytes_to_buffer_at_offset(this,&int16_val,sizeof(u_int16_t),payload_length_position_offset);
+                               break;
+                       }
                        case PROPOSALS:
                        {
                                /* before iterative generate the transforms, store the current payload length position */
index 1b1c136..e57bee3 100644 (file)
@@ -44,6 +44,7 @@
 #include <encoding/payloads/encryption_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>
 
 
@@ -838,6 +839,16 @@ static status_t parse_payload(private_parser_t *this, payload_type_t payload_typ
                                }               
                                break;                  
                        }
+                       case CERTREQ_DATA:
+                       {
+                               size_t data_length = payload_length - CERTREQ_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;
index 1fe6517..5ba6b53 100644 (file)
@@ -46,6 +46,9 @@ OBJS+= $(BUILD_DIR)cert_payload.o
 $(BUILD_DIR)cert_payload.o :                   $(PAYLOADS_DIR)cert_payload.c $(PAYLOADS_DIR)cert_payload.h
                                                                                $(CC) $(CFLAGS) -c -o $@ $<
 
+OBJS+= $(BUILD_DIR)certreq_payload.o
+$(BUILD_DIR)certreq_payload.o :                        $(PAYLOADS_DIR)certreq_payload.c $(PAYLOADS_DIR)certreq_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
                                                                                $(CC) $(CFLAGS) -c -o $@ $<
index 5d95212..58887d8 100644 (file)
@@ -54,5 +54,7 @@ mapping_t encoding_type_m[] = {
        {TS_TYPE, "TS_TYPE"},
        {ADDRESS, "ADDRESS"},
        {TRAFFIC_SELECTORS, "TRAFFIC_SELECTORS"},
+       {CERT_DATA, "CERT_DATA"},
+       {CERTREQ_DATA, "CERTREQ_DATA"},
        {MAPPING_END, NULL}
 };
index 33610f1..fe76104 100644 (file)
@@ -379,6 +379,16 @@ enum encoding_type_t{
        CERT_DATA,
 
        /**
+        * Representating a CERTREQ Data field.
+        * 
+        * When generating the content of the chunkt pointing to 
+        * is written.
+        * 
+        * When parsing (Payload Length - 5) bytes are read and written into the chunk pointing to.
+        */
+       CERTREQ_DATA,
+
+       /**
         * Representating an IKE_SPI field in an IKEv2 Header.
         * 
         * When generating the value of the u_int64_t pointing to 
index 7e64993..bf0134c 100644 (file)
@@ -32,6 +32,7 @@
 #include <encoding/payloads/notify_payload.h>
 #include <encoding/payloads/auth_payload.h>
 #include <encoding/payloads/cert_payload.h>
+#include <encoding/payloads/certreq_payload.h>
 #include <encoding/payloads/encryption_payload.h>
 #include <encoding/payloads/ts_payload.h>
 
@@ -91,6 +92,8 @@ payload_t *payload_create(payload_type_t type)
                        return (payload_t*)auth_payload_create();
                case CERTIFICATE:
                        return (payload_t*)cert_payload_create();
+               case CERTIFICATE_REQUEST:
+                       return (payload_t*)certreq_payload_create();
                case TRAFFIC_SELECTOR_SUBSTRUCTURE:
                        return (payload_t*)traffic_selector_substructure_create();
                case TRAFFIC_SELECTOR_INITIATOR:
index de0d135..bb40b00 100644 (file)
@@ -41,6 +41,7 @@
 #include <encoding/payloads/id_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>
 
 /*
@@ -1056,6 +1057,90 @@ void test_generator_with_auth_payload(tester_t *tester)
 /*
  * Described in header.
  */ 
+void test_generator_with_ts_payload(tester_t *tester)
+{
+       generator_t *generator;
+       ts_payload_t *ts_payload;
+       traffic_selector_substructure_t *ts1, *ts2;
+       host_t *start_host1, *start_host2, *end_host1, *end_host2;
+       logger_t *logger;
+       chunk_t generated_data;
+       
+       logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"Message with TS Payload");
+       
+       /* create generator */
+       generator = generator_create();
+       tester->assert_true(tester,(generator != NULL), "generator create check");
+
+       ts_payload = ts_payload_create(TRUE);
+       
+       /* first traffic selector */
+       ts1 = traffic_selector_substructure_create();
+       
+       start_host1 = host_create(AF_INET,"192.168.1.0",500);
+       ts1->set_start_host(ts1,start_host1);
+       start_host1->destroy(start_host1);
+
+       end_host1 = host_create(AF_INET,"192.168.1.255",500);
+       ts1->set_end_host(ts1,end_host1);
+       end_host1->destroy(end_host1);
+
+       ts_payload->add_traffic_selector_substructure(ts_payload,ts1);
+
+       /* second traffic selector */
+
+       ts2 = traffic_selector_substructure_create();
+       
+       start_host2 = host_create(AF_INET,"0.0.0.0",0);
+       ts2->set_start_host(ts2,start_host2);
+       ts2->set_protocol_id(ts2,3);
+       start_host2->destroy(start_host2);
+
+       end_host2 = host_create(AF_INET,"255.255.255.255",65535);
+       ts2->set_end_host(ts2,end_host2);
+       end_host2->destroy(end_host2);
+
+       ts_payload->add_traffic_selector_substructure(ts_payload,ts2);
+
+       
+       generator->generate_payload(generator,(payload_t *)ts_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,0x28,
+               0x02,0x00,0x00,0x00,
+               
+               /* traffic selector 1 */
+               0x07,0x00,0x00,0x10,
+               0x01,0xF4,0x01,0xF4,
+               0xC0,0xA8,0x01,0x00,
+               0xC0,0xA8,0x01,0xFF,
+
+               /* traffic selector 2 */
+               0x07,0x03,0x00,0x10,
+               0x00,0x00,0xFF,0xFF,
+               0x00,0x00,0x00,0x00,
+               0xFF,0xFF,0xFF,0xFF,                    
+       };
+       
+       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);
+       
+       ts_payload->destroy(ts_payload);
+       generator->destroy(generator);
+               
+       charon->logger_manager->destroy_logger(charon->logger_manager,logger);  
+}
+
+/*
+ * Described in header.
+ */ 
 void test_generator_with_cert_payload(tester_t *tester)
 {
        generator_t *generator;
@@ -1108,73 +1193,41 @@ void test_generator_with_cert_payload(tester_t *tester)
 /*
  * Described in header.
  */ 
-void test_generator_with_ts_payload(tester_t *tester)
+void test_generator_with_certreq_payload(tester_t *tester)
 {
        generator_t *generator;
-       ts_payload_t *ts_payload;
-       traffic_selector_substructure_t *ts1, *ts2;
-       host_t *start_host1, *start_host2, *end_host1, *end_host2;
+       certreq_payload_t *certreq_payload;
        logger_t *logger;
        chunk_t generated_data;
+       chunk_t certreq;
        
-       logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"Message with TS Payload");
+       logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"Message with CERT Payload");
        
        /* create generator */
        generator = generator_create();
        tester->assert_true(tester,(generator != NULL), "generator create check");
-
-       ts_payload = ts_payload_create(TRUE);
        
-       /* first traffic selector */
-       ts1 = traffic_selector_substructure_create();
+       certreq_payload = certreq_payload_create();
        
-       start_host1 = host_create(AF_INET,"192.168.1.0",500);
-       ts1->set_start_host(ts1,start_host1);
-       start_host1->destroy(start_host1);
-
-       end_host1 = host_create(AF_INET,"192.168.1.255",500);
-       ts1->set_end_host(ts1,end_host1);
-       end_host1->destroy(end_host1);
-
-       ts_payload->add_traffic_selector_substructure(ts_payload,ts1);
-
-       /* second traffic selector */
-
-       ts2 = traffic_selector_substructure_create();
        
-       start_host2 = host_create(AF_INET,"0.0.0.0",0);
-       ts2->set_start_host(ts2,start_host2);
-       ts2->set_protocol_id(ts2,3);
-       start_host2->destroy(start_host2);
-
-       end_host2 = host_create(AF_INET,"255.255.255.255",65535);
-       ts2->set_end_host(ts2,end_host2);
-       end_host2->destroy(end_host2);
-
-       ts_payload->add_traffic_selector_substructure(ts_payload,ts2);
+       certreq.ptr = "123456789012";
+       certreq.len = strlen(certreq.ptr);
 
+       certreq_payload->set_cert_encoding(certreq_payload,PGP_CERTIFICATE);
+       certreq_payload->set_data(certreq_payload,certreq);
        
-       generator->generate_payload(generator,(payload_t *)ts_payload);
+       generator->generate_payload(generator,(payload_t *)certreq_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,0x28,
-               0x02,0x00,0x00,0x00,
-               
-               /* traffic selector 1 */
-               0x07,0x00,0x00,0x10,
-               0x01,0xF4,0x01,0xF4,
-               0xC0,0xA8,0x01,0x00,
-               0xC0,0xA8,0x01,0xFF,
-
-               /* traffic selector 2 */
-               0x07,0x03,0x00,0x10,
-               0x00,0x00,0xFF,0xFF,
-               0x00,0x00,0x00,0x00,
-               0xFF,0xFF,0xFF,0xFF,                    
+               0x00,0x00,0x00,0x11,
+               0x02,
+               /* certreq 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));       
@@ -1183,7 +1236,7 @@ void test_generator_with_ts_payload(tester_t *tester)
 
        allocator_free_chunk(&generated_data);
        
-       ts_payload->destroy(ts_payload);
+       certreq_payload->destroy(certreq_payload);
        generator->destroy(generator);
                
        charon->logger_manager->destroy_logger(charon->logger_manager,logger);  
index d5e3aab..4ea7258 100644 (file)
@@ -117,6 +117,15 @@ void test_generator_with_id_payload(tester_t *tester);
 void test_generator_with_auth_payload(tester_t *tester);
 
 /**
+ * @brief Test function used to test the generator with TS payload.
+ * 
+ * @param tester associated tester_t object
+ * 
+ * @ingroup testcases
+ */
+void test_generator_with_ts_payload(tester_t *tester);
+
+/**
  * @brief Test function used to test the generator with CERT payload.
  * 
  * @param tester associated tester_t object
@@ -125,14 +134,15 @@ void test_generator_with_auth_payload(tester_t *tester);
  */
 void test_generator_with_cert_payload(tester_t *tester);
 
+
 /**
- * @brief Test function used to test the generator with TS payload.
+ * @brief Test function used to test the generator with CERTREQ payload.
  * 
  * @param tester associated tester_t object
  * 
  * @ingroup testcases
  */
-void test_generator_with_ts_payload(tester_t *tester);
+void test_generator_with_certreq_payload(tester_t *tester);
 
 
 #endif /*GENERATOR_TEST_H_*/
index d3ec5dd..2de5806 100644 (file)
@@ -37,6 +37,7 @@
 #include <encoding/payloads/notify_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>
 
 
@@ -732,3 +733,42 @@ void test_parser_with_cert_payload(tester_t *tester)
        allocator_free_chunk(&result);
 }
 
+/*
+ * Described in Header 
+ */
+void test_parser_with_certreq_payload(tester_t *tester)
+{
+       parser_t *parser;
+       certreq_payload_t *certreq_payload;
+       status_t status;
+       chunk_t certreq_chunk, result;
+       
+       u_int8_t certreq_bytes[] = {
+               0x00,0x00,0x00,0x11, /* payload header */
+               0x03,
+                       0x04,0x05,0x06,0x07,/* 12 Byte data */
+                       0x08,0x09,0x0A,0x2B,
+                       0x0C,0x0D,0x0E,0x0F
+       };
+       
+       certreq_chunk.ptr = certreq_bytes;
+       certreq_chunk.len = sizeof(certreq_bytes);
+
+       parser = parser_create(certreq_chunk);
+       tester->assert_true(tester,(parser != NULL), "parser create check");
+       status = parser->parse_payload(parser, CERTIFICATE_REQUEST, (payload_t**)&certreq_payload);
+       tester->assert_true(tester,(status == SUCCESS),"parse_payload call check");
+       parser->destroy(parser);
+       
+       if (status != SUCCESS)
+       {
+               return; 
+       }
+       result = certreq_payload->get_data_clone(certreq_payload);
+       tester->assert_true(tester,(certreq_payload->get_cert_encoding(certreq_payload) == DNS_SIGNED_KEY), "is DNS_SIGNED_KEY encoding");
+       tester->assert_true(tester,(result.len == 12), "parsed data lenght");
+       tester->assert_false(tester,(memcmp(certreq_bytes + 5, result.ptr, result.len)), "parsed data");
+       certreq_payload->destroy(certreq_payload);
+       allocator_free_chunk(&result);
+}
+
index 0db5cc2..f0a79b8 100644 (file)
@@ -115,4 +115,14 @@ void test_parser_with_ts_payload(tester_t *tester);
  */
 void test_parser_with_cert_payload(tester_t *tester);
 
+/**
+ * @brief Test function used to test the parser_t functionality when 
+ * parsing a CERTREQ payload.
+ *
+ * @param tester       associated tester_t object
+ * 
+ * @ingroup testcases
+ */
+void test_parser_with_certreq_payload(tester_t *tester);
+
 #endif /*PARSER_TEST_H_*/
index 26c9862..849e8b4 100644 (file)
@@ -90,6 +90,7 @@ test_t generator_test9 = {test_generator_with_id_payload,"Generator: ID Payload"
 test_t generator_test10 = {test_generator_with_auth_payload,"Generator: AUTH Payload"};
 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 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"};
@@ -99,6 +100,7 @@ test_t parser_test6 = {test_parser_with_id_payload, "Parser: ID payload"};
 test_t parser_test7 = {test_parser_with_auth_payload, "Parser: AUTH payload"};
 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 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"};
@@ -189,6 +191,7 @@ int main()
                &parser_test7,
                &parser_test8,
                &parser_test9,
+               &parser_test10,
                &generator_test3,
                &generator_test4,
                &generator_test5,
@@ -199,6 +202,7 @@ int main()
                &generator_test10,
                &generator_test11,
                &generator_test12,
+               &generator_test13,
                &ike_sa_manager_test,
                &packet_test,
                &diffie_hellman_test,
@@ -229,8 +233,8 @@ int main()
        tester_t *tester = tester_create(test_output, FALSE);
        
 
-       tester->perform_tests(tester,all_tests);
-//     tester->perform_test(tester,&parser_test9); 
+//     tester->perform_tests(tester,all_tests);
+       tester->perform_test(tester,&generator_test13); 
        
        
        tester->destroy(tester);