Fixed proposal numbering check in sa_payload
[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 DBG1(DBG_ENC, "proposal number smaller than previous");
118 status = FAILED;
119 break;
120 }
121
122 status = substruct->payload_interface.verify(&substruct->payload_interface);
123 if (status != SUCCESS)
124 {
125 DBG1(DBG_ENC, "PROPOSAL_SUBSTRUCTURE verification failed");
126 break;
127 }
128 expected_number = current_number;
129 }
130 enumerator->destroy(enumerator);
131 return status;
132 }
133
134 METHOD(payload_t, get_encoding_rules, void,
135 private_sa_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
136 {
137 *rules = sa_payload_encodings;
138 *rule_count = countof(sa_payload_encodings);
139 }
140
141 METHOD(payload_t, get_type, payload_type_t,
142 private_sa_payload_t *this)
143 {
144 return SECURITY_ASSOCIATION;
145 }
146
147 METHOD(payload_t, get_next_type, payload_type_t,
148 private_sa_payload_t *this)
149 {
150 return this->next_payload;
151 }
152
153 METHOD(payload_t, set_next_type, void,
154 private_sa_payload_t *this,payload_type_t type)
155 {
156 this->next_payload = type;
157 }
158
159 /**
160 * recompute length of the payload.
161 */
162 static void compute_length(private_sa_payload_t *this)
163 {
164 enumerator_t *enumerator;
165 payload_t *current;
166 size_t length = SA_PAYLOAD_HEADER_LENGTH;
167
168 enumerator = this->proposals->create_enumerator(this->proposals);
169 while (enumerator->enumerate(enumerator, (void **)&current))
170 {
171 length += current->get_length(current);
172 }
173 enumerator->destroy(enumerator);
174
175 this->payload_length = length;
176 }
177
178 METHOD(payload_t, get_length, size_t,
179 private_sa_payload_t *this)
180 {
181 return this->payload_length;
182 }
183
184 METHOD(sa_payload_t, add_proposal, void,
185 private_sa_payload_t *this, proposal_t *proposal)
186 {
187 proposal_substructure_t *substruct, *last;
188 u_int count;
189
190 count = this->proposals->get_count(this->proposals);
191 substruct = proposal_substructure_create_from_proposal(proposal);
192 if (count > 0)
193 {
194 this->proposals->get_last(this->proposals, (void**)&last);
195 /* last transform is now not anymore last one */
196 last->set_is_last_proposal(last, FALSE);
197 }
198 substruct->set_is_last_proposal(substruct, TRUE);
199 if (proposal->get_number(proposal))
200 { /* use the selected proposals number, if any */
201 substruct->set_proposal_number(substruct, proposal->get_number(proposal));
202 }
203 else
204 {
205 substruct->set_proposal_number(substruct, count + 1);
206 }
207 this->proposals->insert_last(this->proposals, substruct);
208 compute_length(this);
209 }
210
211 METHOD(sa_payload_t, get_proposals, linked_list_t*,
212 private_sa_payload_t *this)
213 {
214 int struct_number = 0;
215 int ignore_struct_number = 0;
216 enumerator_t *enumerator;
217 proposal_substructure_t *substruct;
218 linked_list_t *list;
219 proposal_t *proposal;
220
221 list = linked_list_create();
222 /* we do not support proposals split up to two proposal substructures, as
223 * AH+ESP bundles are not supported in RFC4301 anymore.
224 * To handle such structures safely, we just skip proposals with multiple
225 * protocols.
226 */
227 enumerator = this->proposals->create_enumerator(this->proposals);
228 while (enumerator->enumerate(enumerator, &substruct))
229 {
230 /* check if a proposal has a single protocol */
231 if (substruct->get_proposal_number(substruct) == struct_number)
232 {
233 if (ignore_struct_number < struct_number)
234 {
235 /* remove an already added, if first of series */
236 list->remove_last(list, (void**)&proposal);
237 proposal->destroy(proposal);
238 ignore_struct_number = struct_number;
239 }
240 continue;
241 }
242 struct_number++;
243 proposal = substruct->get_proposal(substruct);
244 if (proposal)
245 {
246 list->insert_last(list, proposal);
247 }
248 }
249 enumerator->destroy(enumerator);
250 return list;
251 }
252
253 METHOD(sa_payload_t, create_substructure_enumerator, enumerator_t*,
254 private_sa_payload_t *this)
255 {
256 return this->proposals->create_enumerator(this->proposals);
257 }
258
259 METHOD2(payload_t, sa_payload_t, destroy, void,
260 private_sa_payload_t *this)
261 {
262 this->proposals->destroy_offset(this->proposals,
263 offsetof(proposal_substructure_t, destroy));
264 free(this);
265 }
266
267 /*
268 * Described in header.
269 */
270 sa_payload_t *sa_payload_create()
271 {
272 private_sa_payload_t *this;
273
274 INIT(this,
275 .public = {
276 .payload_interface = {
277 .verify = _verify,
278 .get_encoding_rules = _get_encoding_rules,
279 .get_length = _get_length,
280 .get_next_type = _get_next_type,
281 .set_next_type = _set_next_type,
282 .get_type = _get_type,
283 .destroy = _destroy,
284 },
285 .add_proposal = _add_proposal,
286 .get_proposals = _get_proposals,
287 .create_substructure_enumerator = _create_substructure_enumerator,
288 .destroy = _destroy,
289 },
290 .next_payload = NO_PAYLOAD,
291 .payload_length = SA_PAYLOAD_HEADER_LENGTH,
292 .proposals = linked_list_create(),
293 );
294 return &this->public;
295 }
296
297 /*
298 * Described in header.
299 */
300 sa_payload_t *sa_payload_create_from_proposal_list(linked_list_t *proposals)
301 {
302 private_sa_payload_t *this;
303 enumerator_t *enumerator;
304 proposal_t *proposal;
305
306 this = (private_sa_payload_t*)sa_payload_create();
307 enumerator = proposals->create_enumerator(proposals);
308 while (enumerator->enumerate(enumerator, &proposal))
309 {
310 add_proposal(this, proposal);
311 }
312 enumerator->destroy(enumerator);
313
314 return &this->public;
315 }
316
317 /*
318 * Described in header.
319 */
320 sa_payload_t *sa_payload_create_from_proposal(proposal_t *proposal)
321 {
322 private_sa_payload_t *this;
323
324 this = (private_sa_payload_t*)sa_payload_create();
325 add_proposal(this, proposal);
326
327 return &this->public;
328 }