added _GNU_SOURCE and limits.h to build against glibc-2.8
[strongswan.git] / src / charon / plugins / stroke / stroke_cred.c
1 /*
2 * Copyright (C) 2008 Martin Willi
3 * Hochschule fuer Technik Rapperswil
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 * $Id$
16 */
17
18 #include "stroke_cred.h"
19 #include "stroke_shared_key.h"
20
21 #include <sys/stat.h>
22 #include <limits.h>
23
24 #include <credentials/certificates/x509.h>
25 #include <credentials/certificates/crl.h>
26 #include <credentials/certificates/ac.h>
27 #include <utils/linked_list.h>
28 #include <utils/mutex.h>
29 #include <utils/lexparser.h>
30 #include <asn1/pem.h>
31 #include <daemon.h>
32
33 /* configuration directories and files */
34 #define CONFIG_DIR IPSEC_CONFDIR
35 #define IPSEC_D_DIR CONFIG_DIR "/ipsec.d"
36 #define PRIVATE_KEY_DIR IPSEC_D_DIR "/private"
37 #define CERTIFICATE_DIR IPSEC_D_DIR "/certs"
38 #define CA_CERTIFICATE_DIR IPSEC_D_DIR "/cacerts"
39 #define AA_CERTIFICATE_DIR IPSEC_D_DIR "/aacerts"
40 #define ATTR_CERTIFICATE_DIR IPSEC_D_DIR "/acerts"
41 #define OCSP_CERTIFICATE_DIR IPSEC_D_DIR "/ocspcerts"
42 #define CRL_DIR IPSEC_D_DIR "/crls"
43 #define SECRETS_FILE CONFIG_DIR "/ipsec.secrets"
44
45 typedef struct private_stroke_cred_t private_stroke_cred_t;
46
47 /**
48 * private data of stroke_cred
49 */
50 struct private_stroke_cred_t {
51
52 /**
53 * public functions
54 */
55 stroke_cred_t public;
56
57 /**
58 * list of trusted peer/signer/CA certificates (certificate_t)
59 */
60 linked_list_t *certs;
61
62 /**
63 * list of shared secrets (private_shared_key_t)
64 */
65 linked_list_t *shared;
66
67 /**
68 * list of private keys (private_key_t)
69 */
70 linked_list_t *private;
71
72 /**
73 * mutex to lock lists above
74 */
75 mutex_t *mutex;
76
77 /**
78 * cache CRLs to disk?
79 */
80 bool cachecrl;
81 };
82
83 /**
84 * data to pass to various filters
85 */
86 typedef struct {
87 private_stroke_cred_t *this;
88 identification_t *id;
89 } id_data_t;
90
91 /**
92 * destroy id enumerator data and unlock list
93 */
94 static void id_data_destroy(id_data_t *data)
95 {
96 data->this->mutex->unlock(data->this->mutex);
97 free(data);
98 }
99
100 /**
101 * filter function for private key enumerator
102 */
103 static bool private_filter(id_data_t *data,
104 private_key_t **in, private_key_t **out)
105 {
106 identification_t *candidate;
107
108 if (data->id == NULL)
109 {
110 *out = *in;
111 return TRUE;
112 }
113 candidate = (*in)->get_id(*in, data->id->get_type(data->id));
114 if (candidate && data->id->equals(data->id, candidate))
115 {
116 *out = *in;
117 return TRUE;
118 }
119 return FALSE;
120 }
121
122 /**
123 * Implements credential_set_t.create_private_enumerator
124 */
125 static enumerator_t* create_private_enumerator(private_stroke_cred_t *this,
126 key_type_t type, identification_t *id)
127 {
128 id_data_t *data;
129
130 if (type != KEY_RSA && type != KEY_ANY)
131 { /* we only have RSA keys */
132 return NULL;
133 }
134 data = malloc_thing(id_data_t);
135 data->this = this;
136 data->id = id;
137
138 this->mutex->lock(this->mutex);
139 return enumerator_create_filter(this->private->create_enumerator(this->private),
140 (void*)private_filter, data,
141 (void*)id_data_destroy);
142 }
143
144 /**
145 * filter function for certs enumerator
146 */
147 static bool certs_filter(id_data_t *data, certificate_t **in, certificate_t **out)
148 {
149 public_key_t *public;
150 identification_t *candidate;
151 certificate_t *cert = *in;
152
153 if (cert->get_type(cert) == CERT_X509_CRL)
154 {
155 return FALSE;
156 }
157
158 if (data->id == NULL || cert->has_subject(cert, data->id))
159 {
160 *out = *in;
161 return TRUE;
162 }
163
164 public = (cert)->get_public_key(cert);
165 if (public)
166 {
167 candidate = public->get_id(public, data->id->get_type(data->id));
168 if (candidate && data->id->equals(data->id, candidate))
169 {
170 public->destroy(public);
171 *out = *in;
172 return TRUE;
173 }
174 public->destroy(public);
175 }
176 return FALSE;
177 }
178
179 /**
180 * filter function for crl enumerator
181 */
182 static bool crl_filter(id_data_t *data, certificate_t **in, certificate_t **out)
183 {
184 certificate_t *cert = *in;
185
186 if (cert->get_type(cert) != CERT_X509_CRL)
187 {
188 return FALSE;
189 }
190
191 if (data->id == NULL || cert->has_issuer(cert, data->id))
192 {
193 *out = *in;
194 return TRUE;
195 }
196 return FALSE;
197 }
198
199 /**
200 * Implements credential_set_t.create_cert_enumerator
201 */
202 static enumerator_t* create_cert_enumerator(private_stroke_cred_t *this,
203 certificate_type_t cert, key_type_t key,
204 identification_t *id, bool trusted)
205 {
206 id_data_t *data;
207
208 if (cert == CERT_X509_CRL)
209 {
210 if (trusted)
211 {
212 return NULL;
213 }
214
215 data = malloc_thing(id_data_t);
216 data->this = this;
217 data->id = id;
218
219 this->mutex->lock(this->mutex);
220 return enumerator_create_filter(this->certs->create_enumerator(this->certs),
221 (void*)crl_filter, data,
222 (void*)id_data_destroy);
223 }
224 if (cert != CERT_X509 && cert != CERT_ANY)
225 { /* we only have X509 certificates. TODO: ACs? */
226 return NULL;
227 }
228 if (key != KEY_RSA && key != KEY_ANY)
229 { /* we only have RSA keys */
230 return NULL;
231 }
232 data = malloc_thing(id_data_t);
233 data->this = this;
234 data->id = id;
235
236 this->mutex->lock(this->mutex);
237 return enumerator_create_filter(this->certs->create_enumerator(this->certs),
238 (void*)certs_filter, data,
239 (void*)id_data_destroy);
240 }
241
242 typedef struct {
243 private_stroke_cred_t *this;
244 identification_t *me;
245 identification_t *other;
246 shared_key_type_t type;
247 } shared_data_t;
248
249 /**
250 * free shared key enumerator data and unlock list
251 */
252 static void shared_data_destroy(shared_data_t *data)
253 {
254 data->this->mutex->unlock(data->this->mutex);
255 free(data);
256 }
257
258 /**
259 * filter function for certs enumerator
260 */
261 static bool shared_filter(shared_data_t *data,
262 stroke_shared_key_t **in, shared_key_t **out,
263 void **unused1, id_match_t *me,
264 void **unused2, id_match_t *other)
265 {
266 id_match_t my_match, other_match;
267 stroke_shared_key_t *stroke = *in;
268 shared_key_t *shared = &stroke->shared;
269
270 if (data->type != SHARED_ANY && shared->get_type(shared) != data->type)
271 {
272 return FALSE;
273 }
274
275 my_match = stroke->has_owner(stroke, data->me);
276 other_match = stroke->has_owner(stroke, data->other);
277 if (!my_match && !other_match)
278 {
279 return FALSE;
280 }
281 *out = shared;
282 if (me)
283 {
284 *me = my_match;
285 }
286 if (other)
287 {
288 *other = other_match;
289 }
290 return TRUE;
291 }
292
293 /**
294 * Implements credential_set_t.create_shared_enumerator
295 */
296 static enumerator_t* create_shared_enumerator(private_stroke_cred_t *this,
297 shared_key_type_t type, identification_t *me,
298 identification_t *other)
299 {
300 shared_data_t *data = malloc_thing(shared_data_t);
301
302 data->this = this;
303 data->me = me;
304 data->other = other;
305 data->type = type;
306 this->mutex->lock(this->mutex);
307 return enumerator_create_filter(this->shared->create_enumerator(this->shared),
308 (void*)shared_filter, data,
309 (void*)shared_data_destroy);
310 }
311
312 /**
313 * Add a certificate to chain
314 */
315 static certificate_t* add_cert(private_stroke_cred_t *this, certificate_t *cert)
316 {
317 certificate_t *current;
318 enumerator_t *enumerator;
319 bool new = TRUE;
320
321 this->mutex->lock(this->mutex);
322 enumerator = this->certs->create_enumerator(this->certs);
323 while (enumerator->enumerate(enumerator, (void**)&current))
324 {
325 if (current->equals(current, cert))
326 {
327 /* cert already in queue */
328 cert->destroy(cert);
329 cert = current;
330 new = FALSE;
331 break;
332 }
333 }
334 enumerator->destroy(enumerator);
335
336 if (new)
337 {
338 this->certs->insert_last(this->certs, cert);
339 }
340 this->mutex->unlock(this->mutex);
341 return cert;
342 }
343
344 /**
345 * Implementation of stroke_cred_t.load_ca.
346 */
347 static certificate_t* load_ca(private_stroke_cred_t *this, char *filename)
348 {
349 certificate_t *cert;
350 char path[PATH_MAX];
351
352 if (*filename == '/')
353 {
354 snprintf(path, sizeof(path), "%s", filename);
355 }
356 else
357 {
358 snprintf(path, sizeof(path), "%s/%s", CA_CERTIFICATE_DIR, filename);
359 }
360
361 cert = lib->creds->create(lib->creds,
362 CRED_CERTIFICATE, CERT_X509,
363 BUILD_FROM_FILE, path,
364 BUILD_X509_FLAG, X509_CA,
365 BUILD_END);
366 if (cert)
367 {
368 return (certificate_t*)add_cert(this, cert);
369 }
370 return NULL;
371 }
372
373 /**
374 * Add X.509 CRL to chain
375 */
376 static bool add_crl(private_stroke_cred_t *this, crl_t* crl)
377 {
378 certificate_t *current, *cert = &crl->certificate;
379 enumerator_t *enumerator;
380 bool new = TRUE, found = FALSE;
381
382 this->mutex->lock(this->mutex);
383 enumerator = this->certs->create_enumerator(this->certs);
384 while (enumerator->enumerate(enumerator, (void**)&current))
385 {
386 if (current->get_type(current) == CERT_X509_CRL)
387 {
388 crl_t *crl_c = (crl_t*)current;
389 identification_t *authkey = crl->get_authKeyIdentifier(crl);
390 identification_t *authkey_c = crl_c->get_authKeyIdentifier(crl_c);
391
392 /* if compare authorityKeyIdentifiers if available */
393 if (authkey != NULL && authkey_c != NULL &&
394 authkey->equals(authkey, authkey_c))
395 {
396 found = TRUE;
397 }
398 else
399 {
400 identification_t *issuer = cert->get_issuer(cert);
401 identification_t *issuer_c = current->get_issuer(current);
402
403 /* otherwise compare issuer distinguished names */
404 if (issuer->equals(issuer, issuer_c))
405 {
406 found = TRUE;
407 }
408 }
409 if (found)
410 {
411 new = cert->is_newer(cert, current);
412 if (new)
413 {
414 this->certs->remove_at(this->certs, enumerator);
415 }
416 else
417 {
418 cert->destroy(cert);
419 }
420 break;
421 }
422 }
423 }
424 enumerator->destroy(enumerator);
425
426 if (new)
427 {
428 this->certs->insert_last(this->certs, cert);
429 }
430 this->mutex->unlock(this->mutex);
431 return new;
432 }
433
434 /**
435 * Implementation of stroke_cred_t.load_peer.
436 */
437 static certificate_t* load_peer(private_stroke_cred_t *this, char *filename)
438 {
439 certificate_t *cert;
440 char path[PATH_MAX];
441
442 if (*filename == '/')
443 {
444 snprintf(path, sizeof(path), "%s", filename);
445 }
446 else
447 {
448 snprintf(path, sizeof(path), "%s/%s", CERTIFICATE_DIR, filename);
449 }
450
451 cert = lib->creds->create(lib->creds,
452 CRED_CERTIFICATE, CERT_X509,
453 BUILD_FROM_FILE, path,
454 BUILD_X509_FLAG, 0,
455 BUILD_END);
456 if (cert)
457 {
458 cert = add_cert(this, cert);
459 return cert->get_ref(cert);
460 }
461 return NULL;
462 }
463
464 /**
465 * load trusted certificates from a directory
466 */
467 static void load_certdir(private_stroke_cred_t *this, char *path,
468 certificate_type_t type, x509_flag_t flag)
469 {
470 struct stat st;
471 char *file;
472
473 enumerator_t *enumerator = enumerator_create_directory(path);
474
475 if (!enumerator)
476 {
477 DBG1(DBG_CFG, " reading directory failed");
478 return;
479 }
480
481 while (enumerator->enumerate(enumerator, NULL, &file, &st))
482 {
483 certificate_t *cert;
484
485 if (!S_ISREG(st.st_mode))
486 {
487 /* skip special file */
488 continue;
489 }
490 switch (type)
491 {
492 case CERT_X509:
493 cert = lib->creds->create(lib->creds,
494 CRED_CERTIFICATE, CERT_X509,
495 BUILD_FROM_FILE, file,
496 BUILD_X509_FLAG, flag,
497 BUILD_END);
498 if (cert)
499 {
500 add_cert(this, cert);
501 }
502 break;
503 case CERT_X509_CRL:
504 cert = lib->creds->create(lib->creds,
505 CRED_CERTIFICATE, CERT_X509_CRL,
506 BUILD_FROM_FILE, file,
507 BUILD_END);
508 if (cert)
509 {
510 add_crl(this, (crl_t*)cert);
511 }
512 break;
513 case CERT_X509_AC:
514 cert = lib->creds->create(lib->creds,
515 CRED_CERTIFICATE, CERT_X509_AC,
516 BUILD_FROM_FILE, file,
517 BUILD_END);
518 if (cert)
519 {
520 cert->destroy(cert);
521 }
522 break;
523 default:
524 break;
525 }
526 }
527 enumerator->destroy(enumerator);
528 }
529
530 /**
531 * Implementation of credential_set_t.cache_cert.
532 */
533 static void cache_cert(private_stroke_cred_t *this, certificate_t *cert)
534 {
535 if (cert->get_type(cert) == CERT_X509_CRL && this->cachecrl)
536 {
537 /* CRLs get written to /etc/ipsec.d/crls/authkeyId.crl */
538 crl_t *crl = (crl_t*)cert;
539
540 cert->get_ref(cert);
541 if (add_crl(this, crl))
542 {
543 char buf[256];
544 chunk_t chunk, hex;
545 identification_t *id;
546
547 id = crl->get_authKeyIdentifier(crl);
548 chunk = id->get_encoding(id);
549 hex = chunk_to_hex(chunk, NULL, FALSE);
550 snprintf(buf, sizeof(buf), "%s/%s.crl", CRL_DIR, hex);
551 free(hex.ptr);
552
553 chunk = cert->get_encoding(cert);
554 if (chunk_write(chunk, buf, 022, TRUE))
555 {
556 DBG1(DBG_CFG, " written crl to '%s'", buf);
557 }
558 else
559 {
560 DBG1(DBG_CFG, " writing crl to '%s' failed", buf);
561 }
562 free(chunk.ptr);
563 }
564 }
565 }
566
567 /**
568 * Implementation of stroke_cred_t.cachecrl.
569 */
570 static void cachecrl(private_stroke_cred_t *this, bool enabled)
571 {
572 DBG1(DBG_CFG, "crl caching to %s %s",
573 CRL_DIR, enabled ? "enabled" : "disabled");
574 this->cachecrl = enabled;
575 }
576
577
578 /**
579 * Convert a string of characters into a binary secret
580 * A string between single or double quotes is treated as ASCII characters
581 * A string prepended by 0x is treated as HEX and prepended by 0s as Base64
582 */
583 static err_t extract_secret(chunk_t *secret, chunk_t *line)
584 {
585 chunk_t raw_secret;
586 char delimiter = ' ';
587 bool quotes = FALSE;
588
589 if (!eat_whitespace(line))
590 {
591 return "missing secret";
592 }
593
594 if (*line->ptr == '\'' || *line->ptr == '"')
595 {
596 quotes = TRUE;
597 delimiter = *line->ptr;
598 line->ptr++; line->len--;
599 }
600
601 if (!extract_token(&raw_secret, delimiter, line))
602 {
603 if (delimiter == ' ')
604 {
605 raw_secret = *line;
606 }
607 else
608 {
609 return "missing second delimiter";
610 }
611 }
612
613 if (quotes)
614 {
615 /* treat as an ASCII string */
616 *secret = chunk_clone(raw_secret);
617 return NULL;
618 }
619 /* treat 0x as hex, 0s as base64 */
620 if (raw_secret.len > 2)
621 {
622 if (strncasecmp("0x", raw_secret.ptr, 2) == 0)
623 {
624 *secret = chunk_from_hex(chunk_skip(raw_secret, 2), NULL);
625 return NULL;
626 }
627 if (strncasecmp("0s", raw_secret.ptr, 2) == 0)
628 {
629 *secret = chunk_from_base64(chunk_skip(raw_secret, 2), NULL);
630 return NULL;
631 }
632 }
633 *secret = chunk_clone(raw_secret);
634 return NULL;
635 }
636
637 /**
638 * reload ipsec.secrets
639 */
640 static void load_secrets(private_stroke_cred_t *this)
641 {
642 size_t bytes;
643 int line_nr = 0;
644 chunk_t chunk, src, line;
645 FILE *fd;
646 private_key_t *private;
647 shared_key_t *shared;
648
649 DBG1(DBG_CFG, "loading secrets from '%s'", SECRETS_FILE);
650
651 fd = fopen(SECRETS_FILE, "r");
652 if (fd == NULL)
653 {
654 DBG1(DBG_CFG, "opening secrets file '%s' failed");
655 return;
656 }
657
658 /* TODO: do error checks */
659 fseek(fd, 0, SEEK_END);
660 chunk.len = ftell(fd);
661 rewind(fd);
662 chunk.ptr = malloc(chunk.len);
663 bytes = fread(chunk.ptr, 1, chunk.len, fd);
664 fclose(fd);
665 src = chunk;
666
667 this->mutex->lock(this->mutex);
668 while (this->shared->remove_last(this->shared,
669 (void**)&shared) == SUCCESS)
670 {
671 shared->destroy(shared);
672 }
673 while (this->private->remove_last(this->private,
674 (void**)&private) == SUCCESS)
675 {
676 private->destroy(private);
677 }
678
679 while (fetchline(&src, &line))
680 {
681 chunk_t ids, token;
682 shared_key_type_t type;
683
684 line_nr++;
685
686 if (!eat_whitespace(&line))
687 {
688 continue;
689 }
690 if (!extract_last_token(&ids, ':', &line))
691 {
692 DBG1(DBG_CFG, "line %d: missing ':' separator", line_nr);
693 goto error;
694 }
695 /* NULL terminate the ids string by replacing the : separator */
696 *(ids.ptr + ids.len) = '\0';
697
698 if (!eat_whitespace(&line) || !extract_token(&token, ' ', &line))
699 {
700 DBG1(DBG_CFG, "line %d: missing token", line_nr);
701 goto error;
702 }
703 if (match("RSA", &token))
704 {
705 char path[PATH_MAX];
706 chunk_t filename;
707 chunk_t secret = chunk_empty;
708 private_key_t *key;
709 bool pgp = FALSE;
710 chunk_t chunk = chunk_empty;
711
712 err_t ugh = extract_value(&filename, &line);
713
714 if (ugh != NULL)
715 {
716 DBG1(DBG_CFG, "line %d: %s", line_nr, ugh);
717 goto error;
718 }
719 if (filename.len == 0)
720 {
721 DBG1(DBG_CFG, "line %d: empty filename", line_nr);
722 goto error;
723 }
724 if (*filename.ptr == '/')
725 {
726 /* absolute path name */
727 snprintf(path, sizeof(path), "%.*s", filename.len, filename.ptr);
728 }
729 else
730 {
731 /* relative path name */
732 snprintf(path, sizeof(path), "%s/%.*s", PRIVATE_KEY_DIR,
733 filename.len, filename.ptr);
734 }
735
736 /* check for optional passphrase */
737 if (eat_whitespace(&line))
738 {
739 ugh = extract_secret(&secret, &line);
740 if (ugh != NULL)
741 {
742 DBG1(DBG_CFG, "line %d: malformed passphrase: %s", line_nr, ugh);
743 goto error;
744 }
745 }
746
747 if (pem_asn1_load_file(path, &secret, &chunk, &pgp))
748 {
749 key = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, KEY_RSA,
750 BUILD_BLOB_ASN1_DER, chunk, BUILD_END);
751 if (key)
752 {
753 DBG1(DBG_CFG, " loaded private key file '%s'", path);
754 this->private->insert_last(this->private, key);
755 }
756 }
757 chunk_clear(&secret);
758 }
759 else if ((match("PSK", &token) && (type = SHARED_IKE)) ||
760 (match("EAP", &token) && (type = SHARED_EAP)) ||
761 (match("XAUTH", &token) && (type = SHARED_EAP)) ||
762 (match("PIN", &token) && (type = SHARED_PIN)))
763 {
764 stroke_shared_key_t *shared_key;
765 chunk_t secret = chunk_empty;
766 bool any = TRUE;
767
768 err_t ugh = extract_secret(&secret, &line);
769 if (ugh != NULL)
770 {
771 DBG1(DBG_CFG, "line %d: malformed secret: %s", line_nr, ugh);
772 goto error;
773 }
774 shared_key = stroke_shared_key_create(type, secret);
775 DBG1(DBG_CFG, " loaded %N secret for %s", shared_key_type_names, type,
776 ids.len > 0 ? (char*)ids.ptr : "%any");
777 DBG4(DBG_CFG, " secret: %#B", &secret);
778
779 this->shared->insert_last(this->shared, shared_key);
780 while (ids.len > 0)
781 {
782 chunk_t id;
783 identification_t *peer_id;
784
785 ugh = extract_value(&id, &ids);
786 if (ugh != NULL)
787 {
788 DBG1(DBG_CFG, "line %d: %s", line_nr, ugh);
789 goto error;
790 }
791 if (id.len == 0)
792 {
793 continue;
794 }
795
796 /* NULL terminate the ID string */
797 *(id.ptr + id.len) = '\0';
798
799 peer_id = identification_create_from_string(id.ptr);
800 if (peer_id == NULL)
801 {
802 DBG1(DBG_CFG, "line %d: malformed ID: %s", line_nr, id.ptr);
803 goto error;
804 }
805 if (peer_id->get_type(peer_id) == ID_ANY)
806 {
807 peer_id->destroy(peer_id);
808 continue;
809 }
810
811 shared_key->add_owner(shared_key, peer_id);
812 any = FALSE;
813 }
814 if (any)
815 {
816 shared_key->add_owner(shared_key,
817 identification_create_from_encoding(ID_ANY, chunk_empty));
818 }
819 }
820 else
821 {
822 DBG1(DBG_CFG, "line %d: token must be either "
823 "RSA, PSK, EAP, or PIN", line_nr);
824 goto error;
825 }
826 }
827 error:
828 this->mutex->unlock(this->mutex);
829 chunk_clear(&chunk);
830 }
831
832 /**
833 * load all certificates from ipsec.d
834 */
835 static void load_certs(private_stroke_cred_t *this)
836 {
837 DBG1(DBG_CFG, "loading ca certificates from '%s'",
838 CA_CERTIFICATE_DIR);
839 load_certdir(this, CA_CERTIFICATE_DIR, CERT_X509, X509_CA);
840
841 DBG1(DBG_CFG, "loading aa certificates from '%s'",
842 AA_CERTIFICATE_DIR);
843 load_certdir(this, AA_CERTIFICATE_DIR, CERT_X509, X509_AA);
844
845 DBG1(DBG_CFG, "loading ocsp signer certificates from '%s'",
846 OCSP_CERTIFICATE_DIR);
847 load_certdir(this, OCSP_CERTIFICATE_DIR, CERT_X509, X509_OCSP_SIGNER);
848
849 DBG1(DBG_CFG, "loading attribute certificates from '%s'",
850 ATTR_CERTIFICATE_DIR);
851 load_certdir(this, ATTR_CERTIFICATE_DIR, CERT_X509_AC, 0);
852
853 DBG1(DBG_CFG, "loading crls from '%s'",
854 CRL_DIR);
855 load_certdir(this, CRL_DIR, CERT_X509_CRL, 0);
856 }
857
858 /**
859 * Implementation of stroke_cred_t.reread.
860 */
861 static void reread(private_stroke_cred_t *this, stroke_msg_t *msg)
862 {
863 if (msg->reread.flags & REREAD_SECRETS)
864 {
865 DBG1(DBG_CFG, "rereading secrets");
866 load_secrets(this);
867 }
868 if (msg->reread.flags & REREAD_CACERTS)
869 {
870 DBG1(DBG_CFG, "rereading ca certificates from '%s'",
871 CA_CERTIFICATE_DIR);
872 load_certdir(this, CA_CERTIFICATE_DIR, CERT_X509, X509_CA);
873 }
874 if (msg->reread.flags & REREAD_OCSPCERTS)
875 {
876 DBG1(DBG_CFG, "rereading ocsp signer certificates from '%s'",
877 OCSP_CERTIFICATE_DIR);
878 load_certdir(this, OCSP_CERTIFICATE_DIR, CERT_X509,
879 X509_OCSP_SIGNER);
880 }
881 if (msg->reread.flags & REREAD_AACERTS)
882 {
883 DBG1(DBG_CFG, "rereading aa certificates from '%s'",
884 AA_CERTIFICATE_DIR);
885 load_certdir(this, AA_CERTIFICATE_DIR, CERT_X509, X509_AA);
886 }
887 if (msg->reread.flags & REREAD_ACERTS)
888 {
889 DBG1(DBG_CFG, "rereading attribute certificates from '%s'",
890 ATTR_CERTIFICATE_DIR);
891 load_certdir(this, ATTR_CERTIFICATE_DIR, CERT_X509_AC, 0);
892 }
893 if (msg->reread.flags & REREAD_CRLS)
894 {
895 DBG1(DBG_CFG, "rereading crls from '%s'",
896 CRL_DIR);
897 load_certdir(this, CRL_DIR, CERT_X509_CRL, 0);
898 }
899 }
900
901 /**
902 * Implementation of stroke_cred_t.destroy
903 */
904 static void destroy(private_stroke_cred_t *this)
905 {
906 this->certs->destroy_offset(this->certs, offsetof(certificate_t, destroy));
907 this->shared->destroy_offset(this->shared, offsetof(shared_key_t, destroy));
908 this->private->destroy_offset(this->private, offsetof(private_key_t, destroy));
909 this->mutex->destroy(this->mutex);
910 free(this);
911 }
912
913 /*
914 * see header file
915 */
916 stroke_cred_t *stroke_cred_create()
917 {
918 private_stroke_cred_t *this = malloc_thing(private_stroke_cred_t);
919
920 this->public.set.create_private_enumerator = (void*)create_private_enumerator;
921 this->public.set.create_cert_enumerator = (void*)create_cert_enumerator;
922 this->public.set.create_shared_enumerator = (void*)create_shared_enumerator;
923 this->public.set.create_cdp_enumerator = (void*)return_null;
924 this->public.set.cache_cert = (void*)cache_cert;
925 this->public.reread = (void(*)(stroke_cred_t*, stroke_msg_t *msg))reread;
926 this->public.load_ca = (certificate_t*(*)(stroke_cred_t*, char *filename))load_ca;
927 this->public.load_peer = (certificate_t*(*)(stroke_cred_t*, char *filename))load_peer;
928 this->public.cachecrl = (void(*)(stroke_cred_t*, bool enabled))cachecrl;
929 this->public.destroy = (void(*)(stroke_cred_t*))destroy;
930
931 this->certs = linked_list_create();
932 this->shared = linked_list_create();
933 this->private = linked_list_create();
934 this->mutex = mutex_create(MUTEX_RECURSIVE);
935
936 load_certs(this);
937 load_secrets(this);
938
939 this->cachecrl = FALSE;
940
941 return &this->public;
942 }
943