c94f6c1a23eca905764d5c20fecd1116b41a0e8b
[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 compute_length(this);
207 return this->transform_length;
208 }
209
210 /**
211 * Implementation of transform_substructure_t.create_transform_attribute_iterator.
212 */
213 static iterator_t *create_transform_attribute_iterator (private_transform_substructure_t *this,bool forward)
214 {
215 return this->attributes->create_iterator(this->attributes,forward);
216 }
217
218 /**
219 * Implementation of transform_substructure_t.add_transform_attribute.
220 */
221 static void add_transform_attribute (private_transform_substructure_t *this,transform_attribute_t *attribute)
222 {
223 this->attributes->insert_last(this->attributes,(void *) attribute);
224 compute_length(this);
225 }
226
227 /**
228 * Implementation of transform_substructure_t.set_is_last_transform.
229 */
230 static void set_is_last_transform (private_transform_substructure_t *this, bool is_last)
231 {
232 this->next_payload = (is_last) ? 0: TRANSFORM_TYPE_VALUE;
233 }
234
235 /**
236 * Implementation of transform_substructure_t.get_is_last_transform.
237 */
238 static bool get_is_last_transform (private_transform_substructure_t *this)
239 {
240 return ((this->next_payload == TRANSFORM_TYPE_VALUE) ? FALSE : TRUE);
241 }
242
243 /**
244 * Implementation of payload_t.set_next_type.
245 */
246 static void set_next_type(private_transform_substructure_t *this,payload_type_t type)
247 {
248 }
249
250 /**
251 * Implementation of transform_substructure_t.set_transform_type.
252 */
253 static void set_transform_type (private_transform_substructure_t *this,u_int8_t type)
254 {
255 this->transform_type = type;
256 }
257
258 /**
259 * Implementation of transform_substructure_t.get_transform_type.
260 */
261 static u_int8_t get_transform_type (private_transform_substructure_t *this)
262 {
263 return this->transform_type;
264 }
265
266 /**
267 * Implementation of transform_substructure_t.set_transform_id.
268 */
269 static void set_transform_id (private_transform_substructure_t *this,u_int16_t id)
270 {
271 this->transform_id = id;
272 }
273
274 /**
275 * Implementation of transform_substructure_t.get_transform_id.
276 */
277 static u_int16_t get_transform_id (private_transform_substructure_t *this)
278 {
279 return this->transform_id;
280 }
281
282 /**
283 * Implementation of transform_substructure_t.clone.
284 */
285 static transform_substructure_t *clone_(private_transform_substructure_t *this)
286 {
287 private_transform_substructure_t *clone;
288 iterator_t *attributes;
289 transform_attribute_t *current_attribute;
290
291 clone = (private_transform_substructure_t *) transform_substructure_create();
292 clone->next_payload = this->next_payload;
293 clone->transform_type = this->transform_type;
294 clone->transform_id = this->transform_id;
295
296 attributes = this->attributes->create_iterator(this->attributes, FALSE);
297 while (attributes->iterate(attributes, (void**)&current_attribute))
298 {
299 current_attribute = current_attribute->clone(current_attribute);
300 clone->public.add_transform_attribute(&clone->public, current_attribute);
301 }
302 attributes->destroy(attributes);
303
304 return &clone->public;
305 }
306
307
308 /**
309 * Implementation of transform_substructure_t.get_key_length.
310 */
311 static status_t get_key_length(private_transform_substructure_t *this, u_int16_t *key_length)
312 {
313 iterator_t *attributes;
314 transform_attribute_t *current_attribute;
315
316 attributes = this->attributes->create_iterator(this->attributes, TRUE);
317 while (attributes->iterate(attributes, (void**)&current_attribute))
318 {
319 if (current_attribute->get_attribute_type(current_attribute) == KEY_LENGTH)
320 {
321 *key_length = current_attribute->get_value(current_attribute);
322 attributes->destroy(attributes);
323 return SUCCESS;
324 }
325 }
326 attributes->destroy(attributes);
327 return FAILED;
328 }
329
330
331 /**
332 * Implementation of transform_substructure_t.destroy and payload_t.destroy.
333 */
334 static void destroy(private_transform_substructure_t *this)
335 {
336 this->attributes->destroy_offset(this->attributes,
337 offsetof(transform_attribute_t, destroy));
338 free(this);
339 }
340
341 /*
342 * Described in header.
343 */
344 transform_substructure_t *transform_substructure_create()
345 {
346 private_transform_substructure_t *this = malloc_thing(private_transform_substructure_t);
347
348 /* payload interface */
349 this->public.payload_interface.verify = (status_t (*) (payload_t *))verify;
350 this->public.payload_interface.get_encoding_rules = (void (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
351 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
352 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
353 this->public.payload_interface.set_next_type = (void (*) (payload_t *,payload_type_t)) set_next_type;
354 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_type;
355 this->public.payload_interface.destroy = (void (*) (payload_t *))destroy;
356
357 /* public functions */
358 this->public.create_transform_attribute_iterator = (iterator_t * (*) (transform_substructure_t *,bool)) create_transform_attribute_iterator;
359 this->public.add_transform_attribute = (void (*) (transform_substructure_t *,transform_attribute_t *)) add_transform_attribute;
360 this->public.set_is_last_transform = (void (*) (transform_substructure_t *,bool)) set_is_last_transform;
361 this->public.get_is_last_transform = (bool (*) (transform_substructure_t *)) get_is_last_transform;
362 this->public.set_transform_type = (void (*) (transform_substructure_t *,u_int8_t)) set_transform_type;
363 this->public.get_transform_type = (u_int8_t (*) (transform_substructure_t *)) get_transform_type;
364 this->public.set_transform_id = (void (*) (transform_substructure_t *,u_int16_t)) set_transform_id;
365 this->public.get_transform_id = (u_int16_t (*) (transform_substructure_t *)) get_transform_id;
366 this->public.get_key_length = (status_t (*) (transform_substructure_t *,u_int16_t *)) get_key_length;
367 this->public.clone = (transform_substructure_t* (*) (transform_substructure_t *)) clone_;
368 this->public.destroy = (void (*) (transform_substructure_t *)) destroy;
369
370 /* set default values of the fields */
371 this->next_payload = NO_PAYLOAD;
372 this->transform_length = TRANSFORM_SUBSTRUCTURE_HEADER_LENGTH;
373 this->transform_id = 0;
374 this->transform_type = 0;
375 this->attributes = linked_list_create();
376
377 return (&(this->public));
378 }
379
380 /*
381 * Described in header
382 */
383 transform_substructure_t *transform_substructure_create_type(
384 transform_type_t transform_type,
385 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 if (key_length)
393 {
394 transform_attribute_t *attribute;
395
396 attribute = transform_attribute_create_key_length(key_length);
397 transform->add_transform_attribute(transform, attribute);
398
399 }
400 return transform;
401 }
402