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