a0510bfc3195ef0db0406ac598d9fa8bb44077ed
[strongswan.git] / Source / charon / configuration_manager.c
1 /**
2 * @file configuration.c
3 *
4 * @brief Configuration class used to store IKE_SA-configurations.
5 *
6 * Object of this type represents a configuration for an IKE_SA and its child_sa's.
7 *
8 */
9
10 /*
11 * Copyright (C) 2005 Jan Hutter, Martin Willi
12 * Hochschule fuer Technik Rapperswil
13 *
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
18 *
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * for more details.
23 */
24
25 #include <stdlib.h>
26
27 #include "configuration_manager.h"
28
29 #include "types.h"
30 #include "utils/allocator.h"
31 #include "payloads/nonce_payload.h"
32 #include "payloads/proposal_substructure.h"
33 #include "payloads/ke_payload.h"
34 #include "payloads/transform_attribute.h"
35
36 /**
37 * Private data of an configuration_t object
38 */
39 typedef struct private_configuration_manager_s private_configuration_manager_t;
40
41 struct private_configuration_manager_s {
42
43 /**
44 * Public part
45 */
46 configuration_manager_t public;
47
48 };
49
50 static status_t get_remote_host(private_configuration_manager_t *this, char *name, host_t **host)
51 {
52 /* some hard coded users for testing */
53 host_t *remote;
54 if (strcmp(name, "pinflb30") == 0) {
55 remote = host_create(AF_INET, "152.96.193.130", 500);
56 if (remote == NULL) {
57 return OUT_OF_RES;
58 }
59 *host = remote;
60 return SUCCESS;
61 }
62 else if (strcmp(name, "pinflb31") == 0) {
63 remote = host_create(AF_INET, "152.96.193.131", 500);
64 if (remote == NULL) {
65 return OUT_OF_RES;
66 }
67 *host = remote;
68 return SUCCESS;
69 }
70 return NOT_FOUND;
71 }
72
73 static status_t get_local_host(private_configuration_manager_t *this, char *name, host_t **host)
74 {
75 /* use default route for now */
76 host_t *local;
77 local = host_create(AF_INET, "0.0.0.0", 0);
78 if (local == NULL)
79 {
80 return OUT_OF_RES;
81 }
82 *host = local;
83 return SUCCESS;
84 }
85
86 static status_t get_dh_group_number(private_configuration_manager_t *this,char *name, u_int16_t *dh_group_number, u_int16_t priority)
87 {
88 *dh_group_number = MODP_1024_BIT;
89 return SUCCESS;
90 }
91
92 static status_t get_proposals_for_host(private_configuration_manager_t *this, host_t *host, linked_list_iterator_t *iterator)
93 {
94 /* use a default proposal:
95 * - ENCR_AES_CBC 128Bit
96 * - PRF_HMAC_SHA1 128Bit
97 * - AUTH_HMAC_SHA1_96 96Bit
98 * - MODP_1024_BIT
99 */
100 proposal_substructure_t *proposal;
101 transform_substructure_t *transform;
102 transform_attribute_t *attribute;
103 status_t status;
104
105 proposal = proposal_substructure_create();
106 if (proposal == NULL)
107 {
108 return OUT_OF_RES;
109 }
110
111 /*
112 * Encryption Algorithm
113 */
114 transform = transform_substructure_create();
115 if (transform == NULL)
116 {
117 proposal->destroy(proposal);
118 return OUT_OF_RES;
119 }
120 status = proposal->add_transform_substructure(proposal, transform);
121 if (status != SUCCESS)
122 {
123 proposal->destroy(proposal);
124 return OUT_OF_RES;
125 }
126 transform->set_is_last_transform(transform, FALSE);
127 transform->set_transform_type(transform, ENCRYPTION_ALGORITHM);
128 transform->set_transform_id(transform, ENCR_AES_CBC);
129
130 attribute = transform_attribute_create();
131 if (attribute == NULL)
132 {
133 proposal->destroy(proposal);
134 return OUT_OF_RES;
135 }
136 status = transform->add_transform_attribute(transform, attribute);
137 if (status != SUCCESS)
138 {
139 proposal->destroy(proposal);
140 return OUT_OF_RES;
141 }
142 attribute->set_attribute_type(attribute, KEY_LENGTH);
143 attribute->set_value(attribute, 16);
144
145 /*
146 * Pseudo-random Function
147 */
148 transform = transform_substructure_create();
149 if (transform == NULL)
150 {
151 proposal->destroy(proposal);
152 return OUT_OF_RES;
153 }
154 status = proposal->add_transform_substructure(proposal, transform);
155 if (status != SUCCESS)
156 {
157 proposal->destroy(proposal);
158 return OUT_OF_RES;
159 }
160 transform->set_is_last_transform(transform, FALSE);
161 transform->set_transform_type(transform, PSEUDO_RANDOM_FUNCTION);
162 transform->set_transform_id(transform, PRF_HMAC_SHA1);
163
164 attribute = transform_attribute_create();
165 if (attribute == NULL)
166 {
167 proposal->destroy(proposal);
168 return OUT_OF_RES;
169 }
170 status = transform->add_transform_attribute(transform, attribute);
171 if (status != SUCCESS)
172 {
173 proposal->destroy(proposal);
174 return OUT_OF_RES;
175 }
176 attribute->set_attribute_type(attribute, KEY_LENGTH);
177 attribute->set_value(attribute, 16);
178
179
180 /*
181 * Integrity Algorithm
182 */
183 transform = transform_substructure_create();
184 if (transform == NULL)
185 {
186 proposal->destroy(proposal);
187 return OUT_OF_RES;
188 }
189 status = proposal->add_transform_substructure(proposal, transform);
190 if (status != SUCCESS)
191 {
192 proposal->destroy(proposal);
193 return OUT_OF_RES;
194 }
195 transform->set_is_last_transform(transform, FALSE);
196 transform->set_transform_type(transform, INTEGRITIY_ALGORITHM);
197 transform->set_transform_id(transform, AUTH_HMAC_SHA1_96);
198
199 attribute = transform_attribute_create();
200 if (attribute == NULL)
201 {
202 proposal->destroy(proposal);
203 return OUT_OF_RES;
204 }
205 status = transform->add_transform_attribute(transform, attribute);
206 if (status != SUCCESS)
207 {
208 proposal->destroy(proposal);
209 return OUT_OF_RES;
210 }
211 attribute->set_attribute_type(attribute, KEY_LENGTH);
212 attribute->set_value(attribute, 12);
213
214
215 /*
216 * Diffie-Hellman Group
217 */
218 transform = transform_substructure_create();
219 if (transform == NULL)
220 {
221 proposal->destroy(proposal);
222 return OUT_OF_RES;
223 }
224 status = proposal->add_transform_substructure(proposal, transform);
225 if (status != SUCCESS)
226 {
227 proposal->destroy(proposal);
228 return OUT_OF_RES;
229 }
230 transform->set_is_last_transform(transform, FALSE);
231 transform->set_transform_type(transform, DIFFIE_HELLMAN_GROUP);
232 transform->set_transform_id(transform, MODP_1024_BIT);
233
234 iterator->insert_after(iterator, (void*)proposal);
235
236 return SUCCESS;
237 }
238
239 static status_t select_proposals_for_host(private_configuration_manager_t *this, host_t *host, linked_list_iterator_t *in, linked_list_iterator_t *out)
240 {
241
242
243 return FAILED;
244 }
245
246 static status_t is_dh_group_allowed_for_host(private_configuration_manager_t *this, host_t *host, diffie_hellman_group_t group, bool *allowed)
247 {
248 if (group == MODP_768_BIT ||
249 group == MODP_1024_BIT)
250 {
251 *allowed = TRUE;
252 }
253 *allowed = FALSE;
254 return SUCCESS;
255 }
256
257
258 /**
259 * Implements function destroy of configuration_t.
260 * See #configuration_s.destroy for description.
261 */
262 static status_t destroy(private_configuration_manager_t *this)
263 {
264 allocator_free(this);
265 return SUCCESS;
266 }
267
268 /*
269 * Described in header-file
270 */
271 configuration_manager_t *configuration_manager_create()
272 {
273 private_configuration_manager_t *this = allocator_alloc_thing(private_configuration_manager_t);
274 if (this == NULL)
275 {
276 return NULL;
277 }
278
279 /* public functions */
280 this->public.destroy = (status_t(*)(configuration_manager_t*))destroy;
281 this->public.get_remote_host = (status_t(*)(configuration_manager_t*,char*,host_t**))get_remote_host;
282 this->public.get_local_host = (status_t(*)(configuration_manager_t*,char*,host_t**))get_local_host;
283 this->public.get_dh_group_number = (status_t(*)(configuration_manager_t*,char*,u_int16_t *, u_int16_t))get_dh_group_number;
284 this->public.get_proposals_for_host = (status_t(*)(configuration_manager_t*,host_t*,linked_list_iterator_t*))get_proposals_for_host;
285 this->public.select_proposals_for_host = (status_t(*)(configuration_manager_t*,host_t*,linked_list_iterator_t*,linked_list_iterator_t*))select_proposals_for_host;
286 this->public.is_dh_group_allowed_for_host = (status_t(*)(configuration_manager_t*,host_t*,diffie_hellman_group_t,bool*)) is_dh_group_allowed_for_host;
287
288 return (&this->public);
289 }