Give DH, DSA, and RSA functions to "up" their reference counts. Otherwise,
[openssl.git] / crypto / rsa / rsa_lib.c
index 6ebb0b552a54b629f3535712cbedf1576e1d8032..3accf013f3cbee47c73517acbe29621ec323bca2 100644 (file)
@@ -162,16 +162,23 @@ RSA *RSA_new_method(ENGINE *engine)
                return(NULL);
                }
 
-       if (engine == NULL)
+       if (engine)
                {
-               if((ret->engine=ENGINE_get_default_RSA()) == NULL)
-                       {
-                       OPENSSL_free(ret);
-                       return NULL;
-                       }
+               if(ENGINE_init(engine))
+                       ret->engine = engine;
+               else 
+                       ret->engine = NULL;
                }
        else
-               ret->engine=engine;
+               ret->engine=ENGINE_get_default_RSA();
+
+       if(ret->engine == NULL)
+               {
+               RSAerr(RSA_F_RSA_NEW_METHOD,ERR_LIB_ENGINE);
+               OPENSSL_free(ret);
+               return NULL;
+               }
+
        meth = ENGINE_get_RSA(ret->engine);
 
        ret->pad=0;
@@ -191,13 +198,13 @@ RSA *RSA_new_method(ENGINE *engine)
        ret->blinding=NULL;
        ret->bignum_data=NULL;
        ret->flags=meth->flags;
+       CRYPTO_new_ex_data(rsa_meth,ret,&ret->ex_data);
        if ((meth->init != NULL) && !meth->init(ret))
                {
+               CRYPTO_free_ex_data(rsa_meth, ret, &ret->ex_data);
                OPENSSL_free(ret);
                ret=NULL;
                }
-       else
-               CRYPTO_new_ex_data(rsa_meth,ret,&ret->ex_data);
        return(ret);
        }
 
@@ -221,13 +228,13 @@ void RSA_free(RSA *r)
                }
 #endif
 
-       CRYPTO_free_ex_data(rsa_meth,r,&r->ex_data);
-
        meth = ENGINE_get_RSA(r->engine);
        if (meth->finish != NULL)
                meth->finish(r);
        ENGINE_finish(r->engine);
 
+       CRYPTO_free_ex_data(rsa_meth,r,&r->ex_data);
+
        if (r->n != NULL) BN_clear_free(r->n);
        if (r->e != NULL) BN_clear_free(r->e);
        if (r->d != NULL) BN_clear_free(r->d);
@@ -241,12 +248,29 @@ void RSA_free(RSA *r)
        OPENSSL_free(r);
        }
 
+int RSA_up(RSA *r)
+       {
+       int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_RSA);
+#ifdef REF_PRINT
+       REF_PRINT("RSA",r);
+#endif
+#ifdef REF_CHECK
+       if (i < 2)
+               {
+               fprintf(stderr, "RSA_up, bad reference count\n");
+               abort();
+               }
+#endif
+       return ((i > 1) ? 1 : 0);
+       }
+
 int RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
             CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
         {
-       rsa_meth_num++;
-       return(CRYPTO_get_ex_new_index(rsa_meth_num-1,
-               &rsa_meth,argl,argp,new_func,dup_func,free_func));
+       if(CRYPTO_get_ex_new_index(rsa_meth_num, &rsa_meth, argl, argp,
+                               new_func, dup_func, free_func) < 0)
+               return -1;
+       return (rsa_meth_num++);
         }
 
 int RSA_set_ex_data(RSA *r, int idx, void *arg)
@@ -325,7 +349,7 @@ int RSA_blinding_on(RSA *rsa, BN_CTX *p_ctx)
 
        BN_CTX_start(ctx);
        A = BN_CTX_get(ctx);
-       if (!BN_rand(A,BN_num_bits(rsa->n)-1,1,0)) goto err;
+       if (!BN_rand_range(A,rsa->n)) goto err;
        if ((Ai=BN_mod_inverse(NULL,A,rsa->n,ctx)) == NULL) goto err;
 
        if (!ENGINE_get_RSA(rsa->engine)->bn_mod_exp(A,A,