tpm2_policyauthorizenv.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <stdlib.h>
  3. #include "files.h"
  4. #include "tpm2.h"
  5. #include "tpm2_tool.h"
  6. #include "tpm2_nv_util.h"
  7. #include "tpm2_options.h"
  8. #include "tpm2_policy.h"
  9. typedef struct tpm_policyauthorizenv_ctx tpm_policyauthorizenv_ctx;
  10. struct tpm_policyauthorizenv_ctx {
  11. struct {
  12. const char *ctx_path;
  13. const char *auth_str;
  14. tpm2_loaded_object object;
  15. } auth_hierarchy;
  16. TPM2_HANDLE nv_index;
  17. const char *out_policy_dgst_path;
  18. const char *session_path;
  19. tpm2_session *session;
  20. char *cp_hash_path;
  21. };
  22. static tpm_policyauthorizenv_ctx ctx;
  23. static bool on_arg(int argc, char **argv) {
  24. /* If the user doesn't specify an authorization hierarchy use the index
  25. * passed to -x/--index for the authorization index.
  26. */
  27. if (!ctx.auth_hierarchy.ctx_path) {
  28. ctx.auth_hierarchy.ctx_path = argv[0];
  29. }
  30. return on_arg_nv_index(argc, argv, &ctx.nv_index);
  31. }
  32. static bool on_option(char key, char *value) {
  33. switch (key) {
  34. case 'C':
  35. ctx.auth_hierarchy.ctx_path = value;
  36. break;
  37. case 'P':
  38. ctx.auth_hierarchy.auth_str = value;
  39. break;
  40. case 'L':
  41. ctx.out_policy_dgst_path = value;
  42. break;
  43. case 'S':
  44. ctx.session_path = value;
  45. break;
  46. case 0:
  47. ctx.cp_hash_path = value;
  48. break;
  49. default:
  50. return false;
  51. }
  52. return true;
  53. }
  54. static bool tpm2_tool_onstart(tpm2_options **opts) {
  55. const struct option topts[] = {
  56. { "hierarchy", required_argument, NULL, 'C' },
  57. { "auth", required_argument, NULL, 'P' },
  58. { "policy", required_argument, NULL, 'L' },
  59. { "session", required_argument, NULL, 'S' },
  60. { "cphash", required_argument, NULL, 0 },
  61. };
  62. *opts = tpm2_options_new("C:P:L:S:", ARRAY_LEN(topts), topts, on_option,
  63. on_arg, 0);
  64. return *opts != NULL;
  65. }
  66. static bool is_input_option_args_valid(void) {
  67. if (!ctx.session_path) {
  68. LOG_ERR("Must specify -S session file.");
  69. return false;
  70. }
  71. if (ctx.cp_hash_path && ctx.out_policy_dgst_path) {
  72. LOG_WARN("Cannot output policyhash when calculating cphash.");
  73. return false;
  74. }
  75. return true;
  76. }
  77. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  78. UNUSED(flags);
  79. bool retval = is_input_option_args_valid();
  80. if (!retval) {
  81. return tool_rc_option_error;
  82. }
  83. tool_rc rc = tpm2_util_object_load_auth(ectx, ctx.auth_hierarchy.ctx_path,
  84. ctx.auth_hierarchy.auth_str, &ctx.auth_hierarchy.object, false,
  85. TPM2_HANDLE_FLAGS_NV | TPM2_HANDLE_FLAGS_O | TPM2_HANDLE_FLAGS_P);
  86. if (rc != tool_rc_success) {
  87. LOG_ERR("Invalid handle authorization");
  88. return rc;
  89. }
  90. rc = tpm2_session_restore(ectx, ctx.session_path, false, &ctx.session);
  91. if (rc != tool_rc_success) {
  92. return rc;
  93. }
  94. ESYS_TR policy_session_handle = tpm2_session_get_handle(ctx.session);
  95. if (!ctx.cp_hash_path) {
  96. rc = tpm2_policy_authorize_nv(ectx, &ctx.auth_hierarchy.object,
  97. ctx.nv_index, policy_session_handle, NULL);
  98. if (rc != tool_rc_success) {
  99. return rc;
  100. }
  101. return tpm2_policy_tool_finish(ectx, ctx.session,
  102. ctx.out_policy_dgst_path);
  103. }
  104. TPM2B_DIGEST cp_hash = { .size = 0 };
  105. rc = tpm2_policy_authorize_nv(ectx, &ctx.auth_hierarchy.object,
  106. ctx.nv_index, policy_session_handle, &cp_hash);
  107. if (rc != tool_rc_success) {
  108. goto cphash_error_out;
  109. }
  110. bool result = files_save_digest(&cp_hash, ctx.cp_hash_path);
  111. if (!result) {
  112. rc = tool_rc_general_error;
  113. } else {
  114. goto cphash_out;
  115. }
  116. cphash_error_out:
  117. LOG_ERR("Failed cphash calculation operation");
  118. cphash_out:
  119. return rc;
  120. }
  121. static tool_rc tpm2_tool_onstop(ESYS_CONTEXT *ectx) {
  122. UNUSED(ectx);
  123. tool_rc rc = tool_rc_success;
  124. tool_rc tmp_rc = tool_rc_success;
  125. if (!ctx.cp_hash_path) {
  126. tmp_rc = tpm2_session_close(&ctx.auth_hierarchy.object.session);
  127. if (tmp_rc != tool_rc_success) {
  128. rc = tmp_rc;
  129. }
  130. }
  131. tmp_rc = tpm2_session_close(&ctx.session);
  132. if (tmp_rc != tool_rc_success) {
  133. rc = tmp_rc;
  134. }
  135. return rc;
  136. }
  137. // Register this tool with tpm2_tool.c
  138. TPM2_TOOL_REGISTER("policyauthorizenv", tpm2_tool_onstart, tpm2_tool_onrun, tpm2_tool_onstop, NULL)