From 094c6aa51d2354b24bb525ab8285e6b220067bce Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bodo=20M=C3=B6ller?= Date: Fri, 16 Jun 2006 01:01:34 +0000 Subject: [PATCH] Thread-safety fixes --- crypto/bn/bn_mont.c | 36 +++++++++++++++++++++++++----------- crypto/rsa/rsa_eay.c | 7 +++++++ 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/crypto/bn/bn_mont.c b/crypto/bn/bn_mont.c index 81cb80f32a..ba43e888eb 100644 --- a/crypto/bn/bn_mont.c +++ b/crypto/bn/bn_mont.c @@ -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; } diff --git a/crypto/rsa/rsa_eay.c b/crypto/rsa/rsa_eay.c index 2ab662132d..021b92f9d0 100644 --- a/crypto/rsa/rsa_eay.c +++ b/crypto/rsa/rsa_eay.c @@ -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); -- 2.25.1