Add the possibility to build without the ENGINE framework.
[openssl.git] / crypto / dh / dh_lib.c
index 73f1c249f33b6aa2bff9e68ac0d3562863f561b9..09965ee2ea8001e59679d6b8dd1fb0f3a5dfce2a 100644 (file)
@@ -60,7 +60,9 @@
 #include "cryptlib.h"
 #include <openssl/bn.h>
 #include <openssl/dh.h>
+#ifndef OPENSSL_NO_ENGINE
 #include <openssl/engine.h>
+#endif
 
 const char *DH_version="Diffie-Hellman" OPENSSL_VERSION_PTEXT;
 
@@ -85,15 +87,17 @@ int DH_set_method(DH *dh, const DH_METHOD *meth)
         const DH_METHOD *mtmp;
         mtmp = dh->meth;
         if (mtmp->finish) mtmp->finish(dh);
+#ifndef OPENSSL_NO_ENGINE
        if (dh->engine)
                {
                ENGINE_finish(dh->engine);
                dh->engine = NULL;
                }
+#endif
         dh->meth = meth;
         if (meth->init) meth->init(dh);
         return 1;
-}
+       }
 
 DH *DH_new(void)
        {
@@ -107,25 +111,36 @@ DH *DH_new_method(ENGINE *engine)
        ret=(DH *)OPENSSL_malloc(sizeof(DH));
        if (ret == NULL)
                {
-               DHerr(DH_F_DH_NEW,ERR_R_MALLOC_FAILURE);
+               DHerr(DH_F_DH_NEW_METHOD,ERR_R_MALLOC_FAILURE);
                return(NULL);
                }
 
        ret->meth = DH_get_default_method();
-       ret->engine = engine;
-       if(!ret->engine)
+#ifndef OPENSSL_NO_ENGINE
+       if (engine)
+               {
+               if (!ENGINE_init(engine))
+                       {
+                       DHerr(DH_F_DH_NEW_METHOD, ERR_R_ENGINE_LIB);
+                       OPENSSL_free(ret);
+                       return NULL;
+                       }
+               ret->engine = engine;
+               }
+       else
                ret->engine = ENGINE_get_default_DH();
        if(ret->engine)
                {
                ret->meth = ENGINE_get_DH(ret->engine);
                if(!ret->meth)
                        {
-                       DHerr(DH_F_DH_NEW,ERR_R_ENGINE_LIB);
+                       DHerr(DH_F_DH_NEW_METHOD,ERR_R_ENGINE_LIB);
                        ENGINE_finish(ret->engine);
                        OPENSSL_free(ret);
                        return NULL;
                        }
                }
+#endif
 
        ret->pad=0;
        ret->version=0;
@@ -145,6 +160,10 @@ DH *DH_new_method(ENGINE *engine)
        CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data);
        if ((ret->meth->init != NULL) && !ret->meth->init(ret))
                {
+#ifndef OPENSSL_NO_ENGINE
+               if (ret->engine)
+                       ENGINE_finish(ret->engine);
+#endif
                CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data);
                OPENSSL_free(ret);
                ret=NULL;
@@ -171,8 +190,10 @@ void DH_free(DH *r)
 
        if (r->meth->finish)
                r->meth->finish(r);
+#ifndef OPENSSL_NO_ENGINE
        if (r->engine)
                ENGINE_finish(r->engine);
+#endif
 
        CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data);