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