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