- created encoding package
[strongswan.git] / Source / charon / encoding / payloads / transform_attribute.c
1 /**
2 * @file transform_attribute.c
3 *
4 * @brief Declaration of the class transform_attribute_t.
5 *
6 * An object of this type represents an IKEv2 TRANSFORM attribute.
7 *
8 */
9
10 /*
11 * Copyright (C) 2005 Jan Hutter, Martin Willi
12 * Hochschule fuer Technik Rapperswil
13 *
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
18 *
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * for more details.
23 */
24
25 /* offsetof macro */
26 #include <stddef.h>
27
28 #include "transform_attribute.h"
29
30 #include <encoding/payloads/encodings.h>
31 #include <types.h>
32 #include <utils/allocator.h>
33
34 /**
35 * Private data of an transform_attribute_t Object
36 *
37 */
38 typedef struct private_transform_attribute_s private_transform_attribute_t;
39
40 struct private_transform_attribute_s {
41 /**
42 * public transform_attribute_t interface
43 */
44 transform_attribute_t public;
45
46 /**
47 * Attribute Format Flag
48 *
49 * - TRUE means value is stored in attribute_length_or_value
50 * - FALSE means value is stored in attribute_value
51 */
52 bool attribute_format;
53
54 /**
55 * Type of the attribute
56 */
57 u_int16_t attribute_type;
58
59 /**
60 * Attribute Length if attribute_format is 0, attribute Value otherwise
61 */
62 u_int16_t attribute_length_or_value;
63
64 /**
65 * Attribute value as chunk if attribute_format is 0 (FALSE)
66 */
67 chunk_t attribute_value;
68 };
69
70
71
72 /**
73 * string mappings for transform_attribute_type_t
74 */
75 mapping_t transform_attribute_type_m[] = {
76 {ATTRIBUTE_UNDEFINED, "ATTRIBUTE_UNDEFINED"},
77 {KEY_LENGTH, "KEY_LENGTH"},
78 {MAPPING_END, NULL}
79 };
80
81 /**
82 * Encoding rules to parse or generate a Transform attribute
83 *
84 * The defined offsets are the positions in a object of type
85 * private_transform_attribute_t.
86 *
87 */
88 encoding_rule_t transform_attribute_encodings[] = {
89 /* Flag defining the format of this payload */
90 { ATTRIBUTE_FORMAT, offsetof(private_transform_attribute_t, attribute_format) },
91 /* type of the attribute as 15 bit unsigned integer */
92 { ATTRIBUTE_TYPE, offsetof(private_transform_attribute_t, attribute_type) },
93 /* Length or value, depending on the attribute format flag */
94 { ATTRIBUTE_LENGTH_OR_VALUE, offsetof(private_transform_attribute_t, attribute_length_or_value) },
95 /* Value of attribute if attribute format flag is zero */
96 { ATTRIBUTE_VALUE, offsetof(private_transform_attribute_t, attribute_value) }
97 };
98
99 /*
100 1 2 3
101 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
102 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
103 !A! Attribute Type ! AF=0 Attribute Length !
104 !F! ! AF=1 Attribute Value !
105 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
106 ! AF=0 Attribute Value !
107 ! AF=1 Not Transmitted !
108 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
109 */
110
111 /**
112 * Implements payload_t's verify function.
113 * See #payload_s.verify for description.
114 */
115 static status_t verify(private_transform_attribute_t *this)
116 {
117 if (this->attribute_type != KEY_LENGTH)
118 {
119 return FAILED;
120 }
121
122 return SUCCESS;
123 }
124
125 /**
126 * Implements payload_t's get_encoding_rules function.
127 * See #payload_s.get_encoding_rules for description.
128 */
129 static status_t get_encoding_rules(private_transform_attribute_t *this, encoding_rule_t **rules, size_t *rule_count)
130 {
131 *rules = transform_attribute_encodings;
132 *rule_count = sizeof(transform_attribute_encodings) / sizeof(encoding_rule_t);
133
134 return SUCCESS;
135 }
136
137 /**
138 * Implements payload_t's get_type function.
139 * See #payload_s.get_type for description.
140 */
141 static payload_type_t get_type(private_transform_attribute_t *this)
142 {
143 return TRANSFORM_ATTRIBUTE;
144 }
145
146 /**
147 * Implements payload_t's get_next_type function.
148 * See #payload_s.get_next_type for description.
149 */
150 static payload_type_t get_next_type(private_transform_attribute_t *this)
151 {
152 return (NO_PAYLOAD);
153 }
154
155 /**
156 * Implements payload_t's set_next_type function.
157 * See #payload_s.set_next_type for description.
158 */
159 static status_t set_next_type(private_transform_attribute_t *this,payload_type_t type)
160 {
161 return SUCCESS;
162 }
163
164 /**
165 * Implements payload_t's get_length function.
166 * See #payload_s.get_length for description.
167 */
168 static size_t get_length(private_transform_attribute_t *this)
169 {
170 if (this->attribute_format == TRUE)
171 {
172 /*Attribute size is only 4 byte */
173 return 4;
174 }
175 return (this->attribute_length_or_value + 4);
176 }
177 /**
178 * Implements transform_attribute_t's set_value function.
179 * See #transform_attribute_s.set_value for description.
180 */
181 static status_t set_value_chunk(private_transform_attribute_t *this, chunk_t value)
182 {
183 if (this->attribute_value.ptr != NULL)
184 {
185 /* free existing value */
186 allocator_free(this->attribute_value.ptr);
187 this->attribute_value.ptr = NULL;
188 this->attribute_value.len = 0;
189
190 }
191
192 if (value.len > 2)
193 {
194 this->attribute_value.ptr = allocator_clone_bytes(value.ptr,value.len);
195 if (this->attribute_value.ptr == NULL)
196 {
197 return OUT_OF_RES;
198 }
199 this->attribute_value.len = value.len;
200 this->attribute_length_or_value = value.len;
201 /* attribute has not a fixed length */
202 this->attribute_format = FALSE;
203 }
204 else
205 {
206 memcpy(&(this->attribute_length_or_value),value.ptr,value.len);
207 }
208 return SUCCESS;
209 }
210
211 /**
212 * Implements transform_attribute_t's set_value function.
213 * See #transform_attribute_s.set_value for description.
214 */
215 static status_t set_value(private_transform_attribute_t *this, u_int16_t value)
216 {
217 if (this->attribute_value.ptr != NULL)
218 {
219 /* free existing value */
220 allocator_free(this->attribute_value.ptr);
221 this->attribute_value.ptr = NULL;
222 this->attribute_value.len = 0;
223
224 }
225 this->attribute_length_or_value = value;
226 return SUCCESS;
227 }
228
229 /**
230 * Implements transform_attribute_t's get_value_chunk function.
231 * See #transform_attribute_s.get_value_chunk for description.
232 */
233 static chunk_t get_value_chunk (private_transform_attribute_t *this)
234 {
235 chunk_t value;
236
237 if (this->attribute_format == FALSE)
238 {
239 value.ptr = this->attribute_value.ptr;
240 value.len = this->attribute_value.len;
241 }
242 else
243 {
244 value.ptr = (void *) &(this->attribute_length_or_value);
245 value.len = 2;
246 }
247
248 return value;
249 }
250
251 /**
252 * Implements transform_attribute_t's get_value function.
253 * See #transform_attribute_s.get_value for description.
254 */
255 static u_int16_t get_value (private_transform_attribute_t *this)
256 {
257 return this->attribute_length_or_value;
258 }
259
260
261 /**
262 * Implements transform_attribute_t's set_attribute_type function.
263 * See #transform_attribute_s.set_attribute_type for description.
264 */
265 static status_t set_attribute_type (private_transform_attribute_t *this, u_int16_t type)
266 {
267 this->attribute_type = type & 0x7FFF;
268 return SUCCESS;
269 }
270
271 /**
272 * Implements transform_attribute_t's get_attribute_type function.
273 * See #transform_attribute_s.get_attribute_type for description.
274 */
275 static u_int16_t get_attribute_type (private_transform_attribute_t *this)
276 {
277 return this->attribute_type;
278 }
279
280 /**
281 * Implements transform_attribute_t's clone function.
282 * See transform_attribute_s.clone for description.
283 */
284 static status_t clone(private_transform_attribute_t *this,transform_attribute_t **clone)
285 {
286 private_transform_attribute_t *new_clone;
287
288 new_clone = (private_transform_attribute_t *) transform_attribute_create();
289
290 new_clone->attribute_format = this->attribute_format;
291 new_clone->attribute_type = this->attribute_type;
292 new_clone->attribute_length_or_value = this->attribute_length_or_value;
293
294 if (!new_clone->attribute_format)
295 {
296 new_clone->attribute_value.ptr = allocator_clone_bytes(this->attribute_value.ptr,this->attribute_value.len);
297 new_clone->attribute_value.len = this->attribute_value.len;
298 if (new_clone->attribute_value.ptr == NULL)
299 {
300 new_clone->public.destroy(&(new_clone->public));
301 return OUT_OF_RES;
302 }
303 }
304
305 *clone = (transform_attribute_t *) new_clone;
306 return SUCCESS;
307 }
308
309 /**
310 * Implements payload_t's and transform_attribute_t's destroy function.
311 * See #payload_s.destroy or transform_attribute_s.destroy for description.
312 */
313 static status_t destroy(private_transform_attribute_t *this)
314 {
315 if (this->attribute_value.ptr != NULL)
316 {
317 allocator_free(this->attribute_value.ptr);
318 }
319 allocator_free(this);
320
321 return SUCCESS;
322 }
323
324 /*
325 * Described in header
326 */
327 transform_attribute_t *transform_attribute_create()
328 {
329 private_transform_attribute_t *this = allocator_alloc_thing(private_transform_attribute_t);
330 if (this == NULL)
331 {
332 return NULL;
333 }
334
335 /* payload interface */
336 this->public.payload_interface.verify = (status_t (*) (payload_t *))verify;
337 this->public.payload_interface.get_encoding_rules = (status_t (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
338 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
339 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
340 this->public.payload_interface.set_next_type = (status_t (*) (payload_t *,payload_type_t)) set_next_type;
341 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_type;
342 this->public.payload_interface.destroy = (status_t (*) (payload_t *))destroy;
343
344 /* public functions */
345 this->public.set_value_chunk = (status_t (*) (transform_attribute_t *,chunk_t)) set_value_chunk;
346 this->public.set_value = (status_t (*) (transform_attribute_t *,u_int16_t)) set_value;
347 this->public.get_value_chunk = (chunk_t (*) (transform_attribute_t *)) get_value_chunk;
348 this->public.get_value = (u_int16_t (*) (transform_attribute_t *)) get_value;
349 this->public.set_attribute_type = (status_t (*) (transform_attribute_t *,u_int16_t type)) set_attribute_type;
350 this->public.get_attribute_type = (u_int16_t (*) (transform_attribute_t *)) get_attribute_type;
351 this->public.clone = (status_t (*) (transform_attribute_t *,transform_attribute_t **)) clone;
352 this->public.destroy = (status_t (*) (transform_attribute_t *)) destroy;
353
354 /* set default values of the fields */
355 this->attribute_format = TRUE;
356 this->attribute_type = 0;
357 this->attribute_length_or_value = 0;
358 this->attribute_value.ptr = NULL;
359 this->attribute_value.len = 0;
360
361 return (&(this->public));
362 }
363