Removed strayed code fragment
[strongswan.git] / src / charon / encoding / payloads / sa_payload.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 "sa_payload.h"
20
21 #include <encoding/payloads/encodings.h>
22 #include <utils/linked_list.h>
23 #include <daemon.h>
24
25
26 typedef struct private_sa_payload_t private_sa_payload_t;
27
28 /**
29 * Private data of an sa_payload_t object.
30 *
31 */
32 struct private_sa_payload_t {
33 /**
34 * Public sa_payload_t interface.
35 */
36 sa_payload_t public;
37
38 /**
39 * Next payload type.
40 */
41 u_int8_t next_payload;
42
43 /**
44 * Critical flag.
45 */
46 bool critical;
47
48 /**
49 * Length of this payload.
50 */
51 u_int16_t payload_length;
52
53 /**
54 * Proposals in this payload are stored in a linked_list_t.
55 */
56 linked_list_t * proposals;
57 };
58
59 /**
60 * Encoding rules to parse or generate a IKEv2-SA Payload
61 *
62 * The defined offsets are the positions in a object of type
63 * private_sa_payload_t.
64 *
65 */
66 encoding_rule_t sa_payload_encodings[] = {
67 /* 1 Byte next payload type, stored in the field next_payload */
68 { U_INT_8, offsetof(private_sa_payload_t, next_payload) },
69 /* the critical bit */
70 { FLAG, offsetof(private_sa_payload_t, critical) },
71 /* 7 Bit reserved bits, nowhere stored */
72 { RESERVED_BIT, 0 },
73 { RESERVED_BIT, 0 },
74 { RESERVED_BIT, 0 },
75 { RESERVED_BIT, 0 },
76 { RESERVED_BIT, 0 },
77 { RESERVED_BIT, 0 },
78 { RESERVED_BIT, 0 },
79 /* Length of the whole SA payload*/
80 { PAYLOAD_LENGTH, offsetof(private_sa_payload_t, payload_length) },
81 /* Proposals are stored in a proposal substructure,
82 offset points to a linked_list_t pointer */
83 { PROPOSALS, offsetof(private_sa_payload_t, proposals) }
84 };
85
86 /*
87 1 2 3
88 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
89 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
90 ! Next Payload !C! RESERVED ! Payload Length !
91 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
92 ! !
93 ~ <Proposals> ~
94 ! !
95 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
96 */
97
98 /**
99 * Implementation of payload_t.verify.
100 */
101 static status_t verify(private_sa_payload_t *this)
102 {
103 int expected_number = 1, current_number;
104 status_t status = SUCCESS;
105 iterator_t *iterator;
106 proposal_substructure_t *current_proposal;
107 bool first = TRUE;
108
109 /* check proposal numbering */
110 iterator = this->proposals->create_iterator(this->proposals,TRUE);
111
112 while(iterator->iterate(iterator, (void**)&current_proposal))
113 {
114 current_number = current_proposal->get_proposal_number(current_proposal);
115 if (current_number < expected_number)
116 {
117 if (current_number != (expected_number + 1))
118 {
119 DBG1(DBG_ENC, "proposal number is %d, expected %d or %d",
120 current_number, expected_number, expected_number + 1);
121 status = FAILED;
122 break;
123 }
124 }
125 else if (current_number < expected_number)
126 {
127 /* must not be smaller then proceeding one */
128 DBG1(DBG_ENC, "proposal number smaller than that of previous proposal");
129 status = FAILED;
130 break;
131 }
132
133 status = current_proposal->payload_interface.verify(&(current_proposal->payload_interface));
134 if (status != SUCCESS)
135 {
136 DBG1(DBG_ENC, "PROPOSAL_SUBSTRUCTURE verification failed");
137 break;
138 }
139 first = FALSE;
140 expected_number = current_number;
141 }
142
143 iterator->destroy(iterator);
144 return status;
145 }
146
147
148 /**
149 * Implementation of payload_t.destroy and sa_payload_t.destroy.
150 */
151 static status_t destroy(private_sa_payload_t *this)
152 {
153 this->proposals->destroy_offset(this->proposals,
154 offsetof(proposal_substructure_t, destroy));
155 free(this);
156 return SUCCESS;
157 }
158
159 /**
160 * Implementation of payload_t.get_encoding_rules.
161 */
162 static void get_encoding_rules(private_sa_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
163 {
164 *rules = sa_payload_encodings;
165 *rule_count = sizeof(sa_payload_encodings) / sizeof(encoding_rule_t);
166 }
167
168 /**
169 * Implementation of payload_t.get_type.
170 */
171 static payload_type_t get_type(private_sa_payload_t *this)
172 {
173 return SECURITY_ASSOCIATION;
174 }
175
176 /**
177 * Implementation of payload_t.get_next_type.
178 */
179 static payload_type_t get_next_type(private_sa_payload_t *this)
180 {
181 return (this->next_payload);
182 }
183
184 /**
185 * Implementation of payload_t.set_next_type.
186 */
187 static void set_next_type(private_sa_payload_t *this,payload_type_t type)
188 {
189 this->next_payload = type;
190 }
191
192 /**
193 * recompute length of the payload.
194 */
195 static void compute_length (private_sa_payload_t *this)
196 {
197 iterator_t *iterator;
198 payload_t *current_proposal;
199 size_t length = SA_PAYLOAD_HEADER_LENGTH;
200
201 iterator = this->proposals->create_iterator(this->proposals,TRUE);
202 while (iterator->iterate(iterator, (void **)&current_proposal))
203 {
204 length += current_proposal->get_length(current_proposal);
205 }
206 iterator->destroy(iterator);
207
208 this->payload_length = length;
209 }
210
211 /**
212 * Implementation of payload_t.get_length.
213 */
214 static size_t get_length(private_sa_payload_t *this)
215 {
216 compute_length(this);
217 return this->payload_length;
218 }
219
220 /**
221 * Implementation of sa_payload_t.create_proposal_substructure_iterator.
222 */
223 static iterator_t *create_proposal_substructure_iterator (private_sa_payload_t *this,bool forward)
224 {
225 return this->proposals->create_iterator(this->proposals,forward);
226 }
227
228 /**
229 * Implementation of sa_payload_t.add_proposal_substructure.
230 */
231 static void add_proposal_substructure(private_sa_payload_t *this,proposal_substructure_t *proposal)
232 {
233 status_t status;
234 u_int proposal_count = this->proposals->get_count(this->proposals);
235
236 if (proposal_count > 0)
237 {
238 proposal_substructure_t *last_proposal;
239 status = this->proposals->get_last(this->proposals,(void **) &last_proposal);
240 /* last transform is now not anymore last one */
241 last_proposal->set_is_last_proposal(last_proposal, FALSE);
242 }
243 proposal->set_is_last_proposal(proposal, TRUE);
244 proposal->set_proposal_number(proposal, proposal_count + 1);
245 this->proposals->insert_last(this->proposals,(void *) proposal);
246 compute_length(this);
247 }
248
249 /**
250 * Implementation of sa_payload_t.add_proposal.
251 */
252 static void add_proposal(private_sa_payload_t *this, proposal_t *proposal)
253 {
254 proposal_substructure_t *substructure;
255
256 substructure = proposal_substructure_create_from_proposal(proposal);
257 add_proposal_substructure(this, substructure);
258 }
259
260 /**
261 * Implementation of sa_payload_t.get_proposals.
262 */
263 static linked_list_t *get_proposals(private_sa_payload_t *this)
264 {
265 int struct_number = 0;
266 int ignore_struct_number = 0;
267 iterator_t *iterator;
268 proposal_substructure_t *proposal_struct;
269 linked_list_t *proposal_list;
270
271 /* this list will hold our proposals */
272 proposal_list = linked_list_create();
273
274 /* we do not support proposals split up to two proposal substructures, as
275 * AH+ESP bundles are not supported in RFC4301 anymore.
276 * To handle such structures safely, we just skip proposals with multiple
277 * protocols.
278 */
279 iterator = this->proposals->create_iterator(this->proposals, TRUE);
280 while (iterator->iterate(iterator, (void **)&proposal_struct))
281 {
282 proposal_t *proposal;
283
284 /* check if a proposal has a single protocol */
285 if (proposal_struct->get_proposal_number(proposal_struct) == struct_number)
286 {
287 if (ignore_struct_number < struct_number)
288 {
289 /* remova an already added, if first of series */
290 proposal_list->remove_last(proposal_list, (void**)&proposal);
291 proposal->destroy(proposal);
292 ignore_struct_number = struct_number;
293 }
294 continue;
295 }
296 struct_number++;
297 proposal = proposal_struct->get_proposal(proposal_struct);
298 if (proposal)
299 {
300 proposal_list->insert_last(proposal_list, proposal);
301 }
302 }
303 iterator->destroy(iterator);
304 return proposal_list;
305 }
306
307 /*
308 * Described in header.
309 */
310 sa_payload_t *sa_payload_create()
311 {
312 private_sa_payload_t *this = malloc_thing(private_sa_payload_t);
313
314 /* public interface */
315 this->public.payload_interface.verify = (status_t (*) (payload_t *))verify;
316 this->public.payload_interface.get_encoding_rules = (void (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
317 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
318 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
319 this->public.payload_interface.set_next_type = (void (*) (payload_t *,payload_type_t)) set_next_type;
320 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_type;
321 this->public.payload_interface.destroy = (void (*) (payload_t *))destroy;
322
323 /* public functions */
324 this->public.create_proposal_substructure_iterator = (iterator_t* (*) (sa_payload_t *,bool)) create_proposal_substructure_iterator;
325 this->public.add_proposal_substructure = (void (*) (sa_payload_t *,proposal_substructure_t *)) add_proposal_substructure;
326 this->public.add_proposal = (void (*) (sa_payload_t*,proposal_t*))add_proposal;
327 this->public.get_proposals = (linked_list_t* (*) (sa_payload_t *)) get_proposals;
328 this->public.destroy = (void (*) (sa_payload_t *)) destroy;
329
330 /* set default values of the fields */
331 this->critical = FALSE;
332 this->next_payload = NO_PAYLOAD;
333 this->payload_length = SA_PAYLOAD_HEADER_LENGTH;
334 this->proposals = linked_list_create();
335 return &this->public;
336 }
337
338 /*
339 * Described in header.
340 */
341 sa_payload_t *sa_payload_create_from_proposal_list(linked_list_t *proposals)
342 {
343 iterator_t *iterator;
344 proposal_t *proposal;
345 sa_payload_t *sa_payload = sa_payload_create();
346
347 /* add every payload from the list */
348 iterator = proposals->create_iterator(proposals, TRUE);
349 while (iterator->iterate(iterator, (void**)&proposal))
350 {
351 add_proposal((private_sa_payload_t*)sa_payload, proposal);
352 }
353 iterator->destroy(iterator);
354
355 return sa_payload;
356 }
357
358 /*
359 * Described in header.
360 */
361 sa_payload_t *sa_payload_create_from_proposal(proposal_t *proposal)
362 {
363 sa_payload_t *sa_payload = sa_payload_create();
364
365 add_proposal((private_sa_payload_t*)sa_payload, proposal);
366
367 return sa_payload;
368 }