Showing with 20 additions and 16 deletions.
  1. +16 −16 tools/tpm2_createprimary.c
  2. +4 −0 tools/tpm2_tool.c
32 changes: 16 additions & 16 deletions tools/tpm2_createprimary.c
Expand Up @@ -62,10 +62,9 @@ struct tpm_createprimary_ctx {
char *context_file;
TPM2_HANDLE handle2048rsa;
struct {
UINT8 A : 1;
UINT8 H : 1;
UINT8 g : 1;
UINT8 G : 1;
UINT8 C : 1;
} flags;
};

Expand Down Expand Up @@ -202,7 +201,7 @@ static bool on_option(char key, char *value) {
if (!res) {
return false;
}
ctx.flags.A = 1;
ctx.flags.H = 1;
break;
case 'P':
res = tpm2_password_util_from_optarg(value, &ctx.session_data.hmac);
Expand Down Expand Up @@ -239,7 +238,6 @@ static bool on_option(char key, char *value) {
if(ctx.context_file == NULL || ctx.context_file[0] == '\0') {
return false;
}
ctx.flags.C = 1;
break;
case 'L':
ctx.in_public.publicArea.authPolicy.size = BUFFER_SIZE(TPM2B_DIGEST, buffer);
Expand Down Expand Up @@ -293,24 +291,26 @@ bool tpm2_tool_onstart(tpm2_options **opts) {
return *opts != NULL;
}

int tpm2_tool_onrun(TSS2_SYS_CONTEXT *sapi_context, tpm2_option_flags flags) {
static inline bool valid_ctx(struct tpm_createprimary_ctx ctx) {
return (ctx.flags.H && ctx.flags.g && ctx.flags.G);
}

int tpm2_tool_onrun(TSS2_SYS_CONTEXT *sapi_context, tpm2_option_flags flags) {
UNUSED(flags);
int returnVal = 0;
int returnVal;

if(ctx.flags.A == 1 && ctx.flags.g == 1 && ctx.flags.G == 1) {
returnVal = create_primary(sapi_context);
if (!valid_ctx(ctx)) {
return 1;
}

if (returnVal == 0 && ctx.flags.C) {
returnVal = files_save_tpm_context_to_path(sapi_context, ctx.handle2048rsa,
ctx.context_file) != true;
}
returnVal = create_primary(sapi_context);
if (returnVal != 0 || ctx.context_file == NULL) {
return returnVal;
}

if(returnVal) {
if (!files_save_tpm_context_to_path(sapi_context, ctx.handle2048rsa,
ctx.context_file)) {
return 1;
}
} else {
return 1;
}

return 0;
Expand Down
4 changes: 4 additions & 0 deletions tools/tpm2_tool.c
Expand Up @@ -157,6 +157,10 @@ int main(int argc, char *argv[], char *envp[]) {
* 'main'.
*/
ret = tpm2_tool_onrun(sapi_context, flags) ? 1 : 0;
if (ret != 0) {
LOG_ERR("Unable to run %s", argv[0]);
}

/*
* Cleanup contexts & memory allocated for the modified argument vector
* passed to execute_tool.
Expand Down