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