Make {RSA,DSA,DH}_new_method obtain and release an ENGINE
authorDr. Stephen Henson <steve@openssl.org>
Sat, 9 Mar 2002 18:25:03 +0000 (18:25 +0000)
committerDr. Stephen Henson <steve@openssl.org>
Sat, 9 Mar 2002 18:25:03 +0000 (18:25 +0000)
functional reference in all cases.

crypto/dh/dh.h
crypto/dh/dh_err.c
crypto/dh/dh_lib.c
crypto/dsa/dsa.h
crypto/dsa/dsa_err.c
crypto/dsa/dsa_lib.c
crypto/rsa/rsa_lib.c

index 9bc1d319f9018f84c5db0c76c8779515541425fa..15cf70de0b6badf3bd3669bcbc0409de8de2142b 100644 (file)
@@ -195,7 +195,7 @@ void ERR_load_DH_strings(void);
 #define DH_F_DH_COMPUTE_KEY                             102
 #define DH_F_DH_GENERATE_KEY                            103
 #define DH_F_DH_GENERATE_PARAMETERS                     104
-#define DH_F_DH_NEW                                     105
+#define DH_F_DH_NEW_METHOD                              105
 
 /* Reason codes. */
 #define DH_R_NO_PRIVATE_VALUE                           100
index 86764a3e84fddaf5590468bce2bec19e482f352a..225779336cbae22ec7cc1401547d133e974ff22a 100644 (file)
@@ -71,7 +71,7 @@ static ERR_STRING_DATA DH_str_functs[]=
 {ERR_PACK(0,DH_F_DH_COMPUTE_KEY,0),    "DH_compute_key"},
 {ERR_PACK(0,DH_F_DH_GENERATE_KEY,0),   "DH_generate_key"},
 {ERR_PACK(0,DH_F_DH_GENERATE_PARAMETERS,0),    "DH_generate_parameters"},
-{ERR_PACK(0,DH_F_DH_NEW,0),    "DH_new"},
+{ERR_PACK(0,DH_F_DH_NEW_METHOD,0),     "DH_new_method"},
 {0,NULL}
        };
 
index 367c19dfe98f689b00695ffbd2c1045ba4c8c157..7adf48e6a2e810155f552d9be4d3f948d9a3381b 100644 (file)
@@ -107,20 +107,29 @@ 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)
+       if (engine)
+               {
+               if (!ENGINE_init(engine))
+                       {
+                       DSAerr(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;
@@ -145,6 +154,8 @@ 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))
                {
+               if (ret->engine)
+                       ENGINE_finish(ret->engine);
                CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data);
                OPENSSL_free(ret);
                ret=NULL;
index 1ddc37f9fb9737198b013e69933a44176c9afc95..9b3baadf2c49c95213dfe0cad1b4a25f9e5f0ccb 100644 (file)
@@ -230,7 +230,7 @@ void ERR_load_DSA_strings(void);
 #define DSA_F_DSAPARAMS_PRINT_FP                        101
 #define DSA_F_DSA_DO_SIGN                               112
 #define DSA_F_DSA_DO_VERIFY                             113
-#define DSA_F_DSA_NEW                                   103
+#define DSA_F_DSA_NEW_METHOD                            103
 #define DSA_F_DSA_PRINT                                         104
 #define DSA_F_DSA_PRINT_FP                              105
 #define DSA_F_DSA_SIGN                                  106
index 2956c36d63f803058f9288561140081cf9d859b4..79aa4ff526c4f6cab5133d6f53a07b2364077624 100644 (file)
@@ -71,7 +71,7 @@ static ERR_STRING_DATA DSA_str_functs[]=
 {ERR_PACK(0,DSA_F_DSAPARAMS_PRINT_FP,0),       "DSAparams_print_fp"},
 {ERR_PACK(0,DSA_F_DSA_DO_SIGN,0),      "DSA_do_sign"},
 {ERR_PACK(0,DSA_F_DSA_DO_VERIFY,0),    "DSA_do_verify"},
-{ERR_PACK(0,DSA_F_DSA_NEW,0),  "DSA_new"},
+{ERR_PACK(0,DSA_F_DSA_NEW_METHOD,0),   "DSA_new_method"},
 {ERR_PACK(0,DSA_F_DSA_PRINT,0),        "DSA_print"},
 {ERR_PACK(0,DSA_F_DSA_PRINT_FP,0),     "DSA_print_fp"},
 {ERR_PACK(0,DSA_F_DSA_SIGN,0), "DSA_sign"},
index 900e0098fa876024733112c465c5cc9433d65591..da2cdfa3d642fccb751dc6a923be83597b75f765 100644 (file)
@@ -110,19 +110,28 @@ DSA *DSA_new_method(ENGINE *engine)
        ret=(DSA *)OPENSSL_malloc(sizeof(DSA));
        if (ret == NULL)
                {
-               DSAerr(DSA_F_DSA_NEW,ERR_R_MALLOC_FAILURE);
+               DSAerr(DSA_F_DSA_NEW_METHOD,ERR_R_MALLOC_FAILURE);
                return(NULL);
                }
        ret->meth = DSA_get_default_method();
-       ret->engine = engine;
-       if(!ret->engine)
+       if (engine)
+               {
+               if (!ENGINE_init(engine))
+                       {
+                       DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_ENGINE_LIB);
+                       OPENSSL_free(ret);
+                       return NULL;
+                       }
+               ret->engine = engine;
+               }
+       else
                ret->engine = ENGINE_get_default_DSA();
        if(ret->engine)
                {
                ret->meth = ENGINE_get_DSA(ret->engine);
                if(!ret->meth)
                        {
-                       DSAerr(DSA_F_DSA_NEW,
+                       DSAerr(DSA_F_DSA_NEW_METHOD,
                                ERR_R_ENGINE_LIB);
                        ENGINE_finish(ret->engine);
                        OPENSSL_free(ret);
@@ -149,6 +158,8 @@ DSA *DSA_new_method(ENGINE *engine)
        CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data);
        if ((ret->meth->init != NULL) && !ret->meth->init(ret))
                {
+               if (ret->engine)
+                       ENGINE_finish(ret->engine);
                CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data);
                OPENSSL_free(ret);
                ret=NULL;
index 3856ea5da7f2994b22b17fb54ae7f8648e878b5d..93235744f7a9e365ec03a244e739ac02a6637b0d 100644 (file)
@@ -130,8 +130,17 @@ RSA *RSA_new_method(ENGINE *engine)
                }
 
        ret->meth = RSA_get_default_method();
-       ret->engine = engine;
-       if(!ret->engine)
+       if (engine)
+               {
+               if (!ENGINE_init(engine))
+                       {
+                       RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
+                       OPENSSL_free(ret);
+                       return NULL;
+                       }
+               ret->engine = engine;
+               }
+       else
                ret->engine = ENGINE_get_default_RSA();
        if(ret->engine)
                {
@@ -166,6 +175,8 @@ RSA *RSA_new_method(ENGINE *engine)
        CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data);
        if ((ret->meth->init != NULL) && !ret->meth->init(ret))
                {
+               if (ret->engine)
+                       ENGINE_finish(ret->engine);
                CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data);
                OPENSSL_free(ret);
                ret=NULL;