From: Andre Noll Date: Fri, 6 Jan 2017 17:52:13 +0000 (+0100) Subject: crypt.c: Combine load_key() and get_private_key(). X-Git-Tag: v0.6.0~14^2~1 X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=commitdiff_plain;h=52fb766fd183e555d8c85185e35a4dc365097756 crypt.c: Combine load_key() and get_private_key(). Both functions are short and the former is only called by the latter. --- 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; } /*