- implemented sa_config
[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
28 typedef struct private_sa_config_t private_sa_config_t;
29
30 /**
31 * Private data of an sa_config_t object
32 */
33 struct private_sa_config_t {
34
35 /**
36 * Public part
37 */
38 sa_config_t public;
39
40 /**
41 * id to use to identify us
42 */
43 identification_t *my_id;
44
45 /**
46 * allowed id for other
47 */
48 identification_t *other_id;
49
50 /**
51 * authentification method to use
52 */
53 auth_method_t auth_method;
54
55 /**
56 * list for all proposals
57 */
58 linked_list_t *proposals;
59
60 /**
61 * list for traffic selectors
62 */
63 linked_list_t *ts;
64
65 /**
66 * compare two traffic_selectors for equality
67 */
68 bool (*traffic_selector_equals) (private_sa_config_t *this, traffic_selector_t *first, traffic_selector_t *second);
69
70 /**
71 * compare two proposals for equality
72 */
73 bool (*proposal_equals) (private_sa_config_t *this, child_proposal_t *first, child_proposal_t *second);
74 };
75
76
77 static identification_t *get_my_id(private_sa_config_t *this)
78 {
79 return this->my_id;
80 }
81
82 static identification_t *get_other_id(private_sa_config_t *this)
83 {
84 return this->other_id;
85 }
86
87 static auth_method_t get_auth_method(private_sa_config_t *this)
88 {
89 return this->auth_method;
90 }
91
92 static size_t get_traffic_selectors(private_sa_config_t *this, traffic_selector_t **traffic_selectors)
93 {
94 iterator_t *iterator;
95 traffic_selector_t *current_ts;
96 int counter = 0;
97 *traffic_selectors = allocator_alloc(sizeof(traffic_selector_t) * this->ts->get_count(this->ts));
98
99 /* copy all ts from the list in an array */
100 iterator = this->ts->create_iterator(this->ts, TRUE);
101 while (iterator->has_next(iterator))
102 {
103 iterator->current(iterator, (void**)&current_ts);
104 memcpy((*traffic_selectors) + counter, current_ts, sizeof(traffic_selector_t));
105 counter++;
106 }
107 iterator->destroy(iterator);
108 return counter;
109 }
110
111 static size_t select_traffic_selectors(private_sa_config_t *this, traffic_selector_t *supplied, size_t count, traffic_selector_t **selected)
112 {
113 iterator_t *iterator;
114 traffic_selector_t *current_ts;
115 int i, counter = 0;
116 *selected = allocator_alloc(sizeof(traffic_selector_t) * this->ts->get_count(this->ts));
117
118 /* iterate over all stored proposals */
119 iterator = this->ts->create_iterator(this->ts, TRUE);
120 while (iterator->has_next(iterator))
121 {
122 iterator->current(iterator, (void**)&current_ts);
123 for (i = 0; i < count; i++)
124 {
125 /* copy if a supplied one is equal to ours */
126 if (this->traffic_selector_equals(this, &(supplied[i]), current_ts))
127 {
128 memcpy((*selected) + counter, current_ts, sizeof(traffic_selector_t));
129 counter++;
130 }
131 }
132 }
133 iterator->destroy(iterator);
134
135 /* free unused space */
136 *selected = allocator_realloc(*selected, sizeof(traffic_selector_t) * counter);
137 return counter;
138 }
139
140 static size_t get_proposals(private_sa_config_t *this, child_proposal_t **proposals)
141 {
142 iterator_t *iterator;
143 child_proposal_t *current_proposal;
144 int counter = 0;
145 *proposals = allocator_alloc(sizeof(child_proposal_t) * this->proposals->get_count(this->proposals));
146
147 /* copy all proposals from the list in an array */
148 iterator = this->proposals->create_iterator(this->proposals, TRUE);
149 while (iterator->has_next(iterator))
150 {
151 iterator->current(iterator, (void**)&current_proposal);
152 memcpy((*proposals) + counter, current_proposal, sizeof(child_proposal_t));
153 counter++;
154 }
155 iterator->destroy(iterator);
156 return counter;
157 }
158
159 static child_proposal_t *select_proposal(private_sa_config_t *this, child_proposal_t *supplied, size_t count)
160 {
161 iterator_t *iterator;
162 child_proposal_t *current_proposal, *selected_proposal = NULL;
163 int i;
164
165 /* iterate over all stored proposals */
166 iterator = this->proposals->create_iterator(this->proposals, TRUE);
167 while (iterator->has_next(iterator))
168 {
169 iterator->current(iterator, (void**)&current_proposal);
170 /* copy and break if a proposal matches */
171 for (i = 0; i < count; i++)
172 {
173 if (this->proposal_equals(this, &(supplied[i]), current_proposal))
174 {
175 selected_proposal = allocator_alloc(sizeof(child_proposal_t));
176 memcpy(selected_proposal, current_proposal, sizeof(child_proposal_t));
177 break;
178 }
179 }
180 }
181 iterator->destroy(iterator);
182
183 return selected_proposal;
184 }
185
186 static bool traffic_selector_equals(private_sa_config_t *this, traffic_selector_t *first, traffic_selector_t *second)
187 {
188 if (first->protocol == second->protocol)
189 {
190 if (first->begin->equals(first->begin, second->begin) &&
191 first->end->equals(first->end, second->end))
192 {
193 return TRUE;
194
195 }
196 }
197 return FALSE;
198 }
199
200 static bool proposal_equals(private_sa_config_t *this, child_proposal_t *first, child_proposal_t *second)
201 {
202 if (first->ah.is_set && second->ah.is_set)
203 {
204 if ((first->ah.integrity_algorithm != second->ah.integrity_algorithm) ||
205 (first->ah.key_size != second->ah.key_size))
206 {
207 return FALSE;
208 }
209 }
210 else
211 {
212 return FALSE;
213 }
214 if (first->esp.is_set && second->esp.is_set)
215 {
216 if ((first->esp.encryption_algorithm != second->esp.encryption_algorithm) ||
217 (first->esp.key_size != second->esp.key_size))
218 {
219 return FALSE;
220 }
221 }
222 else
223 {
224 return FALSE;
225 }
226 return TRUE;
227 }
228
229 static void add_traffic_selector(private_sa_config_t *this, traffic_selector_t *traffic_selector)
230 {
231 this->ts->insert_last(this->ts, (void*)traffic_selector);
232 }
233
234 static void add_proposal(private_sa_config_t *this, child_proposal_t *proposal)
235 {
236 this->proposals->insert_last(this->ts, (void*)proposal);
237 }
238
239 /**
240 * Implements sa_config_t.destroy.
241 */
242 static status_t destroy(private_sa_config_t *this)
243 {
244 child_proposal_t *proposal;
245 traffic_selector_t *traffic_selector;
246
247 /* delete proposals */
248 while(this->proposals->get_count(this->proposals) > 0)
249 {
250 this->proposals->remove_last(this->proposals, (void**)&proposal);
251 allocator_free(proposal);
252 }
253 this->proposals->destroy(this->proposals);
254
255 /* delete traffic selectors */
256 while(this->ts->get_count(this->ts) > 0)
257 {
258 this->ts->remove_last(this->ts, (void**)&traffic_selector);
259 allocator_free(traffic_selector);
260 }
261 this->ts->destroy(this->ts);
262
263 allocator_free(this);
264 return SUCCESS;
265 }
266
267 /*
268 * Described in header-file
269 */
270 sa_config_t *sa_config_create()
271 {
272 private_sa_config_t *this = allocator_alloc_thing(private_sa_config_t);
273
274 /* public functions */
275 this->public.get_my_id = (identification_t(*)(sa_config_t*))get_my_id;
276 this->public.get_other_id = (identification_t(*)(sa_config_t*))get_other_id;
277 this->public.get_auth_method = (auth_method_t(*)(sa_config_t*))get_auth_method;
278 this->public.get_traffic_selectors = (size_t(*)(sa_config_t*,traffic_selector_t**))get_traffic_selectors;
279 this->public.select_traffic_selectors = (size_t(*)(sa_config_t*,traffic_selector_t*,size_t,traffic_selector_t**))select_traffic_selectors;
280 this->public.get_proposals = (size_t(*)(sa_config_t*,child_proposal_t**))get_proposals;
281 this->public.select_proposal = (child_proposal_t*(*)(sa_config_t*,child_proposal_t*,size_t))select_proposal;
282 this->public.add_traffic_selector = (void(*)(sa_config_t*,traffic_selector_t*))add_traffic_selector;
283 this->public.add_proposal = (void(*)(sa_config_t*,child_proposal_t*))add_proposal;
284 this->public.destroy = (void(*)(sa_config_t*))destroy;
285
286 /* private variables */
287 this->proposal_equals = proposal_equals;
288 this->traffic_selector_equals = traffic_selector_equals;
289 this->proposals = linked_list_create();
290 this->ts = linked_list_create();
291
292 return (&this->public);
293 }