]> git.ipfire.org Git - thirdparty/openvpn.git/commitdiff
Move code to free cd to a function CAPI_DATA_free()
authorSelva Nair <selva.nair@gmail.com>
Fri, 26 Jan 2018 16:05:32 +0000 (11:05 -0500)
committerGert Doering <gert@greenie.muc.de>
Tue, 20 Feb 2018 21:24:59 +0000 (22:24 +0100)
- Avoids code-repetition especially so when support
  for more key types are added.

Signed-off-by: Selva Nair <selva.nair@gmail.com>
Acked-by: Steffan Karger <steffan.karger@fox-it.com>
Acked-by: Gert Doering <gert@greenie.muc.de>
Message-Id: <1516982732-24145-1-git-send-email-selva.nair@gmail.com>
URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg16383.html
Signed-off-by: Gert Doering <gert@greenie.muc.de>
src/openvpn/cryptoapi.c

index f155123d0bdb87d74401f73439f853af902cbaeb..0349191e7ea89bc18f4149673fc2fab4a5082839 100644 (file)
@@ -108,6 +108,31 @@ typedef struct _CAPI_DATA {
     BOOL free_crypt_prov;
 } CAPI_DATA;
 
+static void
+CAPI_DATA_free(CAPI_DATA *cd)
+{
+    if (!cd)
+    {
+        return;
+    }
+    if (cd->free_crypt_prov && cd->crypt_prov)
+    {
+        if (cd->key_spec == CERT_NCRYPT_KEY_SPEC)
+        {
+            NCryptFreeObject(cd->crypt_prov);
+        }
+        else
+        {
+            CryptReleaseContext(cd->crypt_prov, 0);
+        }
+    }
+    if (cd->cert_context)
+    {
+        CertFreeCertificateContext(cd->cert_context);
+    }
+    free(cd);
+}
+
 static char *
 ms_error_text(DWORD ms_err)
 {
@@ -363,22 +388,7 @@ finish(RSA *rsa)
     {
         return 0;
     }
-    if (cd->crypt_prov && cd->free_crypt_prov)
-    {
-        if (cd->key_spec == CERT_NCRYPT_KEY_SPEC)
-        {
-            NCryptFreeObject(cd->crypt_prov);
-        }
-        else
-        {
-            CryptReleaseContext(cd->crypt_prov, 0);
-        }
-    }
-    if (cd->cert_context)
-    {
-        CertFreeCertificateContext(cd->cert_context);
-    }
-    free(cd);
+    CAPI_DATA_free(cd);
     RSA_meth_free((RSA_METHOD*) rsa_meth);
     return 1;
 }
@@ -614,25 +624,7 @@ err:
         {
             free(my_rsa_method);
         }
-        if (cd)
-        {
-            if (cd->free_crypt_prov && cd->crypt_prov)
-            {
-                if (cd->key_spec == CERT_NCRYPT_KEY_SPEC)
-                {
-                    NCryptFreeObject(cd->crypt_prov);
-                }
-                else
-                {
-                    CryptReleaseContext(cd->crypt_prov, 0);
-                }
-            }
-            if (cd->cert_context)
-            {
-                CertFreeCertificateContext(cd->cert_context);
-            }
-            free(cd);
-        }
+        CAPI_DATA_free(cd);
     }
     return 0;
 }