- removed memory allocation checks!!!
[strongswan.git] / Source / charon / encoding / payloads / sa_payload.c
1 /**
2 * @file sa_payload.c
3 *
4 * @brief Implementation of sa_payload_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 /* offsetof macro */
24 #include <stddef.h>
25
26 #include "sa_payload.h"
27
28 #include <encoding/payloads/encodings.h>
29 #include <utils/allocator.h>
30 #include <utils/linked_list.h>
31
32
33 typedef struct private_sa_payload_t private_sa_payload_t;
34
35 /**
36 * Private data of an sa_payload_t object.
37 *
38 */
39 struct private_sa_payload_t {
40 /**
41 * Public sa_payload_t interface.
42 */
43 sa_payload_t public;
44
45 /**
46 * Next payload type.
47 */
48 u_int8_t next_payload;
49
50 /**
51 * Critical flag.
52 */
53 bool critical;
54
55 /**
56 * Length of this payload.
57 */
58 u_int16_t payload_length;
59
60 /**
61 * Proposals in this payload are stored in a linked_list_t.
62 */
63 linked_list_t * proposals;
64
65 /**
66 * @brief Computes the length of this payload.
67 *
68 * @param this calling private_sa_payload_t object
69 */
70 void (*compute_length) (private_sa_payload_t *this);
71 };
72
73 /**
74 * Encoding rules to parse or generate a IKEv2-SA Payload
75 *
76 * The defined offsets are the positions in a object of type
77 * private_sa_payload_t.
78 *
79 */
80 encoding_rule_t sa_payload_encodings[] = {
81 /* 1 Byte next payload type, stored in the field next_payload */
82 { U_INT_8, offsetof(private_sa_payload_t, next_payload) },
83 /* the critical bit */
84 { FLAG, offsetof(private_sa_payload_t, critical) },
85 /* 7 Bit reserved bits, nowhere stored */
86 { RESERVED_BIT, 0 },
87 { RESERVED_BIT, 0 },
88 { RESERVED_BIT, 0 },
89 { RESERVED_BIT, 0 },
90 { RESERVED_BIT, 0 },
91 { RESERVED_BIT, 0 },
92 { RESERVED_BIT, 0 },
93 /* Length of the whole SA payload*/
94 { PAYLOAD_LENGTH, offsetof(private_sa_payload_t, payload_length) },
95 /* Proposals are stored in a proposal substructure,
96 offset points to a linked_list_t pointer */
97 { PROPOSALS, offsetof(private_sa_payload_t, proposals) }
98 };
99
100 /*
101 1 2 3
102 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
103 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
104 ! Next Payload !C! RESERVED ! Payload Length !
105 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
106 ! !
107 ~ <Proposals> ~
108 ! !
109 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
110 */
111
112 /**
113 * Implementation of payload_t.verify.
114 */
115 static status_t verify(private_sa_payload_t *this)
116 {
117 int proposal_number = 1;
118 status_t status;
119 iterator_t *iterator;
120 bool first = TRUE;
121
122 if (this->critical)
123 {
124 /* critical bit set! */
125 return FAILED;
126 }
127
128 /* check proposal numbering */
129 status = this->proposals->create_iterator(this->proposals,&iterator,TRUE);
130 if (status != SUCCESS)
131 {
132 return status;
133 }
134
135 while(iterator->has_next(iterator))
136 {
137 proposal_substructure_t *current_proposal;
138 status = iterator->current(iterator,(void **)&current_proposal);
139 {
140 break;
141 }
142 if (current_proposal->get_proposal_number(current_proposal) > proposal_number)
143 {
144 if (first)
145 {
146 /* first number must be 1 */
147 status = FAILED;
148 break;
149 }
150
151 if (current_proposal->get_proposal_number(current_proposal) != (proposal_number + 1))
152 {
153 /* must be only one more then previous proposal */
154 status = FAILED;
155 break;
156 }
157 }
158 else if (current_proposal->get_proposal_number(current_proposal) < proposal_number)
159 {
160 iterator->destroy(iterator);
161 /* must not be smaller then proceeding one */
162 status = FAILED;
163 break;
164 }
165 first = FALSE;
166 }
167
168 iterator->destroy(iterator);
169 return status;
170 }
171
172
173 /**
174 * Implementation of payload_t.destroy and sa_payload_t.destroy.
175 */
176 static status_t destroy(private_sa_payload_t *this)
177 {
178 /* all proposals are getting destroyed */
179 while (this->proposals->get_count(this->proposals) > 0)
180 {
181 proposal_substructure_t *current_proposal;
182 this->proposals->remove_last(this->proposals,(void **)&current_proposal);
183 current_proposal->destroy(current_proposal);
184 }
185 this->proposals->destroy(this->proposals);
186
187 allocator_free(this);
188
189 return SUCCESS;
190 }
191
192 /**
193 * Implementation of payload_t.get_encoding_rules.
194 */
195 static void get_encoding_rules(private_sa_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
196 {
197 *rules = sa_payload_encodings;
198 *rule_count = sizeof(sa_payload_encodings) / sizeof(encoding_rule_t);
199 }
200
201 /**
202 * Implementation of payload_t.get_type.
203 */
204 static payload_type_t get_type(private_sa_payload_t *this)
205 {
206 return SECURITY_ASSOCIATION;
207 }
208
209 /**
210 * Implementation of payload_t.get_next_type.
211 */
212 static payload_type_t get_next_type(private_sa_payload_t *this)
213 {
214 return (this->next_payload);
215 }
216
217 /**
218 * Implementation of payload_t.set_next_type.
219 */
220 static void set_next_type(private_sa_payload_t *this,payload_type_t type)
221 {
222 this->next_payload = type;
223 }
224
225 /**
226 * Implementation of payload_t.get_length.
227 */
228 static size_t get_length(private_sa_payload_t *this)
229 {
230 this->compute_length(this);
231 return this->payload_length;
232 }
233
234 /**
235 * Implementation of sa_payload_t.create_proposal_substructure_iterator.
236 */
237 static void create_proposal_substructure_iterator (private_sa_payload_t *this,iterator_t **iterator,bool forward)
238 {
239 this->proposals->create_iterator(this->proposals,iterator,forward);
240 }
241
242 /**
243 * Implementation of sa_payload_t.add_proposal_substructure.
244 */
245 static void add_proposal_substructure (private_sa_payload_t *this,proposal_substructure_t *proposal)
246 {
247 this->proposals->insert_last(this->proposals,(void *) proposal);
248 this->compute_length(this);
249 }
250
251 /**
252 * Implementation of private_sa_payload_t.compute_length.
253 */
254 static void compute_length (private_sa_payload_t *this)
255 {
256 iterator_t *iterator;
257 size_t length = SA_PAYLOAD_HEADER_LENGTH;
258 this->proposals->create_iterator(this->proposals,&iterator,TRUE);
259 while (iterator->has_next(iterator))
260 {
261 payload_t *current_proposal;
262 iterator->current(iterator,(void **) &current_proposal);
263 length += current_proposal->get_length(current_proposal);
264 }
265 iterator->destroy(iterator);
266
267 this->payload_length = length;
268 }
269
270 /*
271 * Described in header.
272 */
273 sa_payload_t *sa_payload_create()
274 {
275 private_sa_payload_t *this = allocator_alloc_thing(private_sa_payload_t);
276
277 /* public interface */
278 this->public.payload_interface.verify = (status_t (*) (payload_t *))verify;
279 this->public.payload_interface.get_encoding_rules = (void (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
280 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
281 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
282 this->public.payload_interface.set_next_type = (void (*) (payload_t *,payload_type_t)) set_next_type;
283 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_type;
284 this->public.payload_interface.destroy = (void (*) (payload_t *))destroy;
285
286 /* public functions */
287 this->public.create_proposal_substructure_iterator = (void (*) (sa_payload_t *,iterator_t **,bool)) create_proposal_substructure_iterator;
288 this->public.add_proposal_substructure = (void (*) (sa_payload_t *,proposal_substructure_t *)) add_proposal_substructure;
289 this->public.destroy = (void (*) (sa_payload_t *)) destroy;
290
291 /* private functions */
292 this->compute_length = compute_length;
293
294 /* set default values of the fields */
295 this->critical = SA_PAYLOAD_CRITICAL_FLAG;
296 this->next_payload = NO_PAYLOAD;
297 this->payload_length = SA_PAYLOAD_HEADER_LENGTH;
298
299 this->proposals = linked_list_create();
300 return (&(this->public));
301 }
302
303