af301262307b16c997fff083bb7d9886e7a92dc1
[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,
103 offset points to a linked_list_t pointer */
104 { PROPOSALS_V1, 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,
142 offset points to a linked_list_t pointer */
143 { PROPOSALS, 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 = 1, current_number;
162 status_t status = SUCCESS;
163 enumerator_t *enumerator;
164 proposal_substructure_t *substruct;
165
166 /* check proposal numbering */
167 enumerator = this->proposals->create_enumerator(this->proposals);
168 while (enumerator->enumerate(enumerator, (void**)&substruct))
169 {
170 current_number = substruct->get_proposal_number(substruct);
171 if (current_number < expected_number)
172 {
173 if (current_number != expected_number + 1)
174 {
175 DBG1(DBG_ENC, "proposal number is %d, expected %d or %d",
176 current_number, expected_number, expected_number + 1);
177 status = FAILED;
178 break;
179 }
180 }
181 else if (current_number < expected_number)
182 {
183 DBG1(DBG_ENC, "proposal number smaller than previous");
184 status = FAILED;
185 break;
186 }
187
188 status = substruct->payload_interface.verify(&substruct->payload_interface);
189 if (status != SUCCESS)
190 {
191 DBG1(DBG_ENC, "PROPOSAL_SUBSTRUCTURE verification failed");
192 break;
193 }
194 expected_number = current_number;
195 }
196 enumerator->destroy(enumerator);
197 return status;
198 }
199
200 METHOD(payload_t, get_encoding_rules, void,
201 private_sa_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
202 {
203 if (this->type == SECURITY_ASSOCIATION_V1)
204 {
205 *rules = encodings_v1;
206 *rule_count = countof(encodings_v1);
207 }
208 else
209 {
210 *rules = encodings_v2;
211 *rule_count = countof(encodings_v2);
212 }
213 }
214
215 METHOD(payload_t, get_type, payload_type_t,
216 private_sa_payload_t *this)
217 {
218 return this->type;
219 }
220
221 METHOD(payload_t, get_next_type, payload_type_t,
222 private_sa_payload_t *this)
223 {
224 return this->next_payload;
225 }
226
227 METHOD(payload_t, set_next_type, void,
228 private_sa_payload_t *this,payload_type_t type)
229 {
230 this->next_payload = type;
231 }
232
233 /**
234 * recompute length of the payload.
235 */
236 static void compute_length(private_sa_payload_t *this)
237 {
238 enumerator_t *enumerator;
239 payload_t *current;
240 size_t length = SA_PAYLOAD_HEADER_LENGTH;
241
242 if (this->type == SECURITY_ASSOCIATION_V1)
243 {
244 length = SA_PAYLOAD_V1_HEADER_LENGTH;
245 }
246
247 enumerator = this->proposals->create_enumerator(this->proposals);
248 while (enumerator->enumerate(enumerator, (void **)&current))
249 {
250 length += current->get_length(current);
251 }
252 enumerator->destroy(enumerator);
253
254 this->payload_length = length;
255 }
256
257 METHOD(payload_t, get_length, size_t,
258 private_sa_payload_t *this)
259 {
260 return this->payload_length;
261 }
262
263 METHOD(sa_payload_t, add_proposal, void,
264 private_sa_payload_t *this, proposal_t *proposal)
265 {
266 proposal_substructure_t *substruct, *last;
267 u_int count;
268
269 count = this->proposals->get_count(this->proposals);
270 substruct = proposal_substructure_create_from_proposal(proposal);
271 if (count > 0)
272 {
273 this->proposals->get_last(this->proposals, (void**)&last);
274 /* last transform is now not anymore last one */
275 last->set_is_last_proposal(last, FALSE);
276 }
277 substruct->set_is_last_proposal(substruct, TRUE);
278 if (proposal->get_number(proposal))
279 { /* use the selected proposals number, if any */
280 substruct->set_proposal_number(substruct, proposal->get_number(proposal));
281 }
282 else
283 {
284 substruct->set_proposal_number(substruct, count + 1);
285 }
286 this->proposals->insert_last(this->proposals, substruct);
287 compute_length(this);
288 }
289
290 METHOD(sa_payload_t, get_proposals, linked_list_t*,
291 private_sa_payload_t *this)
292 {
293 int struct_number = 0;
294 int ignore_struct_number = 0;
295 enumerator_t *enumerator;
296 proposal_substructure_t *substruct;
297 linked_list_t *list;
298 proposal_t *proposal;
299
300 list = linked_list_create();
301 /* we do not support proposals split up to two proposal substructures, as
302 * AH+ESP bundles are not supported in RFC4301 anymore.
303 * To handle such structures safely, we just skip proposals with multiple
304 * protocols.
305 */
306 enumerator = this->proposals->create_enumerator(this->proposals);
307 while (enumerator->enumerate(enumerator, &substruct))
308 {
309 /* check if a proposal has a single protocol */
310 if (substruct->get_proposal_number(substruct) == struct_number)
311 {
312 if (ignore_struct_number < struct_number)
313 {
314 /* remove an already added, if first of series */
315 list->remove_last(list, (void**)&proposal);
316 proposal->destroy(proposal);
317 ignore_struct_number = struct_number;
318 }
319 continue;
320 }
321 struct_number++;
322 proposal = substruct->get_proposal(substruct);
323 if (proposal)
324 {
325 list->insert_last(list, proposal);
326 }
327 }
328 enumerator->destroy(enumerator);
329 return list;
330 }
331
332 METHOD(sa_payload_t, create_substructure_enumerator, enumerator_t*,
333 private_sa_payload_t *this)
334 {
335 return this->proposals->create_enumerator(this->proposals);
336 }
337
338 METHOD2(payload_t, sa_payload_t, destroy, void,
339 private_sa_payload_t *this)
340 {
341 this->proposals->destroy_offset(this->proposals,
342 offsetof(payload_t, destroy));
343 free(this);
344 }
345
346 /*
347 * Described in header.
348 */
349 sa_payload_t *sa_payload_create(payload_type_t type)
350 {
351 private_sa_payload_t *this;
352
353 INIT(this,
354 .public = {
355 .payload_interface = {
356 .verify = _verify,
357 .get_encoding_rules = _get_encoding_rules,
358 .get_length = _get_length,
359 .get_next_type = _get_next_type,
360 .set_next_type = _set_next_type,
361 .get_type = _get_type,
362 .destroy = _destroy,
363 },
364 .add_proposal = _add_proposal,
365 .get_proposals = _get_proposals,
366 .create_substructure_enumerator = _create_substructure_enumerator,
367 .destroy = _destroy,
368 },
369 .next_payload = NO_PAYLOAD,
370 .proposals = linked_list_create(),
371 .type = type,
372 /* for IKEv1 only */
373 .doi = IKEV1_DOI_IPSEC,
374 .situation = SIT_IDENTITY_ONLY,
375 );
376
377 compute_length(this);
378
379 return &this->public;
380 }
381
382 /*
383 * Described in header.
384 */
385 sa_payload_t *sa_payload_create_from_proposal_list(payload_type_t type,
386 linked_list_t *proposals)
387 {
388 sa_payload_t *this;
389 enumerator_t *enumerator;
390 proposal_t *proposal;
391
392 this = sa_payload_create(type);
393 enumerator = proposals->create_enumerator(proposals);
394 while (enumerator->enumerate(enumerator, &proposal))
395 {
396 this->add_proposal(this, proposal);
397 }
398 enumerator->destroy(enumerator);
399
400 return this;
401 }
402
403 /*
404 * Described in header.
405 */
406 sa_payload_t *sa_payload_create_from_proposal(payload_type_t type,
407 proposal_t *proposal)
408 {
409 sa_payload_t *this;
410
411 this = sa_payload_create(type);
412 this->add_proposal(this, proposal);
413
414 return this;
415 }