Skip to content

Commit

Permalink
Merge pull request #391 from dgarske/tpmclear
Browse files Browse the repository at this point in the history
Create separate tool for performing the TPM2_Clear
  • Loading branch information
embhorn authored Dec 17, 2024
2 parents d0618ad + 8177ba8 commit 415f714
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 27 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/management/flush.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

#include <wolftpm/tpm2_wrap.h>

#include <examples/management/flush.h>
#include <examples/management/management.h>
#include <hal/tpm_io.h>
#include <examples/tpm_test.h>

Expand Down
15 changes: 11 additions & 4 deletions examples/management/include.am
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* flush.h
/* management.h
*
* Copyright (C) 2006-2024 wolfSSL Inc.
*
Expand Down Expand Up @@ -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" */
Expand Down
80 changes: 80 additions & 0 deletions examples/management/tpmclear.c
Original file line number Diff line number Diff line change
@@ -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 <config.h>
#endif

#include <wolftpm/tpm2_wrap.h>
#include <examples/management/management.h>
#include <hal/tpm_io.h>
#include <examples/tpm_test.h>

#include <stdio.h>

#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
22 changes: 2 additions & 20 deletions examples/wrap/wrap_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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

Expand Down
1 change: 0 additions & 1 deletion examples/wrap/wrap_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -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[]);

Expand Down

0 comments on commit 415f714

Please sign in to comment.