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