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