]> git.ipfire.org Git - thirdparty/strongswan.git/blame - Source/charon/encoding/payloads/proposal_substructure.c
- code cleaned up
[thirdparty/strongswan.git] / Source / charon / encoding / payloads / proposal_substructure.c
CommitLineData
da42afc5
JH
1/**
2 * @file proposal_substructure.h
3 *
3fe05870 4 * @brief Implementation of proposal_substructure_t.
da42afc5
JH
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 */
b860cffd 22
da42afc5
JH
23 /* offsetof macro */
24#include <stddef.h>
25
26#include "proposal_substructure.h"
27
4a962238
MW
28#include <encoding/payloads/encodings.h>
29#include <encoding/payloads/transform_substructure.h>
696be022
JH
30#include <types.h>
31#include <utils/allocator.h>
32#include <utils/linked_list.h>
da42afc5 33
ae3012a0
JH
34/**
35 * String mappings for protocol_id_t.
36 */
37mapping_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
95c61cb9
JH
46typedef struct private_proposal_substructure_t private_proposal_substructure_t;
47
da42afc5 48/**
3fe05870 49 * Private data of an proposal_substructure_t object.
da42afc5
JH
50 *
51 */
95c61cb9 52struct private_proposal_substructure_t {
da42afc5 53 /**
3fe05870 54 * Public proposal_substructure_t interface.
da42afc5
JH
55 */
56 proposal_substructure_t public;
57
58 /**
3fe05870 59 * Next payload type.
da42afc5
JH
60 */
61 u_int8_t next_payload;
62
da42afc5 63 /**
3fe05870 64 * Length of this payload.
da42afc5
JH
65 */
66 u_int16_t proposal_length;
67
da42afc5 68 /**
3fe05870 69 * Proposal number.
da42afc5
JH
70 */
71 u_int8_t proposal_number;
72
73 /**
3fe05870 74 * Protocol ID.
da42afc5
JH
75 */
76 u_int8_t protocol_id;
77
78 /**
3fe05870 79 * SPI size of the following SPI.
da42afc5
JH
80 */
81 u_int8_t spi_size;
82
83 /**
3fe05870 84 * Number of transforms.
da42afc5
JH
85 */
86 u_int8_t transforms_count;
87
88 /**
3fe05870 89 * SPI is stored as chunk.
da42afc5
JH
90 */
91 chunk_t spi;
92
93 /**
3fe05870 94 * Transforms are stored in a linked_list_t.
da42afc5
JH
95 */
96 linked_list_t * transforms;
67978e0b
JH
97
98 /**
99 * @brief Computes the length of this substructure.
100 *
101 * @param this calling private_proposal_substructure_t object
67978e0b 102 */
3fe05870 103 void (*compute_length) (private_proposal_substructure_t *this);
da42afc5
JH
104};
105
106/**
3fe05870 107 * Encoding rules to parse or generate a Proposal substructure.
da42afc5
JH
108 *
109 * The defined offsets are the positions in a object of type
110 * private_proposal_substructure_t.
111 *
112 */
113encoding_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 },
b860cffd 118 /* Length of the whole proposal substructure payload*/
da42afc5
JH
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
e31eb71e
JH
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/**
3fe05870 152 * Implementation of payload_t.verify.
e31eb71e
JH
153 */
154static status_t verify(private_proposal_substructure_t *this)
155{
346af6f3
JH
156 status_t status = SUCCESS;
157 iterator_t *iterator;
158
b737e9d9 159 if ((this->next_payload != NO_PAYLOAD) && (this->next_payload != 2))
e31eb71e
JH
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
9bdd74ea 170 if ((this->protocol_id == 0) || (this->protocol_id >= 4))
e31eb71e
JH
171 {
172 /* reserved are not supported */
173 return FAILED;
174 }
346af6f3
JH
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
e31eb71e
JH
192
193 /* proposal number is checked in SA payload */
346af6f3 194 return status;
e31eb71e
JH
195}
196
da42afc5 197/**
3fe05870 198 * Implementation of payload_t.get_encoding_rules.
da42afc5 199 */
3fe05870 200static void get_encoding_rules(private_proposal_substructure_t *this, encoding_rule_t **rules, size_t *rule_count)
da42afc5
JH
201{
202 *rules = proposal_substructure_encodings;
203 *rule_count = sizeof(proposal_substructure_encodings) / sizeof(encoding_rule_t);
da42afc5
JH
204}
205
206/**
3fe05870 207 * Implementation of payload_t.get_type.
da42afc5
JH
208 */
209static payload_type_t get_type(private_proposal_substructure_t *this)
210{
211 return PROPOSAL_SUBSTRUCTURE;
212}
213
214/**
3fe05870 215 * Implementation of payload_t.get_next_type.
da42afc5
JH
216 */
217static payload_type_t get_next_type(private_proposal_substructure_t *this)
218{
219 return (this->next_payload);
220}
221
32cbc7bc 222/**
3fe05870 223 * Implementation of payload_t.set_next_type.
32cbc7bc 224 */
3fe05870 225static void set_next_type(private_proposal_substructure_t *this,payload_type_t type)
32cbc7bc 226{
32cbc7bc
JH
227}
228
da42afc5 229/**
3fe05870 230 * Implementation of payload_t.get_length.
da42afc5
JH
231 */
232static size_t get_length(private_proposal_substructure_t *this)
233{
234 return this->proposal_length;
235}
236
b860cffd 237/**
3fe05870 238 * Implementation of proposal_substructure_t.create_transform_substructure_iterator.
b860cffd 239 */
a0753941 240static iterator_t *create_transform_substructure_iterator (private_proposal_substructure_t *this,bool forward)
b860cffd 241{
a0753941 242 return (this->transforms->create_iterator(this->transforms,forward));
b860cffd
JH
243}
244
245/**
3fe05870 246 * Implementation of proposal_substructure_t.add_transform_substructure.
b860cffd 247 */
3fe05870 248static void add_transform_substructure (private_proposal_substructure_t *this,transform_substructure_t *transform)
b860cffd 249{
a14dffd1
JH
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
3fe05870 261 this->transforms->insert_last(this->transforms,(void *) transform);
67978e0b
JH
262 this->compute_length(this);
263}
264
b737e9d9
JH
265/**
266 * Implementation of proposal_substructure_t.proposal_substructure_t.
267 */
268static 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
67978e0b 274/**
3fe05870 275 * Implementation of proposal_substructure_t.set_proposal_number.
67978e0b 276 */
3fe05870 277static void set_proposal_number(private_proposal_substructure_t *this,u_int8_t proposal_number)
67978e0b
JH
278{
279 this->proposal_number = proposal_number;
67978e0b
JH
280}
281
282/**
3fe05870 283 * Implementation of proposal_substructure_t.get_proposal_number.
67978e0b
JH
284 */
285static u_int8_t get_proposal_number (private_proposal_substructure_t *this)
286{
287 return (this->proposal_number);
288}
289
290/**
3fe05870 291 * Implementation of proposal_substructure_t.set_protocol_id.
67978e0b 292 */
3fe05870 293static void set_protocol_id(private_proposal_substructure_t *this,u_int8_t protocol_id)
67978e0b
JH
294{
295 this->protocol_id = protocol_id;
67978e0b
JH
296}
297
298/**
3fe05870 299 * Implementation of proposal_substructure_t.get_protocol_id.
67978e0b
JH
300 */
301static u_int8_t get_protocol_id (private_proposal_substructure_t *this)
302{
303 return (this->protocol_id);
b860cffd
JH
304}
305
67978e0b 306/**
3fe05870 307 * Implementation of proposal_substructure_t.set_spi.
67978e0b 308 */
3fe05870 309static void set_spi (private_proposal_substructure_t *this, chunk_t spi)
67978e0b
JH
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);
67978e0b
JH
321 this->spi.len = spi.len;
322 this->spi_size = spi.len;
323 this->compute_length(this);
67978e0b
JH
324}
325
326/**
3fe05870 327 * Implementation of proposal_substructure_t.get_spi.
67978e0b
JH
328 */
329static 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
3fe05870
JH
338/**
339 * Implementation of proposal_substructure_t.get_info_for_transform_type.
340 */
b9d9f188
JH
341static 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
a0753941 348 iterator = this->transforms->create_iterator(this->transforms,TRUE);
3fe05870 349
b9d9f188
JH
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);
3fe05870 370 return NOT_FOUND;
b9d9f188
JH
371}
372
67978e0b 373/**
3fe05870 374 * Implementation of private_proposal_substructure_t.compute_length.
67978e0b 375 */
3fe05870 376static void compute_length (private_proposal_substructure_t *this)
67978e0b 377{
bdb141cb 378 iterator_t *iterator;
67978e0b
JH
379 size_t transforms_count = 0;
380 size_t length = PROPOSAL_SUBSTRUCTURE_HEADER_LENGTH;
a0753941 381 iterator = this->transforms->create_iterator(this->transforms,TRUE);
67978e0b
JH
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 }
a14dffd1 389 iterator->destroy(iterator);
67978e0b
JH
390
391 length += this->spi.len;
67978e0b
JH
392 this->transforms_count= transforms_count;
393 this->proposal_length = length;
394
67978e0b
JH
395}
396
b737e9d9
JH
397/**
398 * Implementation of proposal_substructure_t.get_transform_count.
399 */
400static 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 */
408static size_t get_spi_size (private_proposal_substructure_t *this)
409{
410 return this->spi.len;
411}
412
b9459040 413/**
3fe05870 414 * Implementation of proposal_substructure_t.clone.
b9459040 415 */
a0753941 416static private_proposal_substructure_t* clone(private_proposal_substructure_t *this)
b9459040
JH
417{
418 private_proposal_substructure_t * new_clone;
bdb141cb 419 iterator_t *transforms;
b9459040
JH
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);
b9459040
JH
430 new_clone->spi.len = this->spi.len;
431 }
432
a0753941 433 transforms = this->transforms->create_iterator(this->transforms,FALSE);
b9459040
JH
434
435 while (transforms->has_next(transforms))
436 {
437 transform_substructure_t *current_transform;
438 transform_substructure_t *current_transform_clone;
3fe05870
JH
439
440 transforms->current(transforms,(void **) &current_transform);
441
a0753941 442 current_transform_clone = current_transform->clone(current_transform);
b9459040 443
3fe05870 444 new_clone->public.add_transform_substructure(&(new_clone->public),current_transform_clone);
b9459040
JH
445 }
446
447 transforms->destroy(transforms);
448
a0753941 449 return new_clone;
b9459040
JH
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 */
456static 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}
67978e0b 479
da42afc5 480/*
3fe05870 481 * Described in header.
da42afc5
JH
482 */
483proposal_substructure_t *proposal_substructure_create()
484{
485 private_proposal_substructure_t *this = allocator_alloc_thing(private_proposal_substructure_t);
e31eb71e
JH
486
487 /* interface functions */
488 this->public.payload_interface.verify = (status_t (*) (payload_t *))verify;
3fe05870 489 this->public.payload_interface.get_encoding_rules = (void (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
da42afc5
JH
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;
3fe05870 492 this->public.payload_interface.set_next_type = (void (*) (payload_t *,payload_type_t)) set_next_type;
da42afc5 493 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_type;
3fe05870 494 this->public.payload_interface.destroy = (void (*) (payload_t *))destroy;
e31eb71e
JH
495
496 /* public functions */
a0753941 497 this->public.create_transform_substructure_iterator = (iterator_t* (*) (proposal_substructure_t *,bool)) create_transform_substructure_iterator;
3fe05870
JH
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;
67978e0b 500 this->public.get_proposal_number = (u_int8_t (*) (proposal_substructure_t *)) get_proposal_number;
3fe05870 501 this->public.set_protocol_id = (void (*) (proposal_substructure_t *,u_int8_t))set_protocol_id;
67978e0b 502 this->public.get_protocol_id = (u_int8_t (*) (proposal_substructure_t *)) get_protocol_id;
b9d9f188 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;
b737e9d9
JH
504 this->public.set_is_last_proposal = (void (*) (proposal_substructure_t *,bool)) set_is_last_proposal;
505
3fe05870 506 this->public.set_spi = (void (*) (proposal_substructure_t *,chunk_t))set_spi;
67978e0b 507 this->public.get_spi = (chunk_t (*) (proposal_substructure_t *)) get_spi;
b737e9d9
JH
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;
a0753941 510 this->public.clone = (proposal_substructure_t * (*) (proposal_substructure_t *)) clone;
3fe05870 511 this->public.destroy = (void (*) (proposal_substructure_t *)) destroy;
da42afc5 512
b9d9f188 513
67978e0b
JH
514 /* private functions */
515 this->compute_length = compute_length;
516
da42afc5
JH
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();
3fe05870 528
da42afc5
JH
529 return (&(this->public));
530}