Credential backends use has_fingerprint() methods to select keys/certificates
[strongswan.git] / src / charon / plugins / stroke / stroke_cred.c
1 /*
2 * Copyright (C) 2008 Tobias Brunner
3 * Copyright (C) 2008 Martin Willi
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include <sys/stat.h>
18 #include <limits.h>
19 #include <glob.h>
20 #include <libgen.h>
21
22 #include "stroke_cred.h"
23 #include "stroke_shared_key.h"
24
25 #include <credentials/certificates/x509.h>
26 #include <credentials/certificates/crl.h>
27 #include <credentials/certificates/ac.h>
28 #include <utils/linked_list.h>
29 #include <utils/lexparser.h>
30 #include <utils/mutex.h>
31 #include <daemon.h>
32
33 /* configuration directories and files */
34 #define CONFIG_DIR IPSEC_CONFDIR
35 #define IPSEC_D_DIR CONFIG_DIR "/ipsec.d"
36 #define PRIVATE_KEY_DIR IPSEC_D_DIR "/private"
37 #define CERTIFICATE_DIR IPSEC_D_DIR "/certs"
38 #define CA_CERTIFICATE_DIR IPSEC_D_DIR "/cacerts"
39 #define AA_CERTIFICATE_DIR IPSEC_D_DIR "/aacerts"
40 #define ATTR_CERTIFICATE_DIR IPSEC_D_DIR "/acerts"
41 #define OCSP_CERTIFICATE_DIR IPSEC_D_DIR "/ocspcerts"
42 #define CRL_DIR IPSEC_D_DIR "/crls"
43 #define SECRETS_FILE CONFIG_DIR "/ipsec.secrets"
44
45 #define MAX_SECRETS_RECURSION 10
46
47 typedef struct private_stroke_cred_t private_stroke_cred_t;
48
49 /**
50 * private data of stroke_cred
51 */
52 struct private_stroke_cred_t {
53
54 /**
55 * public functions
56 */
57 stroke_cred_t public;
58
59 /**
60 * list of trusted peer/signer/CA certificates (certificate_t)
61 */
62 linked_list_t *certs;
63
64 /**
65 * list of shared secrets (private_shared_key_t)
66 */
67 linked_list_t *shared;
68
69 /**
70 * list of private keys (private_key_t)
71 */
72 linked_list_t *private;
73
74 /**
75 * read-write lock to lists
76 */
77 rwlock_t *lock;
78
79 /**
80 * cache CRLs to disk?
81 */
82 bool cachecrl;
83 };
84
85 /**
86 * data to pass to various filters
87 */
88 typedef struct {
89 private_stroke_cred_t *this;
90 identification_t *id;
91 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, other_match;
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 my_match = stroke->has_owner(stroke, data->me);
233 other_match = stroke->has_owner(stroke, data->other);
234 if (!my_match && !other_match)
235 {
236 return FALSE;
237 }
238 *out = shared;
239 if (me)
240 {
241 *me = my_match;
242 }
243 if (other)
244 {
245 *other = other_match;
246 }
247 return TRUE;
248 }
249
250 /**
251 * Implements credential_set_t.create_shared_enumerator
252 */
253 static enumerator_t* create_shared_enumerator(private_stroke_cred_t *this,
254 shared_key_type_t type, identification_t *me,
255 identification_t *other)
256 {
257 shared_data_t *data = malloc_thing(shared_data_t);
258
259 data->this = this;
260 data->me = me;
261 data->other = other;
262 data->type = type;
263 this->lock->read_lock(this->lock);
264 return enumerator_create_filter(this->shared->create_enumerator(this->shared),
265 (void*)shared_filter, data,
266 (void*)shared_data_destroy);
267 }
268
269 /**
270 * Add a certificate to chain
271 */
272 static certificate_t* add_cert(private_stroke_cred_t *this, certificate_t *cert)
273 {
274 certificate_t *current;
275 enumerator_t *enumerator;
276 bool new = TRUE;
277
278 this->lock->read_lock(this->lock);
279 enumerator = this->certs->create_enumerator(this->certs);
280 while (enumerator->enumerate(enumerator, (void**)&current))
281 {
282 if (current->equals(current, cert))
283 {
284 /* cert already in queue */
285 cert->destroy(cert);
286 cert = current;
287 new = FALSE;
288 break;
289 }
290 }
291 enumerator->destroy(enumerator);
292
293 if (new)
294 {
295 this->certs->insert_last(this->certs, cert);
296 }
297 this->lock->unlock(this->lock);
298 return cert;
299 }
300
301 /**
302 * Implementation of stroke_cred_t.load_ca.
303 */
304 static certificate_t* load_ca(private_stroke_cred_t *this, char *filename)
305 {
306 certificate_t *cert;
307 char path[PATH_MAX];
308
309 if (*filename == '/')
310 {
311 snprintf(path, sizeof(path), "%s", filename);
312 }
313 else
314 {
315 snprintf(path, sizeof(path), "%s/%s", CA_CERTIFICATE_DIR, filename);
316 }
317
318 cert = lib->creds->create(lib->creds,
319 CRED_CERTIFICATE, CERT_X509,
320 BUILD_FROM_FILE, path,
321 BUILD_END);
322 if (cert)
323 {
324 x509_t *x509 = (x509_t*)cert;
325
326 if (!(x509->get_flags(x509) & X509_CA))
327 {
328 DBG1(DBG_CFG, " ca certificate '%Y' misses ca basic constraint, "
329 "discarded", cert->get_subject(cert));
330 cert->destroy(cert);
331 return NULL;
332 }
333 return (certificate_t*)add_cert(this, cert);
334 }
335 return NULL;
336 }
337
338 /**
339 * Add X.509 CRL to chain
340 */
341 static bool add_crl(private_stroke_cred_t *this, crl_t* crl)
342 {
343 certificate_t *current, *cert = &crl->certificate;
344 enumerator_t *enumerator;
345 bool new = TRUE, found = FALSE;
346
347 this->lock->write_lock(this->lock);
348 enumerator = this->certs->create_enumerator(this->certs);
349 while (enumerator->enumerate(enumerator, (void**)&current))
350 {
351 if (current->get_type(current) == CERT_X509_CRL)
352 {
353 crl_t *crl_c = (crl_t*)current;
354 chunk_t authkey = crl->get_authKeyIdentifier(crl);
355 chunk_t authkey_c = crl_c->get_authKeyIdentifier(crl_c);
356
357 /* if compare authorityKeyIdentifiers if available */
358 if (authkey.ptr && authkey_c.ptr && chunk_equals(authkey, authkey_c))
359 {
360 found = TRUE;
361 }
362 else
363 {
364 identification_t *issuer = cert->get_issuer(cert);
365 identification_t *issuer_c = current->get_issuer(current);
366
367 /* otherwise compare issuer distinguished names */
368 if (issuer->equals(issuer, issuer_c))
369 {
370 found = TRUE;
371 }
372 }
373 if (found)
374 {
375 new = cert->is_newer(cert, current);
376 if (new)
377 {
378 this->certs->remove_at(this->certs, enumerator);
379 }
380 else
381 {
382 cert->destroy(cert);
383 }
384 break;
385 }
386 }
387 }
388 enumerator->destroy(enumerator);
389
390 if (new)
391 {
392 this->certs->insert_last(this->certs, cert);
393 }
394 this->lock->unlock(this->lock);
395 return new;
396 }
397
398 /**
399 * Add X.509 attribute certificate to chain
400 */
401 static bool add_ac(private_stroke_cred_t *this, ac_t* ac)
402 {
403 certificate_t *cert = &ac->certificate;
404
405 this->lock->write_lock(this->lock);
406 this->certs->insert_last(this->certs, cert);
407 this->lock->unlock(this->lock);
408 return TRUE;
409 }
410
411 /**
412 * Implementation of stroke_cred_t.load_peer.
413 */
414 static certificate_t* load_peer(private_stroke_cred_t *this, char *filename)
415 {
416 certificate_t *cert;
417 char path[PATH_MAX];
418
419 if (*filename == '/')
420 {
421 snprintf(path, sizeof(path), "%s", filename);
422 }
423 else
424 {
425 snprintf(path, sizeof(path), "%s/%s", CERTIFICATE_DIR, filename);
426 }
427
428 cert = lib->creds->create(lib->creds,
429 CRED_CERTIFICATE, CERT_ANY,
430 BUILD_FROM_FILE, path,
431 BUILD_END);
432 if (cert)
433 {
434 cert = add_cert(this, cert);
435 DBG1(DBG_CFG, " loaded certificate '%Y' from "
436 "file '%s'", cert->get_subject(cert), filename);
437 return cert->get_ref(cert);
438 }
439 DBG1(DBG_CFG, " loading certificate from file "
440 "'%s' failed", filename);
441 return NULL;
442 }
443
444 /**
445 * load trusted certificates from a directory
446 */
447 static void load_certdir(private_stroke_cred_t *this, char *path,
448 certificate_type_t type, x509_flag_t flag)
449 {
450 struct stat st;
451 char *file;
452
453 enumerator_t *enumerator = enumerator_create_directory(path);
454
455 if (!enumerator)
456 {
457 DBG1(DBG_CFG, " reading directory failed");
458 return;
459 }
460
461 while (enumerator->enumerate(enumerator, NULL, &file, &st))
462 {
463 certificate_t *cert;
464
465 if (!S_ISREG(st.st_mode))
466 {
467 /* skip special file */
468 continue;
469 }
470 switch (type)
471 {
472 case CERT_X509:
473 if (flag & X509_CA)
474 { /* for CA certificates, we strictly require CA
475 * basicconstraints to be set */
476 cert = lib->creds->create(lib->creds,
477 CRED_CERTIFICATE, CERT_X509,
478 BUILD_FROM_FILE, file, BUILD_END);
479 if (cert)
480 {
481 x509_t *x509 = (x509_t*)cert;
482
483 if (!(x509->get_flags(x509) & X509_CA))
484 {
485 DBG1(DBG_CFG, " ca certificate '%Y' misses "
486 "ca basic constraint, discarded",
487 cert->get_subject(cert));
488 cert->destroy(cert);
489 cert = NULL;
490 }
491 else
492 {
493 DBG1(DBG_CFG, " loaded CA certificate '%Y' from "
494 "file '%s'", cert->get_subject(cert), file);
495 }
496 }
497 else
498 {
499 DBG1(DBG_CFG, " loading CA certificate from file "
500 "'%s' failed", file);
501 }
502 }
503 else
504 { /* for all other flags, we add them to the certificate. */
505 cert = lib->creds->create(lib->creds,
506 CRED_CERTIFICATE, CERT_X509,
507 BUILD_FROM_FILE, file,
508 BUILD_X509_FLAG, flag, BUILD_END);
509 if (cert)
510 {
511 DBG1(DBG_CFG, " loaded certificate '%Y' from "
512 "file '%s'", cert->get_subject(cert), file);
513 }
514 else
515 {
516 DBG1(DBG_CFG, " loading certificate from file "
517 "'%s' failed", file);
518 }
519 }
520 if (cert)
521 {
522 add_cert(this, cert);
523 }
524 break;
525 case CERT_X509_CRL:
526 cert = lib->creds->create(lib->creds,
527 CRED_CERTIFICATE, CERT_X509_CRL,
528 BUILD_FROM_FILE, file,
529 BUILD_END);
530 if (cert)
531 {
532 add_crl(this, (crl_t*)cert);
533 DBG1(DBG_CFG, " loaded crl from file '%s'", file);
534 }
535 else
536 {
537 DBG1(DBG_CFG, " loading crl from file '%s' failed", file);
538 }
539 break;
540 case CERT_X509_AC:
541 cert = lib->creds->create(lib->creds,
542 CRED_CERTIFICATE, CERT_X509_AC,
543 BUILD_FROM_FILE, file,
544 BUILD_END);
545 if (cert)
546 {
547 add_ac(this, (ac_t*)cert);
548 DBG1(DBG_CFG, " loaded attribute certificate from "
549 "file '%s'", file);
550 }
551 else
552 {
553 DBG1(DBG_CFG, " loading attribute certificate from "
554 "file '%s' failed", file);
555 }
556 break;
557 default:
558 break;
559 }
560 }
561 enumerator->destroy(enumerator);
562 }
563
564 /**
565 * Implementation of credential_set_t.cache_cert.
566 */
567 static void cache_cert(private_stroke_cred_t *this, certificate_t *cert)
568 {
569 if (cert->get_type(cert) == CERT_X509_CRL && this->cachecrl)
570 {
571 /* CRLs get written to /etc/ipsec.d/crls/<authkeyId>.crl */
572 crl_t *crl = (crl_t*)cert;
573
574 cert->get_ref(cert);
575 if (add_crl(this, crl))
576 {
577 char buf[BUF_LEN];
578 chunk_t chunk, hex;
579
580 chunk = crl->get_authKeyIdentifier(crl);
581 hex = chunk_to_hex(chunk, NULL, FALSE);
582 snprintf(buf, sizeof(buf), "%s/%s.crl", CRL_DIR, hex);
583 free(hex.ptr);
584
585 chunk = cert->get_encoding(cert);
586 chunk_write(chunk, buf, "crl", 022, TRUE);
587 free(chunk.ptr);
588 }
589 }
590 }
591
592 /**
593 * Implementation of stroke_cred_t.cachecrl.
594 */
595 static void cachecrl(private_stroke_cred_t *this, bool enabled)
596 {
597 DBG1(DBG_CFG, "crl caching to %s %s",
598 CRL_DIR, enabled ? "enabled" : "disabled");
599 this->cachecrl = enabled;
600 }
601
602
603 /**
604 * Convert a string of characters into a binary secret
605 * A string between single or double quotes is treated as ASCII characters
606 * A string prepended by 0x is treated as HEX and prepended by 0s as Base64
607 */
608 static err_t extract_secret(chunk_t *secret, chunk_t *line)
609 {
610 chunk_t raw_secret;
611 char delimiter = ' ';
612 bool quotes = FALSE;
613
614 if (!eat_whitespace(line))
615 {
616 return "missing secret";
617 }
618
619 if (*line->ptr == '\'' || *line->ptr == '"')
620 {
621 quotes = TRUE;
622 delimiter = *line->ptr;
623 line->ptr++; line->len--;
624 }
625
626 if (!extract_token(&raw_secret, delimiter, line))
627 {
628 if (delimiter == ' ')
629 {
630 raw_secret = *line;
631 }
632 else
633 {
634 return "missing second delimiter";
635 }
636 }
637
638 if (quotes)
639 {
640 /* treat as an ASCII string */
641 *secret = chunk_clone(raw_secret);
642 return NULL;
643 }
644 /* treat 0x as hex, 0s as base64 */
645 if (raw_secret.len > 2)
646 {
647 if (strncasecmp("0x", raw_secret.ptr, 2) == 0)
648 {
649 *secret = chunk_from_hex(chunk_skip(raw_secret, 2), NULL);
650 return NULL;
651 }
652 if (strncasecmp("0s", raw_secret.ptr, 2) == 0)
653 {
654 *secret = chunk_from_base64(chunk_skip(raw_secret, 2), NULL);
655 return NULL;
656 }
657 }
658 *secret = chunk_clone(raw_secret);
659 return NULL;
660 }
661
662 /**
663 * Data to pass to passphrase_cb
664 */
665 typedef struct {
666 /** socket we use for prompting */
667 FILE *prompt;
668 /** private key file */
669 char *file;
670 /** buffer for passphrase */
671 char buf[256];
672 } passphrase_cb_data_t;
673
674 /**
675 * Passphrase callback to read from whack fd
676 */
677 chunk_t passphrase_cb(passphrase_cb_data_t *data, int try)
678 {
679 chunk_t secret = chunk_empty;;
680
681 if (try > 5)
682 {
683 fprintf(data->prompt, "invalid passphrase, too many trials\n");
684 return chunk_empty;
685 }
686 if (try == 1)
687 {
688 fprintf(data->prompt, "Private key '%s' is encrypted\n", data->file);
689 }
690 else
691 {
692 fprintf(data->prompt, "invalid passphrase\n");
693 }
694 fprintf(data->prompt, "Passphrase:\n");
695 if (fgets(data->buf, sizeof(data->buf), data->prompt))
696 {
697 secret = chunk_create(data->buf, strlen(data->buf));
698 if (secret.len)
699 { /* trim appended \n */
700 secret.len--;
701 }
702 }
703 return secret;
704 }
705
706 /**
707 * reload ipsec.secrets
708 */
709 static void load_secrets(private_stroke_cred_t *this, char *file, int level,
710 FILE *prompt)
711 {
712 size_t bytes;
713 int line_nr = 0;
714 chunk_t chunk, src, line;
715 FILE *fd;
716 private_key_t *private;
717 shared_key_t *shared;
718
719 DBG1(DBG_CFG, "loading secrets from '%s'", file);
720
721 fd = fopen(file, "r");
722 if (fd == NULL)
723 {
724 DBG1(DBG_CFG, "opening secrets file '%s' failed", file);
725 return;
726 }
727
728 /* TODO: do error checks */
729 fseek(fd, 0, SEEK_END);
730 chunk.len = ftell(fd);
731 rewind(fd);
732 chunk.ptr = malloc(chunk.len);
733 bytes = fread(chunk.ptr, 1, chunk.len, fd);
734 fclose(fd);
735 src = chunk;
736
737 if (level == 0)
738 {
739 this->lock->write_lock(this->lock);
740
741 /* flush secrets on non-recursive invocation */
742 while (this->shared->remove_last(this->shared,
743 (void**)&shared) == SUCCESS)
744 {
745 shared->destroy(shared);
746 }
747 while (this->private->remove_last(this->private,
748 (void**)&private) == SUCCESS)
749 {
750 private->destroy(private);
751 }
752 }
753
754 while (fetchline(&src, &line))
755 {
756 chunk_t ids, token;
757 shared_key_type_t type;
758
759 line_nr++;
760
761 if (!eat_whitespace(&line))
762 {
763 continue;
764 }
765 if (line.len > strlen("include ") &&
766 strneq(line.ptr, "include ", strlen("include ")))
767 {
768 glob_t buf;
769 char **expanded, *dir, pattern[PATH_MAX];
770 u_char *pos;
771
772 if (level > MAX_SECRETS_RECURSION)
773 {
774 DBG1(DBG_CFG, "maximum level of %d includes reached, ignored",
775 MAX_SECRETS_RECURSION);
776 continue;
777 }
778 /* terminate filename by space */
779 line = chunk_skip(line, strlen("include "));
780 pos = memchr(line.ptr, ' ', line.len);
781 if (pos)
782 {
783 line.len = pos - line.ptr;
784 }
785 if (line.len && line.ptr[0] == '/')
786 {
787 if (line.len + 1 > sizeof(pattern))
788 {
789 DBG1(DBG_CFG, "include pattern too long, ignored");
790 continue;
791 }
792 snprintf(pattern, sizeof(pattern), "%.*s", line.len, line.ptr);
793 }
794 else
795 { /* use directory of current file if relative */
796 dir = strdup(file);
797 dir = dirname(dir);
798
799 if (line.len + 1 + strlen(dir) + 1 > sizeof(pattern))
800 {
801 DBG1(DBG_CFG, "include pattern too long, ignored");
802 free(dir);
803 continue;
804 }
805 snprintf(pattern, sizeof(pattern), "%s/%.*s",
806 dir, line.len, line.ptr);
807 free(dir);
808 }
809 if (glob(pattern, GLOB_ERR, NULL, &buf) != 0)
810 {
811 DBG1(DBG_CFG, "expanding file expression '%s' failed", pattern);
812 globfree(&buf);
813 }
814 else
815 {
816 for (expanded = buf.gl_pathv; *expanded != NULL; expanded++)
817 {
818 load_secrets(this, *expanded, level + 1, prompt);
819 }
820 }
821 globfree(&buf);
822 continue;
823 }
824
825 if (line.len > 2 && strneq(": ", line.ptr, 2))
826 {
827 /* no ids, skip the ':' */
828 ids = chunk_empty;
829 line.ptr++;
830 line.len--;
831 }
832 else if (extract_token_str(&ids, " : ", &line))
833 {
834 /* NULL terminate the extracted id string */
835 *(ids.ptr + ids.len) = '\0';
836 }
837 else
838 {
839 DBG1(DBG_CFG, "line %d: missing ' : ' separator", line_nr);
840 goto error;
841 }
842
843 if (!eat_whitespace(&line) || !extract_token(&token, ' ', &line))
844 {
845 DBG1(DBG_CFG, "line %d: missing token", line_nr);
846 goto error;
847 }
848 if (match("RSA", &token) || match("ECDSA", &token))
849 {
850 char path[PATH_MAX];
851 chunk_t filename;
852 chunk_t secret = chunk_empty;
853 private_key_t *key = NULL;
854 key_type_t key_type = match("RSA", &token) ? KEY_RSA : KEY_ECDSA;
855
856 err_t ugh = extract_value(&filename, &line);
857
858 if (ugh != NULL)
859 {
860 DBG1(DBG_CFG, "line %d: %s", line_nr, ugh);
861 goto error;
862 }
863 if (filename.len == 0)
864 {
865 DBG1(DBG_CFG, "line %d: empty filename", line_nr);
866 goto error;
867 }
868 if (*filename.ptr == '/')
869 {
870 /* absolute path name */
871 snprintf(path, sizeof(path), "%.*s", filename.len, filename.ptr);
872 }
873 else
874 {
875 /* relative path name */
876 snprintf(path, sizeof(path), "%s/%.*s", PRIVATE_KEY_DIR,
877 filename.len, filename.ptr);
878 }
879
880 /* check for optional passphrase */
881 if (eat_whitespace(&line))
882 {
883 ugh = extract_secret(&secret, &line);
884 if (ugh != NULL)
885 {
886 DBG1(DBG_CFG, "line %d: malformed passphrase: %s", line_nr, ugh);
887 goto error;
888 }
889 }
890 if (secret.len == 7 && strneq(secret.ptr, "%prompt", 7))
891 {
892 if (prompt)
893 {
894 passphrase_cb_data_t data;
895
896 data.prompt = prompt;
897 data.file = path;
898 key = lib->creds->create(lib->creds, CRED_PRIVATE_KEY,
899 key_type, BUILD_FROM_FILE, path,
900 BUILD_PASSPHRASE_CALLBACK,
901 passphrase_cb, &data, BUILD_END);
902 }
903 }
904 else
905 {
906 key = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, key_type,
907 BUILD_FROM_FILE, path,
908 BUILD_PASSPHRASE, secret, BUILD_END);
909 }
910 if (key)
911 {
912 DBG1(DBG_CFG, " loaded %N private key file '%s'",
913 key_type_names, key->get_type(key), path);
914 this->private->insert_last(this->private, key);
915 }
916 else
917 {
918 DBG1(DBG_CFG, " skipped private key file '%s'", path);
919 }
920 chunk_clear(&secret);
921 }
922 else if (match("PIN", &token))
923 {
924 chunk_t sc = chunk_empty, secret = chunk_empty;
925 char smartcard[32], keyid[22], pin[32];
926 private_key_t *key;
927 u_int slot;
928
929 err_t ugh = extract_value(&sc, &line);
930
931 if (ugh != NULL)
932 {
933 DBG1(DBG_CFG, "line %d: %s", line_nr, ugh);
934 goto error;
935 }
936 if (sc.len == 0)
937 {
938 DBG1(DBG_CFG, "line %d: expected %%smartcard specifier", line_nr);
939 goto error;
940 }
941 snprintf(smartcard, sizeof(smartcard), "%.*s", sc.len, sc.ptr);
942 smartcard[sizeof(smartcard) - 1] = '\0';
943
944 /* parse slot and key id. only two formats are supported.
945 * first try %smartcard<slot>:<keyid> */
946 if (sscanf(smartcard, "%%smartcard%u:%s", &slot, keyid) == 2)
947 {
948 snprintf(smartcard, sizeof(smartcard), "%u:%s", slot, keyid);
949 }
950 /* then try %smartcard:<keyid> */
951 else if (sscanf(smartcard, "%%smartcard:%s", keyid) == 1)
952 {
953 snprintf(smartcard, sizeof(smartcard), "%s", keyid);
954 }
955 else
956 {
957 DBG1(DBG_CFG, "line %d: the given %%smartcard specifier is not"
958 " supported or invalid", line_nr);
959 goto error;
960 }
961
962 if (!eat_whitespace(&line))
963 {
964 DBG1(DBG_CFG, "line %d: expected PIN", line_nr);
965 goto error;
966 }
967 ugh = extract_secret(&secret, &line);
968 if (ugh != NULL)
969 {
970 DBG1(DBG_CFG, "line %d: malformed PIN: %s", line_nr, ugh);
971 goto error;
972 }
973 snprintf(pin, sizeof(pin), "%.*s", secret.len, secret.ptr);
974 pin[sizeof(pin) - 1] = '\0';
975
976 /* we assume an RSA key */
977 key = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, KEY_RSA,
978 BUILD_SMARTCARD_KEYID, smartcard,
979 BUILD_SMARTCARD_PIN, pin, BUILD_END);
980
981 if (key)
982 {
983 DBG1(DBG_CFG, " loaded private key from %.*s", sc.len, sc.ptr);
984 this->private->insert_last(this->private, key);
985 }
986 memset(pin, 0, sizeof(pin));
987 chunk_clear(&secret);
988 }
989 else if ((match("PSK", &token) && (type = SHARED_IKE)) ||
990 (match("EAP", &token) && (type = SHARED_EAP)) ||
991 (match("XAUTH", &token) && (type = SHARED_EAP)))
992 {
993 stroke_shared_key_t *shared_key;
994 chunk_t secret = chunk_empty;
995 bool any = TRUE;
996
997 err_t ugh = extract_secret(&secret, &line);
998 if (ugh != NULL)
999 {
1000 DBG1(DBG_CFG, "line %d: malformed secret: %s", line_nr, ugh);
1001 goto error;
1002 }
1003 shared_key = stroke_shared_key_create(type, secret);
1004 DBG1(DBG_CFG, " loaded %N secret for %s", shared_key_type_names, type,
1005 ids.len > 0 ? (char*)ids.ptr : "%any");
1006 DBG4(DBG_CFG, " secret: %#B", &secret);
1007
1008 this->shared->insert_last(this->shared, shared_key);
1009 while (ids.len > 0)
1010 {
1011 chunk_t id;
1012 identification_t *peer_id;
1013
1014 ugh = extract_value(&id, &ids);
1015 if (ugh != NULL)
1016 {
1017 DBG1(DBG_CFG, "line %d: %s", line_nr, ugh);
1018 goto error;
1019 }
1020 if (id.len == 0)
1021 {
1022 continue;
1023 }
1024
1025 /* NULL terminate the ID string */
1026 *(id.ptr + id.len) = '\0';
1027 peer_id = identification_create_from_string(id.ptr);
1028 if (peer_id->get_type(peer_id) == ID_ANY)
1029 {
1030 peer_id->destroy(peer_id);
1031 continue;
1032 }
1033
1034 shared_key->add_owner(shared_key, peer_id);
1035 any = FALSE;
1036 }
1037 if (any)
1038 {
1039 shared_key->add_owner(shared_key,
1040 identification_create_from_encoding(ID_ANY, chunk_empty));
1041 }
1042 }
1043 else
1044 {
1045 DBG1(DBG_CFG, "line %d: token must be either "
1046 "RSA, ECDSA, PSK, EAP, XAUTH or PIN", line_nr);
1047 goto error;
1048 }
1049 }
1050 error:
1051 if (level == 0)
1052 {
1053 this->lock->unlock(this->lock);
1054 }
1055 chunk_clear(&chunk);
1056 }
1057
1058 /**
1059 * load all certificates from ipsec.d
1060 */
1061 static void load_certs(private_stroke_cred_t *this)
1062 {
1063 DBG1(DBG_CFG, "loading ca certificates from '%s'",
1064 CA_CERTIFICATE_DIR);
1065 load_certdir(this, CA_CERTIFICATE_DIR, CERT_X509, X509_CA);
1066
1067 DBG1(DBG_CFG, "loading aa certificates from '%s'",
1068 AA_CERTIFICATE_DIR);
1069 load_certdir(this, AA_CERTIFICATE_DIR, CERT_X509, X509_AA);
1070
1071 DBG1(DBG_CFG, "loading ocsp signer certificates from '%s'",
1072 OCSP_CERTIFICATE_DIR);
1073 load_certdir(this, OCSP_CERTIFICATE_DIR, CERT_X509, X509_OCSP_SIGNER);
1074
1075 DBG1(DBG_CFG, "loading attribute certificates from '%s'",
1076 ATTR_CERTIFICATE_DIR);
1077 load_certdir(this, ATTR_CERTIFICATE_DIR, CERT_X509_AC, 0);
1078
1079 DBG1(DBG_CFG, "loading crls from '%s'",
1080 CRL_DIR);
1081 load_certdir(this, CRL_DIR, CERT_X509_CRL, 0);
1082 }
1083
1084 /**
1085 * Implementation of stroke_cred_t.reread.
1086 */
1087 static void reread(private_stroke_cred_t *this, stroke_msg_t *msg, FILE *prompt)
1088 {
1089 if (msg->reread.flags & REREAD_SECRETS)
1090 {
1091 DBG1(DBG_CFG, "rereading secrets");
1092 load_secrets(this, SECRETS_FILE, 0, prompt);
1093 }
1094 if (msg->reread.flags & REREAD_CACERTS)
1095 {
1096 DBG1(DBG_CFG, "rereading ca certificates from '%s'",
1097 CA_CERTIFICATE_DIR);
1098 load_certdir(this, CA_CERTIFICATE_DIR, CERT_X509, X509_CA);
1099 }
1100 if (msg->reread.flags & REREAD_OCSPCERTS)
1101 {
1102 DBG1(DBG_CFG, "rereading ocsp signer certificates from '%s'",
1103 OCSP_CERTIFICATE_DIR);
1104 load_certdir(this, OCSP_CERTIFICATE_DIR, CERT_X509,
1105 X509_OCSP_SIGNER);
1106 }
1107 if (msg->reread.flags & REREAD_AACERTS)
1108 {
1109 DBG1(DBG_CFG, "rereading aa certificates from '%s'",
1110 AA_CERTIFICATE_DIR);
1111 load_certdir(this, AA_CERTIFICATE_DIR, CERT_X509, X509_AA);
1112 }
1113 if (msg->reread.flags & REREAD_ACERTS)
1114 {
1115 DBG1(DBG_CFG, "rereading attribute certificates from '%s'",
1116 ATTR_CERTIFICATE_DIR);
1117 load_certdir(this, ATTR_CERTIFICATE_DIR, CERT_X509_AC, 0);
1118 }
1119 if (msg->reread.flags & REREAD_CRLS)
1120 {
1121 DBG1(DBG_CFG, "rereading crls from '%s'",
1122 CRL_DIR);
1123 load_certdir(this, CRL_DIR, CERT_X509_CRL, 0);
1124 }
1125 }
1126
1127 /**
1128 * Implementation of stroke_cred_t.destroy
1129 */
1130 static void destroy(private_stroke_cred_t *this)
1131 {
1132 this->certs->destroy_offset(this->certs, offsetof(certificate_t, destroy));
1133 this->shared->destroy_offset(this->shared, offsetof(shared_key_t, destroy));
1134 this->private->destroy_offset(this->private, offsetof(private_key_t, destroy));
1135 this->lock->destroy(this->lock);
1136 free(this);
1137 }
1138
1139 /*
1140 * see header file
1141 */
1142 stroke_cred_t *stroke_cred_create()
1143 {
1144 private_stroke_cred_t *this = malloc_thing(private_stroke_cred_t);
1145
1146 this->public.set.create_private_enumerator = (void*)create_private_enumerator;
1147 this->public.set.create_cert_enumerator = (void*)create_cert_enumerator;
1148 this->public.set.create_shared_enumerator = (void*)create_shared_enumerator;
1149 this->public.set.create_cdp_enumerator = (void*)return_null;
1150 this->public.set.cache_cert = (void*)cache_cert;
1151 this->public.reread = (void(*)(stroke_cred_t*, stroke_msg_t *msg, FILE*))reread;
1152 this->public.load_ca = (certificate_t*(*)(stroke_cred_t*, char *filename))load_ca;
1153 this->public.load_peer = (certificate_t*(*)(stroke_cred_t*, char *filename))load_peer;
1154 this->public.cachecrl = (void(*)(stroke_cred_t*, bool enabled))cachecrl;
1155 this->public.destroy = (void(*)(stroke_cred_t*))destroy;
1156
1157 this->certs = linked_list_create();
1158 this->shared = linked_list_create();
1159 this->private = linked_list_create();
1160 this->lock = rwlock_create(RWLOCK_TYPE_DEFAULT);
1161
1162 load_certs(this);
1163 load_secrets(this, SECRETS_FILE, 0, NULL);
1164
1165 this->cachecrl = FALSE;
1166
1167 return &this->public;
1168 }
1169