-
Notifications
You must be signed in to change notification settings - Fork 382
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[RFC] tools: add tpm2_policy tool for invoking libpolicy
Create a tpm2_policy tool that can read the FAPI JSON style policies and: 1. Instantiate them -> This process fills in anything missing in the template. TODO: How does this get handled, do we need to tweak any of the callbacks? 2. Calculate them -> This process produces a list of hashes... TODO: Why? Is this a list of all the subordinate policies or can the json file have N policies where N > 1? 3. Execute them -> Execute the policy on a session. TODO: Who is supposed to start the policy session as it seems to be 0? Their are a lot of TODO items in this code. I'm looking for how we want to use this, different tools, like tpm2_policyinit tpm2_policycalc and tpm2_policyexec? Or an all in-one tool. Currently note that ONLY the Execute needs an ESYS context, but instantiate should be filling stuff in so it likely needs a context or the callbacks handled? Signed-off-by: William Roberts <[email protected]>
- Loading branch information
William Roberts
committed
Sep 2, 2021
1 parent
966f3ef
commit 1c4a201
Showing
3 changed files
with
97 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* SPDX-License-Identifier: BSD-3-Clause */ | ||
|
||
#include <inttypes.h> | ||
#include <stdbool.h> | ||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
#include <tss2/tss2_policy.h> | ||
|
||
#include "log.h" | ||
#include "tpm2_tool.h" | ||
#include "tpm2_util.h" | ||
|
||
typedef struct tpm2_policy_ctx tpm2_policy_ctx; | ||
struct tpm2_policy_ctx { | ||
ifapi_policyeval_INST_CB cb; | ||
const char *policy_file; | ||
}; | ||
|
||
static tpm2_policy_ctx ctx; | ||
|
||
static bool on_arg(int argc, char *argv[]) { | ||
|
||
if (argc != 1) { | ||
LOG_ERR("Expected single file path argument"); | ||
return false; | ||
} | ||
|
||
ctx.policy_file = argv[0]; | ||
|
||
return true; | ||
} | ||
|
||
static bool tpm2_tool_onstart(tpm2_options **opts) { | ||
// static const struct option topts[] = { | ||
// { "type", required_argument, NULL, 't' }, | ||
// { "format", required_argument, NULL, 'f' }, | ||
// }; | ||
|
||
*opts = tpm2_options_new(NULL, 0, NULL, NULL, on_arg, | ||
0); | ||
|
||
return *opts != NULL; | ||
} | ||
|
||
static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) { | ||
UNUSED(flags); | ||
|
||
TPMS_POLICY *policy_ctx; | ||
|
||
TSS2_RC rc = Tss2_PolicyInstantiate( | ||
ctx.policy_file, | ||
&ctx.cb, | ||
&policy_ctx); | ||
if (rc) { | ||
LOG_ERR("Instantiate failed"); | ||
return tool_rc_general_error; | ||
} | ||
|
||
rc = Tss2_PolicyCalculate( | ||
policy_ctx->policy, /* could we just pass context and drop these? */ | ||
&policy_ctx->policyDigests, /* same as above */ | ||
TPM2_ALG_SHA256, | ||
32, /* this could be computed from alg... */ | ||
0); /* I can't figure out what this is for, looks like some kind of recursion in the tss2 lib */ | ||
if (rc) { | ||
LOG_ERR("Calculate failed"); | ||
return tool_rc_general_error; | ||
} | ||
|
||
/* Why doesn't calculate give us the aggregate hash? */ | ||
printf("hash: "); | ||
tpm2_util_hexdump2(stdout, policy_ctx->policyDigests.digests[0].digest.sha256, 32); | ||
printf("\n"); | ||
|
||
/* | ||
* This fails as it looks like their is no session in the context, in the Esys Call the | ||
* session is 0. | ||
*/ | ||
rc = Tss2_PolicyExecute( | ||
TPM2_ALG_SHA256, | ||
policy_ctx, | ||
ectx); | ||
if (rc) { | ||
LOG_ERR("Execute failed"); | ||
return tool_rc_general_error; | ||
} | ||
|
||
return tool_rc_success; | ||
} | ||
|
||
// Register this tool with tpm2_tool.c | ||
TPM2_TOOL_REGISTER("policy", tpm2_tool_onstart, tpm2_tool_onrun, NULL, NULL) |