Initial support for DSA_METHOD...
[openssl.git] / crypto / dsa / dsa_lib.c
index ce8e204..ff8b254 100644 (file)
 
 const char *DSA_version="DSA" OPENSSL_VERSION_PTEXT;
 
+static DSA_METHOD *default_DSA_method;
+static int dsa_meth_num = 0;
+static STACK *dsa_meth = NULL;
+
+void DSA_set_default_method(DSA_METHOD *meth)
+{
+       default_DSA_method = meth;
+}
+
+DSA_METHOD *DSA_get_default_method(void)
+{
+       if(!default_DSA_method) default_DSA_method = DSA_OpenSSL();
+       return default_DSA_method;
+}
+
 DSA *DSA_new(void)
+{
+       return DSA_new_method(NULL);
+}
+
+DSA_METHOD *DSA_set_method(DSA *dsa, DSA_METHOD *meth)
+{
+        DSA_METHOD *mtmp;
+        mtmp = dsa->meth;
+        if (mtmp->finish) mtmp->finish(dsa);
+        dsa->meth = meth;
+        if (meth->init) meth->init(dsa);
+        return mtmp;
+}
+
+
+DSA *DSA_new_method(DSA_METHOD *meth)
        {
        DSA *ret;
 
        ret=(DSA *)Malloc(sizeof(DSA));
+       if(!default_DSA_method) default_DSA_method = DSA_OpenSSL();
+       if(meth) ret->meth = meth;
+       else ret->meth = default_DSA_method;
        if (ret == NULL)
                {
                DSAerr(DSA_F_DSA_NEW,ERR_R_MALLOC_FAILURE);
@@ -82,7 +116,6 @@ DSA *DSA_new(void)
        ret->p=NULL;
        ret->q=NULL;
        ret->g=NULL;
-       ret->flags=DSA_FLAG_CACHE_MONT_P;
 
        ret->pub_key=NULL;
        ret->priv_key=NULL;
@@ -92,6 +125,16 @@ DSA *DSA_new(void)
        ret->method_mont_p=NULL;
 
        ret->references=1;
+       /* ret->flags=DSA_FLAG_CACHE_MONT_P; */
+       ret->flags=ret->meth->flags;
+       if ((ret->meth->init != NULL) && !ret->meth->init(ret))
+               {
+               Free(ret);
+               ret=NULL;
+               }
+       else
+               CRYPTO_new_ex_data(dsa_meth,(char *)ret,&ret->ex_data);
+       
        return(ret);
        }
 
@@ -114,6 +157,10 @@ void DSA_free(DSA *r)
                }
 #endif
 
+       CRYPTO_free_ex_data(dsa_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->q != NULL) BN_clear_free(r->q);
        if (r->g != NULL) BN_clear_free(r->g);
@@ -145,6 +192,24 @@ int DSA_size(DSA *r)
        return(ret);
        }
 
+int DSA_get_ex_new_index(long argl, char *argp, int (*new_func)(),
+            int (*dup_func)(), void (*free_func)())
+        {
+       dsa_meth_num++;
+       return(CRYPTO_get_ex_new_index(dsa_meth_num-1,
+               &dsa_meth,argl,argp,new_func,dup_func,free_func));
+        }
+
+int DSA_set_ex_data(DSA *d, int idx, char *arg)
+       {
+       return(CRYPTO_set_ex_data(&d->ex_data,idx,arg));
+       }
+
+char *DSA_get_ex_data(DSA *d, int idx)
+       {
+       return(CRYPTO_get_ex_data(&d->ex_data,idx));
+       }
+
 #ifndef NO_DH
 DH *DSA_dup_DH(DSA *r)
        {