tpm2_evictcontrol.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <stdbool.h>
  3. #include "files.h"
  4. #include "log.h"
  5. #include "tpm2.h"
  6. #include "tpm2_capability.h"
  7. #include "tpm2_tool.h"
  8. typedef struct tpm_evictcontrol_ctx tpm_evictcontrol_ctx;
  9. struct tpm_evictcontrol_ctx {
  10. struct {
  11. const char *ctx_path;
  12. const char *auth_str;
  13. tpm2_loaded_object object;
  14. } auth_hierarchy;
  15. struct {
  16. char *ctx_path;
  17. tpm2_loaded_object object;
  18. } to_persist_key;
  19. TPMI_DH_PERSISTENT persist_handle;
  20. const char *output_arg;
  21. struct {
  22. UINT8 p :1;
  23. UINT8 c :1;
  24. UINT8 o :1;
  25. } flags;
  26. char *cp_hash_path;
  27. };
  28. static tpm_evictcontrol_ctx ctx = {
  29. .auth_hierarchy.ctx_path="o",
  30. };
  31. static bool on_option(char key, char *value) {
  32. switch (key) {
  33. case 'C':
  34. ctx.auth_hierarchy.ctx_path = value;
  35. break;
  36. case 'P':
  37. ctx.auth_hierarchy.auth_str = value;
  38. break;
  39. case 'c':
  40. ctx.to_persist_key.ctx_path = value;
  41. ctx.flags.c = 1;
  42. break;
  43. case 'o':
  44. ctx.output_arg = value;
  45. ctx.flags.o = 1;
  46. break;
  47. case 0:
  48. ctx.cp_hash_path = value;
  49. break;
  50. }
  51. return true;
  52. }
  53. static bool on_arg(int argc, char *argv[]) {
  54. if (argc > 1) {
  55. LOG_ERR("Expected at most one persistent handle, got %d", argc);
  56. return false;
  57. }
  58. const char *value = argv[0];
  59. bool result = tpm2_util_string_to_uint32(value, &ctx.persist_handle);
  60. if (!result) {
  61. LOG_ERR("Could not convert persistent handle to a number, got: \"%s\"",
  62. value);
  63. return false;
  64. }
  65. ctx.flags.p = 1;
  66. return true;
  67. }
  68. static bool tpm2_tool_onstart(tpm2_options **opts) {
  69. const struct option topts[] = {
  70. { "hierarchy", required_argument, NULL, 'C' },
  71. { "auth", required_argument, NULL, 'P' },
  72. { "object-context", required_argument, NULL, 'c' },
  73. { "output", required_argument, NULL, 'o' },
  74. { "cphash", required_argument, NULL, 0 },
  75. };
  76. *opts = tpm2_options_new("C:P:c:o:", ARRAY_LEN(topts), topts, on_option,
  77. on_arg, 0);
  78. return *opts != NULL;
  79. }
  80. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  81. UNUSED(flags);
  82. tool_rc rc = tool_rc_general_error;
  83. bool evicted = false;
  84. /* load up the object/handle to work on */
  85. tool_rc tmp_rc = tpm2_util_object_load(ectx, ctx.to_persist_key.ctx_path,
  86. &ctx.to_persist_key.object, TPM2_HANDLE_ALL_W_NV);
  87. if (tmp_rc != tool_rc_success) {
  88. rc = tmp_rc;
  89. goto out;
  90. }
  91. /* load up the auth hierarchy */
  92. tmp_rc = tpm2_util_object_load_auth(ectx, ctx.auth_hierarchy.ctx_path,
  93. ctx.auth_hierarchy.auth_str, &ctx.auth_hierarchy.object, false,
  94. TPM2_HANDLE_FLAGS_O | TPM2_HANDLE_FLAGS_P);
  95. if (tmp_rc != tool_rc_success) {
  96. rc = tmp_rc;
  97. goto out;
  98. }
  99. if (ctx.to_persist_key.object.handle >> TPM2_HR_SHIFT
  100. == TPM2_HT_PERSISTENT) {
  101. ctx.persist_handle = ctx.to_persist_key.object.handle;
  102. ctx.flags.p = 1;
  103. }
  104. /* If we've been given a handle or context object to persist and not an
  105. * explicit persistent handle to use, find an available vacant handle in
  106. * the persistent namespace and use that.
  107. *
  108. * XXX: We need away to figure out of object is persistent and skip it.
  109. */
  110. if (ctx.flags.c && !ctx.flags.p) {
  111. bool is_platform = ctx.auth_hierarchy.object.handle == TPM2_RH_PLATFORM;
  112. tmp_rc = tpm2_capability_find_vacant_persistent_handle(ectx,
  113. is_platform, &ctx.persist_handle);
  114. if (tmp_rc != tool_rc_success) {
  115. rc = tmp_rc;
  116. goto out;
  117. }
  118. /* we searched and found a persistent handle, so mark that peristent handle valid */
  119. ctx.flags.p = 1;
  120. }
  121. if (ctx.flags.o && !ctx.flags.p) {
  122. LOG_ERR("Cannot specify -o without using a persistent handle");
  123. goto out;
  124. }
  125. ESYS_TR out_tr;
  126. if (ctx.cp_hash_path) {
  127. TPM2B_DIGEST cp_hash = { .size = 0 };
  128. LOG_WARN("Calculating cpHash. Exiting without evicting objects.");
  129. tool_rc rc = tpm2_evictcontrol(ectx, &ctx.auth_hierarchy.object,
  130. &ctx.to_persist_key.object, ctx.persist_handle, &out_tr, &cp_hash);
  131. if (rc != tool_rc_success) {
  132. return rc;
  133. }
  134. bool result = files_save_digest(&cp_hash, ctx.cp_hash_path);
  135. if (!result) {
  136. rc = tool_rc_general_error;
  137. }
  138. return rc;
  139. }
  140. /*
  141. * ESAPI is smart enough that if the object is persistent, to ignore the argument
  142. * for persistent handle. Thus we can use ESYS_TR output to determine if it's
  143. * evicted or not.
  144. */
  145. rc = tpm2_evictcontrol(ectx, &ctx.auth_hierarchy.object,
  146. &ctx.to_persist_key.object, ctx.persist_handle, &out_tr, NULL);
  147. if (rc != tool_rc_success) {
  148. goto out;
  149. }
  150. /*
  151. * Only Close a TR object if it's still resident in the TPM.
  152. * When these handles match, evictcontrol flushed it from the TPM.
  153. * It's evicted when ESAPI sends back a none handle on evictcontrol.
  154. *
  155. * XXX: This output is wrong because we can't determine what handle was
  156. * evicted on ESYS_TR input.
  157. *
  158. * See bug: https://github.com/tpm2-software/tpm2-tools/issues/1816
  159. */
  160. evicted = out_tr == ESYS_TR_NONE;
  161. tpm2_tool_output("persistent-handle: 0x%x\n", ctx.persist_handle);
  162. tpm2_tool_output("action: %s\n", evicted ? "evicted" : "persisted");
  163. if (ctx.output_arg) {
  164. rc = files_save_ESYS_TR(ectx, out_tr, ctx.output_arg);
  165. } else {
  166. rc = tool_rc_success;
  167. }
  168. out:
  169. if (!evicted) {
  170. rc = tpm2_close(ectx, &out_tr);
  171. }
  172. return rc;
  173. }
  174. static tool_rc tpm2_tool_onstop(ESYS_CONTEXT *ectx) {
  175. UNUSED(ectx);
  176. return tpm2_session_close(&ctx.auth_hierarchy.object.session);
  177. }
  178. // Register this tool with tpm2_tool.c
  179. TPM2_TOOL_REGISTER("evictcontrol", tpm2_tool_onstart, tpm2_tool_onrun, tpm2_tool_onstop, NULL)