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