srandom(seed);
}
-static EVP_PKEY *load_key(const char *file)
+static int get_private_key(const char *path, RSA **rsa)
{
- BIO *key;
- EVP_PKEY *pkey = NULL;
+ EVP_PKEY *pkey;
+ BIO *bio = BIO_new(BIO_s_file());
- key = BIO_new(BIO_s_file());
- if (!key)
- return NULL;
- if (BIO_read_filename(key, file) > 0)
- pkey = PEM_read_bio_PrivateKey(key, NULL, NULL, NULL);
- BIO_free(key);
- return pkey;
-}
-
-static int get_private_key(const char *key_file, RSA **rsa)
-{
- EVP_PKEY *key = load_key(key_file);
-
- if (!key)
+ *rsa = NULL;
+ if (!bio)
return -E_PRIVATE_KEY;
- *rsa = EVP_PKEY_get1_RSA(key);
- EVP_PKEY_free(key);
- if (!*rsa)
- return -E_RSA;
- return RSA_size(*rsa);
+ if (BIO_read_filename(bio, path) <= 0)
+ goto bio_free;
+ pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL);
+ if (!pkey)
+ goto bio_free;
+ *rsa = EVP_PKEY_get1_RSA(pkey);
+ EVP_PKEY_free(pkey);
+bio_free:
+ BIO_free(bio);
+ return *rsa? RSA_size(*rsa) : -E_PRIVATE_KEY;
}
/*