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