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