tpm2_policysecret.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <stdbool.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "files.h"
  6. #include "log.h"
  7. #include "tpm2_policy.h"
  8. #include "tpm2_tool.h"
  9. typedef struct tpm2_policysecret_ctx tpm2_policysecret_ctx;
  10. struct tpm2_policysecret_ctx {
  11. struct {
  12. const char *ctx_path; //auth_entity.ctx_path
  13. const char *auth_str; //auth_str
  14. tpm2_loaded_object object; //context_object && pwd_session
  15. } auth_entity;
  16. //File path for storing the policy digest output
  17. const char *policy_digest_path;
  18. //File path for the session context data
  19. const char *extended_session_path;
  20. tpm2_session *extended_session;
  21. INT32 expiration;
  22. char *policy_ticket_path;
  23. char *policy_timeout_path;
  24. const char *qualifier_data_arg;
  25. bool is_nonce_tpm;
  26. struct {
  27. UINT8 c :1;
  28. } flags;
  29. char *cp_hash_path;
  30. };
  31. static tpm2_policysecret_ctx ctx;
  32. static bool on_option(char key, char *value) {
  33. bool result = true;
  34. switch (key) {
  35. case 'L':
  36. ctx.policy_digest_path = value;
  37. break;
  38. case 'S':
  39. ctx.extended_session_path = value;
  40. break;
  41. case 'c':
  42. ctx.auth_entity.ctx_path = value;
  43. ctx.flags.c = 1;
  44. break;
  45. case 0:
  46. ctx.policy_ticket_path = value;
  47. break;
  48. case 1:
  49. ctx.policy_timeout_path = value;
  50. break;
  51. case 't':
  52. result = tpm2_util_string_to_int32(value, &ctx.expiration);
  53. if (!result) {
  54. LOG_ERR("Failed reading expiration duration from value, got:\"%s\"",
  55. value);
  56. return false;
  57. }
  58. break;
  59. case 'x':
  60. ctx.is_nonce_tpm = true;
  61. break;
  62. case 'q':
  63. ctx.qualifier_data_arg = value;
  64. break;
  65. case 2:
  66. ctx.cp_hash_path = value;
  67. break;
  68. }
  69. return result;
  70. }
  71. static bool on_arg(int argc, char **argv) {
  72. if (argc > 1) {
  73. LOG_ERR("Specify a single auth value");
  74. return false;
  75. }
  76. if (!argc) {
  77. //empty auth
  78. return true;
  79. }
  80. ctx.auth_entity.auth_str = argv[0];
  81. return true;
  82. }
  83. static bool tpm2_tool_onstart(tpm2_options **opts) {
  84. static struct option topts[] = {
  85. { "policy", required_argument, NULL, 'L' },
  86. { "session", required_argument, NULL, 'S' },
  87. { "object-context", required_argument, NULL, 'c' },
  88. { "expiration", required_argument, NULL, 't' },
  89. { "nonce-tpm", no_argument, NULL, 'x' },
  90. { "ticket", required_argument, NULL, 0 },
  91. { "timeout", required_argument, NULL, 1 },
  92. { "qualification", required_argument, NULL, 'q' },
  93. { "cphash", required_argument, NULL, 2 },
  94. };
  95. *opts = tpm2_options_new("L:S:c:t:q:x", ARRAY_LEN(topts), topts, on_option,
  96. on_arg, 0);
  97. return *opts != NULL;
  98. }
  99. static bool is_input_option_args_valid(void) {
  100. if (!ctx.extended_session_path) {
  101. LOG_ERR("Must specify -S session file.");
  102. return false;
  103. }
  104. if (!ctx.flags.c) {
  105. LOG_ERR("Must specify -c handle-id/ context file path.");
  106. return false;
  107. }
  108. if (ctx.cp_hash_path && ctx.policy_digest_path) {
  109. LOG_WARN("Cannot output policyhash when calculating cphash.");
  110. return false;
  111. }
  112. return true;
  113. }
  114. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  115. UNUSED(flags);
  116. bool result = is_input_option_args_valid();
  117. if (!result) {
  118. return tool_rc_option_error;
  119. }
  120. tool_rc rc = tpm2_session_restore(ectx, ctx.extended_session_path, false,
  121. &ctx.extended_session);
  122. if (rc != tool_rc_success) {
  123. return rc;
  124. }
  125. /*
  126. * The auth string of the referenced object is strictly for a password session
  127. */
  128. rc = tpm2_util_object_load_auth(ectx, ctx.auth_entity.ctx_path,
  129. ctx.auth_entity.auth_str, &ctx.auth_entity.object, false,
  130. TPM2_HANDLE_ALL_W_NV);
  131. if (rc != tool_rc_success) {
  132. return rc;
  133. }
  134. if (!ctx.cp_hash_path) {
  135. /*
  136. * Build a policysecret using the pwd session. If the event of
  137. * a failure:
  138. * 1. always close the pwd session.
  139. * 2. log the policy secret failure and return tool_rc_general_error.
  140. * 3. if the error was closing the policy secret session, return that rc.
  141. */
  142. TPMT_TK_AUTH *policy_ticket = NULL;
  143. TPM2B_TIMEOUT *timeout = NULL;
  144. rc = tpm2_policy_build_policysecret(ectx, ctx.extended_session,
  145. &ctx.auth_entity.object, ctx.expiration, &policy_ticket, &timeout,
  146. ctx.is_nonce_tpm, ctx.qualifier_data_arg, NULL);
  147. tool_rc rc2 = tpm2_session_close(&ctx.auth_entity.object.session);
  148. if (rc != tool_rc_success) {
  149. goto tpm2_tool_onrun_out;
  150. }
  151. if (rc2 != tool_rc_success) {
  152. rc = rc2;
  153. goto tpm2_tool_onrun_out;
  154. }
  155. rc = tpm2_policy_tool_finish(ectx, ctx.extended_session,
  156. ctx.policy_digest_path);
  157. if (rc != tool_rc_success) {
  158. goto tpm2_tool_onrun_out;
  159. }
  160. if (ctx.policy_timeout_path) {
  161. if(!timeout->size) {
  162. LOG_WARN("Policy assertion did not produce timeout");
  163. } else {
  164. result = files_save_bytes_to_file(ctx.policy_timeout_path,
  165. timeout->buffer, timeout->size);
  166. }
  167. }
  168. if (!result) {
  169. LOG_ERR("Failed to save timeout to file.");
  170. rc = tool_rc_general_error;
  171. goto tpm2_tool_onrun_out;
  172. }
  173. if (ctx.policy_ticket_path) {
  174. if (!policy_ticket->digest.size) {
  175. LOG_WARN("Policy assertion did not produce auth ticket.");
  176. } else {
  177. result = files_save_authorization_ticket(policy_ticket,
  178. ctx.policy_ticket_path);
  179. }
  180. }
  181. if (!result) {
  182. LOG_ERR("Failed to save auth ticket");
  183. rc = tool_rc_general_error;
  184. }
  185. tpm2_tool_onrun_out:
  186. free(policy_ticket);
  187. free(timeout);
  188. if (rc != tool_rc_success) {
  189. LOG_ERR("Could not build policysecret");
  190. }
  191. return rc;
  192. }
  193. TPM2B_DIGEST cp_hash = { .size = 0 };
  194. rc = tpm2_policy_build_policysecret(ectx, ctx.extended_session,
  195. &ctx.auth_entity.object, ctx.expiration, NULL, NULL, ctx.is_nonce_tpm,
  196. ctx.qualifier_data_arg, &cp_hash);
  197. if (rc != tool_rc_success) {
  198. LOG_ERR("Failed cphash calculation operation");
  199. return rc;
  200. }
  201. result = files_save_digest(&cp_hash, ctx.cp_hash_path);
  202. if (!result) {
  203. LOG_ERR("Failed saving command parameter hash for policysecret");
  204. rc = tool_rc_general_error;
  205. }
  206. return rc;
  207. }
  208. static tool_rc tpm2_tool_onstop(ESYS_CONTEXT *ectx) {
  209. UNUSED(ectx);
  210. return tpm2_session_close(&ctx.extended_session);
  211. }
  212. // Register this tool with tpm2_tool.c
  213. TPM2_TOOL_REGISTER("policysecret", tpm2_tool_onstart, tpm2_tool_onrun, tpm2_tool_onstop, NULL)