Make DH_free() free up any ex_data and also call the finish method.
[openssl.git] / crypto / dh / dh_lib.c
index 786a2c14b461bfb772f66c8a6468ddb16260431c..48b9db2ac06996e3d64b0db96ff80b3eef5d59e3 100644 (file)
@@ -1,5 +1,5 @@
 /* crypto/dh/dh_lib.c */
-/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
+/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  * All rights reserved.
  *
  * This package is an SSL implementation written
 
 #include <stdio.h>
 #include "cryptlib.h"
-#include "bn.h"
-#include "dh.h"
+#include <openssl/bn.h>
+#include <openssl/dh.h>
 
-char *DH_version="Diffie-Hellman part of SSLeay 0.8.1b 29-Jun-1998";
+const char *DH_version="Diffie-Hellman" OPENSSL_VERSION_PTEXT;
 
-DH *DH_new()
+static DH_METHOD *default_DH_method;
+static int dh_meth_num = 0;
+static STACK *dh_meth = NULL;
+
+void DH_set_default_method(DH_METHOD *meth)
+{
+       default_DH_method = meth;
+}
+
+DH_METHOD *DH_get_default_method(void)
+{
+       if(!default_DH_method) default_DH_method = DH_OpenSSL();
+       return default_DH_method;
+}
+
+DH_METHOD *DH_set_method(DH *dh, DH_METHOD *meth)
+{
+        DH_METHOD *mtmp;
+        mtmp = dh->meth;
+        if (mtmp->finish) mtmp->finish(dh);
+        dh->meth = meth;
+        if (meth->init) meth->init(dh);
+        return mtmp;
+}
+
+DH *DH_new(void)
+{
+       return DH_new_method(NULL);
+}
+
+DH *DH_new_method(DH_METHOD *meth)
        {
        DH *ret;
-
        ret=(DH *)Malloc(sizeof(DH));
+
        if (ret == NULL)
                {
                DHerr(DH_F_DH_NEW,ERR_R_MALLOC_FAILURE);
                return(NULL);
                }
+       if(!default_DH_method) default_DH_method = DH_OpenSSL();
+       if(meth) ret->meth = meth;
+       else ret->meth = default_DH_method;
        ret->pad=0;
        ret->version=0;
        ret->p=NULL;
@@ -80,12 +113,40 @@ DH *DH_new()
        ret->length=0;
        ret->pub_key=NULL;
        ret->priv_key=NULL;
+       ret->method_mont_p=NULL;
+       ret->references = 1;
+       ret->flags=ret->meth->flags;
+       if ((ret->meth->init != NULL) && !ret->meth->init(ret))
+               {
+               Free(ret);
+               ret=NULL;
+               }
+       else
+               CRYPTO_new_ex_data(dh_meth,(char *)ret,&ret->ex_data);
        return(ret);
        }
 
-void DH_free(r)
-DH *r;
+void DH_free(DH *r)
        {
+       int i;
+       if(r == NULL) return;
+       i = CRYPTO_add(&r->references, -1, CRYPTO_LOCK_DH);
+#ifdef REF_PRINT
+       REF_PRINT("DH",r);
+#endif
+       if (i > 0) return;
+#ifdef REF_CHECK
+       if (i < 0)
+               {
+               fprintf(stderr,"DH_free, bad reference count\n");
+               abort();
+       }
+#endif
+
+       CRYPTO_free_ex_data(dh_meth, (char *)r, &r->ex_data);
+
+       if(r->meth->finish) r->meth->finish(r);
+
        if (r->p != NULL) BN_clear_free(r->p);
        if (r->g != NULL) BN_clear_free(r->g);
        if (r->pub_key != NULL) BN_clear_free(r->pub_key);
@@ -93,8 +154,25 @@ DH *r;
        Free(r);
        }
 
-int DH_size(dh)
-DH *dh;
+int DH_get_ex_new_index(long argl, char *argp, int (*new_func)(),
+            int (*dup_func)(), void (*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, char *arg)
+       {
+       return(CRYPTO_set_ex_data(&d->ex_data,idx,arg));
+       }
+
+char *DH_get_ex_data(DH *d, int idx)
+       {
+       return(CRYPTO_get_ex_data(&d->ex_data,idx));
+       }
+
+int DH_size(DH *dh)
        {
        return(BN_num_bytes(dh->p));
        }