1465572
diff -up openssl-1.0.1e/crypto/fips/fips.c.fips-ctor openssl-1.0.1e/crypto/fips/fips.c
b355146
--- openssl-1.0.1e/crypto/fips/fips.c.fips-ctor	2013-09-26 13:52:30.767885457 +0200
b355146
+++ openssl-1.0.1e/crypto/fips/fips.c	2013-09-26 14:01:29.406010187 +0200
1465572
@@ -60,6 +60,8 @@
1465572
 #include <dlfcn.h>
1465572
 #include <stdio.h>
1465572
 #include <stdlib.h>
1465572
+#include <unistd.h>
1465572
+#include <errno.h>
1465572
 #include "fips_locl.h"
1465572
 
1465572
 #ifdef OPENSSL_FIPS
850ca72
@@ -198,8 +200,10 @@ bin2hex(void *buf, size_t len)
850ca72
 	return hex;
850ca72
 }
850ca72
 
850ca72
-#define HMAC_PREFIX "." 
850ca72
-#define HMAC_SUFFIX ".hmac" 
850ca72
+#define HMAC_PREFIX "."
850ca72
+#ifndef HMAC_SUFFIX
850ca72
+#define HMAC_SUFFIX ".hmac"
850ca72
+#endif
850ca72
 #define READ_BUFFER_LENGTH 16384
850ca72
 
850ca72
 static char *
df94661
@@ -279,19 +283,13 @@ end:
df94661
 }
df94661
 
df94661
 static int
df94661
-FIPSCHECK_verify(const char *libname, const char *symbolname)
df94661
+FIPSCHECK_verify(const char *path)
df94661
 {
df94661
-	char path[PATH_MAX+1];
df94661
-	int rv;
df94661
+	int rv = 0;
df94661
 	FILE *hf;
df94661
 	char *hmacpath, *p;
df94661
 	char *hmac = NULL;
df94661
 	size_t n;
df94661
-	
df94661
-	rv = get_library_path(libname, symbolname, path, sizeof(path));
df94661
-
df94661
-	if (rv < 0)
df94661
-		return 0;
df94661
 
df94661
 	hmacpath = make_hmac_path(path);
df94661
 	if (hmacpath == NULL)
b355146
@@ -341,6 +339,53 @@ end:
1465572
 	return 1;	
1465572
 }
1465572
 
df94661
+static int
df94661
+verify_checksums(void)
df94661
+    {
df94661
+	int rv;
df94661
+	char path[PATH_MAX+1];
df94661
+	char *p;
df94661
+
df94661
+	/* we need to avoid dlopening libssl, assume both libcrypto and libssl
df94661
+	   are in the same directory */
df94661
+
df94661
+	rv = get_library_path("libcrypto.so." SHLIB_VERSION_NUMBER, "FIPS_mode_set", path, sizeof(path));
df94661
+	if (rv < 0)
df94661
+		return 0;
df94661
+
df94661
+	rv = FIPSCHECK_verify(path);
df94661
+	if (!rv)
df94661
+		return 0;
df94661
+
df94661
+	/* replace libcrypto with libssl */
df94661
+	while ((p = strstr(path, "libcrypto.so")) != NULL)
df94661
+	    {
df94661
+		p = stpcpy(p, "libssl");
df94661
+                memmove(p, p+3, strlen(p+2));
df94661
+	    }
df94661
+
df94661
+	rv = FIPSCHECK_verify(path);
df94661
+	if (!rv)
df94661
+		return 0;
df94661
+	return 1;
df94661
+    }
df94661
+
b355146
+#ifndef FIPS_MODULE_PATH
b355146
+#define FIPS_MODULE_PATH "/etc/system-fips"
b355146
+#endif
b355146
+
df94661
+int
df94661
+FIPS_module_installed(void)
1465572
+    {
b355146
+    int rv;
b355146
+    rv = access(FIPS_MODULE_PATH, F_OK);
b355146
+    if (rv < 0 && errno != ENOENT)
b355146
+	rv = 0;
1465572
+
b355146
+    /* Installed == true */
b355146
+    return !rv;
1465572
+    }
1465572
+
1465572
 int FIPS_module_mode_set(int onoff, const char *auth)
1465572
     {
1465572
     int ret = 0;
b355146
@@ -379,15 +424,7 @@ int FIPS_module_mode_set(int onoff, cons
df94661
 	}
df94661
 #endif
df94661
 
df94661
-	if(!FIPSCHECK_verify("libcrypto.so." SHLIB_VERSION_NUMBER,"FIPS_mode_set"))
df94661
-	    {
df94661
-	    FIPSerr(FIPS_F_FIPS_MODULE_MODE_SET,FIPS_R_FINGERPRINT_DOES_NOT_MATCH);
df94661
-	    fips_selftest_fail = 1;
df94661
-	    ret = 0;
df94661
-	    goto end;
df94661
-	    }
df94661
-
df94661
-	if(!FIPSCHECK_verify("libssl.so." SHLIB_VERSION_NUMBER,"SSL_CTX_new"))
df94661
+	if(!verify_checksums())
df94661
 	    {
df94661
 	    FIPSerr(FIPS_F_FIPS_MODULE_MODE_SET,FIPS_R_FINGERPRINT_DOES_NOT_MATCH);
df94661
 	    fips_selftest_fail = 1;
1465572
diff -up openssl-1.0.1e/crypto/fips/fips.h.fips-ctor openssl-1.0.1e/crypto/fips/fips.h
b355146
--- openssl-1.0.1e/crypto/fips/fips.h.fips-ctor	2013-09-26 13:52:30.814886515 +0200
b355146
+++ openssl-1.0.1e/crypto/fips/fips.h	2013-09-26 13:52:30.816886560 +0200
1465572
@@ -74,6 +74,7 @@ struct hmac_ctx_st;
1465572
 
1465572
 int FIPS_module_mode_set(int onoff, const char *auth);
1465572
 int FIPS_module_mode(void);
1465572
+int FIPS_module_installed(void);
1465572
 const void *FIPS_rand_check(void);
1465572
 int FIPS_selftest(void);
1465572
 int FIPS_selftest_failed(void);
1465572
diff -up openssl-1.0.1e/crypto/o_init.c.fips-ctor openssl-1.0.1e/crypto/o_init.c
b355146
--- openssl-1.0.1e/crypto/o_init.c.fips-ctor	2013-09-26 13:52:30.807886357 +0200
b355146
+++ openssl-1.0.1e/crypto/o_init.c	2013-09-26 14:00:21.000000000 +0200
b355146
@@ -71,6 +71,9 @@ static void init_fips_mode(void)
1465572
 	char buf[2] = "0";
1465572
 	int fd;
1465572
 	
b355146
+	/* Ensure the selftests always run */
1465572
+	FIPS_mode_set(1);
1465572
+
1465572
 	if (secure_getenv("OPENSSL_FORCE_FIPS_MODE") != NULL)
1465572
 		{
1465572
 		buf[0] = '1';
b355146
@@ -85,9 +88,15 @@ static void init_fips_mode(void)
1465572
 	 * otherwise. 
1465572
 	 */
1465572
 	
1465572
-	if (buf[0] == '1')
1465572
+	if (buf[0] != '1')
b355146
+		{
1465572
+		/* drop down to non-FIPS mode if it is not requested */
1465572
+		FIPS_mode_set(0);
b355146
+		}
b355146
+	else
b355146
 		{
b355146
-		FIPS_mode_set(1);
b355146
+		/* abort if selftest failed */
b355146
+		FIPS_selftest_check();
1465572
 		}
1465572
 	}
1465572
 #endif
b355146
@@ -96,13 +105,17 @@ static void init_fips_mode(void)
1465572
  * Currently only sets FIPS callbacks
1465572
  */
1465572
 
1465572
-void OPENSSL_init_library(void)
1465572
+void __attribute__ ((constructor)) OPENSSL_init_library(void)
1465572
 	{
1465572
 	static int done = 0;
1465572
 	if (done)
1465572
 		return;
1465572
 	done = 1;
1465572
 #ifdef OPENSSL_FIPS
1465572
+	if (!FIPS_module_installed())
1465572
+		{
1465572
+		return;
1465572
+		}
1465572
 	RAND_init_fips();
1465572
 	init_fips_mode();
1465572
 	if (!FIPS_mode())