Automatically free up dynamically allocated public key methods when
[openssl.git] / crypto / engine / tb_pkmeth.c
index b99dea21903869b0e98cdfb629da57949a89603a..999fc0ac41ebc8409bfcf636d9fd5d080183788d 100644 (file)
@@ -118,7 +118,7 @@ ENGINE *ENGINE_get_pkey_meth_engine(int nid)
 /* Obtains a pkey_meth implementation from an ENGINE functional reference */
 const EVP_PKEY_METHOD *ENGINE_get_pkey_meth(ENGINE *e, int nid)
        {
-       const EVP_PKEY_METHOD *ret;
+       EVP_PKEY_METHOD *ret;
        ENGINE_PKEY_METHS_PTR fn = ENGINE_get_pkey_meths(e);
        if(!fn || !fn(e, &ret, NULL, nid))
                {
@@ -141,3 +141,26 @@ int ENGINE_set_pkey_meths(ENGINE *e, ENGINE_PKEY_METHS_PTR f)
        e->pkey_meths = f;
        return 1;
        }
+
+/* Internal function to free up EVP_PKEY_METHOD structures before an
+ * ENGINE is destroyed
+ */
+
+void engine_pkey_meths_free(ENGINE *e)
+       {
+       int i;
+       EVP_PKEY_METHOD *pkm;
+       if (e->pkey_meths)
+               {
+               const int *pknids;
+               int npknids;
+               npknids = e->pkey_meths(e, NULL, &pknids, 0);
+               for (i = 0; i < npknids; i++)
+                       {
+                       if (e->pkey_meths(e, &pkm, NULL, pknids[i]))
+                               {
+                               EVP_PKEY_meth_free(pkm);
+                               }
+                       }
+               }
+       }