From 1bbdf2c933ff2216b0491d236d63d0c15886bb54 Mon Sep 17 00:00:00 2001 From: Juergen Repp Date: Thu, 16 Jan 2025 19:44:51 +0100 Subject: [PATCH] TCTI: Add null tcti. A tcti module which can only used for tcti initialization is added. Signed-off-by: Juergen Repp --- Makefile.am | 19 ++++ configure.ac | 8 +- include/tss2/tss2_tcti_null.h | 24 +++++ lib/tss2-tcti-null.def | 4 + lib/tss2-tcti-null.map | 7 ++ lib/tss2-tcti-null.pc.in | 12 +++ src/tss2-tcti/tcti-null.c | 173 ++++++++++++++++++++++++++++++++++ src/tss2-tcti/tcti-null.h | 23 +++++ 8 files changed, 269 insertions(+), 1 deletion(-) create mode 100644 include/tss2/tss2_tcti_null.h create mode 100644 lib/tss2-tcti-null.def create mode 100644 lib/tss2-tcti-null.map create mode 100644 lib/tss2-tcti-null.pc.in create mode 100644 src/tss2-tcti/tcti-null.c create mode 100644 src/tss2-tcti/tcti-null.h diff --git a/Makefile.am b/Makefile.am index 1ea007a35..fff4f9080 100644 --- a/Makefile.am +++ b/Makefile.am @@ -383,6 +383,25 @@ src_tss2_tcti_libtss2_tcti_pcap_la_SOURCES = \ endif # ENABLE_TCTI_PCAP EXTRA_DIST += lib/tss2-tcti-pcap.map +# tcti null library +if ENABLE_TCTI_NULL +libtss2_tcti_null = src/tss2-tcti/libtss2-tcti-null.la +tss2_HEADERS += $(srcdir)/include/tss2/tss2_tcti_null.h +lib_LTLIBRARIES += $(libtss2_tcti_null) +pkgconfig_DATA += lib/tss2-tcti-null.pc + +if HAVE_LD_VERSION_SCRIPT +src_tss2_tcti_libtss2_tcti_null_la_LDFLAGS = -Wl,--version-script=$(srcdir)/lib/tss2-tcti-null.map +endif # HAVE_LD_VERSION_SCRIPT +src_tss2_tcti_libtss2_tcti_null_la_LIBADD = $(libtss2_tctildr) $(libtss2_mu) $(libutil) $(libutilio) +src_tss2_tcti_libtss2_tcti_null_la_SOURCES = \ + src/tss2-tcti/tcti-common.c \ + src/tss2-tcti/tcti-null.c \ + src/tss2-tcti/tcti-null.h +endif # ENABLE_TCTI_NULL +EXTRA_DIST += lib/tss2-tcti-null.map + + # tcti libtpms library if ENABLE_TCTI_LIBTPMS libtss2_tcti_libtpms = src/tss2-tcti/libtss2-tcti-libtpms.la diff --git a/configure.ac b/configure.ac index e2d579b8c..6cc3efc84 100644 --- a/configure.ac +++ b/configure.ac @@ -15,7 +15,7 @@ m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])]) #Backward compatible setti AC_CONFIG_HEADERS([config.h]) -AC_CONFIG_FILES([Makefile Doxyfile lib/tss2-sys.pc lib/tss2-esys.pc lib/tss2-mu.pc lib/tss2-tcti-device.pc lib/tss2-tcti-mssim.pc lib/tss2-tcti-swtpm.pc lib/tss2-tcti-pcap.pc lib/tss2-tcti-libtpms.pc lib/tss2-rc.pc lib/tss2-tctildr.pc lib/tss2-fapi.pc lib/tss2-tcti-cmd.pc lib/tss2-policy.pc lib/tss2-tcti-spi-helper.pc lib/tss2-tcti-spi-ltt2go.pc lib/tss2-tcti-spidev.pc lib/tss2-tcti-spi-ftdi.pc lib/tss2-tcti-i2c-helper.pc lib/tss2-tcti-i2c-ftdi.pc]) +AC_CONFIG_FILES([Makefile Doxyfile lib/tss2-sys.pc lib/tss2-esys.pc lib/tss2-mu.pc lib/tss2-tcti-device.pc lib/tss2-tcti-mssim.pc lib/tss2-tcti-swtpm.pc lib/tss2-tcti-pcap.pc lib/tss2-tcti-null.pc lib/tss2-tcti-libtpms.pc lib/tss2-rc.pc lib/tss2-tctildr.pc lib/tss2-fapi.pc lib/tss2-tcti-cmd.pc lib/tss2-policy.pc lib/tss2-tcti-spi-helper.pc lib/tss2-tcti-spi-ltt2go.pc lib/tss2-tcti-spidev.pc lib/tss2-tcti-spi-ftdi.pc lib/tss2-tcti-i2c-helper.pc lib/tss2-tcti-i2c-ftdi.pc]) # propagate configure arguments to distcheck AC_SUBST([DISTCHECK_CONFIGURE_FLAGS],[$ac_configure_args]) @@ -278,6 +278,12 @@ AC_ARG_ENABLE([tcti-pcap], [enable_tcti_pcap=yes]) AM_CONDITIONAL([ENABLE_TCTI_PCAP], [test "x$enable_tcti_pcap" != xno]) +AC_ARG_ENABLE([tcti-null], + [AS_HELP_STRING([--disable-tcti-null], + [don't build the tcti-null module])],, + [enable_tcti_null=yes]) +AM_CONDITIONAL([ENABLE_TCTI_NULL], [test "x$enable_tcti_null" != xno]) + AC_ARG_ENABLE([tcti-libtpms], [AS_HELP_STRING([--disable-tcti-libtpms], [don't build the tcti-libtpms module])], diff --git a/include/tss2/tss2_tcti_null.h b/include/tss2/tss2_tcti_null.h new file mode 100644 index 000000000..45a603cd0 --- /dev/null +++ b/include/tss2/tss2_tcti_null.h @@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: BSD-2-Clause */ +/* + * Copyright (c) 2020 Infineon Technologies AG + * All rights reserved. + */ +#ifndef TSS2_TCTI_NULL_H +#define TSS2_TCTI_NULL_H + +#include "tss2_tcti.h" + +#ifdef __cplusplus +extern "C" { +#endif + +TSS2_RC Tss2_Tcti_Null_Init ( + TSS2_TCTI_CONTEXT *tctiContext, + size_t *size, + const char *conf); + +#ifdef __cplusplus +} +#endif + +#endif /* TSS2_TCTI_NULL_H */ diff --git a/lib/tss2-tcti-null.def b/lib/tss2-tcti-null.def new file mode 100644 index 000000000..5798049ac --- /dev/null +++ b/lib/tss2-tcti-null.def @@ -0,0 +1,4 @@ +LIBRARY tss2-tcti-null +EXPORTS + Tss2_Tcti_Info + Tss2_Tcti_Null_Init diff --git a/lib/tss2-tcti-null.map b/lib/tss2-tcti-null.map new file mode 100644 index 000000000..caeb4c033 --- /dev/null +++ b/lib/tss2-tcti-null.map @@ -0,0 +1,7 @@ +{ + global: + Tss2_Tcti_Info; + Tss2_Tcti_Null_Init; + local: + *; +}; diff --git a/lib/tss2-tcti-null.pc.in b/lib/tss2-tcti-null.pc.in new file mode 100644 index 000000000..05d093376 --- /dev/null +++ b/lib/tss2-tcti-null.pc.in @@ -0,0 +1,12 @@ +prefix=@prefix@ +exec_prefix=@exec_prefix@ +libdir=@libdir@ +includedir=@includedir@ + +Name: tss2-tcti-null +Description: TCTI library for debugging at the TCTI interface. +URL: https://github.com/tpm2-software/tpm2-tss +Version: @VERSION@ +Requires.private: tss2-tctildr +Cflags: -I${includedir} +Libs: -ltss2-tcti-null -L${libdir} diff --git a/src/tss2-tcti/tcti-null.c b/src/tss2-tcti/tcti-null.c new file mode 100644 index 000000000..7066f82da --- /dev/null +++ b/src/tss2-tcti/tcti-null.c @@ -0,0 +1,173 @@ +/* SPDX-License-Identifier: BSD-2-Clause */ +/* + * Copyright (c) 2025 Juergen Repp + * All rights reserved. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" // IWYU pragma: keep +#endif + +#include // for PRIxPTR, uintptr_t, uint8_t, int32_t +#include // for NULL, size_t, memset + +#include "tcti-common.h" // for TSS2_TCTI_COMMON_CONTEXT, TCTI_STATE_... +#include "tss2_common.h" // for TSS2_RC_SUCCESS, TSS2_RC, TSS2_TCTI_R... +#include "tss2_tcti.h" // for TSS2_TCTI_CONTEXT, TSS2_TCTI_INFO +#include "tss2_tpm2_types.h" // for TPM2_RC_SUCCESS +#include "util/aux_util.h" // for UNUSED + +#define LOGMODULE tcti +#include "tcti-null.h" +#include "tss2_tctildr.h" // for Tss2_TctiLdr_Finalize, Tss2_TctiLdr_I... +#include "util/log.h" // for LOG_WARNING, LOG_ERROR, LOGBLOB_DEBUG + +/* + * This function wraps the "up-cast" of the opaque TCTI context type to the + * type for the null TCTI context. The only safeguard we have to ensure this + * operation is possible is the magic number in the null TCTI context. + * If passed a NULL context, or the magic number check fails, this function + * will return NULL. + */ +TSS2_TCTI_NULL_CONTEXT* +tcti_null_context_cast (TSS2_TCTI_CONTEXT *tcti_ctx) +{ + if (tcti_ctx != NULL && TSS2_TCTI_MAGIC (tcti_ctx) == TCTI_NULL_MAGIC) { + return (TSS2_TCTI_NULL_CONTEXT*)tcti_ctx; + } + return NULL; +} + +/* + * This function down-casts the null TCTI context to the common context + * defined in the tcti-common module. + */ +TSS2_TCTI_COMMON_CONTEXT* +tcti_null_down_cast (TSS2_TCTI_NULL_CONTEXT *tcti_null) +{ + if (tcti_null == NULL) { + return NULL; + } + return &tcti_null->common; +} + +TSS2_RC +tcti_null_transmit ( + TSS2_TCTI_CONTEXT *tcti_ctx, + size_t size, + const uint8_t *cmd_buf) +{ + UNUSED(tcti_ctx); + UNUSED(size); + UNUSED(cmd_buf); + return TSS2_TCTI_RC_NOT_IMPLEMENTED; +} + +TSS2_RC +tcti_null_receive ( + TSS2_TCTI_CONTEXT *tctiContext, + size_t *response_size, + unsigned char *response_buffer, + int32_t timeout) +{ + UNUSED(tctiContext); + UNUSED(response_size); + UNUSED(response_buffer); + UNUSED(timeout); + return TSS2_TCTI_RC_NOT_IMPLEMENTED; +} + +TSS2_RC +tcti_null_cancel ( + TSS2_TCTI_CONTEXT *tctiContext) +{ + UNUSED(tctiContext); + return TSS2_TCTI_RC_NOT_IMPLEMENTED; +} + +TSS2_RC +tcti_null_set_locality ( + TSS2_TCTI_CONTEXT *tctiContext, + uint8_t locality) +{ + UNUSED(tctiContext); + UNUSED(locality); + return TSS2_TCTI_RC_NOT_IMPLEMENTED; +} + +TSS2_RC +tcti_null_get_poll_handles ( + TSS2_TCTI_CONTEXT *tctiContext, + TSS2_TCTI_POLL_HANDLE *handles, + size_t *num_handles) +{ + UNUSED(tctiContext); + UNUSED(handles); + UNUSED(num_handles); + return TSS2_TCTI_RC_NOT_IMPLEMENTED; +} + +void +tcti_null_finalize ( + TSS2_TCTI_CONTEXT *tctiContext) +{ + TSS2_TCTI_NULL_CONTEXT *tcti_null = tcti_null_context_cast (tctiContext); + TSS2_TCTI_COMMON_CONTEXT *tcti_common = tcti_null_down_cast (tcti_null); + if (tcti_null == NULL) { + return; + } + + tcti_common->state = TCTI_STATE_FINAL; +} + +/* + * This is an implementation of the standard TCTI initialization function for + * this module. + */ +TSS2_RC +Tss2_Tcti_Null_Init ( + TSS2_TCTI_CONTEXT *tctiContext, + size_t *size, + const char *conf) +{ + UNUSED(conf); + TSS2_TCTI_NULL_CONTEXT *tcti_null = (TSS2_TCTI_NULL_CONTEXT*) tctiContext; + TSS2_TCTI_COMMON_CONTEXT *tcti_common = tcti_null_down_cast (tcti_null); + + if (tctiContext == NULL && size == NULL) { + return TSS2_TCTI_RC_BAD_VALUE; + } else if (tctiContext == NULL) { + *size = sizeof (TSS2_TCTI_NULL_CONTEXT); + return TSS2_RC_SUCCESS; + } + + TSS2_TCTI_MAGIC (tcti_common) = TCTI_NULL_MAGIC; + TSS2_TCTI_VERSION (tcti_common) = TCTI_VERSION; + TSS2_TCTI_TRANSMIT (tcti_common) = tcti_null_transmit; + TSS2_TCTI_RECEIVE (tcti_common) = tcti_null_receive; + TSS2_TCTI_FINALIZE (tcti_common) = tcti_null_finalize; + TSS2_TCTI_CANCEL (tcti_common) = tcti_null_cancel; + TSS2_TCTI_GET_POLL_HANDLES (tcti_common) = tcti_null_get_poll_handles; + TSS2_TCTI_SET_LOCALITY (tcti_common) = tcti_null_set_locality; + TSS2_TCTI_MAKE_STICKY (tcti_common) = tcti_make_sticky_not_implemented; + tcti_common->state = TCTI_STATE_TRANSMIT; + tcti_common->locality = 3; + memset (&tcti_common->header, 0, sizeof (tcti_common->header)); + + return TSS2_RC_SUCCESS; +} + +/* public info structure */ +const TSS2_TCTI_INFO tss2_tcti_info = { + .version = TCTI_VERSION, + .name = "tcti-null", + .description = "TCTI module which only provides initialization.", + .config_help = "", + .init = Tss2_Tcti_Null_Init, +}; + +const TSS2_TCTI_INFO* +Tss2_Tcti_Info (void) +{ + return &tss2_tcti_info; +} diff --git a/src/tss2-tcti/tcti-null.h b/src/tss2-tcti/tcti-null.h new file mode 100644 index 000000000..00934e126 --- /dev/null +++ b/src/tss2-tcti/tcti-null.h @@ -0,0 +1,23 @@ +/* SPDX-License-Identifier: BSD-2-Clause */ +/* + * Copyright (c) 2025 Juergen Repp + * All rights reserved. + */ + +#ifndef TCTI_NULL_H +#define TCTI_NULL_H + +#include "tcti-common.h" +#include "tss2_tcti.h" + +#define TCTI_NULL_MAGIC 0x9af45c5d7d9d0d3fULL + +typedef struct { + const char *child_tcti; +} tcti_null_conf_t; + +typedef struct { + TSS2_TCTI_COMMON_CONTEXT common; +} TSS2_TCTI_NULL_CONTEXT; + +#endif /* TCTI_NULL_H */