AlgorithmIdentifier bugs
[oweals/openssl.git] / crypto / dh / dh_lib.c
index 73f1c249f33b6aa2bff9e68ac0d3562863f561b9..a46270765e5a9b6185db19ee46bbb446a040e5c2 100644 (file)
 #include "cryptlib.h"
 #include <openssl/bn.h>
 #include <openssl/dh.h>
-#include <openssl/engine.h>
 
 const char *DH_version="Diffie-Hellman" OPENSSL_VERSION_PTEXT;
 
-static const DH_METHOD *default_DH_method = NULL;
+static DH_METHOD *default_DH_method = NULL;
+static int dh_meth_num = 0;
+static STACK_OF(CRYPTO_EX_DATA_FUNCS) *dh_meth = NULL;
 
-void DH_set_default_method(const DH_METHOD *meth)
-       {
+void DH_set_default_method(DH_METHOD *meth)
+{
        default_DH_method = meth;
-       }
+}
 
-const DH_METHOD *DH_get_default_method(void)
-       {
-       if(!default_DH_method)
-               default_DH_method = DH_OpenSSL();
+DH_METHOD *DH_get_default_method(void)
+{
+       if(!default_DH_method) default_DH_method = DH_OpenSSL();
        return default_DH_method;
-       }
+}
 
-int DH_set_method(DH *dh, const DH_METHOD *meth)
-       {
-       /* NB: The caller is specifically setting a method, so it's not up to us
-        * to deal with which ENGINE it comes from. */
-        const DH_METHOD *mtmp;
+DH_METHOD *DH_set_method(DH *dh, DH_METHOD *meth)
+{
+        DH_METHOD *mtmp;
         mtmp = dh->meth;
         if (mtmp->finish) mtmp->finish(dh);
-       if (dh->engine)
-               {
-               ENGINE_finish(dh->engine);
-               dh->engine = NULL;
-               }
         dh->meth = meth;
         if (meth->init) meth->init(dh);
-        return 1;
+        return mtmp;
 }
 
 DH *DH_new(void)
-       {
+{
        return DH_new_method(NULL);
-       }
+}
 
-DH *DH_new_method(ENGINE *engine)
+DH *DH_new_method(DH_METHOD *meth)
        {
        DH *ret;
-
        ret=(DH *)OPENSSL_malloc(sizeof(DH));
+
        if (ret == NULL)
                {
                DHerr(DH_F_DH_NEW,ERR_R_MALLOC_FAILURE);
                return(NULL);
                }
-
-       ret->meth = DH_get_default_method();
-       ret->engine = engine;
-       if(!ret->engine)
-               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);
-                       ENGINE_finish(ret->engine);
-                       OPENSSL_free(ret);
-                       return NULL;
-                       }
-               }
-
+       if(meth) ret->meth = meth;
+       else ret->meth = DH_get_default_method();
        ret->pad=0;
        ret->version=0;
        ret->p=NULL;
@@ -142,10 +120,10 @@ DH *DH_new_method(ENGINE *engine)
        ret->method_mont_p=NULL;
        ret->references = 1;
        ret->flags=ret->meth->flags;
-       CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data);
+       CRYPTO_new_ex_data(dh_meth,ret,&ret->ex_data);
        if ((ret->meth->init != NULL) && !ret->meth->init(ret))
                {
-               CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data);
+               CRYPTO_free_ex_data(dh_meth,ret,&ret->ex_data);
                OPENSSL_free(ret);
                ret=NULL;
                }
@@ -169,12 +147,9 @@ void DH_free(DH *r)
        }
 #endif
 
-       if (r->meth->finish)
-               r->meth->finish(r);
-       if (r->engine)
-               ENGINE_finish(r->engine);
+       if(r->meth->finish) r->meth->finish(r);
 
-       CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data);
+       CRYPTO_free_ex_data(dh_meth, r, &r->ex_data);
 
        if (r->p != NULL) BN_clear_free(r->p);
        if (r->g != NULL) BN_clear_free(r->g);
@@ -187,27 +162,12 @@ void DH_free(DH *r)
        OPENSSL_free(r);
        }
 
-int DH_up_ref(DH *r)
-       {
-       int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_DH);
-#ifdef REF_PRINT
-       REF_PRINT("DH",r);
-#endif
-#ifdef REF_CHECK
-       if (i < 2)
-               {
-               fprintf(stderr, "DH_up, bad reference count\n");
-               abort();
-               }
-#endif
-       return ((i > 1) ? 1 : 0);
-       }
-
 int DH_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
             CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
         {
-       return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DH, argl, argp,
-                               new_func, dup_func, free_func);
+       dh_meth_num++;
+       return(CRYPTO_get_ex_new_index(dh_meth_num-1,
+               &dh_meth,argl,argp,new_func,dup_func,free_func));
         }
 
 int DH_set_ex_data(DH *d, int idx, void *arg)
@@ -220,7 +180,7 @@ void *DH_get_ex_data(DH *d, int idx)
        return(CRYPTO_get_ex_data(&d->ex_data,idx));
        }
 
-int DH_size(const DH *dh)
+int DH_size(DH *dh)
        {
        return(BN_num_bytes(dh->p));
        }