- tested sa with traffic selectors
[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
63 */
64 linked_list_t *ts;
65
66 /**
67 * compare two proposals for equality
68 */
69 bool (*proposal_equals) (private_sa_config_t *this, child_proposal_t *first, child_proposal_t *second);
70 };
71
72 /**
73 * implements sa_config_t.get_my_id
74 */
75 static identification_t *get_my_id(private_sa_config_t *this)
76 {
77 return this->my_id;
78 }
79
80 /**
81 * implements sa_config_t.get_other_id
82 */
83 static identification_t *get_other_id(private_sa_config_t *this)
84 {
85 return this->other_id;
86 }
87
88 /**
89 * implements sa_config_t.get_auth_method
90 */
91 static auth_method_t get_auth_method(private_sa_config_t *this)
92 {
93 return this->auth_method;
94 }
95
96 /**
97 * implements sa_config_t.get_traffic_selectors
98 */
99 static size_t get_traffic_selectors(private_sa_config_t *this, traffic_selector_t ***traffic_selectors)
100 {
101 iterator_t *iterator;
102 traffic_selector_t *current_ts;
103 int counter = 0;
104 *traffic_selectors = allocator_alloc(sizeof(traffic_selector_t*) * this->ts->get_count(this->ts));
105
106 /* copy all ts from the list in an array */
107 iterator = this->ts->create_iterator(this->ts, TRUE);
108 while (iterator->has_next(iterator))
109 {
110 iterator->current(iterator, (void**)&current_ts);
111 *((*traffic_selectors) + counter) = current_ts->clone(current_ts);
112 counter++;
113 }
114 iterator->destroy(iterator);
115 return counter;
116 }
117
118 /**
119 * implements sa_config_t.select_traffic_selectors
120 */
121 static size_t select_traffic_selectors(private_sa_config_t *this, traffic_selector_t **supplied, size_t count, traffic_selector_t ***selected)
122 {
123 iterator_t *iterator;
124 traffic_selector_t *current_ts;
125 int i, counter = 0;
126 *selected = allocator_alloc(sizeof(traffic_selector_t*) * this->ts->get_count(this->ts));
127
128 /* iterate over all stored proposals */
129 iterator = this->ts->create_iterator(this->ts, TRUE);
130 while (iterator->has_next(iterator))
131 {
132 iterator->current(iterator, (void**)&current_ts);
133 for (i = 0; i < count; i++)
134 {
135 traffic_selector_t *new_ts;
136 /* compare it */
137 new_ts = current_ts->get_subset(current_ts, supplied[i]);
138 /* match ? */
139 if (new_ts)
140 {
141 *((*selected) + counter) = new_ts;
142 counter++;
143 }
144 }
145 }
146 iterator->destroy(iterator);
147
148 /* free unused space */
149 *selected = allocator_realloc(*selected, sizeof(traffic_selector_t) * counter);
150 return counter;
151 }
152
153 /**
154 * implements sa_config_t.get_proposals
155 */
156 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)
157 {
158 iterator_t *iterator;
159 child_proposal_t *current_proposal;
160 int counter = 0;
161 *proposals = allocator_alloc(sizeof(child_proposal_t) * this->proposals->get_count(this->proposals));
162
163 /* copy all proposals from the list in an array */
164 iterator = this->proposals->create_iterator(this->proposals, TRUE);
165 while (iterator->has_next(iterator))
166 {
167 child_proposal_t *new_proposal = (*proposals) + counter;
168 iterator->current(iterator, (void**)&current_proposal);
169 *new_proposal = *current_proposal;
170 memcpy(new_proposal->ah.spi, ah_spi, 4);
171 memcpy(new_proposal->ah.spi, esp_spi, 4);
172 counter++;
173 }
174 iterator->destroy(iterator);
175 return counter;
176 }
177
178 /**
179 * implements sa_config_t.select_proposal
180 */
181 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)
182 {
183 iterator_t *iterator;
184 child_proposal_t *current_proposal, *selected_proposal;
185 int i;
186
187 /* iterate over all stored proposals */
188 iterator = this->proposals->create_iterator(this->proposals, TRUE);
189 while (iterator->has_next(iterator))
190 {
191 iterator->current(iterator, (void**)&current_proposal);
192 /* copy and break if a proposal matches */
193 for (i = 0; i < count; i++)
194 {
195 if (this->proposal_equals(this, &(supplied[i]), current_proposal))
196 {
197 selected_proposal = allocator_alloc(sizeof(child_proposal_t));
198 *selected_proposal = *current_proposal;
199 memcpy(selected_proposal->ah.spi, ah_spi, 4);
200 memcpy(selected_proposal->ah.spi, esp_spi, 4);
201 iterator->destroy(iterator);
202 return selected_proposal;
203 }
204 }
205 }
206 iterator->destroy(iterator);
207 return NULL;
208 }
209
210
211 /**
212 * implements private_sa_config_t.proposal_equals
213 */
214 static bool proposal_equals(private_sa_config_t *this, child_proposal_t *first, child_proposal_t *second)
215 {
216 bool equal = FALSE;
217
218 if (first->ah.is_set && second->ah.is_set)
219 {
220 if ((first->ah.integrity_algorithm != second->ah.integrity_algorithm) ||
221 (first->ah.integrity_algorithm_key_size != second->ah.integrity_algorithm_key_size) ||
222 (first->ah.diffie_hellman_group != second->ah.diffie_hellman_group) ||
223 (first->ah.extended_sequence_numbers != second->ah.extended_sequence_numbers))
224 {
225 return FALSE;
226 }
227 equal = TRUE;
228 }
229 if (first->esp.is_set && second->esp.is_set)
230 {
231 if ((first->esp.encryption_algorithm != second->esp.encryption_algorithm) ||
232 (first->esp.encryption_algorithm_key_size != second->esp.encryption_algorithm_key_size) ||
233 (first->esp.integrity_algorithm != second->esp.integrity_algorithm) ||
234 (first->esp.integrity_algorithm_key_size != second->esp.integrity_algorithm_key_size) ||
235 (first->esp.diffie_hellman_group != second->esp.diffie_hellman_group) ||
236 (first->esp.extended_sequence_numbers != second->esp.extended_sequence_numbers))
237 {
238 return FALSE;
239 }
240 equal = TRUE;
241 }
242 return equal;
243 }
244
245 /**
246 * implements sa_config_t.add_traffic_selector
247 */
248 static void add_traffic_selector(private_sa_config_t *this, traffic_selector_t *traffic_selector)
249 {
250 /* clone ts, and add*/
251
252 this->ts->insert_last(this->ts, (void*)traffic_selector->clone(traffic_selector));
253 }
254
255 /**
256 * implements sa_config_t.add_proposal
257 */
258 static void add_proposal(private_sa_config_t *this, child_proposal_t *proposal)
259 {
260 /* clone proposal, and add*/
261 child_proposal_t *new_proposal = allocator_alloc_thing(child_proposal_t);
262 *new_proposal = *proposal;
263 this->proposals->insert_last(this->proposals, (void*)new_proposal);
264 }
265
266 /**
267 * Implements sa_config_t.destroy.
268 */
269 static status_t destroy(private_sa_config_t *this)
270 {
271 child_proposal_t *proposal;
272 traffic_selector_t *traffic_selector;
273
274
275 /* delete proposals */
276 while(this->proposals->get_count(this->proposals) > 0)
277 {
278 this->proposals->remove_last(this->proposals, (void**)&proposal);
279 allocator_free(proposal);
280 }
281 this->proposals->destroy(this->proposals);
282
283 /* delete traffic selectors */
284 while(this->ts->get_count(this->ts) > 0)
285 {
286 this->ts->remove_last(this->ts, (void**)&traffic_selector);
287 traffic_selector->destroy(traffic_selector);
288 }
289 this->ts->destroy(this->ts);
290
291 /* delete ids */
292 this->my_id->destroy(this->my_id);
293 this->other_id->destroy(this->other_id);
294
295 allocator_free(this);
296 return SUCCESS;
297 }
298
299 /*
300 * Described in header-file
301 */
302 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)
303 {
304 private_sa_config_t *this = allocator_alloc_thing(private_sa_config_t);
305
306 /* public functions */
307 this->public.get_my_id = (identification_t*(*)(sa_config_t*))get_my_id;
308 this->public.get_other_id = (identification_t*(*)(sa_config_t*))get_other_id;
309 this->public.get_auth_method = (auth_method_t(*)(sa_config_t*))get_auth_method;
310 this->public.get_traffic_selectors = (size_t(*)(sa_config_t*,traffic_selector_t***))get_traffic_selectors;
311 this->public.select_traffic_selectors = (size_t(*)(sa_config_t*,traffic_selector_t**,size_t,traffic_selector_t***))select_traffic_selectors;
312 this->public.get_proposals = (size_t(*)(sa_config_t*,u_int8_t[4],u_int8_t[4],child_proposal_t**))get_proposals;
313 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;
314 this->public.add_traffic_selector = (void(*)(sa_config_t*,traffic_selector_t*))add_traffic_selector;
315 this->public.add_proposal = (void(*)(sa_config_t*,child_proposal_t*))add_proposal;
316 this->public.destroy = (void(*)(sa_config_t*))destroy;
317
318
319 /* apply init values */
320 this->my_id = identification_create_from_string(my_id_type, my_id);
321 if (this->my_id == NULL)
322 {
323 allocator_free(this);
324 return NULL;
325 }
326 this->other_id = identification_create_from_string(other_id_type, other_id);
327 if (this->my_id == NULL)
328 {
329 this->other_id->destroy(this->other_id);
330 allocator_free(this);
331 return NULL;
332 }
333
334 /* init private members*/
335 this->proposal_equals = proposal_equals;
336 this->proposals = linked_list_create();
337 this->ts = linked_list_create();
338
339 return (&this->public);
340 }