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