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