- parser succesfully parses sa payload
[strongswan.git] / Source / charon / payloads / proposal_substructure.c
1 /**
2 * @file proposal_substructure.h
3 *
4 * @brief Declaration of the class proposal_substructure_t.
5 *
6 * An object of this type represents an IKEv2 PROPOSAL Substructure and contains transforms.
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 "proposal_substructure.h"
29
30 #include "encodings.h"
31 #include "transform_substructure.h"
32 #include "../types.h"
33 #include "../utils/allocator.h"
34 #include "../utils/linked_list.h"
35
36 /**
37 * Private data of an proposal_substructure_t' Object
38 *
39 */
40 typedef struct private_proposal_substructure_s private_proposal_substructure_t;
41
42 struct private_proposal_substructure_s {
43 /**
44 * public proposal_substructure_t interface
45 */
46 proposal_substructure_t public;
47
48 /**
49 * next payload type
50 */
51 u_int8_t next_payload;
52
53
54 /**
55 * Length of this payload
56 */
57 u_int16_t proposal_length;
58
59
60 /**
61 * Proposal number
62 */
63 u_int8_t proposal_number;
64
65 /**
66 * Protocol ID
67 */
68 u_int8_t protocol_id;
69
70 /**
71 * SPI size of the following SPI
72 */
73 u_int8_t spi_size;
74
75 /**
76 * Number of transforms
77 */
78 u_int8_t transforms_count;
79
80 /**
81 * SPI is stored as chunk
82 */
83 chunk_t spi;
84
85 /**
86 * Transforms are stored in a linked_list_t
87 */
88 linked_list_t * transforms;
89
90 /**
91 * @brief Computes the length of this substructure.
92 *
93 * @param this calling private_proposal_substructure_t object
94 * @return
95 * SUCCESS in any case
96 */
97 status_t (*compute_length) (private_proposal_substructure_t *this);
98 };
99
100 /**
101 * Encoding rules to parse or generate a Proposal substructure
102 *
103 * The defined offsets are the positions in a object of type
104 * private_proposal_substructure_t.
105 *
106 */
107 encoding_rule_t proposal_substructure_encodings[] = {
108 /* 1 Byte next payload type, stored in the field next_payload */
109 { U_INT_8, offsetof(private_proposal_substructure_t, next_payload) },
110 /* Reserved Byte is skipped */
111 { RESERVED_BYTE, 0 },
112 /* Length of the whole proposal substructure payload*/
113 { PAYLOAD_LENGTH, offsetof(private_proposal_substructure_t, proposal_length) },
114 /* proposal number is a number of 8 bit */
115 { U_INT_8, offsetof(private_proposal_substructure_t, proposal_number) },
116 /* protocol ID is a number of 8 bit */
117 { U_INT_8, offsetof(private_proposal_substructure_t, protocol_id) },
118 /* SPI Size has its own type */
119 { SPI_SIZE, offsetof(private_proposal_substructure_t, spi_size) },
120 /* Number of transforms is a number of 8 bit */
121 { U_INT_8, offsetof(private_proposal_substructure_t, transforms_count) },
122 /* SPI is a chunk of variable size*/
123 { SPI, offsetof(private_proposal_substructure_t, spi) },
124 /* Transforms are stored in a transform substructure,
125 offset points to a linked_list_t pointer */
126 { TRANSFORMS, offsetof(private_proposal_substructure_t, transforms) }
127 };
128
129 /**
130 * Implements payload_t's and proposal_substructure_t's destroy function.
131 * See #payload_s.destroy or proposal_substructure_s.destroy for description.
132 */
133 static status_t destroy(private_proposal_substructure_t *this)
134 {
135 /* all proposals are getting destroyed */
136 while (this->transforms->get_count(this->transforms) > 0)
137 {
138 transform_substructure_t *current_transform;
139 if (this->transforms->remove_last(this->transforms,(void **)&current_transform) != SUCCESS)
140 {
141 break;
142 }
143 current_transform->destroy(current_transform);
144 }
145 this->transforms->destroy(this->transforms);
146
147 if (this->spi.ptr != NULL)
148 {
149 allocator_free(this->spi.ptr);
150 }
151
152 allocator_free(this);
153
154 return SUCCESS;
155 }
156
157 /**
158 * Implements payload_t's get_encoding_rules function.
159 * See #payload_s.get_encoding_rules for description.
160 */
161 static status_t get_encoding_rules(private_proposal_substructure_t *this, encoding_rule_t **rules, size_t *rule_count)
162 {
163 *rules = proposal_substructure_encodings;
164 *rule_count = sizeof(proposal_substructure_encodings) / sizeof(encoding_rule_t);
165
166 return SUCCESS;
167 }
168
169 /**
170 * Implements payload_t's get_type function.
171 * See #payload_s.get_type for description.
172 */
173 static payload_type_t get_type(private_proposal_substructure_t *this)
174 {
175 return PROPOSAL_SUBSTRUCTURE;
176 }
177
178 /**
179 * Implements payload_t's get_next_type function.
180 * See #payload_s.get_next_type for description.
181 */
182 static payload_type_t get_next_type(private_proposal_substructure_t *this)
183 {
184 return (this->next_payload);
185 }
186
187 /**
188 * Implements payload_t's get_length function.
189 * See #payload_s.get_length for description.
190 */
191 static size_t get_length(private_proposal_substructure_t *this)
192 {
193 return this->proposal_length;
194 }
195
196 /**
197 * Implements proposal_substructure_t's create_transform_substructure_iterator function.
198 * See #proposal_substructure_s.create_transform_substructure_iterator for description.
199 */
200 static status_t create_transform_substructure_iterator (private_proposal_substructure_t *this,linked_list_iterator_t **iterator,bool forward)
201 {
202 return (this->transforms->create_iterator(this->transforms,iterator,forward));
203 }
204
205 /**
206 * Implements proposal_substructure_t's add_transform_substructure function.
207 * See #proposal_substructure_s.add_transform_substructure for description.
208 */
209 static status_t add_transform_substructure (private_proposal_substructure_t *this,transform_substructure_t *transform)
210 {
211 status_t status;
212 if (this->transforms->get_count(this->transforms) > 0)
213 {
214 transform_substructure_t *last_transform;
215 status = this->transforms->get_last(this->transforms,(void **) &last_transform);
216 /* last transform is now not anymore last one */
217 last_transform->set_is_last_transform(last_transform,FALSE);
218
219 }
220 transform->set_is_last_transform(transform,TRUE);
221
222 status = this->transforms->insert_last(this->transforms,(void *) transform);
223 this->compute_length(this);
224 return status;
225 }
226
227 /**
228 * Implements proposal_substructure_t's set_proposal_number function.
229 * See #proposal_substructure_s.set_proposal_number for description.
230 */
231 static status_t set_proposal_number(private_proposal_substructure_t *this,u_int8_t proposal_number)
232 {
233 this->proposal_number = proposal_number;
234 return SUCCESS;
235 }
236
237 /**
238 * Implements proposal_substructure_t's get_proposal_number function.
239 * See #proposal_substructure_s.get_proposal_number for description.
240 */
241 static u_int8_t get_proposal_number (private_proposal_substructure_t *this)
242 {
243 return (this->proposal_number);
244 }
245
246 /**
247 * Implements proposal_substructure_t's set_protocol_id function.
248 * See #proposal_substructure_s.set_protocol_id for description.
249 */
250 static status_t set_protocol_id(private_proposal_substructure_t *this,u_int8_t protocol_id)
251 {
252 this->protocol_id = protocol_id;
253 return SUCCESS;
254 }
255
256 /**
257 * Implements proposal_substructure_t's get_protocol_id function.
258 * See #proposal_substructure_s.get_protocol_id for description.
259 */
260 static u_int8_t get_protocol_id (private_proposal_substructure_t *this)
261 {
262 return (this->protocol_id);
263 }
264
265
266 /**
267 * Implements proposal_substructure_t's set_spi function.
268 * See #proposal_substructure_s.set_spi for description.
269 */
270 static status_t set_spi (private_proposal_substructure_t *this, chunk_t spi)
271 {
272 /* first delete already set spi value */
273 if (this->spi.ptr != NULL)
274 {
275 allocator_free(this->spi.ptr);
276 this->spi.ptr = NULL;
277 this->spi.len = 0;
278 this->compute_length(this);
279 }
280
281 this->spi.ptr = allocator_clone_bytes(spi.ptr,spi.len);
282 if (this->spi.ptr == NULL)
283 {
284 return OUT_OF_RES;
285 }
286 this->spi.len = spi.len;
287 this->spi_size = spi.len;
288 this->compute_length(this);
289
290 return SUCCESS;
291 }
292
293 /**
294 * Implements proposal_substructure_t's get_spi function.
295 * See #proposal_substructure_s.get_spi for description.
296 */
297 static chunk_t get_spi (private_proposal_substructure_t *this)
298 {
299 chunk_t spi;
300 spi.ptr = this->spi.ptr;
301 spi.len = this->spi.len;
302
303 return spi;
304 }
305
306 /**
307 * Implements private_proposal_substructure_t's compute_length function.
308 * See #private_proposal_substructure_s.compute_length for description.
309 */
310 static status_t compute_length (private_proposal_substructure_t *this)
311 {
312 linked_list_iterator_t *iterator;
313 status_t status;
314 size_t transforms_count = 0;
315 size_t length = PROPOSAL_SUBSTRUCTURE_HEADER_LENGTH;
316 status = this->transforms->create_iterator(this->transforms,&iterator,TRUE);
317 if (status != SUCCESS)
318 {
319 return length;
320 }
321 while (iterator->has_next(iterator))
322 {
323 payload_t * current_transform;
324 iterator->current(iterator,(void **) &current_transform);
325 length += current_transform->get_length(current_transform);
326 transforms_count++;
327 }
328 iterator->destroy(iterator);
329
330 length += this->spi.len;
331 this->transforms_count= transforms_count;
332 this->proposal_length = length;
333
334 return SUCCESS;
335 }
336
337
338 /*
339 * Described in header
340 */
341 proposal_substructure_t *proposal_substructure_create()
342 {
343 private_proposal_substructure_t *this = allocator_alloc_thing(private_proposal_substructure_t);
344 if (this == NULL)
345 {
346 return NULL;
347 }
348
349 this->public.payload_interface.get_encoding_rules = (status_t (*) (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.get_type = (payload_type_t (*) (payload_t *)) get_type;
353 this->public.payload_interface.destroy = (status_t (*) (payload_t *))destroy;
354 this->public.create_transform_substructure_iterator = (status_t (*) (proposal_substructure_t *,linked_list_iterator_t **,bool)) create_transform_substructure_iterator;
355 this->public.add_transform_substructure = (status_t (*) (proposal_substructure_t *,transform_substructure_t *)) add_transform_substructure;
356 this->public.set_proposal_number = (status_t (*) (proposal_substructure_t *,u_int8_t))set_proposal_number;
357 this->public.get_proposal_number = (u_int8_t (*) (proposal_substructure_t *)) get_proposal_number;
358 this->public.set_protocol_id = (status_t (*) (proposal_substructure_t *,u_int8_t))set_protocol_id;
359 this->public.get_protocol_id = (u_int8_t (*) (proposal_substructure_t *)) get_protocol_id;
360 this->public.set_spi = (status_t (*) (proposal_substructure_t *,chunk_t))set_spi;
361 this->public.get_spi = (chunk_t (*) (proposal_substructure_t *)) get_spi;
362 this->public.destroy = (status_t (*) (proposal_substructure_t *)) destroy;
363
364 /* private functions */
365 this->compute_length = compute_length;
366
367 /* set default values of the fields */
368 this->next_payload = NO_PAYLOAD;
369 this->proposal_length = 0;
370 this->proposal_number = 0;
371 this->protocol_id = 0;
372 this->transforms_count = 0;
373 this->spi_size = 0;
374 this->spi.ptr = NULL;
375 this->spi.len = 0;
376
377 this->transforms = linked_list_create();
378
379 if (this->transforms == NULL)
380 {
381 allocator_free(this);
382 return NULL;
383 }
384 return (&(this->public));
385 }