Skip to content

Commit 1c4a201

Browse files
author
William Roberts
committed
[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 <william.c.roberts@intel.com>
1 parent 966f3ef commit 1c4a201

File tree

3 files changed

+97
-2
lines changed

3 files changed

+97
-2
lines changed

Makefile.am

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ LIB_COMMON := lib/libcommon.a
1919
AM_CFLAGS := \
2020
$(INCLUDE_DIRS) $(EXTRA_CFLAGS) $(TSS2_ESYS_CFLAGS) $(TSS2_MU_CFLAGS) \
2121
$(CRYPTO_CFLAGS) $(CODE_COVERAGE_CFLAGS) $(TSS2_TCTILDR_CFLAGS) \
22-
$(TSS2_RC_CFLAGS) $(TSS2_SYS_CFLAGS)
22+
$(TSS2_RC_CFLAGS) $(TSS2_SYS_CFLAGS) $(TSS2_POLICY_CFLAGS)
2323

2424
AM_LDFLAGS := $(EXTRA_LDFLAGS) $(CODE_COVERAGE_LIBS)
2525

2626
LDADD = \
2727
$(LIB_COMMON) $(TSS2_ESYS_LIBS) $(TSS2_MU_LIBS) $(CRYPTO_LIBS) $(TSS2_TCTILDR_LIBS) \
28-
$(TSS2_RC_LIBS) $(TSS2_SYS_LIBS) $(EFIVAR_LIBS)
28+
$(TSS2_RC_LIBS) $(TSS2_SYS_LIBS) $(EFIVAR_LIBS) $(TSS2_POLICY_LIBS)
2929

3030
AM_DISTCHECK_CONFIGURE_FLAGS = --with-bashcompdir='$$(datarootdir)/bash-completion/completions'
3131

@@ -104,6 +104,7 @@ tpm2_tools = \
104104
tools/misc/tpm2_certifyX509certutil.c \
105105
tools/misc/tpm2_checkquote.c \
106106
tools/misc/tpm2_eventlog.c \
107+
tools/misc/tpm2_policy.c \
107108
tools/misc/tpm2_print.c \
108109
tools/misc/tpm2_rc_decode.c \
109110
tools/tpm2_activatecredential.c \

configure.ac

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ PKG_CHECK_MODULES([TSS2_TCTILDR], [tss2-tctildr])
5858
PKG_CHECK_MODULES([TSS2_MU], [tss2-mu])
5959
PKG_CHECK_MODULES([TSS2_RC], [tss2-rc])
6060
PKG_CHECK_MODULES([TSS2_SYS], [tss2-sys])
61+
PKG_CHECK_MODULES([TSS2_POLICY], [tss2-policy])
6162
PKG_CHECK_MODULES([CRYPTO], [libcrypto >= 1.1.0])
6263
PKG_CHECK_MODULES([CURL], [libcurl])
6364

tools/misc/tpm2_policy.c

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/* SPDX-License-Identifier: BSD-3-Clause */
2+
3+
#include <inttypes.h>
4+
#include <stdbool.h>
5+
#include <stdio.h>
6+
#include <string.h>
7+
8+
#include <tss2/tss2_policy.h>
9+
10+
#include "log.h"
11+
#include "tpm2_tool.h"
12+
#include "tpm2_util.h"
13+
14+
typedef struct tpm2_policy_ctx tpm2_policy_ctx;
15+
struct tpm2_policy_ctx {
16+
ifapi_policyeval_INST_CB cb;
17+
const char *policy_file;
18+
};
19+
20+
static tpm2_policy_ctx ctx;
21+
22+
static bool on_arg(int argc, char *argv[]) {
23+
24+
if (argc != 1) {
25+
LOG_ERR("Expected single file path argument");
26+
return false;
27+
}
28+
29+
ctx.policy_file = argv[0];
30+
31+
return true;
32+
}
33+
34+
static bool tpm2_tool_onstart(tpm2_options **opts) {
35+
// static const struct option topts[] = {
36+
// { "type", required_argument, NULL, 't' },
37+
// { "format", required_argument, NULL, 'f' },
38+
// };
39+
40+
*opts = tpm2_options_new(NULL, 0, NULL, NULL, on_arg,
41+
0);
42+
43+
return *opts != NULL;
44+
}
45+
46+
static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
47+
UNUSED(flags);
48+
49+
TPMS_POLICY *policy_ctx;
50+
51+
TSS2_RC rc = Tss2_PolicyInstantiate(
52+
ctx.policy_file,
53+
&ctx.cb,
54+
&policy_ctx);
55+
if (rc) {
56+
LOG_ERR("Instantiate failed");
57+
return tool_rc_general_error;
58+
}
59+
60+
rc = Tss2_PolicyCalculate(
61+
policy_ctx->policy, /* could we just pass context and drop these? */
62+
&policy_ctx->policyDigests, /* same as above */
63+
TPM2_ALG_SHA256,
64+
32, /* this could be computed from alg... */
65+
0); /* I can't figure out what this is for, looks like some kind of recursion in the tss2 lib */
66+
if (rc) {
67+
LOG_ERR("Calculate failed");
68+
return tool_rc_general_error;
69+
}
70+
71+
/* Why doesn't calculate give us the aggregate hash? */
72+
printf("hash: ");
73+
tpm2_util_hexdump2(stdout, policy_ctx->policyDigests.digests[0].digest.sha256, 32);
74+
printf("\n");
75+
76+
/*
77+
* This fails as it looks like their is no session in the context, in the Esys Call the
78+
* session is 0.
79+
*/
80+
rc = Tss2_PolicyExecute(
81+
TPM2_ALG_SHA256,
82+
policy_ctx,
83+
ectx);
84+
if (rc) {
85+
LOG_ERR("Execute failed");
86+
return tool_rc_general_error;
87+
}
88+
89+
return tool_rc_success;
90+
}
91+
92+
// Register this tool with tpm2_tool.c
93+
TPM2_TOOL_REGISTER("policy", tpm2_tool_onstart, tpm2_tool_onrun, NULL, NULL)

0 commit comments

Comments
 (0)