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