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