Added key strength constraints support to conftest
[strongswan.git] / src / conftest / config.c
1 /*
2 * Copyright (C) 2010 Martin Willi
3 * Copyright (C) 2010 revosec AG
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "config.h"
17
18 #include <daemon.h>
19 #include <conftest.h>
20
21 typedef struct private_config_t private_config_t;
22
23 /**
24 * Private data of an config_t object.
25 */
26 struct private_config_t {
27
28 /**
29 * Public config_t interface.
30 */
31 config_t public;
32
33 /**
34 * List of loaded peer configs
35 */
36 linked_list_t *configs;
37 };
38
39 /**
40 * filter function for ike configs
41 */
42 static bool ike_filter(void *data, peer_cfg_t **in, ike_cfg_t **out)
43 {
44 *out = (*in)->get_ike_cfg(*in);
45 return TRUE;
46 }
47
48 METHOD(backend_t, create_ike_cfg_enumerator, enumerator_t*,
49 private_config_t *this, host_t *me, host_t *other)
50 {
51
52 return enumerator_create_filter(
53 this->configs->create_enumerator(this->configs),
54 (void*)ike_filter, NULL, NULL);
55 }
56
57 METHOD(backend_t, create_peer_cfg_enumerator, enumerator_t*,
58 private_config_t *this, identification_t *me, identification_t *other)
59 {
60 return this->configs->create_enumerator(this->configs);
61 }
62
63 METHOD(backend_t, get_peer_cfg_by_name, peer_cfg_t*,
64 private_config_t *this, char *name)
65 {
66 enumerator_t *e1, *e2;
67 peer_cfg_t *current, *found = NULL;
68 child_cfg_t *child;
69
70 e1 = this->configs->create_enumerator(this->configs);
71 while (e1->enumerate(e1, &current))
72 {
73 e2 = current->create_child_cfg_enumerator(current);
74 while (e2->enumerate(e2, &child))
75 {
76 if (streq(child->get_name(child), name))
77 {
78 found = current;
79 found->get_ref(found);
80 break;
81 }
82 }
83 e2->destroy(e2);
84 if (found)
85 {
86 break;
87 }
88 }
89 e1->destroy(e1);
90 return found;
91 }
92
93 /**
94 * Load IKE config for a given section name
95 */
96 static ike_cfg_t *load_ike_config(private_config_t *this,
97 settings_t *settings, char *config)
98 {
99 enumerator_t *enumerator;
100 ike_cfg_t *ike_cfg;
101 proposal_t *proposal;
102 char *token;
103
104 ike_cfg = ike_cfg_create(TRUE,
105 settings->get_bool(settings, "configs.%s.fake_nat", FALSE, config),
106 settings->get_str(settings, "configs.%s.lhost", "%any", config),
107 settings->get_int(settings, "configs.%s.lport", 500, config),
108 settings->get_str(settings, "configs.%s.rhost", "%any", config),
109 settings->get_int(settings, "configs.%s.rport", 500, config));
110 token = settings->get_str(settings, "configs.%s.proposal", NULL, config);
111 if (token)
112 {
113 enumerator = enumerator_create_token(token, ",", " ");
114 while (enumerator->enumerate(enumerator, &token))
115 {
116 proposal = proposal_create_from_string(PROTO_IKE, token);
117 if (proposal)
118 {
119 ike_cfg->add_proposal(ike_cfg, proposal);
120 }
121 else
122 {
123 DBG1(DBG_CFG, "parsing proposal '%s' failed, skipped", token);
124 }
125 }
126 enumerator->destroy(enumerator);
127 }
128 else
129 {
130 ike_cfg->add_proposal(ike_cfg, proposal_create_default(PROTO_IKE));
131 }
132 return ike_cfg;
133 }
134 /**
135 * Load CHILD config for given section names
136 */
137 static child_cfg_t *load_child_config(private_config_t *this,
138 settings_t *settings, char *config, char *child)
139 {
140 child_cfg_t *child_cfg;
141 lifetime_cfg_t lifetime = {};
142 enumerator_t *enumerator;
143 proposal_t *proposal;
144 traffic_selector_t *ts;
145 ipsec_mode_t mode = MODE_TUNNEL;
146 host_t *net;
147 char *token;
148 int bits;
149
150 if (settings->get_bool(settings, "configs.%s.%s.transport",
151 FALSE, config, child))
152 {
153 mode = MODE_TRANSPORT;
154 }
155 child_cfg = child_cfg_create(child, &lifetime, NULL, FALSE, mode,
156 ACTION_NONE, ACTION_NONE, FALSE, 0, 0, NULL, NULL);
157
158 token = settings->get_str(settings, "configs.%s.%s.proposal",
159 NULL, config, child);
160 if (token)
161 {
162 enumerator = enumerator_create_token(token, ",", " ");
163 while (enumerator->enumerate(enumerator, &token))
164 {
165 proposal = proposal_create_from_string(PROTO_ESP, token);
166 if (proposal)
167 {
168 child_cfg->add_proposal(child_cfg, proposal);
169 }
170 else
171 {
172 DBG1(DBG_CFG, "parsing proposal '%s' failed, skipped", token);
173 }
174 }
175 enumerator->destroy(enumerator);
176 }
177 else
178 {
179 child_cfg->add_proposal(child_cfg, proposal_create_default(PROTO_ESP));
180 }
181
182 token = settings->get_str(settings, "configs.%s.%s.lts", NULL, config);
183 if (token)
184 {
185 enumerator = enumerator_create_token(token, ",", " ");
186 while (enumerator->enumerate(enumerator, &token))
187 {
188 net = host_create_from_subnet(token, &bits);
189 if (net)
190 {
191 ts = traffic_selector_create_from_subnet(net, bits, 0, 0);
192 child_cfg->add_traffic_selector(child_cfg, TRUE, ts);
193 }
194 else
195 {
196 DBG1(DBG_CFG, "invalid local ts: %s, skipped", token);
197 }
198 }
199 enumerator->destroy(enumerator);
200 }
201 else
202 {
203 ts = traffic_selector_create_dynamic(0, 0, 65535);
204 child_cfg->add_traffic_selector(child_cfg, TRUE, ts);
205 }
206
207 token = settings->get_str(settings, "configs.%s.%s.rts", NULL, config);
208 if (token)
209 {
210 enumerator = enumerator_create_token(token, ",", " ");
211 while (enumerator->enumerate(enumerator, &token))
212 {
213 net = host_create_from_subnet(token, &bits);
214 if (net)
215 {
216 ts = traffic_selector_create_from_subnet(net, bits, 0, 0);
217 child_cfg->add_traffic_selector(child_cfg, FALSE, ts);
218 }
219 else
220 {
221 DBG1(DBG_CFG, "invalid remote ts: %s, skipped", token);
222 }
223 }
224 enumerator->destroy(enumerator);
225 }
226 else
227 {
228 ts = traffic_selector_create_dynamic(0, 0, 65535);
229 child_cfg->add_traffic_selector(child_cfg, FALSE, ts);
230 }
231 return child_cfg;
232 }
233
234 /**
235 * Load peer config for a given section name
236 */
237 static peer_cfg_t *load_peer_config(private_config_t *this,
238 settings_t *settings, char *config)
239 {
240 ike_cfg_t *ike_cfg;
241 peer_cfg_t *peer_cfg;
242 auth_cfg_t *auth;
243 child_cfg_t *child_cfg;
244 enumerator_t *enumerator;
245 identification_t *lid, *rid;
246 char *child;
247 uintptr_t strength;
248
249 ike_cfg = load_ike_config(this, settings, config);
250 peer_cfg = peer_cfg_create(config, 2, ike_cfg, CERT_ALWAYS_SEND,
251 UNIQUE_NO, 1, 0, 0, 0, 0, FALSE, 0,
252 NULL, NULL, FALSE, NULL, NULL);
253
254 auth = auth_cfg_create();
255 auth->add(auth, AUTH_RULE_AUTH_CLASS, AUTH_CLASS_PUBKEY);
256 lid = identification_create_from_string(
257 settings->get_str(settings, "configs.%s.lid", "%any", config));
258 auth->add(auth, AUTH_RULE_IDENTITY, lid);
259 peer_cfg->add_auth_cfg(peer_cfg, auth, TRUE);
260
261 auth = auth_cfg_create();
262 auth->add(auth, AUTH_RULE_AUTH_CLASS, AUTH_CLASS_PUBKEY);
263 rid = identification_create_from_string(
264 settings->get_str(settings, "configs.%s.rid", "%any", config));
265 strength = settings->get_int(settings, "configs.%s.rsa_strength", 0);
266 if (strength)
267 {
268 auth->add(auth, AUTH_RULE_RSA_STRENGTH, strength);
269 }
270 strength = settings->get_int(settings, "configs.%s.ecdsa_strength", 0);
271 if (strength)
272 {
273 auth->add(auth, AUTH_RULE_ECDSA_STRENGTH, strength);
274 }
275 auth->add(auth, AUTH_RULE_IDENTITY, rid);
276 peer_cfg->add_auth_cfg(peer_cfg, auth, FALSE);
277
278 DBG1(DBG_CFG, "loaded config %s: %Y - %Y", config, lid, rid);
279
280 enumerator = settings->create_section_enumerator(settings,
281 "configs.%s", config);
282 while (enumerator->enumerate(enumerator, &child))
283 {
284 child_cfg = load_child_config(this, settings, config, child);
285 peer_cfg->add_child_cfg(peer_cfg, child_cfg);
286 }
287 enumerator->destroy(enumerator);
288 return peer_cfg;
289 }
290
291 METHOD(config_t, load, void,
292 private_config_t *this, settings_t *settings)
293 {
294 enumerator_t *enumerator;
295 char *config;
296
297 enumerator = settings->create_section_enumerator(settings, "configs");
298 while (enumerator->enumerate(enumerator, &config))
299 {
300 this->configs->insert_last(this->configs,
301 load_peer_config(this, settings, config));
302 }
303 enumerator->destroy(enumerator);
304 }
305
306 METHOD(config_t, destroy, void,
307 private_config_t *this)
308 {
309 this->configs->destroy_offset(this->configs, offsetof(peer_cfg_t, destroy));
310 free(this);
311 }
312
313 /**
314 * See header
315 */
316 config_t *config_create()
317 {
318 private_config_t *this;
319
320 INIT(this,
321 .public = {
322 .backend = {
323 .create_ike_cfg_enumerator = _create_ike_cfg_enumerator,
324 .create_peer_cfg_enumerator = _create_peer_cfg_enumerator,
325 .get_peer_cfg_by_name = _get_peer_cfg_by_name,
326 },
327 .load = _load,
328 .destroy = _destroy,
329 },
330 .configs = linked_list_create(),
331 );
332
333 return &this->public;
334 }