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