vici: Properly add CRLs to the credential set
[strongswan.git] / src / libcharon / plugins / vici / vici_cred.c
1 /*
2 * Copyright (C) 2014 Martin Willi
3 * Copyright (C) 2014 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 "vici_cred.h"
17 #include "vici_builder.h"
18
19 #include <credentials/sets/mem_cred.h>
20 #include <credentials/certificates/ac.h>
21 #include <credentials/certificates/crl.h>
22 #include <credentials/certificates/x509.h>
23
24 typedef struct private_vici_cred_t private_vici_cred_t;
25
26 /**
27 * Private data of an vici_cred_t object.
28 */
29 struct private_vici_cred_t {
30
31 /**
32 * Public vici_cred_t interface.
33 */
34 vici_cred_t public;
35
36 /**
37 * Dispatcher
38 */
39 vici_dispatcher_t *dispatcher;
40
41 /**
42 * credentials
43 */
44 mem_cred_t *creds;
45 };
46
47 /**
48 * Create a (error) reply message
49 */
50 static vici_message_t* create_reply(char *fmt, ...)
51 {
52 vici_builder_t *builder;
53 va_list args;
54
55 builder = vici_builder_create();
56 builder->add_kv(builder, "success", fmt ? "no" : "yes");
57 if (fmt)
58 {
59 va_start(args, fmt);
60 builder->vadd_kv(builder, "errmsg", fmt, args);
61 va_end(args);
62 }
63 return builder->finalize(builder);
64 }
65
66 CALLBACK(load_cert, vici_message_t*,
67 private_vici_cred_t *this, char *name, u_int id, vici_message_t *message)
68 {
69 certificate_type_t type;
70 x509_flag_t required_flags = 0, additional_flags = 0;
71 certificate_t *cert;
72 x509_t *x509;
73 chunk_t data;
74 char *str;
75
76 str = message->get_str(message, NULL, "type");
77 if (!str)
78 {
79 return create_reply("certificate type missing");
80 }
81 if (strcaseeq(str, "x509"))
82 {
83 type = CERT_X509;
84 }
85 else if (strcaseeq(str, "x509ca"))
86 {
87 type = CERT_X509;
88 required_flags = X509_CA;
89 }
90 else if (strcaseeq(str, "x509aa"))
91 {
92 type = CERT_X509;
93 additional_flags = X509_AA;
94 }
95 else if (strcaseeq(str, "x509crl"))
96 {
97 type = CERT_X509_CRL;
98 }
99 else if (strcaseeq(str, "x509ac"))
100 {
101 type = CERT_X509_AC;
102 }
103 else
104 {
105 return create_reply("invalid certificate type: %s", str);
106 }
107 data = message->get_value(message, chunk_empty, "data");
108 if (!data.len)
109 {
110 return create_reply("certificate data missing");
111 }
112 cert = lib->creds->create(lib->creds, CRED_CERTIFICATE, type,
113 BUILD_BLOB_PEM, data,
114 BUILD_X509_FLAG, additional_flags,
115 BUILD_END);
116 if (!cert)
117 {
118 return create_reply("parsing %N certificate failed",
119 certificate_type_names, type);
120 }
121 if (cert->get_type(cert) == CERT_X509)
122 {
123 x509 = (x509_t*)cert;
124
125 if ((required_flags & x509->get_flags(x509)) != required_flags)
126 {
127 cert->destroy(cert);
128 return create_reply("certificate misses required flag, rejected");
129 }
130 }
131
132 DBG1(DBG_CFG, "loaded certificate '%Y'", cert->get_subject(cert));
133
134 if (type == CERT_X509_CRL)
135 {
136 this->creds->add_crl(this->creds, (crl_t*)cert);
137 }
138 else
139 {
140 this->creds->add_cert(this->creds, TRUE, cert);
141 }
142 return create_reply(NULL);
143 }
144
145 CALLBACK(load_key, vici_message_t*,
146 private_vici_cred_t *this, char *name, u_int id, vici_message_t *message)
147 {
148 key_type_t type;
149 private_key_t *key;
150 chunk_t data;
151 char *str;
152
153 str = message->get_str(message, NULL, "type");
154 if (!str)
155 {
156 return create_reply("key type missing");
157 }
158 if (strcaseeq(str, "any"))
159 {
160 type = KEY_ANY;
161 }
162 else if (strcaseeq(str, "rsa"))
163 {
164 type = KEY_RSA;
165 }
166 else if (strcaseeq(str, "ecdsa"))
167 {
168 type = KEY_ECDSA;
169 }
170 else
171 {
172 return create_reply("invalid key type: %s", str);
173 }
174 data = message->get_value(message, chunk_empty, "data");
175 if (!data.len)
176 {
177 return create_reply("key data missing");
178 }
179 key = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, type,
180 BUILD_BLOB_PEM, data, BUILD_END);
181 if (!key)
182 {
183 return create_reply("parsing %N private key failed",
184 key_type_names, type);
185 }
186
187 DBG1(DBG_CFG, "loaded %N private key", key_type_names, type);
188
189 this->creds->add_key(this->creds, key);
190
191 return create_reply(NULL);
192 }
193
194 CALLBACK(shared_owners, bool,
195 linked_list_t *owners, vici_message_t *message, char *name, chunk_t value)
196 {
197 if (streq(name, "owners"))
198 {
199 char buf[256];
200
201 if (!vici_stringify(value, buf, sizeof(buf)))
202 {
203 return FALSE;
204 }
205 owners->insert_last(owners, identification_create_from_string(buf));
206 }
207 return TRUE;
208 }
209
210 CALLBACK(load_shared, vici_message_t*,
211 private_vici_cred_t *this, char *name, u_int id, vici_message_t *message)
212 {
213 shared_key_type_t type;
214 linked_list_t *owners;
215 chunk_t data;
216 char *str, buf[512] = "";
217 enumerator_t *enumerator;
218 identification_t *owner;
219 int len;
220
221 str = message->get_str(message, NULL, "type");
222 if (!str)
223 {
224 return create_reply("shared key type missing");
225 }
226 if (strcaseeq(str, "ike"))
227 {
228 type = SHARED_IKE;
229 }
230 else if (strcaseeq(str, "eap") || streq(str, "xauth"))
231 {
232 type = SHARED_EAP;
233 }
234 else
235 {
236 return create_reply("invalid shared key type: %s", str);
237 }
238 data = message->get_value(message, chunk_empty, "data");
239 if (!data.len)
240 {
241 return create_reply("shared key data missing");
242 }
243
244 owners = linked_list_create();
245 if (!message->parse(message, NULL, NULL, NULL, shared_owners, owners))
246 {
247 owners->destroy_offset(owners, offsetof(identification_t, destroy));
248 return create_reply("parsing shared key owners failed");
249 }
250 if (owners->get_count(owners) == 0)
251 {
252 owners->insert_last(owners, identification_create_from_string("%any"));
253 }
254
255 enumerator = owners->create_enumerator(owners);
256 while (enumerator->enumerate(enumerator, &owner))
257 {
258 len = strlen(buf);
259 if (len < sizeof(buf))
260 {
261 snprintf(buf + len, sizeof(buf) - len, "%s'%Y'",
262 len ? ", " : "", owner);
263 }
264 }
265 enumerator->destroy(enumerator);
266
267 DBG1(DBG_CFG, "loaded %N shared key for: %s",
268 shared_key_type_names, type, buf);
269
270 this->creds->add_shared_list(this->creds,
271 shared_key_create(type, chunk_clone(data)), owners);
272
273 return create_reply(NULL);
274 }
275
276 CALLBACK(clear_creds, vici_message_t*,
277 private_vici_cred_t *this, char *name, u_int id, vici_message_t *message)
278 {
279 this->creds->clear(this->creds);
280 lib->credmgr->flush_cache(lib->credmgr, CERT_ANY);
281
282 return create_reply(NULL);
283 }
284
285 static void manage_command(private_vici_cred_t *this,
286 char *name, vici_command_cb_t cb, bool reg)
287 {
288 this->dispatcher->manage_command(this->dispatcher, name,
289 reg ? cb : NULL, this);
290 }
291
292 /**
293 * (Un-)register dispatcher functions
294 */
295 static void manage_commands(private_vici_cred_t *this, bool reg)
296 {
297 manage_command(this, "clear-creds", clear_creds, reg);
298 manage_command(this, "load-cert", load_cert, reg);
299 manage_command(this, "load-key", load_key, reg);
300 manage_command(this, "load-shared", load_shared, reg);
301 }
302
303 METHOD(vici_cred_t, add_cert, certificate_t*,
304 private_vici_cred_t *this, certificate_t *cert)
305 {
306 return this->creds->get_cert_ref(this->creds, cert);
307 }
308
309 METHOD(vici_cred_t, destroy, void,
310 private_vici_cred_t *this)
311 {
312 manage_commands(this, FALSE);
313
314 lib->credmgr->remove_set(lib->credmgr, &this->creds->set);
315 this->creds->destroy(this->creds);
316 free(this);
317 }
318
319 /**
320 * See header
321 */
322 vici_cred_t *vici_cred_create(vici_dispatcher_t *dispatcher)
323 {
324 private_vici_cred_t *this;
325
326 INIT(this,
327 .public = {
328 .add_cert = _add_cert,
329 .destroy = _destroy,
330 },
331 .dispatcher = dispatcher,
332 .creds = mem_cred_create(),
333 );
334
335 lib->credmgr->add_set(lib->credmgr, &this->creds->set);
336
337 manage_commands(this, TRUE);
338
339 return &this->public;
340 }