- implemented functionality to generate sa payload from
[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 4 transforms and an empty spi*/
282 if ((current_proposal->get_transform_count(current_proposal) != 4) ||
283 (current_proposal->get_spi_size(current_proposal) != 0))
284 {
285 iterator->destroy(iterator);
286 return FAILED;
287 }
288
289 found_ike_proposals++;
290 }
291 }
292 iterator->reset(iterator);
293
294 if (found_ike_proposals == 0)
295 {
296 iterator->destroy(iterator);
297 return NOT_FOUND;
298 }
299
300 /* allocate memory to hold each proposal as ike_proposal_t */
301
302 tmp_proposals = allocator_alloc(found_ike_proposals * sizeof(ike_proposal_t));
303
304 /* create from each proposal_substructure a ike_proposal_t data area*/
305 while (iterator->has_next(iterator))
306 {
307 proposal_substructure_t *current_proposal;
308 iterator->current(iterator,(void **)&(current_proposal));
309 if (current_proposal->get_protocol_id(current_proposal) == IKE)
310 {
311 bool encryption_algorithm_found = FALSE;
312 bool integrity_algorithm_found = FALSE;
313 bool pseudo_random_function_found = FALSE;
314 bool diffie_hellman_group_found = FALSE;
315 status_t status;
316 iterator_t *transforms;
317
318 transforms = current_proposal->create_transform_substructure_iterator(current_proposal,TRUE);
319 while (transforms->has_next(transforms))
320 {
321 transform_substructure_t *current_transform;
322 transforms->current(transforms,(void **)&(current_transform));
323
324 switch (current_transform->get_transform_type(current_transform))
325 {
326 case ENCRYPTION_ALGORITHM:
327 {
328 tmp_proposals[current_proposal_number].encryption_algorithm = current_transform->get_transform_id(current_transform);
329 status = current_transform->get_key_length(current_transform,&(tmp_proposals[current_proposal_number].encryption_algorithm_key_length));
330 if (status == SUCCESS)
331 {
332 encryption_algorithm_found = TRUE;
333 }
334 break;
335 }
336 case INTEGRITY_ALGORITHM:
337 {
338 tmp_proposals[current_proposal_number].integrity_algorithm = current_transform->get_transform_id(current_transform);
339 status = current_transform->get_key_length(current_transform,&(tmp_proposals[current_proposal_number].integrity_algorithm_key_length));
340 if (status == SUCCESS)
341 {
342 integrity_algorithm_found = TRUE;
343 }
344 break;
345 }
346 case PSEUDO_RANDOM_FUNCTION:
347 {
348 tmp_proposals[current_proposal_number].pseudo_random_function = current_transform->get_transform_id(current_transform);
349 status = current_transform->get_key_length(current_transform,&(tmp_proposals[current_proposal_number].pseudo_random_function_key_length));
350 if (status == SUCCESS)
351 {
352 pseudo_random_function_found = TRUE;
353 }
354 break;
355 }
356 case DIFFIE_HELLMAN_GROUP:
357 {
358 tmp_proposals[current_proposal_number].diffie_hellman_group = current_transform->get_transform_id(current_transform);
359 diffie_hellman_group_found = TRUE;
360 break;
361 }
362 default:
363 {
364 /* not a transform of an ike proposal. Break here */
365 break;
366 }
367 }
368
369 }
370
371 transforms->destroy(transforms);
372
373 if ((!encryption_algorithm_found) ||
374 (!integrity_algorithm_found) ||
375 (!pseudo_random_function_found) ||
376 (!diffie_hellman_group_found))
377 {
378 /* one of needed transforms could not be found */
379 iterator->reset(iterator);
380 allocator_free(tmp_proposals);
381 return FAILED;
382 }
383
384 current_proposal_number++;
385 }
386 }
387
388 iterator->destroy(iterator);
389
390 *proposals = tmp_proposals;
391 *proposal_count = found_ike_proposals;
392
393 return SUCCESS;
394 }
395
396 /**
397 * Implementation of sa_payload_t.get_child_proposals.
398 */
399 static status_t get_child_proposals (private_sa_payload_t *this,child_proposal_t ** proposals, size_t *proposal_count)
400 {
401 int found_child_proposals = 0;
402 int found_suites = 1;
403 int current_suite_number = 0;
404
405 iterator_t *iterator;
406 child_proposal_t *tmp_proposals;
407
408 iterator = this->proposals->create_iterator(this->proposals,TRUE);
409
410 /* first find out the number of child proposals and check their number of transforms and
411 * if the SPI is 4 byte long!*/
412 current_suite_number = 1;
413 while (iterator->has_next(iterator))
414 {
415 proposal_substructure_t *current_proposal;
416 iterator->current(iterator,(void **)&(current_proposal));
417 if ((current_proposal->get_protocol_id(current_proposal) == AH) ||
418 (current_proposal->get_protocol_id(current_proposal) == ESP))
419 {
420 if (current_proposal->get_spi_size(current_proposal) != 4)
421 {
422 iterator->destroy(iterator);
423 return FAILED;
424 }
425 if (current_proposal->get_proposal_number(current_proposal) == (current_suite_number + 1))
426 {
427 found_suites++;
428 current_suite_number = current_proposal->get_proposal_number(current_proposal);
429 }
430 found_child_proposals++;
431 }
432 }
433 iterator->reset(iterator);
434
435 if (found_child_proposals == 0)
436 {
437 iterator->destroy(iterator);
438 return NOT_FOUND;
439 }
440
441 /* allocate memory to hold each proposal as child_proposal_t */
442
443 tmp_proposals = allocator_alloc(found_child_proposals * sizeof(child_proposal_t));
444
445 current_suite_number = 1;
446 tmp_proposals[current_suite_number - 1].ah.extended_sequence_numbers = NO_EXT_SEQ_NUMBERS;
447 tmp_proposals[current_suite_number - 1].ah.diffie_hellman_group = MODP_UNDEFINED;
448
449 tmp_proposals[current_suite_number - 1].esp.integrity_algorithm = AUTH_UNDEFINED;
450 tmp_proposals[current_suite_number - 1].esp.diffie_hellman_group = MODP_UNDEFINED;
451 tmp_proposals[current_suite_number - 1].esp.extended_sequence_numbers = NO_EXT_SEQ_NUMBERS;
452
453 /* create from each proposal_substructure a child_proposal_t data area*/
454 while (iterator->has_next(iterator))
455 {
456 proposal_substructure_t *current_proposal;
457 iterator->current(iterator,(void **)&(current_proposal));
458
459 if (current_proposal->get_proposal_number(current_proposal) == (current_suite_number + 1))
460 {
461 current_suite_number++;
462 if (current_suite_number > found_suites)
463 {
464 /* inconsistent situation */
465 return FAILED;
466 }
467 tmp_proposals[current_suite_number - 1].ah.extended_sequence_numbers = NO_EXT_SEQ_NUMBERS;
468 tmp_proposals[current_suite_number - 1].ah.diffie_hellman_group = MODP_UNDEFINED;
469
470 tmp_proposals[current_suite_number - 1].esp.integrity_algorithm = AUTH_UNDEFINED;
471 tmp_proposals[current_suite_number - 1].esp.diffie_hellman_group = MODP_UNDEFINED;
472 tmp_proposals[current_suite_number - 1].esp.extended_sequence_numbers = NO_EXT_SEQ_NUMBERS;
473 }
474
475 if (current_proposal->get_protocol_id(current_proposal) == AH)
476 {
477 bool integrity_algorithm_found = FALSE;
478 bool diffie_hellman_group_found = FALSE;
479 bool extended_sequence_numbers_found = FALSE;
480 iterator_t *transforms;
481 status_t status;
482
483 chunk_t spi;
484
485 tmp_proposals[current_suite_number - 1].ah.is_set = TRUE;
486
487 spi = current_proposal->get_spi(current_proposal);
488 memcpy(tmp_proposals[current_suite_number - 1].ah.spi,spi.ptr,min(spi.len,4));
489
490 transforms = current_proposal->create_transform_substructure_iterator(current_proposal,TRUE);
491 while (transforms->has_next(transforms))
492 {
493 transform_substructure_t *current_transform;
494 transforms->current(transforms,(void **)&(current_transform));
495
496 switch (current_transform->get_transform_type(current_transform))
497 {
498 case INTEGRITY_ALGORITHM:
499 {
500 u_int16_t key_size;
501
502 if (integrity_algorithm_found)
503 {
504 transforms->destroy(transforms);
505 iterator->destroy(iterator);
506 allocator_free(tmp_proposals);
507 return FAILED;
508 }
509 tmp_proposals[current_suite_number - 1].ah.integrity_algorithm = current_transform->get_transform_id(current_transform);
510 status = current_transform->get_key_length(current_transform,&key_size);
511 tmp_proposals[current_suite_number - 1].ah.integrity_algorithm_key_size = key_size;
512 if (status == SUCCESS)
513 {
514 integrity_algorithm_found = TRUE;
515 }
516 break;
517 }
518 case EXTENDED_SEQUENCE_NUMBERS:
519 {
520 if (extended_sequence_numbers_found)
521 {
522 transforms->destroy(transforms);
523 iterator->destroy(iterator);
524 allocator_free(tmp_proposals);
525 return FAILED;
526 }
527 tmp_proposals[current_suite_number - 1].ah.extended_sequence_numbers = current_transform->get_transform_id(current_transform);
528 extended_sequence_numbers_found = TRUE;
529 break;
530 }
531 case DIFFIE_HELLMAN_GROUP:
532 {
533 if (diffie_hellman_group_found)
534 {
535 transforms->destroy(transforms);
536 iterator->destroy(iterator);
537 allocator_free(tmp_proposals);
538 return FAILED;
539 }
540 tmp_proposals[current_suite_number - 1].ah.diffie_hellman_group = current_transform->get_transform_id(current_transform);
541 diffie_hellman_group_found = TRUE;
542 break;
543 }
544 default:
545 {
546 /* not a transform of an child proposal. return here */
547 transforms->destroy(transforms);
548 iterator->destroy(iterator);
549 allocator_free(tmp_proposals);
550 return FAILED;
551 }
552 }
553
554 }
555 transforms->destroy(transforms);
556
557 if (!integrity_algorithm_found)
558 {
559 /* one of needed transforms could not be found */
560 iterator->reset(iterator);
561 allocator_free(tmp_proposals);
562 return FAILED;
563 }
564 }
565 else if (current_proposal->get_protocol_id(current_proposal) == ESP)
566 {
567 bool encryption_algorithm_found = FALSE;
568 bool integrity_algorithm_found = FALSE;
569 bool diffie_hellman_group_found = FALSE;
570 bool extended_sequence_numbers_found = FALSE;
571 iterator_t *transforms;
572 status_t status;
573 chunk_t spi;
574
575 spi = current_proposal->get_spi(current_proposal);
576 memcpy(tmp_proposals[current_suite_number - 1].esp.spi,spi.ptr,min(spi.len,4));
577 tmp_proposals[current_suite_number - 1].esp.is_set = TRUE;
578
579
580 transforms = current_proposal->create_transform_substructure_iterator(current_proposal,TRUE);
581 while (transforms->has_next(transforms))
582 {
583 transform_substructure_t *current_transform;
584 transforms->current(transforms,(void **)&(current_transform));
585
586 switch (current_transform->get_transform_type(current_transform))
587 {
588 case ENCRYPTION_ALGORITHM:
589 {
590 u_int16_t key_size;
591
592 if (encryption_algorithm_found)
593 {
594 transforms->destroy(transforms);
595 iterator->destroy(iterator);
596 allocator_free(tmp_proposals);
597 return FAILED;
598 }
599 tmp_proposals[current_suite_number - 1].esp.encryption_algorithm = current_transform->get_transform_id(current_transform);
600 status = current_transform->get_key_length(current_transform,&key_size);
601 tmp_proposals[current_suite_number - 1].esp.encryption_algorithm_key_size = key_size;
602 if (status == SUCCESS)
603 {
604 encryption_algorithm_found = TRUE;
605 }
606 break;
607 }
608 case INTEGRITY_ALGORITHM:
609 {
610 u_int16_t key_size;
611
612 if (integrity_algorithm_found)
613 {
614 transforms->destroy(transforms);
615 iterator->destroy(iterator);
616 allocator_free(tmp_proposals);
617 return FAILED;
618 }
619 tmp_proposals[current_suite_number - 1].esp.integrity_algorithm = current_transform->get_transform_id(current_transform);
620 status = current_transform->get_key_length(current_transform,&key_size);
621 tmp_proposals[current_suite_number - 1].esp.integrity_algorithm_key_size = key_size;
622 if (status == SUCCESS)
623 {
624 integrity_algorithm_found = TRUE;
625 }
626 break;
627 }
628 case EXTENDED_SEQUENCE_NUMBERS:
629 {
630 if (extended_sequence_numbers_found)
631 {
632 transforms->destroy(transforms);
633 iterator->destroy(iterator);
634 allocator_free(tmp_proposals);
635 return FAILED;
636 }
637 tmp_proposals[current_suite_number - 1].esp.extended_sequence_numbers = current_transform->get_transform_id(current_transform);
638 extended_sequence_numbers_found = TRUE;
639 break;
640 }
641 case DIFFIE_HELLMAN_GROUP:
642 {
643 if (diffie_hellman_group_found)
644 {
645 transforms->destroy(transforms);
646 iterator->destroy(iterator);
647 allocator_free(tmp_proposals);
648 return FAILED;
649 }
650 tmp_proposals[current_suite_number - 1].esp.diffie_hellman_group = current_transform->get_transform_id(current_transform);
651 diffie_hellman_group_found = TRUE;
652 break;
653 }
654 default:
655 {
656 /* not a transform of an child proposal. return here */
657 transforms->destroy(transforms);
658 iterator->destroy(iterator);
659 allocator_free(tmp_proposals);
660 return FAILED;
661 }
662 }
663
664 }
665 transforms->destroy(transforms);
666
667
668 if (!encryption_algorithm_found)
669 {
670 /* one of needed transforms could not be found */
671 iterator->reset(iterator);
672 allocator_free(tmp_proposals);
673 return FAILED;
674 }
675
676 }
677 }
678
679 iterator->destroy(iterator);
680
681 *proposals = tmp_proposals;
682 *proposal_count = found_suites;
683
684 return SUCCESS;
685 }
686
687
688 /**
689 * Implementation of private_sa_payload_t.compute_length.
690 */
691 static void compute_length (private_sa_payload_t *this)
692 {
693 iterator_t *iterator;
694 size_t length = SA_PAYLOAD_HEADER_LENGTH;
695 iterator = this->proposals->create_iterator(this->proposals,TRUE);
696 while (iterator->has_next(iterator))
697 {
698 payload_t *current_proposal;
699 iterator->current(iterator,(void **) &current_proposal);
700 length += current_proposal->get_length(current_proposal);
701 }
702 iterator->destroy(iterator);
703
704 this->payload_length = length;
705 }
706
707 /*
708 * Described in header.
709 */
710 sa_payload_t *sa_payload_create()
711 {
712 private_sa_payload_t *this = allocator_alloc_thing(private_sa_payload_t);
713
714 /* public interface */
715 this->public.payload_interface.verify = (status_t (*) (payload_t *))verify;
716 this->public.payload_interface.get_encoding_rules = (void (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
717 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
718 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
719 this->public.payload_interface.set_next_type = (void (*) (payload_t *,payload_type_t)) set_next_type;
720 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_type;
721 this->public.payload_interface.destroy = (void (*) (payload_t *))destroy;
722
723 /* public functions */
724 this->public.create_proposal_substructure_iterator = (iterator_t* (*) (sa_payload_t *,bool)) create_proposal_substructure_iterator;
725 this->public.add_proposal_substructure = (void (*) (sa_payload_t *,proposal_substructure_t *)) add_proposal_substructure;
726 this->public.get_ike_proposals = (status_t (*) (sa_payload_t *, ike_proposal_t **, size_t *)) get_ike_proposals;
727 this->public.get_child_proposals = (status_t (*) (sa_payload_t *, child_proposal_t **, size_t *)) get_child_proposals;
728 this->public.destroy = (void (*) (sa_payload_t *)) destroy;
729
730 /* private functions */
731 this->compute_length = compute_length;
732
733 /* set default values of the fields */
734 this->critical = SA_PAYLOAD_CRITICAL_FLAG;
735 this->next_payload = NO_PAYLOAD;
736 this->payload_length = SA_PAYLOAD_HEADER_LENGTH;
737
738 this->proposals = linked_list_create();
739 return (&(this->public));
740 }
741
742 /*
743 * Described in header.
744 */
745 sa_payload_t *sa_payload_create_from_ike_proposals(ike_proposal_t *proposals, size_t proposal_count)
746 {
747 int i;
748 sa_payload_t *sa_payload= sa_payload_create();
749
750 for (i = 0; i < proposal_count; i++)
751 {
752 proposal_substructure_t *proposal_substructure;
753 transform_substructure_t *encryption_algorithm;
754 transform_substructure_t *integrity_algorithm;
755 transform_substructure_t *pseudo_random_function;
756 transform_substructure_t *diffie_hellman_group;
757
758 /* create proposal substructure */
759 proposal_substructure = proposal_substructure_create();
760 proposal_substructure->set_protocol_id(proposal_substructure,IKE);
761 proposal_substructure->set_proposal_number(proposal_substructure,(i + 1));
762
763 /* create transform substructures to hold each specific transform for an ike proposal */
764 encryption_algorithm = transform_substructure_create_type(ENCRYPTION_ALGORITHM,proposals[i].encryption_algorithm,proposals[i].encryption_algorithm_key_length);
765 proposal_substructure->add_transform_substructure(proposal_substructure,encryption_algorithm);
766
767 pseudo_random_function = transform_substructure_create_type(PSEUDO_RANDOM_FUNCTION,proposals[i].pseudo_random_function,proposals[i].pseudo_random_function_key_length);
768 proposal_substructure->add_transform_substructure(proposal_substructure,pseudo_random_function);
769
770 integrity_algorithm = transform_substructure_create_type(INTEGRITY_ALGORITHM,proposals[i].integrity_algorithm,proposals[i].integrity_algorithm_key_length);
771 proposal_substructure->add_transform_substructure(proposal_substructure,integrity_algorithm);
772
773 diffie_hellman_group = transform_substructure_create_type(DIFFIE_HELLMAN_GROUP,proposals[i].diffie_hellman_group,0);
774 proposal_substructure->add_transform_substructure(proposal_substructure,diffie_hellman_group);
775
776 /* add proposal to sa payload */
777 sa_payload->add_proposal_substructure(sa_payload,proposal_substructure);
778 }
779
780 return sa_payload;
781 }
782
783 /*
784 * Described in header.
785 */
786 sa_payload_t *sa_payload_create_from_child_proposals(child_proposal_t *proposals, size_t proposal_count)
787 {
788 int i;
789 sa_payload_t *sa_payload= sa_payload_create();
790
791 for (i = 0; i < proposal_count; i++)
792 {
793 /* first the AH part is created */
794 if (proposals[i].ah.is_set)
795 {
796 transform_substructure_t *integrity_algorithm;
797 proposal_substructure_t *proposal_substructure;
798 chunk_t spi;
799
800 proposal_substructure = proposal_substructure_create();
801 proposal_substructure->set_protocol_id(proposal_substructure,AH);
802 proposal_substructure->set_proposal_number(proposal_substructure,(i + 1));
803 spi.ptr = proposals[i].ah.spi;
804 spi.len = 4;
805 proposal_substructure->set_spi(proposal_substructure,spi);
806
807 integrity_algorithm = transform_substructure_create_type(INTEGRITY_ALGORITHM,proposals[i].ah.integrity_algorithm,proposals[i].ah.integrity_algorithm_key_size);
808 proposal_substructure->add_transform_substructure(proposal_substructure,integrity_algorithm);
809 if (proposals[i].ah.diffie_hellman_group != MODP_UNDEFINED)
810 {
811 transform_substructure_t *diffie_hellman_group;
812 diffie_hellman_group = transform_substructure_create_type(DIFFIE_HELLMAN_GROUP,proposals[i].ah.diffie_hellman_group,0);
813 proposal_substructure->add_transform_substructure(proposal_substructure,diffie_hellman_group);
814
815 }
816 if (proposals[i].ah.extended_sequence_numbers == EXT_SEQ_NUMBERS)
817 {
818 transform_substructure_t *extended_sequence_numbers;
819 extended_sequence_numbers = transform_substructure_create_type(EXTENDED_SEQUENCE_NUMBERS,proposals[i].ah.extended_sequence_numbers,0);
820 proposal_substructure->add_transform_substructure(proposal_substructure,extended_sequence_numbers);
821 }
822
823 sa_payload->add_proposal_substructure(sa_payload,proposal_substructure);
824 }
825
826 /* then the ESP part is created */
827 if (proposals[i].esp.is_set)
828 {
829 transform_substructure_t *encryption_algorithm;
830 proposal_substructure_t *proposal_substructure;
831 chunk_t spi;
832
833 proposal_substructure = proposal_substructure_create();
834 proposal_substructure->set_protocol_id(proposal_substructure,ESP);
835 proposal_substructure->set_proposal_number(proposal_substructure,(i + 1));
836 spi.ptr = proposals[i].esp.spi;
837 spi.len = 4;
838 proposal_substructure->set_spi(proposal_substructure,spi);
839
840 encryption_algorithm = transform_substructure_create_type(ENCRYPTION_ALGORITHM,proposals[i].esp.encryption_algorithm,proposals[i].esp.encryption_algorithm_key_size);
841 proposal_substructure->add_transform_substructure(proposal_substructure,encryption_algorithm);
842
843 if (proposals[i].esp.integrity_algorithm != AUTH_UNDEFINED)
844 {
845 transform_substructure_t *integrity_algorithm;
846 integrity_algorithm = transform_substructure_create_type(INTEGRITY_ALGORITHM,proposals[i].esp.integrity_algorithm,proposals[i].esp.integrity_algorithm_key_size);
847 proposal_substructure->add_transform_substructure(proposal_substructure,integrity_algorithm);
848
849 }
850
851 if (proposals[i].esp.diffie_hellman_group != MODP_UNDEFINED)
852 {
853 transform_substructure_t *diffie_hellman_group;
854 diffie_hellman_group = transform_substructure_create_type(DIFFIE_HELLMAN_GROUP,proposals[i].esp.diffie_hellman_group,0);
855 proposal_substructure->add_transform_substructure(proposal_substructure,diffie_hellman_group);
856
857 }
858 if (proposals[i].esp.extended_sequence_numbers == EXT_SEQ_NUMBERS)
859 {
860 transform_substructure_t *extended_sequence_numbers;
861 extended_sequence_numbers = transform_substructure_create_type(EXTENDED_SEQUENCE_NUMBERS,proposals[i].esp.extended_sequence_numbers,0);
862 proposal_substructure->add_transform_substructure(proposal_substructure,extended_sequence_numbers);
863 }
864
865 sa_payload->add_proposal_substructure(sa_payload,proposal_substructure);
866 }
867
868 }
869
870 return sa_payload;
871 }