- dead end implementation
[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 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 allocator_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 = allocator_clone_bytes(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 /**
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.clone.
410 */
411 static private_proposal_substructure_t* clone(private_proposal_substructure_t *this)
412 {
413 private_proposal_substructure_t * new_clone;
414 iterator_t *transforms;
415
416 new_clone = (private_proposal_substructure_t *) proposal_substructure_create();
417
418 new_clone->next_payload = this->next_payload;
419 new_clone->proposal_number = this->proposal_number;
420 new_clone->protocol_id = this->protocol_id;
421 new_clone->spi_size = this->spi_size;
422 if (this->spi.ptr != NULL)
423 {
424 new_clone->spi.ptr = allocator_clone_bytes(this->spi.ptr,this->spi.len);
425 new_clone->spi.len = this->spi.len;
426 }
427
428 transforms = this->transforms->create_iterator(this->transforms,FALSE);
429
430 while (transforms->has_next(transforms))
431 {
432 transform_substructure_t *current_transform;
433 transform_substructure_t *current_transform_clone;
434
435 transforms->current(transforms,(void **) &current_transform);
436
437 current_transform_clone = current_transform->clone(current_transform);
438
439 new_clone->public.add_transform_substructure(&(new_clone->public),current_transform_clone);
440 }
441
442 transforms->destroy(transforms);
443
444 return new_clone;
445 }
446
447 /**
448 * Implements payload_t's and proposal_substructure_t's destroy function.
449 * See #payload_s.destroy or proposal_substructure_s.destroy for description.
450 */
451 static status_t destroy(private_proposal_substructure_t *this)
452 {
453 /* all proposals are getting destroyed */
454 while (this->transforms->get_count(this->transforms) > 0)
455 {
456 transform_substructure_t *current_transform;
457 if (this->transforms->remove_last(this->transforms,(void **)&current_transform) != SUCCESS)
458 {
459 break;
460 }
461 current_transform->destroy(current_transform);
462 }
463 this->transforms->destroy(this->transforms);
464
465 if (this->spi.ptr != NULL)
466 {
467 allocator_free(this->spi.ptr);
468 }
469
470 allocator_free(this);
471
472 return SUCCESS;
473 }
474
475 /*
476 * Described in header.
477 */
478 proposal_substructure_t *proposal_substructure_create()
479 {
480 private_proposal_substructure_t *this = allocator_alloc_thing(private_proposal_substructure_t);
481
482 /* interface functions */
483 this->public.payload_interface.verify = (status_t (*) (payload_t *))verify;
484 this->public.payload_interface.get_encoding_rules = (void (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
485 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
486 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
487 this->public.payload_interface.set_next_type = (void (*) (payload_t *,payload_type_t)) set_next_type;
488 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_type;
489 this->public.payload_interface.destroy = (void (*) (payload_t *))destroy;
490
491
492 /* public functions */
493 this->public.create_transform_substructure_iterator = (iterator_t* (*) (proposal_substructure_t *,bool)) create_transform_substructure_iterator;
494 this->public.add_transform_substructure = (void (*) (proposal_substructure_t *,transform_substructure_t *)) add_transform_substructure;
495 this->public.set_proposal_number = (void (*) (proposal_substructure_t *,u_int8_t))set_proposal_number;
496 this->public.get_proposal_number = (u_int8_t (*) (proposal_substructure_t *)) get_proposal_number;
497 this->public.set_protocol_id = (void (*) (proposal_substructure_t *,u_int8_t))set_protocol_id;
498 this->public.get_protocol_id = (u_int8_t (*) (proposal_substructure_t *)) get_protocol_id;
499 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;
500 this->public.set_is_last_proposal = (void (*) (proposal_substructure_t *,bool)) set_is_last_proposal;
501
502 this->public.set_spi = (void (*) (proposal_substructure_t *,chunk_t))set_spi;
503 this->public.get_spi = (chunk_t (*) (proposal_substructure_t *)) get_spi;
504 this->public.get_transform_count = (size_t (*) (proposal_substructure_t *)) get_transform_count;
505 this->public.get_spi_size = (size_t (*) (proposal_substructure_t *)) get_spi_size;
506 this->public.clone = (proposal_substructure_t * (*) (proposal_substructure_t *)) clone;
507 this->public.destroy = (void (*) (proposal_substructure_t *)) destroy;
508
509
510 /* private functions */
511 this->compute_length = compute_length;
512
513 /* set default values of the fields */
514 this->next_payload = NO_PAYLOAD;
515 this->proposal_length = 0;
516 this->proposal_number = 0;
517 this->protocol_id = 0;
518 this->transforms_count = 0;
519 this->spi_size = 0;
520 this->spi.ptr = NULL;
521 this->spi.len = 0;
522
523 this->transforms = linked_list_create();
524
525 return (&(this->public));
526 }
527
528 /*
529 * Described in header.
530 */
531 proposal_substructure_t *proposal_substructure_create_from_child_proposal(child_proposal_t *proposal, protocol_id_t *proto)
532 {
533 private_proposal_substructure_t *this = (private_proposal_substructure_t*)proposal_substructure_create();
534 iterator_t *iterator;
535 algorithm_t *algo;
536 transform_substructure_t *transform;
537
538 /* encryption algorithm is only availble in ESP */
539 if (proto == ESP)
540 {
541 iterator = proposal->create_algorithm_iterator(proposal, proto, ENCRYPTION_ALGORITHM);
542 while (iterator->has_next(iterator))
543 {
544 iterator->current(iterator, (void**)&algo);
545 transform = transform_substructure_create_type(ENCRYPTION_ALGORITHM, algo->algorithm, algo->key_size);
546 this->public.add_transform_substructure(&(this->public), transform);
547 }
548 iterator->destroy(iterator);
549 }
550
551 /* integrity algorithms */
552 iterator = proposal->create_algorithm_iterator(proposal, proto, 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 /* dh groups */
563 iterator = proposal->create_algorithm_iterator(proposal, proto, DIFFIE_HELLMAN_GROUP);
564 while (iterator->has_next(iterator))
565 {
566 algorithm_t *algo;
567 iterator->current(iterator, (void**)&algo);
568 transform = transform_substructure_create_type(DIFFIE_HELLMAN_GROUP, algo->algorithm, 0);
569 this->public.add_transform_substructure(&(this->public), transform);
570 }
571 iterator->destroy(iterator);
572
573 /* extended sequence numbers */
574 iterator = proposal->create_algorithm_iterator(proposal, proto, EXTENDED_SEQUENCE_NUMBERS);
575 while (iterator->has_next(iterator))
576 {
577 algorithm_t *algo;
578 iterator->current(iterator, (void**)&algo);
579 transform = transform_substructure_create_type(EXTENDED_SEQUENCE_NUMBERS, algo->algorithm, 0);
580 this->public.add_transform_substructure(&(this->public), transform);
581 }
582 iterator->destroy(iterator);
583 }