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