|
|
fd2893 |
diff -up openssl-1.0.2a/crypto/fips/fips.c.fips-ctor openssl-1.0.2a/crypto/fips/fips.c
|
|
|
fd2893 |
--- openssl-1.0.2a/crypto/fips/fips.c.fips-ctor 2015-04-21 17:42:18.702765856 +0200
|
|
|
fd2893 |
+++ openssl-1.0.2a/crypto/fips/fips.c 2015-04-21 17:42:18.742766794 +0200
|
|
|
fd2893 |
@@ -60,6 +60,8 @@
|
|
|
fd2893 |
#include <dlfcn.h>
|
|
|
fd2893 |
#include <stdio.h>
|
|
|
fd2893 |
#include <stdlib.h>
|
|
|
fd2893 |
+#include <unistd.h>
|
|
|
fd2893 |
+#include <errno.h>
|
|
|
fd2893 |
#include "fips_locl.h"
|
|
|
fd2893 |
|
|
|
fd2893 |
#ifdef OPENSSL_FIPS
|
|
|
fd2893 |
@@ -201,7 +203,9 @@ static char *bin2hex(void *buf, size_t l
|
|
|
fd2893 |
}
|
|
|
fd2893 |
|
|
|
fd2893 |
# define HMAC_PREFIX "."
|
|
|
fd2893 |
-# define HMAC_SUFFIX ".hmac"
|
|
|
fd2893 |
+# ifndef HMAC_SUFFIX
|
|
|
fd2893 |
+# define HMAC_SUFFIX ".hmac"
|
|
|
fd2893 |
+# endif
|
|
|
fd2893 |
# define READ_BUFFER_LENGTH 16384
|
|
|
fd2893 |
|
|
|
fd2893 |
static char *make_hmac_path(const char *origpath)
|
|
|
fd2893 |
@@ -279,20 +283,14 @@ static int compute_file_hmac(const char
|
|
|
fd2893 |
return rv;
|
|
|
fd2893 |
}
|
|
|
fd2893 |
|
|
|
fd2893 |
-static int FIPSCHECK_verify(const char *libname, const char *symbolname)
|
|
|
fd2893 |
+static int FIPSCHECK_verify(const char *path)
|
|
|
fd2893 |
{
|
|
|
fd2893 |
- char path[PATH_MAX + 1];
|
|
|
fd2893 |
- int rv;
|
|
|
fd2893 |
+ int rv = 0;
|
|
|
fd2893 |
FILE *hf;
|
|
|
fd2893 |
char *hmacpath, *p;
|
|
|
fd2893 |
char *hmac = NULL;
|
|
|
fd2893 |
size_t n;
|
|
|
fd2893 |
|
|
|
fd2893 |
- rv = get_library_path(libname, symbolname, path, sizeof(path));
|
|
|
fd2893 |
-
|
|
|
fd2893 |
- if (rv < 0)
|
|
|
fd2893 |
- return 0;
|
|
|
fd2893 |
-
|
|
|
fd2893 |
hmacpath = make_hmac_path(path);
|
|
|
fd2893 |
if (hmacpath == NULL)
|
|
|
fd2893 |
return 0;
|
|
|
fd2893 |
@@ -343,6 +341,51 @@ static int FIPSCHECK_verify(const char *
|
|
|
fd2893 |
return 1;
|
|
|
fd2893 |
}
|
|
|
fd2893 |
|
|
|
fd2893 |
+static int verify_checksums(void)
|
|
|
fd2893 |
+{
|
|
|
fd2893 |
+ int rv;
|
|
|
fd2893 |
+ char path[PATH_MAX + 1];
|
|
|
fd2893 |
+ char *p;
|
|
|
fd2893 |
+
|
|
|
fd2893 |
+ /* we need to avoid dlopening libssl, assume both libcrypto and libssl
|
|
|
fd2893 |
+ are in the same directory */
|
|
|
fd2893 |
+
|
|
|
fd2893 |
+ rv = get_library_path("libcrypto.so." SHLIB_VERSION_NUMBER,
|
|
|
fd2893 |
+ "FIPS_mode_set", path, sizeof(path));
|
|
|
fd2893 |
+ if (rv < 0)
|
|
|
fd2893 |
+ return 0;
|
|
|
fd2893 |
+
|
|
|
fd2893 |
+ rv = FIPSCHECK_verify(path);
|
|
|
fd2893 |
+ if (!rv)
|
|
|
fd2893 |
+ return 0;
|
|
|
fd2893 |
+
|
|
|
fd2893 |
+ /* replace libcrypto with libssl */
|
|
|
fd2893 |
+ while ((p = strstr(path, "libcrypto.so")) != NULL) {
|
|
|
fd2893 |
+ p = stpcpy(p, "libssl");
|
|
|
fd2893 |
+ memmove(p, p + 3, strlen(p + 2));
|
|
|
fd2893 |
+ }
|
|
|
fd2893 |
+
|
|
|
fd2893 |
+ rv = FIPSCHECK_verify(path);
|
|
|
fd2893 |
+ if (!rv)
|
|
|
fd2893 |
+ return 0;
|
|
|
fd2893 |
+ return 1;
|
|
|
fd2893 |
+}
|
|
|
fd2893 |
+
|
|
|
fd2893 |
+# ifndef FIPS_MODULE_PATH
|
|
|
fd2893 |
+# define FIPS_MODULE_PATH "/etc/system-fips"
|
|
|
fd2893 |
+# endif
|
|
|
fd2893 |
+
|
|
|
fd2893 |
+int FIPS_module_installed(void)
|
|
|
fd2893 |
+{
|
|
|
fd2893 |
+ int rv;
|
|
|
fd2893 |
+ rv = access(FIPS_MODULE_PATH, F_OK);
|
|
|
fd2893 |
+ if (rv < 0 && errno != ENOENT)
|
|
|
fd2893 |
+ rv = 0;
|
|
|
fd2893 |
+
|
|
|
fd2893 |
+ /* Installed == true */
|
|
|
fd2893 |
+ return !rv;
|
|
|
fd2893 |
+}
|
|
|
fd2893 |
+
|
|
|
fd2893 |
int FIPS_module_mode_set(int onoff, const char *auth)
|
|
|
fd2893 |
{
|
|
|
fd2893 |
int ret = 0;
|
|
|
fd2893 |
@@ -380,17 +423,7 @@ int FIPS_module_mode_set(int onoff, cons
|
|
|
fd2893 |
}
|
|
|
fd2893 |
# endif
|
|
|
fd2893 |
|
|
|
fd2893 |
- if (!FIPSCHECK_verify
|
|
|
fd2893 |
- ("libcrypto.so." SHLIB_VERSION_NUMBER, "FIPS_mode_set")) {
|
|
|
fd2893 |
- FIPSerr(FIPS_F_FIPS_MODULE_MODE_SET,
|
|
|
fd2893 |
- FIPS_R_FINGERPRINT_DOES_NOT_MATCH);
|
|
|
fd2893 |
- fips_selftest_fail = 1;
|
|
|
fd2893 |
- ret = 0;
|
|
|
fd2893 |
- goto end;
|
|
|
fd2893 |
- }
|
|
|
fd2893 |
-
|
|
|
fd2893 |
- if (!FIPSCHECK_verify
|
|
|
fd2893 |
- ("libssl.so." SHLIB_VERSION_NUMBER, "SSL_CTX_new")) {
|
|
|
fd2893 |
+ if (!verify_checksums()) {
|
|
|
fd2893 |
FIPSerr(FIPS_F_FIPS_MODULE_MODE_SET,
|
|
|
fd2893 |
FIPS_R_FINGERPRINT_DOES_NOT_MATCH);
|
|
|
fd2893 |
fips_selftest_fail = 1;
|
|
|
fd2893 |
diff -up openssl-1.0.2a/crypto/fips/fips.h.fips-ctor openssl-1.0.2a/crypto/fips/fips.h
|
|
|
fd2893 |
--- openssl-1.0.2a/crypto/fips/fips.h.fips-ctor 2015-04-21 17:42:18.739766724 +0200
|
|
|
fd2893 |
+++ openssl-1.0.2a/crypto/fips/fips.h 2015-04-21 17:42:18.743766818 +0200
|
|
|
fd2893 |
@@ -74,6 +74,7 @@ extern "C" {
|
|
|
fd2893 |
|
|
|
fd2893 |
int FIPS_module_mode_set(int onoff, const char *auth);
|
|
|
fd2893 |
int FIPS_module_mode(void);
|
|
|
fd2893 |
+ int FIPS_module_installed(void);
|
|
|
fd2893 |
const void *FIPS_rand_check(void);
|
|
|
fd2893 |
int FIPS_selftest(void);
|
|
|
fd2893 |
int FIPS_selftest_failed(void);
|
|
|
fd2893 |
diff -up openssl-1.0.2a/crypto/o_init.c.fips-ctor openssl-1.0.2a/crypto/o_init.c
|
|
|
fd2893 |
--- openssl-1.0.2a/crypto/o_init.c.fips-ctor 2015-04-21 17:42:18.732766559 +0200
|
|
|
fd2893 |
+++ openssl-1.0.2a/crypto/o_init.c 2015-04-21 17:45:02.662613173 +0200
|
|
|
fd2893 |
@@ -74,6 +74,9 @@ static void init_fips_mode(void)
|
|
|
fd2893 |
char buf[2] = "0";
|
|
|
fd2893 |
int fd;
|
|
|
fd2893 |
|
|
|
fd2893 |
+ /* Ensure the selftests always run */
|
|
|
fd2893 |
+ FIPS_mode_set(1);
|
|
|
fd2893 |
+
|
|
|
fd2893 |
if (secure_getenv("OPENSSL_FORCE_FIPS_MODE") != NULL) {
|
|
|
fd2893 |
buf[0] = '1';
|
|
|
fd2893 |
} else if ((fd = open(FIPS_MODE_SWITCH_FILE, O_RDONLY)) >= 0) {
|
|
|
fd2893 |
@@ -85,8 +88,12 @@ static void init_fips_mode(void)
|
|
|
fd2893 |
* otherwise..
|
|
|
fd2893 |
*/
|
|
|
fd2893 |
|
|
|
fd2893 |
- if (buf[0] == '1') {
|
|
|
fd2893 |
- FIPS_mode_set(1);
|
|
|
fd2893 |
+ if (buf[0] != '1') {
|
|
|
fd2893 |
+ /* drop down to non-FIPS mode if it is not requested */
|
|
|
fd2893 |
+ FIPS_mode_set(0);
|
|
|
fd2893 |
+ } else {
|
|
|
fd2893 |
+ /* abort if selftest failed */
|
|
|
fd2893 |
+ FIPS_selftest_check();
|
|
|
fd2893 |
}
|
|
|
fd2893 |
}
|
|
|
fd2893 |
#endif
|
|
|
fd2893 |
@@ -96,13 +103,16 @@ static void init_fips_mode(void)
|
|
|
fd2893 |
* sets FIPS callbacks
|
|
|
fd2893 |
*/
|
|
|
fd2893 |
|
|
|
fd2893 |
-void OPENSSL_init_library(void)
|
|
|
fd2893 |
+void __attribute__ ((constructor)) OPENSSL_init_library(void)
|
|
|
fd2893 |
{
|
|
|
fd2893 |
static int done = 0;
|
|
|
fd2893 |
if (done)
|
|
|
fd2893 |
return;
|
|
|
fd2893 |
done = 1;
|
|
|
fd2893 |
#ifdef OPENSSL_FIPS
|
|
|
fd2893 |
+ if (!FIPS_module_installed()) {
|
|
|
fd2893 |
+ return;
|
|
|
fd2893 |
+ }
|
|
|
fd2893 |
RAND_init_fips();
|
|
|
fd2893 |
init_fips_mode();
|
|
|
fd2893 |
if (!FIPS_mode()) {
|