354eedb9e274760618e762a4ae3e6daf28bb3541
[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 != PROPOSAL_SUBSTRUCTURE))
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.set_proposal_number.
255 */
256 static void set_proposal_number(private_proposal_substructure_t *this,u_int8_t proposal_number)
257 {
258 this->proposal_number = proposal_number;
259 }
260
261 /**
262 * Implementation of proposal_substructure_t.get_proposal_number.
263 */
264 static u_int8_t get_proposal_number (private_proposal_substructure_t *this)
265 {
266 return (this->proposal_number);
267 }
268
269 /**
270 * Implementation of proposal_substructure_t.set_protocol_id.
271 */
272 static void set_protocol_id(private_proposal_substructure_t *this,u_int8_t protocol_id)
273 {
274 this->protocol_id = protocol_id;
275 }
276
277 /**
278 * Implementation of proposal_substructure_t.get_protocol_id.
279 */
280 static u_int8_t get_protocol_id (private_proposal_substructure_t *this)
281 {
282 return (this->protocol_id);
283 }
284
285 /**
286 * Implementation of proposal_substructure_t.set_spi.
287 */
288 static void set_spi (private_proposal_substructure_t *this, chunk_t spi)
289 {
290 /* first delete already set spi value */
291 if (this->spi.ptr != NULL)
292 {
293 allocator_free(this->spi.ptr);
294 this->spi.ptr = NULL;
295 this->spi.len = 0;
296 this->compute_length(this);
297 }
298
299 this->spi.ptr = allocator_clone_bytes(spi.ptr,spi.len);
300 this->spi.len = spi.len;
301 this->spi_size = spi.len;
302 this->compute_length(this);
303 }
304
305 /**
306 * Implementation of proposal_substructure_t.get_spi.
307 */
308 static chunk_t get_spi (private_proposal_substructure_t *this)
309 {
310 chunk_t spi;
311 spi.ptr = this->spi.ptr;
312 spi.len = this->spi.len;
313
314 return spi;
315 }
316
317 /**
318 * Implementation of proposal_substructure_t.get_info_for_transform_type.
319 */
320 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)
321 {
322 iterator_t *iterator;
323 status_t status;
324 u_int16_t found_transform_id;
325 u_int16_t found_key_length;
326
327 iterator = this->transforms->create_iterator(this->transforms,TRUE);
328
329 while (iterator->has_next(iterator))
330 {
331 transform_substructure_t *current_transform;
332 status = iterator->current(iterator,(void **) &current_transform);
333 if (status != SUCCESS)
334 {
335 break;
336 }
337 if (current_transform->get_transform_type(current_transform) == type)
338 {
339 /* now get data for specific type */
340 found_transform_id = current_transform->get_transform_id(current_transform);
341 status = current_transform->get_key_length(current_transform,&found_key_length);
342 *transform_id = found_transform_id;
343 *key_length = found_key_length;
344 iterator->destroy(iterator);
345 return status;
346 }
347 }
348 iterator->destroy(iterator);
349 return NOT_FOUND;
350 }
351
352 /**
353 * Implementation of private_proposal_substructure_t.compute_length.
354 */
355 static void compute_length (private_proposal_substructure_t *this)
356 {
357 iterator_t *iterator;
358 size_t transforms_count = 0;
359 size_t length = PROPOSAL_SUBSTRUCTURE_HEADER_LENGTH;
360 iterator = this->transforms->create_iterator(this->transforms,TRUE);
361 while (iterator->has_next(iterator))
362 {
363 payload_t * current_transform;
364 iterator->current(iterator,(void **) &current_transform);
365 length += current_transform->get_length(current_transform);
366 transforms_count++;
367 }
368 iterator->destroy(iterator);
369
370 length += this->spi.len;
371 this->transforms_count= transforms_count;
372 this->proposal_length = length;
373
374 }
375
376 /**
377 * Implementation of proposal_substructure_t.clone.
378 */
379 static private_proposal_substructure_t* clone(private_proposal_substructure_t *this)
380 {
381 private_proposal_substructure_t * new_clone;
382 iterator_t *transforms;
383
384 new_clone = (private_proposal_substructure_t *) proposal_substructure_create();
385
386 new_clone->next_payload = this->next_payload;
387 new_clone->proposal_number = this->proposal_number;
388 new_clone->protocol_id = this->protocol_id;
389 new_clone->spi_size = this->spi_size;
390 if (this->spi.ptr != NULL)
391 {
392 new_clone->spi.ptr = allocator_clone_bytes(this->spi.ptr,this->spi.len);
393 new_clone->spi.len = this->spi.len;
394 }
395
396 transforms = this->transforms->create_iterator(this->transforms,FALSE);
397
398 while (transforms->has_next(transforms))
399 {
400 transform_substructure_t *current_transform;
401 transform_substructure_t *current_transform_clone;
402
403 transforms->current(transforms,(void **) &current_transform);
404
405 current_transform_clone = current_transform->clone(current_transform);
406
407 new_clone->public.add_transform_substructure(&(new_clone->public),current_transform_clone);
408 }
409
410 transforms->destroy(transforms);
411
412 return new_clone;
413 }
414
415 /**
416 * Implements payload_t's and proposal_substructure_t's destroy function.
417 * See #payload_s.destroy or proposal_substructure_s.destroy for description.
418 */
419 static status_t destroy(private_proposal_substructure_t *this)
420 {
421 /* all proposals are getting destroyed */
422 while (this->transforms->get_count(this->transforms) > 0)
423 {
424 transform_substructure_t *current_transform;
425 if (this->transforms->remove_last(this->transforms,(void **)&current_transform) != SUCCESS)
426 {
427 break;
428 }
429 current_transform->destroy(current_transform);
430 }
431 this->transforms->destroy(this->transforms);
432
433 if (this->spi.ptr != NULL)
434 {
435 allocator_free(this->spi.ptr);
436 }
437
438 allocator_free(this);
439
440 return SUCCESS;
441 }
442
443 /*
444 * Described in header.
445 */
446 proposal_substructure_t *proposal_substructure_create()
447 {
448 private_proposal_substructure_t *this = allocator_alloc_thing(private_proposal_substructure_t);
449
450 /* interface functions */
451 this->public.payload_interface.verify = (status_t (*) (payload_t *))verify;
452 this->public.payload_interface.get_encoding_rules = (void (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
453 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
454 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
455 this->public.payload_interface.set_next_type = (void (*) (payload_t *,payload_type_t)) set_next_type;
456 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_type;
457 this->public.payload_interface.destroy = (void (*) (payload_t *))destroy;
458
459 /* public functions */
460 this->public.create_transform_substructure_iterator = (iterator_t* (*) (proposal_substructure_t *,bool)) create_transform_substructure_iterator;
461 this->public.add_transform_substructure = (void (*) (proposal_substructure_t *,transform_substructure_t *)) add_transform_substructure;
462 this->public.set_proposal_number = (void (*) (proposal_substructure_t *,u_int8_t))set_proposal_number;
463 this->public.get_proposal_number = (u_int8_t (*) (proposal_substructure_t *)) get_proposal_number;
464 this->public.set_protocol_id = (void (*) (proposal_substructure_t *,u_int8_t))set_protocol_id;
465 this->public.get_protocol_id = (u_int8_t (*) (proposal_substructure_t *)) get_protocol_id;
466 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;
467 this->public.set_spi = (void (*) (proposal_substructure_t *,chunk_t))set_spi;
468 this->public.get_spi = (chunk_t (*) (proposal_substructure_t *)) get_spi;
469 this->public.clone = (proposal_substructure_t * (*) (proposal_substructure_t *)) clone;
470 this->public.destroy = (void (*) (proposal_substructure_t *)) destroy;
471
472
473 /* private functions */
474 this->compute_length = compute_length;
475
476 /* set default values of the fields */
477 this->next_payload = NO_PAYLOAD;
478 this->proposal_length = 0;
479 this->proposal_number = 0;
480 this->protocol_id = 0;
481 this->transforms_count = 0;
482 this->spi_size = 0;
483 this->spi.ptr = NULL;
484 this->spi.len = 0;
485
486 this->transforms = linked_list_create();
487
488 return (&(this->public));
489 }