tpm2_clearcontrol.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <string.h>
  3. #include "files.h"
  4. #include "log.h"
  5. #include "tpm2.h"
  6. #include "tpm2_tool.h"
  7. typedef struct clearcontrol_ctx clearcontrol_ctx;
  8. struct clearcontrol_ctx {
  9. struct {
  10. const char *ctx_path;
  11. const char *auth_str;
  12. tpm2_loaded_object object;
  13. } auth_hierarchy;
  14. TPMI_YES_NO disable_clear;
  15. char *cp_hash_path;
  16. };
  17. static clearcontrol_ctx ctx = {
  18. .auth_hierarchy.ctx_path = "p",
  19. .disable_clear = 0,
  20. };
  21. static tool_rc clearcontrol(ESYS_CONTEXT *ectx) {
  22. LOG_INFO("Sending TPM2_ClearControl(%s) disableClear command with auth "
  23. "handle %s", ctx.disable_clear ? "SET" : "CLEAR",
  24. ctx.auth_hierarchy.object.tr_handle == ESYS_TR_RH_PLATFORM ?
  25. "TPM2_RH_PLATFORM" : "TPM2_RH_LOCKOUT");
  26. if (ctx.cp_hash_path) {
  27. TPM2B_DIGEST cp_hash = { .size = 0 };
  28. tool_rc rc = tpm2_clearcontrol(ectx, &ctx.auth_hierarchy.object,
  29. ctx.disable_clear, &cp_hash);
  30. if (rc != tool_rc_success) {
  31. return rc;
  32. }
  33. bool result = files_save_digest(&cp_hash, ctx.cp_hash_path);
  34. if (!result) {
  35. rc = tool_rc_general_error;
  36. }
  37. return rc;
  38. }
  39. return tpm2_clearcontrol(ectx, &ctx.auth_hierarchy.object,
  40. ctx.disable_clear, NULL);
  41. }
  42. static bool on_arg(int argc, char **argv) {
  43. if (argc > 1) {
  44. LOG_ERR("Specify single set/clear operation as s|c|0|1.");
  45. return false;
  46. }
  47. if (!argc) {
  48. LOG_ERR("Disable clear SET/CLEAR operation must be specified.");
  49. return false;
  50. }
  51. if (!strcmp(argv[0], "s")) {
  52. ctx.disable_clear = 1;
  53. return true;
  54. }
  55. if (!strcmp(argv[0], "c")) {
  56. ctx.disable_clear = 0;
  57. return true;
  58. }
  59. uint32_t value;
  60. bool result = tpm2_util_string_to_uint32(argv[0], &value);
  61. if (!result) {
  62. LOG_ERR("Please specify 0|1|s|c. Could not convert string, got: \"%s\"",
  63. argv[0]);
  64. return false;
  65. }
  66. if (value != 0 && value != 1) {
  67. LOG_ERR("Please use 0|1|s|c as the argument to specify operation");
  68. return false;
  69. }
  70. ctx.disable_clear = value;
  71. return true;
  72. }
  73. static bool on_option(char key, char *value) {
  74. switch (key) {
  75. case 'C':
  76. ctx.auth_hierarchy.ctx_path = value;
  77. break;
  78. case 'P':
  79. ctx.auth_hierarchy.auth_str = value;
  80. break;
  81. case 0:
  82. ctx.cp_hash_path = value;
  83. break;
  84. }
  85. return true;
  86. }
  87. static bool tpm2_tool_onstart(tpm2_options **opts) {
  88. const struct option topts[] = {
  89. { "hierarchy", required_argument, NULL, 'C' },
  90. { "auth", required_argument, NULL, 'P' },
  91. { "cphash", required_argument, NULL, 0 },
  92. };
  93. *opts = tpm2_options_new("C:P:", ARRAY_LEN(topts), topts, on_option, on_arg,
  94. 0);
  95. return *opts != NULL;
  96. }
  97. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  98. UNUSED(flags);
  99. tool_rc rc = tpm2_util_object_load_auth(ectx, ctx.auth_hierarchy.ctx_path,
  100. ctx.auth_hierarchy.auth_str, &ctx.auth_hierarchy.object, false,
  101. TPM2_HANDLE_FLAGS_P | TPM2_HANDLE_FLAGS_L);
  102. if (rc != tool_rc_success) {
  103. LOG_ERR("Invalid authorization");
  104. return rc;
  105. }
  106. if (!ctx.disable_clear
  107. && ctx.auth_hierarchy.object.tr_handle == ESYS_TR_RH_LOCKOUT) {
  108. LOG_ERR("Only platform hierarchy handle can be specified"
  109. " for CLEAR operation on disableClear");
  110. return tool_rc_general_error;
  111. }
  112. if (ctx.cp_hash_path) {
  113. LOG_WARN("Calculating cpHash. Exiting without configuring disableClear");
  114. }
  115. return clearcontrol(ectx);
  116. }
  117. static tool_rc tpm2_tool_onstop(ESYS_CONTEXT *ectx) {
  118. UNUSED(ectx);
  119. return tpm2_session_close(&ctx.auth_hierarchy.object.session);
  120. }
  121. // Register this tool with tpm2_tool.c
  122. TPM2_TOOL_REGISTER("clearcontrol", tpm2_tool_onstart, tpm2_tool_onrun, tpm2_tool_onstop, NULL)