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