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