From 8177ba8c1d1130e546990f8d7f5baba1e0637c2e Mon Sep 17 00:00:00 2001 From: David Garske Date: Fri, 13 Dec 2024 14:52:38 -0800 Subject: [PATCH] Create separate tool for performing the TPM2_Clear (don't use args to wrap_test). --- .gitignore | 1 + examples/management/flush.c | 2 +- examples/management/include.am | 15 +++- examples/management/{flush.h => management.h} | 3 +- examples/management/tpmclear.c | 80 +++++++++++++++++++ examples/wrap/wrap_test.c | 22 +---- examples/wrap/wrap_test.h | 1 - 7 files changed, 97 insertions(+), 27 deletions(-) rename examples/management/{flush.h => management.h} (93%) create mode 100644 examples/management/tpmclear.c diff --git a/.gitignore b/.gitignore index a6baa24..fb9cf76 100644 --- a/.gitignore +++ b/.gitignore @@ -53,6 +53,7 @@ examples/pcr/policy_sign examples/pcr/reset examples/timestamp/clock_set examples/management/flush +examples/management/tpmclear pkcs7tpmsigned.p7s pkcs7tpmsignedex.p7s examples/tls/tls_server diff --git a/examples/management/flush.c b/examples/management/flush.c index 38cc396..959c8c5 100644 --- a/examples/management/flush.c +++ b/examples/management/flush.c @@ -27,7 +27,7 @@ #include -#include +#include #include #include diff --git a/examples/management/include.am b/examples/management/include.am index 032337f..e738a89 100644 --- a/examples/management/include.am +++ b/examples/management/include.am @@ -2,16 +2,23 @@ # All paths should be given relative to the root if BUILD_EXAMPLES -noinst_PROGRAMS += examples/management/flush +noinst_PROGRAMS += examples/management/flush \ + examples/management/tpmclear -noinst_HEADERS += examples/management/flush.h +noinst_HEADERS += examples/management/management.h examples_management_flush_SOURCES = examples/management/flush.c examples_management_flush_LDADD = src/libwolftpm.la $(LIB_STATIC_ADD) examples_management_flush_DEPENDENCIES = src/libwolftpm.la + +examples_management_tpmclear_SOURCES = examples/management/tpmclear.c +examples_management_tpmclear_LDADD = src/libwolftpm.la $(LIB_STATIC_ADD) +examples_management_tpmclear_DEPENDENCIES = src/libwolftpm.la endif example_managementdir = $(exampledir)/management -dist_example_management_DATA = examples/management/flush.c +dist_example_management_DATA = examples/management/flush.c \ + examples/management/tpmclear.c -DISTCLEANFILES+= examples/management/.libs/flush +DISTCLEANFILES+= examples/management/.libs/flush \ + examples/management/.libs/tpmclear diff --git a/examples/management/flush.h b/examples/management/management.h similarity index 93% rename from examples/management/flush.h rename to examples/management/management.h index 7d92e3f..044c5f8 100644 --- a/examples/management/flush.h +++ b/examples/management/management.h @@ -1,4 +1,4 @@ -/* flush.h +/* management.h * * Copyright (C) 2006-2024 wolfSSL Inc. * @@ -27,6 +27,7 @@ #endif int TPM2_Flush_Tool(void* userCtx, int argc, char *argv[]); +int TPM2_Clear_Tool(void* userCtx, int argc, char *argv[]); #ifdef __cplusplus } /* extern "C" */ diff --git a/examples/management/tpmclear.c b/examples/management/tpmclear.c new file mode 100644 index 0000000..2aace27 --- /dev/null +++ b/examples/management/tpmclear.c @@ -0,0 +1,80 @@ +/* tpmclear.c + * + * Copyright (C) 2006-2024 wolfSSL Inc. + * + * This file is part of wolfTPM. + * + * wolfTPM is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * wolfTPM is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA + */ + +/* This is a tool for performing a TPM2_Clear call to reset the NV */ + +#ifdef HAVE_CONFIG_H + #include +#endif + +#include +#include +#include +#include + +#include + +#ifndef WOLFTPM2_NO_WRAPPER +int TPM2_Clear_Tool(void* userCtx, int argc, char *argv[]) +{ + int rc = TPM_RC_FAILURE; + WOLFTPM2_DEV dev; + + (void)argc; + (void)argv; + + printf("Preparing to clear TPM\n"); + rc = wolfTPM2_Init(&dev, TPM2_IoCb, userCtx); + if (rc != TPM_RC_SUCCESS) { + printf("wolfTPM2_Init failed 0x%x: %s\n", rc, TPM2_GetRCString(rc)); + return rc; + } + + /* reset all content on TPM and reseed */ + rc = wolfTPM2_Clear(&dev); + if (rc == 0) { + printf("TPM Clear success\n"); + } + + if (rc != 0) { + printf("Failure 0x%x: %s\n", rc, wolfTPM2_GetRCString(rc)); + } + wolfTPM2_Cleanup(&dev); + return rc; +} +#endif /* !WOLFTPM2_NO_WRAPPER */ + +#ifndef NO_MAIN_DRIVER +int main(int argc, char *argv[]) +{ + int rc = NOT_COMPILED_IN; + +#ifndef WOLFTPM2_NO_WRAPPER + rc = TPM2_Clear_Tool(NULL, argc, argv); +#else + printf("Flush tool not compiled in\n"); + (void)argc; + (void)argv; +#endif + + return rc; +} +#endif diff --git a/examples/wrap/wrap_test.c b/examples/wrap/wrap_test.c index 35703cb..52abbd3 100644 --- a/examples/wrap/wrap_test.c +++ b/examples/wrap/wrap_test.c @@ -47,13 +47,6 @@ /* --- BEGIN Wrapper API Tests -- */ /******************************************************************************/ -static int resetTPM = 0; - -void TPM2_Wrapper_SetReset(int reset) -{ - resetTPM = reset; -} - static void usage(void) { printf("Expected Usage:\n"); @@ -209,12 +202,6 @@ int TPM2_Wrapper_TestArgs(void* userCtx, int argc, char *argv[]) printf("Found %d persistent handles\n", rc); } - if (resetTPM) { - /* reset all content on TPM and reseed */ - rc = wolfTPM2_Clear(&dev); - if (rc != 0) return rc; - } - /* unload all transient handles */ rc = wolfTPM2_UnloadHandles_AllTransient(&dev); if (rc != 0) goto exit; @@ -1045,16 +1032,11 @@ int main(int argc, char *argv[]) { int rc = -1; - if (argc > 1) { - #ifndef WOLFTPM2_NO_WRAPPER - TPM2_Wrapper_SetReset(1); - #endif - } - (void)argv; - #ifndef WOLFTPM2_NO_WRAPPER rc = TPM2_Wrapper_TestArgs(NULL, argc, argv); #else + (void)argc; + (void)argv; printf("Wrapper code not compiled in\n"); #endif diff --git a/examples/wrap/wrap_test.h b/examples/wrap/wrap_test.h index d0e83d7..f8145f5 100644 --- a/examples/wrap/wrap_test.h +++ b/examples/wrap/wrap_test.h @@ -26,7 +26,6 @@ extern "C" { #endif -void TPM2_Wrapper_SetReset(int reset); int TPM2_Wrapper_Test(void* userCtx); int TPM2_Wrapper_TestArgs(void* userCtx, int argc, char *argv[]);