tpm2_policyticket.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "files.h"
  5. #include "log.h"
  6. #include "tpm2_alg_util.h"
  7. #include "tpm2_convert.h"
  8. #include "tpm2_policy.h"
  9. #include "tpm2_tool.h"
  10. typedef struct tpm2_policyticket_ctx tpm2_policyticket_ctx;
  11. struct tpm2_policyticket_ctx {
  12. const char *session_path;
  13. tpm2_session *session;
  14. const char *policy_digest_path;
  15. char *policy_timeout_path;
  16. const char *policy_qualifier_data;
  17. char *policy_ticket_path;
  18. const char *auth_name_file;
  19. };
  20. static tpm2_policyticket_ctx ctx;
  21. static bool on_option(char key, char *value) {
  22. switch (key) {
  23. case 'L':
  24. ctx.policy_digest_path = value;
  25. break;
  26. case 'S':
  27. ctx.session_path = value;
  28. break;
  29. case 'n':
  30. ctx.auth_name_file = value;
  31. break;
  32. case 'q':
  33. ctx.policy_qualifier_data = value;
  34. break;
  35. case 0:
  36. ctx.policy_ticket_path = value;
  37. break;
  38. case 1:
  39. ctx.policy_timeout_path = value;
  40. break;
  41. }
  42. return true;
  43. }
  44. static bool tpm2_tool_onstart(tpm2_options **opts) {
  45. static struct option topts[] = {
  46. { "policy", required_argument, NULL, 'L' },
  47. { "session", required_argument, NULL, 'S' },
  48. { "name", required_argument, NULL, 'n' },
  49. { "qualification", required_argument, NULL, 'q' },
  50. { "ticket", required_argument, NULL, 0 },
  51. { "timeout", required_argument, NULL, 1 },
  52. };
  53. *opts = tpm2_options_new("L:S:n:q:", ARRAY_LEN(topts), topts, on_option,
  54. NULL, 0);
  55. return *opts != NULL;
  56. }
  57. static bool is_input_option_args_valid(void) {
  58. if (!ctx.session_path) {
  59. LOG_ERR("Must specify -S session file.");
  60. return false;
  61. }
  62. if (!ctx.auth_name_file) {
  63. LOG_ERR("Must specify -n authname file.");
  64. return false;
  65. }
  66. if (!ctx.policy_ticket_path) {
  67. LOG_ERR("Must specify --ticket policy ticket file.");
  68. return false;
  69. }
  70. if (!ctx.policy_timeout_path) {
  71. LOG_ERR("Must specify --timeout policy timeout file.");
  72. return false;
  73. }
  74. return true;
  75. }
  76. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  77. UNUSED(flags);
  78. bool retval = is_input_option_args_valid();
  79. if (!retval) {
  80. return tool_rc_option_error;
  81. }
  82. tool_rc rc = tpm2_session_restore(ectx, ctx.session_path, false,
  83. &ctx.session);
  84. if (rc != tool_rc_success) {
  85. return rc;
  86. }
  87. rc = tpm2_policy_build_policyticket(ectx, ctx.session,
  88. ctx.policy_timeout_path, ctx.policy_qualifier_data,
  89. ctx.policy_ticket_path, ctx.auth_name_file);
  90. if (rc != tool_rc_success) {
  91. LOG_ERR("Could not build policyticket TPM");
  92. return rc;
  93. }
  94. rc = tpm2_policy_tool_finish(ectx, ctx.session, ctx.policy_digest_path);
  95. if (rc != tool_rc_success) {
  96. return rc;
  97. }
  98. return tool_rc_success;
  99. }
  100. static tool_rc tpm2_tool_onstop(ESYS_CONTEXT *ectx) {
  101. UNUSED(ectx);
  102. return tpm2_session_close(&ctx.session);
  103. }
  104. // Register this tool with tpm2_tool.c
  105. TPM2_TOOL_REGISTER("policyticket", tpm2_tool_onstart, tpm2_tool_onrun, tpm2_tool_onstop, NULL)