Implemented support for preinstalled PGP certificates in charon
[strongswan.git] / src / charon / plugins / stroke / stroke_cred.c
1 /*
2 * Copyright (C) 2008 Tobias Brunner
3 * Copyright (C) 2008 Martin Willi
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include <sys/stat.h>
18 #include <limits.h>
19 #include <glob.h>
20 #include <libgen.h>
21
22 #include "stroke_cred.h"
23 #include "stroke_shared_key.h"
24
25 #include <credentials/certificates/x509.h>
26 #include <credentials/certificates/crl.h>
27 #include <credentials/certificates/ac.h>
28 #include <utils/linked_list.h>
29 #include <utils/lexparser.h>
30 #include <utils/mutex.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 #define MAX_SECRETS_RECURSION 10
46
47 typedef struct private_stroke_cred_t private_stroke_cred_t;
48
49 /**
50 * private data of stroke_cred
51 */
52 struct private_stroke_cred_t {
53
54 /**
55 * public functions
56 */
57 stroke_cred_t public;
58
59 /**
60 * list of trusted peer/signer/CA certificates (certificate_t)
61 */
62 linked_list_t *certs;
63
64 /**
65 * list of shared secrets (private_shared_key_t)
66 */
67 linked_list_t *shared;
68
69 /**
70 * list of private keys (private_key_t)
71 */
72 linked_list_t *private;
73
74 /**
75 * read-write lock to lists
76 */
77 rwlock_t *lock;
78
79 /**
80 * cache CRLs to disk?
81 */
82 bool cachecrl;
83 };
84
85 /**
86 * data to pass to various filters
87 */
88 typedef struct {
89 private_stroke_cred_t *this;
90 identification_t *id;
91 } id_data_t;
92
93 /**
94 * destroy id enumerator data and unlock list
95 */
96 static void id_data_destroy(id_data_t *data)
97 {
98 data->this->lock->unlock(data->this->lock);
99 free(data);
100 }
101
102 /**
103 * filter function for private key enumerator
104 */
105 static bool private_filter(id_data_t *data,
106 private_key_t **in, private_key_t **out)
107 {
108 private_key_t *key;
109 chunk_t keyid;
110
111 key = *in;
112 if (data->id == NULL)
113 {
114 *out = key;
115 return TRUE;
116 }
117 if (key->get_fingerprint(key, KEY_ID_PUBKEY_SHA1, &keyid) &&
118 chunk_equals(keyid, data->id->get_encoding(data->id)))
119 {
120 *out = key;
121 return TRUE;
122 }
123 return FALSE;
124 }
125
126 /**
127 * Implements credential_set_t.create_private_enumerator
128 */
129 static enumerator_t* create_private_enumerator(private_stroke_cred_t *this,
130 key_type_t type, identification_t *id)
131 {
132 id_data_t *data;
133
134 data = malloc_thing(id_data_t);
135 data->this = this;
136 data->id = id;
137
138 this->lock->read_lock(this->lock);
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 certificate_t *cert = *in;
151 chunk_t keyid;
152
153 if (cert->get_type(cert) == CERT_X509_CRL ||
154 cert->get_type(cert) == CERT_X509_AC)
155 {
156 return FALSE;
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 if (public->get_fingerprint(public, KEY_ID_PUBKEY_SHA1, &keyid) &&
168 chunk_equals(keyid, data->id->get_encoding(data->id)))
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 * filter function for attribute certificate enumerator
201 */
202 static bool ac_filter(id_data_t *data, certificate_t **in, certificate_t **out)
203 {
204 certificate_t *cert = *in;
205
206 if (cert->get_type(cert) != CERT_X509_AC)
207 {
208 return FALSE;
209 }
210
211 if (data->id == NULL || cert->has_subject(cert, data->id))
212 {
213 *out = *in;
214 return TRUE;
215 }
216 return FALSE;
217 }
218
219 /**
220 * Implements credential_set_t.create_cert_enumerator
221 */
222 static enumerator_t* create_cert_enumerator(private_stroke_cred_t *this,
223 certificate_type_t cert, key_type_t key,
224 identification_t *id, bool trusted)
225 {
226 id_data_t *data;
227
228 if (cert == CERT_X509_CRL || cert == CERT_X509_AC)
229 {
230 if (trusted)
231 {
232 return NULL;
233 }
234 data = malloc_thing(id_data_t);
235 data->this = this;
236 data->id = id;
237
238 this->lock->read_lock(this->lock);
239 return enumerator_create_filter(this->certs->create_enumerator(this->certs),
240 (cert == CERT_X509_CRL)? (void*)crl_filter : (void*)ac_filter,
241 data, (void*)id_data_destroy);
242 }
243 if (cert != CERT_X509 && cert != CERT_GPG && cert != CERT_ANY)
244 { /* we have X509/PGP certificates. TODO: ACs? */
245 return NULL;
246 }
247 data = malloc_thing(id_data_t);
248 data->this = this;
249 data->id = id;
250
251 this->lock->read_lock(this->lock);
252 return enumerator_create_filter(this->certs->create_enumerator(this->certs),
253 (void*)certs_filter, data,
254 (void*)id_data_destroy);
255 }
256
257 typedef struct {
258 private_stroke_cred_t *this;
259 identification_t *me;
260 identification_t *other;
261 shared_key_type_t type;
262 } shared_data_t;
263
264 /**
265 * free shared key enumerator data and unlock list
266 */
267 static void shared_data_destroy(shared_data_t *data)
268 {
269 data->this->lock->unlock(data->this->lock);
270 free(data);
271 }
272
273 /**
274 * filter function for certs enumerator
275 */
276 static bool shared_filter(shared_data_t *data,
277 stroke_shared_key_t **in, shared_key_t **out,
278 void **unused1, id_match_t *me,
279 void **unused2, id_match_t *other)
280 {
281 id_match_t my_match, other_match;
282 stroke_shared_key_t *stroke = *in;
283 shared_key_t *shared = &stroke->shared;
284
285 if (data->type != SHARED_ANY && shared->get_type(shared) != data->type)
286 {
287 return FALSE;
288 }
289
290 my_match = stroke->has_owner(stroke, data->me);
291 other_match = stroke->has_owner(stroke, data->other);
292 if (!my_match && !other_match)
293 {
294 return FALSE;
295 }
296 *out = shared;
297 if (me)
298 {
299 *me = my_match;
300 }
301 if (other)
302 {
303 *other = other_match;
304 }
305 return TRUE;
306 }
307
308 /**
309 * Implements credential_set_t.create_shared_enumerator
310 */
311 static enumerator_t* create_shared_enumerator(private_stroke_cred_t *this,
312 shared_key_type_t type, identification_t *me,
313 identification_t *other)
314 {
315 shared_data_t *data = malloc_thing(shared_data_t);
316
317 data->this = this;
318 data->me = me;
319 data->other = other;
320 data->type = type;
321 this->lock->read_lock(this->lock);
322 return enumerator_create_filter(this->shared->create_enumerator(this->shared),
323 (void*)shared_filter, data,
324 (void*)shared_data_destroy);
325 }
326
327 /**
328 * Add a certificate to chain
329 */
330 static certificate_t* add_cert(private_stroke_cred_t *this, certificate_t *cert)
331 {
332 certificate_t *current;
333 enumerator_t *enumerator;
334 bool new = TRUE;
335
336 this->lock->read_lock(this->lock);
337 enumerator = this->certs->create_enumerator(this->certs);
338 while (enumerator->enumerate(enumerator, (void**)&current))
339 {
340 if (current->equals(current, cert))
341 {
342 /* cert already in queue */
343 cert->destroy(cert);
344 cert = current;
345 new = FALSE;
346 break;
347 }
348 }
349 enumerator->destroy(enumerator);
350
351 if (new)
352 {
353 this->certs->insert_last(this->certs, cert);
354 }
355 this->lock->unlock(this->lock);
356 return cert;
357 }
358
359 /**
360 * Implementation of stroke_cred_t.load_ca.
361 */
362 static certificate_t* load_ca(private_stroke_cred_t *this, char *filename)
363 {
364 certificate_t *cert;
365 char path[PATH_MAX];
366
367 if (*filename == '/')
368 {
369 snprintf(path, sizeof(path), "%s", filename);
370 }
371 else
372 {
373 snprintf(path, sizeof(path), "%s/%s", CA_CERTIFICATE_DIR, filename);
374 }
375
376 cert = lib->creds->create(lib->creds,
377 CRED_CERTIFICATE, CERT_X509,
378 BUILD_FROM_FILE, path,
379 BUILD_END);
380 if (cert)
381 {
382 x509_t *x509 = (x509_t*)cert;
383
384 if (!(x509->get_flags(x509) & X509_CA))
385 {
386 DBG1(DBG_CFG, " ca certificate '%Y' misses ca basic constraint, "
387 "discarded", cert->get_subject(cert));
388 cert->destroy(cert);
389 return NULL;
390 }
391 return (certificate_t*)add_cert(this, cert);
392 }
393 return NULL;
394 }
395
396 /**
397 * Add X.509 CRL to chain
398 */
399 static bool add_crl(private_stroke_cred_t *this, crl_t* crl)
400 {
401 certificate_t *current, *cert = &crl->certificate;
402 enumerator_t *enumerator;
403 bool new = TRUE, found = FALSE;
404
405 this->lock->write_lock(this->lock);
406 enumerator = this->certs->create_enumerator(this->certs);
407 while (enumerator->enumerate(enumerator, (void**)&current))
408 {
409 if (current->get_type(current) == CERT_X509_CRL)
410 {
411 crl_t *crl_c = (crl_t*)current;
412 chunk_t authkey = crl->get_authKeyIdentifier(crl);
413 chunk_t authkey_c = crl_c->get_authKeyIdentifier(crl_c);
414
415 /* if compare authorityKeyIdentifiers if available */
416 if (authkey.ptr && authkey_c.ptr && chunk_equals(authkey, authkey_c))
417 {
418 found = TRUE;
419 }
420 else
421 {
422 identification_t *issuer = cert->get_issuer(cert);
423 identification_t *issuer_c = current->get_issuer(current);
424
425 /* otherwise compare issuer distinguished names */
426 if (issuer->equals(issuer, issuer_c))
427 {
428 found = TRUE;
429 }
430 }
431 if (found)
432 {
433 new = cert->is_newer(cert, current);
434 if (new)
435 {
436 this->certs->remove_at(this->certs, enumerator);
437 }
438 else
439 {
440 cert->destroy(cert);
441 }
442 break;
443 }
444 }
445 }
446 enumerator->destroy(enumerator);
447
448 if (new)
449 {
450 this->certs->insert_last(this->certs, cert);
451 }
452 this->lock->unlock(this->lock);
453 return new;
454 }
455
456 /**
457 * Add X.509 attribute certificate to chain
458 */
459 static bool add_ac(private_stroke_cred_t *this, ac_t* ac)
460 {
461 certificate_t *cert = &ac->certificate;
462
463 this->lock->write_lock(this->lock);
464 this->certs->insert_last(this->certs, cert);
465 this->lock->unlock(this->lock);
466 return TRUE;
467 }
468
469 /**
470 * Implementation of stroke_cred_t.load_peer.
471 */
472 static certificate_t* load_peer(private_stroke_cred_t *this, char *filename)
473 {
474 certificate_t *cert;
475 char path[PATH_MAX];
476
477 if (*filename == '/')
478 {
479 snprintf(path, sizeof(path), "%s", filename);
480 }
481 else
482 {
483 snprintf(path, sizeof(path), "%s/%s", CERTIFICATE_DIR, filename);
484 }
485
486 cert = lib->creds->create(lib->creds,
487 CRED_CERTIFICATE, CERT_ANY,
488 BUILD_FROM_FILE, path,
489 BUILD_END);
490 if (cert)
491 {
492 cert = add_cert(this, cert);
493 DBG1(DBG_CFG, " loaded certificate '%Y' from "
494 "file '%s'", cert->get_subject(cert), filename);
495 return cert->get_ref(cert);
496 }
497 DBG1(DBG_CFG, " loading certificate from file "
498 "'%s' failed", filename);
499 return NULL;
500 }
501
502 /**
503 * load trusted certificates from a directory
504 */
505 static void load_certdir(private_stroke_cred_t *this, char *path,
506 certificate_type_t type, x509_flag_t flag)
507 {
508 struct stat st;
509 char *file;
510
511 enumerator_t *enumerator = enumerator_create_directory(path);
512
513 if (!enumerator)
514 {
515 DBG1(DBG_CFG, " reading directory failed");
516 return;
517 }
518
519 while (enumerator->enumerate(enumerator, NULL, &file, &st))
520 {
521 certificate_t *cert;
522
523 if (!S_ISREG(st.st_mode))
524 {
525 /* skip special file */
526 continue;
527 }
528 switch (type)
529 {
530 case CERT_X509:
531 if (flag & X509_CA)
532 { /* for CA certificates, we strictly require CA
533 * basicconstraints to be set */
534 cert = lib->creds->create(lib->creds,
535 CRED_CERTIFICATE, CERT_X509,
536 BUILD_FROM_FILE, file, BUILD_END);
537 if (cert)
538 {
539 x509_t *x509 = (x509_t*)cert;
540
541 if (!(x509->get_flags(x509) & X509_CA))
542 {
543 DBG1(DBG_CFG, " ca certificate '%Y' misses "
544 "ca basic constraint, discarded",
545 cert->get_subject(cert));
546 cert->destroy(cert);
547 cert = NULL;
548 }
549 else
550 {
551 DBG1(DBG_CFG, " loaded CA certificate '%Y' from "
552 "file '%s'", cert->get_subject(cert), file);
553 }
554 }
555 else
556 {
557 DBG1(DBG_CFG, " loading CA certificate from file "
558 "'%s' failed", file);
559 }
560 }
561 else
562 { /* for all other flags, we add them to the certificate. */
563 cert = lib->creds->create(lib->creds,
564 CRED_CERTIFICATE, CERT_X509,
565 BUILD_FROM_FILE, file,
566 BUILD_X509_FLAG, flag, BUILD_END);
567 if (cert)
568 {
569 DBG1(DBG_CFG, " loaded certificate '%Y' from "
570 "file '%s'", cert->get_subject(cert), file);
571 }
572 else
573 {
574 DBG1(DBG_CFG, " loading certificate from file "
575 "'%s' failed", file);
576 }
577 }
578 if (cert)
579 {
580 add_cert(this, cert);
581 }
582 break;
583 case CERT_X509_CRL:
584 cert = lib->creds->create(lib->creds,
585 CRED_CERTIFICATE, CERT_X509_CRL,
586 BUILD_FROM_FILE, file,
587 BUILD_END);
588 if (cert)
589 {
590 add_crl(this, (crl_t*)cert);
591 DBG1(DBG_CFG, " loaded crl from file '%s'", file);
592 }
593 else
594 {
595 DBG1(DBG_CFG, " loading crl from file '%s' failed", file);
596 }
597 break;
598 case CERT_X509_AC:
599 cert = lib->creds->create(lib->creds,
600 CRED_CERTIFICATE, CERT_X509_AC,
601 BUILD_FROM_FILE, file,
602 BUILD_END);
603 if (cert)
604 {
605 add_ac(this, (ac_t*)cert);
606 DBG1(DBG_CFG, " loaded attribute certificate from "
607 "file '%s'", file);
608 }
609 else
610 {
611 DBG1(DBG_CFG, " loading attribute certificate from "
612 "file '%s' failed", file);
613 }
614 break;
615 default:
616 break;
617 }
618 }
619 enumerator->destroy(enumerator);
620 }
621
622 /**
623 * Implementation of credential_set_t.cache_cert.
624 */
625 static void cache_cert(private_stroke_cred_t *this, certificate_t *cert)
626 {
627 if (cert->get_type(cert) == CERT_X509_CRL && this->cachecrl)
628 {
629 /* CRLs get written to /etc/ipsec.d/crls/<authkeyId>.crl */
630 crl_t *crl = (crl_t*)cert;
631
632 cert->get_ref(cert);
633 if (add_crl(this, crl))
634 {
635 char buf[BUF_LEN];
636 chunk_t chunk, hex;
637
638 chunk = crl->get_authKeyIdentifier(crl);
639 hex = chunk_to_hex(chunk, NULL, FALSE);
640 snprintf(buf, sizeof(buf), "%s/%s.crl", CRL_DIR, hex);
641 free(hex.ptr);
642
643 chunk = cert->get_encoding(cert);
644 chunk_write(chunk, buf, "crl", 022, TRUE);
645 free(chunk.ptr);
646 }
647 }
648 }
649
650 /**
651 * Implementation of stroke_cred_t.cachecrl.
652 */
653 static void cachecrl(private_stroke_cred_t *this, bool enabled)
654 {
655 DBG1(DBG_CFG, "crl caching to %s %s",
656 CRL_DIR, enabled ? "enabled" : "disabled");
657 this->cachecrl = enabled;
658 }
659
660
661 /**
662 * Convert a string of characters into a binary secret
663 * A string between single or double quotes is treated as ASCII characters
664 * A string prepended by 0x is treated as HEX and prepended by 0s as Base64
665 */
666 static err_t extract_secret(chunk_t *secret, chunk_t *line)
667 {
668 chunk_t raw_secret;
669 char delimiter = ' ';
670 bool quotes = FALSE;
671
672 if (!eat_whitespace(line))
673 {
674 return "missing secret";
675 }
676
677 if (*line->ptr == '\'' || *line->ptr == '"')
678 {
679 quotes = TRUE;
680 delimiter = *line->ptr;
681 line->ptr++; line->len--;
682 }
683
684 if (!extract_token(&raw_secret, delimiter, line))
685 {
686 if (delimiter == ' ')
687 {
688 raw_secret = *line;
689 }
690 else
691 {
692 return "missing second delimiter";
693 }
694 }
695
696 if (quotes)
697 {
698 /* treat as an ASCII string */
699 *secret = chunk_clone(raw_secret);
700 return NULL;
701 }
702 /* treat 0x as hex, 0s as base64 */
703 if (raw_secret.len > 2)
704 {
705 if (strncasecmp("0x", raw_secret.ptr, 2) == 0)
706 {
707 *secret = chunk_from_hex(chunk_skip(raw_secret, 2), NULL);
708 return NULL;
709 }
710 if (strncasecmp("0s", raw_secret.ptr, 2) == 0)
711 {
712 *secret = chunk_from_base64(chunk_skip(raw_secret, 2), NULL);
713 return NULL;
714 }
715 }
716 *secret = chunk_clone(raw_secret);
717 return NULL;
718 }
719
720 /**
721 * Data to pass to passphrase_cb
722 */
723 typedef struct {
724 /** socket we use for prompting */
725 FILE *prompt;
726 /** private key file */
727 char *file;
728 /** buffer for passphrase */
729 char buf[256];
730 } passphrase_cb_data_t;
731
732 /**
733 * Passphrase callback to read from whack fd
734 */
735 chunk_t passphrase_cb(passphrase_cb_data_t *data, int try)
736 {
737 chunk_t secret = chunk_empty;;
738
739 if (try > 5)
740 {
741 fprintf(data->prompt, "invalid passphrase, too many trials\n");
742 return chunk_empty;
743 }
744 if (try == 1)
745 {
746 fprintf(data->prompt, "Private key '%s' is encrypted\n", data->file);
747 }
748 else
749 {
750 fprintf(data->prompt, "invalid passphrase\n");
751 }
752 fprintf(data->prompt, "Passphrase:\n");
753 if (fgets(data->buf, sizeof(data->buf), data->prompt))
754 {
755 secret = chunk_create(data->buf, strlen(data->buf));
756 if (secret.len)
757 { /* trim appended \n */
758 secret.len--;
759 }
760 }
761 return secret;
762 }
763
764 /**
765 * reload ipsec.secrets
766 */
767 static void load_secrets(private_stroke_cred_t *this, char *file, int level,
768 FILE *prompt)
769 {
770 size_t bytes;
771 int line_nr = 0;
772 chunk_t chunk, src, line;
773 FILE *fd;
774 private_key_t *private;
775 shared_key_t *shared;
776
777 DBG1(DBG_CFG, "loading secrets from '%s'", file);
778
779 fd = fopen(file, "r");
780 if (fd == NULL)
781 {
782 DBG1(DBG_CFG, "opening secrets file '%s' failed", file);
783 return;
784 }
785
786 /* TODO: do error checks */
787 fseek(fd, 0, SEEK_END);
788 chunk.len = ftell(fd);
789 rewind(fd);
790 chunk.ptr = malloc(chunk.len);
791 bytes = fread(chunk.ptr, 1, chunk.len, fd);
792 fclose(fd);
793 src = chunk;
794
795 if (level == 0)
796 {
797 this->lock->write_lock(this->lock);
798
799 /* flush secrets on non-recursive invocation */
800 while (this->shared->remove_last(this->shared,
801 (void**)&shared) == SUCCESS)
802 {
803 shared->destroy(shared);
804 }
805 while (this->private->remove_last(this->private,
806 (void**)&private) == SUCCESS)
807 {
808 private->destroy(private);
809 }
810 }
811
812 while (fetchline(&src, &line))
813 {
814 chunk_t ids, token;
815 shared_key_type_t type;
816
817 line_nr++;
818
819 if (!eat_whitespace(&line))
820 {
821 continue;
822 }
823 if (line.len > strlen("include ") &&
824 strneq(line.ptr, "include ", strlen("include ")))
825 {
826 glob_t buf;
827 char **expanded, *dir, pattern[PATH_MAX];
828 u_char *pos;
829
830 if (level > MAX_SECRETS_RECURSION)
831 {
832 DBG1(DBG_CFG, "maximum level of %d includes reached, ignored",
833 MAX_SECRETS_RECURSION);
834 continue;
835 }
836 /* terminate filename by space */
837 line = chunk_skip(line, strlen("include "));
838 pos = memchr(line.ptr, ' ', line.len);
839 if (pos)
840 {
841 line.len = pos - line.ptr;
842 }
843 if (line.len && line.ptr[0] == '/')
844 {
845 if (line.len + 1 > sizeof(pattern))
846 {
847 DBG1(DBG_CFG, "include pattern too long, ignored");
848 continue;
849 }
850 snprintf(pattern, sizeof(pattern), "%.*s", line.len, line.ptr);
851 }
852 else
853 { /* use directory of current file if relative */
854 dir = strdup(file);
855 dir = dirname(dir);
856
857 if (line.len + 1 + strlen(dir) + 1 > sizeof(pattern))
858 {
859 DBG1(DBG_CFG, "include pattern too long, ignored");
860 free(dir);
861 continue;
862 }
863 snprintf(pattern, sizeof(pattern), "%s/%.*s",
864 dir, line.len, line.ptr);
865 free(dir);
866 }
867 if (glob(pattern, GLOB_ERR, NULL, &buf) != 0)
868 {
869 DBG1(DBG_CFG, "expanding file expression '%s' failed", pattern);
870 globfree(&buf);
871 }
872 else
873 {
874 for (expanded = buf.gl_pathv; *expanded != NULL; expanded++)
875 {
876 load_secrets(this, *expanded, level + 1, prompt);
877 }
878 }
879 globfree(&buf);
880 continue;
881 }
882
883 if (line.len > 2 && strneq(": ", line.ptr, 2))
884 {
885 /* no ids, skip the ':' */
886 ids = chunk_empty;
887 line.ptr++;
888 line.len--;
889 }
890 else if (extract_token_str(&ids, " : ", &line))
891 {
892 /* NULL terminate the extracted id string */
893 *(ids.ptr + ids.len) = '\0';
894 }
895 else
896 {
897 DBG1(DBG_CFG, "line %d: missing ' : ' separator", line_nr);
898 goto error;
899 }
900
901 if (!eat_whitespace(&line) || !extract_token(&token, ' ', &line))
902 {
903 DBG1(DBG_CFG, "line %d: missing token", line_nr);
904 goto error;
905 }
906 if (match("RSA", &token) || match("ECDSA", &token))
907 {
908 char path[PATH_MAX];
909 chunk_t filename;
910 chunk_t secret = chunk_empty;
911 private_key_t *key = NULL;
912 key_type_t key_type = match("RSA", &token) ? KEY_RSA : KEY_ECDSA;
913
914 err_t ugh = extract_value(&filename, &line);
915
916 if (ugh != NULL)
917 {
918 DBG1(DBG_CFG, "line %d: %s", line_nr, ugh);
919 goto error;
920 }
921 if (filename.len == 0)
922 {
923 DBG1(DBG_CFG, "line %d: empty filename", line_nr);
924 goto error;
925 }
926 if (*filename.ptr == '/')
927 {
928 /* absolute path name */
929 snprintf(path, sizeof(path), "%.*s", filename.len, filename.ptr);
930 }
931 else
932 {
933 /* relative path name */
934 snprintf(path, sizeof(path), "%s/%.*s", PRIVATE_KEY_DIR,
935 filename.len, filename.ptr);
936 }
937
938 /* check for optional passphrase */
939 if (eat_whitespace(&line))
940 {
941 ugh = extract_secret(&secret, &line);
942 if (ugh != NULL)
943 {
944 DBG1(DBG_CFG, "line %d: malformed passphrase: %s", line_nr, ugh);
945 goto error;
946 }
947 }
948 if (secret.len == 7 && strneq(secret.ptr, "%prompt", 7))
949 {
950 if (prompt)
951 {
952 passphrase_cb_data_t data;
953
954 data.prompt = prompt;
955 data.file = path;
956 key = lib->creds->create(lib->creds, CRED_PRIVATE_KEY,
957 key_type, BUILD_FROM_FILE, path,
958 BUILD_PASSPHRASE_CALLBACK,
959 passphrase_cb, &data, BUILD_END);
960 }
961 }
962 else
963 {
964 key = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, key_type,
965 BUILD_FROM_FILE, path,
966 BUILD_PASSPHRASE, secret, BUILD_END);
967 }
968 if (key)
969 {
970 DBG1(DBG_CFG, " loaded %N private key file '%s'",
971 key_type_names, key->get_type(key), path);
972 this->private->insert_last(this->private, key);
973 }
974 else
975 {
976 DBG1(DBG_CFG, " skipped private key file '%s'", path);
977 }
978 chunk_clear(&secret);
979 }
980 else if (match("PIN", &token))
981 {
982 chunk_t sc = chunk_empty, secret = chunk_empty;
983 char smartcard[32], keyid[22], pin[32];
984 private_key_t *key;
985 u_int slot;
986
987 err_t ugh = extract_value(&sc, &line);
988
989 if (ugh != NULL)
990 {
991 DBG1(DBG_CFG, "line %d: %s", line_nr, ugh);
992 goto error;
993 }
994 if (sc.len == 0)
995 {
996 DBG1(DBG_CFG, "line %d: expected %%smartcard specifier", line_nr);
997 goto error;
998 }
999 snprintf(smartcard, sizeof(smartcard), "%.*s", sc.len, sc.ptr);
1000 smartcard[sizeof(smartcard) - 1] = '\0';
1001
1002 /* parse slot and key id. only two formats are supported.
1003 * first try %smartcard<slot>:<keyid> */
1004 if (sscanf(smartcard, "%%smartcard%u:%s", &slot, keyid) == 2)
1005 {
1006 snprintf(smartcard, sizeof(smartcard), "%u:%s", slot, keyid);
1007 }
1008 /* then try %smartcard:<keyid> */
1009 else if (sscanf(smartcard, "%%smartcard:%s", keyid) == 1)
1010 {
1011 snprintf(smartcard, sizeof(smartcard), "%s", keyid);
1012 }
1013 else
1014 {
1015 DBG1(DBG_CFG, "line %d: the given %%smartcard specifier is not"
1016 " supported or invalid", line_nr);
1017 goto error;
1018 }
1019
1020 if (!eat_whitespace(&line))
1021 {
1022 DBG1(DBG_CFG, "line %d: expected PIN", line_nr);
1023 goto error;
1024 }
1025 ugh = extract_secret(&secret, &line);
1026 if (ugh != NULL)
1027 {
1028 DBG1(DBG_CFG, "line %d: malformed PIN: %s", line_nr, ugh);
1029 goto error;
1030 }
1031 snprintf(pin, sizeof(pin), "%.*s", secret.len, secret.ptr);
1032 pin[sizeof(pin) - 1] = '\0';
1033
1034 /* we assume an RSA key */
1035 key = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, KEY_RSA,
1036 BUILD_SMARTCARD_KEYID, smartcard,
1037 BUILD_SMARTCARD_PIN, pin, BUILD_END);
1038
1039 if (key)
1040 {
1041 DBG1(DBG_CFG, " loaded private key from %.*s", sc.len, sc.ptr);
1042 this->private->insert_last(this->private, key);
1043 }
1044 memset(pin, 0, sizeof(pin));
1045 chunk_clear(&secret);
1046 }
1047 else if ((match("PSK", &token) && (type = SHARED_IKE)) ||
1048 (match("EAP", &token) && (type = SHARED_EAP)) ||
1049 (match("XAUTH", &token) && (type = SHARED_EAP)))
1050 {
1051 stroke_shared_key_t *shared_key;
1052 chunk_t secret = chunk_empty;
1053 bool any = TRUE;
1054
1055 err_t ugh = extract_secret(&secret, &line);
1056 if (ugh != NULL)
1057 {
1058 DBG1(DBG_CFG, "line %d: malformed secret: %s", line_nr, ugh);
1059 goto error;
1060 }
1061 shared_key = stroke_shared_key_create(type, secret);
1062 DBG1(DBG_CFG, " loaded %N secret for %s", shared_key_type_names, type,
1063 ids.len > 0 ? (char*)ids.ptr : "%any");
1064 DBG4(DBG_CFG, " secret: %#B", &secret);
1065
1066 this->shared->insert_last(this->shared, shared_key);
1067 while (ids.len > 0)
1068 {
1069 chunk_t id;
1070 identification_t *peer_id;
1071
1072 ugh = extract_value(&id, &ids);
1073 if (ugh != NULL)
1074 {
1075 DBG1(DBG_CFG, "line %d: %s", line_nr, ugh);
1076 goto error;
1077 }
1078 if (id.len == 0)
1079 {
1080 continue;
1081 }
1082
1083 /* NULL terminate the ID string */
1084 *(id.ptr + id.len) = '\0';
1085 peer_id = identification_create_from_string(id.ptr);
1086 if (peer_id->get_type(peer_id) == ID_ANY)
1087 {
1088 peer_id->destroy(peer_id);
1089 continue;
1090 }
1091
1092 shared_key->add_owner(shared_key, peer_id);
1093 any = FALSE;
1094 }
1095 if (any)
1096 {
1097 shared_key->add_owner(shared_key,
1098 identification_create_from_encoding(ID_ANY, chunk_empty));
1099 }
1100 }
1101 else
1102 {
1103 DBG1(DBG_CFG, "line %d: token must be either "
1104 "RSA, ECDSA, PSK, EAP, XAUTH or PIN", line_nr);
1105 goto error;
1106 }
1107 }
1108 error:
1109 if (level == 0)
1110 {
1111 this->lock->unlock(this->lock);
1112 }
1113 chunk_clear(&chunk);
1114 }
1115
1116 /**
1117 * load all certificates from ipsec.d
1118 */
1119 static void load_certs(private_stroke_cred_t *this)
1120 {
1121 DBG1(DBG_CFG, "loading ca certificates from '%s'",
1122 CA_CERTIFICATE_DIR);
1123 load_certdir(this, CA_CERTIFICATE_DIR, CERT_X509, X509_CA);
1124
1125 DBG1(DBG_CFG, "loading aa certificates from '%s'",
1126 AA_CERTIFICATE_DIR);
1127 load_certdir(this, AA_CERTIFICATE_DIR, CERT_X509, X509_AA);
1128
1129 DBG1(DBG_CFG, "loading ocsp signer certificates from '%s'",
1130 OCSP_CERTIFICATE_DIR);
1131 load_certdir(this, OCSP_CERTIFICATE_DIR, CERT_X509, X509_OCSP_SIGNER);
1132
1133 DBG1(DBG_CFG, "loading attribute certificates from '%s'",
1134 ATTR_CERTIFICATE_DIR);
1135 load_certdir(this, ATTR_CERTIFICATE_DIR, CERT_X509_AC, 0);
1136
1137 DBG1(DBG_CFG, "loading crls from '%s'",
1138 CRL_DIR);
1139 load_certdir(this, CRL_DIR, CERT_X509_CRL, 0);
1140 }
1141
1142 /**
1143 * Implementation of stroke_cred_t.reread.
1144 */
1145 static void reread(private_stroke_cred_t *this, stroke_msg_t *msg, FILE *prompt)
1146 {
1147 if (msg->reread.flags & REREAD_SECRETS)
1148 {
1149 DBG1(DBG_CFG, "rereading secrets");
1150 load_secrets(this, SECRETS_FILE, 0, prompt);
1151 }
1152 if (msg->reread.flags & REREAD_CACERTS)
1153 {
1154 DBG1(DBG_CFG, "rereading ca certificates from '%s'",
1155 CA_CERTIFICATE_DIR);
1156 load_certdir(this, CA_CERTIFICATE_DIR, CERT_X509, X509_CA);
1157 }
1158 if (msg->reread.flags & REREAD_OCSPCERTS)
1159 {
1160 DBG1(DBG_CFG, "rereading ocsp signer certificates from '%s'",
1161 OCSP_CERTIFICATE_DIR);
1162 load_certdir(this, OCSP_CERTIFICATE_DIR, CERT_X509,
1163 X509_OCSP_SIGNER);
1164 }
1165 if (msg->reread.flags & REREAD_AACERTS)
1166 {
1167 DBG1(DBG_CFG, "rereading aa certificates from '%s'",
1168 AA_CERTIFICATE_DIR);
1169 load_certdir(this, AA_CERTIFICATE_DIR, CERT_X509, X509_AA);
1170 }
1171 if (msg->reread.flags & REREAD_ACERTS)
1172 {
1173 DBG1(DBG_CFG, "rereading attribute certificates from '%s'",
1174 ATTR_CERTIFICATE_DIR);
1175 load_certdir(this, ATTR_CERTIFICATE_DIR, CERT_X509_AC, 0);
1176 }
1177 if (msg->reread.flags & REREAD_CRLS)
1178 {
1179 DBG1(DBG_CFG, "rereading crls from '%s'",
1180 CRL_DIR);
1181 load_certdir(this, CRL_DIR, CERT_X509_CRL, 0);
1182 }
1183 }
1184
1185 /**
1186 * Implementation of stroke_cred_t.destroy
1187 */
1188 static void destroy(private_stroke_cred_t *this)
1189 {
1190 this->certs->destroy_offset(this->certs, offsetof(certificate_t, destroy));
1191 this->shared->destroy_offset(this->shared, offsetof(shared_key_t, destroy));
1192 this->private->destroy_offset(this->private, offsetof(private_key_t, destroy));
1193 this->lock->destroy(this->lock);
1194 free(this);
1195 }
1196
1197 /*
1198 * see header file
1199 */
1200 stroke_cred_t *stroke_cred_create()
1201 {
1202 private_stroke_cred_t *this = malloc_thing(private_stroke_cred_t);
1203
1204 this->public.set.create_private_enumerator = (void*)create_private_enumerator;
1205 this->public.set.create_cert_enumerator = (void*)create_cert_enumerator;
1206 this->public.set.create_shared_enumerator = (void*)create_shared_enumerator;
1207 this->public.set.create_cdp_enumerator = (void*)return_null;
1208 this->public.set.cache_cert = (void*)cache_cert;
1209 this->public.reread = (void(*)(stroke_cred_t*, stroke_msg_t *msg, FILE*))reread;
1210 this->public.load_ca = (certificate_t*(*)(stroke_cred_t*, char *filename))load_ca;
1211 this->public.load_peer = (certificate_t*(*)(stroke_cred_t*, char *filename))load_peer;
1212 this->public.cachecrl = (void(*)(stroke_cred_t*, bool enabled))cachecrl;
1213 this->public.destroy = (void(*)(stroke_cred_t*))destroy;
1214
1215 this->certs = linked_list_create();
1216 this->shared = linked_list_create();
1217 this->private = linked_list_create();
1218 this->lock = rwlock_create(RWLOCK_TYPE_DEFAULT);
1219
1220 load_certs(this);
1221 load_secrets(this, SECRETS_FILE, 0, NULL);
1222
1223 this->cachecrl = FALSE;
1224
1225 return &this->public;
1226 }
1227