/* Exported API */
int key_init(void);
key_t *key_get_by_opt(const char *opt);
+int key_new(key_t *key);
int key_create(key_t *key, int type);
int key_load(key_t *key, unsigned int *err_code);
int key_store(key_t *key);
/*
* Create a new key container
*/
-static int key_new(key_t *key)
+int key_new(key_t *key)
{
/* Create key pair container */
key->key = EVP_PKEY_new();
return 0;
}
- /* Create OpenSSL key container */
- if (!key_new(key)) {
- return 0;
- }
-
if (key_create_fn[type]) {
return key_create_fn[type](key);
}
FILE *fp = NULL;
EVP_PKEY *k = NULL;
- /* Create OpenSSL key container */
- if (!key_new(key)) {
- *err_code = KEY_ERR_MALLOC;
- return 0;
- }
-
if (key->fn) {
/* Load key from file */
fp = fopen(key->fn, "r");
/* Load private keys from files (or generate new ones) */
for (i = 0 ; i < num_keys ; i++) {
+ if (!key_new(&keys[i])) {
+ ERROR("Failed to allocate key container\n");
+ exit(1);
+ }
+
/* First try to load the key from disk */
if (key_load(&keys[i], &err_code)) {
/* Key loaded successfully */
}
/* Key not loaded. Check the error code */
- if (err_code == KEY_ERR_MALLOC) {
- /* Cannot allocate memory. Abort. */
- ERROR("Malloc error while loading '%s'\n", keys[i].fn);
- exit(1);
- } else if (err_code == KEY_ERR_LOAD) {
+ if (err_code == KEY_ERR_LOAD) {
/* File exists, but it does not contain a valid private
* key. Abort. */
ERROR("Error loading '%s'\n", keys[i].fn);