- installing of child sa works
[strongswan.git] / Source / charon / encoding / payloads / proposal_substructure.c
1 /**
2 * @file proposal_substructure.h
3 *
4 * @brief Implementation of proposal_substructure_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 "proposal_substructure.h"
27
28 #include <encoding/payloads/encodings.h>
29 #include <encoding/payloads/transform_substructure.h>
30 #include <types.h>
31 #include <utils/allocator.h>
32 #include <utils/linked_list.h>
33
34
35 /**
36 * IKEv1 Value for a proposal payload.
37 */
38 #define PROPOSAL_TYPE_VALUE 2
39
40
41 typedef struct private_proposal_substructure_t private_proposal_substructure_t;
42
43 /**
44 * Private data of an proposal_substructure_t object.
45 *
46 */
47 struct private_proposal_substructure_t {
48 /**
49 * Public proposal_substructure_t interface.
50 */
51 proposal_substructure_t public;
52
53 /**
54 * Next payload type.
55 */
56 u_int8_t next_payload;
57
58 /**
59 * Length of this payload.
60 */
61 u_int16_t proposal_length;
62
63 /**
64 * Proposal number.
65 */
66 u_int8_t proposal_number;
67
68 /**
69 * Protocol ID.
70 */
71 u_int8_t protocol_id;
72
73 /**
74 * SPI size of the following SPI.
75 */
76 u_int8_t spi_size;
77
78 /**
79 * Number of transforms.
80 */
81 u_int8_t transforms_count;
82
83 /**
84 * SPI is stored as chunk.
85 */
86 chunk_t spi;
87
88 /**
89 * Transforms are stored in a linked_list_t.
90 */
91 linked_list_t * transforms;
92
93 /**
94 * @brief Computes the length of this substructure.
95 *
96 * @param this calling private_proposal_substructure_t object
97 */
98 void (*compute_length) (private_proposal_substructure_t *this);
99 };
100
101 /**
102 * Encoding rules to parse or generate a Proposal substructure.
103 *
104 * The defined offsets are the positions in a object of type
105 * private_proposal_substructure_t.
106 *
107 */
108 encoding_rule_t proposal_substructure_encodings[] = {
109 /* 1 Byte next payload type, stored in the field next_payload */
110 { U_INT_8, offsetof(private_proposal_substructure_t, next_payload) },
111 /* Reserved Byte is skipped */
112 { RESERVED_BYTE, 0 },
113 /* Length of the whole proposal substructure payload*/
114 { PAYLOAD_LENGTH, offsetof(private_proposal_substructure_t, proposal_length) },
115 /* proposal number is a number of 8 bit */
116 { U_INT_8, offsetof(private_proposal_substructure_t, proposal_number) },
117 /* protocol ID is a number of 8 bit */
118 { U_INT_8, offsetof(private_proposal_substructure_t, protocol_id) },
119 /* SPI Size has its own type */
120 { SPI_SIZE, offsetof(private_proposal_substructure_t, spi_size) },
121 /* Number of transforms is a number of 8 bit */
122 { U_INT_8, offsetof(private_proposal_substructure_t, transforms_count) },
123 /* SPI is a chunk of variable size*/
124 { SPI, offsetof(private_proposal_substructure_t, spi) },
125 /* Transforms are stored in a transform substructure,
126 offset points to a linked_list_t pointer */
127 { TRANSFORMS, offsetof(private_proposal_substructure_t, transforms) }
128 };
129
130 /*
131 1 2 3
132 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
133 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
134 ! 0 (last) or 2 ! RESERVED ! Proposal Length !
135 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
136 ! Proposal # ! Protocol ID ! SPI Size !# of Transforms!
137 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
138 ~ SPI (variable) ~
139 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
140 ! !
141 ~ <Transforms> ~
142 ! !
143 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
144 */
145
146 /**
147 * Implementation of payload_t.verify.
148 */
149 static status_t verify(private_proposal_substructure_t *this)
150 {
151 status_t status = SUCCESS;
152 iterator_t *iterator;
153
154 if ((this->next_payload != NO_PAYLOAD) && (this->next_payload != 2))
155 {
156 /* must be 0 or 2 */
157 return FAILED;
158 }
159 if (this->transforms_count != this->transforms->get_count(this->transforms))
160 {
161 /* must be the same! */
162 return FAILED;
163 }
164
165 if ((this->protocol_id == 0) || (this->protocol_id >= 4))
166 {
167 /* reserved are not supported */
168 return FAILED;
169 }
170
171 iterator = this->transforms->create_iterator(this->transforms,TRUE);
172
173 while(iterator->has_next(iterator))
174 {
175 payload_t *current_transform;
176 iterator->current(iterator,(void **)&current_transform);
177
178 status = current_transform->verify(current_transform);
179 if (status != SUCCESS)
180 {
181 break;
182 }
183 }
184
185 iterator->destroy(iterator);
186
187
188 /* proposal number is checked in SA payload */
189 return status;
190 }
191
192 /**
193 * Implementation of payload_t.get_encoding_rules.
194 */
195 static void get_encoding_rules(private_proposal_substructure_t *this, encoding_rule_t **rules, size_t *rule_count)
196 {
197 *rules = proposal_substructure_encodings;
198 *rule_count = sizeof(proposal_substructure_encodings) / sizeof(encoding_rule_t);
199 }
200
201 /**
202 * Implementation of payload_t.get_type.
203 */
204 static payload_type_t get_type(private_proposal_substructure_t *this)
205 {
206 return PROPOSAL_SUBSTRUCTURE;
207 }
208
209 /**
210 * Implementation of payload_t.get_next_type.
211 */
212 static payload_type_t get_next_type(private_proposal_substructure_t *this)
213 {
214 return (this->next_payload);
215 }
216
217 /**
218 * Implementation of payload_t.set_next_type.
219 */
220 static void set_next_type(private_proposal_substructure_t *this,payload_type_t type)
221 {
222 }
223
224 /**
225 * Implementation of payload_t.get_length.
226 */
227 static size_t get_length(private_proposal_substructure_t *this)
228 {
229 this->compute_length(this);
230 return this->proposal_length;
231 }
232
233 /**
234 * Implementation of proposal_substructure_t.create_transform_substructure_iterator.
235 */
236 static iterator_t *create_transform_substructure_iterator (private_proposal_substructure_t *this,bool forward)
237 {
238 return (this->transforms->create_iterator(this->transforms,forward));
239 }
240
241 /**
242 * Implementation of proposal_substructure_t.add_transform_substructure.
243 */
244 static void add_transform_substructure (private_proposal_substructure_t *this,transform_substructure_t *transform)
245 {
246 status_t status;
247 if (this->transforms->get_count(this->transforms) > 0)
248 {
249 transform_substructure_t *last_transform;
250 status = this->transforms->get_last(this->transforms,(void **) &last_transform);
251 /* last transform is now not anymore last one */
252 last_transform->set_is_last_transform(last_transform,FALSE);
253
254 }
255 transform->set_is_last_transform(transform,TRUE);
256
257 this->transforms->insert_last(this->transforms,(void *) transform);
258 this->compute_length(this);
259 }
260
261 /**
262 * Implementation of proposal_substructure_t.proposal_substructure_t.
263 */
264 static void set_is_last_proposal (private_proposal_substructure_t *this, bool is_last)
265 {
266 this->next_payload = (is_last) ? 0: PROPOSAL_TYPE_VALUE;
267 }
268
269
270 /**
271 * Implementation of proposal_substructure_t.set_proposal_number.
272 */
273 static void set_proposal_number(private_proposal_substructure_t *this,u_int8_t proposal_number)
274 {
275 this->proposal_number = proposal_number;
276 }
277
278 /**
279 * Implementation of proposal_substructure_t.get_proposal_number.
280 */
281 static u_int8_t get_proposal_number (private_proposal_substructure_t *this)
282 {
283 return (this->proposal_number);
284 }
285
286 /**
287 * Implementation of proposal_substructure_t.set_protocol_id.
288 */
289 static void set_protocol_id(private_proposal_substructure_t *this,u_int8_t protocol_id)
290 {
291 this->protocol_id = protocol_id;
292 }
293
294 /**
295 * Implementation of proposal_substructure_t.get_protocol_id.
296 */
297 static u_int8_t get_protocol_id (private_proposal_substructure_t *this)
298 {
299 return (this->protocol_id);
300 }
301
302 /**
303 * Implementation of proposal_substructure_t.set_spi.
304 */
305 static void set_spi (private_proposal_substructure_t *this, chunk_t spi)
306 {
307 /* first delete already set spi value */
308 if (this->spi.ptr != NULL)
309 {
310 allocator_free(this->spi.ptr);
311 this->spi.ptr = NULL;
312 this->spi.len = 0;
313 this->compute_length(this);
314 }
315
316 this->spi.ptr = allocator_clone_bytes(spi.ptr,spi.len);
317 this->spi.len = spi.len;
318 this->spi_size = spi.len;
319 this->compute_length(this);
320 }
321
322 /**
323 * Implementation of proposal_substructure_t.get_spi.
324 */
325 static chunk_t get_spi (private_proposal_substructure_t *this)
326 {
327 chunk_t spi;
328 spi.ptr = this->spi.ptr;
329 spi.len = this->spi.len;
330
331 return spi;
332 }
333
334 /**
335 * Implementation of proposal_substructure_t.get_info_for_transform_type.
336 */
337 static status_t get_info_for_transform_type (private_proposal_substructure_t *this,transform_type_t type, u_int16_t *transform_id, u_int16_t *key_length)
338 {
339 iterator_t *iterator;
340 status_t status;
341 u_int16_t found_transform_id;
342 u_int16_t found_key_length;
343
344 iterator = this->transforms->create_iterator(this->transforms,TRUE);
345
346 while (iterator->has_next(iterator))
347 {
348 transform_substructure_t *current_transform;
349 status = iterator->current(iterator,(void **) &current_transform);
350 if (status != SUCCESS)
351 {
352 break;
353 }
354 if (current_transform->get_transform_type(current_transform) == type)
355 {
356 /* now get data for specific type */
357 found_transform_id = current_transform->get_transform_id(current_transform);
358 status = current_transform->get_key_length(current_transform,&found_key_length);
359 *transform_id = found_transform_id;
360 *key_length = found_key_length;
361 iterator->destroy(iterator);
362 return status;
363 }
364 }
365 iterator->destroy(iterator);
366 return NOT_FOUND;
367 }
368
369 /**
370 * Implementation of private_proposal_substructure_t.compute_length.
371 */
372 static void compute_length (private_proposal_substructure_t *this)
373 {
374 iterator_t *iterator;
375 size_t transforms_count = 0;
376 size_t length = PROPOSAL_SUBSTRUCTURE_HEADER_LENGTH;
377 iterator = this->transforms->create_iterator(this->transforms,TRUE);
378 while (iterator->has_next(iterator))
379 {
380 payload_t * current_transform;
381 iterator->current(iterator,(void **) &current_transform);
382 length += current_transform->get_length(current_transform);
383 transforms_count++;
384 }
385 iterator->destroy(iterator);
386
387 length += this->spi.len;
388 this->transforms_count = transforms_count;
389 this->proposal_length = length;
390 }
391
392 /**
393 * Implementation of proposal_substructure_t.get_transform_count.
394 */
395 static size_t get_transform_count (private_proposal_substructure_t *this)
396 {
397 return this->transforms->get_count(this->transforms);
398 }
399
400 /**
401 * Implementation of proposal_substructure_t.get_spi_size.
402 */
403 static size_t get_spi_size (private_proposal_substructure_t *this)
404 {
405 return this->spi.len;
406 }
407
408 /**
409 * Implementation of proposal_substructure_t.add_to_proposal.
410 */
411 void add_to_proposal(private_proposal_substructure_t *this, proposal_t *proposal)
412 {
413 iterator_t *iterator = this->transforms->create_iterator(this->transforms, TRUE);
414 u_int32_t spi;
415
416
417 while (iterator->has_next(iterator))
418 {
419 transform_substructure_t *transform;
420 transform_type_t transform_type;
421 u_int16_t transform_id;
422 u_int16_t key_length = 0;
423
424 iterator->current(iterator, (void**)&transform);
425
426 transform_type = transform->get_transform_type(transform);
427 transform_id = transform->get_transform_id(transform);
428 transform->get_key_length(transform, &key_length);
429
430 proposal->add_algorithm(proposal, this->protocol_id, transform_type, transform_id, key_length);
431 }
432 iterator->destroy(iterator);
433
434 spi = *((u_int32_t*)this->spi.ptr);
435
436 proposal->set_spi(proposal, this->protocol_id, spi);
437 }
438
439 /**
440 * Implementation of proposal_substructure_t.clone.
441 */
442 static private_proposal_substructure_t* clone(private_proposal_substructure_t *this)
443 {
444 private_proposal_substructure_t * new_clone;
445 iterator_t *transforms;
446
447 new_clone = (private_proposal_substructure_t *) proposal_substructure_create();
448
449 new_clone->next_payload = this->next_payload;
450 new_clone->proposal_number = this->proposal_number;
451 new_clone->protocol_id = this->protocol_id;
452 new_clone->spi_size = this->spi_size;
453 if (this->spi.ptr != NULL)
454 {
455 new_clone->spi.ptr = allocator_clone_bytes(this->spi.ptr,this->spi.len);
456 new_clone->spi.len = this->spi.len;
457 }
458
459 transforms = this->transforms->create_iterator(this->transforms,FALSE);
460
461 while (transforms->has_next(transforms))
462 {
463 transform_substructure_t *current_transform;
464 transform_substructure_t *current_transform_clone;
465
466 transforms->current(transforms,(void **) &current_transform);
467
468 current_transform_clone = current_transform->clone(current_transform);
469
470 new_clone->public.add_transform_substructure(&(new_clone->public),current_transform_clone);
471 }
472
473 transforms->destroy(transforms);
474
475 return new_clone;
476 }
477
478 /**
479 * Implements payload_t's and proposal_substructure_t's destroy function.
480 * See #payload_s.destroy or proposal_substructure_s.destroy for description.
481 */
482 static status_t destroy(private_proposal_substructure_t *this)
483 {
484 /* all proposals are getting destroyed */
485 while (this->transforms->get_count(this->transforms) > 0)
486 {
487 transform_substructure_t *current_transform;
488 if (this->transforms->remove_last(this->transforms,(void **)&current_transform) != SUCCESS)
489 {
490 break;
491 }
492 current_transform->destroy(current_transform);
493 }
494 this->transforms->destroy(this->transforms);
495
496 if (this->spi.ptr != NULL)
497 {
498 allocator_free(this->spi.ptr);
499 }
500
501 allocator_free(this);
502
503 return SUCCESS;
504 }
505
506 /*
507 * Described in header.
508 */
509 proposal_substructure_t *proposal_substructure_create()
510 {
511 private_proposal_substructure_t *this = allocator_alloc_thing(private_proposal_substructure_t);
512
513 /* interface functions */
514 this->public.payload_interface.verify = (status_t (*) (payload_t *))verify;
515 this->public.payload_interface.get_encoding_rules = (void (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
516 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
517 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
518 this->public.payload_interface.set_next_type = (void (*) (payload_t *,payload_type_t)) set_next_type;
519 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_type;
520 this->public.payload_interface.destroy = (void (*) (payload_t *))destroy;
521
522
523 /* public functions */
524 this->public.create_transform_substructure_iterator = (iterator_t* (*) (proposal_substructure_t *,bool)) create_transform_substructure_iterator;
525 this->public.add_transform_substructure = (void (*) (proposal_substructure_t *,transform_substructure_t *)) add_transform_substructure;
526 this->public.set_proposal_number = (void (*) (proposal_substructure_t *,u_int8_t))set_proposal_number;
527 this->public.get_proposal_number = (u_int8_t (*) (proposal_substructure_t *)) get_proposal_number;
528 this->public.set_protocol_id = (void (*) (proposal_substructure_t *,u_int8_t))set_protocol_id;
529 this->public.get_protocol_id = (u_int8_t (*) (proposal_substructure_t *)) get_protocol_id;
530 this->public.get_info_for_transform_type = (status_t (*) (proposal_substructure_t *,transform_type_t,u_int16_t *, u_int16_t *))get_info_for_transform_type;
531 this->public.set_is_last_proposal = (void (*) (proposal_substructure_t *,bool)) set_is_last_proposal;
532 this->public.add_to_proposal = (void (*) (proposal_substructure_t*,proposal_t*))add_to_proposal;
533 this->public.set_spi = (void (*) (proposal_substructure_t *,chunk_t))set_spi;
534 this->public.get_spi = (chunk_t (*) (proposal_substructure_t *)) get_spi;
535 this->public.get_transform_count = (size_t (*) (proposal_substructure_t *)) get_transform_count;
536 this->public.get_spi_size = (size_t (*) (proposal_substructure_t *)) get_spi_size;
537 this->public.clone = (proposal_substructure_t * (*) (proposal_substructure_t *)) clone;
538 this->public.destroy = (void (*) (proposal_substructure_t *)) destroy;
539
540 /* private functions */
541 this->compute_length = compute_length;
542
543 /* set default values of the fields */
544 this->next_payload = NO_PAYLOAD;
545 this->proposal_length = 0;
546 this->proposal_number = 0;
547 this->protocol_id = 0;
548 this->transforms_count = 0;
549 this->spi_size = 0;
550 this->spi.ptr = NULL;
551 this->spi.len = 0;
552
553 this->transforms = linked_list_create();
554
555 return (&(this->public));
556 }
557
558 /*
559 * Described in header.
560 */
561 proposal_substructure_t *proposal_substructure_create_from_proposal(proposal_t *proposal, protocol_id_t proto)
562 {
563 private_proposal_substructure_t *this = (private_proposal_substructure_t*)proposal_substructure_create();
564 iterator_t *iterator;
565 algorithm_t *algo;
566 transform_substructure_t *transform;
567
568 /* encryption algorithm is only availble in ESP */
569 iterator = proposal->create_algorithm_iterator(proposal, proto, ENCRYPTION_ALGORITHM);
570 while (iterator->has_next(iterator))
571 {
572 iterator->current(iterator, (void**)&algo);
573 transform = transform_substructure_create_type(ENCRYPTION_ALGORITHM, algo->algorithm, algo->key_size);
574 this->public.add_transform_substructure(&(this->public), transform);
575 }
576 iterator->destroy(iterator);
577
578 /* integrity algorithms */
579 iterator = proposal->create_algorithm_iterator(proposal, proto, INTEGRITY_ALGORITHM);
580 while (iterator->has_next(iterator))
581 {
582 algorithm_t *algo;
583 iterator->current(iterator, (void**)&algo);
584 transform = transform_substructure_create_type(INTEGRITY_ALGORITHM, algo->algorithm, algo->key_size);
585 this->public.add_transform_substructure(&(this->public), transform);
586 }
587 iterator->destroy(iterator);
588
589 /* prf algorithms */
590 iterator = proposal->create_algorithm_iterator(proposal, proto, PSEUDO_RANDOM_FUNCTION);
591 while (iterator->has_next(iterator))
592 {
593 algorithm_t *algo;
594 iterator->current(iterator, (void**)&algo);
595 transform = transform_substructure_create_type(PSEUDO_RANDOM_FUNCTION, algo->algorithm, algo->key_size);
596 this->public.add_transform_substructure(&(this->public), transform);
597 }
598 iterator->destroy(iterator);
599
600 /* dh groups */
601 iterator = proposal->create_algorithm_iterator(proposal, proto, DIFFIE_HELLMAN_GROUP);
602 while (iterator->has_next(iterator))
603 {
604 algorithm_t *algo;
605 iterator->current(iterator, (void**)&algo);
606 transform = transform_substructure_create_type(DIFFIE_HELLMAN_GROUP, algo->algorithm, 0);
607 this->public.add_transform_substructure(&(this->public), transform);
608 }
609 iterator->destroy(iterator);
610
611 /* extended sequence numbers */
612 iterator = proposal->create_algorithm_iterator(proposal, proto, EXTENDED_SEQUENCE_NUMBERS);
613 while (iterator->has_next(iterator))
614 {
615 algorithm_t *algo;
616 iterator->current(iterator, (void**)&algo);
617 transform = transform_substructure_create_type(EXTENDED_SEQUENCE_NUMBERS, algo->algorithm, 0);
618 this->public.add_transform_substructure(&(this->public), transform);
619 }
620 iterator->destroy(iterator);
621
622 /* take over general infos */
623 this->spi_size = proto == IKE ? 8 : 4;
624 this->spi.len = this->spi_size;
625 this->spi.ptr = allocator_alloc(this->spi_size);
626 *((u_int32_t*)this->spi.ptr) = proposal->get_spi(proposal, proto);
627 this->proposal_number = proposal->get_number(proposal);
628 this->protocol_id = proto;
629
630 return &(this->public);
631 }