To protect FIPS-related global variables, add locking mechanisms
authorRichard Levitte <levitte@openssl.org>
Fri, 30 Jul 2004 14:38:02 +0000 (14:38 +0000)
committerRichard Levitte <levitte@openssl.org>
Fri, 30 Jul 2004 14:38:02 +0000 (14:38 +0000)
around them.

NOTE: because two new locks are added, this adds potential binary
incompatibility with earlier versions in the 0.9.7 series.  However,
those locks will only ever be touched when FIPS_mode_set() is called
and after, thanks to a variable that's only changed from 0 to 1 once
(when FIPS_mode_set() is called).  So basically, as long as FIPS mode
hasn't been engaged explicitely by the calling application, the new
locks are treated as if they didn't exist at all, thus not becoming a
problem.  Applications that are built or rebuilt to use FIPS
functionality will need to be recompiled in any case, thus not being a
problem either.

31 files changed:
crypto/cryptlib.c
crypto/crypto.h
crypto/dsa/dsa_sign.c
crypto/dsa/dsa_vrf.c
crypto/md32_common.h
crypto/pem/pem_all.c
crypto/rand/md_rand.c
crypto/rand/rand_lib.c
fips/aes/fingerprint.sha1
fips/aes/fips_aes_core.c
fips/des/fingerprint.sha1
fips/des/fips_des_enc.c
fips/des/fips_set_key.c
fips/dh/fingerprint.sha1
fips/dh/fips_dh_gen.c
fips/dsa/fingerprint.sha1
fips/dsa/fips_dsa_gen.c
fips/dsa/fips_dsa_ossl.c
fips/fingerprint.sha1
fips/fips.c
fips/fips.h
fips/fips_locl.h
fips/rsa/fingerprint.sha1
fips/rsa/fips_rsa_eay.c
fips/rsa/fips_rsa_gen.c
fips/sha1/fingerprint.sha1
fips/sha1/fips_md32_common.h
fips/sha1/fips_sha_locl.h
fips/sha1/fips_standalone_sha1.c
fips/sha1/standalone.sha1
ssl/ssl_ciph.c

index 84c1e6853f3dd79ba8d3dde40910cdd863a4f51b..d1851b3085ff316306bb3b943ba084aae534c6fd 100644 (file)
 static double SSLeay_MSVC5_hack=0.0; /* and for VC1.5 */
 #endif
 
-#ifdef OPENSSL_FIPS
-int FIPS_mode;
-void *FIPS_rand_check;
-#endif /* def OPENSSL_FIPS */
-
 DECLARE_STACK_OF(CRYPTO_dynlock)
 IMPLEMENT_STACK_OF(CRYPTO_dynlock)
 
@@ -110,7 +105,9 @@ static const char* lock_names[CRYPTO_NUM_LOCKS] =
        "engine",
        "ui",
        "hwcrhk",               /* This is a HACK which will disappear in 0.9.8 */
-#if CRYPTO_NUM_LOCKS != 33
+       "fips",
+       "fips2",
+#if CRYPTO_NUM_LOCKS != 35
 # error "Inconsistency between crypto.h and cryptlib.c"
 #endif
        };
@@ -517,3 +514,122 @@ void OpenSSLDie(const char *file,int line,const char *assertion)
                file,line,assertion);
        abort();
        }
+
+#ifdef OPENSSL_FIPS
+static int fips_started = 0;
+static int fips_mode = 0;
+static void *fips_rand_check = 0;
+static unsigned long fips_thread = 0;
+
+void fips_set_started(void)
+       {
+       fips_started = 1;
+       }
+
+int fips_is_started(void)
+       {
+       return fips_started;
+       }
+
+int fips_is_owning_thread(void)
+       {
+       int ret = 0;
+
+       if (fips_is_started())
+               {
+               CRYPTO_r_lock(CRYPTO_LOCK_FIPS2);
+               if (fips_thread != 0 && fips_thread == CRYPTO_thread_id())
+                       ret = 1;
+               CRYPTO_r_unlock(CRYPTO_LOCK_FIPS2);
+               }
+       return ret;
+       }
+
+int fips_set_owning_thread(void)
+       {
+       int ret = 0;
+
+       if (fips_is_started())
+               {
+               CRYPTO_r_lock(CRYPTO_LOCK_FIPS2);
+               if (fips_thread == 0)
+                       {
+                       fips_thread = CRYPTO_thread_id();
+                       ret = 1;
+                       }
+               CRYPTO_r_unlock(CRYPTO_LOCK_FIPS2);
+               }
+       return ret;
+       }
+
+int fips_clear_owning_thread(void)
+       {
+       int ret = 0;
+
+       if (fips_is_started())
+               {
+               CRYPTO_r_lock(CRYPTO_LOCK_FIPS2);
+               if (fips_thread == CRYPTO_thread_id())
+                       {
+                       fips_thread = 0;
+                       ret = 1;
+                       }
+               CRYPTO_r_unlock(CRYPTO_LOCK_FIPS2);
+               }
+       return ret;
+       }
+
+void fips_set_mode(int onoff)
+       {
+       int owning_thread = fips_is_owning_thread();
+
+       if (fips_is_started())
+               {
+               if (!owning_thread) CRYPTO_w_lock(CRYPTO_LOCK_FIPS);
+               fips_mode = onoff;
+               if (!owning_thread) CRYPTO_w_unlock(CRYPTO_LOCK_FIPS);
+               }
+       }
+
+void fips_set_rand_check(void *rand_check)
+       {
+       int owning_thread = fips_is_owning_thread();
+
+       if (fips_is_started())
+               {
+               if (!owning_thread) CRYPTO_w_lock(CRYPTO_LOCK_FIPS);
+               fips_rand_check = rand_check;
+               if (!owning_thread) CRYPTO_w_unlock(CRYPTO_LOCK_FIPS);
+               }
+       }
+
+int FIPS_mode(void)
+       {
+       int ret = 0;
+       int owning_thread = fips_is_owning_thread();
+
+       if (fips_is_started())
+               {
+               if (!owning_thread) CRYPTO_r_lock(CRYPTO_LOCK_FIPS);
+               ret = fips_mode;
+               if (!owning_thread) CRYPTO_r_unlock(CRYPTO_LOCK_FIPS);
+               }
+       return ret;
+       }
+
+void *FIPS_rand_check(void)
+       {
+       void *ret = 0;
+       int owning_thread = fips_is_owning_thread();
+
+       if (fips_is_started())
+               {
+               if (!owning_thread) CRYPTO_r_lock(CRYPTO_LOCK_FIPS);
+               ret = fips_rand_check;
+               if (!owning_thread) CRYPTO_r_unlock(CRYPTO_LOCK_FIPS);
+               }
+       return ret;
+       }
+
+#endif /* OPENSSL_FIPS */
+
index 273bc5e3f87df60d82e4dc75feed4c109d2ce3e0..b779a14d12f10feece4af82f2e00f282d3be9747 100644 (file)
@@ -128,7 +128,9 @@ extern "C" {
 #define CRYPTO_LOCK_ENGINE             30
 #define CRYPTO_LOCK_UI                 31
 #define CRYPTO_LOCK_HWCRHK             32 /* This is a HACK which will disappear in 0.9.8 */
-#define CRYPTO_NUM_LOCKS               33
+#define CRYPTO_LOCK_FIPS               33
+#define CRYPTO_LOCK_FIPS2              34
+#define CRYPTO_NUM_LOCKS               35
 
 #define CRYPTO_LOCK            1
 #define CRYPTO_UNLOCK          2
@@ -434,6 +436,11 @@ void CRYPTO_mem_leaks_cb(CRYPTO_MEM_LEAK_CB *cb);
 void OpenSSLDie(const char *file,int line,const char *assertion);
 #define OPENSSL_assert(e)      ((e) ? (void)0 : OpenSSLDie(__FILE__, __LINE__, #e))
 
+#ifdef OPENSSL_FIPS
+int FIPS_mode(void);
+void *FIPS_rand_check(void);
+#endif /* def OPENSSL_FIPS */
+
 /* BEGIN ERROR CODES */
 /* The following lines are auto generated by the script mkerr.pl. Any changes
  * made after this point may be overwritten when the script is next run.
index 1821adf3d6b219df7d678c10097bbe9994116eaf..3c9753bac391f0a67bbd70ccccbe0aae0e8edaf8 100644 (file)
@@ -72,7 +72,7 @@
 DSA_SIG * DSA_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
        {
 #ifdef OPENSSL_FIPS
-       if(FIPS_mode && !FIPS_dsa_check(dsa))
+       if(FIPS_mode() && !FIPS_dsa_check(dsa))
                return NULL;
 #endif
        return dsa->meth->dsa_do_sign(dgst, dlen, dsa);
@@ -96,7 +96,7 @@ int DSA_sign(int type, const unsigned char *dgst, int dlen, unsigned char *sig,
 int DSA_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp)
        {
 #ifdef OPENSSL_FIPS
-       if(FIPS_mode && !FIPS_dsa_check(dsa))
+       if(FIPS_mode() && !FIPS_dsa_check(dsa))
                return 0;
 #endif
        return dsa->meth->dsa_sign_setup(dsa, ctx_in, kinvp, rp);
index 493bdd07cd9dc5dedd1d8fb712469e5a9de9f1d7..8ef0c4502527c67a587448d831566574e7e20856 100644 (file)
@@ -74,7 +74,7 @@ int DSA_do_verify(const unsigned char *dgst, int dgst_len, DSA_SIG *sig,
                  DSA *dsa)
        {
 #ifdef OPENSSL_FIPS
-       if(FIPS_mode && !FIPS_dsa_check(dsa))
+       if(FIPS_mode() && !FIPS_dsa_check(dsa))
                return -1;
 #endif
        return dsa->meth->dsa_do_verify(dgst, dgst_len, sig, dsa);
index 6fc4467a676db5494841cf771b14e663eaeab9e9..8137c57b1c55f85cfc8992fa9278eb655efe50a4 100644 (file)
  *                                     <appro@fy.chalmers.se>
  */
 
+#include <openssl/crypto.h>
 #include <openssl/fips.h>
 #include <openssl/err.h>
-#include "../fips/fips_locl.h"
 
 #if !defined(DATA_ORDER_IS_BIG_ENDIAN) && !defined(DATA_ORDER_IS_LITTLE_ENDIAN)
 #error "DATA_ORDER must be defined!"
@@ -560,7 +560,7 @@ int HASH_FINAL (unsigned char *md, HASH_CTX *c)
        const unsigned char *cp=end;
 
 #ifdef OPENSSL_FIPS
-       if(FIPS_mode && !FIPS_md5_allowed)
+       if(FIPS_mode() && !FIPS_md5_allowed())
            {
            FIPSerr(FIPS_F_HASH_FINAL,FIPS_R_NON_FIPS_METHOD);
            return 0;
index 2e9662941795ed36dfa2dfa54d5d742522650429..07963314c95110f95f72db654ba4e339bf3691b7 100644 (file)
@@ -281,7 +281,7 @@ int PEM_write_bio_PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
                                                unsigned char *kstr, int klen,
                                                pem_password_cb *cb, void *u)
        {
-               if (FIPS_mode)
+               if (FIPS_mode())
                        return PEM_write_bio_PKCS8PrivateKey(bp, x, enc,
                                                (char *)kstr, klen, cb, u);
                else
@@ -295,7 +295,7 @@ int PEM_write_PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
                                                unsigned char *kstr, int klen,
                                                pem_password_cb *cb, void *u)
        {
-               if (FIPS_mode)
+               if (FIPS_mode())
                        return PEM_write_PKCS8PrivateKey(fp, x, enc,
                                                (char *)kstr, klen, cb, u);
                else
index 24e590ab64dde34998cb8b502b18a0e62483e458..c84968df88fe5d5b0f0a6176699d123d395c6552 100644 (file)
@@ -334,7 +334,7 @@ static int ssleay_rand_bytes(unsigned char *buf, int num)
        int do_stir_pool = 0;
 
 #ifdef OPENSSL_FIPS
-       if(FIPS_mode)
+       if(FIPS_mode())
            {
            FIPSerr(FIPS_F_SSLEAY_RAND_BYTES,FIPS_R_NON_FIPS_METHOD);
            return 0;
index 869d11cab6c1ee37e82a46acbce7c7c980516791..88f1b56d91e21a4c939bb115ac2237a08312e4ca 100644 (file)
@@ -88,7 +88,8 @@ int RAND_set_rand_method(const RAND_METHOD *meth)
 const RAND_METHOD *RAND_get_rand_method(void)
        {
 #ifdef OPENSSL_FIPS
-       if(FIPS_mode && default_RAND_meth != FIPS_rand_check)
+       if(FIPS_mode()
+               && default_RAND_meth != FIPS_rand_check())
            {
            RANDerr(RAND_F_RAND_GET_RAND_METHOD,RAND_R_NON_FIPS_METHOD);
            return 0;
index 121def333c30a686d1c6fbc4b7ff7c337b794161..969c5b300db8f169a99dbcdcf8d4a2b1fc9d5b08 100644 (file)
@@ -1,3 +1,3 @@
-HMAC-SHA1(fips_aes_core.c)= e3b9b4ddceaca72392f59ee05164d9e6a81521a7
+HMAC-SHA1(fips_aes_core.c)= b70bbbd675efe0613da0d57055310926a0104d55
 HMAC-SHA1(fips_aes_selftest.c)= 98b01502221e7fe529fd981222f2cbb52eb4cbe0
 HMAC-SHA1(fips_aes_locl.h)= ded58f0cda8cb967dc5f5f3a860601c0b8744623
index 49169e7a4ed9f0122895264fc713cbedb0aaa422..82199c92e67856351685514bae08f9309d0e64b6 100644 (file)
@@ -738,7 +738,7 @@ int AES_set_encrypt_key(const unsigned char *userKey,
                return -1;
        if (bits != 128 && bits != 192 && bits != 256)
                return -2;
-       if(FIPS_selftest_fail)
+       if(FIPS_selftest_failed())
                return -3;
 
        rk = key->rd_key;
index 816714f5e048567bc7f682c8c3aaf3b7ca4353fe..83833d83d120ef2bf1b0e8bbf2dae24764246eaa 100644 (file)
@@ -1,5 +1,5 @@
-HMAC-SHA1(fips_des_enc.c)= ea89417ba58c148c3d72d29438cd0bedc2315f7f
+HMAC-SHA1(fips_des_enc.c)= 9527f8ea81602358f1aa11348237fdb1e9eeff32
 HMAC-SHA1(asm/fips-dx86-elf.s)= 2f85e8e86806c92ee4c12cf5354e19eccf6ed47d
 HMAC-SHA1(fips_des_selftest.c)= 3bc574e51647c5f5ab45d1007b2cf461d67764a9
-HMAC-SHA1(fips_set_key.c)= dfe1bf8221a8cce7591ad33c9433271613332bd0
+HMAC-SHA1(fips_set_key.c)= 2858450d3d9c8d4ab8edea683baa54fa34f3a605
 HMAC-SHA1(fips_des_locl.h)= 7053848e884df47f06de9f2248380b92e58ef4e5
index 8d364fe28b158eb31974427d2026cf05f0676f96..40e25efa58213160c7c8225a5cb9db98b6894f86 100644 (file)
@@ -72,7 +72,7 @@ void DES_encrypt1(DES_LONG *data, DES_key_schedule *ks, int enc)
 #endif
        register DES_LONG *s;
 
-       if(FIPS_selftest_fail)
+       if(FIPS_selftest_failed())
            {
            data[0]=data[1]=0;
            return;
@@ -176,7 +176,7 @@ void DES_encrypt2(DES_LONG *data, DES_key_schedule *ks, int enc)
 #endif
        register DES_LONG *s;
 
-       if(FIPS_selftest_fail)
+       if(FIPS_selftest_failed())
            {
            data[0]=data[1]=0;
            return;
index 5ca00c15aa66024c6552b2b10e52aecf2af3127f..1490a3cf549bf1235b4a631c75b85affc31e0689 100644 (file)
@@ -64,7 +64,7 @@
  * 1.0 First working version
  */
 #include "fips_des_locl.h"
-#include "../fips.h"
+#include <openssl/fips.h>
 
 #ifdef OPENSSL_FIPS
 
@@ -312,7 +312,7 @@ static const DES_LONG des_skb[8][64]={
 
 int DES_set_key(const_DES_cblock *key, DES_key_schedule *schedule)
        {
-       if (FIPS_selftest_fail)
+       if (FIPS_selftest_failed())
                return -3;
        if (DES_check_key)
                {
@@ -335,7 +335,7 @@ int DES_set_key_checked(const_DES_cblock *key, DES_key_schedule *schedule)
                return(-1);
        if (DES_is_weak_key(key))
                return(-2);
-       if (FIPS_selftest_fail)
+       if (FIPS_selftest_failed())
                return -3;
 
        DES_set_key_unchecked(key, schedule);
index 6e4b2444d6f6db5f115ac7483ade74afdf022939..6896d386493fc1ff49be08fdf5069df5afb60ca3 100644 (file)
@@ -1,3 +1,3 @@
 HMAC-SHA1(fips_dh_check.c)= 63347e2007e224381d4a7b6d871633889de72cf3
-HMAC-SHA1(fips_dh_gen.c)= 6aaf18dab32bf9a5fb4b7449ac4cbe559040adb4
+HMAC-SHA1(fips_dh_gen.c)= 93fe69b758ca9d70d70cda1c57fff4eb5c668e85
 HMAC-SHA1(fips_dh_key.c)= 7bf23b329a776953bbe7c30ebd7f9faf5249ddbe
index 09fbc4d53f28a45a9a8076e3ee38fdde75dfb921..b569e3912d220225c6faff8a5c48350761842c48 100644 (file)
@@ -102,7 +102,7 @@ DH *DH_generate_parameters(int prime_len, int generator,
        int g,ok= -1;
        BN_CTX *ctx=NULL;
 
-       if(FIPS_selftest_fail)
+       if(FIPS_selftest_failed())
                {
                FIPSerr(FIPS_F_DH_GENERATE_PARAMETERS,FIPS_R_FIPS_SELFTEST_FAILED);
                return NULL;
index 444eb690470c0042a7827b3f2690e9ddc8daea23..4a89db5cf33d19aaf44413a39c56f5e48ad28e0a 100644 (file)
@@ -1,3 +1,3 @@
-HMAC-SHA1(fips_dsa_ossl.c)= b817acc77487f42298205cc5fdd2593e30c66a9d
-HMAC-SHA1(fips_dsa_gen.c)= 6276272125759148b60f2500fa40beea84648a21
+HMAC-SHA1(fips_dsa_ossl.c)= d5f718695397fe56d6bb46f7c410794cb895e206
+HMAC-SHA1(fips_dsa_gen.c)= c252db14699f3ff641db052311da7d7521569c53
 HMAC-SHA1(fips_dsa_selftest.c)= 4bfc5d3a6b977527b053f3a03d0760a822a26135
index 6fa63f61cdbb7b7b020abe75c51fe1b65de50e57..21fa3d1783cd43c3d99fbb090f3290e97944a70e 100644 (file)
@@ -75,7 +75,7 @@
 /*#include "cryptlib.h"*/
 #include <openssl/evp.h>
 #include <openssl/bn.h>
-#ifndef OPENSSL_NO_SHA
+#ifndef OPENSSL_NO_DSA
 #include <openssl/dsa.h>
 #endif
 #ifndef OPENSSL_NO_RAND
 #endif
 #ifndef OPENSSL_NO_SHA
 #include <openssl/sha.h>
+#endif
 #include <openssl/fips.h>
 #include <openssl/err.h>
 
+#ifndef OPENSSL_NO_DSA
 #ifdef OPENSSL_FIPS
 
 static int fips_check_dsa(DSA *dsa)
@@ -124,7 +126,7 @@ DSA *DSA_generate_parameters(FIPS_DSA_SIZE_T bits,
        DSA *ret=NULL;
        unsigned char *seed_out=seed_in;
 
-       if(FIPS_selftest_fail)
+       if(FIPS_selftest_failed())
            {
            FIPSerr(FIPS_F_DSA_GENERATE_PARAMETERS,
                    FIPS_R_FIPS_SELFTEST_FAILED);
@@ -369,5 +371,4 @@ err:
        return(ok);
        }
 #endif
-
 #endif
index a39286101bdae974b6511766c2161094b3608ec0..0ae5eb4b9e1326484146dc4e36554c48dbb21cc3 100644 (file)
@@ -125,7 +125,7 @@ static DSA_SIG *dsa_do_sign(const unsigned char *dgst, FIPS_DSA_SIZE_T dlen, DSA
        int i,reason=ERR_R_BN_LIB;
        DSA_SIG *ret=NULL;
 
-       if(FIPS_selftest_fail)
+       if(FIPS_selftest_failed())
            {
            FIPSerr(FIPS_F_DSA_DO_SIGN,FIPS_R_FIPS_SELFTEST_FAILED);
            return NULL;
@@ -271,7 +271,7 @@ static int dsa_do_verify(const unsigned char *dgst, FIPS_DSA_SIZE_T dgst_len, DS
                return -1;
                }
 
-       if(FIPS_selftest_fail)
+       if(FIPS_selftest_failed())
            {
            FIPSerr(FIPS_F_DSA_DO_VERIFY,FIPS_R_FIPS_SELFTEST_FAILED);
            return -1;
index 4c6a2db1ace2f166c408100a531216b67b9f3833..2e02be6f9c6c2ecf2e049f1c6564346fee0ef772 100644 (file)
@@ -1,4 +1,4 @@
-HMAC-SHA1(fips.c)= 5d2cc58767613ab8fb98927eaa2015f357dd4012
+HMAC-SHA1(fips.c)= bb4247d57a16f571ded9b2feea827a90cb2dbb79
 HMAC-SHA1(fips_err_wrapper.c)= d3e2be316062510312269e98f964cb87e7577898
-HMAC-SHA1(fips.h)= e793b0a7017d57a37b89743cf59b40a30385b63f
+HMAC-SHA1(fips.h)= fbedad5dbd8986ddd521ea576bf2a20e6881540a
 HMAC-SHA1(fips_err.h)= 4a73f2a88e206f1f88edfd9b26609a0eed818491
index 0fe28125046c94b95da6b64e80d52f2c5bc18fa7..48b1b6338d4a29a37f1c9ab8a63a1da5fc25a488 100644 (file)
 #define PATH_MAX 1024
 #endif
 
-int FIPS_md5_allowed;
-int FIPS_selftest_fail;
+static int fips_md5_allowed = 0;
+static int fips_selftest_fail = 0;
+
+void FIPS_allow_md5(int onoff)
+    {
+    if (fips_is_started())
+       {
+       int owning_thread = fips_is_owning_thread();
+
+       if (!owning_thread) CRYPTO_w_lock(CRYPTO_LOCK_FIPS);
+       fips_md5_allowed = onoff;
+       if (!owning_thread) CRYPTO_w_unlock(CRYPTO_LOCK_FIPS);
+       }
+    }
+
+int FIPS_md5_allowed(void)
+    {
+    int ret = 1;
+    if (fips_is_started())
+       {
+       int owning_thread = fips_is_owning_thread();
+
+       if (!owning_thread) CRYPTO_r_lock(CRYPTO_LOCK_FIPS);
+       ret = fips_md5_allowed;
+       if (!owning_thread) CRYPTO_r_unlock(CRYPTO_LOCK_FIPS);
+       }
+    return ret;
+    }
+
+int FIPS_selftest_failed(void)
+    {
+    int ret = 0;
+    if (fips_is_started())
+       {
+       int owning_thread = fips_is_owning_thread();
+
+       if (!owning_thread) CRYPTO_r_lock(CRYPTO_LOCK_FIPS);
+       ret = fips_selftest_fail;
+       if (!owning_thread) CRYPTO_r_unlock(CRYPTO_LOCK_FIPS);
+       }
+    return ret;
+    }
 
 int FIPS_selftest()
     {
@@ -126,25 +166,36 @@ static int FIPS_check_exe(const char *path)
 
 int FIPS_mode_set(int onoff,const char *path)
     {
+    void fips_set_mode(int onoff);
+    int fips_set_owning_thread();
+    int fips_clear_owning_thread();
+    int ret = 0;
+
+    CRYPTO_w_lock(CRYPTO_LOCK_FIPS);
+    fips_set_started();
+    fips_set_owning_thread();
+
     if(onoff)
        {
        unsigned char buf[24];
 
-       FIPS_selftest_fail=0;
+       fips_selftest_fail = 0;
 
        /* Don't go into FIPS mode twice, just so we can do automagic
           seeding */
-       if(FIPS_mode)
+       if(FIPS_mode())
            {
            FIPSerr(FIPS_F_FIPS_MODE_SET,FIPS_R_FIPS_MODE_ALREADY_SET);
-           FIPS_selftest_fail=1;
-           return 0;
+           fips_selftest_fail = 1;
+           ret = 0;
+           goto end;
            }
 
        if(!FIPS_check_exe(path))
            {
-           FIPS_selftest_fail=1;
-           return 0;
+           fips_selftest_fail = 1;
+           ret = 0;
+           goto end;
            }
 
        /* automagically seed PRNG if not already seeded */
@@ -152,33 +203,35 @@ int FIPS_mode_set(int onoff,const char *path)
            {
            if(RAND_bytes(buf,sizeof buf) <= 0)
                {
-               FIPS_selftest_fail=1;
-               return 0;
+               fips_selftest_fail = 1;
+               ret = 0;
+               goto end;
                }
            FIPS_set_prng_key(buf,buf+8);
            FIPS_rand_seed(buf+16,8);
            }
 
        /* now switch into FIPS mode */
-       FIPS_rand_check=FIPS_rand_method();
+       fips_set_rand_check(FIPS_rand_method());
        RAND_set_rand_method(FIPS_rand_method());
        if(FIPS_selftest())
-           FIPS_mode=1;
+           fips_set_mode(1);
        else
            {
-           FIPS_selftest_fail=1;
-           return 0;
+           fips_selftest_fail = 1;
+           ret = 0;
+           goto end;
            }
-       return 1;
+       ret = 1;
+       goto end;
        }
-    FIPS_mode=0;
-    FIPS_selftest_fail=0;
-    return 1;
-    }
-
-void FIPS_allow_md5(int onoff)
-    {
-    FIPS_md5_allowed=onoff;
+    fips_set_mode(0);
+    fips_selftest_fail = 0;
+    ret = 1;
+end:
+    fips_clear_owning_thread();
+    CRYPTO_w_unlock(CRYPTO_LOCK_FIPS);
+    return ret;
     }
 
 #if 0
index 9ce48dce7bc83a393462fdf4bf8d043a7bf87e3e..a4df06b1482fa20dc1353ca4f95d5897794df131 100644 (file)
@@ -58,13 +58,12 @@ extern "C" {
 /* Note that these are defined in crypto/cryptlib.c so they're
  * available even without -lfips.
  */
-extern int FIPS_mode;
-extern int FIPS_selftest_fail;
-extern void *FIPS_rand_check;
 struct dsa_st;
 
 int FIPS_mode_set(int onoff,const char *path);
 void FIPS_allow_md5(int onoff);
+int FIPS_md5_allowed(void);
+int FIPS_selftest_failed(void);
 int FIPS_dsa_check(struct dsa_st *dsa);
 void FIPS_corrupt_sha1(void);
 int FIPS_selftest_sha1(void);
index 501fc756f30163b209e040283828602b9b52af15..215e3825494d615b7dd45409ebdf461ad6de2d01 100644 (file)
 extern "C" {
 #endif
 
-/* FIPS 140 allows MD5 to be used during certain parts of TLS */
-extern int FIPS_md5_allowed;
+/* These are really defined in crypto/cryptlib.c */
+void fips_set_started(void);
+int fips_is_started(void);
+int fips_is_owning_thread(void);
+int fips_set_owning_thread(void);
+int fips_clear_owning_thread(void);
+void fips_set_rand_check(void *rand_check);
 
 #ifdef  __cplusplus
 }
index 8b42e7506d9855f3e81a8ba7dde4f0465aa20a48..057ad15c3ef808678b938eeecddecb2e200ab22b 100644 (file)
@@ -1,3 +1,3 @@
-HMAC-SHA1(fips_rsa_eay.c)= 660512794d0a702fc2bf17ae094e9e3181bf9152
-HMAC-SHA1(fips_rsa_gen.c)= 6bcf339dda5bb7d7e162c30d579431848a5e921f
+HMAC-SHA1(fips_rsa_eay.c)= eabab59a2f11f3da4c21e1144efe1684f5e8f1ec
+HMAC-SHA1(fips_rsa_gen.c)= 4bbc0afcade1ac53f469aaa89f84c413678254bf
 HMAC-SHA1(fips_rsa_selftest.c)= ed69ec28f12af451b8e694e52ac8b6c9bffc0db2
index e4b11d9956088ce3a65bf08bfc35ab24d2db74a1..c571e2b1bf617dcb011ad67008ef3ccfd04abf8f 100644 (file)
@@ -108,7 +108,7 @@ static int RSA_eay_public_encrypt(FIPS_RSA_SIZE_T flen, const unsigned char *fro
        BN_init(&f);
        BN_init(&ret);
 
-       if(FIPS_selftest_fail)
+       if(FIPS_selftest_failed())
                {
                FIPSerr(FIPS_F_RSA_EAY_PUBLIC_ENCRYPT,FIPS_R_FIPS_SELFTEST_FAILED);
                goto err;
index cb671678b70e0eb0149ba460dbfbd0f6fc65792e..2c92112477288c912811a3388224849fdb2fd8f2 100644 (file)
@@ -112,7 +112,7 @@ RSA *RSA_generate_key(FIPS_RSA_SIZE_T bits, unsigned long e_value,
        int bitsp,bitsq,ok= -1,n=0,i;
        BN_CTX *ctx=NULL,*ctx2=NULL;
 
-       if(FIPS_selftest_fail)
+       if(FIPS_selftest_failed())
            {
            FIPSerr(FIPS_F_RSA_GENERATE_KEY,FIPS_R_FIPS_SELFTEST_FAILED);
            return NULL;
index 9b57f9d9945e19c992f9b2f612d6f11627776ecd..27a69970f4739777a8a9b8813f05ff140a42eb20 100644 (file)
@@ -1,5 +1,5 @@
 HMAC-SHA1(fips_sha1dgst.c)= 10575600a9540eb15188a7d3b0b031e60aedbc18
 HMAC-SHA1(fips_sha1_selftest.c)= 98910a0c85eff1688bd7adb23e738dc75b39546e
 HMAC-SHA1(asm/sx86-elf.s)= 6286cba0ea3b071e67ab5c1e607d1387de6a871d
-HMAC-SHA1(fips_sha_locl.h)= b793c80946d1029a630844393e294b27f61b1485
-HMAC-SHA1(fips_md32_common.h)= cd86b0f4a9a22552dce8db3ae5f2614e54a61f15
+HMAC-SHA1(fips_sha_locl.h)= 61e5b59c8a43e21d2f022101852467a7176e52eb
+HMAC-SHA1(fips_md32_common.h)= 1c7e761db430067391b1b7b86da5d2bf6df92834
index e714475dc6cc785c8d9f19800cf1e7d45f641ed2..55d65f5821c314125596e8c0965a4429a92172f4 100644 (file)
@@ -417,7 +417,7 @@ int HASH_UPDATE (HASH_CTX *c, const void *data_, FIPS_SHA_SIZE_T len)
        register HASH_LONG l;
        int sw,sc,ew,ec;
 
-       if(FIPS_selftest_fail)
+       if(FIPS_selftest_failed())
                return 0;
 
        if (len==0) return 1;
index 5008aa79b978100337b5f7359b80683ae924ec8f..84f65584591ef8e85aba173647b169fe01af08c8 100644 (file)
@@ -233,7 +233,7 @@ void HASH_BLOCK_HOST_ORDER (SHA_CTX *c, const void *d, FIPS_SHA_SIZE_T num)
        SHA_LONG        XX[16];
 #endif
 
-       if(FIPS_selftest_fail)
+       if(FIPS_selftest_failed())
            return;
 
        A=c->h0;
@@ -361,7 +361,7 @@ void HASH_BLOCK_DATA_ORDER (SHA_CTX *c, const void *p, FIPS_SHA_SIZE_T num)
        SHA_LONG        XX[16];
 #endif
 
-       if(FIPS_selftest_fail)
+       if(FIPS_selftest_failed())
            return;
 
        A=c->h0;
index bd3e72c719539f8da32f4a6d7261d42556445d2d..1c2ec469e9cf2253e1d66eead3c18100c75c741c 100644 (file)
@@ -54,7 +54,7 @@
 #include <stdlib.h>
 #include <string.h>
 
-int FIPS_selftest_fail;
+int FIPS_selftest_failed() { return 0; }
 
 #ifdef OPENSSL_FIPS
 
index d11f6424f20e72fd2f964b01ddde70f42e4f7845..f95b1426df847b51db81ccc6b396231788cacea7 100644 (file)
@@ -1,6 +1,6 @@
 HMAC-SHA1(fips_sha1dgst.c)= 10575600a9540eb15188a7d3b0b031e60aedbc18
 HMAC-SHA1(fips_sha1_selftest.c)= 98910a0c85eff1688bd7adb23e738dc75b39546e
 HMAC-SHA1(asm/sx86-elf.s)= 6286cba0ea3b071e67ab5c1e607d1387de6a871d
-HMAC-SHA1(fips_standalone_sha1.c)= c17f83ccfe601558b33b6df27d2d82887b8c9dc2
-HMAC-SHA1(fips_sha_locl.h)= b793c80946d1029a630844393e294b27f61b1485
-HMAC-SHA1(fips_md32_common.h)= cd86b0f4a9a22552dce8db3ae5f2614e54a61f15
+HMAC-SHA1(fips_standalone_sha1.c)= 93203c569097189b47a0085bc9fc55193867d4ce
+HMAC-SHA1(fips_sha_locl.h)= 61e5b59c8a43e21d2f022101852467a7176e52eb
+HMAC-SHA1(fips_md32_common.h)= 1c7e761db430067391b1b7b86da5d2bf6df92834
index 78251d9f9ebf81eae6d7c20a2b2f40bb54603a00..f76ba92348fbe0705b77efbb94c26e8bb4debf03 100644 (file)
@@ -363,7 +363,7 @@ static void ssl_cipher_collect_ciphers(const SSL_METHOD *ssl_method,
                /* drop those that use any of that is not available */
 #ifdef OPENSSL_FIPS
                if ((c != NULL) && c->valid && !(c->algorithms & mask)
-                       && (!FIPS_mode || (c->algo_strength & SSL_FIPS)))
+                       && (!FIPS_mode() || (c->algo_strength & SSL_FIPS)))
 #else
                if ((c != NULL) && c->valid && !(c->algorithms & mask))
 #endif
@@ -862,7 +862,7 @@ STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(const SSL_METHOD *ssl_method,
        for (curr = head; curr != NULL; curr = curr->next)
                {
 #ifdef OPENSSL_FIPS
-               if (curr->active && (!FIPS_mode || curr->cipher->algo_strength & SSL_FIPS))
+               if (curr->active && (!FIPS_mode() || curr->cipher->algo_strength & SSL_FIPS))
 #else
                if (curr->active)
 #endif