ecbf6ac5692c2b02ff1f4ee1930ba33e297c9ced
[strongswan.git] / src / charon / encoding / payloads / transform_substructure.c
1 /**
2 * @file transform_substructure.h
3 *
4 * @brief Implementation of transform_substructure_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005-2006 Martin Willi
10 * Copyright (C) 2005 Jan Hutter
11 * Hochschule fuer Technik Rapperswil
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 *
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
22 */
23
24 #include <stddef.h>
25
26 #include "transform_substructure.h"
27
28 #include <encoding/payloads/transform_attribute.h>
29 #include <encoding/payloads/encodings.h>
30 #include <types.h>
31 #include <utils/linked_list.h>
32
33
34 typedef struct private_transform_substructure_t private_transform_substructure_t;
35
36 /**
37 * Private data of an transform_substructure_t object.
38 *
39 */
40 struct private_transform_substructure_t {
41 /**
42 * Public transform_substructure_t interface.
43 */
44 transform_substructure_t public;
45
46 /**
47 * Next payload type.
48 */
49 u_int8_t next_payload;
50
51
52 /**
53 * Length of this payload.
54 */
55 u_int16_t transform_length;
56
57
58 /**
59 * Type of the transform.
60 */
61 u_int8_t transform_type;
62
63 /**
64 * Transform ID.
65 */
66 u_int16_t transform_id;
67
68 /**
69 * Transforms Attributes are stored in a linked_list_t.
70 */
71 linked_list_t *attributes;
72
73 /**
74 * @brief Computes the length of this substructure.
75 *
76 * @param this calling private_transform_substructure_t object
77 */
78 void (*compute_length) (private_transform_substructure_t *this);
79 };
80
81
82 /**
83 * Encoding rules to parse or generate a Transform substructure.
84 *
85 * The defined offsets are the positions in a object of type
86 * private_transform_substructure_t.
87 *
88 */
89 encoding_rule_t transform_substructure_encodings[] = {
90 /* 1 Byte next payload type, stored in the field next_payload */
91 { U_INT_8, offsetof(private_transform_substructure_t, next_payload) },
92 /* Reserved Byte is skipped */
93 { RESERVED_BYTE, 0 },
94 /* Length of the whole transform substructure*/
95 { PAYLOAD_LENGTH, offsetof(private_transform_substructure_t, transform_length) },
96 /* transform type is a number of 8 bit */
97 { U_INT_8, offsetof(private_transform_substructure_t, transform_type) },
98 /* Reserved Byte is skipped */
99 { RESERVED_BYTE, 0 },
100 /* tranform ID is a number of 8 bit */
101 { U_INT_16, offsetof(private_transform_substructure_t, transform_id) },
102 /* Attributes are stored in a transform attribute,
103 offset points to a linked_list_t pointer */
104 { TRANSFORM_ATTRIBUTES, offsetof(private_transform_substructure_t, attributes) }
105 };
106
107 /*
108 1 2 3
109 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
110 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
111 ! 0 (last) or 3 ! RESERVED ! Transform Length !
112 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
113 !Transform Type ! RESERVED ! Transform ID !
114 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
115 ! !
116 ~ Transform Attributes ~
117 ! !
118 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
119 */
120
121
122 /**
123 * Implementation of payload_t.verify.
124 */
125 static status_t verify(private_transform_substructure_t *this)
126 {
127 status_t status = SUCCESS;
128 iterator_t *iterator;
129
130 if ((this->next_payload != NO_PAYLOAD) && (this->next_payload != 3))
131 {
132 /* must be 0 or 3 */
133 return FAILED;
134 }
135
136 switch (this->transform_type)
137 {
138 case ENCRYPTION_ALGORITHM:
139 {
140 if ((this->transform_id < ENCR_DES_IV64) || (this->transform_id > ENCR_AES_CTR))
141 {
142 return FAILED;
143 }
144 break;
145 }
146 case PSEUDO_RANDOM_FUNCTION:
147 {
148 if ((this->transform_id < PRF_HMAC_MD5) || (this->transform_id > PRF_AES128_CBC))
149 {
150 return FAILED;
151 }
152 break;
153 }
154 case INTEGRITY_ALGORITHM:
155 {
156 if ((this->transform_id < AUTH_HMAC_MD5_96) || (this->transform_id > AUTH_AES_XCBC_96))
157 {
158 return FAILED;
159 }
160 break;
161 }
162 case DIFFIE_HELLMAN_GROUP:
163 {
164 switch (this->transform_id)
165 {
166 case MODP_768_BIT:
167 case MODP_1024_BIT:
168 case MODP_1536_BIT:
169 case MODP_2048_BIT:
170 case MODP_3072_BIT:
171 case MODP_4096_BIT:
172 case MODP_6144_BIT:
173 case MODP_8192_BIT:
174 {
175 break;
176 }
177 default:
178 {
179 return FAILED;
180 }
181 }
182
183
184 break;
185 }
186 case EXTENDED_SEQUENCE_NUMBERS:
187 {
188 if ((this->transform_id != NO_EXT_SEQ_NUMBERS) && (this->transform_id != EXT_SEQ_NUMBERS))
189 {
190 return FAILED;
191 }
192 break;
193 }
194 default:
195 {
196 /* not a supported transform type! */
197 return FAILED;
198 }
199 }
200 iterator = this->attributes->create_iterator(this->attributes,TRUE);
201
202 while(iterator->has_next(iterator))
203 {
204 payload_t *current_attributes;
205 iterator->current(iterator,(void **)&current_attributes);
206
207 status = current_attributes->verify(current_attributes);
208 if (status != SUCCESS)
209 {
210 break;
211 }
212 }
213
214 iterator->destroy(iterator);
215
216
217 /* proposal number is checked in SA payload */
218 return status;
219 }
220
221 /**
222 * Implementation of payload_t.get_encoding_rules.
223 */
224 static void get_encoding_rules(private_transform_substructure_t *this, encoding_rule_t **rules, size_t *rule_count)
225 {
226 *rules = transform_substructure_encodings;
227 *rule_count = sizeof(transform_substructure_encodings) / sizeof(encoding_rule_t);
228 }
229
230 /**
231 * Implementation of payload_t.get_type.
232 */
233 static payload_type_t get_type(private_transform_substructure_t *this)
234 {
235 return TRANSFORM_SUBSTRUCTURE;
236 }
237
238 /**
239 * Implementation of payload_t.get_next_type.
240 */
241 static payload_type_t get_next_type(private_transform_substructure_t *this)
242 {
243 return (this->next_payload);
244 }
245
246 /**
247 * Implementation of payload_t.get_length.
248 */
249 static size_t get_length(private_transform_substructure_t *this)
250 {
251 this->compute_length(this);
252
253 return this->transform_length;
254 }
255
256 /**
257 * Implementation of transform_substructure_t.create_transform_attribute_iterator.
258 */
259 static iterator_t *create_transform_attribute_iterator (private_transform_substructure_t *this,bool forward)
260 {
261 return this->attributes->create_iterator(this->attributes,forward);
262 }
263
264 /**
265 * Implementation of transform_substructure_t.add_transform_attribute.
266 */
267 static void add_transform_attribute (private_transform_substructure_t *this,transform_attribute_t *attribute)
268 {
269 this->attributes->insert_last(this->attributes,(void *) attribute);
270 this->compute_length(this);
271 }
272
273 /**
274 * Implementation of transform_substructure_t.set_is_last_transform.
275 */
276 static void set_is_last_transform (private_transform_substructure_t *this, bool is_last)
277 {
278 this->next_payload = (is_last) ? 0: TRANSFORM_TYPE_VALUE;
279 }
280
281 /**
282 * Implementation of transform_substructure_t.get_is_last_transform.
283 */
284 static bool get_is_last_transform (private_transform_substructure_t *this)
285 {
286 return ((this->next_payload == TRANSFORM_TYPE_VALUE) ? FALSE : TRUE);
287 }
288
289 /**
290 * Implementation of payload_t.set_next_type.
291 */
292 static void set_next_type(private_transform_substructure_t *this,payload_type_t type)
293 {
294 }
295
296 /**
297 * Implementation of transform_substructure_t.set_transform_type.
298 */
299 static void set_transform_type (private_transform_substructure_t *this,u_int8_t type)
300 {
301 this->transform_type = type;
302 }
303
304 /**
305 * Implementation of transform_substructure_t.get_transform_type.
306 */
307 static u_int8_t get_transform_type (private_transform_substructure_t *this)
308 {
309 return this->transform_type;
310 }
311
312 /**
313 * Implementation of transform_substructure_t.set_transform_id.
314 */
315 static void set_transform_id (private_transform_substructure_t *this,u_int16_t id)
316 {
317 this->transform_id = id;
318 }
319
320 /**
321 * Implementation of transform_substructure_t.get_transform_id.
322 */
323 static u_int16_t get_transform_id (private_transform_substructure_t *this)
324 {
325 return this->transform_id;
326 }
327
328 /**
329 * Implementation of private_transform_substructure_t.compute_length.
330 */
331 static void compute_length (private_transform_substructure_t *this)
332 {
333 iterator_t *iterator;
334 size_t length = TRANSFORM_SUBSTRUCTURE_HEADER_LENGTH;
335 iterator = this->attributes->create_iterator(this->attributes,TRUE);
336 while (iterator->has_next(iterator))
337 {
338 payload_t * current_attribute;
339 iterator->current(iterator,(void **) &current_attribute);
340 length += current_attribute->get_length(current_attribute);
341 }
342 iterator->destroy(iterator);
343
344 this->transform_length = length;
345 }
346
347 /**
348 * Implementation of transform_substructure_t.clone.
349 */
350 static transform_substructure_t *clone(private_transform_substructure_t *this)
351 {
352 private_transform_substructure_t *new_clone;
353 iterator_t *attributes;
354
355 new_clone = (private_transform_substructure_t *) transform_substructure_create();
356
357 new_clone->next_payload = this->next_payload;
358 new_clone->transform_type = this->transform_type;
359 new_clone->transform_id = this->transform_id;
360
361 attributes = this->attributes->create_iterator(this->attributes,FALSE);
362
363 while (attributes->has_next(attributes))
364 {
365 transform_attribute_t *current_attribute;
366 transform_attribute_t *current_attribute_clone;
367 attributes->current(attributes,(void **) &current_attribute);
368
369 current_attribute_clone = current_attribute->clone(current_attribute);
370
371 new_clone->public.add_transform_attribute(&(new_clone->public),current_attribute_clone);
372 }
373
374 attributes->destroy(attributes);
375
376 return &(new_clone->public);
377 }
378
379
380 /**
381 * Implementation of transform_substructure_t.get_key_length.
382 */
383 static status_t get_key_length(private_transform_substructure_t *this, u_int16_t *key_length)
384 {
385 iterator_t *attributes;
386
387 attributes = this->attributes->create_iterator(this->attributes,TRUE);
388
389 while (attributes->has_next(attributes))
390 {
391 transform_attribute_t *current_attribute;
392 attributes->current(attributes,(void **) &current_attribute);
393
394 if (current_attribute->get_attribute_type(current_attribute) == KEY_LENGTH)
395 {
396 *key_length = current_attribute->get_value(current_attribute);
397 attributes->destroy(attributes);
398 return SUCCESS;
399 }
400
401 }
402 attributes->destroy(attributes);
403
404 return FAILED;
405 }
406
407
408 /**
409 * Implementation of transform_substructure_t.destroy and payload_t.destroy.
410 */
411 static void destroy(private_transform_substructure_t *this)
412 {
413 /* all proposals are getting destroyed */
414 while (this->attributes->get_count(this->attributes) > 0)
415 {
416 transform_attribute_t *current_attribute;
417 this->attributes->remove_last(this->attributes,(void **)&current_attribute);
418 current_attribute->destroy(current_attribute);
419 }
420 this->attributes->destroy(this->attributes);
421
422 free(this);
423 }
424
425 /*
426 * Described in header.
427 */
428 transform_substructure_t *transform_substructure_create()
429 {
430 private_transform_substructure_t *this = malloc_thing(private_transform_substructure_t);
431
432 /* payload interface */
433 this->public.payload_interface.verify = (status_t (*) (payload_t *))verify;
434 this->public.payload_interface.get_encoding_rules = (void (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
435 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
436 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
437 this->public.payload_interface.set_next_type = (void (*) (payload_t *,payload_type_t)) set_next_type;
438 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_type;
439 this->public.payload_interface.destroy = (void (*) (payload_t *))destroy;
440
441 /* public functions */
442 this->public.create_transform_attribute_iterator = (iterator_t * (*) (transform_substructure_t *,bool)) create_transform_attribute_iterator;
443 this->public.add_transform_attribute = (void (*) (transform_substructure_t *,transform_attribute_t *)) add_transform_attribute;
444 this->public.set_is_last_transform = (void (*) (transform_substructure_t *,bool)) set_is_last_transform;
445 this->public.get_is_last_transform = (bool (*) (transform_substructure_t *)) get_is_last_transform;
446 this->public.set_transform_type = (void (*) (transform_substructure_t *,u_int8_t)) set_transform_type;
447 this->public.get_transform_type = (u_int8_t (*) (transform_substructure_t *)) get_transform_type;
448 this->public.set_transform_id = (void (*) (transform_substructure_t *,u_int16_t)) set_transform_id;
449 this->public.get_transform_id = (u_int16_t (*) (transform_substructure_t *)) get_transform_id;
450 this->public.get_key_length = (status_t (*) (transform_substructure_t *,u_int16_t *)) get_key_length;
451 this->public.clone = (transform_substructure_t* (*) (transform_substructure_t *)) clone;
452 this->public.destroy = (void (*) (transform_substructure_t *)) destroy;
453
454 /* private functions */
455 this->compute_length = compute_length;
456
457 /* set default values of the fields */
458 this->next_payload = NO_PAYLOAD;
459 this->transform_length = TRANSFORM_SUBSTRUCTURE_HEADER_LENGTH;
460 this->transform_id = 0;
461 this->transform_type = 0;
462 this->attributes = linked_list_create();
463
464 return (&(this->public));
465 }
466
467 /*
468 * Described in header
469 */
470 transform_substructure_t *transform_substructure_create_type(transform_type_t transform_type, u_int16_t transform_id, u_int16_t key_length)
471 {
472 transform_substructure_t *transform = transform_substructure_create();
473
474 transform->set_transform_type(transform,transform_type);
475 transform->set_transform_id(transform,transform_id);
476
477 /* a keylength attribute is only created for variable length algos */
478 if (transform_type == ENCRYPTION_ALGORITHM &&
479 (transform_id == ENCR_AES_CBC ||
480 transform_id == ENCR_IDEA ||
481 transform_id == ENCR_CAST ||
482 transform_id == ENCR_BLOWFISH))
483 {
484 transform_attribute_t *attribute = transform_attribute_create_key_length(key_length);
485 transform->add_transform_attribute(transform,attribute);
486 }
487
488 return transform;
489 }