- implemented and tested cert_payload_t
[strongswan.git] / Source / charon / testcases / generator_test.c
1 /**
2 * @file generator_test.c
3 *
4 * @brief Tests for the generator_t class.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
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>.
16 *
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
20 * for more details.
21 */
22
23 #include <string.h>
24
25 #include "generator_test.h"
26
27 #include <daemon.h>
28 #include <encoding/generator.h>
29 #include <utils/allocator.h>
30 #include <utils/logger_manager.h>
31 #include <utils/logger.h>
32 #include <encoding/payloads/encodings.h>
33 #include <encoding/payloads/ike_header.h>
34 #include <encoding/payloads/transform_attribute.h>
35 #include <encoding/payloads/transform_substructure.h>
36 #include <encoding/payloads/proposal_substructure.h>
37 #include <encoding/payloads/sa_payload.h>
38 #include <encoding/payloads/ke_payload.h>
39 #include <encoding/payloads/notify_payload.h>
40 #include <encoding/payloads/nonce_payload.h>
41 #include <encoding/payloads/id_payload.h>
42 #include <encoding/payloads/auth_payload.h>
43 #include <encoding/payloads/cert_payload.h>
44 #include <encoding/payloads/certreq_payload.h>
45 #include <encoding/payloads/ts_payload.h>
46
47 /*
48 * Described in Header
49 */
50 void test_generator_with_header_payload(tester_t *tester)
51 {
52 generator_t *generator;
53 ike_header_t *header_data;
54 chunk_t generated_data;
55 logger_t *logger;
56
57 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"header payload");
58
59 header_data = ike_header_create();
60 header_data->set_initiator_spi(header_data,1);
61 header_data->set_responder_spi(header_data,2);
62 ((payload_t *) header_data)->set_next_type((payload_t *) header_data, 3);
63 header_data->set_exchange_type(header_data, 6);
64 header_data->set_initiator_flag(header_data, TRUE);
65 header_data->set_response_flag(header_data, TRUE);
66 header_data->set_message_id(header_data,7);
67
68 generator = generator_create();
69 tester->assert_true(tester,(generator != NULL), "generator create check");
70
71 generator->generate_payload(generator,(payload_t *) header_data);
72
73 generator->write_to_chunk(generator,&generated_data);
74
75 u_int8_t expected_generation[] = {
76 0x01,0x00,0x00,0x00,
77 0x00,0x00,0x00,0x00,
78 0x02,0x00,0x00,0x00,
79 0x00,0x00,0x00,0x00,
80 0x03,0x20,0x06,0x28,
81 0x00,0x00,0x00,0x07,
82 0x00,0x00,0x00,0x1C,
83 };
84
85 logger->log_bytes(logger,RAW,"expected header",expected_generation,sizeof(expected_generation));
86 tester->assert_true(tester,(generated_data.len == sizeof(expected_generation)), "compare generated data length");
87 logger->log_chunk(logger,RAW,"generated header",&generated_data);
88 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data 1");
89 allocator_free_chunk(&generated_data);
90
91 generator->destroy(generator);
92
93 header_data->set_initiator_spi(header_data,0x22000054231234LL);
94 header_data->set_responder_spi(header_data,0x122398);
95 ((payload_t *) header_data)->set_next_type((payload_t *) header_data,0xF3);
96 header_data->set_exchange_type(header_data, 0x12);
97 header_data->set_initiator_flag(header_data, TRUE);
98 header_data->set_response_flag(header_data, TRUE);
99 header_data->set_message_id(header_data,0x33AFF3);
100
101 generator = generator_create();
102 tester->assert_true(tester,(generator != NULL), "generator create check");
103
104 generator->generate_payload(generator,(payload_t *)header_data);
105
106 generator->write_to_chunk(generator,&generated_data);
107
108 u_int8_t expected_generation2[] = {
109 0x34,0x12,0x23,0x54,
110 0x00,0x00,0x22,0x00,
111 0x98,0x23,0x12,0x00,
112 0x00,0x00,0x00,0x00,
113 0xF3,0x20,0x12,0x28,
114 0x00,0x33,0xAF,0xF3,
115 0x00,0x00,0x00,0x1C,
116 };
117
118
119 logger->log_bytes(logger,RAW,"expected header",expected_generation2,sizeof(expected_generation2));
120
121 logger->log_chunk(logger,RAW,"generated header",&generated_data);
122
123 tester->assert_true(tester,(memcmp(expected_generation2,generated_data.ptr,sizeof(expected_generation2)) == 0), "compare generated data 2");
124 allocator_free_chunk(&generated_data);
125
126 header_data->destroy(header_data);
127
128 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
129 generator->destroy(generator);
130 }
131
132 /*
133 * Described in header
134 */
135 void test_generator_with_transform_attribute(tester_t *tester)
136 {
137 generator_t *generator;
138 transform_attribute_t *attribute;
139 chunk_t generated_data;
140 logger_t *logger;
141
142 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"transform_attribute payload");
143
144
145 /* test empty attribute */
146 generator = generator_create();
147 tester->assert_true(tester,(generator != NULL), "generator create check");
148 attribute = transform_attribute_create();
149 generator->generate_payload(generator,(payload_t *)attribute);
150 generator->write_to_chunk(generator,&generated_data);
151 logger->log_chunk(logger,RAW,"generated attribute",&generated_data);
152
153 u_int8_t expected_generation[] = {
154 0x80,0x00,0x00,0x00,
155 };
156 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
157 allocator_free_chunk(&generated_data);
158 attribute->destroy(attribute);
159 generator->destroy(generator);
160
161 /* test attribute with 2 byte data */
162 generator = generator_create();
163 tester->assert_true(tester,(generator != NULL), "generator create check");
164
165 attribute = transform_attribute_create();
166 u_int16_t dataval = 5768;
167 chunk_t data;
168 data.ptr = (void *) &dataval;
169 data.len = 2;
170
171 attribute->set_value_chunk(attribute,data);
172
173 generator->generate_payload(generator,(payload_t *)attribute);
174 generator->write_to_chunk(generator,&generated_data);
175 logger->log_chunk(logger,RAW,"generated attribute",&generated_data);
176
177 u_int8_t expected_generation2[] = {
178 0x80,0x00,0x16,0x88,
179 };
180 tester->assert_true(tester,(memcmp(expected_generation2,generated_data.ptr,sizeof(expected_generation2)) == 0), "compare generated data");
181
182 allocator_free_chunk(&generated_data);
183 attribute->destroy(attribute);
184 generator->destroy(generator);
185
186
187
188 /* test attribute with 25 byte data */
189 generator = generator_create();
190 tester->assert_true(tester,(generator != NULL), "generator create check");
191
192 attribute = transform_attribute_create();
193 char *stringval = "ddddddddddeeeeeeeeeefffff";
194 data.ptr = (void *) stringval;
195 data.len = 25;
196
197 attribute->set_value_chunk(attribute,data);
198
199 attribute->set_attribute_type(attribute,456);
200
201
202 generator->generate_payload(generator,(payload_t *)attribute);
203 generator->write_to_chunk(generator,&generated_data);
204 logger->log_chunk(logger,RAW,"generated attribute",&generated_data);
205
206 u_int8_t expected_generation3[] = {
207 0x01,0xC8,0x00,0x19,
208 0x64,0x64,0x64,0x64,
209 0x64,0x64,0x64,0x64,
210 0x64,0x64,0x65,0x65,
211 0x65,0x65,0x65,0x65,
212 0x65,0x65,0x65,0x65,
213 0x66,0x66,0x66,0x66,
214 0x66
215 };
216 tester->assert_true(tester,(memcmp(expected_generation3,generated_data.ptr,sizeof(expected_generation3)) == 0), "compare generated data");
217
218 allocator_free_chunk(&generated_data);
219 attribute->destroy(attribute);
220 generator->destroy(generator);
221
222
223 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
224 }
225
226
227
228 /*
229 * Described in header
230 */
231 void test_generator_with_transform_substructure(tester_t *tester)
232 {
233 generator_t *generator;
234 transform_attribute_t *attribute1, *attribute2;
235 transform_substructure_t *transform;
236 chunk_t data;
237 chunk_t generated_data;
238 logger_t *logger;
239
240 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"transform substr.");
241
242 /* create generator */
243 generator = generator_create();
244 tester->assert_true(tester,(generator != NULL), "generator create check");
245
246 /* create attribute 1 */
247 attribute1 = transform_attribute_create();
248 char *stringval = "abcd";
249 data.ptr = (void *) stringval;
250 data.len = 4;
251 attribute1->set_value_chunk(attribute1,data);
252 attribute1->set_attribute_type(attribute1,0);
253 logger->log(logger,CONTROL,"attribute1 created");
254
255 /* create attribute 2 */
256 attribute2 = transform_attribute_create();
257 stringval = "efgh";
258 data.ptr = (void *) stringval;
259 data.len = 4;
260 attribute2->set_value_chunk(attribute2,data);
261 attribute2->set_attribute_type(attribute2,0);
262 logger->log(logger,CONTROL,"attribute2 created");
263
264 /* create transform */
265 transform = transform_substructure_create();
266 tester->assert_true(tester,(transform != NULL), "transform create check");
267 transform->add_transform_attribute(transform,attribute1);
268 transform->add_transform_attribute(transform,attribute2);
269 transform->set_transform_type(transform,5); /* hex 5 */
270 transform->set_transform_id(transform,65000); /* hex FDE8 */
271
272
273 logger->log(logger,CONTROL,"transform created");
274
275 generator->generate_payload(generator,(payload_t *)transform);
276 generator->write_to_chunk(generator,&generated_data);
277 logger->log_chunk(logger,RAW,"generated transform",&generated_data);
278
279 u_int8_t expected_generation3[] = {
280 0x00,0x00,0x00,0x18,
281 0x05,0x00,0xFD,0xE8,
282 0x00,0x00,0x00,0x04,
283 0x61,0x62,0x63,0x64,
284 0x00,0x00,0x00,0x04,
285 0x65,0x66,0x67,0x68,
286 };
287 tester->assert_true(tester,(memcmp(expected_generation3,generated_data.ptr,sizeof(expected_generation3)) == 0), "compare generated data");
288
289 allocator_free_chunk(&generated_data);
290 transform->destroy(transform);
291 generator->destroy(generator);
292
293
294 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
295 }
296
297
298 /*
299 * Described in header
300 */
301 void test_generator_with_proposal_substructure(tester_t *tester)
302 {
303 generator_t *generator;
304 transform_attribute_t *attribute1, *attribute2, *attribute3;
305 transform_substructure_t *transform1, *transform2;
306 proposal_substructure_t *proposal;
307 chunk_t data;
308 chunk_t generated_data;
309 logger_t *logger;
310
311 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"transform substr.");
312
313 /* create generator */
314 generator = generator_create();
315 tester->assert_true(tester,(generator != NULL), "generator create check");
316
317 /* create attribute 1 */
318 attribute1 = transform_attribute_create();
319 char *stringval = "abcd";
320 data.ptr = (void *) stringval;
321 data.len = 4;
322 attribute1->set_value_chunk(attribute1,data);
323 attribute1->set_attribute_type(attribute1,0);
324
325 logger->log(logger,CONTROL,"attribute1 created");
326
327 /* create attribute 2 */
328 attribute2 = transform_attribute_create();
329 stringval = "efgh";
330 data.ptr = (void *) stringval;
331 data.len = 4;
332 attribute2->set_value_chunk(attribute2,data);
333 attribute2->set_attribute_type(attribute2,0);
334 logger->log(logger,CONTROL,"attribute2 created");
335
336 /* create attribute 3 */
337 attribute3 = transform_attribute_create();
338 stringval = "ijkl";
339 data.ptr = (void *) stringval;
340 data.len = 4;
341 attribute3->set_value_chunk(attribute3,data);
342 attribute3->set_attribute_type(attribute3,0);
343 logger->log(logger,CONTROL,"attribute3 created");
344
345 /* create transform 1*/
346 transform1 = transform_substructure_create();
347 tester->assert_true(tester,(transform1 != NULL), "transform create check");
348 transform1->add_transform_attribute(transform1,attribute1);
349 transform1->add_transform_attribute(transform1,attribute2);
350 transform1->set_transform_type(transform1,5); /* hex 5 */
351 transform1->set_transform_id(transform1,65000); /* hex FDE8 */
352
353 /* create transform 2*/
354 transform2 = transform_substructure_create();
355 tester->assert_true(tester,(transform2 != NULL), "transform create check");
356 transform2->add_transform_attribute(transform2,attribute3);
357 transform2->set_transform_type(transform2,3); /* hex 3 */
358 transform2->set_transform_id(transform2,4); /* hex 4 */
359
360 logger->log(logger,CONTROL,"transforms created");
361
362 proposal = proposal_substructure_create();
363 tester->assert_true(tester,(proposal != NULL), "proposal create check");
364
365 stringval = "ABCDEFGH";
366 data.ptr = (void *) stringval;
367 data.len = 8;
368
369 proposal->add_transform_substructure(proposal,transform1);
370 proposal->add_transform_substructure(proposal,transform2);
371 proposal->set_spi(proposal,data);
372 proposal->set_proposal_number(proposal,7);
373 proposal->set_protocol_id(proposal,4);
374
375 generator->generate_payload(generator,(payload_t *)proposal);
376 generator->write_to_chunk(generator,&generated_data);
377 logger->log_chunk(logger,RAW,"generated transform",&generated_data);
378
379 u_int8_t expected_generation[] = {
380 /* proposal header */
381 0x00,0x00,0x00,0x38,
382 0x07,0x04,0x08,0x02,
383 /* SPI */
384 0x41,0x42,0x43,0x44,
385 0x45,0x46,0x47,0x48,
386 /* first transform */
387 0x03,0x00,0x00,0x18,
388 0x05,0x00,0xFD,0xE8,
389 /* first transform attributes */
390 0x00,0x00,0x00,0x04,
391 0x61,0x62,0x63,0x64,
392 0x00,0x00,0x00,0x04,
393 0x65,0x66,0x67,0x68,
394 /* second transform */
395 0x00,0x00,0x00,0x10,
396 0x03,0x00,0x00,0x04,
397 /* second transform attributes */
398 0x00,0x00,0x00,0x04,
399 0x69,0x6A,0x6B,0x6C
400 };
401 logger->log_bytes(logger,RAW,"expected transform",expected_generation,sizeof(expected_generation));
402
403 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
404
405 allocator_free_chunk(&generated_data);
406 proposal->destroy(proposal);
407 generator->destroy(generator);
408
409
410 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
411 }
412
413 /*
414 * Described in header
415 */
416 void test_generator_with_sa_payload(tester_t *tester)
417 {
418 generator_t *generator;
419 transform_attribute_t *attribute1, *attribute2, *attribute3;
420 transform_substructure_t *transform1, *transform2;
421 proposal_substructure_t *proposal1, *proposal2;
422 ike_proposal_t *ike_proposals;
423 size_t child_proposal_count;
424 child_proposal_t *child_proposals;
425 size_t ike_proposal_count;
426 sa_payload_t *sa_payload;
427 ike_header_t *ike_header;
428
429 chunk_t data;
430 chunk_t generated_data;
431 logger_t *logger;
432
433 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"Message with SA Payload");
434
435 /* create generator */
436 generator = generator_create();
437 tester->assert_true(tester,(generator != NULL), "generator create check");
438
439 /* --------------------------- */
440 /* test first with self created proposals */
441
442 /* create attribute 1 */
443 attribute1 = transform_attribute_create();
444 char *stringval = "abcd";
445 data.ptr = (void *) stringval;
446 data.len = 4;
447 attribute1->set_value_chunk(attribute1,data);
448 attribute1->set_attribute_type(attribute1,0);
449 logger->log(logger,CONTROL,"attribute1 created");
450
451 /* create attribute 2 */
452 attribute2 = transform_attribute_create();
453 stringval = "efgh";
454 data.ptr = (void *) stringval;
455 data.len = 4;
456 attribute2->set_value_chunk(attribute2,data);
457 attribute2->set_attribute_type(attribute2,0);
458 logger->log(logger,CONTROL,"attribute2 created");
459
460 /* create attribute 3 */
461 attribute3 = transform_attribute_create();
462 stringval = "ijkl";
463 data.ptr = (void *) stringval;
464 data.len = 4;
465 attribute3->set_value_chunk(attribute3,data);
466 attribute3->set_attribute_type(attribute3,0);
467 logger->log(logger,CONTROL,"attribute3 created");
468
469 /* create transform 1*/
470 transform1 = transform_substructure_create();
471 tester->assert_true(tester,(transform1 != NULL), "transform create check");
472 transform1->add_transform_attribute(transform1,attribute1);
473 transform1->add_transform_attribute(transform1,attribute2);
474 transform1->set_transform_type(transform1,5); /* hex 5 */
475 transform1->set_transform_id(transform1,65000); /* hex FDE8 */
476
477 /* create transform 2*/
478 transform2 = transform_substructure_create();
479 tester->assert_true(tester,(transform2 != NULL), "transform create check");
480 transform2->add_transform_attribute(transform2,attribute3);
481 transform2->set_transform_type(transform2,3); /* hex 3 */
482 transform2->set_transform_id(transform2,4); /* hex 4 */
483
484 logger->log(logger,CONTROL,"transforms created");
485
486 /* create proposal 1 */
487 proposal1 = proposal_substructure_create();
488 tester->assert_true(tester,(proposal1 != NULL), "proposal create check");
489
490 stringval = "ABCDEFGH";
491 data.ptr = (void *) stringval;
492 data.len = 8;
493
494 proposal1->add_transform_substructure(proposal1,transform1);
495 proposal1->add_transform_substructure(proposal1,transform2);
496 proposal1->set_spi(proposal1,data);
497 proposal1->set_proposal_number(proposal1,7);
498 proposal1->set_protocol_id(proposal1,4);
499
500 /* create proposal 2 */
501 proposal2 = proposal_substructure_create();
502 tester->assert_true(tester,(proposal2 != NULL), "proposal create check");
503 proposal2->set_proposal_number(proposal2,7);
504 proposal2->set_protocol_id(proposal2,5);
505
506 /* create sa_payload */
507 sa_payload = sa_payload_create();
508
509 sa_payload->add_proposal_substructure(sa_payload,proposal1);
510 sa_payload->add_proposal_substructure(sa_payload,proposal2);
511
512 ike_header = ike_header_create();
513 ike_header->set_initiator_spi(ike_header,0x22000054231234LL);
514 ike_header->set_responder_spi(ike_header,0x122398);
515 ((payload_t *) ike_header)->set_next_type((payload_t *) ike_header,SECURITY_ASSOCIATION);
516 ike_header->set_exchange_type(ike_header, 0x12);
517 ike_header->set_initiator_flag(ike_header, TRUE);
518 ike_header->set_response_flag(ike_header, TRUE);
519 ike_header->set_message_id(ike_header,0x33AFF3);
520
521 generator->generate_payload(generator,(payload_t *)ike_header);
522 generator->generate_payload(generator,(payload_t *)sa_payload);
523 generator->write_to_chunk(generator,&generated_data);
524 logger->log_chunk(logger,RAW,"generated transform",&generated_data);
525
526 u_int8_t expected_generation[] = {
527 /* sa payload header */
528 0x34,0x12,0x23,0x54,
529 0x00,0x00,0x22,0x00,
530 0x98,0x23,0x12,0x00,
531 0x00,0x00,0x00,0x00,
532 0x21,0x20,0x12,0x28,
533 0x00,0x33,0xAF,0xF3,
534 0x00,0x00,0x00,0x60,
535
536 /* sa payload header */
537 0x00,0x00,0x00,0x44,
538 /* proposal header */
539 0x02,0x00,0x00,0x38,
540 0x07,0x04,0x08,0x02,
541 /* SPI */
542 0x41,0x42,0x43,0x44,
543 0x45,0x46,0x47,0x48,
544 /* first transform */
545 0x03,0x00,0x00,0x18,
546 0x05,0x00,0xFD,0xE8,
547 /* first transform attributes */
548 0x00,0x00,0x00,0x04,
549 0x61,0x62,0x63,0x64,
550 0x00,0x00,0x00,0x04,
551 0x65,0x66,0x67,0x68,
552 /* second transform */
553 0x00,0x00,0x00,0x10,
554 0x03,0x00,0x00,0x04,
555 /* second transform attributes */
556 0x00,0x00,0x00,0x04,
557 0x69,0x6A,0x6B,0x6C,
558 /* proposal header 2*/
559 0x00,0x00,0x00,0x08,
560 0x07,0x05,0x00,0x00,
561
562 };
563
564 logger->log_bytes(logger,RAW,"expected transform",expected_generation,sizeof(expected_generation));
565
566 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
567
568 allocator_free_chunk(&generated_data);
569 ike_header->destroy(ike_header);
570 sa_payload->destroy(sa_payload);
571 generator->destroy(generator);
572
573 /* --------------------------- */
574 /* test with automatic created proposals */
575
576 generator = generator_create();
577 tester->assert_true(tester,(generator != NULL), "generator create check");
578
579
580 ike_proposal_count = 2;
581 ike_proposals = allocator_alloc(ike_proposal_count * (sizeof(ike_proposal_t)));
582
583 ike_proposals[0].encryption_algorithm = 1;
584 ike_proposals[0].encryption_algorithm_key_length = 20;
585 ike_proposals[0].pseudo_random_function = 2;
586 ike_proposals[0].pseudo_random_function_key_length = 22;
587 ike_proposals[0].integrity_algorithm = 3;
588 ike_proposals[0].integrity_algorithm_key_length = 24;
589 ike_proposals[0].diffie_hellman_group = 4;
590
591 ike_proposals[1].encryption_algorithm = 5;
592 ike_proposals[1].encryption_algorithm_key_length = 26;
593 ike_proposals[1].pseudo_random_function = 6;
594 ike_proposals[1].pseudo_random_function_key_length = 28;
595 ike_proposals[1].integrity_algorithm = 7;
596 ike_proposals[1].integrity_algorithm_key_length = 30;
597 ike_proposals[1].diffie_hellman_group = 8;
598
599 sa_payload = sa_payload_create_from_ike_proposals(ike_proposals,ike_proposal_count);
600 tester->assert_true(tester,(sa_payload != NULL), "sa_payload create check");
601
602 generator->generate_payload(generator,(payload_t *)sa_payload);
603 generator->write_to_chunk(generator,&generated_data);
604 logger->log_chunk(logger,RAW,"generated",&generated_data);
605
606 u_int8_t expected_generation2[] = {
607 0x00,0x00,0x00,0x6C, /* payload header*/
608 0x02,0x00,0x00,0x34, /* a proposal */
609 0x01,0x01,0x00,0x04,
610 0x03,0x00,0x00,0x0C, /* transform 1 */
611 0x01,0x00,0x00,0x01,
612 0x80,0x0E,0x00,0x14, /* keylength attribute with 20 bytes length */
613 0x03,0x00,0x00,0x0C, /* transform 2 */
614 0x02,0x00,0x00,0x02,
615 0x80,0x0E,0x00,0x16, /* keylength attribute with 20 bytes length */
616 0x03,0x00,0x00,0x0C, /* transform 3 */
617 0x03,0x00,0x00,0x03,
618 0x80,0x0E,0x00,0x18, /* keylength attribute with 20 bytes length */
619 0x00,0x00,0x00,0x08, /* transform 4 */
620 0x04,0x00,0x00,0x04,
621 0x00,0x00,0x00,0x34, /* a proposal */
622 0x02,0x01,0x00,0x04,
623 0x03,0x00,0x00,0x0C, /* transform 1 */
624 0x01,0x00,0x00,0x05,
625 0x80,0x0E,0x00,0x1A, /* keylength attribute with 16 bytes length */
626 0x03,0x00,0x00,0x0C, /* transform 2 */
627 0x02,0x00,0x00,0x06,
628 0x80,0x0E,0x00,0x1C, /* keylength attribute with 16 bytes length */
629 0x03,0x00,0x00,0x0C, /* transform 3 */
630 0x03,0x00,0x00,0x07,
631 0x80,0x0E,0x00,0x1E, /* keylength attribute with 16 bytes length */
632 0x00,0x00,0x00,0x08, /* transform 4 */
633 0x04,0x00,0x00,0x08,
634
635 };
636
637 logger->log_bytes(logger,RAW,"expected",expected_generation2,sizeof(expected_generation2));
638
639 tester->assert_true(tester,(memcmp(expected_generation2,generated_data.ptr,sizeof(expected_generation2)) == 0), "compare generated data");
640
641 sa_payload->destroy(sa_payload);
642 allocator_free(ike_proposals);
643 allocator_free_chunk(&generated_data);
644 generator->destroy(generator);
645
646
647 /* --------------------------- */
648 /* test with automatic created child proposals */
649
650 generator = generator_create();
651 tester->assert_true(tester,(generator != NULL), "generator create check");
652
653
654 child_proposal_count = 2;
655 child_proposals = allocator_alloc(child_proposal_count * (sizeof(child_proposal_t)));
656
657 child_proposals[0].ah.is_set = TRUE;
658 child_proposals[0].ah.integrity_algorithm = AUTH_HMAC_MD5_96;
659 child_proposals[0].ah.integrity_algorithm_key_size = 20;
660 child_proposals[0].ah.diffie_hellman_group = MODP_2048_BIT;
661 child_proposals[0].ah.extended_sequence_numbers = EXT_SEQ_NUMBERS;
662 child_proposals[0].ah.spi[0] = 1;
663 child_proposals[0].ah.spi[1] = 1;
664 child_proposals[0].ah.spi[2] = 1;
665 child_proposals[0].ah.spi[3] = 1;
666
667 child_proposals[0].esp.is_set = TRUE;
668 child_proposals[0].esp.diffie_hellman_group = MODP_1024_BIT;
669 child_proposals[0].esp.encryption_algorithm = ENCR_AES_CBC;
670 child_proposals[0].esp.encryption_algorithm_key_size = 32;
671 child_proposals[0].esp.integrity_algorithm = AUTH_UNDEFINED;
672 child_proposals[0].esp.spi[0] = 2;
673 child_proposals[0].esp.spi[1] = 2;
674 child_proposals[0].esp.spi[2] = 2;
675 child_proposals[0].esp.spi[3] = 2;
676
677 child_proposals[1].ah.is_set = TRUE;
678 child_proposals[1].ah.integrity_algorithm = AUTH_HMAC_MD5_96;
679 child_proposals[1].ah.integrity_algorithm_key_size = 20;
680 child_proposals[1].ah.diffie_hellman_group = MODP_2048_BIT;
681 child_proposals[1].ah.extended_sequence_numbers = EXT_SEQ_NUMBERS;
682 child_proposals[1].ah.spi[0] = 1;
683 child_proposals[1].ah.spi[1] = 1;
684 child_proposals[1].ah.spi[2] = 1;
685 child_proposals[1].ah.spi[3] = 1;
686
687 child_proposals[1].esp.is_set = TRUE;
688 child_proposals[1].esp.diffie_hellman_group = MODP_1024_BIT;
689 child_proposals[1].esp.encryption_algorithm = ENCR_AES_CBC;
690 child_proposals[1].esp.encryption_algorithm_key_size = 32;
691 child_proposals[1].esp.integrity_algorithm = AUTH_HMAC_MD5_96;
692 child_proposals[1].esp.integrity_algorithm_key_size = 20;
693 child_proposals[1].esp.spi[0] = 2;
694 child_proposals[1].esp.spi[1] = 2;
695 child_proposals[1].esp.spi[2] = 2;
696 child_proposals[1].esp.spi[3] = 2;
697
698
699 sa_payload = sa_payload_create_from_child_proposals(child_proposals,child_proposal_count);
700 tester->assert_true(tester,(sa_payload != NULL), "sa_payload create check");
701
702 generator->generate_payload(generator,(payload_t *)sa_payload);
703 generator->write_to_chunk(generator,&generated_data);
704 logger->log_chunk(logger,RAW,"generated",&generated_data);
705
706 u_int8_t expected_generation3[] = {
707 0x00,0x00,0x00,0xA0, /* payload header*/
708
709 /* suite 1 */
710 0x02,0x00,0x00,0x28, /* a proposal */
711 0x01,0x02,0x04,0x03,
712 0x01,0x01,0x01,0x01,
713 0x03,0x00,0x00,0x0C, /* transform 1 */
714 0x03,0x00,0x00,0x01,
715 0x80,0x0E,0x00,0x14, /* keylength attribute with 20 bytes length */
716
717 0x03,0x00,0x00,0x08, /* transform 2 */
718 0x04,0x00,0x00,0x0E,
719
720 0x00,0x00,0x00,0x08, /* transform 3 */
721 0x05,0x00,0x00,0x01,
722
723
724 0x02,0x00,0x00,0x20, /* a proposal */
725 0x01,0x03,0x04,0x02,
726 0x02,0x02,0x02,0x02,
727
728 0x03,0x00,0x00,0x0C, /* transform 1 */
729 0x01,0x00,0x00,0x0C,
730 0x80,0x0E,0x00,0x20, /* keylength attribute with 32 bytes length */
731
732 0x00,0x00,0x00,0x08, /* transform 2 */
733 0x04,0x00,0x00,0x02,
734
735 /* suite 2 */
736 0x02,0x00,0x00,0x28, /* a proposal */
737 0x02,0x02,0x04,0x03,
738 0x01,0x01,0x01,0x01,
739 0x03,0x00,0x00,0x0C, /* transform 1 */
740 0x03,0x00,0x00,0x01,
741 0x80,0x0E,0x00,0x14, /* keylength attribute with 20 bytes length */
742
743 0x03,0x00,0x00,0x08, /* transform 2 */
744 0x04,0x00,0x00,0x0E,
745
746 0x00,0x00,0x00,0x08, /* transform 3 */
747 0x05,0x00,0x00,0x01,
748
749
750 0x00,0x00,0x00,0x2C, /* a proposal */
751 0x02,0x03,0x04,0x03,
752 0x02,0x02,0x02,0x02,
753
754 0x03,0x00,0x00,0x0C, /* transform 1 */
755 0x01,0x00,0x00,0x0C,
756 0x80,0x0E,0x00,0x20, /* keylength attribute with 32 bytes length */
757
758 0x03,0x00,0x00,0x0C, /* transform 2 */
759 0x03,0x00,0x00,0x01,
760 0x80,0x0E,0x00,0x14, /* keylength attribute with 20 bytes length */
761
762 0x00,0x00,0x00,0x08, /* transform 3 */
763 0x04,0x00,0x00,0x02,
764
765 };
766
767
768 logger->log_bytes(logger,RAW,"expected",expected_generation3,sizeof(expected_generation3));
769
770 tester->assert_true(tester,(memcmp(expected_generation3,generated_data.ptr,sizeof(expected_generation3)) == 0), "compare generated data");
771
772 sa_payload->destroy(sa_payload);
773 allocator_free(child_proposals);
774 allocator_free_chunk(&generated_data);
775 generator->destroy(generator);
776
777
778 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
779
780 }
781
782 /*
783 * Described in header
784 */
785 void test_generator_with_ke_payload(tester_t *tester)
786 {
787 generator_t *generator;
788 ke_payload_t *ke_payload;
789 logger_t *logger;
790 chunk_t generated_data;
791 chunk_t key_exchange_data;
792
793 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"Message with KE Payload");
794
795 /* create generator */
796 generator = generator_create();
797 tester->assert_true(tester,(generator != NULL), "generator create check");
798
799 ke_payload = ke_payload_create();
800
801
802 key_exchange_data.ptr = "test-text";
803 key_exchange_data.len = strlen(key_exchange_data.ptr);
804
805 ke_payload->set_key_exchange_data(ke_payload,key_exchange_data);
806
807 ke_payload->set_dh_group_number(ke_payload,7777);
808
809 generator->generate_payload(generator,(payload_t *)ke_payload);
810 generator->write_to_chunk(generator,&generated_data);
811 logger->log_chunk(logger,RAW,"generated payload",&generated_data);
812
813 u_int8_t expected_generation[] = {
814 /* payload header */
815 0x00,0x00,0x00,0x11,
816 0x1E,0x61,0x00,0x00,
817 /* key exchange data */
818 0x74,0x65,0x73,0x74,
819 0x2D,0x74,0x65,0x78,
820 0x74
821 };
822
823
824 logger->log_bytes(logger,RAW,"expected payload",expected_generation,sizeof(expected_generation));
825
826 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
827
828 allocator_free_chunk(&generated_data);
829
830 ke_payload->destroy(ke_payload);
831 generator->destroy(generator);
832
833 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
834
835 }
836
837 /*
838 * Described in header
839 */
840 void test_generator_with_notify_payload(tester_t *tester)
841 {
842 generator_t *generator;
843 notify_payload_t *notify_payload;
844 logger_t *logger;
845 chunk_t generated_data;
846 chunk_t spi,notification_data;
847
848 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"Message with Notify Payload");
849
850 /* create generator */
851 generator = generator_create();
852 tester->assert_true(tester,(generator != NULL), "generator create check");
853
854 notify_payload = notify_payload_create();
855
856
857 spi.ptr = "12345";
858 spi.len = strlen(spi.ptr);
859
860 notification_data.ptr = "67890";
861 notification_data.len = strlen(notification_data.ptr);
862
863 notify_payload->set_protocol_id(notify_payload,255);
864 notify_payload->set_notify_message_type(notify_payload,63333); /* Hex F765 */
865 notify_payload->set_spi(notify_payload,spi);
866 notify_payload->set_notification_data(notify_payload,notification_data);
867
868 generator->generate_payload(generator,(payload_t *)notify_payload);
869 generator->write_to_chunk(generator,&generated_data);
870 logger->log_chunk(logger,RAW,"generated payload",&generated_data);
871
872 u_int8_t expected_generation[] = {
873 /* payload header */
874 0x00,0x00,0x00,0x12,
875 0xFF,0x05,0xF7,0x65,
876 /* spi */
877 0x31,0x32,0x33,0x34,
878 0x35,
879 /* notification data */
880 0x36,0x37,0x38,0x39,
881 0x30,
882 };
883
884 logger->log_bytes(logger,RAW,"expected payload",expected_generation,sizeof(expected_generation));
885
886 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
887
888 allocator_free_chunk(&generated_data);
889
890 notify_payload->destroy(notify_payload);
891 generator->destroy(generator);
892
893 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
894
895 }
896
897 /*
898 * Described in header
899 */
900 void test_generator_with_nonce_payload(tester_t *tester)
901 {
902 generator_t *generator;
903 nonce_payload_t *nonce_payload;
904 logger_t *logger;
905 chunk_t generated_data;
906 chunk_t nonce;
907
908 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"Message with Nonce Payload");
909
910 /* create generator */
911 generator = generator_create();
912 tester->assert_true(tester,(generator != NULL), "generator create check");
913
914 nonce_payload = nonce_payload_create();
915
916
917 nonce.ptr = "1234567890123456";
918 nonce.len = strlen("1234567890123456");
919
920 nonce_payload->set_nonce(nonce_payload,nonce);
921
922 generator->generate_payload(generator,(payload_t *)nonce_payload);
923 generator->write_to_chunk(generator,&generated_data);
924 logger->log_chunk(logger,RAW,"generated payload",&generated_data);
925
926
927 u_int8_t expected_generation[] = {
928 /* payload header */
929 0x00,0x00,0x00,0x14,
930 /* nonce data */
931 0x31,0x32,0x33,0x34,
932 0x35,0x36,0x37,0x38,
933 0x39,0x30,0x31,0x32,
934 0x33,0x34,0x35,0x36
935 };
936
937 logger->log_bytes(logger,RAW,"expected payload",expected_generation,sizeof(expected_generation));
938
939 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
940
941 allocator_free_chunk(&generated_data);
942
943
944 nonce_payload->destroy(nonce_payload);
945 generator->destroy(generator);
946
947 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
948
949 }
950
951 /*
952 * Described in header.
953 */
954 void test_generator_with_id_payload(tester_t *tester)
955 {
956 generator_t *generator;
957 id_payload_t *id_payload;
958 logger_t *logger;
959 chunk_t generated_data;
960 chunk_t id;
961
962 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"Message with ID Payload");
963
964 /* create generator */
965 generator = generator_create();
966 tester->assert_true(tester,(generator != NULL), "generator create check");
967
968 id_payload = id_payload_create(FALSE);
969
970
971 id.ptr = "123456789012";
972 id.len = strlen(id.ptr);
973
974 id_payload->set_id_type(id_payload,ID_IPV4_ADDR);
975 id_payload->set_data(id_payload,id);
976
977 generator->generate_payload(generator,(payload_t *)id_payload);
978 generator->write_to_chunk(generator,&generated_data);
979 logger->log_chunk(logger,RAW,"generated payload",&generated_data);
980
981
982 u_int8_t expected_generation[] = {
983 /* payload header */
984 0x00,0x00,0x00,0x14,
985 0x01,0x00,0x00,0x00,
986 /* id data */
987 0x31,0x32,0x33,0x34,
988 0x35,0x36,0x37,0x38,
989 0x39,0x30,0x31,0x32,
990 };
991
992 logger->log_bytes(logger,RAW,"expected payload",expected_generation,sizeof(expected_generation));
993
994 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
995
996 allocator_free_chunk(&generated_data);
997
998 id_payload->destroy(id_payload);
999 generator->destroy(generator);
1000
1001 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
1002 }
1003
1004 /*
1005 * Described in header.
1006 */
1007 void test_generator_with_auth_payload(tester_t *tester)
1008 {
1009 generator_t *generator;
1010 auth_payload_t *auth_payload;
1011 logger_t *logger;
1012 chunk_t generated_data;
1013 chunk_t auth;
1014
1015 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"Message with AUTH Payload");
1016
1017 /* create generator */
1018 generator = generator_create();
1019 tester->assert_true(tester,(generator != NULL), "generator create check");
1020
1021 auth_payload = auth_payload_create(FALSE);
1022
1023
1024 auth.ptr = "123456789012";
1025 auth.len = strlen(auth.ptr);
1026
1027 auth_payload->set_auth_method(auth_payload,SHARED_KEY_MESSAGE_INTEGRITY_CODE);
1028 auth_payload->set_data(auth_payload,auth);
1029
1030 generator->generate_payload(generator,(payload_t *)auth_payload);
1031 generator->write_to_chunk(generator,&generated_data);
1032 logger->log_chunk(logger,RAW,"generated payload",&generated_data);
1033
1034
1035 u_int8_t expected_generation[] = {
1036 /* payload header */
1037 0x00,0x00,0x00,0x14,
1038 0x02,0x00,0x00,0x00,
1039 /* auth data */
1040 0x31,0x32,0x33,0x34,
1041 0x35,0x36,0x37,0x38,
1042 0x39,0x30,0x31,0x32,
1043 };
1044
1045 logger->log_bytes(logger,RAW,"expected payload",expected_generation,sizeof(expected_generation));
1046
1047 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
1048
1049 allocator_free_chunk(&generated_data);
1050
1051 auth_payload->destroy(auth_payload);
1052 generator->destroy(generator);
1053
1054 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
1055 }
1056
1057 /*
1058 * Described in header.
1059 */
1060 void test_generator_with_ts_payload(tester_t *tester)
1061 {
1062 generator_t *generator;
1063 ts_payload_t *ts_payload;
1064 traffic_selector_substructure_t *ts1, *ts2;
1065 host_t *start_host1, *start_host2, *end_host1, *end_host2;
1066 logger_t *logger;
1067 chunk_t generated_data;
1068
1069 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"Message with TS Payload");
1070
1071 /* create generator */
1072 generator = generator_create();
1073 tester->assert_true(tester,(generator != NULL), "generator create check");
1074
1075 ts_payload = ts_payload_create(TRUE);
1076
1077 /* first traffic selector */
1078 ts1 = traffic_selector_substructure_create();
1079
1080 start_host1 = host_create(AF_INET,"192.168.1.0",500);
1081 ts1->set_start_host(ts1,start_host1);
1082 start_host1->destroy(start_host1);
1083
1084 end_host1 = host_create(AF_INET,"192.168.1.255",500);
1085 ts1->set_end_host(ts1,end_host1);
1086 end_host1->destroy(end_host1);
1087
1088 ts_payload->add_traffic_selector_substructure(ts_payload,ts1);
1089
1090 /* second traffic selector */
1091
1092 ts2 = traffic_selector_substructure_create();
1093
1094 start_host2 = host_create(AF_INET,"0.0.0.0",0);
1095 ts2->set_start_host(ts2,start_host2);
1096 ts2->set_protocol_id(ts2,3);
1097 start_host2->destroy(start_host2);
1098
1099 end_host2 = host_create(AF_INET,"255.255.255.255",65535);
1100 ts2->set_end_host(ts2,end_host2);
1101 end_host2->destroy(end_host2);
1102
1103 ts_payload->add_traffic_selector_substructure(ts_payload,ts2);
1104
1105
1106 generator->generate_payload(generator,(payload_t *)ts_payload);
1107 generator->write_to_chunk(generator,&generated_data);
1108 logger->log_chunk(logger,RAW,"generated payload",&generated_data);
1109
1110
1111 u_int8_t expected_generation[] = {
1112 /* payload header */
1113 0x00,0x00,0x00,0x28,
1114 0x02,0x00,0x00,0x00,
1115
1116 /* traffic selector 1 */
1117 0x07,0x00,0x00,0x10,
1118 0x01,0xF4,0x01,0xF4,
1119 0xC0,0xA8,0x01,0x00,
1120 0xC0,0xA8,0x01,0xFF,
1121
1122 /* traffic selector 2 */
1123 0x07,0x03,0x00,0x10,
1124 0x00,0x00,0xFF,0xFF,
1125 0x00,0x00,0x00,0x00,
1126 0xFF,0xFF,0xFF,0xFF,
1127 };
1128
1129 logger->log_bytes(logger,RAW,"expected payload",expected_generation,sizeof(expected_generation));
1130
1131 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
1132
1133 allocator_free_chunk(&generated_data);
1134
1135 ts_payload->destroy(ts_payload);
1136 generator->destroy(generator);
1137
1138 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
1139 }
1140
1141 /*
1142 * Described in header.
1143 */
1144 void test_generator_with_cert_payload(tester_t *tester)
1145 {
1146 generator_t *generator;
1147 cert_payload_t *cert_payload;
1148 logger_t *logger;
1149 chunk_t generated_data;
1150 chunk_t cert;
1151
1152 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"Message with CERT Payload");
1153
1154 /* create generator */
1155 generator = generator_create();
1156 tester->assert_true(tester,(generator != NULL), "generator create check");
1157
1158 cert_payload = cert_payload_create();
1159
1160
1161 cert.ptr = "123456789012";
1162 cert.len = strlen(cert.ptr);
1163
1164 cert_payload->set_cert_encoding(cert_payload,PGP_CERTIFICATE);
1165 cert_payload->set_data(cert_payload,cert);
1166
1167 generator->generate_payload(generator,(payload_t *)cert_payload);
1168 generator->write_to_chunk(generator,&generated_data);
1169 logger->log_chunk(logger,RAW,"generated payload",&generated_data);
1170
1171 u_int8_t expected_generation[] = {
1172 /* payload header */
1173 0x00,0x00,0x00,0x11,
1174 0x02,
1175 /* cert data */
1176 0x31,0x32,0x33,0x34,
1177 0x35,0x36,0x37,0x38,
1178 0x39,0x30,0x31,0x32,
1179 };
1180
1181 logger->log_bytes(logger,RAW,"expected payload",expected_generation,sizeof(expected_generation));
1182
1183 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
1184
1185 allocator_free_chunk(&generated_data);
1186
1187 cert_payload->destroy(cert_payload);
1188 generator->destroy(generator);
1189
1190 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
1191 }
1192
1193 /*
1194 * Described in header.
1195 */
1196 void test_generator_with_certreq_payload(tester_t *tester)
1197 {
1198 generator_t *generator;
1199 certreq_payload_t *certreq_payload;
1200 logger_t *logger;
1201 chunk_t generated_data;
1202 chunk_t certreq;
1203
1204 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"Message with CERT Payload");
1205
1206 /* create generator */
1207 generator = generator_create();
1208 tester->assert_true(tester,(generator != NULL), "generator create check");
1209
1210 certreq_payload = certreq_payload_create();
1211
1212
1213 certreq.ptr = "123456789012";
1214 certreq.len = strlen(certreq.ptr);
1215
1216 certreq_payload->set_cert_encoding(certreq_payload,PGP_CERTIFICATE);
1217 certreq_payload->set_data(certreq_payload,certreq);
1218
1219 generator->generate_payload(generator,(payload_t *)certreq_payload);
1220 generator->write_to_chunk(generator,&generated_data);
1221 logger->log_chunk(logger,RAW,"generated payload",&generated_data);
1222
1223 u_int8_t expected_generation[] = {
1224 /* payload header */
1225 0x00,0x00,0x00,0x11,
1226 0x02,
1227 /* certreq data */
1228 0x31,0x32,0x33,0x34,
1229 0x35,0x36,0x37,0x38,
1230 0x39,0x30,0x31,0x32,
1231 };
1232
1233 logger->log_bytes(logger,RAW,"expected payload",expected_generation,sizeof(expected_generation));
1234
1235 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
1236
1237 allocator_free_chunk(&generated_data);
1238
1239 certreq_payload->destroy(certreq_payload);
1240 generator->destroy(generator);
1241
1242 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
1243 }