tpm2_policynv.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <stdlib.h>
  3. #include "files.h"
  4. #include "tpm2.h"
  5. #include "tpm2_tool.h"
  6. #include "tpm2_nv_util.h"
  7. #include "tpm2_options.h"
  8. #include "tpm2_policy.h"
  9. typedef struct tpm_policynv_ctx tpm_policynv_ctx;
  10. struct tpm_policynv_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. const char *policy_digest_path;
  18. const char *session_path;
  19. tpm2_session *session;
  20. TPM2B_OPERAND operand_b;
  21. UINT16 offset;
  22. TPM2_EO operation;
  23. char *cp_hash_path;
  24. };
  25. static tpm_policynv_ctx ctx = {
  26. .operand_b = { .size = BUFFER_SIZE(TPM2B_OPERAND, buffer) }
  27. };
  28. static bool on_arg(int argc, char **argv) {
  29. switch (argc) {
  30. case 2:
  31. break;
  32. default:
  33. goto on_arg_error;
  34. }
  35. uint8_t argv_index_ctr = argc;
  36. do {
  37. argv_index_ctr--;
  38. if (!strcmp(argv[argv_index_ctr], "eq")) {
  39. ctx.operation = TPM2_EO_EQ;
  40. } else if (!strcmp(argv[argv_index_ctr], "neq")) {
  41. ctx.operation = TPM2_EO_NEQ;
  42. } else if (!strcmp(argv[argv_index_ctr], "sgt")) {
  43. ctx.operation = TPM2_EO_SIGNED_GT;
  44. } else if (!strcmp(argv[argv_index_ctr], "ugt")) {
  45. ctx.operation = TPM2_EO_UNSIGNED_GT;
  46. } else if (!strcmp(argv[argv_index_ctr], "slt")) {
  47. ctx.operation = TPM2_EO_SIGNED_LT;
  48. } else if (!strcmp(argv[argv_index_ctr], "ult")) {
  49. ctx.operation = TPM2_EO_UNSIGNED_LT;
  50. } else if (!strcmp(argv[argv_index_ctr], "sge")) {
  51. ctx.operation = TPM2_EO_SIGNED_GE;
  52. } else if (!strcmp(argv[argv_index_ctr], "uge")) {
  53. ctx.operation = TPM2_EO_UNSIGNED_GE;
  54. } else if (!strcmp(argv[argv_index_ctr], "sle")) {
  55. ctx.operation = TPM2_EO_SIGNED_LE;
  56. } else if (!strcmp(argv[argv_index_ctr], "ule")) {
  57. ctx.operation = TPM2_EO_UNSIGNED_LE;
  58. } else if (!strcmp(argv[argv_index_ctr], "bs")) {
  59. ctx.operation = TPM2_EO_BITSET;
  60. } else if (!strcmp(argv[argv_index_ctr], "bc")) {
  61. ctx.operation = TPM2_EO_BITCLEAR;
  62. } else {
  63. // Process it as NV index instead
  64. /*
  65. * Use the index as an authorization hierarchy If the user doesn't specify
  66. */
  67. if (!ctx.auth_hierarchy.ctx_path) {
  68. ctx.auth_hierarchy.ctx_path = argv[argv_index_ctr];
  69. }
  70. return on_arg_nv_index( 1, argv, &ctx.nv_index); //Only 1 arg for NV index
  71. }
  72. } while(argv_index_ctr > 0); // Loop to iterate through the two arguments
  73. /*
  74. * Invalid argument specified
  75. */
  76. on_arg_error:
  77. LOG_ERR("Specify 2 arguments - NV-Index and Comparison-Operartion");
  78. return false;
  79. }
  80. static bool on_option(char key, char *value) {
  81. bool result = false;
  82. char *input_file;
  83. switch (key) {
  84. case 'C':
  85. ctx.auth_hierarchy.ctx_path = value;
  86. break;
  87. case 'P':
  88. ctx.auth_hierarchy.auth_str = value;
  89. break;
  90. case 'L':
  91. ctx.policy_digest_path = value;
  92. break;
  93. case 'S':
  94. ctx.session_path = value;
  95. break;
  96. case 'i':
  97. input_file = strcmp("-", value) ? value : NULL;
  98. if (input_file) {
  99. result = files_get_file_size_path(value,
  100. (long unsigned *) &ctx.operand_b.size);
  101. }
  102. if (input_file && !result) {
  103. return false;
  104. }
  105. result = files_load_bytes_from_buffer_or_file_or_stdin(NULL, input_file,
  106. &ctx.operand_b.size,
  107. ctx.operand_b.buffer);
  108. if (!result) {
  109. return false;
  110. }
  111. break;
  112. case 0:
  113. if (!tpm2_util_string_to_uint16(value, &ctx.offset)) {
  114. LOG_ERR("Could not convert starting offset, got: \"%s\"", value);
  115. return false;
  116. }
  117. break;
  118. case 1:
  119. ctx.cp_hash_path = value;
  120. break;
  121. default:
  122. return false;
  123. }
  124. return true;
  125. }
  126. static bool tpm2_tool_onstart(tpm2_options **opts) {
  127. const struct option topts[] = {
  128. { "hierarchy", required_argument, NULL, 'C' },
  129. { "auth", required_argument, NULL, 'P' },
  130. { "policy", required_argument, NULL, 'L' },
  131. { "session", required_argument, NULL, 'S' },
  132. { "input", required_argument, NULL, 'i' },
  133. { "offset", required_argument, NULL, 0 },
  134. { "cphash", required_argument, NULL, 1 },
  135. };
  136. *opts = tpm2_options_new("C:P:L:S:i:", ARRAY_LEN(topts), topts, on_option,
  137. on_arg, 0);
  138. return *opts != NULL;
  139. }
  140. static bool is_input_option_args_valid(void) {
  141. if (!ctx.session_path) {
  142. LOG_ERR("Must specify -S session file.");
  143. return false;
  144. }
  145. if (ctx.cp_hash_path && ctx.policy_digest_path) {
  146. LOG_WARN("Cannot output policyhash when calculating cphash.");
  147. return false;
  148. }
  149. return true;
  150. }
  151. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  152. UNUSED(flags);
  153. bool retval = is_input_option_args_valid();
  154. if (!retval) {
  155. return tool_rc_option_error;
  156. }
  157. if (!ctx.operand_b.size) {
  158. LOG_WARN("Data to compare is of size 0");
  159. }
  160. /*
  161. * Ensure that NV index is large enough to compare the size of input data.
  162. */
  163. TPM2B_NV_PUBLIC *nv_public = NULL;
  164. tool_rc rc = tpm2_util_nv_read_public(ectx, ctx.nv_index, &nv_public);
  165. if (rc != tool_rc_success) {
  166. LOG_ERR("Failed to access NVRAM public area at index 0x%X",
  167. ctx.nv_index);
  168. free(nv_public);
  169. return rc;
  170. }
  171. if (ctx.operand_b.size > nv_public->nvPublic.dataSize - ctx.offset) {
  172. LOG_ERR("The operand size is larger than NV data"
  173. " starting at the offset");
  174. free(nv_public);
  175. return tool_rc_general_error;
  176. }
  177. free(nv_public);
  178. rc = tpm2_util_object_load_auth(ectx, ctx.auth_hierarchy.ctx_path,
  179. ctx.auth_hierarchy.auth_str, &ctx.auth_hierarchy.object, false,
  180. TPM2_HANDLE_FLAGS_NV | TPM2_HANDLE_FLAGS_O | TPM2_HANDLE_FLAGS_P);
  181. if (rc != tool_rc_success) {
  182. LOG_ERR("Invalid handle authorization");
  183. return rc;
  184. }
  185. rc = tpm2_session_restore(ectx, ctx.session_path, false, &ctx.session);
  186. if (rc != tool_rc_success) {
  187. return rc;
  188. }
  189. ESYS_TR policy_session_handle = tpm2_session_get_handle(ctx.session);
  190. if (!ctx.cp_hash_path) {
  191. rc = tpm2_policy_nv(ectx, &ctx.auth_hierarchy.object, ctx.nv_index,
  192. policy_session_handle, &ctx.operand_b, ctx.offset, ctx.operation,
  193. NULL);
  194. if (rc != tool_rc_success) {
  195. return rc;
  196. }
  197. return tpm2_policy_tool_finish(ectx, ctx.session, ctx.policy_digest_path);
  198. }
  199. TPM2B_DIGEST cp_hash = { .size = 0 };
  200. rc = tpm2_policy_nv(ectx, &ctx.auth_hierarchy.object, ctx.nv_index,
  201. policy_session_handle, &ctx.operand_b, ctx.offset, ctx.operation,
  202. &cp_hash);
  203. if (rc != tool_rc_success) {
  204. goto cphash_error_out;
  205. }
  206. bool result = files_save_digest(&cp_hash, ctx.cp_hash_path);
  207. if (!result) {
  208. rc = tool_rc_general_error;
  209. } else {
  210. goto cphash_out;
  211. }
  212. cphash_error_out:
  213. LOG_ERR("Failed cphash calculation operation");
  214. cphash_out:
  215. return rc;
  216. }
  217. static tool_rc tpm2_tool_onstop(ESYS_CONTEXT *ectx) {
  218. UNUSED(ectx);
  219. tool_rc rc = tool_rc_success;
  220. tool_rc tmp_rc = tool_rc_success;
  221. if (!ctx.cp_hash_path) {
  222. tmp_rc = tpm2_session_close(&ctx.auth_hierarchy.object.session);
  223. if (tmp_rc != tool_rc_success) {
  224. rc = tmp_rc;
  225. }
  226. }
  227. tmp_rc = tpm2_session_close(&ctx.session);
  228. if (tmp_rc != tool_rc_success) {
  229. rc = tmp_rc;
  230. }
  231. return rc;
  232. }
  233. // Register this tool with tpm2_tool.c
  234. TPM2_TOOL_REGISTER("policynv", tpm2_tool_onstart, tpm2_tool_onrun, tpm2_tool_onstop, NULL)