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