2 * Copyright (C) 2008-2015 Tobias Brunner
3 * Copyright (C) 2008 Martin Willi
4 * Hochschule fuer Technik Rapperswil
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>.
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
17 #include <sys/types.h>
28 #include "stroke_cred.h"
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>
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"
53 #define MAX_SECRETS_RECURSION 10
55 typedef struct private_stroke_cred_t private_stroke_cred_t
;
58 * private data of stroke_cred
60 struct private_stroke_cred_t
{
68 * secrets file with credential information
73 * credentials: end entity certs, attribute certs, CRLs, etc.
78 * Attribute Authority certificates
83 * ignore missing CA basic constraint (i.e. treat all certificates in
84 * ipsec.conf ca sections and ipsec.d/cacerts as CA certificates)
94 * CA certificate store
99 /** Length of smartcard specifier parts (module, keyid) */
100 #define SC_PART_LEN 128
103 * Kind of smartcard specifier token
106 SC_FORMAT_SLOT_MODULE_KEYID
,
107 SC_FORMAT_SLOT_KEYID
,
110 } smartcard_format_t
;
113 * Parse a smartcard specifier token
115 static smartcard_format_t
parse_smartcard(char *smartcard
, u_int
*slot
,
116 char *module
, char *keyid
)
118 /* The token has one of the following three formats:
119 * - %smartcard<slot>@<module>:<keyid>
120 * - %smartcard<slot>:<keyid>
121 * - %smartcard:<keyid>
123 char buf
[2 * SC_PART_LEN
], *pos
;
125 if (sscanf(smartcard
, "%%smartcard%u@%255s", slot
, buf
) == 2)
127 pos
= strchr(buf
, ':');
130 return SC_FORMAT_INVALID
;
133 snprintf(module
, SC_PART_LEN
, "%s", buf
);
134 snprintf(keyid
, SC_PART_LEN
, "%s", pos
);
135 return SC_FORMAT_SLOT_MODULE_KEYID
;
137 if (sscanf(smartcard
, "%%smartcard%u:%127s", slot
, keyid
) == 2)
139 return SC_FORMAT_SLOT_KEYID
;
141 if (sscanf(smartcard
, "%%smartcard:%127s", keyid
) == 1)
143 return SC_FORMAT_KEYID
;
145 return SC_FORMAT_INVALID
;
149 * Load a credential from a smartcard
151 static certificate_t
*load_from_smartcard(smartcard_format_t format
,
152 u_int slot
, char *module
, char *keyid
,
153 credential_type_t type
, int subtype
)
158 chunk
= chunk_from_hex(chunk_create(keyid
, strlen(keyid
)), NULL
);
161 case SC_FORMAT_SLOT_MODULE_KEYID
:
162 cred
= lib
->creds
->create(lib
->creds
, type
, subtype
,
163 BUILD_PKCS11_SLOT
, slot
,
164 BUILD_PKCS11_MODULE
, module
,
165 BUILD_PKCS11_KEYID
, chunk
, BUILD_END
);
167 case SC_FORMAT_SLOT_KEYID
:
168 cred
= lib
->creds
->create(lib
->creds
, type
, subtype
,
169 BUILD_PKCS11_SLOT
, slot
,
170 BUILD_PKCS11_KEYID
, chunk
, BUILD_END
);
172 case SC_FORMAT_KEYID
:
173 cred
= lib
->creds
->create(lib
->creds
, type
, subtype
,
174 BUILD_PKCS11_KEYID
, chunk
, BUILD_END
);
185 METHOD(stroke_cred_t
, load_peer
, certificate_t
*,
186 private_stroke_cred_t
*this, char *filename
)
188 certificate_t
*cert
= NULL
;
191 if (strpfx(filename
, "%smartcard"))
193 smartcard_format_t format
;
194 char module
[SC_PART_LEN
], keyid
[SC_PART_LEN
];
197 format
= parse_smartcard(filename
, &slot
, module
, keyid
);
198 if (format
!= SC_FORMAT_INVALID
)
200 cert
= (certificate_t
*)load_from_smartcard(format
,
201 slot
, module
, keyid
, CRED_CERTIFICATE
, CERT_X509
);
206 if (*filename
== '/')
208 snprintf(path
, sizeof(path
), "%s", filename
);
212 snprintf(path
, sizeof(path
), "%s/%s", CERTIFICATE_DIR
, filename
);
215 cert
= lib
->creds
->create(lib
->creds
,
216 CRED_CERTIFICATE
, CERT_ANY
,
217 BUILD_FROM_FILE
, path
,
222 cert
= this->creds
->add_cert_ref(this->creds
, TRUE
, cert
);
223 DBG1(DBG_CFG
, " loaded certificate \"%Y\" from '%s'",
224 cert
->get_subject(cert
), filename
);
227 DBG1(DBG_CFG
, " loading certificate from '%s' failed", filename
);
231 METHOD(stroke_cred_t
, load_pubkey
, certificate_t
*,
232 private_stroke_cred_t
*this, char *filename
, identification_t
*identity
)
237 builder_part_t build_part
;
238 key_type_t type
= KEY_ANY
;
240 if (streq(filename
, "%dns"))
244 if (strncaseeq(filename
, "dns:", 4))
245 { /* RFC 3110 format */
246 build_part
= BUILD_BLOB_DNSKEY
;
247 /* not a complete RR, only RSA supported */
251 else if (strncaseeq(filename
, "ssh:", 4))
253 build_part
= BUILD_BLOB_SSHKEY
;
257 { /* try PKCS#1 by default */
258 build_part
= BUILD_BLOB_ASN1_DER
;
260 if (strncaseeq(filename
, "0x", 2) || strncaseeq(filename
, "0s", 2))
262 chunk_t printable_key
, raw_key
;
264 printable_key
= chunk_create(filename
+ 2, strlen(filename
) - 2);
265 raw_key
= strncaseeq(filename
, "0x", 2) ?
266 chunk_from_hex(printable_key
, NULL
) :
267 chunk_from_base64(printable_key
, NULL
);
268 key
= lib
->creds
->create(lib
->creds
, CRED_PUBLIC_KEY
, type
,
269 build_part
, raw_key
, BUILD_END
);
270 chunk_free(&raw_key
);
273 cert
= lib
->creds
->create(lib
->creds
, CRED_CERTIFICATE
,
275 BUILD_PUBLIC_KEY
, key
,
276 BUILD_SUBJECT
, identity
,
278 type
= key
->get_type(key
);
282 cert
= this->creds
->add_cert_ref(this->creds
, TRUE
, cert
);
283 DBG1(DBG_CFG
, " loaded %N public key for \"%Y\"",
284 key_type_names
, type
, identity
);
288 DBG1(DBG_CFG
, " loading public key for \"%Y\" failed", identity
);
292 if (*filename
== '/')
294 snprintf(path
, sizeof(path
), "%s", filename
);
298 snprintf(path
, sizeof(path
), "%s/%s", CERTIFICATE_DIR
, filename
);
301 cert
= lib
->creds
->create(lib
->creds
,
302 CRED_CERTIFICATE
, CERT_TRUSTED_PUBKEY
,
303 BUILD_FROM_FILE
, path
,
304 BUILD_SUBJECT
, identity
,
308 cert
= this->creds
->add_cert_ref(this->creds
, TRUE
, cert
);
309 key
= cert
->get_public_key(cert
);
310 type
= key
->get_type(key
);
312 DBG1(DBG_CFG
, " loaded %N public key for \"%Y\" from '%s'",
313 key_type_names
, type
, identity
, filename
);
316 DBG1(DBG_CFG
, " loading public key for \"%Y\" from '%s' failed",
323 * Load a CA certificate, optionally force it to be one
325 static certificate_t
*load_ca_cert(char *filename
, bool force_ca_cert
)
327 certificate_t
*cert
= NULL
;
330 if (strpfx(filename
, "%smartcard"))
332 smartcard_format_t format
;
333 char module
[SC_PART_LEN
], keyid
[SC_PART_LEN
];
336 format
= parse_smartcard(filename
, &slot
, module
, keyid
);
337 if (format
!= SC_FORMAT_INVALID
)
339 cert
= (certificate_t
*)load_from_smartcard(format
,
340 slot
, module
, keyid
, CRED_CERTIFICATE
, CERT_X509
);
345 if (*filename
== '/')
347 snprintf(path
, sizeof(path
), "%s", filename
);
351 snprintf(path
, sizeof(path
), "%s/%s", CA_CERTIFICATE_DIR
, filename
);
355 { /* we treat this certificate as a CA certificate even if it has no
356 * CA basic constraint */
357 cert
= lib
->creds
->create(lib
->creds
,
358 CRED_CERTIFICATE
, CERT_X509
,
359 BUILD_FROM_FILE
, path
, BUILD_X509_FLAG
, X509_CA
,
364 cert
= lib
->creds
->create(lib
->creds
,
365 CRED_CERTIFICATE
, CERT_X509
,
366 BUILD_FROM_FILE
, path
,
372 x509_t
*x509
= (x509_t
*)cert
;
374 if (!(x509
->get_flags(x509
) & X509_CA
))
376 DBG1(DBG_CFG
, " ca certificate \"%Y\" lacks ca basic constraint, "
377 "discarded", cert
->get_subject(cert
));
381 DBG1(DBG_CFG
, " loaded ca certificate \"%Y\" from '%s'",
382 cert
->get_subject(cert
), filename
);
389 * Used by stroke_ca.c
391 certificate_t
*stroke_load_ca_cert(char *filename
)
395 force_ca_cert
= lib
->settings
->get_bool(lib
->settings
,
396 "%s.plugins.stroke.ignore_missing_ca_basic_constraint",
398 return load_ca_cert(filename
, force_ca_cert
);
402 * Load a CA certificate from disk
404 static void load_x509_ca(private_stroke_cred_t
*this, char *file
,
409 cert
= load_ca_cert(file
, this->force_ca_cert
);
412 cert
= this->ca
->get_cert_ref(this->ca
, cert
);
413 creds
->add_cert(creds
, TRUE
, cert
);
417 DBG1(DBG_CFG
, " loading ca certificate from '%s' failed", file
);
422 * Load AA certificate with flags from disk
424 static void load_x509_aa(private_stroke_cred_t
*this,char *file
,
429 cert
= lib
->creds
->create(lib
->creds
, CRED_CERTIFICATE
, CERT_X509
,
430 BUILD_FROM_FILE
, file
,
431 BUILD_X509_FLAG
, X509_AA
, BUILD_END
);
434 DBG1(DBG_CFG
, " loaded AA certificate \"%Y\" from '%s'",
435 cert
->get_subject(cert
), file
);
436 creds
->add_cert(creds
, TRUE
, cert
);
440 DBG1(DBG_CFG
, " loading AA certificate from '%s' failed", file
);
445 * Load a certificate with flags from disk
447 static void load_x509(private_stroke_cred_t
*this, char *file
, x509_flag_t flag
,
452 /* for all other flags, we add them to the certificate. */
453 cert
= lib
->creds
->create(lib
->creds
, CRED_CERTIFICATE
, CERT_X509
,
454 BUILD_FROM_FILE
, file
,
455 BUILD_X509_FLAG
, flag
, BUILD_END
);
458 DBG1(DBG_CFG
, " loaded certificate \"%Y\" from '%s'",
459 cert
->get_subject(cert
), file
);
460 creds
->add_cert(creds
, TRUE
, cert
);
464 DBG1(DBG_CFG
, " loading certificate from '%s' failed", file
);
469 * Load a CRL from a file
471 static void load_x509_crl(private_stroke_cred_t
*this, char *file
,
476 cert
= lib
->creds
->create(lib
->creds
, CRED_CERTIFICATE
, CERT_X509_CRL
,
477 BUILD_FROM_FILE
, file
, BUILD_END
);
480 DBG1(DBG_CFG
, " loaded crl from '%s'", file
);
481 creds
->add_crl(creds
, (crl_t
*)cert
);
485 DBG1(DBG_CFG
, " loading crl from '%s' failed", file
);
490 * Load an attribute certificate from a file
492 static void load_x509_ac(private_stroke_cred_t
*this, char *file
,
497 cert
= lib
->creds
->create(lib
->creds
, CRED_CERTIFICATE
, CERT_X509_AC
,
498 BUILD_FROM_FILE
, file
, BUILD_END
);
501 DBG1(DBG_CFG
, " loaded attribute certificate from '%s'", file
);
502 creds
->add_cert(creds
, FALSE
, cert
);
506 DBG1(DBG_CFG
, " loading attribute certificate from '%s' failed", file
);
511 * load trusted certificates from a directory
513 static void load_certdir(private_stroke_cred_t
*this, char *path
,
514 certificate_type_t type
, x509_flag_t flag
,
517 enumerator_t
*enumerator
;
521 enumerator
= enumerator_create_directory(path
);
524 while (enumerator
->enumerate(enumerator
, NULL
, &file
, &st
))
526 if (!S_ISREG(st
.st_mode
))
528 /* skip special file */
536 load_x509_ca(this, file
, creds
);
538 else if (flag
& X509_AA
)
540 load_x509_aa(this, file
, creds
);
544 load_x509(this, file
, flag
, creds
);
548 load_x509_crl(this, file
, creds
);
551 load_x509_ac(this, file
, creds
);
557 enumerator
->destroy(enumerator
);
561 DBG1(DBG_CFG
, " reading directory failed");
565 METHOD(credential_set_t
, cache_cert
, void,
566 private_stroke_cred_t
*this, certificate_t
*cert
)
568 if (cert
->get_type(cert
) == CERT_X509_CRL
&& this->cachecrl
)
570 /* CRLs get written to /etc/ipsec.d/crls/<authkeyId>.crl */
571 crl_t
*crl
= (crl_t
*)cert
;
574 if (this->creds
->add_crl(this->creds
, crl
))
580 is_delta_crl
= crl
->is_delta_crl(crl
, NULL
);
582 chunk
= crl
->get_authKeyIdentifier(crl
);
583 hex
= chunk_to_hex(chunk
, NULL
, FALSE
);
584 snprintf(buf
, sizeof(buf
), "%s/%s%s.crl", CRL_DIR
, hex
.ptr
,
585 is_delta_crl ?
"_delta" : "");
588 if (cert
->get_encoding(cert
, CERT_ASN1_DER
, &chunk
))
590 if (chunk_write(chunk
, buf
, 022, TRUE
))
592 DBG1(DBG_CFG
, " written crl file '%s' (%d bytes)",
597 DBG1(DBG_CFG
, " writing crl file '%s' failed: %s",
598 buf
, strerror(errno
));
606 METHOD(stroke_cred_t
, cachecrl
, void,
607 private_stroke_cred_t
*this, bool enabled
)
609 DBG1(DBG_CFG
, "crl caching to %s %s",
610 CRL_DIR
, enabled ?
"enabled" : "disabled");
611 this->cachecrl
= enabled
;
616 * Convert a string of characters into a binary secret
617 * A string between single or double quotes is treated as ASCII characters
618 * A string prepended by 0x is treated as HEX and prepended by 0s as Base64
620 static err_t
extract_secret(chunk_t
*secret
, chunk_t
*line
)
623 char delimiter
= ' ';
626 if (!eat_whitespace(line
))
628 return "missing secret";
631 if (*line
->ptr
== '\'' || *line
->ptr
== '"')
634 delimiter
= *line
->ptr
;
635 line
->ptr
++; line
->len
--;
638 if (!extract_token(&raw_secret
, delimiter
, line
))
640 if (delimiter
== ' ')
646 return "missing second delimiter";
652 /* treat as an ASCII string */
653 *secret
= chunk_clone(raw_secret
);
656 /* treat 0x as hex, 0s as base64 */
657 if (raw_secret
.len
> 2)
659 if (strncasecmp("0x", raw_secret
.ptr
, 2) == 0)
661 *secret
= chunk_from_hex(chunk_skip(raw_secret
, 2), NULL
);
664 if (strncasecmp("0s", raw_secret
.ptr
, 2) == 0)
666 *secret
= chunk_from_base64(chunk_skip(raw_secret
, 2), NULL
);
670 *secret
= chunk_clone(raw_secret
);
675 * Data for passphrase callback
678 /** cached passphrases */
680 /** socket we use for prompting */
682 /** type of secret to unlock */
684 /** private key file */
686 /** number of tries */
688 } passphrase_cb_data_t
;
691 * Callback function to receive passphrases
693 static shared_key_t
* passphrase_cb(passphrase_cb_data_t
*data
,
694 shared_key_type_t type
, identification_t
*me
,
695 identification_t
*other
, id_match_t
*match_me
,
696 id_match_t
*match_other
)
698 static const int max_tries
= 3;
699 shared_key_t
*shared
;
703 if (type
!= SHARED_ANY
&& type
!= SHARED_PRIVATE_KEY_PASS
)
709 if (data
->try > max_tries
+ 1)
710 { /* another builder might call this after we gave up, fail silently */
713 if (data
->try > max_tries
)
715 fprintf(data
->prompt
, "Passphrase invalid, giving up.\n");
720 fprintf(data
->prompt
, "Passphrase invalid!\n");
722 fprintf(data
->prompt
, "%s '%s' is encrypted.\n",
723 data
->type
== CRED_PRIVATE_KEY ?
"Private key" : "PKCS#12 file",
725 fprintf(data
->prompt
, "Passphrase:\n");
726 if (fgets(buf
, sizeof(buf
), data
->prompt
))
728 secret
= chunk_create(buf
, strlen(buf
));
730 { /* trim appended \n */
734 *match_me
= ID_MATCH_PERFECT
;
738 *match_other
= ID_MATCH_NONE
;
740 shared
= shared_key_create(SHARED_PRIVATE_KEY_PASS
,
741 chunk_clone(secret
));
742 data
->cache
->add_shared(data
->cache
, shared
->get_ref(shared
), NULL
);
750 * Data for PIN callback
753 /** socket we use for prompting */
759 /** number of tries */
762 shared_key_t
*shared
;
766 * Callback function to receive PINs
768 static shared_key_t
* pin_cb(pin_cb_data_t
*data
, shared_key_type_t type
,
769 identification_t
*me
, identification_t
*other
,
770 id_match_t
*match_me
, id_match_t
*match_other
)
775 if (type
!= SHARED_ANY
&& type
!= SHARED_PIN
)
780 if (!me
|| !chunk_equals(me
->get_encoding(me
), data
->keyid
))
788 fprintf(data
->prompt
, "PIN invalid, aborting.\n");
791 fprintf(data
->prompt
, "Login to '%s' required\n", data
->card
);
792 fprintf(data
->prompt
, "PIN:\n");
793 if (fgets(buf
, sizeof(buf
), data
->prompt
))
795 secret
= chunk_create(buf
, strlen(buf
));
797 { /* trim appended \n */
801 *match_me
= ID_MATCH_PERFECT
;
805 *match_other
= ID_MATCH_NONE
;
807 DESTROY_IF(data
->shared
);
808 data
->shared
= shared_key_create(SHARED_PIN
, chunk_clone(secret
));
809 return data
->shared
->get_ref(data
->shared
);
816 * Load a smartcard with a PIN
818 static bool load_pin(mem_cred_t
*secrets
, chunk_t line
, int line_nr
,
821 chunk_t sc
= chunk_empty
, secret
= chunk_empty
;
822 char smartcard
[BUF_LEN
], keyid
[SC_PART_LEN
], module
[SC_PART_LEN
];
823 private_key_t
*key
= NULL
;
826 shared_key_t
*shared
= NULL
;
827 identification_t
*id
;
828 mem_cred_t
*mem
= NULL
;
829 callback_cred_t
*cb
= NULL
;
830 pin_cb_data_t pin_data
;
831 smartcard_format_t format
;
833 err_t ugh
= extract_value(&sc
, &line
);
837 DBG1(DBG_CFG
, "line %d: %s", line_nr
, ugh
);
842 DBG1(DBG_CFG
, "line %d: expected %%smartcard specifier", line_nr
);
845 snprintf(smartcard
, sizeof(smartcard
), "%.*s", (int)sc
.len
, sc
.ptr
);
846 smartcard
[sizeof(smartcard
) - 1] = '\0';
848 format
= parse_smartcard(smartcard
, &slot
, module
, keyid
);
849 if (format
== SC_FORMAT_INVALID
)
851 DBG1(DBG_CFG
, "line %d: the given %%smartcard specifier is not"
852 " supported or invalid", line_nr
);
856 if (!eat_whitespace(&line
))
858 DBG1(DBG_CFG
, "line %d: expected PIN", line_nr
);
861 ugh
= extract_secret(&secret
, &line
);
864 DBG1(DBG_CFG
, "line %d: malformed PIN: %s", line_nr
, ugh
);
868 chunk
= chunk_from_hex(chunk_create(keyid
, strlen(keyid
)), NULL
);
869 if (secret
.len
== 7 && strpfx(secret
.ptr
, "%prompt"))
873 { /* no IO channel to prompt, skip */
877 /* use callback credential set to prompt for the pin */
878 pin_data
= (pin_cb_data_t
){
883 cb
= callback_cred_create_shared((void*)pin_cb
, &pin_data
);
884 lib
->credmgr
->add_local_set(lib
->credmgr
, &cb
->set
, FALSE
);
888 /* provide our pin in a temporary credential set */
889 shared
= shared_key_create(SHARED_PIN
, secret
);
890 id
= identification_create_from_encoding(ID_KEY_ID
, chunk
);
891 mem
= mem_cred_create();
892 mem
->add_shared(mem
, shared
->get_ref(shared
), id
, NULL
);
893 lib
->credmgr
->add_local_set(lib
->credmgr
, &mem
->set
, FALSE
);
896 /* unlock: smartcard needs the pin and potentially calls public set */
897 key
= (private_key_t
*)load_from_smartcard(format
, slot
, module
, keyid
,
898 CRED_PRIVATE_KEY
, KEY_ANY
);
902 DBG1(DBG_CFG
, " loaded private key from %.*s", (int)sc
.len
, sc
.ptr
);
903 secrets
->add_key(secrets
, key
);
909 shared
->destroy(shared
);
912 lib
->credmgr
->remove_local_set(lib
->credmgr
, &mem
->set
);
919 shared
= pin_data
.shared
;
923 DESTROY_IF(pin_data
.shared
);
925 lib
->credmgr
->remove_local_set(lib
->credmgr
, &cb
->set
);
930 id
= identification_create_from_encoding(ID_KEY_ID
, chunk
);
931 secrets
->add_shared(secrets
, shared
, id
, NULL
);
938 * Load a private key or PKCS#12 container from a file
940 static bool load_from_file(chunk_t line
, int line_nr
, FILE *prompt
,
941 char *path
, int type
, int subtype
,
945 chunk_t secret
= chunk_empty
;
947 err_t ugh
= extract_value(&filename
, &line
);
951 DBG1(DBG_CFG
, "line %d: %s", line_nr
, ugh
);
954 if (filename
.len
== 0)
956 DBG1(DBG_CFG
, "line %d: empty filename", line_nr
);
959 if (*filename
.ptr
== '/')
961 /* absolute path name */
962 snprintf(path
, PATH_MAX
, "%.*s", (int)filename
.len
, filename
.ptr
);
966 /* relative path name */
967 snprintf(path
, PATH_MAX
, "%s/%.*s", PRIVATE_KEY_DIR
,
968 (int)filename
.len
, filename
.ptr
);
971 /* check for optional passphrase */
972 if (eat_whitespace(&line
))
974 ugh
= extract_secret(&secret
, &line
);
977 DBG1(DBG_CFG
, "line %d: malformed passphrase: %s", line_nr
, ugh
);
981 if (secret
.len
== 7 && strpfx(secret
.ptr
, "%prompt"))
984 passphrase_cb_data_t pp_data
= {
997 /* add cache first so if valid passphrases are needed multiple times
998 * the callback is not called anymore */
999 pp_data
.cache
= mem_cred_create();
1000 lib
->credmgr
->add_local_set(lib
->credmgr
, &pp_data
.cache
->set
, FALSE
);
1001 /* use callback credential set to prompt for the passphrase */
1002 cb
= callback_cred_create_shared((void*)passphrase_cb
, &pp_data
);
1003 lib
->credmgr
->add_local_set(lib
->credmgr
, &cb
->set
, FALSE
);
1005 *result
= lib
->creds
->create(lib
->creds
, type
, subtype
,
1006 BUILD_FROM_FILE
, path
, BUILD_END
);
1008 lib
->credmgr
->remove_local_set(lib
->credmgr
, &cb
->set
);
1010 lib
->credmgr
->remove_local_set(lib
->credmgr
, &pp_data
.cache
->set
);
1011 pp_data
.cache
->destroy(pp_data
.cache
);
1015 mem_cred_t
*mem
= NULL
;
1016 shared_key_t
*shared
;
1018 /* provide our pin in a temporary credential set */
1019 shared
= shared_key_create(SHARED_PRIVATE_KEY_PASS
, secret
);
1020 mem
= mem_cred_create();
1021 mem
->add_shared(mem
, shared
, NULL
);
1022 if (eat_whitespace(&line
))
1023 { /* if there is a second passphrase add that too, could be needed for
1024 * PKCS#12 files using different passwords for MAC and encryption */
1025 ugh
= extract_secret(&secret
, &line
);
1028 DBG1(DBG_CFG
, "line %d: malformed passphrase: %s", line_nr
, ugh
);
1032 shared
= shared_key_create(SHARED_PRIVATE_KEY_PASS
, secret
);
1033 mem
->add_shared(mem
, shared
, NULL
);
1035 lib
->credmgr
->add_local_set(lib
->credmgr
, &mem
->set
, FALSE
);
1037 *result
= lib
->creds
->create(lib
->creds
, type
, subtype
,
1038 BUILD_FROM_FILE
, path
, BUILD_END
);
1040 lib
->credmgr
->remove_local_set(lib
->credmgr
, &mem
->set
);
1047 * Load a private key
1049 static bool load_private(mem_cred_t
*secrets
, chunk_t line
, int line_nr
,
1050 FILE *prompt
, key_type_t key_type
)
1052 char path
[PATH_MAX
];
1055 if (!load_from_file(line
, line_nr
, prompt
, path
, CRED_PRIVATE_KEY
,
1056 key_type
, (void**)&key
))
1062 DBG1(DBG_CFG
, " loaded %N private key from '%s'",
1063 key_type_names
, key
->get_type(key
), path
);
1064 secrets
->add_key(secrets
, key
);
1068 DBG1(DBG_CFG
, " loading private key from '%s' failed", path
);
1074 * Load a PKCS#12 container
1076 static bool load_pkcs12(private_stroke_cred_t
*this, mem_cred_t
*secrets
,
1077 chunk_t line
, int line_nr
, FILE *prompt
)
1079 enumerator_t
*enumerator
;
1080 char path
[PATH_MAX
];
1081 certificate_t
*cert
;
1085 if (!load_from_file(line
, line_nr
, prompt
, path
, CRED_CONTAINER
,
1086 CONTAINER_PKCS12
, (void**)&pkcs12
))
1092 DBG1(DBG_CFG
, " loading credentials from '%s' failed", path
);
1095 enumerator
= pkcs12
->create_cert_enumerator(pkcs12
);
1096 while (enumerator
->enumerate(enumerator
, &cert
))
1098 x509_t
*x509
= (x509_t
*)cert
;
1100 if (x509
->get_flags(x509
) & X509_CA
)
1102 DBG1(DBG_CFG
, " loaded ca certificate \"%Y\" from '%s'",
1103 cert
->get_subject(cert
), path
);
1107 DBG1(DBG_CFG
, " loaded certificate \"%Y\" from '%s'",
1108 cert
->get_subject(cert
), path
);
1110 this->creds
->add_cert(this->creds
, TRUE
, cert
->get_ref(cert
));
1112 enumerator
->destroy(enumerator
);
1113 enumerator
= pkcs12
->create_key_enumerator(pkcs12
);
1114 while (enumerator
->enumerate(enumerator
, &key
))
1116 DBG1(DBG_CFG
, " loaded %N private key from '%s'",
1117 key_type_names
, key
->get_type(key
), path
);
1118 secrets
->add_key(secrets
, key
->get_ref(key
));
1120 enumerator
->destroy(enumerator
);
1121 pkcs12
->container
.destroy(&pkcs12
->container
);
1128 static bool load_shared(mem_cred_t
*secrets
, chunk_t line
, int line_nr
,
1129 shared_key_type_t type
, chunk_t ids
)
1131 shared_key_t
*shared_key
;
1132 linked_list_t
*owners
;
1133 chunk_t secret
= chunk_empty
;
1136 err_t ugh
= extract_secret(&secret
, &line
);
1139 DBG1(DBG_CFG
, "line %d: malformed secret: %s", line_nr
, ugh
);
1142 shared_key
= shared_key_create(type
, secret
);
1143 DBG1(DBG_CFG
, " loaded %N secret for %s", shared_key_type_names
, type
,
1144 ids
.len
> 0 ?
(char*)ids
.ptr
: "%any");
1145 DBG4(DBG_CFG
, " secret: %#B", &secret
);
1147 owners
= linked_list_create();
1151 identification_t
*peer_id
;
1153 ugh
= extract_value(&id
, &ids
);
1156 DBG1(DBG_CFG
, "line %d: %s", line_nr
, ugh
);
1157 shared_key
->destroy(shared_key
);
1158 owners
->destroy_offset(owners
, offsetof(identification_t
, destroy
));
1166 /* NULL terminate the ID string */
1167 *(id
.ptr
+ id
.len
) = '\0';
1168 peer_id
= identification_create_from_string(id
.ptr
);
1169 if (peer_id
->get_type(peer_id
) == ID_ANY
)
1171 peer_id
->destroy(peer_id
);
1175 owners
->insert_last(owners
, peer_id
);
1180 owners
->insert_last(owners
,
1181 identification_create_from_encoding(ID_ANY
, chunk_empty
));
1183 secrets
->add_shared_list(secrets
, shared_key
, owners
);
1188 * reload ipsec.secrets
1190 static void load_secrets(private_stroke_cred_t
*this, mem_cred_t
*secrets
,
1191 char *file
, int level
, FILE *prompt
)
1196 DBG1(DBG_CFG
, "loading secrets from '%s'", file
);
1197 src
= chunk_map(file
, FALSE
);
1200 DBG1(DBG_CFG
, "opening secrets file '%s' failed: %s", file
,
1207 secrets
= mem_cred_create();
1210 while (fetchline(src
, &line
))
1213 key_type_t key_type
;
1214 shared_key_type_t type
;
1218 if (!eat_whitespace(&line
))
1222 if (line
.len
> strlen("include ") && strpfx(line
.ptr
, "include "))
1224 char **expanded
, *dir
, pattern
[PATH_MAX
];
1227 if (level
> MAX_SECRETS_RECURSION
)
1229 DBG1(DBG_CFG
, "maximum level of %d includes reached, ignored",
1230 MAX_SECRETS_RECURSION
);
1233 /* terminate filename by space */
1234 line
= chunk_skip(line
, strlen("include "));
1235 pos
= memchr(line
.ptr
, ' ', line
.len
);
1238 line
.len
= pos
- line
.ptr
;
1240 if (line
.len
&& line
.ptr
[0] == '/')
1242 if (line
.len
+ 1 > sizeof(pattern
))
1244 DBG1(DBG_CFG
, "include pattern too long, ignored");
1247 snprintf(pattern
, sizeof(pattern
), "%.*s",
1248 (int)line
.len
, line
.ptr
);
1251 { /* use directory of current file if relative */
1252 dir
= path_dirname(file
);
1254 if (line
.len
+ 1 + strlen(dir
) + 1 > sizeof(pattern
))
1256 DBG1(DBG_CFG
, "include pattern too long, ignored");
1260 snprintf(pattern
, sizeof(pattern
), "%s/%.*s",
1261 dir
, (int)line
.len
, line
.ptr
);
1267 if (glob(pattern
, GLOB_ERR
, NULL
, &buf
) != 0)
1269 DBG1(DBG_CFG
, "expanding file expression '%s' failed",
1274 for (expanded
= buf
.gl_pathv
; *expanded
!= NULL
; expanded
++)
1276 load_secrets(this, secrets
, *expanded
, level
+ 1,
1282 #else /* HAVE_GLOB_H */
1283 /* if glob(3) is not available, try to load pattern directly */
1284 load_secrets(this, secrets
, pattern
, level
+ 1, prompt
);
1285 #endif /* HAVE_GLOB_H */
1289 if (line
.len
> 2 && strpfx(line
.ptr
, ": "))
1291 /* no ids, skip the ':' */
1296 else if (extract_token_str(&ids
, " : ", &line
))
1298 /* NULL terminate the extracted id string */
1299 *(ids
.ptr
+ ids
.len
) = '\0';
1303 DBG1(DBG_CFG
, "line %d: missing ' : ' separator", line_nr
);
1307 if (!eat_whitespace(&line
) || !extract_token(&token
, ' ', &line
))
1309 DBG1(DBG_CFG
, "line %d: missing token", line_nr
);
1312 if (match("RSA", &token
) || match("ECDSA", &token
) ||
1313 match("BLISS", &token
) || match("PKCS8", &token
))
1315 if (match("RSA", &token
))
1319 else if (match("ECDSA", &token
))
1321 key_type
= KEY_ECDSA
;
1323 else if (match("BLISS", &token
))
1325 key_type
= KEY_BLISS
;
1331 if (!load_private(secrets
, line
, line_nr
, prompt
, key_type
))
1336 else if (match("P12", &token
))
1338 if (!load_pkcs12(this, secrets
, line
, line_nr
, prompt
))
1343 else if (match("PIN", &token
))
1345 if (!load_pin(secrets
, line
, line_nr
, prompt
))
1350 else if ((match("PSK", &token
) && (type
= SHARED_IKE
)) ||
1351 (match("EAP", &token
) && (type
= SHARED_EAP
)) ||
1352 (match("NTLM", &token
) && (type
= SHARED_NT_HASH
)) ||
1353 (match("XAUTH", &token
) && (type
= SHARED_EAP
)))
1355 if (!load_shared(secrets
, line
, line_nr
, type
, ids
))
1362 DBG1(DBG_CFG
, "line %d: token must be either RSA, ECDSA, BLISS, "
1363 "PKCS8 P12, PIN, PSK, EAP, XAUTH or NTLM", line_nr
);
1370 { /* replace secrets in active credential set */
1371 this->creds
->replace_secrets(this->creds
, secrets
, FALSE
);
1372 secrets
->destroy(secrets
);
1377 * load all certificates from ipsec.d
1379 static void load_certs(private_stroke_cred_t
*this)
1383 DBG1(DBG_CFG
, "loading ca certificates from '%s'",
1384 CA_CERTIFICATE_DIR
);
1385 creds
= mem_cred_create();
1386 load_certdir(this, CA_CERTIFICATE_DIR
, CERT_X509
, X509_CA
, creds
);
1387 this->ca
->replace_certs(this->ca
, creds
);
1388 creds
->destroy(creds
);
1390 DBG1(DBG_CFG
, "loading aa certificates from '%s'",
1391 AA_CERTIFICATE_DIR
);
1392 load_certdir(this, AA_CERTIFICATE_DIR
, CERT_X509
, X509_AA
, this->aacerts
);
1394 DBG1(DBG_CFG
, "loading ocsp signer certificates from '%s'",
1395 OCSP_CERTIFICATE_DIR
);
1396 load_certdir(this, OCSP_CERTIFICATE_DIR
, CERT_X509
, X509_OCSP_SIGNER
,
1399 DBG1(DBG_CFG
, "loading attribute certificates from '%s'",
1400 ATTR_CERTIFICATE_DIR
);
1401 load_certdir(this, ATTR_CERTIFICATE_DIR
, CERT_X509_AC
, 0, this->creds
);
1403 DBG1(DBG_CFG
, "loading crls from '%s'",
1405 load_certdir(this, CRL_DIR
, CERT_X509_CRL
, 0, this->creds
);
1408 METHOD(stroke_cred_t
, reread
, void,
1409 private_stroke_cred_t
*this, stroke_msg_t
*msg
, FILE *prompt
)
1413 if (msg
->reread
.flags
& REREAD_SECRETS
)
1415 DBG1(DBG_CFG
, "rereading secrets");
1416 load_secrets(this, NULL
, this->secrets_file
, 0, prompt
);
1418 if (msg
->reread
.flags
& REREAD_CACERTS
)
1420 /* first reload certificates in ca sections, so we can refer to them */
1421 this->ca
->reload_certs(this->ca
);
1423 DBG1(DBG_CFG
, "rereading ca certificates from '%s'",
1424 CA_CERTIFICATE_DIR
);
1425 creds
= mem_cred_create();
1426 load_certdir(this, CA_CERTIFICATE_DIR
, CERT_X509
, X509_CA
, creds
);
1427 this->ca
->replace_certs(this->ca
, creds
);
1428 creds
->destroy(creds
);
1430 if (msg
->reread
.flags
& REREAD_AACERTS
)
1432 DBG1(DBG_CFG
, "rereading aa certificates from '%s'",
1433 AA_CERTIFICATE_DIR
);
1434 creds
= mem_cred_create();
1435 load_certdir(this, AA_CERTIFICATE_DIR
, CERT_X509
, X509_AA
, creds
);
1436 this->aacerts
->replace_certs(this->aacerts
, creds
, FALSE
);
1437 creds
->destroy(creds
);
1438 lib
->credmgr
->flush_cache(lib
->credmgr
, CERT_X509
);
1440 if (msg
->reread
.flags
& REREAD_OCSPCERTS
)
1442 DBG1(DBG_CFG
, "rereading ocsp signer certificates from '%s'",
1443 OCSP_CERTIFICATE_DIR
);
1444 load_certdir(this, OCSP_CERTIFICATE_DIR
, CERT_X509
,
1445 X509_OCSP_SIGNER
, this->creds
);
1447 if (msg
->reread
.flags
& REREAD_ACERTS
)
1449 DBG1(DBG_CFG
, "rereading attribute certificates from '%s'",
1450 ATTR_CERTIFICATE_DIR
);
1451 load_certdir(this, ATTR_CERTIFICATE_DIR
, CERT_X509_AC
, 0, this->creds
);
1453 if (msg
->reread
.flags
& REREAD_CRLS
)
1455 DBG1(DBG_CFG
, "rereading crls from '%s'",
1457 load_certdir(this, CRL_DIR
, CERT_X509_CRL
, 0, this->creds
);
1461 METHOD(stroke_cred_t
, add_shared
, void,
1462 private_stroke_cred_t
*this, shared_key_t
*shared
, linked_list_t
*owners
)
1464 this->creds
->add_shared_list(this->creds
, shared
, owners
);
1467 METHOD(stroke_cred_t
, destroy
, void,
1468 private_stroke_cred_t
*this)
1470 lib
->credmgr
->remove_set(lib
->credmgr
, &this->aacerts
->set
);
1471 lib
->credmgr
->remove_set(lib
->credmgr
, &this->creds
->set
);
1472 this->aacerts
->destroy(this->aacerts
);
1473 this->creds
->destroy(this->creds
);
1480 stroke_cred_t
*stroke_cred_create(stroke_ca_t
*ca
)
1482 private_stroke_cred_t
*this;
1487 .create_private_enumerator
= (void*)return_null
,
1488 .create_cert_enumerator
= (void*)return_null
,
1489 .create_shared_enumerator
= (void*)return_null
,
1490 .create_cdp_enumerator
= (void*)return_null
,
1491 .cache_cert
= (void*)_cache_cert
,
1494 .load_peer
= _load_peer
,
1495 .load_pubkey
= _load_pubkey
,
1496 .add_shared
= _add_shared
,
1497 .cachecrl
= _cachecrl
,
1498 .destroy
= _destroy
,
1500 .secrets_file
= lib
->settings
->get_str(lib
->settings
,
1501 "%s.plugins.stroke.secrets_file", SECRETS_FILE
,
1503 .creds
= mem_cred_create(),
1504 .aacerts
= mem_cred_create(),
1508 if (lib
->settings
->get_bool(lib
->settings
, "%s.cache_crls", FALSE
, lib
->ns
))
1510 cachecrl(this, TRUE
);
1512 lib
->credmgr
->add_set(lib
->credmgr
, &this->creds
->set
);
1513 lib
->credmgr
->add_set(lib
->credmgr
, &this->aacerts
->set
);
1515 this->force_ca_cert
= lib
->settings
->get_bool(lib
->settings
,
1516 "%s.plugins.stroke.ignore_missing_ca_basic_constraint",
1520 load_secrets(this, NULL
, this->secrets_file
, 0, NULL
);
1522 return &this->public;