Use a generic list encoding rule we can use to specify the wrapped payload type
[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 /* IKEv1 situation */
26 #define SIT_IDENTITY_ONLY 1
27
28 typedef struct private_sa_payload_t private_sa_payload_t;
29
30 /**
31 * Private data of an sa_payload_t object.
32 */
33 struct private_sa_payload_t {
34
35 /**
36 * Public sa_payload_t interface.
37 */
38 sa_payload_t public;
39
40 /**
41 * Next payload type.
42 */
43 u_int8_t next_payload;
44
45 /**
46 * Critical flag.
47 */
48 bool critical;
49
50 /**
51 * Reserved bits
52 */
53 bool reserved[8];
54
55 /**
56 * Length of this payload.
57 */
58 u_int16_t payload_length;
59
60 /**
61 * Proposals in this payload are stored in a linked_list_t.
62 */
63 linked_list_t *proposals;
64
65 /**
66 * Type of this payload, V1 or V2
67 */
68 payload_type_t type;
69
70 /**
71 * IKEv1 DOI
72 */
73 u_int32_t doi;
74
75 /**
76 * IKEv1 situation
77 */
78 u_int32_t situation;
79 };
80
81 /**
82 * Encoding rules for IKEv1 SA payload
83 */
84 static encoding_rule_t encodings_v1[] = {
85 /* 1 Byte next payload type, stored in the field next_payload */
86 { U_INT_8, offsetof(private_sa_payload_t, next_payload) },
87 /* 8 reserved bits */
88 { RESERVED_BIT, offsetof(private_sa_payload_t, reserved[0]) },
89 { RESERVED_BIT, offsetof(private_sa_payload_t, reserved[1]) },
90 { RESERVED_BIT, offsetof(private_sa_payload_t, reserved[2]) },
91 { RESERVED_BIT, offsetof(private_sa_payload_t, reserved[3]) },
92 { RESERVED_BIT, offsetof(private_sa_payload_t, reserved[4]) },
93 { RESERVED_BIT, offsetof(private_sa_payload_t, reserved[5]) },
94 { RESERVED_BIT, offsetof(private_sa_payload_t, reserved[6]) },
95 { RESERVED_BIT, offsetof(private_sa_payload_t, reserved[7]) },
96 /* Length of the whole SA payload*/
97 { PAYLOAD_LENGTH, offsetof(private_sa_payload_t, payload_length) },
98 /* DOI*/
99 { U_INT_32, offsetof(private_sa_payload_t, doi) },
100 /* Situation*/
101 { U_INT_32, offsetof(private_sa_payload_t, situation) },
102 /* Proposals are stored in a proposal substructure list */
103 { PAYLOAD_LIST + PROPOSAL_SUBSTRUCTURE_V1,
104 offsetof(private_sa_payload_t, proposals) },
105 };
106
107 /*
108 1 2 3
109 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
110 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
111 ! Next Payload ! RESERVED ! Payload Length !
112 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
113 ! DOI !
114 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
115 ! Situation !
116 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
117 ! !
118 ~ <Proposals> ~
119 ! !
120 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
121 */
122
123 /**
124 * Encoding rules for IKEv2 SA payload
125 */
126 static encoding_rule_t encodings_v2[] = {
127 /* 1 Byte next payload type, stored in the field next_payload */
128 { U_INT_8, offsetof(private_sa_payload_t, next_payload) },
129 /* the critical bit */
130 { FLAG, offsetof(private_sa_payload_t, critical) },
131 /* 7 Bit reserved bits */
132 { RESERVED_BIT, offsetof(private_sa_payload_t, reserved[0]) },
133 { RESERVED_BIT, offsetof(private_sa_payload_t, reserved[1]) },
134 { RESERVED_BIT, offsetof(private_sa_payload_t, reserved[2]) },
135 { RESERVED_BIT, offsetof(private_sa_payload_t, reserved[3]) },
136 { RESERVED_BIT, offsetof(private_sa_payload_t, reserved[4]) },
137 { RESERVED_BIT, offsetof(private_sa_payload_t, reserved[5]) },
138 { RESERVED_BIT, offsetof(private_sa_payload_t, reserved[6]) },
139 /* Length of the whole SA payload*/
140 { PAYLOAD_LENGTH, offsetof(private_sa_payload_t, payload_length) },
141 /* Proposals are stored in a proposal substructure list */
142 { PAYLOAD_LIST + PROPOSAL_SUBSTRUCTURE,
143 offsetof(private_sa_payload_t, proposals) },
144 };
145
146 /*
147 1 2 3
148 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
149 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
150 ! Next Payload !C! RESERVED ! Payload Length !
151 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
152 ! !
153 ~ <Proposals> ~
154 ! !
155 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
156 */
157
158 METHOD(payload_t, verify, status_t,
159 private_sa_payload_t *this)
160 {
161 int expected_number = 0, current_number;
162 status_t status = SUCCESS;
163 enumerator_t *enumerator;
164 proposal_substructure_t *substruct;
165
166 if (this->type == SECURITY_ASSOCIATION)
167 {
168 expected_number = 1;
169 }
170
171 /* check proposal numbering */
172 enumerator = this->proposals->create_enumerator(this->proposals);
173 while (enumerator->enumerate(enumerator, (void**)&substruct))
174 {
175 current_number = substruct->get_proposal_number(substruct);
176 if (current_number < expected_number)
177 {
178 if (current_number != expected_number + 1)
179 {
180 DBG1(DBG_ENC, "proposal number is %d, expected %d or %d",
181 current_number, expected_number, expected_number + 1);
182 status = FAILED;
183 break;
184 }
185 }
186 else if (current_number < expected_number)
187 {
188 DBG1(DBG_ENC, "proposal number smaller than previous");
189 status = FAILED;
190 break;
191 }
192
193 status = substruct->payload_interface.verify(&substruct->payload_interface);
194 if (status != SUCCESS)
195 {
196 DBG1(DBG_ENC, "PROPOSAL_SUBSTRUCTURE verification failed");
197 break;
198 }
199 expected_number = current_number;
200 }
201 enumerator->destroy(enumerator);
202 return status;
203 }
204
205 METHOD(payload_t, get_encoding_rules, int,
206 private_sa_payload_t *this, encoding_rule_t **rules)
207 {
208 if (this->type == SECURITY_ASSOCIATION_V1)
209 {
210 *rules = encodings_v1;
211 return countof(encodings_v1);
212 }
213 *rules = encodings_v2;
214 return countof(encodings_v2);
215 }
216
217 METHOD(payload_t, get_header_length, int,
218 private_sa_payload_t *this)
219 {
220 if (this->type == SECURITY_ASSOCIATION_V1)
221 {
222 return 12;
223 }
224 return 4;
225 }
226
227 METHOD(payload_t, get_type, payload_type_t,
228 private_sa_payload_t *this)
229 {
230 return this->type;
231 }
232
233 METHOD(payload_t, get_next_type, payload_type_t,
234 private_sa_payload_t *this)
235 {
236 return this->next_payload;
237 }
238
239 METHOD(payload_t, set_next_type, void,
240 private_sa_payload_t *this,payload_type_t type)
241 {
242 this->next_payload = type;
243 }
244
245 /**
246 * recompute length of the payload.
247 */
248 static void compute_length(private_sa_payload_t *this)
249 {
250 enumerator_t *enumerator;
251 payload_t *current;
252
253 this->payload_length = get_header_length(this);
254
255 enumerator = this->proposals->create_enumerator(this->proposals);
256 while (enumerator->enumerate(enumerator, (void **)&current))
257 {
258 this->payload_length += current->get_length(current);
259 }
260 enumerator->destroy(enumerator);
261 }
262
263 METHOD(payload_t, get_length, size_t,
264 private_sa_payload_t *this)
265 {
266 return this->payload_length;
267 }
268
269 METHOD(sa_payload_t, add_proposal, void,
270 private_sa_payload_t *this, proposal_t *proposal)
271 {
272 proposal_substructure_t *substruct, *last;
273 payload_type_t subtype = PROPOSAL_SUBSTRUCTURE;
274 u_int count;
275
276 if (this->type == SECURITY_ASSOCIATION_V1)
277 {
278 subtype = PROPOSAL_SUBSTRUCTURE_V1;
279 }
280 substruct = proposal_substructure_create_from_proposal(subtype, proposal);
281 count = this->proposals->get_count(this->proposals);
282 if (count > 0)
283 {
284 this->proposals->get_last(this->proposals, (void**)&last);
285 /* last transform is now not anymore last one */
286 last->set_is_last_proposal(last, FALSE);
287 }
288 substruct->set_is_last_proposal(substruct, TRUE);
289 if (proposal->get_number(proposal))
290 { /* use the selected proposals number, if any */
291 substruct->set_proposal_number(substruct, proposal->get_number(proposal));
292 }
293 else
294 {
295 substruct->set_proposal_number(substruct, count + 1);
296 }
297 this->proposals->insert_last(this->proposals, substruct);
298 compute_length(this);
299 }
300
301 METHOD(sa_payload_t, get_proposals, linked_list_t*,
302 private_sa_payload_t *this)
303 {
304 int struct_number = 0;
305 int ignore_struct_number = 0;
306 enumerator_t *enumerator;
307 proposal_substructure_t *substruct;
308 linked_list_t *list;
309 proposal_t *proposal;
310
311 if (this->type == SECURITY_ASSOCIATION_V1)
312 { /* IKEv1 proposals start with 0 */
313 struct_number = ignore_struct_number = -1;
314 }
315
316 list = linked_list_create();
317 /* we do not support proposals split up to two proposal substructures, as
318 * AH+ESP bundles are not supported in RFC4301 anymore.
319 * To handle such structures safely, we just skip proposals with multiple
320 * protocols.
321 */
322 enumerator = this->proposals->create_enumerator(this->proposals);
323 while (enumerator->enumerate(enumerator, &substruct))
324 {
325 /* check if a proposal has a single protocol */
326 if (substruct->get_proposal_number(substruct) == struct_number)
327 {
328 if (ignore_struct_number < struct_number)
329 {
330 /* remove an already added, if first of series */
331 list->remove_last(list, (void**)&proposal);
332 proposal->destroy(proposal);
333 ignore_struct_number = struct_number;
334 }
335 continue;
336 }
337 struct_number++;
338 proposal = substruct->get_proposal(substruct);
339 if (proposal)
340 {
341 list->insert_last(list, proposal);
342 }
343 }
344 enumerator->destroy(enumerator);
345 return list;
346 }
347
348 METHOD(sa_payload_t, create_substructure_enumerator, enumerator_t*,
349 private_sa_payload_t *this)
350 {
351 return this->proposals->create_enumerator(this->proposals);
352 }
353
354 METHOD2(payload_t, sa_payload_t, destroy, void,
355 private_sa_payload_t *this)
356 {
357 this->proposals->destroy_offset(this->proposals,
358 offsetof(payload_t, destroy));
359 free(this);
360 }
361
362 /*
363 * Described in header.
364 */
365 sa_payload_t *sa_payload_create(payload_type_t type)
366 {
367 private_sa_payload_t *this;
368
369 INIT(this,
370 .public = {
371 .payload_interface = {
372 .verify = _verify,
373 .get_encoding_rules = _get_encoding_rules,
374 .get_header_length = _get_header_length,
375 .get_length = _get_length,
376 .get_next_type = _get_next_type,
377 .set_next_type = _set_next_type,
378 .get_type = _get_type,
379 .destroy = _destroy,
380 },
381 .add_proposal = _add_proposal,
382 .get_proposals = _get_proposals,
383 .create_substructure_enumerator = _create_substructure_enumerator,
384 .destroy = _destroy,
385 },
386 .next_payload = NO_PAYLOAD,
387 .proposals = linked_list_create(),
388 .type = type,
389 /* for IKEv1 only */
390 .doi = IKEV1_DOI_IPSEC,
391 .situation = SIT_IDENTITY_ONLY,
392 );
393
394 compute_length(this);
395
396 return &this->public;
397 }
398
399 /*
400 * Described in header.
401 */
402 sa_payload_t *sa_payload_create_from_proposal_list(payload_type_t type,
403 linked_list_t *proposals)
404 {
405 sa_payload_t *this;
406 enumerator_t *enumerator;
407 proposal_t *proposal;
408
409 this = sa_payload_create(type);
410 enumerator = proposals->create_enumerator(proposals);
411 while (enumerator->enumerate(enumerator, &proposal))
412 {
413 this->add_proposal(this, proposal);
414 }
415 enumerator->destroy(enumerator);
416
417 return this;
418 }
419
420 /*
421 * Described in header.
422 */
423 sa_payload_t *sa_payload_create_from_proposal(payload_type_t type,
424 proposal_t *proposal)
425 {
426 sa_payload_t *this;
427
428 this = sa_payload_create(type);
429 this->add_proposal(this, proposal);
430
431 return this;
432 }