Blame SOURCES/gnutls-3.7.3-libtss2-dlopen.patch

cabbaa
From f5e5ab910b8b1d69f962ca033d1295c3e1e1e131 Mon Sep 17 00:00:00 2001
cabbaa
From: Daiki Ueno <ueno@gnu.org>
cabbaa
Date: Wed, 23 Feb 2022 19:48:52 +0100
cabbaa
Subject: [PATCH] tpm2: dynamically load tss2 libraries as needed
cabbaa
cabbaa
libtss2-esys links to OpenSSL or mbed TLS for cryptography, which may
cabbaa
cause packaging issues.  This instead dlopen's tss2 libraries as
cabbaa
needed so non-TPM applications continue working without loading
cabbaa
multiple crypto libraries.
cabbaa
cabbaa
Signed-off-by: Daiki Ueno <ueno@gnu.org>
cabbaa
---
cabbaa
 configure.ac        |  11 +-
cabbaa
 lib/Makefile.am     |   6 +-
cabbaa
 lib/tpm2.c          |   2 +-
cabbaa
 lib/tpm2.h          |   2 +-
cabbaa
 lib/tpm2_esys.c     | 273 ++++++++++++++++++++++++++++++++++++--------
cabbaa
 tests/Makefile.am   |   3 +-
cabbaa
 tests/sanity-lib.sh |  40 +++++++
cabbaa
 tests/tpm2.sh       |  14 ++-
cabbaa
 8 files changed, 296 insertions(+), 55 deletions(-)
cabbaa
 create mode 100644 tests/sanity-lib.sh
cabbaa
cabbaa
diff --git a/configure.ac b/configure.ac
cabbaa
index 53c3aefca1..721ff208f0 100644
cabbaa
--- a/configure.ac
cabbaa
+++ b/configure.ac
cabbaa
@@ -882,6 +882,8 @@ AM_CONDITIONAL(P11KIT_0_23_11_API, $PKG_CONFIG --atleast-version=0.23.11 p11-kit
cabbaa
 
cabbaa
 AM_CONDITIONAL(ENABLE_PKCS11, test "$with_p11_kit" != "no")
cabbaa
 
cabbaa
+need_ltlibdl=no
cabbaa
+
cabbaa
 AC_ARG_WITH(tpm2,
cabbaa
 	AS_HELP_STRING([--without-tpm2],
cabbaa
 		[Disable TPM2 support.]),
cabbaa
@@ -892,6 +894,7 @@ if test "$with_tpm2" != "no"; then
cabbaa
 	if test "$have_tpm2" = "yes"; then
cabbaa
 		tss2lib="tss2-esys tss2-mu tss2-tctildr"
cabbaa
 		AC_DEFINE([HAVE_TSS2], 1, [Have TSS2])
cabbaa
+		need_ltlibdl=yes
cabbaa
 	elif test "$with_tpm2" = "yes"; then
cabbaa
 		AC_MSG_ERROR([[
cabbaa
 ***
cabbaa
@@ -920,7 +923,8 @@ if test "$with_tpm" != "no"; then
cabbaa
 		   AC_SUBST([TSS_LIBS], [-ltspi])
cabbaa
 		   AC_SUBST([TSS_CFLAGS], [])
cabbaa
 		   AC_DEFINE([HAVE_TROUSERS], 1, [Enable TPM])
cabbaa
-		   with_tpm=yes],
cabbaa
+		   with_tpm=yes,
cabbaa
+		   need_ltlibdl=yes],
cabbaa
 		  [AC_MSG_RESULT(no)
cabbaa
 		   AC_MSG_WARN([[
cabbaa
 *** 
cabbaa
@@ -957,6 +961,9 @@ fi
cabbaa
 AC_DEFINE_UNQUOTED([TROUSERS_LIB], ["$ac_trousers_lib"], [the location of the trousers library])
cabbaa
 AC_SUBST(TROUSERS_LIB)
cabbaa
 
cabbaa
+
cabbaa
+AM_CONDITIONAL(NEED_LTLIBDL, test "$need_ltlibdl" = yes)
cabbaa
+
cabbaa
 # For minitasn1.
cabbaa
 AC_CHECK_SIZEOF(unsigned long int, 4)
cabbaa
 AC_CHECK_SIZEOF(unsigned int, 4)
cabbaa
@@ -1312,7 +1319,7 @@ AC_MSG_NOTICE([External hardware support:
cabbaa
   Random gen. variant:  $rnd_variant
cabbaa
   PKCS#11 support:      $with_p11_kit
cabbaa
   TPM support:          $with_tpm
cabbaa
-  TPM2 support:         $have_tpm2
cabbaa
+  TPM2 support:         $with_tpm2
cabbaa
   KTLS support:         $enable_ktls
cabbaa
 ])
cabbaa
 
cabbaa
diff --git a/lib/Makefile.am b/lib/Makefile.am
cabbaa
index 35df35ee8d..e61ee1b6ae 100644
cabbaa
--- a/lib/Makefile.am
cabbaa
+++ b/lib/Makefile.am
cabbaa
@@ -44,7 +44,7 @@ AM_CPPFLAGS = \
cabbaa
 	-I$(srcdir)/x509			\
cabbaa
 	$(LIBTASN1_CFLAGS)			\
cabbaa
 	$(P11_KIT_CFLAGS)			\
cabbaa
-	$(TPM2_CFLAGS)
cabbaa
+	$(TSS2_CFLAGS)
cabbaa
 
cabbaa
 if !HAVE_LIBUNISTRING
cabbaa
 SUBDIRS += unistring
cabbaa
@@ -156,7 +156,7 @@ libgnutls_la_LIBADD = ../gl/libgnu.la x509/libgnutls_x509.la \
cabbaa
 	auth/libgnutls_auth.la algorithms/libgnutls_alg.la \
cabbaa
 	extras/libgnutls_extras.la
cabbaa
 thirdparty_libadd = $(LTLIBZ) $(LTLIBINTL) $(LIBSOCKET) $(LTLIBNSL) \
cabbaa
-	$(P11_KIT_LIBS) $(LIB_SELECT) $(TSS2_LIBS) $(GNUTLS_LIBS_PRIVATE)
cabbaa
+	$(P11_KIT_LIBS) $(LIB_SELECT) $(GNUTLS_LIBS_PRIVATE)
cabbaa
 
cabbaa
 if HAVE_LIBIDN2
cabbaa
 thirdparty_libadd += $(LIBIDN2_LIBS)
cabbaa
@@ -203,7 +203,7 @@ all-local: $(hmac_files)
cabbaa
 CLEANFILES = $(hmac_files)
cabbaa
 endif
cabbaa
 
cabbaa
-if ENABLE_TROUSERS
cabbaa
+if NEED_LTLIBDL
cabbaa
 thirdparty_libadd += $(LTLIBDL)
cabbaa
 endif
cabbaa
 
cabbaa
diff --git a/lib/tpm2.c b/lib/tpm2.c
cabbaa
index 076cc7f407..750eadc777 100644
cabbaa
--- a/lib/tpm2.c
cabbaa
+++ b/lib/tpm2.c
cabbaa
@@ -297,5 +297,5 @@ int _gnutls_load_tpm2_key(gnutls_privkey_t pkey, const gnutls_datum_t *fdata)
cabbaa
 
cabbaa
 void _gnutls_tpm2_deinit(void)
cabbaa
 {
cabbaa
-	tpm2_tcti_deinit();
cabbaa
+	tpm2_esys_deinit();
cabbaa
 }
cabbaa
diff --git a/lib/tpm2.h b/lib/tpm2.h
cabbaa
index e40dc01df7..7966e2d811 100644
cabbaa
--- a/lib/tpm2.h
cabbaa
+++ b/lib/tpm2.h
cabbaa
@@ -37,7 +37,7 @@ struct tpm2_info_st;
cabbaa
 
cabbaa
 struct tpm2_info_st *tpm2_info_init(struct pin_info_st *pin);
cabbaa
 
cabbaa
-void tpm2_tcti_deinit(void);
cabbaa
+void tpm2_esys_deinit(void);
cabbaa
 
cabbaa
 void release_tpm2_ctx(struct tpm2_info_st *info);
cabbaa
 
cabbaa
diff --git a/lib/tpm2_esys.c b/lib/tpm2_esys.c
cabbaa
index 93e54413ba..4000c1b76e 100644
cabbaa
--- a/lib/tpm2_esys.c
cabbaa
+++ b/lib/tpm2_esys.c
cabbaa
@@ -72,6 +72,170 @@
cabbaa
 #include <tss2/tss2_esys.h>
cabbaa
 #include <tss2/tss2_tctildr.h>
cabbaa
 
cabbaa
+#include <dlfcn.h>
cabbaa
+
cabbaa
+/* We don't want to link to libtss2-esys, as it brings in other
cabbaa
+ * crypto libraries.  Instead, only dlopen it as needed.
cabbaa
+ */
cabbaa
+
cabbaa
+static void *_gnutls_tss2_esys_dlhandle;
cabbaa
+static void *_gnutls_tss2_mu_dlhandle;
cabbaa
+static void *_gnutls_tss2_tctildr_dlhandle;
cabbaa
+
cabbaa
+static TSS2_RC
cabbaa
+(*_gnutls_tss2_Esys_GetCapability)(ESYS_CONTEXT *esysContext,
cabbaa
+				   ESYS_TR shandle1,
cabbaa
+				   ESYS_TR shandle2,
cabbaa
+				   ESYS_TR shandle3,
cabbaa
+				   TPM2_CAP capability,
cabbaa
+				   UINT32 property,
cabbaa
+				   UINT32 propertyCount,
cabbaa
+				   TPMI_YES_NO *moreData,
cabbaa
+				   TPMS_CAPABILITY_DATA **capabilityData);
cabbaa
+static void (*_gnutls_tss2_Esys_Free)(void *__ptr);
cabbaa
+static TSS2_RC (*_gnutls_tss2_Esys_TR_SetAuth)(ESYS_CONTEXT *esysContext,
cabbaa
+					       ESYS_TR handle,
cabbaa
+					       TPM2B_AUTH const *authValue);
cabbaa
+static TSS2_RC
cabbaa
+(*_gnutls_tss2_Esys_CreatePrimary)(ESYS_CONTEXT *esysContext,
cabbaa
+				   ESYS_TR primaryHandle,
cabbaa
+				   ESYS_TR shandle1,
cabbaa
+				   ESYS_TR shandle2,
cabbaa
+				   ESYS_TR shandle3,
cabbaa
+				   const TPM2B_SENSITIVE_CREATE *inSensitive,
cabbaa
+				   const TPM2B_PUBLIC *inPublic,
cabbaa
+				   const TPM2B_DATA *outsideInfo,
cabbaa
+				   const TPML_PCR_SELECTION *creationPCR,
cabbaa
+				   ESYS_TR *objectHandle,
cabbaa
+				   TPM2B_PUBLIC **outPublic,
cabbaa
+				   TPM2B_CREATION_DATA **creationData,
cabbaa
+				   TPM2B_DIGEST **creationHash,
cabbaa
+				   TPMT_TK_CREATION **creationTicket);
cabbaa
+static TSS2_RC (*_gnutls_tss2_Esys_Initialize)(ESYS_CONTEXT **esys_context,
cabbaa
+					       TSS2_TCTI_CONTEXT *tcti,
cabbaa
+					       TSS2_ABI_VERSION *abiVersion);
cabbaa
+static TSS2_RC (*_gnutls_tss2_Esys_Startup)(ESYS_CONTEXT *esysContext,
cabbaa
+					    TPM2_SU startupType);
cabbaa
+static TSS2_RC (*_gnutls_tss2_Esys_TR_FromTPMPublic)(ESYS_CONTEXT *esysContext,
cabbaa
+						     TPM2_HANDLE tpm_handle,
cabbaa
+						     ESYS_TR optionalSession1,
cabbaa
+						     ESYS_TR optionalSession2,
cabbaa
+						     ESYS_TR optionalSession3,
cabbaa
+						     ESYS_TR *object);
cabbaa
+static TSS2_RC (*_gnutls_tss2_Esys_ReadPublic)(ESYS_CONTEXT *esysContext,
cabbaa
+					       ESYS_TR objectHandle,
cabbaa
+					       ESYS_TR shandle1,
cabbaa
+					       ESYS_TR shandle2,
cabbaa
+					       ESYS_TR shandle3,
cabbaa
+					       TPM2B_PUBLIC **outPublic,
cabbaa
+					       TPM2B_NAME **name,
cabbaa
+					       TPM2B_NAME **qualifiedName);
cabbaa
+static TSS2_RC (*_gnutls_tss2_Esys_Load)(ESYS_CONTEXT *esysContext,
cabbaa
+					 ESYS_TR parentHandle,
cabbaa
+					 ESYS_TR shandle1,
cabbaa
+					 ESYS_TR shandle2,
cabbaa
+					 ESYS_TR shandle3,
cabbaa
+					 const TPM2B_PRIVATE *inPrivate,
cabbaa
+					 const TPM2B_PUBLIC *inPublic,
cabbaa
+					 ESYS_TR *objectHandle);
cabbaa
+static TSS2_RC (*_gnutls_tss2_Esys_FlushContext)(ESYS_CONTEXT *esysContext,
cabbaa
+						 ESYS_TR flushHandle);
cabbaa
+static void (*_gnutls_tss2_Esys_Finalize)(ESYS_CONTEXT **context);
cabbaa
+static TSS2_RC
cabbaa
+(*_gnutls_tss2_Esys_RSA_Decrypt)(ESYS_CONTEXT *esysContext,
cabbaa
+				 ESYS_TR keyHandle,
cabbaa
+				 ESYS_TR shandle1,
cabbaa
+				 ESYS_TR shandle2,
cabbaa
+				 ESYS_TR shandle3,
cabbaa
+				 const TPM2B_PUBLIC_KEY_RSA *cipherText,
cabbaa
+				 const TPMT_RSA_DECRYPT *inScheme,
cabbaa
+				 const TPM2B_DATA *label,
cabbaa
+				 TPM2B_PUBLIC_KEY_RSA **message);
cabbaa
+static TSS2_RC (*_gnutls_tss2_Esys_Sign)(ESYS_CONTEXT *esysContext,
cabbaa
+					 ESYS_TR keyHandle,
cabbaa
+					 ESYS_TR shandle1,
cabbaa
+					 ESYS_TR shandle2,
cabbaa
+					 ESYS_TR shandle3,
cabbaa
+					 const TPM2B_DIGEST *digest,
cabbaa
+					 const TPMT_SIG_SCHEME *inScheme,
cabbaa
+					 const TPMT_TK_HASHCHECK *validation,
cabbaa
+					 TPMT_SIGNATURE **signature);
cabbaa
+
cabbaa
+static TSS2_RC
cabbaa
+(*_gnutls_tss2_Tss2_MU_TPM2B_PRIVATE_Unmarshal)(uint8_t const buffer[],
cabbaa
+						size_t buffer_size,
cabbaa
+						size_t *offset,
cabbaa
+						TPM2B_PRIVATE *dest);
cabbaa
+static TSS2_RC
cabbaa
+(*_gnutls_tss2_Tss2_MU_TPM2B_PUBLIC_Unmarshal)(uint8_t const buffer[],
cabbaa
+					       size_t buffer_size,
cabbaa
+					       size_t *offset,
cabbaa
+					       TPM2B_PUBLIC *dest);
cabbaa
+
cabbaa
+static TSS2_RC
cabbaa
+(*_gnutls_tss2_Tss2_TctiLdr_Initialize)(const char *nameConf,
cabbaa
+					TSS2_TCTI_CONTEXT **context);
cabbaa
+static void (*_gnutls_tss2_Tss2_TctiLdr_Finalize)(TSS2_TCTI_CONTEXT **context);
cabbaa
+
cabbaa
+#define DLSYM_TSS2(sys, sym)						\
cabbaa
+	_gnutls_tss2_##sym = dlsym(_gnutls_tss2_##sys##_dlhandle, #sym); \
cabbaa
+	if (!_gnutls_tss2_##sym) {					\
cabbaa
+		return -1;						\
cabbaa
+	}
cabbaa
+
cabbaa
+static int
cabbaa
+init_tss2_funcs(void)
cabbaa
+{
cabbaa
+	if (!_gnutls_tss2_esys_dlhandle) {
cabbaa
+		_gnutls_tss2_esys_dlhandle =
cabbaa
+			dlopen("libtss2-esys.so.0", RTLD_NOW | RTLD_GLOBAL);
cabbaa
+		if (!_gnutls_tss2_esys_dlhandle) {
cabbaa
+			_gnutls_debug_log("tpm2: unable to dlopen libtss2-esys\n");
cabbaa
+			return -1;
cabbaa
+		}
cabbaa
+	}
cabbaa
+
cabbaa
+	DLSYM_TSS2(esys, Esys_GetCapability)
cabbaa
+	DLSYM_TSS2(esys, Esys_Free)
cabbaa
+	DLSYM_TSS2(esys, Esys_TR_SetAuth)
cabbaa
+	DLSYM_TSS2(esys, Esys_CreatePrimary)
cabbaa
+	DLSYM_TSS2(esys, Esys_Initialize)
cabbaa
+	DLSYM_TSS2(esys, Esys_Startup)
cabbaa
+	DLSYM_TSS2(esys, Esys_TR_FromTPMPublic)
cabbaa
+	DLSYM_TSS2(esys, Esys_ReadPublic)
cabbaa
+	DLSYM_TSS2(esys, Esys_Load)
cabbaa
+	DLSYM_TSS2(esys, Esys_FlushContext)
cabbaa
+	DLSYM_TSS2(esys, Esys_Finalize)
cabbaa
+	DLSYM_TSS2(esys, Esys_RSA_Decrypt)
cabbaa
+	DLSYM_TSS2(esys, Esys_Sign)
cabbaa
+
cabbaa
+	if (!_gnutls_tss2_mu_dlhandle) {
cabbaa
+		_gnutls_tss2_mu_dlhandle =
cabbaa
+			dlopen("libtss2-mu.so.0", RTLD_NOW | RTLD_GLOBAL);
cabbaa
+		if (!_gnutls_tss2_mu_dlhandle) {
cabbaa
+			_gnutls_debug_log("tpm2: unable to dlopen libtss2-mu\n");
cabbaa
+			return -1;
cabbaa
+		}
cabbaa
+	}
cabbaa
+
cabbaa
+	DLSYM_TSS2(mu, Tss2_MU_TPM2B_PRIVATE_Unmarshal)
cabbaa
+	DLSYM_TSS2(mu, Tss2_MU_TPM2B_PUBLIC_Unmarshal)
cabbaa
+
cabbaa
+	if (!_gnutls_tss2_tctildr_dlhandle) {
cabbaa
+		_gnutls_tss2_tctildr_dlhandle =
cabbaa
+			dlopen("libtss2-tctildr.so.0", RTLD_NOW | RTLD_GLOBAL);
cabbaa
+		if (!_gnutls_tss2_tctildr_dlhandle) {
cabbaa
+			_gnutls_debug_log("tpm2: unable to dlopen libtss2-tctildr\n");
cabbaa
+			return -1;
cabbaa
+		}
cabbaa
+	}
cabbaa
+
cabbaa
+	DLSYM_TSS2(tctildr, Tss2_TctiLdr_Initialize)
cabbaa
+	DLSYM_TSS2(tctildr, Tss2_TctiLdr_Finalize)
cabbaa
+
cabbaa
+	return 0;
cabbaa
+}
cabbaa
+
cabbaa
 struct tpm2_info_st {
cabbaa
 	TPM2B_PUBLIC pub;
cabbaa
 	TPM2B_PRIVATE priv;
cabbaa
@@ -227,10 +391,10 @@ get_primary_template(ESYS_CONTEXT *ctx)
cabbaa
 	UINT32 i;
cabbaa
 	TSS2_RC rc;
cabbaa
 
cabbaa
-	rc = Esys_GetCapability (ctx,
cabbaa
-				 ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE,
cabbaa
-				 TPM2_CAP_ALGS, 0, TPM2_MAX_CAP_ALGS,
cabbaa
-				 NULL, &capability_data);
cabbaa
+	rc = _gnutls_tss2_Esys_GetCapability(ctx,
cabbaa
+				ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE,
cabbaa
+				TPM2_CAP_ALGS, 0, TPM2_MAX_CAP_ALGS,
cabbaa
+				NULL, &capability_data);
cabbaa
 	if (rc) {
cabbaa
 		_gnutls_debug_log("tpm2: Esys_GetCapability failed: 0x%x\n", rc);
cabbaa
 		return NULL;
cabbaa
@@ -239,7 +403,7 @@ get_primary_template(ESYS_CONTEXT *ctx)
cabbaa
 	for (i = 0; i < capability_data->data.algorithms.count; i++) {
cabbaa
 		if (capability_data->data.algorithms.algProperties[i].alg ==
cabbaa
 		    TPM2_ALG_ECC) {
cabbaa
-			Esys_Free(capability_data);
cabbaa
+			_gnutls_tss2_Esys_Free(capability_data);
cabbaa
 			return &primary_template_ecc;
cabbaa
 		}
cabbaa
 	}
cabbaa
@@ -247,12 +411,12 @@ get_primary_template(ESYS_CONTEXT *ctx)
cabbaa
 	for (i = 0; i < capability_data->data.algorithms.count; i++) {
cabbaa
 		if (capability_data->data.algorithms.algProperties[i].alg ==
cabbaa
 		    TPM2_ALG_RSA) {
cabbaa
-			Esys_Free(capability_data);
cabbaa
+			_gnutls_tss2_Esys_Free(capability_data);
cabbaa
 			return &primary_template_rsa;
cabbaa
 		}
cabbaa
         }
cabbaa
 
cabbaa
-	Esys_Free(capability_data);
cabbaa
+	_gnutls_tss2_Esys_Free(capability_data);
cabbaa
 	_gnutls_debug_log("tpm2: unable to find primary template\n");
cabbaa
 	return NULL;
cabbaa
 }
cabbaa
@@ -320,7 +484,7 @@ static int init_tpm2_primary(struct tpm2_info_st *info,
cabbaa
 		install_tpm_passphrase(&info->ownerauth, pass);
cabbaa
 		info->need_ownerauth = false;
cabbaa
 	}
cabbaa
-	rc = Esys_TR_SetAuth(ctx, hierarchy, &info->ownerauth);
cabbaa
+	rc = _gnutls_tss2_Esys_TR_SetAuth(ctx, hierarchy, &info->ownerauth);
cabbaa
 	if (rc) {
cabbaa
 		_gnutls_debug_log("tpm2: Esys_TR_SetAuth failed: 0x%x\n", rc);
cabbaa
 		return gnutls_assert_val(GNUTLS_E_TPM_ERROR);
cabbaa
@@ -329,7 +493,7 @@ static int init_tpm2_primary(struct tpm2_info_st *info,
cabbaa
 	if (!primary_template) {
cabbaa
 		return gnutls_assert_val(GNUTLS_E_TPM_ERROR);
cabbaa
 	}
cabbaa
-	rc = Esys_CreatePrimary(ctx, hierarchy,
cabbaa
+	rc = _gnutls_tss2_Esys_CreatePrimary(ctx, hierarchy,
cabbaa
 				ESYS_TR_PASSWORD, ESYS_TR_NONE, ESYS_TR_NONE,
cabbaa
 				&primary_sensitive,
cabbaa
 				primary_template,
cabbaa
@@ -359,14 +523,14 @@ static int init_tpm2_key(ESYS_CONTEXT **ctx, ESYS_TR *key_handle,
cabbaa
 
cabbaa
 	_gnutls_debug_log("tpm2: establishing connection with TPM\n");
cabbaa
 
cabbaa
-	rc = Esys_Initialize(ctx, tcti_ctx, NULL);
cabbaa
+	rc = _gnutls_tss2_Esys_Initialize(ctx, tcti_ctx, NULL);
cabbaa
 	if (rc) {
cabbaa
 		gnutls_assert();
cabbaa
 		_gnutls_debug_log("tpm2: Esys_Initialize failed: 0x%x\n", rc);
cabbaa
 		goto error;
cabbaa
 	}
cabbaa
 
cabbaa
-	rc = Esys_Startup(*ctx, TPM2_SU_CLEAR);
cabbaa
+	rc = _gnutls_tss2_Esys_Startup(*ctx, TPM2_SU_CLEAR);
cabbaa
 	if (rc == TPM2_RC_INITIALIZE) {
cabbaa
 		_gnutls_debug_log("tpm2: was already started up thus false positive failing in tpm2tss log\n");
cabbaa
 	} else if (rc) {
cabbaa
@@ -381,7 +545,7 @@ static int init_tpm2_key(ESYS_CONTEXT **ctx, ESYS_TR *key_handle,
cabbaa
 			goto error;
cabbaa
 		}
cabbaa
 	} else {
cabbaa
-		rc = Esys_TR_FromTPMPublic(*ctx, info->parent,
cabbaa
+		rc = _gnutls_tss2_Esys_TR_FromTPMPublic(*ctx, info->parent,
cabbaa
 					   ESYS_TR_NONE,
cabbaa
 					   ESYS_TR_NONE,
cabbaa
 					   ESYS_TR_NONE,
cabbaa
@@ -399,7 +563,7 @@ static int init_tpm2_key(ESYS_CONTEXT **ctx, ESYS_TR *key_handle,
cabbaa
 		if (!info->did_ownerauth && !info->ownerauth.size) {
cabbaa
 			TPM2B_PUBLIC *pub = NULL;
cabbaa
 
cabbaa
-			rc = Esys_ReadPublic(*ctx, parent_handle,
cabbaa
+			rc = _gnutls_tss2_Esys_ReadPublic(*ctx, parent_handle,
cabbaa
 					     ESYS_TR_NONE,
cabbaa
 					     ESYS_TR_NONE,
cabbaa
 					     ESYS_TR_NONE,
cabbaa
@@ -408,7 +572,7 @@ static int init_tpm2_key(ESYS_CONTEXT **ctx, ESYS_TR *key_handle,
cabbaa
 			    !(pub->publicArea.objectAttributes & TPMA_OBJECT_NODA)) {
cabbaa
 				info->need_ownerauth = true;
cabbaa
 			}
cabbaa
-			Esys_Free(pub);
cabbaa
+			_gnutls_tss2_Esys_Free(pub);
cabbaa
 		}
cabbaa
 	reauth:
cabbaa
 		if (info->need_ownerauth) {
cabbaa
@@ -420,7 +584,7 @@ static int init_tpm2_key(ESYS_CONTEXT **ctx, ESYS_TR *key_handle,
cabbaa
 			install_tpm_passphrase(&info->ownerauth, pass);
cabbaa
 			info->need_ownerauth = false;
cabbaa
 		}
cabbaa
-		rc = Esys_TR_SetAuth(*ctx, parent_handle, &info->ownerauth);
cabbaa
+		rc = _gnutls_tss2_Esys_TR_SetAuth(*ctx, parent_handle, &info->ownerauth);
cabbaa
 		if (rc) {
cabbaa
 			gnutls_assert();
cabbaa
 			_gnutls_debug_log("tpm2: Esys_TR_SetAuth failed: 0x%x\n",
cabbaa
@@ -432,7 +596,7 @@ static int init_tpm2_key(ESYS_CONTEXT **ctx, ESYS_TR *key_handle,
cabbaa
 	_gnutls_debug_log("tpm2: loading TPM2 key blob, parent handle 0x%x\n",
cabbaa
 			  parent_handle);
cabbaa
 
cabbaa
-	rc = Esys_Load(*ctx, parent_handle,
cabbaa
+	rc = _gnutls_tss2_Esys_Load(*ctx, parent_handle,
cabbaa
 		       ESYS_TR_PASSWORD, ESYS_TR_NONE, ESYS_TR_NONE,
cabbaa
 		       &info->priv, &info->pub,
cabbaa
 		       key_handle);
cabbaa
@@ -450,7 +614,7 @@ static int init_tpm2_key(ESYS_CONTEXT **ctx, ESYS_TR *key_handle,
cabbaa
 	info->did_ownerauth = true;
cabbaa
 
cabbaa
 	if (parent_is_generated(info->parent)) {
cabbaa
-		rc = Esys_FlushContext(*ctx, parent_handle);
cabbaa
+		rc = _gnutls_tss2_Esys_FlushContext(*ctx, parent_handle);
cabbaa
 		if (rc) {
cabbaa
 			_gnutls_debug_log("tpm2: Esys_FlushContext for generated primary failed: 0x%x\n",
cabbaa
 					  rc);
cabbaa
@@ -461,14 +625,14 @@ static int init_tpm2_key(ESYS_CONTEXT **ctx, ESYS_TR *key_handle,
cabbaa
 	return 0;
cabbaa
  error:
cabbaa
 	if (parent_is_generated(info->parent) && parent_handle != ESYS_TR_NONE) {
cabbaa
-		Esys_FlushContext(*ctx, parent_handle);
cabbaa
+		_gnutls_tss2_Esys_FlushContext(*ctx, parent_handle);
cabbaa
 	}
cabbaa
 	if (*key_handle != ESYS_TR_NONE) {
cabbaa
-		Esys_FlushContext(*ctx, *key_handle);
cabbaa
+		_gnutls_tss2_Esys_FlushContext(*ctx, *key_handle);
cabbaa
 	}
cabbaa
 	*key_handle = ESYS_TR_NONE;
cabbaa
 
cabbaa
-	Esys_Finalize(ctx);
cabbaa
+	_gnutls_tss2_Esys_Finalize(ctx);
cabbaa
 	return GNUTLS_E_TPM_ERROR;
cabbaa
 }
cabbaa
 
cabbaa
@@ -488,7 +652,7 @@ auth_tpm2_key(struct tpm2_info_st *info, ESYS_CONTEXT *ctx, ESYS_TR key_handle)
cabbaa
 		info->need_userauth = false;
cabbaa
 	}
cabbaa
 
cabbaa
-	rc = Esys_TR_SetAuth(ctx, key_handle, &info->userauth);
cabbaa
+	rc = _gnutls_tss2_Esys_TR_SetAuth(ctx, key_handle, &info->userauth);
cabbaa
 	if (rc) {
cabbaa
 		_gnutls_debug_log("tpm2: Esys_TR_SetAuth failed: 0x%x\n", rc);
cabbaa
 		return gnutls_assert_val(GNUTLS_E_TPM_ERROR);
cabbaa
@@ -574,7 +738,7 @@ int tpm2_rsa_sign_hash_fn(gnutls_privkey_t key, gnutls_sign_algorithm_t algo,
cabbaa
 		goto out;
cabbaa
 	}
cabbaa
 
cabbaa
-	rc = Esys_RSA_Decrypt(ectx, key_handle,
cabbaa
+	rc = _gnutls_tss2_Esys_RSA_Decrypt(ectx, key_handle,
cabbaa
 			      ESYS_TR_PASSWORD, ESYS_TR_NONE, ESYS_TR_NONE,
cabbaa
 			      &digest, &in_scheme, &label, &tsig);
cabbaa
 	if (rc_is_key_auth_failed(rc)) {
cabbaa
@@ -591,14 +755,14 @@ int tpm2_rsa_sign_hash_fn(gnutls_privkey_t key, gnutls_sign_algorithm_t algo,
cabbaa
 
cabbaa
 	ret = _gnutls_set_datum(sig, tsig->buffer, tsig->size);
cabbaa
  out:
cabbaa
-	Esys_Free(tsig);
cabbaa
+	_gnutls_tss2_Esys_Free(tsig);
cabbaa
 
cabbaa
 	if (key_handle != ESYS_TR_NONE) {
cabbaa
-		Esys_FlushContext(ectx, key_handle);
cabbaa
+		_gnutls_tss2_Esys_FlushContext(ectx, key_handle);
cabbaa
 	}
cabbaa
 
cabbaa
 	if (ectx) {
cabbaa
-		Esys_Finalize(&ectx);
cabbaa
+		_gnutls_tss2_Esys_Finalize(&ectx);
cabbaa
 	}
cabbaa
 
cabbaa
 	return ret;
cabbaa
@@ -661,7 +825,7 @@ int tpm2_ec_sign_hash_fn(gnutls_privkey_t key, gnutls_sign_algorithm_t algo,
cabbaa
 		goto out;
cabbaa
 	}
cabbaa
 
cabbaa
-	rc = Esys_Sign(ectx, key_handle,
cabbaa
+	rc = _gnutls_tss2_Esys_Sign(ectx, key_handle,
cabbaa
 		       ESYS_TR_PASSWORD, ESYS_TR_NONE, ESYS_TR_NONE,
cabbaa
 		       &digest, &in_scheme, &validation,
cabbaa
 		       &tsig);
cabbaa
@@ -682,31 +846,23 @@ int tpm2_ec_sign_hash_fn(gnutls_privkey_t key, gnutls_sign_algorithm_t algo,
cabbaa
 
cabbaa
 	ret = gnutls_encode_rs_value(sig, &sig_r, &sig_s);
cabbaa
  out:
cabbaa
-	Esys_Free(tsig);
cabbaa
+	_gnutls_tss2_Esys_Free(tsig);
cabbaa
 
cabbaa
 	if (key_handle != ESYS_TR_NONE) {
cabbaa
-		Esys_FlushContext(ectx, key_handle);
cabbaa
+		_gnutls_tss2_Esys_FlushContext(ectx, key_handle);
cabbaa
 	}
cabbaa
 
cabbaa
 	if (ectx) {
cabbaa
-		Esys_Finalize(&ectx);
cabbaa
+		_gnutls_tss2_Esys_Finalize(&ectx);
cabbaa
 	}
cabbaa
 
cabbaa
 	return ret;
cabbaa
 }
cabbaa
 
cabbaa
-GNUTLS_ONCE(tcti_once);
cabbaa
-
cabbaa
-void
cabbaa
-tpm2_tcti_deinit(void)
cabbaa
-{
cabbaa
-	if (tcti_ctx) {
cabbaa
-		Tss2_TctiLdr_Finalize(&tcti_ctx);
cabbaa
-	}
cabbaa
-}
cabbaa
+GNUTLS_ONCE(tpm2_esys_once);
cabbaa
 
cabbaa
 static void
cabbaa
-tcti_once_init(void)
cabbaa
+tpm2_esys_once_init(void)
cabbaa
 {
cabbaa
 	const char *tcti;
cabbaa
 	const char * const tcti_vars[] = {
cabbaa
@@ -718,6 +874,11 @@ tcti_once_init(void)
cabbaa
 	size_t i;
cabbaa
 	TSS2_RC rc;
cabbaa
 
cabbaa
+	if (init_tss2_funcs() < 0) {
cabbaa
+		_gnutls_debug_log("tpm2: unable to initialize TSS2 functions\n");
cabbaa
+		return;
cabbaa
+	}
cabbaa
+
cabbaa
 	for (i = 0; i < sizeof(tcti_vars) / sizeof(tcti_vars[0]); i++) {
cabbaa
 		tcti = secure_getenv(tcti_vars[i]);
cabbaa
 		if (tcti && *tcti != '\0') {
cabbaa
@@ -727,7 +888,7 @@ tcti_once_init(void)
cabbaa
 		}
cabbaa
 	}
cabbaa
 	if (tcti && *tcti != '\0') {
cabbaa
-		rc = Tss2_TctiLdr_Initialize(tcti, &tcti_ctx);
cabbaa
+		rc = _gnutls_tss2_Tss2_TctiLdr_Initialize(tcti, &tcti_ctx);
cabbaa
 		if (rc) {
cabbaa
 			_gnutls_debug_log("tpm2: TSS2_TctiLdr_Initialize failed: 0x%x\n",
cabbaa
 					  rc);
cabbaa
@@ -735,13 +896,35 @@ tcti_once_init(void)
cabbaa
 	}
cabbaa
 }
cabbaa
 
cabbaa
+/* called by the global destructor through _gnutls_tpm2_deinit */
cabbaa
+void
cabbaa
+tpm2_esys_deinit(void)
cabbaa
+{
cabbaa
+	if (tcti_ctx) {
cabbaa
+		_gnutls_tss2_Tss2_TctiLdr_Finalize(&tcti_ctx);
cabbaa
+		tcti_ctx = NULL;
cabbaa
+	}
cabbaa
+	if (_gnutls_tss2_esys_dlhandle) {
cabbaa
+		dlclose(_gnutls_tss2_esys_dlhandle);
cabbaa
+		_gnutls_tss2_esys_dlhandle = NULL;
cabbaa
+	}
cabbaa
+	if (_gnutls_tss2_mu_dlhandle) {
cabbaa
+		dlclose(_gnutls_tss2_mu_dlhandle);
cabbaa
+		_gnutls_tss2_mu_dlhandle = NULL;
cabbaa
+	}
cabbaa
+	if (_gnutls_tss2_tctildr_dlhandle) {
cabbaa
+		dlclose(_gnutls_tss2_tctildr_dlhandle);
cabbaa
+		_gnutls_tss2_tctildr_dlhandle = NULL;
cabbaa
+	}
cabbaa
+}
cabbaa
+
cabbaa
 int install_tpm2_key(struct tpm2_info_st *info, gnutls_privkey_t pkey,
cabbaa
 		     unsigned int parent, bool emptyauth,
cabbaa
 		     gnutls_datum_t *privdata, gnutls_datum_t *pubdata)
cabbaa
 {
cabbaa
 	TSS2_RC rc;
cabbaa
 
cabbaa
-	(void)gnutls_once(&tcti_once, tcti_once_init);
cabbaa
+	(void)gnutls_once(&tpm2_esys_once, tpm2_esys_once_init);
cabbaa
 
cabbaa
 	if (!tcti_ctx) {
cabbaa
 		return gnutls_assert_val(GNUTLS_E_TPM_ERROR);
cabbaa
@@ -757,16 +940,16 @@ int install_tpm2_key(struct tpm2_info_st *info, gnutls_privkey_t pkey,
cabbaa
 
cabbaa
 	info->parent = parent;
cabbaa
 
cabbaa
-	rc = Tss2_MU_TPM2B_PRIVATE_Unmarshal(privdata->data, privdata->size, NULL,
cabbaa
-					     &info->priv);
cabbaa
+	rc = _gnutls_tss2_Tss2_MU_TPM2B_PRIVATE_Unmarshal(privdata->data, privdata->size, NULL,
cabbaa
+							     &info->priv);
cabbaa
 	if (rc) {
cabbaa
 		_gnutls_debug_log("tpm2: failed to import private key data: 0x%x\n",
cabbaa
 				  rc);
cabbaa
 		return gnutls_assert_val(GNUTLS_E_TPM_ERROR);
cabbaa
 	}
cabbaa
 
cabbaa
-	rc = Tss2_MU_TPM2B_PUBLIC_Unmarshal(pubdata->data, pubdata->size, NULL,
cabbaa
-					    &info->pub);
cabbaa
+	rc = _gnutls_tss2_Tss2_MU_TPM2B_PUBLIC_Unmarshal(pubdata->data, pubdata->size, NULL,
cabbaa
+							    &info->pub);
cabbaa
 	if (rc) {
cabbaa
 		_gnutls_debug_log("tpm2: failed to import public key data: 0x%x\n",
cabbaa
 				  rc);
cabbaa
diff --git a/tests/Makefile.am b/tests/Makefile.am
cabbaa
index 529f1cc077..64ce470a02 100644
cabbaa
--- a/tests/Makefile.am
cabbaa
+++ b/tests/Makefile.am
cabbaa
@@ -515,7 +515,8 @@ dist_check_SCRIPTS += fastopen.sh pkgconfig.sh starttls.sh starttls-ftp.sh start
cabbaa
 	psktool.sh ocsp-tests/ocsp-load-chain.sh gnutls-cli-save-data.sh gnutls-cli-debug.sh \
cabbaa
 	sni-resume.sh ocsp-tests/ocsptool.sh cert-reencoding.sh pkcs7-cat.sh long-crl.sh \
cabbaa
 	serv-udp.sh logfile-option.sh gnutls-cli-resume.sh profile-tests.sh \
cabbaa
-	server-weak-keys.sh ocsp-tests/ocsp-signer-verify.sh cfg-test.sh
cabbaa
+	server-weak-keys.sh ocsp-tests/ocsp-signer-verify.sh cfg-test.sh \
cabbaa
+	sanity-lib.sh
cabbaa
 
cabbaa
 if !DISABLE_SYSTEM_CONFIG
cabbaa
 dist_check_SCRIPTS += system-override-sig.sh system-override-hash.sh \
cabbaa
diff --git a/tests/sanity-lib.sh b/tests/sanity-lib.sh
cabbaa
new file mode 100644
cabbaa
index 0000000000..1e3612781b
cabbaa
--- /dev/null
cabbaa
+++ b/tests/sanity-lib.sh
cabbaa
@@ -0,0 +1,40 @@
cabbaa
+#!/bin/sh
cabbaa
+
cabbaa
+# Copyright (C) 2022 Red Hat, Inc.
cabbaa
+#
cabbaa
+# Author: Daiki Ueno
cabbaa
+#
cabbaa
+# This file is part of GnuTLS.
cabbaa
+#
cabbaa
+# GnuTLS is free software; you can redistribute it and/or modify it
cabbaa
+# under the terms of the GNU General Public License as published by the
cabbaa
+# Free Software Foundation; either version 3 of the License, or (at
cabbaa
+# your option) any later version.
cabbaa
+#
cabbaa
+# GnuTLS is distributed in the hope that it will be useful, but
cabbaa
+# WITHOUT ANY WARRANTY; without even the implied warranty of
cabbaa
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
cabbaa
+# General Public License for more details.
cabbaa
+#
cabbaa
+# You should have received a copy of the GNU Lesser General Public License
cabbaa
+# along with this program.  If not, see <https://www.gnu.org/licenses/>
cabbaa
+
cabbaa
+: ${top_builddir=..}
cabbaa
+: ${CLI_DEBUG=../src/gnutls-cli-debug${EXEEXT}}
cabbaa
+: ${LDD=ldd}
cabbaa
+: ${LIBTOOL=libtool}
cabbaa
+
cabbaa
+if ! test -x "${CLI_DEBUG}"; then
cabbaa
+	exit 77
cabbaa
+fi
cabbaa
+
cabbaa
+# ldd.sh doesn't check recursive dependencies
cabbaa
+${LDD} --version >/dev/null || exit 77
cabbaa
+
cabbaa
+# We use gnutls-cli-debug, as it has the fewest dependencies among our
cabbaa
+# commands (e.g., gnutls-cli pulls in OpenSSL through libunbound).
cabbaa
+if ${LIBTOOL} --mode=execute ${LDD} ${CLI_DEBUG} | \
cabbaa
+    grep '^[[:space:]]*\(libcrypto\.\|libssl\.\|libgcrypt\.\)'; then
cabbaa
+    echo "gnutls-cli-debug links to other crypto library"
cabbaa
+    exit 1
cabbaa
+fi
cabbaa
diff --git a/tests/tpm2.sh b/tests/tpm2.sh
cabbaa
index 854986c552..6f8e44c64b 100755
cabbaa
--- a/tests/tpm2.sh
cabbaa
+++ b/tests/tpm2.sh
cabbaa
@@ -21,8 +21,6 @@
cabbaa
 # along with GnuTLS; if not, write to the Free Software Foundation,
cabbaa
 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
cabbaa
 
cabbaa
-set +e
cabbaa
-
cabbaa
 : ${srcdir=.}
cabbaa
 : ${CERTTOOL=../src/certtool${EXEEXT}}
cabbaa
 KEYPEMFILE=tpmkey.$$.key.pem
cabbaa
@@ -192,6 +190,10 @@ run_tests()
cabbaa
 
cabbaa
 	echo " - Generating ${KEYPEMFILE}"
cabbaa
 	tpm2tss-genkey -a ${kalg} -o ${OPASS} ${KEYPEMFILE}
cabbaa
+	if [ $? -ne 0 ]; then
cabbaa
+		echo "unable to generate key"
cabbaa
+		return 1
cabbaa
+	fi
cabbaa
 	cat ${KEYPEMFILE}
cabbaa
 
cabbaa
 	echo " - Generating certificate based on key"
cabbaa
@@ -200,6 +202,10 @@ run_tests()
cabbaa
 	"${CERTTOOL}" --generate-self-signed -d 3 \
cabbaa
 		--load-privkey "${KEYPEMFILE}" \
cabbaa
 		--template "${srcdir}/cert-tests/templates/template-test.tmpl"
cabbaa
+	if [ $? -ne 0 ]; then
cabbaa
+		echo "unable to generate certificate"
cabbaa
+		return 1
cabbaa
+	fi
cabbaa
 
cabbaa
 	if test "${kalg}" = "rsa";then
cabbaa
 		echo " - Generating RSA-PSS certificate based on key"
cabbaa
@@ -207,6 +213,10 @@ run_tests()
cabbaa
 			--load-privkey "${KEYPEMFILE}" \
cabbaa
 			--sign-params rsa-pss \
cabbaa
 			--template "${srcdir}/cert-tests/templates/template-test.tmpl"
cabbaa
+		if [ $? -ne 0 ]; then
cabbaa
+			echo "unable to generate certificate"
cabbaa
+			return 1
cabbaa
+		fi
cabbaa
 	fi
cabbaa
 
cabbaa
 	stop_swtpm
cabbaa
-- 
cabbaa
2.34.1
cabbaa