tpm2_nvundefine.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <stdlib.h>
  3. #include "files.h"
  4. #include "log.h"
  5. #include "tpm2.h"
  6. #include "tpm2_tool.h"
  7. #include "tpm2_nv_util.h"
  8. #include "tpm2_options.h"
  9. typedef struct tpm_nvundefine_ctx tpm_nvundefine_ctx;
  10. struct tpm_nvundefine_ctx {
  11. struct {
  12. const char *ctx_path;
  13. const char *auth_str;
  14. tpm2_loaded_object object;
  15. } auth_hierarchy;
  16. struct {
  17. const char *path;
  18. tpm2_session *session;
  19. } policy_session;
  20. struct {
  21. bool C;
  22. } flags;
  23. TPM2_HANDLE nv_index;
  24. char *cp_hash_path;
  25. };
  26. static tpm_nvundefine_ctx ctx = {
  27. .auth_hierarchy.ctx_path = "owner",
  28. };
  29. static bool on_option(char key, char *value) {
  30. switch (key) {
  31. case 'C':
  32. ctx.flags.C = true;
  33. ctx.auth_hierarchy.ctx_path = value;
  34. break;
  35. case 'P':
  36. ctx.auth_hierarchy.auth_str = value;
  37. break;
  38. case 'S':
  39. ctx.policy_session.path = value;
  40. break;
  41. case 0:
  42. ctx.cp_hash_path = value;
  43. break;
  44. }
  45. return true;
  46. }
  47. static bool on_arg(int argc, char **argv) {
  48. return on_arg_nv_index(argc, argv, &ctx.nv_index);
  49. }
  50. static bool tpm2_tool_onstart(tpm2_options **opts) {
  51. const struct option topts[] = {
  52. { "hierarchy", required_argument, NULL, 'C' },
  53. { "auth", required_argument, NULL, 'P' },
  54. { "session", required_argument, NULL, 'S' },
  55. { "cphash", required_argument, NULL, 0 },
  56. };
  57. *opts = tpm2_options_new("C:P:S:", ARRAY_LEN(topts), topts, on_option, on_arg,
  58. 0);
  59. return *opts != NULL;
  60. }
  61. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  62. UNUSED(flags);
  63. /*
  64. * Read the public portion of the NV index so we can ascertain if
  65. * TPMA_NV_POLICYDELETE is set. This determines which command to use
  66. * to undefine the space. Either undefine or undefinespecial.
  67. */
  68. TPM2B_NV_PUBLIC *nv_public = NULL;
  69. tool_rc rc = tpm2_util_nv_read_public(ectx, ctx.nv_index, &nv_public);
  70. if (rc != tool_rc_success) {
  71. LOG_ERR("Failed to read the public part of NV index 0x%X", ctx.nv_index);
  72. return rc;
  73. }
  74. bool has_policy_delete_set = !!(nv_public->nvPublic.attributes & TPMA_NV_POLICY_DELETE);
  75. Esys_Free(nv_public);
  76. /*
  77. * The default hierarchy is typically owner, however with a policy delete object it's always
  78. * the platform.
  79. */
  80. if (!ctx.flags.C) {
  81. ctx.auth_hierarchy.ctx_path = has_policy_delete_set ? "platform" : "owner";
  82. }
  83. /* now with the default sorted out, load the authorization object */
  84. rc = tpm2_util_object_load_auth(ectx, ctx.auth_hierarchy.ctx_path,
  85. ctx.auth_hierarchy.auth_str, &ctx.auth_hierarchy.object, false,
  86. TPM2_HANDLE_FLAGS_O | TPM2_HANDLE_FLAGS_P);
  87. if (rc != tool_rc_success) {
  88. LOG_ERR("Invalid handle authorization");
  89. return rc;
  90. }
  91. bool result = true;
  92. TPM2B_DIGEST cp_hash = { .size = 0 };
  93. /* has policy delete set, so do NV undefine special */
  94. if (has_policy_delete_set) {
  95. if (!ctx.policy_session.path) {
  96. LOG_ERR("NV Spaces with attribute TPMA_NV_POLICY_DELETE require a"
  97. " policy session to be specified via \"-S\"");
  98. return tool_rc_general_error;
  99. }
  100. rc = tpm2_session_restore(ectx, ctx.policy_session.path, false,
  101. &ctx.policy_session.session);
  102. if (rc != tool_rc_success) {
  103. return rc;
  104. }
  105. /*
  106. * has to be an admin policy session for undefinespecial.
  107. * We can at least check that it is a session.
  108. */
  109. TPM2_SE type = tpm2_session_get_type(ctx.policy_session.session);
  110. if (type != TPM2_SE_POLICY) {
  111. LOG_ERR("Expected a policy session when NV index has attribute"
  112. " TPMA_NV_POLICY_DELETE set.");
  113. return tool_rc_general_error;
  114. }
  115. if (!ctx.cp_hash_path) {
  116. return tpm2_nvundefinespecial(ectx, &ctx.auth_hierarchy.object,
  117. ctx.nv_index, ctx.policy_session.session, NULL);
  118. }
  119. rc = tpm2_nvundefinespecial(ectx, &ctx.auth_hierarchy.object,
  120. ctx.nv_index, ctx.policy_session.session, &cp_hash);;
  121. if (rc != tool_rc_success) {
  122. return rc;
  123. }
  124. goto nvundefine_out;
  125. }
  126. if (ctx.policy_session.path && !has_policy_delete_set) {
  127. LOG_WARN("Option -S is not required on NV indices that don't have"
  128. " attribute TPMA_NV_POLICY_DELETE set");
  129. }
  130. if (!ctx.cp_hash_path) {
  131. return tpm2_nvundefine(ectx, &ctx.auth_hierarchy.object, ctx.nv_index,
  132. NULL);
  133. }
  134. rc = tpm2_nvundefine(ectx, &ctx.auth_hierarchy.object, ctx.nv_index,
  135. &cp_hash);;
  136. if (rc != tool_rc_success) {
  137. return rc;
  138. }
  139. nvundefine_out:
  140. result = files_save_digest(&cp_hash, ctx.cp_hash_path);
  141. if (!result) {
  142. rc = tool_rc_general_error;
  143. }
  144. return rc;
  145. }
  146. static tool_rc tpm2_tool_onstop(ESYS_CONTEXT *ectx) {
  147. UNUSED(ectx);
  148. /* attempt to close all sessions and report errors */
  149. tool_rc rc = tool_rc_success;
  150. if (!ctx.cp_hash_path) {
  151. rc = tpm2_session_close(&ctx.policy_session.session);
  152. rc |= tpm2_session_close(&ctx.auth_hierarchy.object.session);
  153. }
  154. return rc;
  155. }
  156. // Register this tool with tpm2_tool.c
  157. TPM2_TOOL_REGISTER("nvundefine", tpm2_tool_onstart, tpm2_tool_onrun, tpm2_tool_onstop, NULL)