]> git.proxmox.com Git - swtpm.git/commitdiff
swtpm: Disable OpenSSL FIPS mode to avoid libtpms failures
authorStefan Berger <stefanb@linux.ibm.com>
Wed, 8 Jun 2022 13:19:07 +0000 (09:19 -0400)
committerStefan Berger <stefanb@us.ibm.com>
Thu, 16 Jun 2022 11:36:12 +0000 (07:36 -0400)
While libtpms does not provide any means to disable FIPS-disabled crypto
algorithms from being used, work around the issue by simply disabling the
FIPS mode of OpenSSL if it is enabled. If it cannot be disabled, exit
swtpm with a failure message that it cannot be disabled. If FIPS mode
was successfully disabled, print out a message as well.

Buglink: https://bugzilla.redhat.com/show_bug.cgi?id=2090219
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
configure.ac
src/swtpm/Makefile.am
src/swtpm/cuse_tpm.c
src/swtpm/fips.c [new file with mode: 0644]
src/swtpm/fips.h [new file with mode: 0644]
src/swtpm/swtpm.c
src/swtpm/swtpm_chardev.c
src/swtpm/utils.h

index ad3054e5c7e3f7698917272f1baec8c3e11d534a..30288c7a24ac25aef68aed000b009a0e506e9e55 100644 (file)
@@ -156,6 +156,15 @@ openssl)
        AC_MSG_RESULT([Building with openssl crypto library])
        LIBCRYPTO_LIBS=$(pkg-config --libs libcrypto)
        AC_SUBST([LIBCRYPTO_LIBS])
+       AC_CHECK_HEADERS([openssl/fips.h],
+                        [AC_DEFINE_UNQUOTED([HAVE_OPENSSL_FIPS_H], 1,
+                                            [whether openssl/fips.h is available])]
+                        )
+       AC_CHECK_LIB(crypto,
+                    [FIPS_mode_set],
+                    [AC_DEFINE_UNQUOTED([HAVE_OPENSSL_FIPS_MODE_SET_API], 1,
+                                        [whether FIPS_mode_set API is available])]
+                    )
        ;;
 esac
 
index 5454a6f7b7473a3963c1d081ccbd1eb31176dabf..2a659500a17f1c1acf409de6f91ed33bcc534246 100644 (file)
@@ -11,6 +11,7 @@ noinst_HEADERS = \
        capabilities.h \
        common.h \
        ctrlchannel.h \
+       fips.h \
        key.h \
        locality.h \
        logging.h \
@@ -40,6 +41,7 @@ libswtpm_libtpms_la_SOURCES = \
        capabilities.c \
        common.c \
        ctrlchannel.c \
+       fips.c \
        key.c \
        logging.c \
        mainloop.c \
index 9dbc00df28255172db3ebb476cc9261dd9852557..3026e26fffb300cb066875157cec03df2dc3f5de 100644 (file)
@@ -1695,6 +1695,11 @@ int swtpm_cuse_main(int argc, char **argv, const char *prgname, const char *ifac
         goto exit;
     }
 
+    if (disable_fips_mode() < 0) {
+        ret = -1;
+        goto exit;
+    }
+
     if (tpmlib_register_callbacks(&cbs) != TPM_SUCCESS) {
         ret = -1;
         goto exit;
diff --git a/src/swtpm/fips.c b/src/swtpm/fips.c
new file mode 100644 (file)
index 0000000..eeb2a0c
--- /dev/null
@@ -0,0 +1,100 @@
+/*
+ * fips.c -- FIPS mode related functions
+ *
+ * (c) Copyright IBM Corporation 2022.
+ *
+ * Author: Stefan Berger <stefanb@us.ibm.com>
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * Neither the names of the IBM Corporation nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+
+#include "fips.h"
+#include "logging.h"
+
+#if defined(HAVE_OPENSSL_FIPS_H)
+# include <openssl/fips.h>
+#elif defined(HAVE_OPENSSL_FIPS_MODE_SET_API)
+/* Cygwin has no fips.h but API exists */
+extern int FIPS_mode(void);
+extern int FIPS_mode_set(int);
+#endif
+
+#if OPENSSL_VERSION_NUMBER >= 0x30000000L
+# include <openssl/evp.h>
+#endif
+
+#include <openssl/err.h>
+
+/*
+ * disable_fips_mode: If possible, disable FIPS mode to avoid libtpms failures
+ *
+ * While libtpms does not provide a solution to disable deactivated algorithms
+ * avoid libtpms failures due to FIPS mode enablement by disabling FIPS mode.
+ *
+ * Returns < 0 on error, 0 otherwise.
+ */
+#if defined(HAVE_OPENSSL_FIPS_H) || defined(HAVE_OPENSSL_FIPS_MODE_SET_API)
+int disable_fips_mode(void)
+{
+#if OPENSSL_VERSION_NUMBER >= 0x30000000L
+    int mode = EVP_default_properties_is_fips_enabled(NULL);
+#else
+    int mode = FIPS_mode();
+#endif
+    int ret = 0;
+
+    if (mode != 0) {
+#if OPENSSL_VERSION_NUMBER >= 0x30000000L
+        int rc = EVP_default_properties_enable_fips(NULL, 0);
+#else
+        int rc = FIPS_mode_set(0);
+#endif
+        if (rc == 1) {
+            logprintf(STDOUT_FILENO,
+                      "Warning: Disabled OpenSSL FIPS mode\n");
+        } else {
+            unsigned long err = ERR_get_error();
+            logprintf(STDERR_FILENO,
+                      "Failed to disable OpenSSL FIPS mode: %s\n",
+                      ERR_error_string(err, NULL));
+            ret = -1;
+        }
+    }
+    return ret;
+}
+#else
+/* OpenBSD & DragonFlyBSD case */
+int disable_fips_mode(void)
+{
+    return 0;
+}
+#endif
diff --git a/src/swtpm/fips.h b/src/swtpm/fips.h
new file mode 100644 (file)
index 0000000..14d4e9f
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * fips.h -- FIPS mode related functions
+ *
+ * (c) Copyright IBM Corporation 2015.
+ *
+ * Author: Stefan Berger <stefanb@us.ibm.com>
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * Neither the names of the IBM Corporation nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef _SWTPM_UTILS_H_
+#define _SWTPM_UTILS_H_
+
+int disable_fips_mode(void);
+
+#endif /* _SWTPM_UTILS_H_ */
index 722a7432f2513631f60ded7dadb692212a9c932f..e618c567b1d2428e58ff75be30c8bc4a23cc7d90 100644 (file)
@@ -521,6 +521,9 @@ int swtpm_main(int argc, char **argv, const char *prgname, const char *iface)
         daemonize_finish();
     }
 
+    if (disable_fips_mode() < 0)
+        goto error_seccomp_profile;
+
     rc = mainLoop(&mlp, notify_fd[0]);
 
 error_seccomp_profile:
index 9710927ec6268216824568fa2fd2d9034f89267e..ab6d8fd417c6b52849d8c4977b2db78adf128228 100644 (file)
@@ -573,6 +573,9 @@ int swtpm_chardev_main(int argc, char **argv, const char *prgname, const char *i
         daemonize_finish();
     }
 
+    if (disable_fips_mode() < 0)
+        goto error_seccomp_profile;
+
     rc = mainLoop(&mlp, notify_fd[0]);
 
 error_seccomp_profile:
index 75024425d4b07e9dcebe677ef4ee25c96eccf0fc..b8acd895a2ac84f51b0ef9ece8a0318a3cf1cc82 100644 (file)
@@ -71,4 +71,6 @@ ssize_t writev_full(int fd, const struct iovec *iov, int iovcnt);
 
 ssize_t read_eintr(int fd, void *buffer, size_t buflen);
 
+int disable_fips_mode(void);
+
 #endif /* _SWTPM_UTILS_H_ */