- added ts for both initator and responder
[strongswan.git] / Source / charon / config / sa_config.c
1 /**
2 * @file sa_config.c
3 *
4 * @brief Implementation of sa_config_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #include "sa_config.h"
24
25 #include <utils/linked_list.h>
26 #include <utils/allocator.h>
27 #include <utils/identification.h>
28
29 typedef struct private_sa_config_t private_sa_config_t;
30
31 /**
32 * Private data of an sa_config_t object
33 */
34 struct private_sa_config_t {
35
36 /**
37 * Public part
38 */
39 sa_config_t public;
40
41 /**
42 * id to use to identify us
43 */
44 identification_t *my_id;
45
46 /**
47 * allowed id for other
48 */
49 identification_t *other_id;
50
51 /**
52 * authentification method to use
53 */
54 auth_method_t auth_method;
55
56 /**
57 * list for all proposals
58 */
59 linked_list_t *proposals;
60
61 /**
62 * list for traffic selectors for initiator site
63 */
64 linked_list_t *ts_initiator;
65
66 /**
67 * list for traffic selectors for responder site
68 */
69 linked_list_t *ts_responder;
70
71 /**
72 * compare two proposals for equality
73 */
74 bool (*proposal_equals) (private_sa_config_t *this, child_proposal_t *first, child_proposal_t *second);
75
76 /**
77 * get_traffic_selectors for both
78 */
79 size_t (*get_traffic_selectors) (private_sa_config_t *,linked_list_t*,traffic_selector_t**[]);
80
81 /**
82 * select_traffic_selectors for both
83 */
84 size_t (*select_traffic_selectors) (private_sa_config_t *,linked_list_t*,traffic_selector_t*[],size_t,traffic_selector_t**[]);
85 };
86
87 /**
88 * implements sa_config_t.get_my_id
89 */
90 static identification_t *get_my_id(private_sa_config_t *this)
91 {
92 return this->my_id;
93 }
94
95 /**
96 * implements sa_config_t.get_other_id
97 */
98 static identification_t *get_other_id(private_sa_config_t *this)
99 {
100 return this->other_id;
101 }
102
103 /**
104 * implements sa_config_t.get_auth_method
105 */
106 static auth_method_t get_auth_method(private_sa_config_t *this)
107 {
108 return this->auth_method;
109 }
110
111
112 /**
113 * implements sa_config_t.get_traffic_selectors_initiator
114 */
115 static size_t get_traffic_selectors_initiator(private_sa_config_t *this, traffic_selector_t **traffic_selectors[])
116 {
117 return this->get_traffic_selectors(this, this->ts_initiator, traffic_selectors);
118 }
119
120 /**
121 * implements sa_config_t.get_traffic_selectors_responder
122 */
123 static size_t get_traffic_selectors_responder(private_sa_config_t *this, traffic_selector_t **traffic_selectors[])
124 {
125 return this->get_traffic_selectors(this, this->ts_responder, traffic_selectors);
126 }
127
128 /**
129 * implements private_sa_config_t.get_traffic_selectors
130 */
131 static size_t get_traffic_selectors(private_sa_config_t *this, linked_list_t *ts_list, traffic_selector_t **traffic_selectors[])
132 {
133 iterator_t *iterator;
134 traffic_selector_t *current_ts;
135 int counter = 0;
136 *traffic_selectors = allocator_alloc(sizeof(traffic_selector_t*) * ts_list->get_count(ts_list));
137
138 /* copy all ts from the list in an array */
139 iterator = ts_list->create_iterator(ts_list, TRUE);
140 while (iterator->has_next(iterator))
141 {
142 iterator->current(iterator, (void**)&current_ts);
143 *((*traffic_selectors) + counter) = current_ts->clone(current_ts);
144 counter++;
145 }
146 iterator->destroy(iterator);
147 return counter;
148 }
149
150 /**
151 * implements private_sa_config_t.select_traffic_selectors_initiator
152 */
153 static size_t select_traffic_selectors_initiator(private_sa_config_t *this,traffic_selector_t *supplied[], size_t count, traffic_selector_t **selected[])
154 {
155 return this->select_traffic_selectors(this, this->ts_initiator, supplied, count, selected);
156 }
157
158 /**
159 * implements private_sa_config_t.select_traffic_selectors_responder
160 */
161 static size_t select_traffic_selectors_responder(private_sa_config_t *this,traffic_selector_t *supplied[], size_t count, traffic_selector_t **selected[])
162 {
163 return this->select_traffic_selectors(this, this->ts_responder, supplied, count, selected);
164 }
165 /**
166 * implements private_sa_config_t.select_traffic_selectors
167 */
168 static size_t select_traffic_selectors(private_sa_config_t *this, linked_list_t *ts_list, traffic_selector_t *supplied[], size_t count, traffic_selector_t **selected[])
169 {
170 iterator_t *iterator;
171 traffic_selector_t *current_ts;
172 int i, counter = 0;
173 *selected = allocator_alloc(sizeof(traffic_selector_t*) * ts_list->get_count(ts_list));
174
175 /* iterate over all stored proposals */
176 iterator = ts_list->create_iterator(ts_list, TRUE);
177 while (iterator->has_next(iterator))
178 {
179 iterator->current(iterator, (void**)&current_ts);
180 for (i = 0; i < count; i++)
181 {
182 traffic_selector_t *new_ts;
183 /* compare it */
184 new_ts = current_ts->get_subset(current_ts, supplied[i]);
185 /* match ? */
186 if (new_ts)
187 {
188 *((*selected) + counter) = new_ts;
189 counter++;
190 }
191 }
192 }
193 iterator->destroy(iterator);
194
195 /* free unused space */
196 *selected = allocator_realloc(*selected, sizeof(traffic_selector_t) * counter);
197 return counter;
198 }
199
200 /**
201 * implements sa_config_t.get_proposals
202 */
203 static size_t get_proposals(private_sa_config_t *this, u_int8_t ah_spi[4], u_int8_t esp_spi[4], child_proposal_t **proposals)
204 {
205 iterator_t *iterator;
206 child_proposal_t *current_proposal;
207 int counter = 0;
208 *proposals = allocator_alloc(sizeof(child_proposal_t) * this->proposals->get_count(this->proposals));
209
210 /* copy all proposals from the list in an array */
211 iterator = this->proposals->create_iterator(this->proposals, TRUE);
212 while (iterator->has_next(iterator))
213 {
214 child_proposal_t *new_proposal = (*proposals) + counter;
215 iterator->current(iterator, (void**)&current_proposal);
216 *new_proposal = *current_proposal;
217 memcpy(new_proposal->ah.spi, ah_spi, 4);
218 memcpy(new_proposal->ah.spi, esp_spi, 4);
219 counter++;
220 }
221 iterator->destroy(iterator);
222 return counter;
223 }
224
225 /**
226 * implements sa_config_t.select_proposal
227 */
228 static child_proposal_t *select_proposal(private_sa_config_t *this, u_int8_t ah_spi[4], u_int8_t esp_spi[4], child_proposal_t *supplied, size_t count)
229 {
230 iterator_t *iterator;
231 child_proposal_t *current_proposal, *selected_proposal;
232 int i;
233
234 /* iterate over all stored proposals */
235 iterator = this->proposals->create_iterator(this->proposals, TRUE);
236 while (iterator->has_next(iterator))
237 {
238 iterator->current(iterator, (void**)&current_proposal);
239 /* copy and break if a proposal matches */
240 for (i = 0; i < count; i++)
241 {
242 if (this->proposal_equals(this, &(supplied[i]), current_proposal))
243 {
244 selected_proposal = allocator_alloc(sizeof(child_proposal_t));
245 *selected_proposal = *current_proposal;
246 memcpy(selected_proposal->ah.spi, ah_spi, 4);
247 memcpy(selected_proposal->ah.spi, esp_spi, 4);
248 iterator->destroy(iterator);
249 return selected_proposal;
250 }
251 }
252 }
253 iterator->destroy(iterator);
254 return NULL;
255 }
256
257
258 /**
259 * implements private_sa_config_t.proposal_equals
260 */
261 static bool proposal_equals(private_sa_config_t *this, child_proposal_t *first, child_proposal_t *second)
262 {
263 bool equal = FALSE;
264
265 if (first->ah.is_set && second->ah.is_set)
266 {
267 if ((first->ah.integrity_algorithm != second->ah.integrity_algorithm) ||
268 (first->ah.integrity_algorithm_key_size != second->ah.integrity_algorithm_key_size) ||
269 (first->ah.diffie_hellman_group != second->ah.diffie_hellman_group) ||
270 (first->ah.extended_sequence_numbers != second->ah.extended_sequence_numbers))
271 {
272 return FALSE;
273 }
274 equal = TRUE;
275 }
276 if (first->esp.is_set && second->esp.is_set)
277 {
278 if ((first->esp.encryption_algorithm != second->esp.encryption_algorithm) ||
279 (first->esp.encryption_algorithm_key_size != second->esp.encryption_algorithm_key_size) ||
280 (first->esp.integrity_algorithm != second->esp.integrity_algorithm) ||
281 (first->esp.integrity_algorithm_key_size != second->esp.integrity_algorithm_key_size) ||
282 (first->esp.diffie_hellman_group != second->esp.diffie_hellman_group) ||
283 (first->esp.extended_sequence_numbers != second->esp.extended_sequence_numbers))
284 {
285 return FALSE;
286 }
287 equal = TRUE;
288 }
289 return equal;
290 }
291
292 /**
293 * implements sa_config_t.add_traffic_selector_initiator
294 */
295 static void add_traffic_selector_initiator(private_sa_config_t *this, traffic_selector_t *traffic_selector)
296 {
297 /* clone ts, and add*/
298 this->ts_initiator->insert_last(this->ts_initiator, (void*)traffic_selector->clone(traffic_selector));
299 }
300
301 /**
302 * implements sa_config_t.add_traffic_selector_responder
303 */
304 static void add_traffic_selector_responder(private_sa_config_t *this, traffic_selector_t *traffic_selector)
305 {
306 /* clone ts, and add*/
307 this->ts_responder->insert_last(this->ts_responder, (void*)traffic_selector->clone(traffic_selector));
308 }
309
310 /**
311 * implements sa_config_t.add_proposal
312 */
313 static void add_proposal(private_sa_config_t *this, child_proposal_t *proposal)
314 {
315 /* clone proposal, and add*/
316 child_proposal_t *new_proposal = allocator_alloc_thing(child_proposal_t);
317 *new_proposal = *proposal;
318 this->proposals->insert_last(this->proposals, (void*)new_proposal);
319 }
320
321 /**
322 * Implements sa_config_t.destroy.
323 */
324 static status_t destroy(private_sa_config_t *this)
325 {
326 child_proposal_t *proposal;
327 traffic_selector_t *traffic_selector;
328
329
330 /* delete proposals */
331 while(this->proposals->get_count(this->proposals) > 0)
332 {
333 this->proposals->remove_last(this->proposals, (void**)&proposal);
334 allocator_free(proposal);
335 }
336 this->proposals->destroy(this->proposals);
337
338 /* delete traffic selectors */
339 while(this->ts_initiator->get_count(this->ts_initiator) > 0)
340 {
341 this->ts_initiator->remove_last(this->ts_initiator, (void**)&traffic_selector);
342 traffic_selector->destroy(traffic_selector);
343 }
344 this->ts_initiator->destroy(this->ts_initiator);
345
346 /* delete traffic selectors */
347 while(this->ts_responder->get_count(this->ts_responder) > 0)
348 {
349 this->ts_responder->remove_last(this->ts_responder, (void**)&traffic_selector);
350 traffic_selector->destroy(traffic_selector);
351 }
352 this->ts_responder->destroy(this->ts_responder);
353
354 /* delete ids */
355 this->my_id->destroy(this->my_id);
356 this->other_id->destroy(this->other_id);
357
358 allocator_free(this);
359 return SUCCESS;
360 }
361
362 /*
363 * Described in header-file
364 */
365 sa_config_t *sa_config_create(id_type_t my_id_type, char *my_id, id_type_t other_id_type, char *other_id, auth_method_t auth_method)
366 {
367 private_sa_config_t *this = allocator_alloc_thing(private_sa_config_t);
368
369 /* public functions */
370 this->public.get_my_id = (identification_t*(*)(sa_config_t*))get_my_id;
371 this->public.get_other_id = (identification_t*(*)(sa_config_t*))get_other_id;
372 this->public.get_auth_method = (auth_method_t(*)(sa_config_t*))get_auth_method;
373 this->public.get_traffic_selectors_initiator = (size_t(*)(sa_config_t*,traffic_selector_t**[]))get_traffic_selectors_initiator;
374 this->public.select_traffic_selectors_initiator = (size_t(*)(sa_config_t*,traffic_selector_t*[],size_t,traffic_selector_t**[]))select_traffic_selectors_initiator;
375 this->public.get_traffic_selectors_responder = (size_t(*)(sa_config_t*,traffic_selector_t**[]))get_traffic_selectors_responder;
376 this->public.select_traffic_selectors_responder = (size_t(*)(sa_config_t*,traffic_selector_t*[],size_t,traffic_selector_t**[]))select_traffic_selectors_responder;
377 this->public.get_proposals = (size_t(*)(sa_config_t*,u_int8_t[4],u_int8_t[4],child_proposal_t**))get_proposals;
378 this->public.select_proposal = (child_proposal_t*(*)(sa_config_t*,u_int8_t[4],u_int8_t[4],child_proposal_t*,size_t))select_proposal;
379 this->public.add_traffic_selector_initiator = (void(*)(sa_config_t*,traffic_selector_t*))add_traffic_selector_initiator;
380 this->public.add_traffic_selector_responder = (void(*)(sa_config_t*,traffic_selector_t*))add_traffic_selector_responder;
381 this->public.add_proposal = (void(*)(sa_config_t*,child_proposal_t*))add_proposal;
382 this->public.destroy = (void(*)(sa_config_t*))destroy;
383
384
385 /* apply init values */
386 this->my_id = identification_create_from_string(my_id_type, my_id);
387 if (this->my_id == NULL)
388 {
389 allocator_free(this);
390 return NULL;
391 }
392 this->other_id = identification_create_from_string(other_id_type, other_id);
393 if (this->my_id == NULL)
394 {
395 this->other_id->destroy(this->other_id);
396 allocator_free(this);
397 return NULL;
398 }
399
400 /* init private members*/
401 this->proposal_equals = proposal_equals;
402 this->select_traffic_selectors = select_traffic_selectors;
403 this->get_traffic_selectors = get_traffic_selectors;
404 this->proposals = linked_list_create();
405 this->ts_initiator = linked_list_create();
406 this->ts_responder = linked_list_create();
407
408 return (&this->public);
409 }