tpm2_nvextend.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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_nvextend_ctx tpm_nvextend_ctx;
  10. #define MAX_SESSIONS 3
  11. #define MAX_AUX_SESSIONS 2
  12. struct tpm_nvextend_ctx {
  13. /*
  14. * Inputs
  15. */
  16. struct {
  17. const char *ctx_path;
  18. const char *auth_str;
  19. tpm2_loaded_object object;
  20. } auth_hierarchy;
  21. const char *input_path;
  22. TPM2_HANDLE nv_index;
  23. TPM2B_MAX_NV_BUFFER data;
  24. /*
  25. * Outputs
  26. */
  27. /*
  28. * Parameter hashes
  29. */
  30. const char *cp_hash_path;
  31. TPM2B_DIGEST cp_hash;
  32. const char *rp_hash_path;
  33. TPM2B_DIGEST rp_hash;
  34. bool is_command_dispatch;
  35. TPMI_ALG_HASH parameter_hash_algorithm;
  36. /*
  37. * Aux sessions
  38. */
  39. uint8_t aux_session_cnt;
  40. tpm2_session *aux_session[MAX_AUX_SESSIONS];
  41. const char *aux_session_path[MAX_AUX_SESSIONS];
  42. ESYS_TR aux_session_handle[MAX_AUX_SESSIONS];
  43. };
  44. static tpm_nvextend_ctx ctx = {
  45. .data.size = sizeof(ctx.data.buffer),
  46. .aux_session_handle[0] = ESYS_TR_NONE,
  47. .aux_session_handle[1] = ESYS_TR_NONE,
  48. .parameter_hash_algorithm = TPM2_ALG_ERROR,
  49. };
  50. static tool_rc nvextend(ESYS_CONTEXT *ectx) {
  51. /*
  52. * 1. TPM2_CC_<command> OR Retrieve cpHash
  53. */
  54. return tpm2_nvextend(ectx, &ctx.auth_hierarchy.object, ctx.nv_index,
  55. &ctx.data, &ctx.cp_hash, &ctx.rp_hash, ctx.parameter_hash_algorithm,
  56. ctx.aux_session_handle[0], ctx.aux_session_handle[1]);
  57. }
  58. static tool_rc process_output(ESYS_CONTEXT *ectx) {
  59. UNUSED(ectx);
  60. /*
  61. * 1. Outputs that do not require TPM2_CC_<command> dispatch
  62. */
  63. bool is_file_op_success = true;
  64. if (ctx.cp_hash_path) {
  65. is_file_op_success = files_save_digest(&ctx.cp_hash, ctx.cp_hash_path);
  66. if (!is_file_op_success) {
  67. return tool_rc_general_error;
  68. }
  69. }
  70. if (!ctx.is_command_dispatch) {
  71. return tool_rc_success;
  72. }
  73. /*
  74. * 2. Outputs generated after TPM2_CC_<command> dispatch
  75. */
  76. if (ctx.rp_hash_path) {
  77. is_file_op_success = files_save_digest(&ctx.rp_hash, ctx.rp_hash_path);
  78. }
  79. return is_file_op_success ? tool_rc_success : tool_rc_general_error;
  80. }
  81. static tool_rc process_inputs(ESYS_CONTEXT *ectx) {
  82. /*
  83. * 1. Object and auth initializations
  84. */
  85. /*
  86. * 1.a Add the new-auth values to be set for the object.
  87. */
  88. /*
  89. * 1.b Add object names and their auth sessions
  90. */
  91. tool_rc rc = tpm2_util_object_load_auth(ectx, ctx.auth_hierarchy.ctx_path,
  92. ctx.auth_hierarchy.auth_str, &ctx.auth_hierarchy.object, false,
  93. TPM2_HANDLE_FLAGS_NV | TPM2_HANDLE_FLAGS_O | TPM2_HANDLE_FLAGS_P);
  94. if (rc != tool_rc_success) {
  95. LOG_ERR("Invalid handle authorization");
  96. return rc;
  97. }
  98. /*
  99. * 2. Restore auxiliary sessions
  100. */
  101. rc = tpm2_util_aux_sessions_setup(ectx, ctx.aux_session_cnt,
  102. ctx.aux_session_path, ctx.aux_session_handle, ctx.aux_session);
  103. if (rc != tool_rc_success) {
  104. return rc;
  105. }
  106. /*
  107. * 3. Command specific initializations dependent on loaded objects
  108. */
  109. ctx.input_path = strcmp(ctx.input_path, "-") ? ctx.input_path : NULL;
  110. bool result = files_load_bytes_from_buffer_or_file_or_stdin(NULL,
  111. ctx.input_path, &ctx.data.size, ctx.data.buffer);
  112. if (!result) {
  113. return tool_rc_general_error;
  114. }
  115. /*
  116. * 4. Configuration for calculating the pHash
  117. */
  118. /* 4.a Determine pHash length and alg */
  119. tpm2_session *all_sessions[MAX_SESSIONS] = {
  120. ctx.auth_hierarchy.object.session,
  121. ctx.aux_session[0],
  122. ctx.aux_session[1]
  123. };
  124. const char **cphash_path = ctx.cp_hash_path ? &ctx.cp_hash_path : 0;
  125. const char **rphash_path = ctx.rp_hash_path ? &ctx.rp_hash_path : 0;
  126. ctx.parameter_hash_algorithm = tpm2_util_calculate_phash_algorithm(ectx,
  127. cphash_path, &ctx.cp_hash, rphash_path, &ctx.rp_hash, all_sessions);
  128. /*
  129. * 4.b Determine if TPM2_CC_<command> is to be dispatched
  130. * !rphash && !cphash [Y]
  131. * !rphash && cphash [N]
  132. * rphash && !cphash [Y]
  133. * rphash && cphash [Y]
  134. */
  135. ctx.is_command_dispatch = (ctx.cp_hash_path && !ctx.rp_hash_path) ?
  136. false : true;
  137. return rc;
  138. }
  139. static tool_rc check_options(void) {
  140. if (!ctx.input_path) {
  141. LOG_ERR("Must specify source of data to write to NV index");
  142. return tool_rc_general_error;
  143. }
  144. return tool_rc_success;
  145. }
  146. static bool on_option(char key, char *value) {
  147. switch (key) {
  148. case 'C':
  149. ctx.auth_hierarchy.ctx_path = value;
  150. break;
  151. case 'P':
  152. ctx.auth_hierarchy.auth_str = value;
  153. break;
  154. case 'i':
  155. ctx.input_path = value;
  156. break;
  157. case 0:
  158. ctx.cp_hash_path = value;
  159. break;
  160. case 1:
  161. ctx.rp_hash_path = value;
  162. break;
  163. case 'S':
  164. ctx.aux_session_path[ctx.aux_session_cnt] = value;
  165. if (ctx.aux_session_cnt < MAX_AUX_SESSIONS) {
  166. ctx.aux_session_cnt++;
  167. } else {
  168. return false;
  169. }
  170. break;
  171. }
  172. return true;
  173. }
  174. static bool on_arg(int argc, char **argv) {
  175. /*
  176. * If the user doesn't specify an authorization hierarchy use the index.
  177. */
  178. if (!ctx.auth_hierarchy.ctx_path) {
  179. ctx.auth_hierarchy.ctx_path = argv[0];
  180. }
  181. return on_arg_nv_index(argc, argv, &ctx.nv_index);
  182. }
  183. static bool tpm2_tool_onstart(tpm2_options **opts) {
  184. const struct option topts[] = {
  185. { "hierarchy", required_argument, NULL, 'C' },
  186. { "auth", required_argument, NULL, 'P' },
  187. { "input", required_argument, NULL, 'i' },
  188. { "cphash", required_argument, NULL, 0 },
  189. { "rphash", required_argument, NULL, 1 },
  190. { "session", required_argument, NULL, 'S' },
  191. };
  192. *opts = tpm2_options_new("S:C:P:i:", ARRAY_LEN(topts), topts, on_option,
  193. on_arg, 0);
  194. return *opts != NULL;
  195. }
  196. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  197. UNUSED(flags);
  198. /*
  199. * 1. Process options
  200. */
  201. tool_rc rc = check_options();
  202. if (rc != tool_rc_success) {
  203. return rc;
  204. }
  205. /*
  206. * 2. Process inputs
  207. */
  208. rc = process_inputs(ectx);
  209. if (rc != tool_rc_success) {
  210. return rc;
  211. }
  212. /*
  213. * 3. TPM2_CC_<command> call
  214. */
  215. rc = nvextend(ectx);
  216. if (rc != tool_rc_success) {
  217. return rc;
  218. }
  219. /*
  220. * 4. Process outputs
  221. */
  222. return process_output(ectx);
  223. }
  224. static tool_rc tpm2_tool_onstop(ESYS_CONTEXT *ectx) {
  225. UNUSED(ectx);
  226. /*
  227. * 1. Free objects
  228. */
  229. /*
  230. * 2. Close authorization sessions
  231. */
  232. tool_rc rc = tool_rc_success;
  233. tool_rc tmp_rc = tpm2_session_close(&ctx.auth_hierarchy.object.session);
  234. if (tmp_rc != tool_rc_success) {
  235. rc = tmp_rc;
  236. }
  237. /*
  238. * 3. Close auxiliary sessions
  239. */
  240. uint8_t i = 0;
  241. for(i = 0; i < ctx.aux_session_cnt; i++) {
  242. if (ctx.aux_session_path[i]) {
  243. tmp_rc = tpm2_session_close(&ctx.aux_session[i]);
  244. }
  245. if (tmp_rc != tool_rc_success) {
  246. rc = tmp_rc;
  247. }
  248. }
  249. return rc;
  250. }
  251. // Register this tool with tpm2_tool.c
  252. TPM2_TOOL_REGISTER("nvextend", tpm2_tool_onstart, tpm2_tool_onrun,
  253. tpm2_tool_onstop, NULL)