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