corrected variable name
[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 * 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 cached to /etc/ipsec.d/crls/authkeyId.der */
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 char *hex;
545 chunk_t chunk;
546 identification_t *id;
547
548 id = crl->get_authKeyIdentifier(crl);
549 chunk = id->get_encoding(id);
550 hex = chunk_to_hex(chunk, FALSE);
551 snprintf(buf, sizeof(buf), "%s/%s.der", CRL_DIR, hex);
552 free(hex);
553
554 chunk = cert->get_encoding(cert);
555 if (chunk_write(chunk, buf, 022, TRUE))
556 {
557 DBG1(DBG_CFG, "cached crl to %s", buf);
558 }
559 else
560 {
561 DBG1(DBG_CFG, "caching crl to %s failed", buf);
562 }
563 free(chunk.ptr);
564 }
565 }
566 }
567
568 /**
569 * Implementation of stroke_cred_t.cachecrl.
570 */
571 static void cachecrl(private_stroke_cred_t *this, bool enabled)
572 {
573 DBG1(DBG_CFG, "crl caching to %s %s",
574 CRL_DIR, enabled ? "enabled" : "disabled");
575 this->cachecrl = enabled;
576 }
577
578
579 /**
580 * Convert a string of characters into a binary secret
581 * A string between single or double quotes is treated as ASCII characters
582 * A string prepended by 0x is treated as HEX and prepended by 0s as Base64
583 */
584 static err_t extract_secret(chunk_t *secret, chunk_t *line)
585 {
586 chunk_t raw_secret;
587 char delimiter = ' ';
588 bool quotes = FALSE;
589
590 if (!eat_whitespace(line))
591 {
592 return "missing secret";
593 }
594
595 if (*line->ptr == '\'' || *line->ptr == '"')
596 {
597 quotes = TRUE;
598 delimiter = *line->ptr;
599 line->ptr++; line->len--;
600 }
601
602 if (!extract_token(&raw_secret, delimiter, line))
603 {
604 if (delimiter == ' ')
605 {
606 raw_secret = *line;
607 }
608 else
609 {
610 return "missing second delimiter";
611 }
612 }
613
614 if (quotes)
615 {
616 /* treat as an ASCII string */
617 *secret = chunk_clone(raw_secret);
618 }
619 else
620 {
621 size_t len;
622 err_t ugh;
623
624 /* secret converted to binary form doesn't use more space than the raw_secret */
625 *secret = chunk_alloc(raw_secret.len);
626
627 /* convert from HEX or Base64 to binary */
628 ugh = ttodata(raw_secret.ptr, raw_secret.len, 0, secret->ptr, secret->len, &len);
629
630 if (ugh != NULL)
631 {
632 chunk_clear(secret);
633 return ugh;
634 }
635 secret->len = len;
636 }
637 return NULL;
638 }
639
640 /**
641 * reload ipsec.secrets
642 */
643 static void load_secrets(private_stroke_cred_t *this)
644 {
645 size_t bytes;
646 int line_nr = 0;
647 chunk_t chunk, src, line;
648 FILE *fd;
649 private_key_t *private;
650 shared_key_t *shared;
651
652 DBG1(DBG_CFG, "loading secrets from '%s'", SECRETS_FILE);
653
654 fd = fopen(SECRETS_FILE, "r");
655 if (fd == NULL)
656 {
657 DBG1(DBG_CFG, "opening secrets file '%s' failed");
658 return;
659 }
660
661 /* TODO: do error checks */
662 fseek(fd, 0, SEEK_END);
663 chunk.len = ftell(fd);
664 rewind(fd);
665 chunk.ptr = malloc(chunk.len);
666 bytes = fread(chunk.ptr, 1, chunk.len, fd);
667 fclose(fd);
668 src = chunk;
669
670 this->mutex->lock(this->mutex);
671 while (this->shared->remove_last(this->shared,
672 (void**)&shared) == SUCCESS)
673 {
674 shared->destroy(shared);
675 }
676 while (this->private->remove_last(this->private,
677 (void**)&private) == SUCCESS)
678 {
679 private->destroy(private);
680 }
681
682 while (fetchline(&src, &line))
683 {
684 chunk_t ids, token;
685 shared_key_type_t type;
686
687 line_nr++;
688
689 if (!eat_whitespace(&line))
690 {
691 continue;
692 }
693 if (!extract_last_token(&ids, ':', &line))
694 {
695 DBG1(DBG_CFG, "line %d: missing ':' separator", line_nr);
696 goto error;
697 }
698 /* NULL terminate the ids string by replacing the : separator */
699 *(ids.ptr + ids.len) = '\0';
700
701 if (!eat_whitespace(&line) || !extract_token(&token, ' ', &line))
702 {
703 DBG1(DBG_CFG, "line %d: missing token", line_nr);
704 goto error;
705 }
706 if (match("RSA", &token))
707 {
708 char path[PATH_MAX];
709 chunk_t filename;
710 chunk_t secret = chunk_empty;
711 private_key_t *key;
712 bool pgp = FALSE;
713 chunk_t chunk = chunk_empty;
714
715 err_t ugh = extract_value(&filename, &line);
716
717 if (ugh != NULL)
718 {
719 DBG1(DBG_CFG, "line %d: %s", line_nr, ugh);
720 goto error;
721 }
722 if (filename.len == 0)
723 {
724 DBG1(DBG_CFG, "line %d: empty filename", line_nr);
725 goto error;
726 }
727 if (*filename.ptr == '/')
728 {
729 /* absolute path name */
730 snprintf(path, sizeof(path), "%.*s", filename.len, filename.ptr);
731 }
732 else
733 {
734 /* relative path name */
735 snprintf(path, sizeof(path), "%s/%.*s", PRIVATE_KEY_DIR,
736 filename.len, filename.ptr);
737 }
738
739 /* check for optional passphrase */
740 if (eat_whitespace(&line))
741 {
742 ugh = extract_secret(&secret, &line);
743 if (ugh != NULL)
744 {
745 DBG1(DBG_CFG, "line %d: malformed passphrase: %s", line_nr, ugh);
746 goto error;
747 }
748 }
749
750 if (pem_asn1_load_file(path, &secret, &chunk, &pgp))
751 {
752 key = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, KEY_RSA,
753 BUILD_BLOB_ASN1_DER, chunk, BUILD_END);
754 if (key)
755 {
756 DBG1(DBG_CFG, " loaded private key file '%s'", path);
757 this->private->insert_last(this->private, key);
758 }
759 }
760 chunk_clear(&secret);
761 }
762 else if ((match("PSK", &token) && (type = SHARED_IKE)) ||
763 (match("EAP", &token) && (type = SHARED_EAP)) ||
764 (match("XAUTH", &token) && (type = SHARED_EAP)) ||
765 (match("PIN", &token) && (type = SHARED_PIN)))
766 {
767 stroke_shared_key_t *shared_key;
768 chunk_t secret = chunk_empty;
769 bool any = TRUE;
770
771 err_t ugh = extract_secret(&secret, &line);
772 if (ugh != NULL)
773 {
774 DBG1(DBG_CFG, "line %d: malformed secret: %s", line_nr, ugh);
775 goto error;
776 }
777 shared_key = stroke_shared_key_create(type, secret);
778 DBG1(DBG_CFG, " loaded %N secret for %s", shared_key_type_names, type,
779 ids.len > 0 ? (char*)ids.ptr : "%any");
780 DBG4(DBG_CFG, " secret: %#B", &secret);
781
782 this->shared->insert_last(this->shared, shared_key);
783 while (ids.len > 0)
784 {
785 chunk_t id;
786 identification_t *peer_id;
787
788 ugh = extract_value(&id, &ids);
789 if (ugh != NULL)
790 {
791 DBG1(DBG_CFG, "line %d: %s", line_nr, ugh);
792 goto error;
793 }
794 if (id.len == 0)
795 {
796 continue;
797 }
798
799 /* NULL terminate the ID string */
800 *(id.ptr + id.len) = '\0';
801
802 peer_id = identification_create_from_string(id.ptr);
803 if (peer_id == NULL)
804 {
805 DBG1(DBG_CFG, "line %d: malformed ID: %s", line_nr, id.ptr);
806 goto error;
807 }
808 if (peer_id->get_type(peer_id) == ID_ANY)
809 {
810 peer_id->destroy(peer_id);
811 continue;
812 }
813
814 shared_key->add_owner(shared_key, peer_id);
815 any = FALSE;
816 }
817 if (any)
818 {
819 shared_key->add_owner(shared_key,
820 identification_create_from_encoding(ID_ANY, chunk_empty));
821 }
822 }
823 else
824 {
825 DBG1(DBG_CFG, "line %d: token must be either "
826 "RSA, PSK, EAP, or PIN", line_nr);
827 goto error;
828 }
829 }
830 error:
831 this->mutex->unlock(this->mutex);
832 chunk_clear(&chunk);
833 }
834
835 /**
836 * load all certificates from ipsec.d
837 */
838 static void load_certs(private_stroke_cred_t *this)
839 {
840 DBG1(DBG_CFG, "loading ca certificates from '%s'",
841 CA_CERTIFICATE_DIR);
842 load_certdir(this, CA_CERTIFICATE_DIR, CERT_X509, X509_CA);
843
844 DBG1(DBG_CFG, "loading aa certificates from '%s'",
845 AA_CERTIFICATE_DIR);
846 load_certdir(this, AA_CERTIFICATE_DIR, CERT_X509, X509_AA);
847
848 DBG1(DBG_CFG, "loading ocsp signer certificates from '%s'",
849 OCSP_CERTIFICATE_DIR);
850 load_certdir(this, OCSP_CERTIFICATE_DIR, CERT_X509, X509_OCSP_SIGNER);
851
852 DBG1(DBG_CFG, "loading attribute certificates from '%s'",
853 ATTR_CERTIFICATE_DIR);
854 load_certdir(this, ATTR_CERTIFICATE_DIR, CERT_X509_AC, 0);
855
856 DBG1(DBG_CFG, "loading crls from '%s'",
857 CRL_DIR);
858 load_certdir(this, CRL_DIR, CERT_X509_CRL, 0);
859 }
860
861 /**
862 * Implementation of stroke_cred_t.reread.
863 */
864 static void reread(private_stroke_cred_t *this, stroke_msg_t *msg)
865 {
866 if (msg->reread.flags & REREAD_SECRETS)
867 {
868 DBG1(DBG_CFG, "rereading secrets");
869 load_secrets(this);
870 }
871 if (msg->reread.flags & REREAD_CACERTS)
872 {
873 DBG1(DBG_CFG, "rereading ca certificates from '%s'",
874 CA_CERTIFICATE_DIR);
875 load_certdir(this, CA_CERTIFICATE_DIR, CERT_X509, X509_CA);
876 }
877 if (msg->reread.flags & REREAD_OCSPCERTS)
878 {
879 DBG1(DBG_CFG, "rereading ocsp signer certificates from '%s'",
880 OCSP_CERTIFICATE_DIR);
881 load_certdir(this, OCSP_CERTIFICATE_DIR, CERT_X509,
882 X509_OCSP_SIGNER);
883 }
884 if (msg->reread.flags & REREAD_AACERTS)
885 {
886 DBG1(DBG_CFG, "rereading aa certificates from '%s'",
887 AA_CERTIFICATE_DIR);
888 load_certdir(this, AA_CERTIFICATE_DIR, CERT_X509, X509_AA);
889 }
890 if (msg->reread.flags & REREAD_ACERTS)
891 {
892 DBG1(DBG_CFG, "rereading attribute certificates from '%s'",
893 ATTR_CERTIFICATE_DIR);
894 load_certdir(this, ATTR_CERTIFICATE_DIR, CERT_X509_AC, 0);
895 }
896 if (msg->reread.flags & REREAD_CRLS)
897 {
898 DBG1(DBG_CFG, "rereading crls from '%s'",
899 CRL_DIR);
900 load_certdir(this, CRL_DIR, CERT_X509_CRL, 0);
901 }
902 }
903
904 /**
905 * Implementation of stroke_cred_t.destroy
906 */
907 static void destroy(private_stroke_cred_t *this)
908 {
909 this->certs->destroy_offset(this->certs, offsetof(certificate_t, destroy));
910 this->shared->destroy_offset(this->shared, offsetof(shared_key_t, destroy));
911 this->private->destroy_offset(this->private, offsetof(private_key_t, destroy));
912 this->mutex->destroy(this->mutex);
913 free(this);
914 }
915
916 /*
917 * see header file
918 */
919 stroke_cred_t *stroke_cred_create()
920 {
921 private_stroke_cred_t *this = malloc_thing(private_stroke_cred_t);
922
923 this->public.set.create_private_enumerator = (void*)create_private_enumerator;
924 this->public.set.create_cert_enumerator = (void*)create_cert_enumerator;
925 this->public.set.create_shared_enumerator = (void*)create_shared_enumerator;
926 this->public.set.create_cdp_enumerator = (void*)return_null;
927 this->public.set.cache_cert = (void*)cache_cert;
928 this->public.reread = (void(*)(stroke_cred_t*, stroke_msg_t *msg))reread;
929 this->public.load_ca = (certificate_t*(*)(stroke_cred_t*, char *filename))load_ca;
930 this->public.load_peer = (certificate_t*(*)(stroke_cred_t*, char *filename))load_peer;
931 this->public.cachecrl = (void(*)(stroke_cred_t*, bool enabled))cachecrl;
932 this->public.destroy = (void(*)(stroke_cred_t*))destroy;
933
934 this->certs = linked_list_create();
935 this->shared = linked_list_create();
936 this->private = linked_list_create();
937 this->mutex = mutex_create(MUTEX_RECURSIVE);
938
939 load_certs(this);
940 load_secrets(this);
941
942 this->cachecrl = FALSE;
943
944 return &this->public;
945 }
946