]> git.ipfire.org Git - people/ms/strongswan.git/blob - programs/charon/charon/encoding/payloads/sa_payload.c
- renamed get_block_size of hasher
[people/ms/strongswan.git] / programs / charon / 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 if (this->proposals->get_count(this->proposals) > 0)
239 {
240 proposal_substructure_t *last_proposal;
241 status = this->proposals->get_last(this->proposals,(void **) &last_proposal);
242 /* last transform is now not anymore last one */
243 last_proposal->set_is_last_proposal(last_proposal,FALSE);
244 }
245 proposal->set_is_last_proposal(proposal,TRUE);
246
247 this->proposals->insert_last(this->proposals,(void *) proposal);
248 this->compute_length(this);
249 }
250
251 /**
252 * Implementation of sa_payload_t.add_proposal.
253 */
254 static void add_proposal(private_sa_payload_t *this, proposal_t *proposal)
255 {
256 proposal_substructure_t *substructure;
257 protocol_id_t proto[2];
258 u_int i;
259
260 /* build the substructures for every protocol */
261 proposal->get_protocols(proposal, proto);
262 for (i = 0; i<2; i++)
263 {
264 if (proto[i] != PROTO_NONE)
265 {
266 substructure = proposal_substructure_create_from_proposal(proposal, proto[i]);
267 add_proposal_substructure(this, substructure);
268 }
269 }
270 }
271
272 /**
273 * Implementation of sa_payload_t.get_proposals.
274 */
275 static linked_list_t *get_proposals(private_sa_payload_t *this)
276 {
277 int proposal_struct_number = 0;
278 iterator_t *iterator;
279 proposal_t *proposal;
280 linked_list_t *proposal_list;
281
282 /* this list will hold our proposals */
283 proposal_list = linked_list_create();
284
285 /* iterate over structures, one OR MORE structures will result in a proposal */
286 iterator = this->proposals->create_iterator(this->proposals,TRUE);
287 while (iterator->has_next(iterator))
288 {
289 proposal_substructure_t *proposal_struct;
290 iterator->current(iterator,(void **)&(proposal_struct));
291
292 if (proposal_struct->get_proposal_number(proposal_struct) > proposal_struct_number)
293 {
294 /* here starts a new proposal, create a new one and add it to the list */
295 proposal_struct_number = proposal_struct->get_proposal_number(proposal_struct);
296 proposal = proposal_create(proposal_struct_number);
297 proposal_list->insert_last(proposal_list, proposal);
298 }
299 /* proposal_substructure_t does the dirty work and builds up the proposal */
300 proposal_struct->add_to_proposal(proposal_struct, proposal);
301 }
302 iterator->destroy(iterator);
303 return proposal_list;
304 }
305
306 /**
307 * Implementation of private_sa_payload_t.compute_length.
308 */
309 static void compute_length (private_sa_payload_t *this)
310 {
311 iterator_t *iterator;
312 size_t length = SA_PAYLOAD_HEADER_LENGTH;
313 iterator = this->proposals->create_iterator(this->proposals,TRUE);
314 while (iterator->has_next(iterator))
315 {
316 payload_t *current_proposal;
317 iterator->current(iterator,(void **) &current_proposal);
318 length += current_proposal->get_length(current_proposal);
319 }
320 iterator->destroy(iterator);
321
322 this->payload_length = length;
323 }
324
325 /*
326 * Described in header.
327 */
328 sa_payload_t *sa_payload_create()
329 {
330 private_sa_payload_t *this = malloc_thing(private_sa_payload_t);
331
332 /* public interface */
333 this->public.payload_interface.verify = (status_t (*) (payload_t *))verify;
334 this->public.payload_interface.get_encoding_rules = (void (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
335 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
336 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
337 this->public.payload_interface.set_next_type = (void (*) (payload_t *,payload_type_t)) set_next_type;
338 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_type;
339 this->public.payload_interface.destroy = (void (*) (payload_t *))destroy;
340
341 /* public functions */
342 this->public.create_proposal_substructure_iterator = (iterator_t* (*) (sa_payload_t *,bool)) create_proposal_substructure_iterator;
343 this->public.add_proposal_substructure = (void (*) (sa_payload_t *,proposal_substructure_t *)) add_proposal_substructure;
344 this->public.get_proposals = (linked_list_t* (*) (sa_payload_t *)) get_proposals;
345 this->public.destroy = (void (*) (sa_payload_t *)) destroy;
346
347 /* private functions */
348 this->compute_length = compute_length;
349
350 /* set default values of the fields */
351 this->critical = FALSE;
352 this->next_payload = NO_PAYLOAD;
353 this->payload_length = SA_PAYLOAD_HEADER_LENGTH;
354
355 this->proposals = linked_list_create();
356 return (&(this->public));
357 }
358
359 /*
360 * Described in header.
361 */
362 sa_payload_t *sa_payload_create_from_proposal_list(linked_list_t *proposals)
363 {
364 iterator_t *iterator;
365 proposal_t *proposal;
366 sa_payload_t *sa_payload = sa_payload_create();
367
368 /* add every payload from the list */
369 iterator = proposals->create_iterator(proposals, TRUE);
370 while (iterator->has_next(iterator))
371 {
372 iterator->current(iterator, (void**)&proposal);
373 add_proposal((private_sa_payload_t*)sa_payload, proposal);
374 }
375 iterator->destroy(iterator);
376
377 return sa_payload;
378 }
379
380 /*
381 * Described in header.
382 */
383 sa_payload_t *sa_payload_create_from_proposal(proposal_t *proposal)
384 {
385 sa_payload_t *sa_payload = sa_payload_create();
386
387 add_proposal((private_sa_payload_t*)sa_payload, proposal);
388
389 return sa_payload;
390 }