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