tpm2_dictionarylockout.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <stdbool.h>
  3. #include <stdlib.h>
  4. #include "files.h"
  5. #include "log.h"
  6. #include "tpm2.h"
  7. #include "tpm2_tool.h"
  8. #include "tpm2_options.h"
  9. typedef struct dictionarylockout_ctx dictionarylockout_ctx;
  10. struct dictionarylockout_ctx {
  11. struct {
  12. const char *ctx_path;
  13. const char *auth_str;
  14. tpm2_loaded_object object;
  15. } auth_hierarchy;
  16. UINT32 max_tries;
  17. UINT32 recovery_time;
  18. UINT32 lockout_recovery_time;
  19. bool clear_lockout;
  20. bool setup_parameters;
  21. bool is_setup_max_tries;
  22. bool is_setup_recovery_time;
  23. bool is_setup_lockoutrecovery_time;
  24. char *cp_hash_path;
  25. };
  26. static dictionarylockout_ctx ctx = {
  27. .auth_hierarchy.ctx_path = "l",
  28. };
  29. static bool on_option(char key, char *value) {
  30. bool result;
  31. switch (key) {
  32. case 'c':
  33. ctx.clear_lockout = true;
  34. break;
  35. case 's':
  36. ctx.setup_parameters = true;
  37. break;
  38. case 'p':
  39. ctx.auth_hierarchy.auth_str = value;
  40. break;
  41. case 'n':
  42. result = tpm2_util_string_to_uint32(value, &ctx.max_tries);
  43. if (!result) {
  44. LOG_ERR("Could not convert max_tries to number, got: \"%s\"",
  45. value);
  46. return false;
  47. }
  48. if (ctx.max_tries == 0) {
  49. return false;
  50. }
  51. ctx.is_setup_max_tries = true;
  52. break;
  53. case 't':
  54. result = tpm2_util_string_to_uint32(value, &ctx.recovery_time);
  55. if (!result) {
  56. LOG_ERR("Could not convert recovery_time to number, got: \"%s\"",
  57. value);
  58. return false;
  59. }
  60. ctx.is_setup_recovery_time = true;
  61. break;
  62. case 'l':
  63. result = tpm2_util_string_to_uint32(value, &ctx.lockout_recovery_time);
  64. if (!result) {
  65. LOG_ERR("Could not convert lockout_recovery_time to number, got: "
  66. "\"%s\"", value);
  67. return false;
  68. }
  69. ctx.is_setup_lockoutrecovery_time = true;
  70. break;
  71. case 0:
  72. ctx.cp_hash_path = value;
  73. break;
  74. }
  75. return true;
  76. }
  77. static bool tpm2_tool_onstart(tpm2_options **opts) {
  78. const struct option topts[] = {
  79. { "max-tries", required_argument, NULL, 'n' },
  80. { "recovery-time", required_argument, NULL, 't' },
  81. { "lockout-recovery-time", required_argument, NULL, 'l' },
  82. { "auth", required_argument, NULL, 'p' },
  83. { "clear-lockout", no_argument, NULL, 'c' },
  84. { "setup-parameters", no_argument, NULL, 's' },
  85. { "cphash", required_argument, NULL, 0 },
  86. };
  87. *opts = tpm2_options_new("n:t:l:p:cs", ARRAY_LEN(topts), topts, on_option,
  88. NULL, 0);
  89. return *opts != NULL;
  90. }
  91. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  92. UNUSED(flags);
  93. if (!ctx.clear_lockout && !ctx.setup_parameters) {
  94. LOG_ERR("Invalid operational input: Neither Setup nor Clear lockout "
  95. "requested.");
  96. return tool_rc_option_error;
  97. }
  98. tool_rc rc = tpm2_util_object_load_auth(ectx, ctx.auth_hierarchy.ctx_path,
  99. ctx.auth_hierarchy.auth_str, &ctx.auth_hierarchy.object, false,
  100. TPM2_HANDLE_FLAGS_L);
  101. if (rc != tool_rc_success) {
  102. LOG_ERR("Invalid authorization");
  103. return rc;
  104. }
  105. /*
  106. * If calculating cpHash without executing the command, skip reading
  107. * existing dictionary lockout parameters.
  108. */
  109. if (ctx.setup_parameters && !ctx.cp_hash_path) {
  110. TPMS_CAPABILITY_DATA *capabilities = NULL;
  111. rc = tpm2_getcap(ectx, TPM2_CAP_TPM_PROPERTIES, TPM2_PT_VAR,
  112. TPM2_MAX_TPM_PROPERTIES, NULL, &capabilities);
  113. if (rc != tool_rc_success) {
  114. LOG_ERR("Couldn't read the currently setup parameters.");
  115. return rc;
  116. }
  117. TPMS_TAGGED_PROPERTY *properties = capabilities->data.tpmProperties.tpmProperty;
  118. size_t count = capabilities->data.tpmProperties.count;
  119. if (!count) {
  120. LOG_ERR("Couldn't read the currently setup parameters.");
  121. free(capabilities);
  122. return tool_rc_general_error;
  123. }
  124. size_t i;
  125. for (i = 0; i < count; i++) {
  126. if (!ctx.is_setup_max_tries &&
  127. properties[i].property == TPM2_PT_MAX_AUTH_FAIL) {
  128. ctx.max_tries = properties[i].value;
  129. continue;
  130. }
  131. if (!ctx.is_setup_recovery_time &&
  132. properties[i].property == TPM2_PT_LOCKOUT_INTERVAL) {
  133. ctx.recovery_time = properties[i].value;
  134. continue;
  135. }
  136. if (!ctx.is_setup_lockoutrecovery_time &&
  137. properties[i].property == TPM2_PT_LOCKOUT_RECOVERY) {
  138. ctx.lockout_recovery_time = properties[i].value;
  139. continue;
  140. }
  141. }
  142. free(capabilities);
  143. }
  144. TPM2B_DIGEST cp_hash = { .size = 0 };
  145. if (ctx.cp_hash_path && ctx.clear_lockout) {
  146. LOG_WARN("Calculating cpHash. Exiting without resetting dictionary lockout");
  147. tool_rc rc = tpm2_dictionarylockout_reset(ectx,
  148. &ctx.auth_hierarchy.object, &cp_hash);
  149. if (rc != tool_rc_success) {
  150. return rc;
  151. }
  152. goto out;
  153. }
  154. if (ctx.cp_hash_path && ctx.setup_parameters) {
  155. LOG_WARN("Calculating cpHash. Exiting without setting dictionary lockout");
  156. tool_rc rc = tpm2_dictionarylockout_setup(ectx,
  157. &ctx.auth_hierarchy.object, ctx.max_tries, ctx.recovery_time,
  158. ctx.lockout_recovery_time, &cp_hash);
  159. if (rc != tool_rc_success) {
  160. return rc;
  161. }
  162. goto out;
  163. }
  164. /*
  165. * If setup params and clear lockout are both required, clear lockout should
  166. * precede parameters setup.
  167. */
  168. if (ctx.clear_lockout) {
  169. return tpm2_dictionarylockout_reset(ectx, &ctx.auth_hierarchy.object,
  170. NULL);
  171. }
  172. if (ctx.setup_parameters) {
  173. return tpm2_dictionarylockout_setup(ectx, &ctx.auth_hierarchy.object,
  174. ctx.max_tries, ctx.recovery_time, ctx.lockout_recovery_time, NULL);
  175. }
  176. out:
  177. if (ctx.cp_hash_path) {
  178. bool result = files_save_digest(&cp_hash, ctx.cp_hash_path);
  179. if (!result) {
  180. rc = tool_rc_general_error;
  181. }
  182. }
  183. return rc;
  184. }
  185. static tool_rc tpm2_tool_onstop(ESYS_CONTEXT *ectx) {
  186. UNUSED(ectx);
  187. return tpm2_session_close(&ctx.auth_hierarchy.object.session);
  188. }
  189. // Register this tool with tpm2_tool.c
  190. TPM2_TOOL_REGISTER("dictionarylockout", tpm2_tool_onstart, tpm2_tool_onrun, tpm2_tool_onstop, NULL)