- fixed memleaks
[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 /* must not be smaller then proceeding one */
154 status = FAILED;
155 break;
156 }
157
158 status = current_proposal->payload_interface.verify(&(current_proposal->payload_interface));
159 if (status != SUCCESS)
160 {
161 break;
162 }
163 first = FALSE;
164 }
165
166 iterator->destroy(iterator);
167 return status;
168 }
169
170
171 /**
172 * Implementation of payload_t.destroy and sa_payload_t.destroy.
173 */
174 static status_t destroy(private_sa_payload_t *this)
175 {
176 /* all proposals are getting destroyed */
177 while (this->proposals->get_count(this->proposals) > 0)
178 {
179 proposal_substructure_t *current_proposal;
180 this->proposals->remove_last(this->proposals,(void **)&current_proposal);
181 current_proposal->destroy(current_proposal);
182 }
183 this->proposals->destroy(this->proposals);
184
185 allocator_free(this);
186
187 return SUCCESS;
188 }
189
190 /**
191 * Implementation of payload_t.get_encoding_rules.
192 */
193 static void get_encoding_rules(private_sa_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
194 {
195 *rules = sa_payload_encodings;
196 *rule_count = sizeof(sa_payload_encodings) / sizeof(encoding_rule_t);
197 }
198
199 /**
200 * Implementation of payload_t.get_type.
201 */
202 static payload_type_t get_type(private_sa_payload_t *this)
203 {
204 return SECURITY_ASSOCIATION;
205 }
206
207 /**
208 * Implementation of payload_t.get_next_type.
209 */
210 static payload_type_t get_next_type(private_sa_payload_t *this)
211 {
212 return (this->next_payload);
213 }
214
215 /**
216 * Implementation of payload_t.set_next_type.
217 */
218 static void set_next_type(private_sa_payload_t *this,payload_type_t type)
219 {
220 this->next_payload = type;
221 }
222
223 /**
224 * Implementation of payload_t.get_length.
225 */
226 static size_t get_length(private_sa_payload_t *this)
227 {
228 this->compute_length(this);
229 return this->payload_length;
230 }
231
232 /**
233 * Implementation of sa_payload_t.create_proposal_substructure_iterator.
234 */
235 static iterator_t *create_proposal_substructure_iterator (private_sa_payload_t *this,bool forward)
236 {
237 return this->proposals->create_iterator(this->proposals,forward);
238 }
239
240 /**
241 * Implementation of sa_payload_t.add_proposal_substructure.
242 */
243 static void add_proposal_substructure (private_sa_payload_t *this,proposal_substructure_t *proposal)
244 {
245 status_t status;
246 if (this->proposals->get_count(this->proposals) > 0)
247 {
248 proposal_substructure_t *last_proposal;
249 status = this->proposals->get_last(this->proposals,(void **) &last_proposal);
250 /* last transform is now not anymore last one */
251 last_proposal->set_is_last_proposal(last_proposal,FALSE);
252 }
253 proposal->set_is_last_proposal(proposal,TRUE);
254
255 this->proposals->insert_last(this->proposals,(void *) proposal);
256 this->compute_length(this);
257 }
258
259 /**
260 * Implementation of sa_payload_t.add_child_proposal.
261 */
262 static void add_child_proposal(private_sa_payload_t *this, child_proposal_t *proposal)
263 {
264 proposal_substructure_t *substructure;
265 protocol_id_t proto[2];
266 u_int i;
267
268 /* build the substructures for every protocol */
269 proposal->get_protocols(proposal, proto);
270 for (i = 0; i<2; i++)
271 {
272 if (proto[i] != UNDEFINED_PROTOCOL_ID)
273 {
274 substructure = proposal_substructure_create_from_child_proposal(proposal, proto[i]);
275 add_proposal_substructure(this, substructure);
276 }
277 }
278 }
279
280
281 /**
282 * Implementation of sa_payload_t.get_ike_proposals.
283 */
284 static status_t get_ike_proposals (private_sa_payload_t *this,ike_proposal_t ** proposals, size_t *proposal_count)
285 {
286 int found_ike_proposals = 0;
287 int current_proposal_number = 0;
288 iterator_t *iterator;
289 ike_proposal_t *tmp_proposals;
290
291 iterator = this->proposals->create_iterator(this->proposals,TRUE);
292
293 /* first find out the number of ike proposals and check their number of transforms and
294 * if the SPI is empty!*/
295 while (iterator->has_next(iterator))
296 {
297 proposal_substructure_t *current_proposal;
298 iterator->current(iterator,(void **)&(current_proposal));
299 if (current_proposal->get_protocol_id(current_proposal) == IKE)
300 {
301 /* a ike proposal consists of an empty spi*/
302 if (current_proposal->get_spi_size(current_proposal) != 0)
303 {
304 iterator->destroy(iterator);
305 return FAILED;
306 }
307
308 found_ike_proposals++;
309 }
310 }
311 iterator->reset(iterator);
312
313 if (found_ike_proposals == 0)
314 {
315 iterator->destroy(iterator);
316 return NOT_FOUND;
317 }
318
319 /* allocate memory to hold each proposal as ike_proposal_t */
320
321 tmp_proposals = allocator_alloc(found_ike_proposals * sizeof(ike_proposal_t));
322
323 /* create from each proposal_substructure a ike_proposal_t data area*/
324 while (iterator->has_next(iterator))
325 {
326 proposal_substructure_t *current_proposal;
327 iterator->current(iterator,(void **)&(current_proposal));
328 if (current_proposal->get_protocol_id(current_proposal) == IKE)
329 {
330 bool encryption_algorithm_found = FALSE;
331 bool integrity_algorithm_found = FALSE;
332 bool pseudo_random_function_found = FALSE;
333 bool diffie_hellman_group_found = FALSE;
334 status_t status;
335 iterator_t *transforms;
336
337 transforms = current_proposal->create_transform_substructure_iterator(current_proposal,TRUE);
338 while (transforms->has_next(transforms))
339 {
340 transform_substructure_t *current_transform;
341 transforms->current(transforms,(void **)&(current_transform));
342
343 switch (current_transform->get_transform_type(current_transform))
344 {
345 case ENCRYPTION_ALGORITHM:
346 {
347 tmp_proposals[current_proposal_number].encryption_algorithm = current_transform->get_transform_id(current_transform);
348 status = current_transform->get_key_length(current_transform,&(tmp_proposals[current_proposal_number].encryption_algorithm_key_length));
349 if (status == SUCCESS)
350 {
351 encryption_algorithm_found = TRUE;
352 }
353 break;
354 }
355 case INTEGRITY_ALGORITHM:
356 {
357 tmp_proposals[current_proposal_number].integrity_algorithm = current_transform->get_transform_id(current_transform);
358 status = current_transform->get_key_length(current_transform,&(tmp_proposals[current_proposal_number].integrity_algorithm_key_length));
359 if (status == SUCCESS)
360 {
361 integrity_algorithm_found = TRUE;
362 }
363 break;
364 }
365 case PSEUDO_RANDOM_FUNCTION:
366 {
367 tmp_proposals[current_proposal_number].pseudo_random_function = current_transform->get_transform_id(current_transform);
368 status = current_transform->get_key_length(current_transform,&(tmp_proposals[current_proposal_number].pseudo_random_function_key_length));
369 if (status == SUCCESS)
370 {
371 pseudo_random_function_found = TRUE;
372 }
373 break;
374 }
375 case DIFFIE_HELLMAN_GROUP:
376 {
377 tmp_proposals[current_proposal_number].diffie_hellman_group = current_transform->get_transform_id(current_transform);
378 diffie_hellman_group_found = TRUE;
379 break;
380 }
381 default:
382 {
383 /* not a transform of an ike proposal. Break here */
384 break;
385 }
386 }
387
388 }
389
390 transforms->destroy(transforms);
391
392 if ((!encryption_algorithm_found) ||
393 (!integrity_algorithm_found) ||
394 (!pseudo_random_function_found) ||
395 (!diffie_hellman_group_found))
396 {
397 /* one of needed transforms could not be found */
398 iterator->reset(iterator);
399 allocator_free(tmp_proposals);
400 return FAILED;
401 }
402
403 current_proposal_number++;
404 }
405 }
406
407 iterator->destroy(iterator);
408
409 *proposals = tmp_proposals;
410 *proposal_count = found_ike_proposals;
411
412 return SUCCESS;
413 }
414
415 /**
416 * Implementation of sa_payload_t.get_child_proposals.
417 */
418 static linked_list_t *get_child_proposals(private_sa_payload_t *this)
419 {
420 int proposal_struct_number = 0;
421 iterator_t *iterator;
422 child_proposal_t *proposal;
423 linked_list_t *proposal_list;
424
425 /* this list will hold our proposals */
426 proposal_list = linked_list_create();
427
428 /* iterate over structures, one OR MORE structures will result in a child_proposal */
429 iterator = this->proposals->create_iterator(this->proposals,TRUE);
430 while (iterator->has_next(iterator))
431 {
432 proposal_substructure_t *proposal_struct;
433 iterator->current(iterator,(void **)&(proposal_struct));
434
435 if (proposal_struct->get_proposal_number(proposal_struct) > proposal_struct_number)
436 {
437 /* here starts a new proposal, create a new one and add it to the list */
438 proposal_struct_number = proposal_struct->get_proposal_number(proposal_struct);
439 proposal = child_proposal_create(proposal_struct_number);
440 proposal_list->insert_last(proposal_list, proposal);
441 }
442 /* proposal_substructure_t does the dirty work and builds up the proposal */
443 proposal_struct->add_to_child_proposal(proposal_struct, proposal);
444 }
445 iterator->destroy(iterator);
446 return proposal_list;
447 }
448
449 /**
450 * Implementation of private_sa_payload_t.compute_length.
451 */
452 static void compute_length (private_sa_payload_t *this)
453 {
454 iterator_t *iterator;
455 size_t length = SA_PAYLOAD_HEADER_LENGTH;
456 iterator = this->proposals->create_iterator(this->proposals,TRUE);
457 while (iterator->has_next(iterator))
458 {
459 payload_t *current_proposal;
460 iterator->current(iterator,(void **) &current_proposal);
461 length += current_proposal->get_length(current_proposal);
462 }
463 iterator->destroy(iterator);
464
465 this->payload_length = length;
466 }
467
468 /*
469 * Described in header.
470 */
471 sa_payload_t *sa_payload_create()
472 {
473 private_sa_payload_t *this = allocator_alloc_thing(private_sa_payload_t);
474
475 /* public interface */
476 this->public.payload_interface.verify = (status_t (*) (payload_t *))verify;
477 this->public.payload_interface.get_encoding_rules = (void (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
478 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
479 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
480 this->public.payload_interface.set_next_type = (void (*) (payload_t *,payload_type_t)) set_next_type;
481 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_type;
482 this->public.payload_interface.destroy = (void (*) (payload_t *))destroy;
483
484 /* public functions */
485 this->public.create_proposal_substructure_iterator = (iterator_t* (*) (sa_payload_t *,bool)) create_proposal_substructure_iterator;
486 this->public.add_proposal_substructure = (void (*) (sa_payload_t *,proposal_substructure_t *)) add_proposal_substructure;
487 this->public.get_ike_proposals = (status_t (*) (sa_payload_t *, ike_proposal_t **, size_t *)) get_ike_proposals;
488 this->public.get_child_proposals = (linked_list_t* (*) (sa_payload_t *)) get_child_proposals;
489 this->public.destroy = (void (*) (sa_payload_t *)) destroy;
490
491 /* private functions */
492 this->compute_length = compute_length;
493
494 /* set default values of the fields */
495 this->critical = SA_PAYLOAD_CRITICAL_FLAG;
496 this->next_payload = NO_PAYLOAD;
497 this->payload_length = SA_PAYLOAD_HEADER_LENGTH;
498
499 this->proposals = linked_list_create();
500 return (&(this->public));
501 }
502
503 /*
504 * Described in header.
505 */
506 sa_payload_t *sa_payload_create_from_ike_proposals(ike_proposal_t *proposals, size_t proposal_count)
507 {
508 int i;
509 sa_payload_t *sa_payload= sa_payload_create();
510
511 for (i = 0; i < proposal_count; i++)
512 {
513 proposal_substructure_t *proposal_substructure;
514 transform_substructure_t *encryption_algorithm;
515 transform_substructure_t *integrity_algorithm;
516 transform_substructure_t *pseudo_random_function;
517 transform_substructure_t *diffie_hellman_group;
518
519 /* create proposal substructure */
520 proposal_substructure = proposal_substructure_create();
521 proposal_substructure->set_protocol_id(proposal_substructure,IKE);
522 proposal_substructure->set_proposal_number(proposal_substructure,(i + 1));
523
524
525 /* create transform substructures to hold each specific transform for an ike proposal */
526 encryption_algorithm = transform_substructure_create_type(ENCRYPTION_ALGORITHM,proposals[i].encryption_algorithm,proposals[i].encryption_algorithm_key_length);
527 proposal_substructure->add_transform_substructure(proposal_substructure,encryption_algorithm);
528
529 pseudo_random_function = transform_substructure_create_type(PSEUDO_RANDOM_FUNCTION,proposals[i].pseudo_random_function,proposals[i].pseudo_random_function_key_length);
530 proposal_substructure->add_transform_substructure(proposal_substructure,pseudo_random_function);
531
532 integrity_algorithm = transform_substructure_create_type(INTEGRITY_ALGORITHM,proposals[i].integrity_algorithm,proposals[i].integrity_algorithm_key_length);
533 proposal_substructure->add_transform_substructure(proposal_substructure,integrity_algorithm);
534
535 diffie_hellman_group = transform_substructure_create_type(DIFFIE_HELLMAN_GROUP,proposals[i].diffie_hellman_group,0);
536 proposal_substructure->add_transform_substructure(proposal_substructure,diffie_hellman_group);
537
538 /* add proposal to sa payload */
539 sa_payload->add_proposal_substructure(sa_payload,proposal_substructure);
540 }
541
542 return sa_payload;
543 }
544
545 /*
546 * Described in header.
547 */
548 sa_payload_t *sa_payload_create_from_child_proposal_list(linked_list_t *proposals)
549 {
550 iterator_t *iterator;
551 child_proposal_t *proposal;
552 sa_payload_t *sa_payload = sa_payload_create();
553
554 /* add every payload from the list */
555 iterator = proposals->create_iterator(proposals, TRUE);
556 while (iterator->has_next(iterator))
557 {
558 iterator->current(iterator, (void**)&proposal);
559 add_child_proposal((private_sa_payload_t*)sa_payload, proposal);
560 }
561 iterator->destroy(iterator);
562
563 return sa_payload;
564 }
565
566 /*
567 * Described in header.
568 */
569 sa_payload_t *sa_payload_create_from_child_proposal(child_proposal_t *proposal)
570 {
571 sa_payload_t *sa_payload = sa_payload_create();
572
573 add_child_proposal((private_sa_payload_t*)sa_payload, proposal);
574
575 return sa_payload;
576 }