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