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