89464ac3d6403e04f960184fa5a3068b40e70540
[strongswan.git] / src / libcharon / encoding / payloads / sa_payload.c
1 /*
2 * Copyright (C) 2005-2010 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 struct private_sa_payload_t {
32
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 * Reserved bits
50 */
51 bool reserved[7];
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 /**
65 * Encoding rules to parse or generate a IKEv2-SA Payload
66 *
67 * The defined offsets are the positions in a object of type
68 * private_sa_payload_t.
69 */
70 encoding_rule_t sa_payload_encodings[] = {
71 /* 1 Byte next payload type, stored in the field next_payload */
72 { U_INT_8, offsetof(private_sa_payload_t, next_payload) },
73 /* the critical bit */
74 { FLAG, offsetof(private_sa_payload_t, critical) },
75 /* 7 Bit reserved bits, nowhere stored */
76 { RESERVED_BIT, offsetof(private_sa_payload_t, reserved[0]) },
77 { RESERVED_BIT, offsetof(private_sa_payload_t, reserved[1]) },
78 { RESERVED_BIT, offsetof(private_sa_payload_t, reserved[2]) },
79 { RESERVED_BIT, offsetof(private_sa_payload_t, reserved[3]) },
80 { RESERVED_BIT, offsetof(private_sa_payload_t, reserved[4]) },
81 { RESERVED_BIT, offsetof(private_sa_payload_t, reserved[5]) },
82 { RESERVED_BIT, offsetof(private_sa_payload_t, reserved[6]) },
83 /* Length of the whole SA payload*/
84 { PAYLOAD_LENGTH, offsetof(private_sa_payload_t, payload_length) },
85 /* Proposals are stored in a proposal substructure,
86 offset points to a linked_list_t pointer */
87 { PROPOSALS, offsetof(private_sa_payload_t, proposals) },
88 };
89
90 /*
91 1 2 3
92 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
93 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
94 ! Next Payload !C! RESERVED ! Payload Length !
95 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
96 ! !
97 ~ <Proposals> ~
98 ! !
99 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
100 */
101
102 METHOD(payload_t, verify, status_t,
103 private_sa_payload_t *this)
104 {
105 int expected_number = 1, current_number;
106 status_t status = SUCCESS;
107 enumerator_t *enumerator;
108 proposal_substructure_t *substruct;
109
110 /* check proposal numbering */
111 enumerator = this->proposals->create_enumerator(this->proposals);
112 while (enumerator->enumerate(enumerator, (void**)&substruct))
113 {
114 current_number = substruct->get_proposal_number(substruct);
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 DBG1(DBG_ENC, "proposal number smaller than previous");
128 status = FAILED;
129 break;
130 }
131
132 status = substruct->payload_interface.verify(&substruct->payload_interface);
133 if (status != SUCCESS)
134 {
135 DBG1(DBG_ENC, "PROPOSAL_SUBSTRUCTURE verification failed");
136 break;
137 }
138 expected_number = current_number;
139 }
140 enumerator->destroy(enumerator);
141 return status;
142 }
143
144 METHOD(payload_t, get_encoding_rules, void,
145 private_sa_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
146 {
147 *rules = sa_payload_encodings;
148 *rule_count = countof(sa_payload_encodings);
149 }
150
151 METHOD(payload_t, get_type, payload_type_t,
152 private_sa_payload_t *this)
153 {
154 return SECURITY_ASSOCIATION;
155 }
156
157 METHOD(payload_t, get_next_type, payload_type_t,
158 private_sa_payload_t *this)
159 {
160 return this->next_payload;
161 }
162
163 METHOD(payload_t, set_next_type, void,
164 private_sa_payload_t *this,payload_type_t type)
165 {
166 this->next_payload = type;
167 }
168
169 /**
170 * recompute length of the payload.
171 */
172 static void compute_length(private_sa_payload_t *this)
173 {
174 enumerator_t *enumerator;
175 payload_t *current;
176 size_t length = SA_PAYLOAD_HEADER_LENGTH;
177
178 enumerator = this->proposals->create_enumerator(this->proposals);
179 while (enumerator->enumerate(enumerator, (void **)&current))
180 {
181 length += current->get_length(current);
182 }
183 enumerator->destroy(enumerator);
184
185 this->payload_length = length;
186 }
187
188 METHOD(payload_t, get_length, size_t,
189 private_sa_payload_t *this)
190 {
191 return this->payload_length;
192 }
193
194 METHOD(sa_payload_t, add_proposal, void,
195 private_sa_payload_t *this, proposal_t *proposal)
196 {
197 proposal_substructure_t *substruct, *last;
198 u_int count;
199
200 count = this->proposals->get_count(this->proposals);
201 substruct = proposal_substructure_create_from_proposal(proposal);
202 if (count > 0)
203 {
204 this->proposals->get_last(this->proposals, (void**)&last);
205 /* last transform is now not anymore last one */
206 last->set_is_last_proposal(last, FALSE);
207 }
208 substruct->set_is_last_proposal(substruct, TRUE);
209 if (proposal->get_number(proposal))
210 { /* use the selected proposals number, if any */
211 substruct->set_proposal_number(substruct, proposal->get_number(proposal));
212 }
213 else
214 {
215 substruct->set_proposal_number(substruct, count + 1);
216 }
217 this->proposals->insert_last(this->proposals, substruct);
218 compute_length(this);
219 }
220
221 METHOD(sa_payload_t, get_proposals, linked_list_t*,
222 private_sa_payload_t *this)
223 {
224 int struct_number = 0;
225 int ignore_struct_number = 0;
226 enumerator_t *enumerator;
227 proposal_substructure_t *substruct;
228 linked_list_t *list;
229 proposal_t *proposal;
230
231 list = linked_list_create();
232 /* we do not support proposals split up to two proposal substructures, as
233 * AH+ESP bundles are not supported in RFC4301 anymore.
234 * To handle such structures safely, we just skip proposals with multiple
235 * protocols.
236 */
237 enumerator = this->proposals->create_enumerator(this->proposals);
238 while (enumerator->enumerate(enumerator, &substruct))
239 {
240 /* check if a proposal has a single protocol */
241 if (substruct->get_proposal_number(substruct) == struct_number)
242 {
243 if (ignore_struct_number < struct_number)
244 {
245 /* remove an already added, if first of series */
246 list->remove_last(list, (void**)&proposal);
247 proposal->destroy(proposal);
248 ignore_struct_number = struct_number;
249 }
250 continue;
251 }
252 struct_number++;
253 proposal = substruct->get_proposal(substruct);
254 if (proposal)
255 {
256 list->insert_last(list, proposal);
257 }
258 }
259 enumerator->destroy(enumerator);
260 return list;
261 }
262
263 METHOD(sa_payload_t, create_substructure_enumerator, enumerator_t*,
264 private_sa_payload_t *this)
265 {
266 return this->proposals->create_enumerator(this->proposals);
267 }
268
269 METHOD2(payload_t, sa_payload_t, destroy, void,
270 private_sa_payload_t *this)
271 {
272 this->proposals->destroy_offset(this->proposals,
273 offsetof(proposal_substructure_t, destroy));
274 free(this);
275 }
276
277 /*
278 * Described in header.
279 */
280 sa_payload_t *sa_payload_create()
281 {
282 private_sa_payload_t *this;
283
284 INIT(this,
285 .public = {
286 .payload_interface = {
287 .verify = _verify,
288 .get_encoding_rules = _get_encoding_rules,
289 .get_length = _get_length,
290 .get_next_type = _get_next_type,
291 .set_next_type = _set_next_type,
292 .get_type = _get_type,
293 .destroy = _destroy,
294 },
295 .add_proposal = _add_proposal,
296 .get_proposals = _get_proposals,
297 .create_substructure_enumerator = _create_substructure_enumerator,
298 .destroy = _destroy,
299 },
300 .next_payload = NO_PAYLOAD,
301 .payload_length = SA_PAYLOAD_HEADER_LENGTH,
302 .proposals = linked_list_create(),
303 );
304 return &this->public;
305 }
306
307 /*
308 * Described in header.
309 */
310 sa_payload_t *sa_payload_create_from_proposal_list(linked_list_t *proposals)
311 {
312 private_sa_payload_t *this;
313 enumerator_t *enumerator;
314 proposal_t *proposal;
315
316 this = (private_sa_payload_t*)sa_payload_create();
317 enumerator = proposals->create_enumerator(proposals);
318 while (enumerator->enumerate(enumerator, &proposal))
319 {
320 add_proposal(this, proposal);
321 }
322 enumerator->destroy(enumerator);
323
324 return &this->public;
325 }
326
327 /*
328 * Described in header.
329 */
330 sa_payload_t *sa_payload_create_from_proposal(proposal_t *proposal)
331 {
332 private_sa_payload_t *this;
333
334 this = (private_sa_payload_t*)sa_payload_create();
335 add_proposal(this, proposal);
336
337 return &this->public;
338 }