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