tpm2_nvreadlock.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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_nvreadlock_ctx tpm_nvreadlock_ctx;
  10. struct tpm_nvreadlock_ctx {
  11. struct {
  12. const char *ctx_path;
  13. const char *auth_str;
  14. tpm2_loaded_object object;
  15. } auth_hierarchy;
  16. TPM2_HANDLE nv_index;
  17. char *cp_hash_path;
  18. };
  19. static tpm_nvreadlock_ctx ctx;
  20. static bool on_arg(int argc, char **argv) {
  21. /* If the user doesn't specify an authorization hierarchy use the index
  22. * passed to -x/--index for the authorization index.
  23. */
  24. if (!ctx.auth_hierarchy.ctx_path) {
  25. ctx.auth_hierarchy.ctx_path = argv[0];
  26. }
  27. return on_arg_nv_index(argc, argv, &ctx.nv_index);
  28. }
  29. static bool on_option(char key, char *value) {
  30. switch (key) {
  31. case 'C':
  32. ctx.auth_hierarchy.ctx_path = value;
  33. break;
  34. case 'P':
  35. ctx.auth_hierarchy.auth_str = value;
  36. break;
  37. case 0:
  38. ctx.cp_hash_path = value;
  39. break;
  40. }
  41. return true;
  42. }
  43. static bool tpm2_tool_onstart(tpm2_options **opts) {
  44. const struct option topts[] = {
  45. { "hierarchy", required_argument, NULL, 'C' },
  46. { "auth", required_argument, NULL, 'P' },
  47. { "cphash", required_argument, NULL, 0 },
  48. };
  49. *opts = tpm2_options_new("C:P:", ARRAY_LEN(topts), topts, on_option, on_arg,
  50. 0);
  51. return *opts != NULL;
  52. }
  53. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  54. UNUSED(flags);
  55. tool_rc rc = tpm2_util_object_load_auth(ectx, ctx.auth_hierarchy.ctx_path,
  56. ctx.auth_hierarchy.auth_str, &ctx.auth_hierarchy.object, false,
  57. TPM2_HANDLE_FLAGS_NV | TPM2_HANDLE_FLAGS_O | TPM2_HANDLE_FLAGS_P);
  58. if (rc != tool_rc_success) {
  59. LOG_ERR("Invalid handle authorization");
  60. return rc;
  61. }
  62. if (!ctx.cp_hash_path) {
  63. return tpm2_nvreadlock(ectx, &ctx.auth_hierarchy.object, ctx.nv_index,
  64. NULL);
  65. }
  66. TPM2B_DIGEST cp_hash = { .size = 0 };
  67. rc = tpm2_nvreadlock(ectx, &ctx.auth_hierarchy.object, ctx.nv_index,
  68. &cp_hash);
  69. if (rc != tool_rc_success) {
  70. return rc;
  71. }
  72. bool result = files_save_digest(&cp_hash, ctx.cp_hash_path);
  73. if (!result) {
  74. rc = tool_rc_general_error;
  75. }
  76. return rc;
  77. }
  78. static tool_rc tpm2_tool_onstop(ESYS_CONTEXT *ectx) {
  79. UNUSED(ectx);
  80. if (!ctx.cp_hash_path) {
  81. return tpm2_session_close(&ctx.auth_hierarchy.object.session);
  82. }
  83. return tool_rc_success;
  84. }
  85. // Register this tool with tpm2_tool.c
  86. TPM2_TOOL_REGISTER("nvreadlock", tpm2_tool_onstart, tpm2_tool_onrun, tpm2_tool_onstop, NULL)