From 52fb766fd183e555d8c85185e35a4dc365097756 Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Fri, 6 Jan 2017 18:52:13 +0100 Subject: [PATCH 1/1] crypt.c: Combine load_key() and get_private_key(). Both functions are short and the former is only called by the latter. --- crypt.c | 37 +++++++++++++++---------------------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/crypt.c b/crypt.c index 34b78798..29a1c955 100644 --- a/crypt.c +++ b/crypt.c @@ -61,31 +61,24 @@ void init_random_seed_or_die(void) 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; } /* -- 2.39.2