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