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