tpm2_nvread.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <stdlib.h>
  3. #include "files.h"
  4. #include "tpm2_tool.h"
  5. #include "tpm2_nv_util.h"
  6. #include "tpm2_options.h"
  7. typedef struct tpm_nvread_ctx tpm_nvread_ctx;
  8. struct tpm_nvread_ctx {
  9. struct {
  10. const char *ctx_path;
  11. const char *auth_str;
  12. tpm2_loaded_object object;
  13. } auth_hierarchy;
  14. TPM2_HANDLE nv_index;
  15. UINT32 size_to_read;
  16. UINT32 offset;
  17. char *output_file;
  18. char *cp_hash_path;
  19. };
  20. static tpm_nvread_ctx ctx;
  21. static tool_rc nv_read(ESYS_CONTEXT *ectx, tpm2_option_flags flags,
  22. TPM2B_DIGEST *cp_hash) {
  23. UINT8* data_buffer = NULL;
  24. UINT16 bytes_written = 0;
  25. tool_rc rc = tpm2_util_nv_read(ectx, ctx.nv_index, ctx.size_to_read,
  26. ctx.offset, &ctx.auth_hierarchy.object, &data_buffer, &bytes_written,
  27. cp_hash);
  28. if (rc != tool_rc_success || cp_hash != NULL) {
  29. goto out;
  30. }
  31. /* dump data_buffer to output file, if specified */
  32. if (ctx.output_file) {
  33. if (!files_save_bytes_to_file(ctx.output_file, data_buffer,
  34. bytes_written)) {
  35. rc = tool_rc_general_error;
  36. goto out;
  37. }
  38. /* else use stdout if quiet is not specified */
  39. } else if (!flags.quiet) {
  40. if (!files_write_bytes(stdout, data_buffer, bytes_written)) {
  41. rc = tool_rc_general_error;
  42. goto out;
  43. }
  44. }
  45. out:
  46. if (data_buffer) {
  47. free(data_buffer);
  48. }
  49. return rc;
  50. }
  51. static bool on_arg(int argc, char **argv) {
  52. /* If the user doesn't specify an authorization hierarchy use the index
  53. * passed to -x/--index for the authorization index.
  54. */
  55. if (!ctx.auth_hierarchy.ctx_path) {
  56. ctx.auth_hierarchy.ctx_path = argv[0];
  57. }
  58. return on_arg_nv_index(argc, argv, &ctx.nv_index);
  59. }
  60. static bool on_option(char key, char *value) {
  61. bool result;
  62. switch (key) {
  63. case 'C':
  64. ctx.auth_hierarchy.ctx_path = value;
  65. break;
  66. case 'o':
  67. ctx.output_file = value;
  68. break;
  69. case 'P':
  70. ctx.auth_hierarchy.auth_str = value;
  71. break;
  72. case 's':
  73. result = tpm2_util_string_to_uint32(value, &ctx.size_to_read);
  74. if (!result) {
  75. LOG_ERR("Could not convert size to number, got: \"%s\"", value);
  76. return false;
  77. }
  78. break;
  79. case 0:
  80. result = tpm2_util_string_to_uint32(value, &ctx.offset);
  81. if (!result) {
  82. LOG_ERR("Could not convert offset to number, got: \"%s\"", value);
  83. return false;
  84. }
  85. break;
  86. case 1:
  87. ctx.cp_hash_path = value;
  88. break;
  89. /* no default */
  90. }
  91. return true;
  92. }
  93. static bool tpm2_tool_onstart(tpm2_options **opts) {
  94. const struct option topts[] = {
  95. { "hierarchy", required_argument, NULL, 'C' },
  96. { "output", required_argument, NULL, 'o' },
  97. { "size", required_argument, NULL, 's' },
  98. { "offset", required_argument, NULL, 0 },
  99. { "cphash", required_argument, NULL, 1 },
  100. { "auth", required_argument, NULL, 'P' },
  101. };
  102. *opts = tpm2_options_new("C:s:o:P:", ARRAY_LEN(topts), topts, on_option,
  103. on_arg, 0);
  104. return *opts != NULL;
  105. }
  106. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  107. UNUSED(flags);
  108. tool_rc rc = tpm2_util_object_load_auth(ectx, ctx.auth_hierarchy.ctx_path,
  109. ctx.auth_hierarchy.auth_str, &ctx.auth_hierarchy.object, false,
  110. TPM2_HANDLE_FLAGS_NV | TPM2_HANDLE_FLAGS_O | TPM2_HANDLE_FLAGS_P);
  111. if (rc != tool_rc_success) {
  112. LOG_ERR("Invalid handle authorization");
  113. return rc;
  114. }
  115. if (!ctx.cp_hash_path) {
  116. return nv_read(ectx, flags, NULL);
  117. }
  118. TPM2B_DIGEST cp_hash = { .size = 0 };
  119. rc = nv_read(ectx, flags, &cp_hash);
  120. if (rc != tool_rc_success) {
  121. LOG_ERR("CpHash calculation failed!");
  122. return rc;
  123. }
  124. bool result = files_save_digest(&cp_hash, ctx.cp_hash_path);
  125. if (!result) {
  126. rc = tool_rc_general_error;
  127. }
  128. return rc;
  129. }
  130. static tool_rc tpm2_tool_onstop(ESYS_CONTEXT *ectx) {
  131. UNUSED(ectx);
  132. return tpm2_session_close(&ctx.auth_hierarchy.object.session);
  133. }
  134. // Register this tool with tpm2_tool.c
  135. TPM2_TOOL_REGISTER("nvread", tpm2_tool_onstart, tpm2_tool_onrun, tpm2_tool_onstop, NULL)