Thread-safety fixes
authorBodo Möller <bodo@openssl.org>
Fri, 16 Jun 2006 01:01:34 +0000 (01:01 +0000)
committerBodo Möller <bodo@openssl.org>
Fri, 16 Jun 2006 01:01:34 +0000 (01:01 +0000)
crypto/bn/bn_mont.c
crypto/rsa/rsa_eay.c

index 81cb80f32a482b313d3363cd073b10bf1b1c4578..ba43e888eb6eec7f6074164ba08e18ad21ce94e9 100644 (file)
@@ -403,18 +403,32 @@ BN_MONT_CTX *BN_MONT_CTX_copy(BN_MONT_CTX *to, BN_MONT_CTX *from)
 BN_MONT_CTX *BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, int lock,
                                        const BIGNUM *mod, BN_CTX *ctx)
        {
-       if (*pmont)
-               return *pmont;
-       CRYPTO_w_lock(lock);
+       int got_write_lock = 0;
+       BN_MONT_CTX *ret;
+
+       CRYPTO_r_lock(lock);
        if (!*pmont)
                {
-               BN_MONT_CTX *mtmp;
-               mtmp = BN_MONT_CTX_new();
-               if (mtmp && !BN_MONT_CTX_set(mtmp, mod, ctx))
-                       BN_MONT_CTX_free(mtmp);
-               else
-                       *pmont = mtmp;
+               CRYPTO_r_unlock(lock);
+               CRYPTO_w_lock(lock);
+               got_write_lock = 1;
+
+               if (!*pmont)
+                       {
+                       ret = BN_MONT_CTX_new();
+                       if (ret && !BN_MONT_CTX_set(ret, mod, ctx))
+                               BN_MONT_CTX_free(ret);
+                       else
+                               *pmont = ret;
+                       }
                }
-       CRYPTO_w_unlock(lock);
-       return *pmont;
+       
+       ret = *pmont;
+       
+       if (got_write_lock)
+               CRYPTO_w_unlock(lock);
+       else
+               CRYPTO_r_unlock(lock);
+               
+       return ret;
        }
index 2ab662132d7ea0ada01afab7e7001846d2cf4748..021b92f9d00ac48a89edd1ba16f7dc130942d0c5 100644 (file)
@@ -287,6 +287,13 @@ static BN_BLINDING *setup_blinding(RSA *rsa, BN_CTX *ctx)
                }
        if ((Ai=BN_mod_inverse(NULL,A,rsa->n,ctx)) == NULL) goto err;
 
+       if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
+               {
+               if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n,
+                                       CRYPTO_LOCK_RSA, rsa->n, ctx))
+                       goto err;
+               }
+
        if (!rsa->meth->bn_mod_exp(A,A,rsa->e,rsa->n,ctx,rsa->_method_mod_n))
                goto err;
        ret = BN_BLINDING_new(A,Ai,rsa->n);