From: Martin Willi Date: Fri, 29 Oct 2010 09:55:19 +0000 (+0200) Subject: Load private keys from suite and test configs X-Git-Tag: 4.5.1~321 X-Git-Url: https://git.strongswan.org/?p=strongswan.git;a=commitdiff_plain;h=3f759bb75b52d115d908a7fcc7b04a017fe8f690 Load private keys from suite and test configs --- diff --git a/src/conftest/conftest.c b/src/conftest/conftest.c index 3405197..6efd063 100644 --- a/src/conftest/conftest.c +++ b/src/conftest/conftest.c @@ -149,6 +149,66 @@ static bool load_certs(settings_t *settings, char *dir) } /** + * Load private keys from the confiuguration file + */ +static bool load_keys(settings_t *settings, char *dir) +{ + enumerator_t *enumerator; + char *type, *value, wd[PATH_MAX]; + private_key_t *key; + key_type_t key_type; + + if (getcwd(wd, sizeof(wd)) == NULL) + { + fprintf(stderr, "getting cwd failed: %s\n", strerror(errno)); + return FALSE; + } + if (chdir(dir) != 0) + { + fprintf(stderr, "opening directory '%s' failed: %s\n", + dir, strerror(errno)); + return FALSE; + } + + enumerator = settings->create_key_value_enumerator(settings, "keys"); + while (enumerator->enumerate(enumerator, &type, &value)) + { + if (strcaseeq(type, "ecdsa")) + { + key_type = KEY_ECDSA; + } + else if (strcaseeq(type, "rsa")) + { + key_type = KEY_RSA; + } + else + { + fprintf(stderr, "unkown key type: '%s'\n", type); + enumerator->destroy(enumerator); + return FALSE; + } + key = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, key_type, + BUILD_FROM_FILE, value, BUILD_END); + if (!key) + { + fprintf(stderr, "loading %s key from '%s' failed\n", type, value); + enumerator->destroy(enumerator); + return FALSE; + } + conftest->creds->add_key(conftest->creds, key); + } + enumerator->destroy(enumerator); + + if (chdir(wd) != 0) + { + fprintf(stderr, "opening directory '%s' failed: %s\n", + wd, strerror(errno)); + return FALSE; + } + return TRUE; +} + +/** * Load configured hooks */ static bool load_hooks() @@ -306,6 +366,11 @@ int main(int argc, char *argv[]) { return 1; } + if (!load_keys(conftest->suite, suite_file) || + !load_keys(conftest->test, test_file)) + { + return 1; + } if (!load_hooks()) { return 1;