Browse code

Move code to free cd to a function CAPI_DATA_free()

- 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>

Selva Nair authored on 2018/01/27 01:05:32
Showing 1 changed files
... ...
@@ -108,6 +108,31 @@ typedef struct _CAPI_DATA {
108 108
     BOOL free_crypt_prov;
109 109
 } CAPI_DATA;
110 110
 
111
+static void
112
+CAPI_DATA_free(CAPI_DATA *cd)
113
+{
114
+    if (!cd)
115
+    {
116
+        return;
117
+    }
118
+    if (cd->free_crypt_prov && cd->crypt_prov)
119
+    {
120
+        if (cd->key_spec == CERT_NCRYPT_KEY_SPEC)
121
+        {
122
+            NCryptFreeObject(cd->crypt_prov);
123
+        }
124
+        else
125
+        {
126
+            CryptReleaseContext(cd->crypt_prov, 0);
127
+        }
128
+    }
129
+    if (cd->cert_context)
130
+    {
131
+        CertFreeCertificateContext(cd->cert_context);
132
+    }
133
+    free(cd);
134
+}
135
+
111 136
 static char *
112 137
 ms_error_text(DWORD ms_err)
113 138
 {
... ...
@@ -363,22 +388,7 @@ finish(RSA *rsa)
363 363
     {
364 364
         return 0;
365 365
     }
366
-    if (cd->crypt_prov && cd->free_crypt_prov)
367
-    {
368
-        if (cd->key_spec == CERT_NCRYPT_KEY_SPEC)
369
-        {
370
-            NCryptFreeObject(cd->crypt_prov);
371
-        }
372
-        else
373
-        {
374
-            CryptReleaseContext(cd->crypt_prov, 0);
375
-        }
376
-    }
377
-    if (cd->cert_context)
378
-    {
379
-        CertFreeCertificateContext(cd->cert_context);
380
-    }
381
-    free(cd);
366
+    CAPI_DATA_free(cd);
382 367
     RSA_meth_free((RSA_METHOD*) rsa_meth);
383 368
     return 1;
384 369
 }
... ...
@@ -614,25 +624,7 @@ err:
614 614
         {
615 615
             free(my_rsa_method);
616 616
         }
617
-        if (cd)
618
-        {
619
-            if (cd->free_crypt_prov && cd->crypt_prov)
620
-            {
621
-                if (cd->key_spec == CERT_NCRYPT_KEY_SPEC)
622
-                {
623
-                    NCryptFreeObject(cd->crypt_prov);
624
-                }
625
-                else
626
-                {
627
-                    CryptReleaseContext(cd->crypt_prov, 0);
628
-                }
629
-            }
630
-            if (cd->cert_context)
631
-            {
632
-                CertFreeCertificateContext(cd->cert_context);
633
-            }
634
-            free(cd);
635
-        }
617
+        CAPI_DATA_free(cd);
636 618
     }
637 619
     return 0;
638 620
 }