Splitted up the load_secrets() function
[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 * Load a smartcard with a PIN
740 */
741 static bool load_pin(private_stroke_cred_t *this, chunk_t line, int line_nr,
742 FILE *prompt)
743 {
744 chunk_t sc = chunk_empty, secret = chunk_empty;
745 char smartcard[64], keyid[64], module[64], *pos;
746 private_key_t *key = NULL;
747 u_int slot;
748 chunk_t chunk;
749 enum {
750 SC_FORMAT_SLOT_MODULE_KEYID,
751 SC_FORMAT_SLOT_KEYID,
752 SC_FORMAT_KEYID,
753 } format;
754
755 err_t ugh = extract_value(&sc, &line);
756
757 if (ugh != NULL)
758 {
759 DBG1(DBG_CFG, "line %d: %s", line_nr, ugh);
760 return FALSE;
761 }
762 if (sc.len == 0)
763 {
764 DBG1(DBG_CFG, "line %d: expected %%smartcard specifier", line_nr);
765 return FALSE;
766 }
767 snprintf(smartcard, sizeof(smartcard), "%.*s", sc.len, sc.ptr);
768 smartcard[sizeof(smartcard) - 1] = '\0';
769
770 /* parse slot and key id. Three formats are supported:
771 * - %smartcard<slot>@<module>:<keyid>
772 * - %smartcard<slot>:<keyid>
773 * - %smartcard:<keyid>
774 */
775 if (sscanf(smartcard, "%%smartcard%u@%s", &slot, module) == 2)
776 {
777 pos = strchr(module, ':');
778 if (!pos)
779 {
780 DBG1(DBG_CFG, "line %d: the given %%smartcard specifier is "
781 "invalid", line_nr);
782 return FALSE;
783 }
784 *pos = '\0';
785 strcpy(keyid, pos + 1);
786 format = SC_FORMAT_SLOT_MODULE_KEYID;
787 }
788 else if (sscanf(smartcard, "%%smartcard%u:%s", &slot, keyid) == 2)
789 {
790 format = SC_FORMAT_SLOT_KEYID;
791 }
792 else if (sscanf(smartcard, "%%smartcard:%s", keyid) == 1)
793 {
794 format = SC_FORMAT_KEYID;
795 }
796 else
797 {
798 DBG1(DBG_CFG, "line %d: the given %%smartcard specifier is not"
799 " supported or invalid", line_nr);
800 return FALSE;
801 }
802
803 if (!eat_whitespace(&line))
804 {
805 DBG1(DBG_CFG, "line %d: expected PIN", line_nr);
806 return FALSE;
807 }
808 ugh = extract_secret(&secret, &line);
809 if (ugh != NULL)
810 {
811 DBG1(DBG_CFG, "line %d: malformed PIN: %s", line_nr, ugh);
812 return FALSE;
813 }
814
815 chunk = chunk_from_hex(chunk_create(keyid, strlen(keyid)), NULL);
816
817 if (secret.len == 7 && strneq(secret.ptr, "%prompt", 7))
818 {
819 if (prompt)
820 {
821 passphrase_cb_data_t data = {
822 .prompt = prompt,
823 .file = smartcard,
824 };
825
826 switch (format)
827 {
828 case SC_FORMAT_SLOT_MODULE_KEYID:
829 key = lib->creds->create(lib->creds,
830 CRED_PRIVATE_KEY, KEY_ANY,
831 BUILD_PKCS11_SLOT, slot,
832 BUILD_PKCS11_MODULE, module,
833 BUILD_PKCS11_KEYID, chunk,
834 BUILD_PASSPHRASE_CALLBACK,
835 smartcard_cb, &data, BUILD_END);
836 break;
837 case SC_FORMAT_SLOT_KEYID:
838 key = lib->creds->create(lib->creds,
839 CRED_PRIVATE_KEY, KEY_ANY,
840 BUILD_PKCS11_SLOT, slot,
841 BUILD_PKCS11_KEYID, chunk,
842 BUILD_PASSPHRASE_CALLBACK,
843 smartcard_cb, &data, BUILD_END);
844 break;
845 case SC_FORMAT_KEYID:
846 key = lib->creds->create(lib->creds,
847 CRED_PRIVATE_KEY, KEY_ANY,
848 BUILD_PKCS11_KEYID, chunk,
849 BUILD_PASSPHRASE_CALLBACK,
850 smartcard_cb, &data, BUILD_END);
851 break;
852 }
853 }
854 }
855 else
856 {
857 switch (format)
858 {
859 case SC_FORMAT_SLOT_MODULE_KEYID:
860 key = lib->creds->create(lib->creds,
861 CRED_PRIVATE_KEY, KEY_ANY,
862 BUILD_PKCS11_SLOT, slot,
863 BUILD_PKCS11_MODULE, module,
864 BUILD_PKCS11_KEYID, chunk,
865 BUILD_PASSPHRASE, secret, BUILD_END);
866 break;
867 case SC_FORMAT_SLOT_KEYID:
868 key = lib->creds->create(lib->creds,
869 CRED_PRIVATE_KEY, KEY_ANY,
870 BUILD_PKCS11_SLOT, slot,
871 BUILD_PKCS11_KEYID, chunk,
872 BUILD_PASSPHRASE, secret, BUILD_END);
873 break;
874 case SC_FORMAT_KEYID:
875 key = lib->creds->create(lib->creds,
876 CRED_PRIVATE_KEY, KEY_ANY,
877 BUILD_PKCS11_KEYID, chunk,
878 BUILD_PASSPHRASE, secret, BUILD_END);
879 break;
880 }
881 }
882 free(chunk.ptr);
883 if (key)
884 {
885 DBG1(DBG_CFG, " loaded private key from %.*s", sc.len, sc.ptr);
886 this->private->insert_last(this->private, key);
887 }
888 chunk_clear(&secret);
889 return TRUE;
890 }
891
892 /**
893 * Load a private key
894 */
895 static bool load_private(private_stroke_cred_t *this, chunk_t line, int line_nr,
896 FILE *prompt, key_type_t key_type)
897 {
898 char path[PATH_MAX];
899 chunk_t filename;
900 chunk_t secret = chunk_empty;
901 private_key_t *key = NULL;
902
903 err_t ugh = extract_value(&filename, &line);
904
905 if (ugh != NULL)
906 {
907 DBG1(DBG_CFG, "line %d: %s", line_nr, ugh);
908 return FALSE;
909 }
910 if (filename.len == 0)
911 {
912 DBG1(DBG_CFG, "line %d: empty filename", line_nr);
913 return FALSE;
914 }
915 if (*filename.ptr == '/')
916 {
917 /* absolute path name */
918 snprintf(path, sizeof(path), "%.*s", filename.len, filename.ptr);
919 }
920 else
921 {
922 /* relative path name */
923 snprintf(path, sizeof(path), "%s/%.*s", PRIVATE_KEY_DIR,
924 filename.len, filename.ptr);
925 }
926
927 /* check for optional passphrase */
928 if (eat_whitespace(&line))
929 {
930 ugh = extract_secret(&secret, &line);
931 if (ugh != NULL)
932 {
933 DBG1(DBG_CFG, "line %d: malformed passphrase: %s", line_nr, ugh);
934 return FALSE;
935 }
936 }
937 if (secret.len == 7 && strneq(secret.ptr, "%prompt", 7))
938 {
939 if (prompt)
940 {
941 passphrase_cb_data_t data = {
942 .prompt = prompt,
943 .file = path,
944 };
945 key = lib->creds->create(lib->creds, CRED_PRIVATE_KEY,
946 key_type, BUILD_FROM_FILE, path,
947 BUILD_PASSPHRASE_CALLBACK,
948 passphrase_cb, &data, BUILD_END);
949 }
950 }
951 else
952 {
953 key = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, key_type,
954 BUILD_FROM_FILE, path,
955 BUILD_PASSPHRASE, secret, BUILD_END);
956 }
957 if (key)
958 {
959 DBG1(DBG_CFG, " loaded %N private key from '%s'",
960 key_type_names, key->get_type(key), path);
961 this->private->insert_last(this->private, key);
962 }
963 else
964 {
965 DBG1(DBG_CFG, " loading private key from '%s' failed", path);
966 }
967 chunk_clear(&secret);
968 return TRUE;
969 }
970
971 /**
972 * Load a shared key
973 */
974 static bool load_shared(private_stroke_cred_t *this, chunk_t line, int line_nr,
975 shared_key_type_t type, chunk_t ids)
976 {
977 stroke_shared_key_t *shared_key;
978 chunk_t secret = chunk_empty;
979 bool any = TRUE;
980
981 err_t ugh = extract_secret(&secret, &line);
982 if (ugh != NULL)
983 {
984 DBG1(DBG_CFG, "line %d: malformed secret: %s", line_nr, ugh);
985 return FALSE;
986 }
987 shared_key = stroke_shared_key_create(type, secret);
988 DBG1(DBG_CFG, " loaded %N secret for %s", shared_key_type_names, type,
989 ids.len > 0 ? (char*)ids.ptr : "%any");
990 DBG4(DBG_CFG, " secret: %#B", &secret);
991
992 this->shared->insert_last(this->shared, shared_key);
993 while (ids.len > 0)
994 {
995 chunk_t id;
996 identification_t *peer_id;
997
998 ugh = extract_value(&id, &ids);
999 if (ugh != NULL)
1000 {
1001 DBG1(DBG_CFG, "line %d: %s", line_nr, ugh);
1002 return FALSE;
1003 }
1004 if (id.len == 0)
1005 {
1006 continue;
1007 }
1008
1009 /* NULL terminate the ID string */
1010 *(id.ptr + id.len) = '\0';
1011 peer_id = identification_create_from_string(id.ptr);
1012 if (peer_id->get_type(peer_id) == ID_ANY)
1013 {
1014 peer_id->destroy(peer_id);
1015 continue;
1016 }
1017
1018 shared_key->add_owner(shared_key, peer_id);
1019 any = FALSE;
1020 }
1021 if (any)
1022 {
1023 shared_key->add_owner(shared_key,
1024 identification_create_from_encoding(ID_ANY, chunk_empty));
1025 }
1026 return TRUE;
1027 }
1028
1029 /**
1030 * reload ipsec.secrets
1031 */
1032 static void load_secrets(private_stroke_cred_t *this, char *file, int level,
1033 FILE *prompt)
1034 {
1035 size_t bytes;
1036 int line_nr = 0;
1037 chunk_t chunk, src, line;
1038 FILE *fd;
1039 private_key_t *private;
1040 shared_key_t *shared;
1041
1042 DBG1(DBG_CFG, "loading secrets from '%s'", file);
1043
1044 fd = fopen(file, "r");
1045 if (fd == NULL)
1046 {
1047 DBG1(DBG_CFG, "opening secrets file '%s' failed", file);
1048 return;
1049 }
1050
1051 /* TODO: do error checks */
1052 fseek(fd, 0, SEEK_END);
1053 chunk.len = ftell(fd);
1054 rewind(fd);
1055 chunk.ptr = malloc(chunk.len);
1056 bytes = fread(chunk.ptr, 1, chunk.len, fd);
1057 fclose(fd);
1058 src = chunk;
1059
1060 if (level == 0)
1061 {
1062 this->lock->write_lock(this->lock);
1063
1064 /* flush secrets on non-recursive invocation */
1065 while (this->shared->remove_last(this->shared,
1066 (void**)&shared) == SUCCESS)
1067 {
1068 shared->destroy(shared);
1069 }
1070 while (this->private->remove_last(this->private,
1071 (void**)&private) == SUCCESS)
1072 {
1073 private->destroy(private);
1074 }
1075 }
1076
1077 while (fetchline(&src, &line))
1078 {
1079 chunk_t ids, token;
1080 shared_key_type_t type;
1081
1082 line_nr++;
1083
1084 if (!eat_whitespace(&line))
1085 {
1086 continue;
1087 }
1088 if (line.len > strlen("include ") &&
1089 strneq(line.ptr, "include ", strlen("include ")))
1090 {
1091 glob_t buf;
1092 char **expanded, *dir, pattern[PATH_MAX];
1093 u_char *pos;
1094
1095 if (level > MAX_SECRETS_RECURSION)
1096 {
1097 DBG1(DBG_CFG, "maximum level of %d includes reached, ignored",
1098 MAX_SECRETS_RECURSION);
1099 continue;
1100 }
1101 /* terminate filename by space */
1102 line = chunk_skip(line, strlen("include "));
1103 pos = memchr(line.ptr, ' ', line.len);
1104 if (pos)
1105 {
1106 line.len = pos - line.ptr;
1107 }
1108 if (line.len && line.ptr[0] == '/')
1109 {
1110 if (line.len + 1 > sizeof(pattern))
1111 {
1112 DBG1(DBG_CFG, "include pattern too long, ignored");
1113 continue;
1114 }
1115 snprintf(pattern, sizeof(pattern), "%.*s", line.len, line.ptr);
1116 }
1117 else
1118 { /* use directory of current file if relative */
1119 dir = strdup(file);
1120 dir = dirname(dir);
1121
1122 if (line.len + 1 + strlen(dir) + 1 > sizeof(pattern))
1123 {
1124 DBG1(DBG_CFG, "include pattern too long, ignored");
1125 free(dir);
1126 continue;
1127 }
1128 snprintf(pattern, sizeof(pattern), "%s/%.*s",
1129 dir, line.len, line.ptr);
1130 free(dir);
1131 }
1132 if (glob(pattern, GLOB_ERR, NULL, &buf) != 0)
1133 {
1134 DBG1(DBG_CFG, "expanding file expression '%s' failed", pattern);
1135 globfree(&buf);
1136 }
1137 else
1138 {
1139 for (expanded = buf.gl_pathv; *expanded != NULL; expanded++)
1140 {
1141 load_secrets(this, *expanded, level + 1, prompt);
1142 }
1143 }
1144 globfree(&buf);
1145 continue;
1146 }
1147
1148 if (line.len > 2 && strneq(": ", line.ptr, 2))
1149 {
1150 /* no ids, skip the ':' */
1151 ids = chunk_empty;
1152 line.ptr++;
1153 line.len--;
1154 }
1155 else if (extract_token_str(&ids, " : ", &line))
1156 {
1157 /* NULL terminate the extracted id string */
1158 *(ids.ptr + ids.len) = '\0';
1159 }
1160 else
1161 {
1162 DBG1(DBG_CFG, "line %d: missing ' : ' separator", line_nr);
1163 break;
1164 }
1165
1166 if (!eat_whitespace(&line) || !extract_token(&token, ' ', &line))
1167 {
1168 DBG1(DBG_CFG, "line %d: missing token", line_nr);
1169 break;
1170 }
1171 if (match("RSA", &token) || match("ECDSA", &token))
1172 {
1173 if (!load_private(this, line, line_nr, prompt,
1174 match("RSA", &token) ? KEY_RSA : KEY_ECDSA))
1175 {
1176 break;
1177 }
1178 }
1179 else if (match("PIN", &token))
1180 {
1181 if (!load_pin(this, line, line_nr, prompt))
1182 {
1183 break;
1184 }
1185 }
1186 else if ((match("PSK", &token) && (type = SHARED_IKE)) ||
1187 (match("EAP", &token) && (type = SHARED_EAP)) ||
1188 (match("NTLM", &token) && (type = SHARED_NT_HASH)) ||
1189 (match("XAUTH", &token) && (type = SHARED_EAP)))
1190 {
1191 if (!load_shared(this, line, line_nr, type, ids))
1192 {
1193 break;
1194 }
1195 }
1196 else
1197 {
1198 DBG1(DBG_CFG, "line %d: token must be either "
1199 "RSA, ECDSA, PSK, EAP, XAUTH or PIN", line_nr);
1200 break;
1201 }
1202 }
1203 if (level == 0)
1204 {
1205 this->lock->unlock(this->lock);
1206 }
1207 chunk_clear(&chunk);
1208 }
1209
1210 /**
1211 * load all certificates from ipsec.d
1212 */
1213 static void load_certs(private_stroke_cred_t *this)
1214 {
1215 DBG1(DBG_CFG, "loading ca certificates from '%s'",
1216 CA_CERTIFICATE_DIR);
1217 load_certdir(this, CA_CERTIFICATE_DIR, CERT_X509, X509_CA);
1218
1219 DBG1(DBG_CFG, "loading aa certificates from '%s'",
1220 AA_CERTIFICATE_DIR);
1221 load_certdir(this, AA_CERTIFICATE_DIR, CERT_X509, X509_AA);
1222
1223 DBG1(DBG_CFG, "loading ocsp signer certificates from '%s'",
1224 OCSP_CERTIFICATE_DIR);
1225 load_certdir(this, OCSP_CERTIFICATE_DIR, CERT_X509, X509_OCSP_SIGNER);
1226
1227 DBG1(DBG_CFG, "loading attribute certificates from '%s'",
1228 ATTR_CERTIFICATE_DIR);
1229 load_certdir(this, ATTR_CERTIFICATE_DIR, CERT_X509_AC, 0);
1230
1231 DBG1(DBG_CFG, "loading crls from '%s'",
1232 CRL_DIR);
1233 load_certdir(this, CRL_DIR, CERT_X509_CRL, 0);
1234 }
1235
1236 /**
1237 * Implementation of stroke_cred_t.reread.
1238 */
1239 static void reread(private_stroke_cred_t *this, stroke_msg_t *msg, FILE *prompt)
1240 {
1241 if (msg->reread.flags & REREAD_SECRETS)
1242 {
1243 DBG1(DBG_CFG, "rereading secrets");
1244 load_secrets(this, SECRETS_FILE, 0, prompt);
1245 }
1246 if (msg->reread.flags & REREAD_CACERTS)
1247 {
1248 DBG1(DBG_CFG, "rereading ca certificates from '%s'",
1249 CA_CERTIFICATE_DIR);
1250 load_certdir(this, CA_CERTIFICATE_DIR, CERT_X509, X509_CA);
1251 }
1252 if (msg->reread.flags & REREAD_OCSPCERTS)
1253 {
1254 DBG1(DBG_CFG, "rereading ocsp signer certificates from '%s'",
1255 OCSP_CERTIFICATE_DIR);
1256 load_certdir(this, OCSP_CERTIFICATE_DIR, CERT_X509,
1257 X509_OCSP_SIGNER);
1258 }
1259 if (msg->reread.flags & REREAD_AACERTS)
1260 {
1261 DBG1(DBG_CFG, "rereading aa certificates from '%s'",
1262 AA_CERTIFICATE_DIR);
1263 load_certdir(this, AA_CERTIFICATE_DIR, CERT_X509, X509_AA);
1264 }
1265 if (msg->reread.flags & REREAD_ACERTS)
1266 {
1267 DBG1(DBG_CFG, "rereading attribute certificates from '%s'",
1268 ATTR_CERTIFICATE_DIR);
1269 load_certdir(this, ATTR_CERTIFICATE_DIR, CERT_X509_AC, 0);
1270 }
1271 if (msg->reread.flags & REREAD_CRLS)
1272 {
1273 DBG1(DBG_CFG, "rereading crls from '%s'",
1274 CRL_DIR);
1275 load_certdir(this, CRL_DIR, CERT_X509_CRL, 0);
1276 }
1277 }
1278
1279 /**
1280 * Implementation of stroke_cred_t.destroy
1281 */
1282 static void destroy(private_stroke_cred_t *this)
1283 {
1284 this->certs->destroy_offset(this->certs, offsetof(certificate_t, destroy));
1285 this->shared->destroy_offset(this->shared, offsetof(shared_key_t, destroy));
1286 this->private->destroy_offset(this->private, offsetof(private_key_t, destroy));
1287 this->lock->destroy(this->lock);
1288 free(this);
1289 }
1290
1291 /*
1292 * see header file
1293 */
1294 stroke_cred_t *stroke_cred_create()
1295 {
1296 private_stroke_cred_t *this = malloc_thing(private_stroke_cred_t);
1297
1298 this->public.set.create_private_enumerator = (void*)create_private_enumerator;
1299 this->public.set.create_cert_enumerator = (void*)create_cert_enumerator;
1300 this->public.set.create_shared_enumerator = (void*)create_shared_enumerator;
1301 this->public.set.create_cdp_enumerator = (void*)return_null;
1302 this->public.set.cache_cert = (void*)cache_cert;
1303 this->public.reread = (void(*)(stroke_cred_t*, stroke_msg_t *msg, FILE*))reread;
1304 this->public.load_ca = (certificate_t*(*)(stroke_cred_t*, char *filename))load_ca;
1305 this->public.load_peer = (certificate_t*(*)(stroke_cred_t*, char *filename))load_peer;
1306 this->public.cachecrl = (void(*)(stroke_cred_t*, bool enabled))cachecrl;
1307 this->public.destroy = (void(*)(stroke_cred_t*))destroy;
1308
1309 this->certs = linked_list_create();
1310 this->shared = linked_list_create();
1311 this->private = linked_list_create();
1312 this->lock = rwlock_create(RWLOCK_TYPE_DEFAULT);
1313
1314 load_certs(this);
1315 load_secrets(this, SECRETS_FILE, 0, NULL);
1316
1317 this->cachecrl = FALSE;
1318
1319 return &this->public;
1320 }
1321