fab06e6c5910d2a7bc54e7cdf0ac287e65e20546
[strongswan.git] / src / charon / plugins / stroke / stroke_ca.c
1 /*
2 * Copyright (C) 2008 Tobias Brunner
3 * Copyright (C) 2008 Martin Willi
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include "stroke_ca.h"
18 #include "stroke_cred.h"
19
20 #include <utils/mutex.h>
21 #include <utils/linked_list.h>
22 #include <crypto/hashers/hasher.h>
23
24 #include <daemon.h>
25
26 typedef struct private_stroke_ca_t private_stroke_ca_t;
27
28 /**
29 * private data of stroke_ca
30 */
31 struct private_stroke_ca_t {
32
33 /**
34 * public functions
35 */
36 stroke_ca_t public;
37
38 /**
39 * read-write lock to lists
40 */
41 rwlock_t *lock;
42
43 /**
44 * list of starters CA sections and its certificates (ca_section_t)
45 */
46 linked_list_t *sections;
47
48 /**
49 * stroke credentials, stores our CA certificates
50 */
51 stroke_cred_t *cred;
52 };
53
54 typedef struct ca_section_t ca_section_t;
55
56 /**
57 * loaded ipsec.conf CA sections
58 */
59 struct ca_section_t {
60
61 /**
62 * name of the CA section
63 */
64 char *name;
65
66 /**
67 * reference to cert in trusted_credential_t
68 */
69 certificate_t *cert;
70
71 /**
72 * CRL URIs
73 */
74 linked_list_t *crl;
75
76 /**
77 * OCSP URIs
78 */
79 linked_list_t *ocsp;
80
81 /**
82 * Hashes of certificates issued by this CA
83 */
84 linked_list_t *hashes;
85
86 /**
87 * Base URI used for certificates from this CA
88 */
89 char *certuribase;
90 };
91
92 /**
93 * create a new CA section
94 */
95 static ca_section_t *ca_section_create(char *name, certificate_t *cert)
96 {
97 ca_section_t *ca = malloc_thing(ca_section_t);
98
99 ca->name = strdup(name);
100 ca->crl = linked_list_create();
101 ca->ocsp = linked_list_create();
102 ca->cert = cert;
103 ca->hashes = linked_list_create();
104 ca->certuribase = NULL;
105 return ca;
106 }
107
108 /**
109 * destroy a ca section entry
110 */
111 static void ca_section_destroy(ca_section_t *this)
112 {
113 this->crl->destroy_function(this->crl, free);
114 this->ocsp->destroy_function(this->ocsp, free);
115 this->hashes->destroy_offset(this->hashes, offsetof(identification_t, destroy));
116 free(this->certuribase);
117 free(this->name);
118 free(this);
119 }
120
121 /**
122 * data to pass to create_inner_cdp
123 */
124 typedef struct {
125 private_stroke_ca_t *this;
126 certificate_type_t type;
127 identification_t *id;
128 } cdp_data_t;
129
130 /**
131 * destroy cdp enumerator data and unlock list
132 */
133 static void cdp_data_destroy(cdp_data_t *data)
134 {
135 data->this->lock->unlock(data->this->lock);
136 free(data);
137 }
138
139 /**
140 * inner enumerator constructor for CDP URIs
141 */
142 static enumerator_t *create_inner_cdp(ca_section_t *section, cdp_data_t *data)
143 {
144 public_key_t *public;
145 identification_t *keyid;
146 enumerator_t *enumerator = NULL;
147 linked_list_t *list;
148
149 if (data->type == CERT_X509_OCSP_RESPONSE)
150 {
151 list = section->ocsp;
152 }
153 else
154 {
155 list = section->crl;
156 }
157
158 public = section->cert->get_public_key(section->cert);
159 if (public)
160 {
161 if (!data->id)
162 {
163 enumerator = list->create_enumerator(list);
164 }
165 else
166 {
167 keyid = public->get_id(public, data->id->get_type(data->id));
168 if (keyid && keyid->matches(keyid, data->id))
169 {
170 enumerator = list->create_enumerator(list);
171 }
172 }
173 public->destroy(public);
174 }
175 return enumerator;
176 }
177
178 /**
179 * inner enumerator constructor for "Hash and URL"
180 */
181 static enumerator_t *create_inner_cdp_hashandurl(ca_section_t *section, cdp_data_t *data)
182 {
183 enumerator_t *enumerator = NULL, *hash_enum;
184 identification_t *current;
185
186 if (!data->id || !section->certuribase)
187 {
188 return NULL;
189 }
190
191 hash_enum = section->hashes->create_enumerator(section->hashes);
192 while (hash_enum->enumerate(hash_enum, &current))
193 {
194 if (current->matches(current, data->id))
195 {
196 char *url, *hash;
197
198 url = malloc(strlen(section->certuribase) + 40 + 1);
199 strcpy(url, section->certuribase);
200 hash = chunk_to_hex(current->get_encoding(current), NULL, FALSE).ptr;
201 strncat(url, hash, 40);
202 free(hash);
203
204 enumerator = enumerator_create_single(url, free);
205 break;
206 }
207 }
208 hash_enum->destroy(hash_enum);
209 return enumerator;
210 }
211
212 /**
213 * Implementation of credential_set_t.create_cdp_enumerator.
214 */
215 static enumerator_t *create_cdp_enumerator(private_stroke_ca_t *this,
216 certificate_type_t type, identification_t *id)
217 {
218 cdp_data_t *data;
219
220 switch (type)
221 { /* we serve CRLs, OCSP responders and URLs for "Hash and URL" */
222 case CERT_X509:
223 case CERT_X509_CRL:
224 case CERT_X509_OCSP_RESPONSE:
225 case CERT_ANY:
226 break;
227 default:
228 return NULL;
229 }
230 data = malloc_thing(cdp_data_t);
231 data->this = this;
232 data->type = type;
233 data->id = id;
234
235 this->lock->read_lock(this->lock);
236 return enumerator_create_nested(this->sections->create_enumerator(this->sections),
237 (type == CERT_X509) ? (void*)create_inner_cdp_hashandurl : (void*)create_inner_cdp,
238 data, (void*)cdp_data_destroy);
239 }
240 /**
241 * Implementation of stroke_ca_t.add.
242 */
243 static void add(private_stroke_ca_t *this, stroke_msg_t *msg)
244 {
245 certificate_t *cert;
246 ca_section_t *ca;
247
248 if (msg->add_ca.cacert == NULL)
249 {
250 DBG1(DBG_CFG, "missing cacert parameter");
251 return;
252 }
253 cert = this->cred->load_ca(this->cred, msg->add_ca.cacert);
254 if (cert)
255 {
256 ca = ca_section_create(msg->add_ca.name, cert);
257 if (msg->add_ca.crluri)
258 {
259 ca->crl->insert_last(ca->crl, strdup(msg->add_ca.crluri));
260 }
261 if (msg->add_ca.crluri2)
262 {
263 ca->crl->insert_last(ca->crl, strdup(msg->add_ca.crluri2));
264 }
265 if (msg->add_ca.ocspuri)
266 {
267 ca->ocsp->insert_last(ca->ocsp, strdup(msg->add_ca.ocspuri));
268 }
269 if (msg->add_ca.ocspuri2)
270 {
271 ca->ocsp->insert_last(ca->ocsp, strdup(msg->add_ca.ocspuri2));
272 }
273 if (msg->add_ca.certuribase)
274 {
275 ca->certuribase = strdup(msg->add_ca.certuribase);
276 }
277 this->lock->write_lock(this->lock);
278 this->sections->insert_last(this->sections, ca);
279 this->lock->unlock(this->lock);
280 DBG1(DBG_CFG, "added ca '%s'", msg->add_ca.name);
281 }
282 }
283
284 /**
285 * Implementation of stroke_ca_t.del.
286 */
287 static void del(private_stroke_ca_t *this, stroke_msg_t *msg)
288 {
289 enumerator_t *enumerator;
290 ca_section_t *ca = NULL;
291
292 this->lock->write_lock(this->lock);
293 enumerator = this->sections->create_enumerator(this->sections);
294 while (enumerator->enumerate(enumerator, &ca))
295 {
296 if (streq(ca->name, msg->del_ca.name))
297 {
298 this->sections->remove_at(this->sections, enumerator);
299 break;
300 }
301 ca = NULL;
302 }
303 enumerator->destroy(enumerator);
304 this->lock->unlock(this->lock);
305 if (ca == NULL)
306 {
307 DBG1(DBG_CFG, "no ca named '%s' found\n", msg->del_ca.name);
308 return;
309 }
310 ca_section_destroy(ca);
311 /* TODO: flush cached certs */
312 }
313
314 /**
315 * list crl or ocsp URIs
316 */
317 static void list_uris(linked_list_t *list, char *label, FILE *out)
318 {
319 bool first = TRUE;
320 char *uri;
321 enumerator_t *enumerator;
322
323 enumerator = list->create_enumerator(list);
324 while (enumerator->enumerate(enumerator, (void**)&uri))
325 {
326 if (first)
327 {
328 fprintf(out, label);
329 first = FALSE;
330 }
331 else
332 {
333 fprintf(out, " ");
334 }
335 fprintf(out, "'%s'\n", uri);
336 }
337 enumerator->destroy(enumerator);
338 }
339
340 /**
341 * Implementation of stroke_ca_t.check_for_hash_and_url.
342 */
343 static void check_for_hash_and_url(private_stroke_ca_t *this, certificate_t* cert)
344 {
345 ca_section_t *section;
346 enumerator_t *enumerator;
347
348 hasher_t *hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA1);
349 if (hasher == NULL)
350 {
351 DBG1(DBG_IKE, "unable to use hash-and-url: sha1 not supported");
352 return;
353 }
354
355 this->lock->write_lock(this->lock);
356 enumerator = this->sections->create_enumerator(this->sections);
357 while (enumerator->enumerate(enumerator, (void**)&section))
358 {
359 if (section->certuribase && cert->issued_by(cert, section->cert))
360 {
361 chunk_t hash, encoded = cert->get_encoding(cert);
362 hasher->allocate_hash(hasher, encoded, &hash);
363 section->hashes->insert_last(section->hashes,
364 identification_create_from_encoding(ID_CERT_DER_SHA1, hash));
365 chunk_free(&hash);
366 chunk_free(&encoded);
367 break;
368 }
369 }
370 enumerator->destroy(enumerator);
371 this->lock->unlock(this->lock);
372
373 hasher->destroy(hasher);
374 }
375
376 /**
377 * Implementation of stroke_ca_t.list.
378 */
379 static void list(private_stroke_ca_t *this, stroke_msg_t *msg, FILE *out)
380 {
381 bool first = TRUE;
382 ca_section_t *section;
383 enumerator_t *enumerator;
384
385 this->lock->read_lock(this->lock);
386 enumerator = this->sections->create_enumerator(this->sections);
387 while (enumerator->enumerate(enumerator, (void**)&section))
388 {
389 certificate_t *cert = section->cert;
390 public_key_t *public = cert->get_public_key(cert);
391
392 if (first)
393 {
394 fprintf(out, "\n");
395 fprintf(out, "List of CA Information Sections:\n");
396 first = FALSE;
397 }
398 fprintf(out, "\n");
399 fprintf(out, " authname: \"%Y\"\n", cert->get_subject(cert));
400
401 /* list authkey and keyid */
402 if (public)
403 {
404 fprintf(out, " authkey: %Y\n",
405 public->get_id(public, ID_PUBKEY_SHA1));
406 fprintf(out, " keyid: %Y\n",
407 public->get_id(public, ID_PUBKEY_INFO_SHA1));
408 public->destroy(public);
409 }
410 list_uris(section->crl, " crluris: ", out);
411 list_uris(section->ocsp, " ocspuris: ", out);
412 if (section->certuribase)
413 {
414 fprintf(out, " certuribase: '%s'\n", section->certuribase);
415 }
416 }
417 enumerator->destroy(enumerator);
418 this->lock->unlock(this->lock);
419 }
420
421 /**
422 * Implementation of stroke_ca_t.destroy
423 */
424 static void destroy(private_stroke_ca_t *this)
425 {
426 this->sections->destroy_function(this->sections, (void*)ca_section_destroy);
427 this->lock->destroy(this->lock);
428 free(this);
429 }
430
431 /*
432 * see header file
433 */
434 stroke_ca_t *stroke_ca_create(stroke_cred_t *cred)
435 {
436 private_stroke_ca_t *this = malloc_thing(private_stroke_ca_t);
437
438 this->public.set.create_private_enumerator = (void*)return_null;
439 this->public.set.create_cert_enumerator = (void*)return_null;
440 this->public.set.create_shared_enumerator = (void*)return_null;
441 this->public.set.create_cdp_enumerator = (void*)create_cdp_enumerator;
442 this->public.set.cache_cert = (void*)nop;
443 this->public.add = (void(*)(stroke_ca_t*, stroke_msg_t *msg))add;
444 this->public.del = (void(*)(stroke_ca_t*, stroke_msg_t *msg))del;
445 this->public.list = (void(*)(stroke_ca_t*, stroke_msg_t *msg, FILE *out))list;
446 this->public.check_for_hash_and_url = (void(*)(stroke_ca_t*, certificate_t*))check_for_hash_and_url;
447 this->public.destroy = (void(*)(stroke_ca_t*))destroy;
448
449 this->sections = linked_list_create();
450 this->lock = rwlock_create(RWLOCK_DEFAULT);
451 this->cred = cred;
452
453 return &this->public;
454 }
455