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