tpm2_nvwrite.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <errno.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "files.h"
  7. #include "tpm2_nv_util.h"
  8. #include "tpm2_tool.h"
  9. typedef struct tpm_nvwrite_ctx tpm_nvwrite_ctx;
  10. struct tpm_nvwrite_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. BYTE nv_buffer[TPM2_MAX_NV_BUFFER_SIZE];
  18. FILE *input_file;
  19. UINT16 data_size;
  20. UINT16 offset;
  21. char *cp_hash_path;
  22. };
  23. static tpm_nvwrite_ctx ctx = {
  24. .data_size = TPM2_MAX_NV_BUFFER_SIZE
  25. };
  26. static bool is_input_options_args_valid(ESYS_CONTEXT *ectx) {
  27. if (ctx.cp_hash_path && ctx.data_size > TPM2_MAX_NV_BUFFER_SIZE) {
  28. LOG_ERR("Cannot calculat cpHash for buffers larger than NV max buffer");
  29. return false;
  30. }
  31. if (!ctx.data_size) {
  32. LOG_WARN("Data to write is of size 0");
  33. }
  34. /*
  35. * Ensure that writes will fit before attempting write to prevent data
  36. * from being partially written to the index.
  37. */
  38. TPM2B_NV_PUBLIC *nv_public = NULL;
  39. tool_rc rc = tpm2_util_nv_read_public(ectx, ctx.nv_index, &nv_public);
  40. if (rc != tool_rc_success) {
  41. LOG_ERR("Failed to write NVRAM public area at index 0x%X",
  42. ctx.nv_index);
  43. free(nv_public);
  44. return false;
  45. }
  46. if (ctx.offset + ctx.data_size > nv_public->nvPublic.dataSize) {
  47. LOG_ERR("The starting offset (%u) and the size (%u) are larger than the"
  48. " defined space: %u.", ctx.offset, ctx.data_size,
  49. nv_public->nvPublic.dataSize);
  50. free(nv_public);
  51. return false;
  52. }
  53. free(nv_public);
  54. return true;
  55. }
  56. static tool_rc nv_write(ESYS_CONTEXT *ectx) {
  57. TPM2B_MAX_NV_BUFFER nv_write_data;
  58. UINT16 data_offset = 0;
  59. if (ctx.cp_hash_path) {
  60. nv_write_data.size = ctx.data_size;
  61. memcpy(nv_write_data.buffer, &ctx.nv_buffer, ctx.data_size);
  62. LOG_WARN("Calculating cpHash. Exiting without performing write.");
  63. TPM2B_DIGEST cp_hash = { .size = 0 };
  64. tool_rc rc = tpm2_nvwrite(ectx, &ctx.auth_hierarchy.object, ctx.nv_index,
  65. &nv_write_data, ctx.offset, &cp_hash);
  66. if (rc != tool_rc_success) {
  67. LOG_ERR("CpHash calculation failed!");
  68. return rc;
  69. }
  70. bool result = files_save_digest(&cp_hash, ctx.cp_hash_path);
  71. if (!result) {
  72. rc = tool_rc_general_error;
  73. }
  74. return rc;
  75. }
  76. UINT32 max_data_size;
  77. tool_rc rc = tpm2_util_nv_max_buffer_size(ectx, &max_data_size);
  78. if (rc != tool_rc_success) {
  79. return rc;
  80. }
  81. if (max_data_size > TPM2_MAX_NV_BUFFER_SIZE) {
  82. max_data_size = TPM2_MAX_NV_BUFFER_SIZE;
  83. } else if (max_data_size == 0) {
  84. max_data_size = NV_DEFAULT_BUFFER_SIZE;
  85. }
  86. while (ctx.data_size > 0) {
  87. nv_write_data.size =
  88. ctx.data_size > max_data_size ? max_data_size : ctx.data_size;
  89. LOG_INFO("The data(size=%d) to be written:", nv_write_data.size);
  90. memcpy(nv_write_data.buffer, &ctx.nv_buffer[data_offset],
  91. nv_write_data.size);
  92. rc = tpm2_nvwrite(ectx, &ctx.auth_hierarchy.object, ctx.nv_index,
  93. &nv_write_data, ctx.offset + data_offset, NULL);
  94. if (rc != tool_rc_success) {
  95. return rc;
  96. }
  97. ctx.data_size -= nv_write_data.size;
  98. data_offset += nv_write_data.size;
  99. }
  100. return tool_rc_success;
  101. }
  102. static bool on_option(char key, char *value) {
  103. char *input_file;
  104. switch (key) {
  105. case 'C':
  106. ctx.auth_hierarchy.ctx_path = value;
  107. break;
  108. case 'P':
  109. ctx.auth_hierarchy.auth_str = value;
  110. break;
  111. case 'i':
  112. input_file = strcmp("-", value) ? value : NULL;
  113. return files_load_bytes_from_buffer_or_file_or_stdin(NULL, input_file,
  114. &ctx.data_size, ctx.nv_buffer);
  115. break;
  116. case 0:
  117. if (!tpm2_util_string_to_uint16(value, &ctx.offset)) {
  118. LOG_ERR("Could not convert starting offset, got: \"%s\"", value);
  119. return false;
  120. }
  121. break;
  122. case 1:
  123. ctx.cp_hash_path = value;
  124. break;
  125. }
  126. return true;
  127. }
  128. static bool on_arg(int argc, char **argv) {
  129. /* If the user doesn't specify an authorization hierarchy use the index
  130. * passed to -x/--index for the authorization index.
  131. */
  132. if (!ctx.auth_hierarchy.ctx_path) {
  133. ctx.auth_hierarchy.ctx_path = argv[0];
  134. }
  135. return on_arg_nv_index(argc, argv, &ctx.nv_index);
  136. }
  137. static bool tpm2_tool_onstart(tpm2_options **opts) {
  138. const struct option topts[] = {
  139. { "hierarchy", required_argument, NULL, 'C' },
  140. { "auth", required_argument, NULL, 'P' },
  141. { "input", required_argument, NULL, 'i' },
  142. { "offset", required_argument, NULL, 0 },
  143. { "cphash", required_argument, NULL, 1 },
  144. };
  145. *opts = tpm2_options_new("C:P:i:", ARRAY_LEN(topts), topts, on_option,
  146. on_arg, 0);
  147. return *opts != NULL;
  148. }
  149. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  150. UNUSED(flags);
  151. bool retval = is_input_options_args_valid(ectx);
  152. if (!retval) {
  153. return tool_rc_option_error;
  154. }
  155. tool_rc rc = tpm2_util_object_load_auth(ectx, ctx.auth_hierarchy.ctx_path,
  156. ctx.auth_hierarchy.auth_str, &ctx.auth_hierarchy.object, false,
  157. TPM2_HANDLE_FLAGS_NV | TPM2_HANDLE_FLAGS_O | TPM2_HANDLE_FLAGS_P);
  158. if (rc != tool_rc_success) {
  159. LOG_ERR("Invalid handle authorization");
  160. return rc;
  161. }
  162. return nv_write(ectx);
  163. }
  164. static tool_rc tpm2_tool_onstop(ESYS_CONTEXT *ectx) {
  165. UNUSED(ectx);
  166. return tpm2_session_close(&ctx.auth_hierarchy.object.session);
  167. }
  168. // Register this tool with tpm2_tool.c
  169. TPM2_TOOL_REGISTER("nvwrite", tpm2_tool_onstart, tpm2_tool_onrun, tpm2_tool_onstop, NULL)