- added todo's
[strongswan.git] / Source / charon / encoding / payloads / sa_payload.c
1 /**
2 * @file sa_payload.c
3 *
4 * @brief Implementation of sa_payload_t.
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 /* offsetof macro */
24 #include <stddef.h>
25
26 #include "sa_payload.h"
27
28 #include <encoding/payloads/encodings.h>
29 #include <utils/allocator.h>
30 #include <utils/linked_list.h>
31
32
33 typedef struct private_sa_payload_t private_sa_payload_t;
34
35 /**
36 * Private data of an sa_payload_t object.
37 *
38 */
39 struct private_sa_payload_t {
40 /**
41 * Public sa_payload_t interface.
42 */
43 sa_payload_t public;
44
45 /**
46 * Next payload type.
47 */
48 u_int8_t next_payload;
49
50 /**
51 * Critical flag.
52 */
53 bool critical;
54
55 /**
56 * Length of this payload.
57 */
58 u_int16_t payload_length;
59
60 /**
61 * Proposals in this payload are stored in a linked_list_t.
62 */
63 linked_list_t * proposals;
64
65 /**
66 * @brief Computes the length of this payload.
67 *
68 * @param this calling private_sa_payload_t object
69 */
70 void (*compute_length) (private_sa_payload_t *this);
71 };
72
73 /**
74 * Encoding rules to parse or generate a IKEv2-SA Payload
75 *
76 * The defined offsets are the positions in a object of type
77 * private_sa_payload_t.
78 *
79 */
80 encoding_rule_t sa_payload_encodings[] = {
81 /* 1 Byte next payload type, stored in the field next_payload */
82 { U_INT_8, offsetof(private_sa_payload_t, next_payload) },
83 /* the critical bit */
84 { FLAG, offsetof(private_sa_payload_t, critical) },
85 /* 7 Bit reserved bits, nowhere stored */
86 { RESERVED_BIT, 0 },
87 { RESERVED_BIT, 0 },
88 { RESERVED_BIT, 0 },
89 { RESERVED_BIT, 0 },
90 { RESERVED_BIT, 0 },
91 { RESERVED_BIT, 0 },
92 { RESERVED_BIT, 0 },
93 /* Length of the whole SA payload*/
94 { PAYLOAD_LENGTH, offsetof(private_sa_payload_t, payload_length) },
95 /* Proposals are stored in a proposal substructure,
96 offset points to a linked_list_t pointer */
97 { PROPOSALS, offsetof(private_sa_payload_t, proposals) }
98 };
99
100 /*
101 1 2 3
102 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
103 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
104 ! Next Payload !C! RESERVED ! Payload Length !
105 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
106 ! !
107 ~ <Proposals> ~
108 ! !
109 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
110 */
111
112 /**
113 * Implementation of payload_t.verify.
114 */
115 static status_t verify(private_sa_payload_t *this)
116 {
117 int proposal_number = 1;
118 status_t status = SUCCESS;
119 iterator_t *iterator;
120 bool first = TRUE;
121
122 if (this->critical)
123 {
124 /* critical bit set! */
125 return FAILED;
126 }
127
128 /* check proposal numbering */
129 iterator = this->proposals->create_iterator(this->proposals,TRUE);
130
131 while(iterator->has_next(iterator))
132 {
133 proposal_substructure_t *current_proposal;
134 iterator->current(iterator,(void **)&current_proposal);
135 if (current_proposal->get_proposal_number(current_proposal) > proposal_number)
136 {
137 if (first)
138 {
139 /* first number must be 1 */
140 status = FAILED;
141 break;
142 }
143
144 if (current_proposal->get_proposal_number(current_proposal) != (proposal_number + 1))
145 {
146 /* must be only one more then previous proposal */
147 status = FAILED;
148 break;
149 }
150 }
151 else if (current_proposal->get_proposal_number(current_proposal) < proposal_number)
152 {
153 iterator->destroy(iterator);
154 /* must not be smaller then proceeding one */
155 status = FAILED;
156 break;
157 }
158
159 status = current_proposal->payload_interface.verify(&(current_proposal->payload_interface));
160 if (status != SUCCESS)
161 {
162 break;
163 }
164 first = FALSE;
165 }
166
167 iterator->destroy(iterator);
168 return status;
169 }
170
171
172 /**
173 * Implementation of payload_t.destroy and sa_payload_t.destroy.
174 */
175 static status_t destroy(private_sa_payload_t *this)
176 {
177 /* all proposals are getting destroyed */
178 while (this->proposals->get_count(this->proposals) > 0)
179 {
180 proposal_substructure_t *current_proposal;
181 this->proposals->remove_last(this->proposals,(void **)&current_proposal);
182 current_proposal->destroy(current_proposal);
183 }
184 this->proposals->destroy(this->proposals);
185
186 allocator_free(this);
187
188 return SUCCESS;
189 }
190
191 /**
192 * Implementation of payload_t.get_encoding_rules.
193 */
194 static void get_encoding_rules(private_sa_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
195 {
196 *rules = sa_payload_encodings;
197 *rule_count = sizeof(sa_payload_encodings) / sizeof(encoding_rule_t);
198 }
199
200 /**
201 * Implementation of payload_t.get_type.
202 */
203 static payload_type_t get_type(private_sa_payload_t *this)
204 {
205 return SECURITY_ASSOCIATION;
206 }
207
208 /**
209 * Implementation of payload_t.get_next_type.
210 */
211 static payload_type_t get_next_type(private_sa_payload_t *this)
212 {
213 return (this->next_payload);
214 }
215
216 /**
217 * Implementation of payload_t.set_next_type.
218 */
219 static void set_next_type(private_sa_payload_t *this,payload_type_t type)
220 {
221 this->next_payload = type;
222 }
223
224 /**
225 * Implementation of payload_t.get_length.
226 */
227 static size_t get_length(private_sa_payload_t *this)
228 {
229 this->compute_length(this);
230 return this->payload_length;
231 }
232
233 /**
234 * Implementation of sa_payload_t.create_proposal_substructure_iterator.
235 */
236 static iterator_t *create_proposal_substructure_iterator (private_sa_payload_t *this,bool forward)
237 {
238 return this->proposals->create_iterator(this->proposals,forward);
239 }
240
241 /**
242 * Implementation of sa_payload_t.add_proposal_substructure.
243 */
244 static void add_proposal_substructure (private_sa_payload_t *this,proposal_substructure_t *proposal)
245 {
246 status_t status;
247 if (this->proposals->get_count(this->proposals) > 0)
248 {
249 proposal_substructure_t *last_proposal;
250 status = this->proposals->get_last(this->proposals,(void **) &last_proposal);
251 /* last transform is now not anymore last one */
252 last_proposal->set_is_last_proposal(last_proposal,FALSE);
253 }
254 proposal->set_is_last_proposal(proposal,TRUE);
255
256 this->proposals->insert_last(this->proposals,(void *) proposal);
257 this->compute_length(this);
258 }
259
260 /**
261 * Implementation of sa_payload_t.get_ike_proposals.
262 */
263 static status_t get_ike_proposals (private_sa_payload_t *this,ike_proposal_t ** proposals, size_t *proposal_count)
264 {
265 int found_ike_proposals = 0;
266 int current_proposal_number = 0;
267 iterator_t *iterator;
268 ike_proposal_t *tmp_proposals;
269
270
271 iterator = this->proposals->create_iterator(this->proposals,TRUE);
272
273 /* first find out the number of ike proposals and check their number of transforms and
274 * if the SPI is empty!*/
275 while (iterator->has_next(iterator))
276 {
277 proposal_substructure_t *current_proposal;
278 iterator->current(iterator,(void **)&(current_proposal));
279 if (current_proposal->get_protocol_id(current_proposal) == IKE)
280 {
281 /* a ike proposal consists of an empty spi*/
282 if (current_proposal->get_spi_size(current_proposal) != 0)
283 {
284 iterator->destroy(iterator);
285 return FAILED;
286 }
287
288 found_ike_proposals++;
289 }
290 }
291 iterator->reset(iterator);
292
293 if (found_ike_proposals == 0)
294 {
295 iterator->destroy(iterator);
296 return NOT_FOUND;
297 }
298
299 /* allocate memory to hold each proposal as ike_proposal_t */
300
301 tmp_proposals = allocator_alloc(found_ike_proposals * sizeof(ike_proposal_t));
302
303 /* create from each proposal_substructure a ike_proposal_t data area*/
304 while (iterator->has_next(iterator))
305 {
306 proposal_substructure_t *current_proposal;
307 iterator->current(iterator,(void **)&(current_proposal));
308 if (current_proposal->get_protocol_id(current_proposal) == IKE)
309 {
310 bool encryption_algorithm_found = FALSE;
311 bool integrity_algorithm_found = FALSE;
312 bool pseudo_random_function_found = FALSE;
313 bool diffie_hellman_group_found = FALSE;
314 status_t status;
315 iterator_t *transforms;
316
317 transforms = current_proposal->create_transform_substructure_iterator(current_proposal,TRUE);
318 while (transforms->has_next(transforms))
319 {
320 transform_substructure_t *current_transform;
321 transforms->current(transforms,(void **)&(current_transform));
322
323 switch (current_transform->get_transform_type(current_transform))
324 {
325 case ENCRYPTION_ALGORITHM:
326 {
327 tmp_proposals[current_proposal_number].encryption_algorithm = current_transform->get_transform_id(current_transform);
328 status = current_transform->get_key_length(current_transform,&(tmp_proposals[current_proposal_number].encryption_algorithm_key_length));
329 if (status == SUCCESS)
330 {
331 encryption_algorithm_found = TRUE;
332 }
333 break;
334 }
335 case INTEGRITY_ALGORITHM:
336 {
337 tmp_proposals[current_proposal_number].integrity_algorithm = current_transform->get_transform_id(current_transform);
338 status = current_transform->get_key_length(current_transform,&(tmp_proposals[current_proposal_number].integrity_algorithm_key_length));
339 if (status == SUCCESS)
340 {
341 integrity_algorithm_found = TRUE;
342 }
343 break;
344 }
345 case PSEUDO_RANDOM_FUNCTION:
346 {
347 tmp_proposals[current_proposal_number].pseudo_random_function = current_transform->get_transform_id(current_transform);
348 status = current_transform->get_key_length(current_transform,&(tmp_proposals[current_proposal_number].pseudo_random_function_key_length));
349 if (status == SUCCESS)
350 {
351 pseudo_random_function_found = TRUE;
352 }
353 break;
354 }
355 case DIFFIE_HELLMAN_GROUP:
356 {
357 tmp_proposals[current_proposal_number].diffie_hellman_group = current_transform->get_transform_id(current_transform);
358 diffie_hellman_group_found = TRUE;
359 break;
360 }
361 default:
362 {
363 /* not a transform of an ike proposal. Break here */
364 break;
365 }
366 }
367
368 }
369
370 transforms->destroy(transforms);
371
372 if ((!encryption_algorithm_found) ||
373 (!integrity_algorithm_found) ||
374 (!pseudo_random_function_found) ||
375 (!diffie_hellman_group_found))
376 {
377 /* one of needed transforms could not be found */
378 iterator->reset(iterator);
379 allocator_free(tmp_proposals);
380 return FAILED;
381 }
382
383 current_proposal_number++;
384 }
385 }
386
387 iterator->destroy(iterator);
388
389 *proposals = tmp_proposals;
390 *proposal_count = found_ike_proposals;
391
392 return SUCCESS;
393 }
394
395 /**
396 * Implementation of sa_payload_t.get_child_proposals.
397 */
398 static status_t get_child_proposals (private_sa_payload_t *this,child_proposal_t ** proposals, size_t *proposal_count)
399 {
400 int found_child_proposals = 0;
401 int found_suites = 1;
402 int current_suite_number = 0;
403
404 iterator_t *iterator;
405 child_proposal_t *tmp_proposals;
406
407 iterator = this->proposals->create_iterator(this->proposals,TRUE);
408
409 /* first find out the number of child proposals and check their number of transforms and
410 * if the SPI is 4 byte long!*/
411 current_suite_number = 1;
412 while (iterator->has_next(iterator))
413 {
414 proposal_substructure_t *current_proposal;
415 iterator->current(iterator,(void **)&(current_proposal));
416 if ((current_proposal->get_protocol_id(current_proposal) == AH) ||
417 (current_proposal->get_protocol_id(current_proposal) == ESP))
418 {
419 if (current_proposal->get_spi_size(current_proposal) != 4)
420 {
421 iterator->destroy(iterator);
422 return FAILED;
423 }
424 if (current_proposal->get_proposal_number(current_proposal) == (current_suite_number + 1))
425 {
426 found_suites++;
427 current_suite_number = current_proposal->get_proposal_number(current_proposal);
428 }
429 found_child_proposals++;
430 }
431 }
432 iterator->reset(iterator);
433
434 if (found_child_proposals == 0)
435 {
436 iterator->destroy(iterator);
437 return NOT_FOUND;
438 }
439
440 /* allocate memory to hold each proposal as child_proposal_t */
441
442 tmp_proposals = allocator_alloc(found_child_proposals * sizeof(child_proposal_t));
443
444 current_suite_number = 1;
445 tmp_proposals[current_suite_number - 1].ah.extended_sequence_numbers = NO_EXT_SEQ_NUMBERS;
446 tmp_proposals[current_suite_number - 1].ah.diffie_hellman_group = MODP_UNDEFINED;
447 tmp_proposals[current_suite_number - 1].ah.integrity_algorithm = AUTH_UNDEFINED;
448 tmp_proposals[current_suite_number - 1].ah.is_set = FALSE;
449
450 tmp_proposals[current_suite_number - 1].esp.integrity_algorithm = AUTH_UNDEFINED;
451 tmp_proposals[current_suite_number - 1].esp.diffie_hellman_group = MODP_UNDEFINED;
452 tmp_proposals[current_suite_number - 1].esp.extended_sequence_numbers = NO_EXT_SEQ_NUMBERS;
453 tmp_proposals[current_suite_number - 1].esp.is_set = FALSE;
454
455 /* create from each proposal_substructure a child_proposal_t data area*/
456 while (iterator->has_next(iterator))
457 {
458 proposal_substructure_t *current_proposal;
459 iterator->current(iterator,(void **)&(current_proposal));
460
461 if (current_proposal->get_proposal_number(current_proposal) == (current_suite_number + 1))
462 {
463 current_suite_number++;
464 if (current_suite_number > found_suites)
465 {
466 /* inconsistent situation */
467 return FAILED;
468 }
469 tmp_proposals[current_suite_number - 1].ah.extended_sequence_numbers = NO_EXT_SEQ_NUMBERS;
470 tmp_proposals[current_suite_number - 1].ah.diffie_hellman_group = MODP_UNDEFINED;
471
472 tmp_proposals[current_suite_number - 1].esp.integrity_algorithm = AUTH_UNDEFINED;
473 tmp_proposals[current_suite_number - 1].esp.diffie_hellman_group = MODP_UNDEFINED;
474 tmp_proposals[current_suite_number - 1].esp.extended_sequence_numbers = NO_EXT_SEQ_NUMBERS;
475 }
476
477 if (current_proposal->get_protocol_id(current_proposal) == AH)
478 {
479 bool integrity_algorithm_found = FALSE;
480 bool diffie_hellman_group_found = FALSE;
481 bool extended_sequence_numbers_found = FALSE;
482 iterator_t *transforms;
483 status_t status;
484
485 chunk_t spi;
486
487 tmp_proposals[current_suite_number - 1].ah.is_set = TRUE;
488
489 spi = current_proposal->get_spi(current_proposal);
490 memcpy(tmp_proposals[current_suite_number - 1].ah.spi,spi.ptr,min(spi.len,4));
491
492 transforms = current_proposal->create_transform_substructure_iterator(current_proposal,TRUE);
493 while (transforms->has_next(transforms))
494 {
495 transform_substructure_t *current_transform;
496 transforms->current(transforms,(void **)&(current_transform));
497
498 switch (current_transform->get_transform_type(current_transform))
499 {
500 case INTEGRITY_ALGORITHM:
501 {
502 u_int16_t key_size;
503
504 if (integrity_algorithm_found)
505 {
506 transforms->destroy(transforms);
507 iterator->destroy(iterator);
508 allocator_free(tmp_proposals);
509 return FAILED;
510 }
511 tmp_proposals[current_suite_number - 1].ah.integrity_algorithm = current_transform->get_transform_id(current_transform);
512 status = current_transform->get_key_length(current_transform,&key_size);
513 tmp_proposals[current_suite_number - 1].ah.integrity_algorithm_key_size = key_size;
514 if (status == SUCCESS)
515 {
516 integrity_algorithm_found = TRUE;
517 }
518 break;
519 }
520 case EXTENDED_SEQUENCE_NUMBERS:
521 {
522 if (extended_sequence_numbers_found)
523 {
524 transforms->destroy(transforms);
525 iterator->destroy(iterator);
526 allocator_free(tmp_proposals);
527 return FAILED;
528 }
529 tmp_proposals[current_suite_number - 1].ah.extended_sequence_numbers = current_transform->get_transform_id(current_transform);
530 extended_sequence_numbers_found = TRUE;
531 break;
532 }
533 case DIFFIE_HELLMAN_GROUP:
534 {
535 if (diffie_hellman_group_found)
536 {
537 transforms->destroy(transforms);
538 iterator->destroy(iterator);
539 allocator_free(tmp_proposals);
540 return FAILED;
541 }
542 tmp_proposals[current_suite_number - 1].ah.diffie_hellman_group = current_transform->get_transform_id(current_transform);
543 diffie_hellman_group_found = TRUE;
544 break;
545 }
546 default:
547 {
548 /* not a transform of an child proposal. return here */
549 transforms->destroy(transforms);
550 iterator->destroy(iterator);
551 allocator_free(tmp_proposals);
552 return FAILED;
553 }
554 }
555
556 }
557 transforms->destroy(transforms);
558
559 if (!integrity_algorithm_found)
560 {
561 /* one of needed transforms could not be found */
562 iterator->reset(iterator);
563 allocator_free(tmp_proposals);
564 return FAILED;
565 }
566 }
567 else if (current_proposal->get_protocol_id(current_proposal) == ESP)
568 {
569 bool encryption_algorithm_found = FALSE;
570 bool integrity_algorithm_found = FALSE;
571 bool diffie_hellman_group_found = FALSE;
572 bool extended_sequence_numbers_found = FALSE;
573 iterator_t *transforms;
574 status_t status;
575 chunk_t spi;
576
577 spi = current_proposal->get_spi(current_proposal);
578 memcpy(tmp_proposals[current_suite_number - 1].esp.spi,spi.ptr,min(spi.len,4));
579 tmp_proposals[current_suite_number - 1].esp.is_set = TRUE;
580
581
582 transforms = current_proposal->create_transform_substructure_iterator(current_proposal,TRUE);
583 while (transforms->has_next(transforms))
584 {
585 transform_substructure_t *current_transform;
586 transforms->current(transforms,(void **)&(current_transform));
587
588 switch (current_transform->get_transform_type(current_transform))
589 {
590 case ENCRYPTION_ALGORITHM:
591 {
592 u_int16_t key_size;
593
594 if (encryption_algorithm_found)
595 {
596 transforms->destroy(transforms);
597 iterator->destroy(iterator);
598 allocator_free(tmp_proposals);
599 return FAILED;
600 }
601 tmp_proposals[current_suite_number - 1].esp.encryption_algorithm = current_transform->get_transform_id(current_transform);
602 status = current_transform->get_key_length(current_transform,&key_size);
603 tmp_proposals[current_suite_number - 1].esp.encryption_algorithm_key_size = key_size;
604 if (status == SUCCESS)
605 {
606 encryption_algorithm_found = TRUE;
607 }
608 break;
609 }
610 case INTEGRITY_ALGORITHM:
611 {
612 u_int16_t key_size;
613
614 if (integrity_algorithm_found)
615 {
616 transforms->destroy(transforms);
617 iterator->destroy(iterator);
618 allocator_free(tmp_proposals);
619 return FAILED;
620 }
621 tmp_proposals[current_suite_number - 1].esp.integrity_algorithm = current_transform->get_transform_id(current_transform);
622 status = current_transform->get_key_length(current_transform,&key_size);
623 tmp_proposals[current_suite_number - 1].esp.integrity_algorithm_key_size = key_size;
624 if (status == SUCCESS)
625 {
626 integrity_algorithm_found = TRUE;
627 }
628 break;
629 }
630 case EXTENDED_SEQUENCE_NUMBERS:
631 {
632 if (extended_sequence_numbers_found)
633 {
634 transforms->destroy(transforms);
635 iterator->destroy(iterator);
636 allocator_free(tmp_proposals);
637 return FAILED;
638 }
639 tmp_proposals[current_suite_number - 1].esp.extended_sequence_numbers = current_transform->get_transform_id(current_transform);
640 extended_sequence_numbers_found = TRUE;
641 break;
642 }
643 case DIFFIE_HELLMAN_GROUP:
644 {
645 if (diffie_hellman_group_found)
646 {
647 transforms->destroy(transforms);
648 iterator->destroy(iterator);
649 allocator_free(tmp_proposals);
650 return FAILED;
651 }
652 tmp_proposals[current_suite_number - 1].esp.diffie_hellman_group = current_transform->get_transform_id(current_transform);
653 diffie_hellman_group_found = TRUE;
654 break;
655 }
656 default:
657 {
658 /* not a transform of an child proposal. return here */
659 transforms->destroy(transforms);
660 iterator->destroy(iterator);
661 allocator_free(tmp_proposals);
662 return FAILED;
663 }
664 }
665
666 }
667 transforms->destroy(transforms);
668
669
670 if (!encryption_algorithm_found)
671 {
672 /* one of needed transforms could not be found */
673 iterator->reset(iterator);
674 allocator_free(tmp_proposals);
675 return FAILED;
676 }
677
678 }
679 }
680
681 iterator->destroy(iterator);
682
683 *proposals = tmp_proposals;
684 *proposal_count = found_suites;
685
686 return SUCCESS;
687 }
688
689
690 /**
691 * Implementation of private_sa_payload_t.compute_length.
692 */
693 static void compute_length (private_sa_payload_t *this)
694 {
695 iterator_t *iterator;
696 size_t length = SA_PAYLOAD_HEADER_LENGTH;
697 iterator = this->proposals->create_iterator(this->proposals,TRUE);
698 while (iterator->has_next(iterator))
699 {
700 payload_t *current_proposal;
701 iterator->current(iterator,(void **) &current_proposal);
702 length += current_proposal->get_length(current_proposal);
703 }
704 iterator->destroy(iterator);
705
706 this->payload_length = length;
707 }
708
709 /*
710 * Described in header.
711 */
712 sa_payload_t *sa_payload_create()
713 {
714 private_sa_payload_t *this = allocator_alloc_thing(private_sa_payload_t);
715
716 /* public interface */
717 this->public.payload_interface.verify = (status_t (*) (payload_t *))verify;
718 this->public.payload_interface.get_encoding_rules = (void (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
719 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
720 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
721 this->public.payload_interface.set_next_type = (void (*) (payload_t *,payload_type_t)) set_next_type;
722 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_type;
723 this->public.payload_interface.destroy = (void (*) (payload_t *))destroy;
724
725 /* public functions */
726 this->public.create_proposal_substructure_iterator = (iterator_t* (*) (sa_payload_t *,bool)) create_proposal_substructure_iterator;
727 this->public.add_proposal_substructure = (void (*) (sa_payload_t *,proposal_substructure_t *)) add_proposal_substructure;
728 this->public.get_ike_proposals = (status_t (*) (sa_payload_t *, ike_proposal_t **, size_t *)) get_ike_proposals;
729 this->public.get_child_proposals = (status_t (*) (sa_payload_t *, child_proposal_t **, size_t *)) get_child_proposals;
730 this->public.destroy = (void (*) (sa_payload_t *)) destroy;
731
732 /* private functions */
733 this->compute_length = compute_length;
734
735 /* set default values of the fields */
736 this->critical = SA_PAYLOAD_CRITICAL_FLAG;
737 this->next_payload = NO_PAYLOAD;
738 this->payload_length = SA_PAYLOAD_HEADER_LENGTH;
739
740 this->proposals = linked_list_create();
741 return (&(this->public));
742 }
743
744 /*
745 * Described in header.
746 */
747 sa_payload_t *sa_payload_create_from_ike_proposals(ike_proposal_t *proposals, size_t proposal_count)
748 {
749 int i;
750 sa_payload_t *sa_payload= sa_payload_create();
751
752 for (i = 0; i < proposal_count; i++)
753 {
754 proposal_substructure_t *proposal_substructure;
755 transform_substructure_t *encryption_algorithm;
756 transform_substructure_t *integrity_algorithm;
757 transform_substructure_t *pseudo_random_function;
758 transform_substructure_t *diffie_hellman_group;
759
760 /* create proposal substructure */
761 proposal_substructure = proposal_substructure_create();
762 proposal_substructure->set_protocol_id(proposal_substructure,IKE);
763 proposal_substructure->set_proposal_number(proposal_substructure,(i + 1));
764
765 /* create transform substructures to hold each specific transform for an ike proposal */
766 encryption_algorithm = transform_substructure_create_type(ENCRYPTION_ALGORITHM,proposals[i].encryption_algorithm,proposals[i].encryption_algorithm_key_length);
767 proposal_substructure->add_transform_substructure(proposal_substructure,encryption_algorithm);
768
769 pseudo_random_function = transform_substructure_create_type(PSEUDO_RANDOM_FUNCTION,proposals[i].pseudo_random_function,proposals[i].pseudo_random_function_key_length);
770 proposal_substructure->add_transform_substructure(proposal_substructure,pseudo_random_function);
771
772 integrity_algorithm = transform_substructure_create_type(INTEGRITY_ALGORITHM,proposals[i].integrity_algorithm,proposals[i].integrity_algorithm_key_length);
773 proposal_substructure->add_transform_substructure(proposal_substructure,integrity_algorithm);
774
775 diffie_hellman_group = transform_substructure_create_type(DIFFIE_HELLMAN_GROUP,proposals[i].diffie_hellman_group,0);
776 proposal_substructure->add_transform_substructure(proposal_substructure,diffie_hellman_group);
777
778 /* add proposal to sa payload */
779 sa_payload->add_proposal_substructure(sa_payload,proposal_substructure);
780 }
781
782 return sa_payload;
783 }
784
785 /*
786 * Described in header.
787 */
788 sa_payload_t *sa_payload_create_from_child_proposals(child_proposal_t *proposals, size_t proposal_count)
789 {
790 int i;
791 sa_payload_t *sa_payload= sa_payload_create();
792
793 for (i = 0; i < proposal_count; i++)
794 {
795 /* first the AH part is created */
796 if (proposals[i].ah.is_set)
797 {
798 transform_substructure_t *integrity_algorithm;
799 proposal_substructure_t *proposal_substructure;
800 chunk_t spi;
801
802 proposal_substructure = proposal_substructure_create();
803 proposal_substructure->set_protocol_id(proposal_substructure,AH);
804 proposal_substructure->set_proposal_number(proposal_substructure,(i + 1));
805 spi.ptr = proposals[i].ah.spi;
806 spi.len = 4;
807 proposal_substructure->set_spi(proposal_substructure,spi);
808
809 integrity_algorithm = transform_substructure_create_type(INTEGRITY_ALGORITHM,proposals[i].ah.integrity_algorithm,proposals[i].ah.integrity_algorithm_key_size);
810 proposal_substructure->add_transform_substructure(proposal_substructure,integrity_algorithm);
811 if (proposals[i].ah.diffie_hellman_group != MODP_UNDEFINED)
812 {
813 transform_substructure_t *diffie_hellman_group;
814 diffie_hellman_group = transform_substructure_create_type(DIFFIE_HELLMAN_GROUP,proposals[i].ah.diffie_hellman_group,0);
815 proposal_substructure->add_transform_substructure(proposal_substructure,diffie_hellman_group);
816
817 }
818 if (proposals[i].ah.extended_sequence_numbers == EXT_SEQ_NUMBERS)
819 {
820 transform_substructure_t *extended_sequence_numbers;
821 extended_sequence_numbers = transform_substructure_create_type(EXTENDED_SEQUENCE_NUMBERS,proposals[i].ah.extended_sequence_numbers,0);
822 proposal_substructure->add_transform_substructure(proposal_substructure,extended_sequence_numbers);
823 }
824
825 sa_payload->add_proposal_substructure(sa_payload,proposal_substructure);
826 }
827
828 /* then the ESP part is created */
829 if (proposals[i].esp.is_set)
830 {
831 transform_substructure_t *encryption_algorithm;
832 proposal_substructure_t *proposal_substructure;
833 chunk_t spi;
834
835 proposal_substructure = proposal_substructure_create();
836 proposal_substructure->set_protocol_id(proposal_substructure,ESP);
837 proposal_substructure->set_proposal_number(proposal_substructure,(i + 1));
838 spi.ptr = proposals[i].esp.spi;
839 spi.len = 4;
840 proposal_substructure->set_spi(proposal_substructure,spi);
841
842 encryption_algorithm = transform_substructure_create_type(ENCRYPTION_ALGORITHM,proposals[i].esp.encryption_algorithm,proposals[i].esp.encryption_algorithm_key_size);
843 proposal_substructure->add_transform_substructure(proposal_substructure,encryption_algorithm);
844
845 if (proposals[i].esp.integrity_algorithm != AUTH_UNDEFINED)
846 {
847 transform_substructure_t *integrity_algorithm;
848 integrity_algorithm = transform_substructure_create_type(INTEGRITY_ALGORITHM,proposals[i].esp.integrity_algorithm,proposals[i].esp.integrity_algorithm_key_size);
849 proposal_substructure->add_transform_substructure(proposal_substructure,integrity_algorithm);
850
851 }
852
853 if (proposals[i].esp.diffie_hellman_group != MODP_UNDEFINED)
854 {
855 transform_substructure_t *diffie_hellman_group;
856 diffie_hellman_group = transform_substructure_create_type(DIFFIE_HELLMAN_GROUP,proposals[i].esp.diffie_hellman_group,0);
857 proposal_substructure->add_transform_substructure(proposal_substructure,diffie_hellman_group);
858
859 }
860 if (proposals[i].esp.extended_sequence_numbers == EXT_SEQ_NUMBERS)
861 {
862 transform_substructure_t *extended_sequence_numbers;
863 extended_sequence_numbers = transform_substructure_create_type(EXTENDED_SEQUENCE_NUMBERS,proposals[i].esp.extended_sequence_numbers,0);
864 proposal_substructure->add_transform_substructure(proposal_substructure,extended_sequence_numbers);
865 }
866
867 sa_payload->add_proposal_substructure(sa_payload,proposal_substructure);
868 }
869
870 }
871
872 return sa_payload;
873 }