mturk / rpms / openssl

Forked from rpms/openssl 3 years ago
Clone
4d4d77e
--- openssl-0.9.8b/crypto/rsa/rsa_eay.c.bn-threadsafe	2005-09-23 01:32:49.000000000 +0200
4d4d77e
+++ openssl-0.9.8b/crypto/rsa/rsa_eay.c	2006-07-20 13:41:44.000000000 +0200
4d4d77e
@@ -56,7 +56,7 @@
4d4d77e
  * [including the GNU Public Licence.]
4d4d77e
  */
4d4d77e
 /* ====================================================================
4d4d77e
- * Copyright (c) 1998-2005 The OpenSSL Project.  All rights reserved.
4d4d77e
+ * Copyright (c) 1998-2006 The OpenSSL Project.  All rights reserved.
4d4d77e
  *
4d4d77e
  * Redistribution and use in source and binary forms, with or without
4d4d77e
  * modification, are permitted provided that the following conditions
4d4d77e
@@ -238,40 +238,63 @@
4d4d77e
 	return(r);
4d4d77e
 	}
4d4d77e
 
4d4d77e
-static BN_BLINDING *rsa_get_blinding(RSA *rsa, BIGNUM **r, int *local, BN_CTX *ctx)
4d4d77e
+static BN_BLINDING *rsa_get_blinding(RSA *rsa, int *local, BN_CTX *ctx)
4d4d77e
 {
4d4d77e
 	BN_BLINDING *ret;
4d4d77e
+	int got_write_lock = 0;
4d4d77e
+
4d4d77e
+	CRYPTO_r_lock(CRYPTO_LOCK_RSA);
4d4d77e
 
4d4d77e
 	if (rsa->blinding == NULL)
4d4d77e
 		{
4d4d77e
+		CRYPTO_r_unlock(CRYPTO_LOCK_RSA);
4d4d77e
+		CRYPTO_w_lock(CRYPTO_LOCK_RSA);
4d4d77e
+		got_write_lock = 1;
4d4d77e
+
4d4d77e
 		if (rsa->blinding == NULL)
4d4d77e
-			{
4d4d77e
-			CRYPTO_w_lock(CRYPTO_LOCK_RSA);
4d4d77e
-			if (rsa->blinding == NULL)
4d4d77e
-				rsa->blinding = RSA_setup_blinding(rsa, ctx);
4d4d77e
-			CRYPTO_w_unlock(CRYPTO_LOCK_RSA);
4d4d77e
-			}
4d4d77e
+			rsa->blinding = RSA_setup_blinding(rsa, ctx);
4d4d77e
 		}
4d4d77e
 
4d4d77e
 	ret = rsa->blinding;
4d4d77e
 	if (ret == NULL)
4d4d77e
-		return NULL;
4d4d77e
+		goto err;
4d4d77e
 
4d4d77e
-	if (BN_BLINDING_get_thread_id(ret) != CRYPTO_thread_id())
4d4d77e
+	if (BN_BLINDING_get_thread_id(ret) == CRYPTO_thread_id())
4d4d77e
 		{
4d4d77e
-		*local = 0;
4d4d77e
+		/* rsa->blinding is ours! */
4d4d77e
+
4d4d77e
+		*local = 1;
4d4d77e
+		}
4d4d77e
+	else
4d4d77e
+		{
4d4d77e
+		/* resort to rsa->mt_blinding instead */
4d4d77e
+
4d4d77e
+		*local = 0; /* instructs rsa_blinding_convert(), rsa_blinding_invert()
4d4d77e
+		             * that the BN_BLINDING is shared, meaning that accesses
4d4d77e
+		             * require locks, and that the blinding factor must be
4d4d77e
+		             * stored outside the BN_BLINDING
4d4d77e
+		             */
4d4d77e
+
4d4d77e
 		if (rsa->mt_blinding == NULL)
4d4d77e
 			{
4d4d77e
-			CRYPTO_w_lock(CRYPTO_LOCK_RSA);
4d4d77e
+			if (!got_write_lock)
4d4d77e
+				{
4d4d77e
+				CRYPTO_r_unlock(CRYPTO_LOCK_RSA);
4d4d77e
+				CRYPTO_w_lock(CRYPTO_LOCK_RSA);
4d4d77e
+				got_write_lock = 1;
4d4d77e
+				}
4d4d77e
+			
4d4d77e
 			if (rsa->mt_blinding == NULL)
4d4d77e
 				rsa->mt_blinding = RSA_setup_blinding(rsa, ctx);
4d4d77e
-			CRYPTO_w_unlock(CRYPTO_LOCK_RSA);
4d4d77e
 			}
4d4d77e
 		ret = rsa->mt_blinding;
4d4d77e
 		}
4d4d77e
-	else
4d4d77e
-		*local = 1;
4d4d77e
 
4d4d77e
+ err:
4d4d77e
+	if (got_write_lock)
4d4d77e
+		CRYPTO_w_unlock(CRYPTO_LOCK_RSA);
4d4d77e
+	else
4d4d77e
+		CRYPTO_r_unlock(CRYPTO_LOCK_RSA);
4d4d77e
 	return ret;
4d4d77e
 }
4d4d77e
 
4d4d77e
@@ -358,7 +381,7 @@
4d4d77e
 
4d4d77e
 	if (!(rsa->flags & RSA_FLAG_NO_BLINDING))
4d4d77e
 		{
4d4d77e
-		blinding = rsa_get_blinding(rsa, &br, &local_blinding, ctx);
4d4d77e
+		blinding = rsa_get_blinding(rsa, &local_blinding, ctx);
4d4d77e
 		if (blinding == NULL)
4d4d77e
 			{
4d4d77e
 			RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, ERR_R_INTERNAL_ERROR);
4d4d77e
@@ -479,7 +502,7 @@
4d4d77e
 
4d4d77e
 	if (!(rsa->flags & RSA_FLAG_NO_BLINDING))
4d4d77e
 		{
4d4d77e
-		blinding = rsa_get_blinding(rsa, &br, &local_blinding, ctx);
4d4d77e
+		blinding = rsa_get_blinding(rsa, &local_blinding, ctx);
4d4d77e
 		if (blinding == NULL)
4d4d77e
 			{
4d4d77e
 			RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT, ERR_R_INTERNAL_ERROR);
4d4d77e
--- openssl-0.9.8b/crypto/bn/bn_mont.c.bn-threadsafe	2005-11-11 13:59:39.000000000 +0100
4d4d77e
+++ openssl-0.9.8b/crypto/bn/bn_mont.c	2006-07-20 13:42:07.000000000 +0200
4d4d77e
@@ -55,6 +55,59 @@
4d4d77e
  * copied and put under another distribution licence
4d4d77e
  * [including the GNU Public Licence.]
4d4d77e
  */
4d4d77e
+/* ====================================================================
4d4d77e
+ * Copyright (c) 1998-2006 The OpenSSL Project.  All rights reserved.
4d4d77e
+ *
4d4d77e
+ * Redistribution and use in source and binary forms, with or without
4d4d77e
+ * modification, are permitted provided that the following conditions
4d4d77e
+ * are met:
4d4d77e
+ *
4d4d77e
+ * 1. Redistributions of source code must retain the above copyright
4d4d77e
+ *    notice, this list of conditions and the following disclaimer. 
4d4d77e
+ *
4d4d77e
+ * 2. Redistributions in binary form must reproduce the above copyright
4d4d77e
+ *    notice, this list of conditions and the following disclaimer in
4d4d77e
+ *    the documentation and/or other materials provided with the
4d4d77e
+ *    distribution.
4d4d77e
+ *
4d4d77e
+ * 3. All advertising materials mentioning features or use of this
4d4d77e
+ *    software must display the following acknowledgment:
4d4d77e
+ *    "This product includes software developed by the OpenSSL Project
4d4d77e
+ *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
4d4d77e
+ *
4d4d77e
+ * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
4d4d77e
+ *    endorse or promote products derived from this software without
4d4d77e
+ *    prior written permission. For written permission, please contact
4d4d77e
+ *    openssl-core@openssl.org.
4d4d77e
+ *
4d4d77e
+ * 5. Products derived from this software may not be called "OpenSSL"
4d4d77e
+ *    nor may "OpenSSL" appear in their names without prior written
4d4d77e
+ *    permission of the OpenSSL Project.
4d4d77e
+ *
4d4d77e
+ * 6. Redistributions of any form whatsoever must retain the following
4d4d77e
+ *    acknowledgment:
4d4d77e
+ *    "This product includes software developed by the OpenSSL Project
4d4d77e
+ *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
4d4d77e
+ *
4d4d77e
+ * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
4d4d77e
+ * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4d4d77e
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
4d4d77e
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
4d4d77e
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
4d4d77e
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
4d4d77e
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
4d4d77e
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4d4d77e
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
4d4d77e
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
4d4d77e
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
4d4d77e
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
4d4d77e
+ * ====================================================================
4d4d77e
+ *
4d4d77e
+ * This product includes cryptographic software written by Eric Young
4d4d77e
+ * (eay@cryptsoft.com).  This product includes software written by Tim
4d4d77e
+ * Hudson (tjh@cryptsoft.com).
4d4d77e
+ *
4d4d77e
+ */
4d4d77e
 
4d4d77e
 /*
4d4d77e
  * Details about Montgomery multiplication algorithms can be found at
4d4d77e
@@ -353,18 +406,32 @@
4d4d77e
 BN_MONT_CTX *BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, int lock,
4d4d77e
 					const BIGNUM *mod, BN_CTX *ctx)
4d4d77e
 	{
4d4d77e
-	if (*pmont)
4d4d77e
-		return *pmont;
4d4d77e
-	CRYPTO_w_lock(lock);
4d4d77e
+	int got_write_lock = 0;
4d4d77e
+	BN_MONT_CTX *ret;
4d4d77e
+
4d4d77e
+	CRYPTO_r_lock(lock);
4d4d77e
 	if (!*pmont)
4d4d77e
 		{
4d4d77e
-		BN_MONT_CTX *mtmp;
4d4d77e
-		mtmp = BN_MONT_CTX_new();
4d4d77e
-		if (mtmp && !BN_MONT_CTX_set(mtmp, mod, ctx))
4d4d77e
-			BN_MONT_CTX_free(mtmp);
4d4d77e
-		else
4d4d77e
-			*pmont = mtmp;
4d4d77e
+		CRYPTO_r_unlock(lock);
4d4d77e
+		CRYPTO_w_lock(lock);
4d4d77e
+		got_write_lock = 1;
4d4d77e
+
4d4d77e
+		if (!*pmont)
4d4d77e
+			{
4d4d77e
+			ret = BN_MONT_CTX_new();
4d4d77e
+			if (ret && !BN_MONT_CTX_set(ret, mod, ctx))
4d4d77e
+				BN_MONT_CTX_free(ret);
4d4d77e
+			else
4d4d77e
+				*pmont = ret;
4d4d77e
+			}
4d4d77e
 		}
4d4d77e
-	CRYPTO_w_unlock(lock);
4d4d77e
-	return *pmont;
4d4d77e
+	
4d4d77e
+	ret = *pmont;
4d4d77e
+	
4d4d77e
+	if (got_write_lock)
4d4d77e
+		CRYPTO_w_unlock(lock);
4d4d77e
+	else
4d4d77e
+		CRYPTO_r_unlock(lock);
4d4d77e
+		
4d4d77e
+	return ret;
4d4d77e
 	}
4d4d77e
--- openssl-0.9.8b/crypto/err/err.c.bn-threadsafe	2006-02-08 20:16:16.000000000 +0100
4d4d77e
+++ openssl-0.9.8b/crypto/err/err.c	2006-07-20 13:41:44.000000000 +0200
4d4d77e
@@ -548,9 +548,20 @@
4d4d77e
 	int i;
4d4d77e
 	static int init = 1;
4d4d77e
 
4d4d77e
-	if (!init) return;
4d4d77e
-
4d4d77e
+	CRYPTO_r_lock(CRYPTO_LOCK_ERR);
4d4d77e
+	if (!init)
4d4d77e
+		{
4d4d77e
+		CRYPTO_r_unlock(CRYPTO_LOCK_ERR);
4d4d77e
+		return;
4d4d77e
+		}
4d4d77e
+	
4d4d77e
+	CRYPTO_r_unlock(CRYPTO_LOCK_ERR);
4d4d77e
 	CRYPTO_w_lock(CRYPTO_LOCK_ERR);
4d4d77e
+	if (!init)
4d4d77e
+		{
4d4d77e
+		CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
4d4d77e
+		return;
4d4d77e
+		}
4d4d77e
 
4d4d77e
 	for (i = 1; i <= NUM_SYS_STR_REASONS; i++)
4d4d77e
 		{
4d4d77e
--- openssl-0.9.8b/ssl/ssl_ciph.c.bn-threadsafe	2006-04-15 02:22:34.000000000 +0200
4d4d77e
+++ openssl-0.9.8b/ssl/ssl_ciph.c	2006-07-20 13:41:44.000000000 +0200
4d4d77e
@@ -56,6 +56,59 @@
4d4d77e
  * [including the GNU Public Licence.]
4d4d77e
  */
4d4d77e
 /* ====================================================================
4d4d77e
+ * Copyright (c) 1998-2006 The OpenSSL Project.  All rights reserved.
4d4d77e
+ *
4d4d77e
+ * Redistribution and use in source and binary forms, with or without
4d4d77e
+ * modification, are permitted provided that the following conditions
4d4d77e
+ * are met:
4d4d77e
+ *
4d4d77e
+ * 1. Redistributions of source code must retain the above copyright
4d4d77e
+ *    notice, this list of conditions and the following disclaimer. 
4d4d77e
+ *
4d4d77e
+ * 2. Redistributions in binary form must reproduce the above copyright
4d4d77e
+ *    notice, this list of conditions and the following disclaimer in
4d4d77e
+ *    the documentation and/or other materials provided with the
4d4d77e
+ *    distribution.
4d4d77e
+ *
4d4d77e
+ * 3. All advertising materials mentioning features or use of this
4d4d77e
+ *    software must display the following acknowledgment:
4d4d77e
+ *    "This product includes software developed by the OpenSSL Project
4d4d77e
+ *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
4d4d77e
+ *
4d4d77e
+ * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
4d4d77e
+ *    endorse or promote products derived from this software without
4d4d77e
+ *    prior written permission. For written permission, please contact
4d4d77e
+ *    openssl-core@openssl.org.
4d4d77e
+ *
4d4d77e
+ * 5. Products derived from this software may not be called "OpenSSL"
4d4d77e
+ *    nor may "OpenSSL" appear in their names without prior written
4d4d77e
+ *    permission of the OpenSSL Project.
4d4d77e
+ *
4d4d77e
+ * 6. Redistributions of any form whatsoever must retain the following
4d4d77e
+ *    acknowledgment:
4d4d77e
+ *    "This product includes software developed by the OpenSSL Project
4d4d77e
+ *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
4d4d77e
+ *
4d4d77e
+ * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
4d4d77e
+ * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4d4d77e
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
4d4d77e
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
4d4d77e
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
4d4d77e
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
4d4d77e
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
4d4d77e
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4d4d77e
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
4d4d77e
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
4d4d77e
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
4d4d77e
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
4d4d77e
+ * ====================================================================
4d4d77e
+ *
4d4d77e
+ * This product includes cryptographic software written by Eric Young
4d4d77e
+ * (eay@cryptsoft.com).  This product includes software written by Tim
4d4d77e
+ * Hudson (tjh@cryptsoft.com).
4d4d77e
+ *
4d4d77e
+ */
4d4d77e
+/* ====================================================================
4d4d77e
  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
4d4d77e
  * ECC cipher suite support in OpenSSL originally developed by 
4d4d77e
  * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
4d4d77e
@@ -203,36 +256,46 @@
4d4d77e
 
4d4d77e
 static void load_builtin_compressions(void)
4d4d77e
 	{
4d4d77e
-	if (ssl_comp_methods != NULL)
4d4d77e
-		return;
4d4d77e
+	int got_write_lock = 0;
4d4d77e
 
4d4d77e
-	CRYPTO_w_lock(CRYPTO_LOCK_SSL);
4d4d77e
+	CRYPTO_r_lock(CRYPTO_LOCK_SSL);
4d4d77e
 	if (ssl_comp_methods == NULL)
4d4d77e
 		{
4d4d77e
-		SSL_COMP *comp = NULL;
4d4d77e
-
4d4d77e
-		MemCheck_off();
4d4d77e
-		ssl_comp_methods=sk_SSL_COMP_new(sk_comp_cmp);
4d4d77e
-		if (ssl_comp_methods != NULL)
4d4d77e
+		CRYPTO_r_unlock(CRYPTO_LOCK_SSL);
4d4d77e
+		CRYPTO_w_lock(CRYPTO_LOCK_SSL);
4d4d77e
+		got_write_lock = 1;
4d4d77e
+		
4d4d77e
+		if (ssl_comp_methods == NULL)
4d4d77e
 			{
4d4d77e
-			comp=(SSL_COMP *)OPENSSL_malloc(sizeof(SSL_COMP));
4d4d77e
-			if (comp != NULL)
4d4d77e
+			SSL_COMP *comp = NULL;
4d4d77e
+
4d4d77e
+			MemCheck_off();
4d4d77e
+			ssl_comp_methods=sk_SSL_COMP_new(sk_comp_cmp);
4d4d77e
+			if (ssl_comp_methods != NULL)
4d4d77e
 				{
4d4d77e
-				comp->method=COMP_zlib();
4d4d77e
-				if (comp->method
4d4d77e
-					&& comp->method->type == NID_undef)
4d4d77e
-					OPENSSL_free(comp);
4d4d77e
-				else
4d4d77e
+				comp=(SSL_COMP *)OPENSSL_malloc(sizeof(SSL_COMP));
4d4d77e
+				if (comp != NULL)
4d4d77e
 					{
4d4d77e
-					comp->id=SSL_COMP_ZLIB_IDX;
4d4d77e
-					comp->name=comp->method->name;
4d4d77e
-					sk_SSL_COMP_push(ssl_comp_methods,comp);
4d4d77e
+					comp->method=COMP_zlib();
4d4d77e
+					if (comp->method
4d4d77e
+						&& comp->method->type == NID_undef)
4d4d77e
+						OPENSSL_free(comp);
4d4d77e
+					else
4d4d77e
+						{
4d4d77e
+						comp->id=SSL_COMP_ZLIB_IDX;
4d4d77e
+						comp->name=comp->method->name;
4d4d77e
+						sk_SSL_COMP_push(ssl_comp_methods,comp);
4d4d77e
+						}
4d4d77e
 					}
4d4d77e
 				}
4d4d77e
+			MemCheck_on();
4d4d77e
 			}
4d4d77e
-		MemCheck_on();
4d4d77e
 		}
4d4d77e
-	CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
4d4d77e
+	
4d4d77e
+	if (got_write_lock)
4d4d77e
+		CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
4d4d77e
+	else
4d4d77e
+		CRYPTO_r_unlock(CRYPTO_LOCK_SSL);
4d4d77e
 	}
4d4d77e
 #endif
4d4d77e
 
4d4d77e
--- openssl-0.9.8b/ssl/ssl_cert.c.bn-threadsafe	2006-02-24 18:58:35.000000000 +0100
4d4d77e
+++ openssl-0.9.8b/ssl/ssl_cert.c	2006-07-20 13:41:44.000000000 +0200
4d4d77e
@@ -56,7 +56,7 @@
4d4d77e
  * [including the GNU Public Licence.]
4d4d77e
  */
4d4d77e
 /* ====================================================================
4d4d77e
- * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
4d4d77e
+ * Copyright (c) 1998-2006 The OpenSSL Project.  All rights reserved.
4d4d77e
  *
4d4d77e
  * Redistribution and use in source and binary forms, with or without
4d4d77e
  * modification, are permitted provided that the following conditions
4d4d77e
@@ -73,12 +73,12 @@
4d4d77e
  * 3. All advertising materials mentioning features or use of this
4d4d77e
  *    software must display the following acknowledgment:
4d4d77e
  *    "This product includes software developed by the OpenSSL Project
4d4d77e
- *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
4d4d77e
+ *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
4d4d77e
  *
4d4d77e
  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
4d4d77e
  *    endorse or promote products derived from this software without
4d4d77e
  *    prior written permission. For written permission, please contact
4d4d77e
- *    openssl-core@OpenSSL.org.
4d4d77e
+ *    openssl-core@openssl.org.
4d4d77e
  *
4d4d77e
  * 5. Products derived from this software may not be called "OpenSSL"
4d4d77e
  *    nor may "OpenSSL" appear in their names without prior written
4d4d77e
@@ -87,7 +87,7 @@
4d4d77e
  * 6. Redistributions of any form whatsoever must retain the following
4d4d77e
  *    acknowledgment:
4d4d77e
  *    "This product includes software developed by the OpenSSL Project
4d4d77e
- *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
4d4d77e
+ *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
4d4d77e
  *
4d4d77e
  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
4d4d77e
  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4d4d77e
@@ -102,6 +102,11 @@
4d4d77e
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
4d4d77e
  * OF THE POSSIBILITY OF SUCH DAMAGE.
4d4d77e
  * ====================================================================
4d4d77e
+ *
4d4d77e
+ * This product includes cryptographic software written by Eric Young
4d4d77e
+ * (eay@cryptsoft.com).  This product includes software written by Tim
4d4d77e
+ * Hudson (tjh@cryptsoft.com).
4d4d77e
+ *
4d4d77e
  */
4d4d77e
 /* ====================================================================
4d4d77e
  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
4d4d77e
@@ -130,21 +135,28 @@
4d4d77e
 int SSL_get_ex_data_X509_STORE_CTX_idx(void)
4d4d77e
 	{
4d4d77e
 	static volatile int ssl_x509_store_ctx_idx= -1;
4d4d77e
+	int got_write_lock = 0;
4d4d77e
+
4d4d77e
+	CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
4d4d77e
 
4d4d77e
 	if (ssl_x509_store_ctx_idx < 0)
4d4d77e
 		{
4d4d77e
-		/* any write lock will do; usually this branch
4d4d77e
-		 * will only be taken once anyway */
4d4d77e
+		CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
4d4d77e
 		CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
4d4d77e
+		got_write_lock = 1;
4d4d77e
 		
4d4d77e
 		if (ssl_x509_store_ctx_idx < 0)
4d4d77e
 			{
4d4d77e
 			ssl_x509_store_ctx_idx=X509_STORE_CTX_get_ex_new_index(
4d4d77e
 				0,"SSL for verify callback",NULL,NULL,NULL);
4d4d77e
 			}
4d4d77e
-		
4d4d77e
-		CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
4d4d77e
 		}
4d4d77e
+
4d4d77e
+	if (got_write_lock)
4d4d77e
+		CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
4d4d77e
+	else
4d4d77e
+		CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
4d4d77e
+	
4d4d77e
 	return ssl_x509_store_ctx_idx;
4d4d77e
 	}
4d4d77e