|
| 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