Showing with 81 additions and 17 deletions.
  1. +18 −1 man/tpm2_listpersistent.1.md
  2. +63 −16 tools/tpm2_listpersistent.c
19 changes: 18 additions & 1 deletion man/tpm2_listpersistent.1.md
Expand Up @@ -19,18 +19,35 @@ tpm2_listpersistent(1) - display all defined persistent objects.

OPTIONS
-----------
This tool takes no tool specific options.
These options for listing the persistent objects:

* `-g`, `--halg`=_ALGORITHM_:
Only display persistent objects using this hash algorithm. Algorithms should
follow the " formatting standards, see section "Algorithm Specifiers".
Also, see section "Supported Hash Algorithms" for a list of supported
hash algorithms.

* `-G`, `--kalg`=_KEY\_ALGORITHM_:
Only display persistent objects using this key algorithm. It accepts friendly
names just like `-g` option. See section "Supported Public Object Algorithms"
for a list of supported object algorithms.

[common options](common/options.md)

[common tcti options](common/tcti.md)

[supported hash algorithms](common/hash.md)

[supported public object algorithms](common/object-alg.md)

[algorithm specifiers](common/alg.md)

EXAMPLES
--------

```
tpm2_listpersistent
tpm2_listpersistent -g sha256 -G ecc
```

Expand Down
79 changes: 63 additions & 16 deletions tools/tpm2_listpersistent.c
Expand Up @@ -40,12 +40,45 @@

#include "tpm2_options.h"
#include "files.h"
#include "log.h"
#include "tpm2_alg_util.h"
#include "tpm2_tool.h"
#include "tpm2_util.h"

int readPublic(TSS2_SYS_CONTEXT *sapi_context,
TPMI_DH_OBJECT objectHandle)
{
typedef struct tpm_listpersistent_context tpm_listpersistent_context;
struct tpm_listpersistent_context {
TPMI_ALG_HASH nameAlg;
TPMI_ALG_PUBLIC type;
UINT32 count;
};

static tpm_listpersistent_context ctx = {
.nameAlg = TPM_ALG_NULL,
.type = TPM_ALG_NULL,
};

static bool on_option(char key, char *value) {

switch (key) {
case 'g':
ctx.nameAlg = tpm2_alg_util_from_optarg(value);
if(ctx.nameAlg == TPM_ALG_ERROR) {
LOG_ERR("Invalid hash algorithm, got \"%s\"", value);
return false;
}
break;
case 'G':
ctx.type = tpm2_alg_util_from_optarg(value);
if(ctx.type == TPM_ALG_ERROR) {
LOG_ERR("Invalid key algorithm, got \"%s\"", value);
return false;
}
}

return true;
}

int readPublic(TSS2_SYS_CONTEXT *sapi_context, TPMI_DH_OBJECT objectHandle) {
UINT32 rval;
TPMS_AUTH_RESPONSE sessionDataOut;
TSS2_SYS_RSP_AUTHS sessionsDataOut;
Expand All @@ -62,19 +95,37 @@ int readPublic(TSS2_SYS_CONTEXT *sapi_context,
rval = Tss2_Sys_ReadPublic(sapi_context, objectHandle, 0, &outPublic, &name, &qualifiedName, &sessionsDataOut);
if(rval != TPM_RC_SUCCESS)
{
printf("\nTPM2_ReadPublic error: rval = 0x%0x\n\n",rval);
LOG_ERR("\nTPM2_ReadPublic error: rval = 0x%0x\n",rval);
return -1;
}

printf(" {\n");
printf("\tType: 0x%x\n ", outPublic.t.publicArea.type);
printf("\tHash algorithm(nameAlg): 0x%x\n ", outPublic.t.publicArea.nameAlg);
printf("\tAttributes: 0x%x\n", outPublic.t.publicArea.objectAttributes.val);
printf(" }\n");
TPMI_ALG_PUBLIC type = outPublic.t.publicArea.type;
TPMI_ALG_HASH nameAlg = outPublic.t.publicArea.nameAlg;
UINT32 attrs = outPublic.t.publicArea.objectAttributes.val;

if ((ctx.type != TPM_ALG_NULL && ctx.type != type) ||
(ctx.nameAlg != TPM_ALG_NULL && ctx.nameAlg != nameAlg))
return 0;

tpm2_tool_output("persistent-handle[%d]:0x%x key-alg:%s hash-alg:%s object-attr:0x%x\n",
ctx.count++, objectHandle, tpm2_alg_util_algtostr(type),
tpm2_alg_util_algtostr(nameAlg), attrs);

return 0;
}

bool tpm2_tool_onstart(tpm2_options **opts) {

static struct option topts[] = {
{"halg", required_argument, NULL, 'g'},
{"kalg", required_argument, NULL, 'G'},
};

*opts = tpm2_options_new("g:G:", ARRAY_LEN(topts), topts, on_option, NULL);

return *opts != NULL;
}

int tpm2_tool_onrun(TSS2_SYS_CONTEXT *sapi_context, tpm2_option_flags flags) {

UNUSED(flags);
Expand All @@ -89,20 +140,16 @@ int tpm2_tool_onrun(TSS2_SYS_CONTEXT *sapi_context, tpm2_option_flags flags) {
&capabilityData, 0 );
if(rval != TPM_RC_SUCCESS)
{
printf("\n......GetCapability: Get persistent object list Error."
" TPM Error:0x%x......\n", rval);
LOG_ERR("\n......GetCapability: Get persistent object list Error."
" TPM Error:0x%x......", rval);
return 1;
}

printf( "%d persistent objects defined.\n", capabilityData.data.handles.count);
UINT32 i;
for( i=0; i < capabilityData.data.handles.count; i++ )
{
printf("\n %d. Persistent handle: 0x%x\n", i, capabilityData.data.handles.handle[i]);
for(i = 0; i < capabilityData.data.handles.count; i++) {
if(readPublic(sapi_context, capabilityData.data.handles.handle[i]))
return 2;
}
printf("\n");

return 0;
}