tpm2_policyauthvalue.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <stdlib.h>
  3. #include "files.h"
  4. #include "log.h"
  5. #include "tpm2_policy.h"
  6. #include "tpm2_tool.h"
  7. typedef struct tpm2_policyauthvalue_ctx tpm2_policyauthvalue_ctx;
  8. struct tpm2_policyauthvalue_ctx {
  9. //File path for the session context data
  10. const char *session_path;
  11. //File path for storing the policy digest output
  12. const char *policy_digest_path;
  13. tpm2_session *session;
  14. };
  15. static tpm2_policyauthvalue_ctx ctx;
  16. static bool on_option(char key, char *value) {
  17. switch (key) {
  18. case 'L':
  19. ctx.policy_digest_path = value;
  20. break;
  21. case 'S':
  22. ctx.session_path = value;
  23. break;
  24. }
  25. return true;
  26. }
  27. static bool tpm2_tool_onstart(tpm2_options **opts) {
  28. static struct option topts[] = {
  29. { "policy", required_argument, NULL, 'L' },
  30. { "session", required_argument, NULL, 'S' },
  31. };
  32. *opts = tpm2_options_new("S:L:", ARRAY_LEN(topts), topts, on_option,
  33. NULL, 0);
  34. return *opts != NULL;
  35. }
  36. static bool is_input_option_args_valid(void) {
  37. if (!ctx.session_path) {
  38. LOG_ERR("Must specify -S session file.");
  39. return false;
  40. }
  41. return true;
  42. }
  43. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  44. UNUSED(flags);
  45. bool retval = is_input_option_args_valid();
  46. if (!retval) {
  47. return tool_rc_option_error;
  48. }
  49. tool_rc rc = tpm2_session_restore(ectx, ctx.session_path, false,
  50. &ctx.session);
  51. if (rc != tool_rc_success) {
  52. return rc;
  53. }
  54. rc = tpm2_policy_build_policyauthvalue(ectx, ctx.session);
  55. if (rc != tool_rc_success) {
  56. LOG_ERR("Could not build policyauthvalue TPM");
  57. return rc;
  58. }
  59. return tpm2_policy_tool_finish(ectx, ctx.session, ctx.policy_digest_path);
  60. }
  61. static tool_rc tpm2_tool_onstop(ESYS_CONTEXT *ectx) {
  62. UNUSED(ectx);
  63. return tpm2_session_close(&ctx.session);
  64. }
  65. // Register this tool with tpm2_tool.c
  66. TPM2_TOOL_REGISTER("policyauthvalue", tpm2_tool_onstart, tpm2_tool_onrun, tpm2_tool_onstop, NULL)