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