Do not update payload length during generation, allows hooks override payload length
[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 * Length of this payload.
50 */
51 u_int16_t payload_length;
52
53 /**
54 * Proposals in this payload are stored in a linked_list_t.
55 */
56 linked_list_t * proposals;
57 };
58
59 /**
60 * Encoding rules to parse or generate a IKEv2-SA Payload
61 *
62 * The defined offsets are the positions in a object of type
63 * private_sa_payload_t.
64 */
65 encoding_rule_t sa_payload_encodings[] = {
66 /* 1 Byte next payload type, stored in the field next_payload */
67 { U_INT_8, offsetof(private_sa_payload_t, next_payload) },
68 /* the critical bit */
69 { FLAG, offsetof(private_sa_payload_t, critical) },
70 /* 7 Bit reserved bits, nowhere stored */
71 { RESERVED_BIT, 0 },
72 { RESERVED_BIT, 0 },
73 { RESERVED_BIT, 0 },
74 { RESERVED_BIT, 0 },
75 { RESERVED_BIT, 0 },
76 { RESERVED_BIT, 0 },
77 { RESERVED_BIT, 0 },
78 /* Length of the whole SA payload*/
79 { PAYLOAD_LENGTH, offsetof(private_sa_payload_t, payload_length) },
80 /* Proposals are stored in a proposal substructure,
81 offset points to a linked_list_t pointer */
82 { PROPOSALS, offsetof(private_sa_payload_t, proposals) },
83 };
84
85 /*
86 1 2 3
87 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
88 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
89 ! Next Payload !C! RESERVED ! Payload Length !
90 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
91 ! !
92 ~ <Proposals> ~
93 ! !
94 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
95 */
96
97 METHOD(payload_t, verify, status_t,
98 private_sa_payload_t *this)
99 {
100 int expected_number = 1, current_number;
101 status_t status = SUCCESS;
102 enumerator_t *enumerator;
103 proposal_substructure_t *substruct;
104 bool first = TRUE;
105
106 /* check proposal numbering */
107 enumerator = this->proposals->create_enumerator(this->proposals);
108 while (enumerator->enumerate(enumerator, (void**)&substruct))
109 {
110 current_number = substruct->get_proposal_number(substruct);
111 if (current_number < expected_number)
112 {
113 if (current_number != expected_number + 1)
114 {
115 DBG1(DBG_ENC, "proposal number is %d, expected %d or %d",
116 current_number, expected_number, expected_number + 1);
117 status = FAILED;
118 break;
119 }
120 }
121 else if (current_number < expected_number)
122 {
123 DBG1(DBG_ENC, "proposal number smaller than previous");
124 status = FAILED;
125 break;
126 }
127
128 status = substruct->payload_interface.verify(&substruct->payload_interface);
129 if (status != SUCCESS)
130 {
131 DBG1(DBG_ENC, "PROPOSAL_SUBSTRUCTURE verification failed");
132 break;
133 }
134 first = FALSE;
135 expected_number = current_number;
136 }
137 enumerator->destroy(enumerator);
138 return status;
139 }
140
141 METHOD(payload_t, get_encoding_rules, void,
142 private_sa_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
143 {
144 *rules = sa_payload_encodings;
145 *rule_count = countof(sa_payload_encodings);
146 }
147
148 METHOD(payload_t, get_type, payload_type_t,
149 private_sa_payload_t *this)
150 {
151 return SECURITY_ASSOCIATION;
152 }
153
154 METHOD(payload_t, get_next_type, payload_type_t,
155 private_sa_payload_t *this)
156 {
157 return this->next_payload;
158 }
159
160 METHOD(payload_t, set_next_type, void,
161 private_sa_payload_t *this,payload_type_t type)
162 {
163 this->next_payload = type;
164 }
165
166 /**
167 * recompute length of the payload.
168 */
169 static void compute_length(private_sa_payload_t *this)
170 {
171 enumerator_t *enumerator;
172 payload_t *current;
173 size_t length = SA_PAYLOAD_HEADER_LENGTH;
174
175 enumerator = this->proposals->create_enumerator(this->proposals);
176 while (enumerator->enumerate(enumerator, (void **)&current))
177 {
178 length += current->get_length(current);
179 }
180 enumerator->destroy(enumerator);
181
182 this->payload_length = length;
183 }
184
185 METHOD(payload_t, get_length, size_t,
186 private_sa_payload_t *this)
187 {
188 return this->payload_length;
189 }
190
191 METHOD(sa_payload_t, add_proposal, void,
192 private_sa_payload_t *this, proposal_t *proposal)
193 {
194 proposal_substructure_t *substruct, *last;
195 u_int count;
196
197 count = this->proposals->get_count(this->proposals);
198 substruct = proposal_substructure_create_from_proposal(proposal);
199 if (count > 0)
200 {
201 this->proposals->get_last(this->proposals, (void**)&last);
202 /* last transform is now not anymore last one */
203 last->set_is_last_proposal(last, FALSE);
204 }
205 substruct->set_is_last_proposal(substruct, TRUE);
206 if (proposal->get_number(proposal))
207 { /* use the selected proposals number, if any */
208 substruct->set_proposal_number(substruct, proposal->get_number(proposal));
209 }
210 else
211 {
212 substruct->set_proposal_number(substruct, count + 1);
213 }
214 this->proposals->insert_last(this->proposals, substruct);
215 compute_length(this);
216 }
217
218 METHOD(sa_payload_t, get_proposals, linked_list_t*,
219 private_sa_payload_t *this)
220 {
221 int struct_number = 0;
222 int ignore_struct_number = 0;
223 enumerator_t *enumerator;
224 proposal_substructure_t *substruct;
225 linked_list_t *list;
226 proposal_t *proposal;
227
228 list = linked_list_create();
229 /* we do not support proposals split up to two proposal substructures, as
230 * AH+ESP bundles are not supported in RFC4301 anymore.
231 * To handle such structures safely, we just skip proposals with multiple
232 * protocols.
233 */
234 enumerator = this->proposals->create_enumerator(this->proposals);
235 while (enumerator->enumerate(enumerator, &substruct))
236 {
237 /* check if a proposal has a single protocol */
238 if (substruct->get_proposal_number(substruct) == struct_number)
239 {
240 if (ignore_struct_number < struct_number)
241 {
242 /* remove an already added, if first of series */
243 list->remove_last(list, (void**)&proposal);
244 proposal->destroy(proposal);
245 ignore_struct_number = struct_number;
246 }
247 continue;
248 }
249 struct_number++;
250 proposal = substruct->get_proposal(substruct);
251 if (proposal)
252 {
253 list->insert_last(list, proposal);
254 }
255 }
256 enumerator->destroy(enumerator);
257 return list;
258 }
259
260 METHOD2(payload_t, sa_payload_t, destroy, void,
261 private_sa_payload_t *this)
262 {
263 this->proposals->destroy_offset(this->proposals,
264 offsetof(proposal_substructure_t, destroy));
265 free(this);
266 }
267
268 /*
269 * Described in header.
270 */
271 sa_payload_t *sa_payload_create()
272 {
273 private_sa_payload_t *this;
274
275 INIT(this,
276 .public = {
277 .payload_interface = {
278 .verify = _verify,
279 .get_encoding_rules = _get_encoding_rules,
280 .get_length = _get_length,
281 .get_next_type = _get_next_type,
282 .set_next_type = _set_next_type,
283 .get_type = _get_type,
284 .destroy = _destroy,
285 },
286 .add_proposal = _add_proposal,
287 .get_proposals = _get_proposals,
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 }