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