Migrated transform_substructure to INIT/METHOD macros
[strongswan.git] / src / libcharon / encoding / payloads / transform_substructure.c
1 /*
2 * Copyright (C) 2005-2010 Martin Willi
3 * Copyright (C) 2010 revosec AG
4 * Copyright (C) 2005 Jan Hutter
5 * Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 #include <stddef.h>
19
20 #include "transform_substructure.h"
21
22 #include <encoding/payloads/transform_attribute.h>
23 #include <encoding/payloads/encodings.h>
24 #include <library.h>
25 #include <utils/linked_list.h>
26 #include <daemon.h>
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 struct private_transform_substructure_t {
34
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 * Length of this payload.
47 */
48 u_int16_t transform_length;
49
50 /**
51 * Type of the transform.
52 */
53 u_int8_t transform_type;
54
55 /**
56 * Transform ID.
57 */
58 u_int16_t transform_id;
59
60 /**
61 * Transforms Attributes are stored in a linked_list_t.
62 */
63 linked_list_t *attributes;
64 };
65
66 /**
67 * Encoding rules to parse or generate a Transform substructure.
68 *
69 * The defined offsets are the positions in a object of type
70 * private_transform_substructure_t.
71 */
72 encoding_rule_t transform_substructure_encodings[] = {
73 /* 1 Byte next payload type, stored in the field next_payload */
74 { U_INT_8, offsetof(private_transform_substructure_t, next_payload) },
75 /* Reserved Byte is skipped */
76 { RESERVED_BYTE, 0 },
77 /* Length of the whole transform substructure*/
78 { PAYLOAD_LENGTH, offsetof(private_transform_substructure_t, transform_length)},
79 /* transform type is a number of 8 bit */
80 { U_INT_8, offsetof(private_transform_substructure_t, transform_type) },
81 /* Reserved Byte is skipped */
82 { RESERVED_BYTE, 0 },
83 /* tranform ID is a number of 8 bit */
84 { U_INT_16, offsetof(private_transform_substructure_t, transform_id) },
85 /* Attributes are stored in a transform attribute,
86 offset points to a linked_list_t pointer */
87 { TRANSFORM_ATTRIBUTES, offsetof(private_transform_substructure_t, attributes) }
88 };
89
90 /*
91 1 2 3
92 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
93 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
94 ! 0 (last) or 3 ! RESERVED ! Transform Length !
95 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
96 !Transform Type ! RESERVED ! Transform ID !
97 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
98 ! !
99 ~ Transform Attributes ~
100 ! !
101 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
102 */
103
104 METHOD(payload_t, verify, status_t,
105 private_transform_substructure_t *this)
106 {
107 status_t status = SUCCESS;
108 enumerator_t *enumerator;
109 payload_t *attribute;
110
111 if (this->next_payload != NO_PAYLOAD && this->next_payload != 3)
112 {
113 DBG1(DBG_ENC, "inconsistent next payload");
114 return FAILED;
115 }
116
117 switch (this->transform_type)
118 {
119 case ENCRYPTION_ALGORITHM:
120 case PSEUDO_RANDOM_FUNCTION:
121 case INTEGRITY_ALGORITHM:
122 case DIFFIE_HELLMAN_GROUP:
123 case EXTENDED_SEQUENCE_NUMBERS:
124 /* we don't check transform ID, we want to reply
125 * cleanly with NO_PROPOSAL_CHOSEN or so if we don't support it */
126 break;
127 default:
128 {
129 DBG1(DBG_ENC, "invalid transform type: %d", this->transform_type);
130 return FAILED;
131 }
132 }
133
134 enumerator = this->attributes->create_enumerator(this->attributes);
135 while (enumerator->enumerate(enumerator, &attribute))
136 {
137 status = attribute->verify(attribute);
138 if (status != SUCCESS)
139 {
140 DBG1(DBG_ENC, "TRANSFORM_ATTRIBUTE verification failed");
141 break;
142 }
143 }
144 enumerator->destroy(enumerator);
145
146 /* proposal number is checked in SA payload */
147 return status;
148 }
149
150 METHOD(payload_t, get_encoding_rules, void,
151 private_transform_substructure_t *this, encoding_rule_t **rules,
152 size_t *rule_count)
153 {
154 *rules = transform_substructure_encodings;
155 *rule_count = countof(transform_substructure_encodings);
156 }
157
158 METHOD(payload_t, get_type, payload_type_t,
159 private_transform_substructure_t *this)
160 {
161 return TRANSFORM_SUBSTRUCTURE;
162 }
163
164 METHOD(payload_t, get_next_type, payload_type_t,
165 private_transform_substructure_t *this)
166 {
167 return this->next_payload;
168 }
169
170 /**
171 * recompute the length of the payload.
172 */
173 static void compute_length (private_transform_substructure_t *this)
174 {
175 enumerator_t *enumerator;
176 payload_t *attribute;
177
178 this->transform_length = TRANSFORM_SUBSTRUCTURE_HEADER_LENGTH;
179 enumerator = this->attributes->create_enumerator(this->attributes);
180 while (enumerator->enumerate(enumerator, &attribute))
181 {
182 this->transform_length += attribute->get_length(attribute);
183 }
184 enumerator->destroy(enumerator);
185 }
186
187 METHOD(payload_t, get_length, size_t,
188 private_transform_substructure_t *this)
189 {
190 return this->transform_length;
191 }
192
193 METHOD(transform_substructure_t, set_is_last_transform, void,
194 private_transform_substructure_t *this, bool is_last)
195 {
196 this->next_payload = is_last ? 0: TRANSFORM_TYPE_VALUE;
197 }
198
199 METHOD(payload_t, set_next_type, void,
200 private_transform_substructure_t *this,payload_type_t type)
201 {
202 }
203
204 METHOD(transform_substructure_t, get_transform_type, u_int8_t,
205 private_transform_substructure_t *this)
206 {
207 return this->transform_type;
208 }
209
210 METHOD(transform_substructure_t, get_transform_id, u_int16_t,
211 private_transform_substructure_t *this)
212 {
213 return this->transform_id;
214 }
215
216 METHOD(transform_substructure_t, get_key_length, status_t,
217 private_transform_substructure_t *this, u_int16_t *key_length)
218 {
219 enumerator_t *enumerator;
220 transform_attribute_t *attribute;
221
222 enumerator = this->attributes->create_enumerator(this->attributes);
223 while (enumerator->enumerate(enumerator, &attribute))
224 {
225 if (attribute->get_attribute_type(attribute) == KEY_LENGTH)
226 {
227 *key_length = attribute->get_value(attribute);
228 enumerator->destroy(enumerator);
229 return SUCCESS;
230 }
231 }
232 enumerator->destroy(enumerator);
233 return FAILED;
234 }
235
236 METHOD2(payload_t, transform_substructure_t, destroy, void,
237 private_transform_substructure_t *this)
238 {
239 this->attributes->destroy_offset(this->attributes,
240 offsetof(transform_attribute_t, destroy));
241 free(this);
242 }
243
244 /*
245 * Described in header.
246 */
247 transform_substructure_t *transform_substructure_create()
248 {
249 private_transform_substructure_t *this;
250
251 INIT(this,
252 .public = {
253 .payload_interface = {
254 .verify = _verify,
255 .get_encoding_rules = _get_encoding_rules,
256 .get_length = _get_length,
257 .get_next_type = _get_next_type,
258 .set_next_type = _set_next_type,
259 .get_type = _get_type,
260 .destroy = _destroy,
261 },
262 .set_is_last_transform = _set_is_last_transform,
263 .get_transform_type = _get_transform_type,
264 .get_transform_id = _get_transform_id,
265 .get_key_length = _get_key_length,
266 .destroy = _destroy,
267 },
268 .next_payload = NO_PAYLOAD,
269 .transform_length = TRANSFORM_SUBSTRUCTURE_HEADER_LENGTH,
270 .attributes = linked_list_create(),
271 );
272 return &this->public;
273 }
274
275 /*
276 * Described in header
277 */
278 transform_substructure_t *transform_substructure_create_type(
279 transform_type_t type, u_int16_t id, u_int16_t key_length)
280 {
281 private_transform_substructure_t *this;
282
283 this = (private_transform_substructure_t*)transform_substructure_create();
284
285 this->transform_type = type;
286 this->transform_id = id;
287 if (key_length)
288 {
289 this->attributes->insert_last(this->attributes,
290 (void*)transform_attribute_create_key_length(key_length));
291 compute_length(this);
292 }
293 return &this->public;
294 }
295