Skip to content

Commit 415f714

Browse files
authored
Merge pull request #391 from dgarske/tpmclear
Create separate tool for performing the TPM2_Clear
2 parents d0618ad + 8177ba8 commit 415f714

File tree

7 files changed

+97
-27
lines changed

7 files changed

+97
-27
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ examples/pcr/policy_sign
5353
examples/pcr/reset
5454
examples/timestamp/clock_set
5555
examples/management/flush
56+
examples/management/tpmclear
5657
pkcs7tpmsigned.p7s
5758
pkcs7tpmsignedex.p7s
5859
examples/tls/tls_server

examples/management/flush.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
#include <wolftpm/tpm2_wrap.h>
2929

30-
#include <examples/management/flush.h>
30+
#include <examples/management/management.h>
3131
#include <hal/tpm_io.h>
3232
#include <examples/tpm_test.h>
3333

examples/management/include.am

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,23 @@
22
# All paths should be given relative to the root
33

44
if BUILD_EXAMPLES
5-
noinst_PROGRAMS += examples/management/flush
5+
noinst_PROGRAMS += examples/management/flush \
6+
examples/management/tpmclear
67

7-
noinst_HEADERS += examples/management/flush.h
8+
noinst_HEADERS += examples/management/management.h
89

910
examples_management_flush_SOURCES = examples/management/flush.c
1011
examples_management_flush_LDADD = src/libwolftpm.la $(LIB_STATIC_ADD)
1112
examples_management_flush_DEPENDENCIES = src/libwolftpm.la
13+
14+
examples_management_tpmclear_SOURCES = examples/management/tpmclear.c
15+
examples_management_tpmclear_LDADD = src/libwolftpm.la $(LIB_STATIC_ADD)
16+
examples_management_tpmclear_DEPENDENCIES = src/libwolftpm.la
1217
endif
1318

1419
example_managementdir = $(exampledir)/management
15-
dist_example_management_DATA = examples/management/flush.c
20+
dist_example_management_DATA = examples/management/flush.c \
21+
examples/management/tpmclear.c
1622

17-
DISTCLEANFILES+= examples/management/.libs/flush
23+
DISTCLEANFILES+= examples/management/.libs/flush \
24+
examples/management/.libs/tpmclear
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* flush.h
1+
/* management.h
22
*
33
* Copyright (C) 2006-2024 wolfSSL Inc.
44
*
@@ -27,6 +27,7 @@
2727
#endif
2828

2929
int TPM2_Flush_Tool(void* userCtx, int argc, char *argv[]);
30+
int TPM2_Clear_Tool(void* userCtx, int argc, char *argv[]);
3031

3132
#ifdef __cplusplus
3233
} /* extern "C" */

examples/management/tpmclear.c

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/* tpmclear.c
2+
*
3+
* Copyright (C) 2006-2024 wolfSSL Inc.
4+
*
5+
* This file is part of wolfTPM.
6+
*
7+
* wolfTPM is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation; either version 2 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* wolfTPM is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20+
*/
21+
22+
/* This is a tool for performing a TPM2_Clear call to reset the NV */
23+
24+
#ifdef HAVE_CONFIG_H
25+
#include <config.h>
26+
#endif
27+
28+
#include <wolftpm/tpm2_wrap.h>
29+
#include <examples/management/management.h>
30+
#include <hal/tpm_io.h>
31+
#include <examples/tpm_test.h>
32+
33+
#include <stdio.h>
34+
35+
#ifndef WOLFTPM2_NO_WRAPPER
36+
int TPM2_Clear_Tool(void* userCtx, int argc, char *argv[])
37+
{
38+
int rc = TPM_RC_FAILURE;
39+
WOLFTPM2_DEV dev;
40+
41+
(void)argc;
42+
(void)argv;
43+
44+
printf("Preparing to clear TPM\n");
45+
rc = wolfTPM2_Init(&dev, TPM2_IoCb, userCtx);
46+
if (rc != TPM_RC_SUCCESS) {
47+
printf("wolfTPM2_Init failed 0x%x: %s\n", rc, TPM2_GetRCString(rc));
48+
return rc;
49+
}
50+
51+
/* reset all content on TPM and reseed */
52+
rc = wolfTPM2_Clear(&dev);
53+
if (rc == 0) {
54+
printf("TPM Clear success\n");
55+
}
56+
57+
if (rc != 0) {
58+
printf("Failure 0x%x: %s\n", rc, wolfTPM2_GetRCString(rc));
59+
}
60+
wolfTPM2_Cleanup(&dev);
61+
return rc;
62+
}
63+
#endif /* !WOLFTPM2_NO_WRAPPER */
64+
65+
#ifndef NO_MAIN_DRIVER
66+
int main(int argc, char *argv[])
67+
{
68+
int rc = NOT_COMPILED_IN;
69+
70+
#ifndef WOLFTPM2_NO_WRAPPER
71+
rc = TPM2_Clear_Tool(NULL, argc, argv);
72+
#else
73+
printf("Flush tool not compiled in\n");
74+
(void)argc;
75+
(void)argv;
76+
#endif
77+
78+
return rc;
79+
}
80+
#endif

examples/wrap/wrap_test.c

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,6 @@
4747
/* --- BEGIN Wrapper API Tests -- */
4848
/******************************************************************************/
4949

50-
static int resetTPM = 0;
51-
52-
void TPM2_Wrapper_SetReset(int reset)
53-
{
54-
resetTPM = reset;
55-
}
56-
5750
static void usage(void)
5851
{
5952
printf("Expected Usage:\n");
@@ -209,12 +202,6 @@ int TPM2_Wrapper_TestArgs(void* userCtx, int argc, char *argv[])
209202
printf("Found %d persistent handles\n", rc);
210203
}
211204

212-
if (resetTPM) {
213-
/* reset all content on TPM and reseed */
214-
rc = wolfTPM2_Clear(&dev);
215-
if (rc != 0) return rc;
216-
}
217-
218205
/* unload all transient handles */
219206
rc = wolfTPM2_UnloadHandles_AllTransient(&dev);
220207
if (rc != 0) goto exit;
@@ -1045,16 +1032,11 @@ int main(int argc, char *argv[])
10451032
{
10461033
int rc = -1;
10471034

1048-
if (argc > 1) {
1049-
#ifndef WOLFTPM2_NO_WRAPPER
1050-
TPM2_Wrapper_SetReset(1);
1051-
#endif
1052-
}
1053-
(void)argv;
1054-
10551035
#ifndef WOLFTPM2_NO_WRAPPER
10561036
rc = TPM2_Wrapper_TestArgs(NULL, argc, argv);
10571037
#else
1038+
(void)argc;
1039+
(void)argv;
10581040
printf("Wrapper code not compiled in\n");
10591041
#endif
10601042

examples/wrap/wrap_test.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
extern "C" {
2727
#endif
2828

29-
void TPM2_Wrapper_SetReset(int reset);
3029
int TPM2_Wrapper_Test(void* userCtx);
3130
int TPM2_Wrapper_TestArgs(void* userCtx, int argc, char *argv[]);
3231

0 commit comments

Comments
 (0)