tpm2_getpolicydigest.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 "log.h"
  8. #include "tpm2.h"
  9. #include "tpm2_tool.h"
  10. typedef struct tpm_getpolicydigest_ctx tpm_getpolicydigest_ctx;
  11. struct tpm_getpolicydigest_ctx {
  12. /*
  13. * Input options
  14. */
  15. bool hex;
  16. /*
  17. * Outputs
  18. */
  19. const char *output_file;
  20. TPM2B_DIGEST *policy_digest;
  21. /*
  22. * Parameter hashes
  23. */
  24. /*
  25. * Aux Sessions
  26. */
  27. tpm2_session *session;
  28. const char *session_path;
  29. ESYS_TR session_handle;
  30. };
  31. static tpm_getpolicydigest_ctx ctx;
  32. static tool_rc get_policydigest(ESYS_CONTEXT *ectx) {
  33. /*
  34. * 1. TPM2_CC_<command> OR Retrieve cpHash
  35. */
  36. tool_rc rc = tpm2_policy_getdigest(ectx, ctx.session_handle,
  37. ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE, &ctx.policy_digest);
  38. if (rc != tool_rc_success) {
  39. LOG_ERR("Failed getrandom");
  40. }
  41. return rc;
  42. }
  43. static tool_rc process_outputs(void) {
  44. /*
  45. * 1. Outputs that do not require TPM2_CC_<command> dispatch
  46. */
  47. /*
  48. * 2. Outputs generated after TPM2_CC_<command> dispatch
  49. */
  50. /*
  51. * Either open an output file, or if stdout, do nothing as -Q
  52. * was specified.
  53. */
  54. tool_rc rc = tool_rc_success;
  55. FILE *out = stdout;
  56. if (ctx.output_file) {
  57. out = fopen(ctx.output_file, "wb+");
  58. if (!out) {
  59. LOG_ERR("Could not open output file \"%s\", error: %s",
  60. ctx.output_file, strerror(errno));
  61. rc = tool_rc_general_error;
  62. goto out;
  63. }
  64. } else if (!output_enabled) {
  65. goto out;
  66. }
  67. if (ctx.hex) {
  68. tpm2_util_print_tpm2b2(out, ctx.policy_digest);
  69. goto out;
  70. }
  71. bool is_file_op_success = files_write_bytes(out, ctx.policy_digest->buffer,
  72. ctx.policy_digest->size);
  73. if (!is_file_op_success) {
  74. rc = tool_rc_general_error;
  75. }
  76. out:
  77. if (out && out != stdout) {
  78. fclose(out);
  79. }
  80. return rc;
  81. }
  82. static tool_rc process_inputs(ESYS_CONTEXT *ectx) {
  83. /*
  84. * 1. Object and auth initializations
  85. */
  86. /*
  87. * 1.a Add the new-auth values to be set for the object.
  88. */
  89. /*
  90. * 1.b Add object names and their auth sessions
  91. * Note: Old-auth value is ignored when calculating cpHash.
  92. */
  93. /*
  94. * 2. Restore auxiliary sessions
  95. */
  96. /*
  97. * 3. Command specific initializations
  98. */
  99. tool_rc rc = tool_rc_success;
  100. TPM2_HANDLE handle;
  101. bool result = tpm2_util_string_to_uint32(ctx.session_path, &handle);
  102. if (result) {
  103. rc = tpm2_util_sys_handle_to_esys_handle(ectx, handle,
  104. &ctx.session_handle);
  105. if (rc != tool_rc_success) {
  106. return rc;
  107. }
  108. } else {
  109. rc = tpm2_session_restore(ectx, ctx.session_path, false, &ctx.session);
  110. if (rc != tool_rc_success) {
  111. return rc;
  112. }
  113. ctx.session_handle = tpm2_session_get_handle(ctx.session);
  114. }
  115. /*
  116. * 4. Configuration for calculating the pHash
  117. */
  118. /*
  119. * 4.a Determine pHash length and alg
  120. */
  121. /*
  122. * 4.b Determine if TPM2_CC_<command> is to be dispatched
  123. */
  124. return tool_rc_success;
  125. }
  126. static tool_rc check_options(void) {
  127. if (!ctx.session_path) {
  128. LOG_ERR("Specify the session context.");
  129. return tool_rc_option_error;
  130. }
  131. return tool_rc_success;
  132. }
  133. static bool on_option(char key, char *value) {
  134. UNUSED(key);
  135. switch (key) {
  136. case 'o':
  137. ctx.output_file = value;
  138. break;
  139. case 0:
  140. ctx.hex = true;
  141. break;
  142. case 'S':
  143. ctx.session_path = value;
  144. break;
  145. /* no default */
  146. }
  147. return true;
  148. }
  149. static bool tpm2_tool_onstart(tpm2_options **opts) {
  150. const struct option topts[] = {
  151. { "output", required_argument, NULL, 'o' },
  152. { "hex", no_argument, NULL, 0 },
  153. { "session", required_argument, NULL, 'S' },
  154. };
  155. *opts = tpm2_options_new("S:o:", ARRAY_LEN(topts), topts, on_option, 0, 0);
  156. return *opts != NULL;
  157. }
  158. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  159. UNUSED(flags);
  160. /*
  161. * 1. Process options
  162. */
  163. tool_rc rc = check_options();
  164. if (rc != tool_rc_success) {
  165. return rc;
  166. }
  167. /*
  168. * 2. Process inputs
  169. */
  170. rc = process_inputs(ectx);
  171. if (rc != tool_rc_success) {
  172. return rc;
  173. }
  174. /*
  175. * 3. TPM2_CC_<command> call
  176. */
  177. rc = get_policydigest(ectx);
  178. if (rc != tool_rc_success) {
  179. return rc;
  180. }
  181. /*
  182. * 4. Process outputs
  183. */
  184. return process_outputs();
  185. }
  186. static tool_rc tpm2_tool_onstop(ESYS_CONTEXT *ectx) {
  187. UNUSED(ectx);
  188. /*
  189. * 1. Free objects
  190. */
  191. free(ctx.policy_digest);
  192. /*
  193. * 2. Close authorization sessions
  194. */
  195. return tpm2_session_close(&ctx.session);
  196. /*
  197. * 3. Close auxiliary sessions
  198. */
  199. }
  200. // Register this tool with tpm2_tool.c
  201. TPM2_TOOL_REGISTER("getpolicydigest", tpm2_tool_onstart, tpm2_tool_onrun,
  202. tpm2_tool_onstop, NULL)