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