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