tpm2_setclock.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <inttypes.h>
  3. #include "files.h"
  4. #include "log.h"
  5. #include "tpm2.h"
  6. #include "tpm2_tool.h"
  7. #include "tpm2_util.h"
  8. typedef struct tpm2_setclock_ctx tpm2_setclock_ctx;
  9. struct tpm2_setclock_ctx {
  10. bool time_set;
  11. UINT64 new_time;
  12. const char *auth_hierarchy;
  13. tpm2_loaded_object object;
  14. const char *auth_value;
  15. char *cp_hash_path;
  16. };
  17. static tpm2_setclock_ctx ctx = {
  18. .auth_hierarchy = "o" /* default to owner hierarchy */
  19. };
  20. static bool on_arg(int argc, char **argv) {
  21. if (argc != 1) {
  22. LOG_ERR("Can only specify 1 time to set, got: %d", argc);
  23. return false;
  24. }
  25. bool result = tpm2_util_string_to_uint64(argv[0], &ctx.new_time);
  26. if (!result) {
  27. LOG_ERR("Could not convert argument to time, got: \"%s\"", argv[0]);
  28. return false;
  29. }
  30. ctx.time_set = true;
  31. return true;
  32. }
  33. static bool on_option(char key, char *value) {
  34. switch (key) {
  35. case 'c':
  36. ctx.auth_hierarchy = value;
  37. break;
  38. case 'p':
  39. ctx.auth_value = value;
  40. break;
  41. case 0:
  42. ctx.cp_hash_path = value;
  43. break;
  44. /* no default */
  45. }
  46. return true;
  47. }
  48. static bool tpm2_tool_onstart(tpm2_options **opts) {
  49. const struct option topts[] = {
  50. { "hierarchy", required_argument, NULL, 'c' },
  51. { "auth", required_argument, NULL, 'p' },
  52. { "cphash", required_argument, NULL, 0 },
  53. };
  54. *opts = tpm2_options_new("c:p:", ARRAY_LEN(topts), topts, on_option,
  55. on_arg, 0);
  56. return *opts != NULL;
  57. }
  58. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  59. UNUSED(flags);
  60. if (!ctx.time_set) {
  61. LOG_ERR("Expected single argument of time to set");
  62. return tool_rc_general_error;
  63. }
  64. tool_rc rc = tpm2_util_object_load_auth(ectx, ctx.auth_hierarchy,
  65. ctx.auth_value, &ctx.object, false,
  66. TPM2_HANDLE_FLAGS_P|TPM2_HANDLE_FLAGS_O);
  67. if (rc != tool_rc_success) {
  68. return rc;
  69. }
  70. if (ctx.cp_hash_path) {
  71. TPM2B_DIGEST cp_hash = { .size = 0 };
  72. LOG_WARN("Calculating cpHash. Exiting without setting clock.");
  73. rc = tpm2_setclock(ectx, &ctx.object, ctx.new_time, &cp_hash);
  74. if (rc != tool_rc_success) {
  75. return rc;
  76. }
  77. bool result = files_save_digest(&cp_hash, ctx.cp_hash_path);
  78. if (!result) {
  79. rc = tool_rc_general_error;
  80. }
  81. return rc;
  82. }
  83. return tpm2_setclock(ectx, &ctx.object, ctx.new_time, NULL);
  84. }
  85. static tool_rc tpm2_tool_onstop(ESYS_CONTEXT *ectx) {
  86. UNUSED(ectx);
  87. return tpm2_session_close(&ctx.object.session);
  88. }
  89. // Register this tool with tpm2_tool.c
  90. TPM2_TOOL_REGISTER("setclock", tpm2_tool_onstart, tpm2_tool_onrun, tpm2_tool_onstop, NULL)