In RSA, DSA, DH, and RAND - if the "***_new()" function fails because the
[openssl.git] / crypto / rsa / rsa_lib.c
index 4cde842c3516e6d6ba5fa70beed8cd526730de5a..3a4760c8d4e31ae4a54d441e8f246d3552184ba2 100644 (file)
@@ -101,7 +101,7 @@ const RSA_METHOD *RSA_get_default_openssl_method(void)
 #ifdef RSA_NULL
                default_RSA_meth=RSA_null_method();
 #else
-#if 0 /* was: #ifdef RSAref
+#if 0 /* was: #ifdef RSAref */
                default_RSA_meth=RSA_PKCS1_RSAref();
 #else
                default_RSA_meth=RSA_PKCS1_SSLeay();
@@ -166,6 +166,7 @@ RSA *RSA_new_method(ENGINE *engine)
                {
                if((ret->engine=ENGINE_get_default_RSA()) == NULL)
                        {
+                       RSAerr(RSA_F_RSA_NEW_METHOD,ERR_LIB_ENGINE);
                        OPENSSL_free(ret);
                        return NULL;
                        }
@@ -191,13 +192,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 +222,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);
@@ -325,7 +326,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,