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