From 113445861084746a80a6630955bed513f6fb8c57 Mon Sep 17 00:00:00 2001
From: Alexander Lamaison <awl03@doc.ic.ac.uk>
Date: Tue, 14 Jul 2009 13:59:21 +0100
Subject: [PATCH] Combined RSA and DSA key-reading functions.

The read_rsa_from_file() and read_dsa_from_file() functions were identical except for the final PEM_read_bio_*PrivateKey() calls.  These have been combined into read_private_key_from_file() which takes the PEM function to call as an function-pointer argument.
---
 src/openssl.c |   53 ++++++++++++++++++-----------------------------------
 1 files changed, 18 insertions(+), 35 deletions(-)

diff --git a/src/openssl.c b/src/openssl.c
index 5b3981b..26d81d8 100644
--- a/src/openssl.c
+++ b/src/openssl.c
@@ -244,14 +244,17 @@ read_file_into_string(char ** key, LIBSSH2_SESSION * session, FILE * fp)
     return 0;
 }
 
+typedef void * (*pem_read_bio_func)(BIO *, void **, pem_password_cb *, void *u);
+
 static int
-read_rsa_from_file(libssh2_rsa_ctx ** rsa, LIBSSH2_SESSION * session,
-                   FILE * fp, unsigned const char *passphrase)
+read_private_key_from_file(void ** key_ctx, LIBSSH2_SESSION * session,
+                           pem_read_bio_func read_private_key,
+                           FILE * fp, unsigned const char *passphrase)
 {
     char * key;
     BIO * bp;
 
-    *rsa = NULL;
+    *key_ctx = NULL;
 
     if(read_file_into_string(&key, session, fp)) {
         return -1;
@@ -263,12 +266,12 @@ read_rsa_from_file(libssh2_rsa_ctx ** rsa, LIBSSH2_SESSION * session,
         return -1;
     }
 
-    *rsa = PEM_read_bio_RSAPrivateKey(bp, NULL, (void *) passphrase_cb,
-                                      (void *) passphrase);
+    *key_ctx = read_private_key(bp, NULL, (void *) passphrase_cb,
+                                (void *) passphrase);
 
     BIO_free(bp);
     LIBSSH2_FREE(session, key);
-    return (*rsa) ? 0 : -1;
+    return (*key_ctx) ? 0 : -1;
 }
 
 int
@@ -276,6 +279,9 @@ _libssh2_rsa_new_private(libssh2_rsa_ctx ** rsa,
                          LIBSSH2_SESSION * session,
                          FILE * fp, unsigned const char *passphrase)
 {
+    pem_read_bio_func read_rsa =
+        (pem_read_bio_func) &PEM_read_bio_RSAPrivateKey;
+
     if (!EVP_get_cipherbyname("des")) {
 /* If this cipher isn't loaded it's a pretty good indication that none are.
  * I have *NO DOUBT* that there's a better way to deal with this ($#&%#$(%$#(
@@ -284,34 +290,7 @@ _libssh2_rsa_new_private(libssh2_rsa_ctx ** rsa,
         OpenSSL_add_all_ciphers();
     }
 
-    return read_rsa_from_file(rsa, session, fp, passphrase);
-}
-
-static int
-read_dsa_from_file(libssh2_dsa_ctx ** dsa, LIBSSH2_SESSION * session,
-                   FILE * fp, unsigned const char *passphrase)
-{
-    char * key;
-    BIO * bp;
-
-    *dsa = NULL;
-
-    if(read_file_into_string(&key, session, fp)) {
-        return -1;
-    }
-
-    bp = BIO_new_mem_buf(key, -1);
-    if (!bp) {
-        LIBSSH2_FREE(session, key);
-        return -1;
-    }
-
-    *dsa = PEM_read_bio_DSAPrivateKey(bp, NULL, (void *) passphrase_cb,
-                                      (void *) passphrase);
-
-    BIO_free(bp);
-    LIBSSH2_FREE(session, key);
-    return (*dsa) ? 0 : -1;
+    return read_private_key_from_file(rsa, session, read_rsa, fp, passphrase);
 }
 
 int
@@ -319,6 +298,9 @@ _libssh2_dsa_new_private(libssh2_dsa_ctx ** dsa,
                          LIBSSH2_SESSION * session,
                          FILE * fp, unsigned const char *passphrase)
 {
+    pem_read_bio_func read_dsa =
+        (pem_read_bio_func) &PEM_read_bio_DSAPrivateKey;
+
     if (!EVP_get_cipherbyname("des")) {
 /* If this cipher isn't loaded it's a pretty good indication that none are.
  * I have *NO DOUBT* that there's a better way to deal with this ($#&%#$(%$#(
@@ -326,7 +308,8 @@ _libssh2_dsa_new_private(libssh2_dsa_ctx ** dsa,
  */
         OpenSSL_add_all_ciphers();
     }
-    return read_dsa_from_file(dsa, session, fp, passphrase);
+
+    return read_private_key_from_file(dsa, session, read_dsa, fp, passphrase);
 }
 
 int
-- 
1.6.3.2.1299.gee46c

