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