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