tpm2_policyauthorize.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <stdlib.h>
  3. #include "files.h"
  4. #include "log.h"
  5. #include "tpm2_tool.h"
  6. #include "tpm2_policy.h"
  7. #include "tpm2_tool.h"
  8. typedef struct tpm2_policyauthorize_ctx tpm2_policyauthorize_ctx;
  9. struct tpm2_policyauthorize_ctx {
  10. //File path for the session context data
  11. const char *session_path;
  12. //File path for the policy digest that will be authorized
  13. const char *policy_digest_path;
  14. //File path for the policy qualifier data
  15. const char *qualifier_data_path;
  16. //File path for the verifying public key name
  17. const char *verifying_pubkey_name_path;
  18. //File path for the verification ticket
  19. const char *ticket_path;
  20. //File path for storing the policy digest output
  21. const char *out_policy_dgst_path;
  22. tpm2_session *session;
  23. TPM2B_DIGEST *policy_digest;
  24. };
  25. static tpm2_policyauthorize_ctx ctx;
  26. static bool on_option(char key, char *value) {
  27. switch (key) {
  28. case 'L':
  29. ctx.out_policy_dgst_path = value;
  30. break;
  31. case 'S':
  32. ctx.session_path = value;
  33. break;
  34. case 'i':
  35. ctx.policy_digest_path = value;
  36. break;
  37. case 'q':
  38. ctx.qualifier_data_path = value;
  39. break;
  40. case 'n':
  41. ctx.verifying_pubkey_name_path = value;
  42. break;
  43. case 't':
  44. ctx.ticket_path = value;
  45. break;
  46. }
  47. return true;
  48. }
  49. static bool tpm2_tool_onstart(tpm2_options **opts) {
  50. static struct option topts[] = {
  51. { "policy", required_argument, NULL, 'L' },
  52. { "session", required_argument, NULL, 'S' },
  53. { "input", required_argument, NULL, 'i' },
  54. { "qualification", required_argument, NULL, 'q' },
  55. { "name", required_argument, NULL, 'n' },
  56. { "ticket", required_argument, NULL, 't' },
  57. };
  58. *opts = tpm2_options_new("L:S:i:q:n:t:", ARRAY_LEN(topts), topts, on_option,
  59. NULL, 0);
  60. return *opts != NULL;
  61. }
  62. bool is_check_input_options_ok(void) {
  63. if (!ctx.session_path) {
  64. LOG_ERR("Must specify a session file with -S.");
  65. return false;
  66. }
  67. if (!ctx.verifying_pubkey_name_path) {
  68. LOG_ERR("Must specify name of the public key used for verification -n.");
  69. return false;
  70. }
  71. return true;
  72. }
  73. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  74. UNUSED(flags);
  75. if (!is_check_input_options_ok()) {
  76. return tool_rc_option_error;
  77. }
  78. tool_rc rc = tpm2_session_restore(ectx, ctx.session_path, false,
  79. &ctx.session);
  80. if (rc != tool_rc_success) {
  81. return rc;
  82. }
  83. rc = tpm2_policy_build_policyauthorize(ectx, ctx.session,
  84. ctx.policy_digest_path, ctx.qualifier_data_path,
  85. ctx.verifying_pubkey_name_path, ctx.ticket_path);
  86. if (rc != tool_rc_success) {
  87. LOG_ERR("Could not build tpm authorized policy");
  88. return rc;
  89. }
  90. return tpm2_policy_tool_finish(ectx, ctx.session, ctx.out_policy_dgst_path);
  91. }
  92. static tool_rc tpm2_tool_onstop(ESYS_CONTEXT *ectx) {
  93. UNUSED(ectx);
  94. free(ctx.policy_digest);
  95. return tpm2_session_close(&ctx.session);
  96. }
  97. // Register this tool with tpm2_tool.c
  98. TPM2_TOOL_REGISTER("policyauthorize", tpm2_tool_onstart, tpm2_tool_onrun, tpm2_tool_onstop, NULL)