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