tpm2_policysigned.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "files.h"
  5. #include "log.h"
  6. #include "tpm2_alg_util.h"
  7. #include "tpm2_convert.h"
  8. #include "tpm2_policy.h"
  9. #include "tpm2_tool.h"
  10. typedef struct tpm2_policysigned_ctx tpm2_policysigned_ctx;
  11. struct tpm2_policysigned_ctx {
  12. const char *session_path;
  13. tpm2_session *session;
  14. const char *policy_digest_path;
  15. TPMT_SIGNATURE signature;
  16. TPMI_ALG_SIG_SCHEME format;
  17. TPMI_ALG_HASH halg;
  18. char *sig_file_path;
  19. const char *context_arg;
  20. tpm2_loaded_object key_context_object;
  21. bool is_nonce_tpm;
  22. INT32 expiration;
  23. char *policy_ticket_path;
  24. char *policy_timeout_path;
  25. const char *raw_data_path;
  26. const char *cphash_path;
  27. const char *policy_qualifier_data;
  28. union {
  29. struct {
  30. UINT8 halg :1;
  31. UINT8 sig :1;
  32. UINT8 fmt :1;
  33. };
  34. UINT8 all;
  35. } flags;
  36. };
  37. static tpm2_policysigned_ctx ctx = {
  38. .signature = {
  39. .sigAlg = TPM2_ALG_RSASSA,
  40. .signature.rsassa.hash = TPM2_ALG_SHA256,
  41. },
  42. .halg = TPM2_ALG_SHA256
  43. };
  44. static bool on_option(char key, char *value) {
  45. bool result = true;
  46. switch (key) {
  47. case 'L':
  48. ctx.policy_digest_path = value;
  49. break;
  50. case 'S':
  51. ctx.session_path = value;
  52. break;
  53. case 'g':
  54. ctx.halg = tpm2_alg_util_from_optarg(value, tpm2_alg_util_flags_hash);
  55. if (ctx.halg == TPM2_ALG_ERROR) {
  56. LOG_ERR("Unable to convert algorithm, got: \"%s\"", value);
  57. return false;
  58. }
  59. ctx.flags.halg = 1;
  60. break;
  61. case 'f':
  62. ctx.format = tpm2_alg_util_from_optarg(value, tpm2_alg_util_flags_sig);
  63. if (ctx.format == TPM2_ALG_ERROR) {
  64. LOG_ERR("Unknown signing scheme, got: \"%s\"", value);
  65. return false;
  66. }
  67. ctx.flags.fmt = 1;
  68. break;
  69. case 's':
  70. ctx.sig_file_path = value;
  71. ctx.flags.sig = 1;
  72. break;
  73. case 'c':
  74. ctx.context_arg = value;
  75. break;
  76. case 'q':
  77. ctx.policy_qualifier_data = value;
  78. break;
  79. case 0:
  80. ctx.policy_ticket_path = value;
  81. break;
  82. case 1:
  83. ctx.policy_timeout_path = value;
  84. break;
  85. case 2:
  86. ctx.raw_data_path = value;
  87. break;
  88. case 3:
  89. ctx.cphash_path = value;
  90. break;
  91. case 'x':
  92. ctx.is_nonce_tpm = true;
  93. break;
  94. case 't':
  95. result = tpm2_util_string_to_int32(value, &ctx.expiration);
  96. if (!result) {
  97. LOG_ERR("Failed reading expiration duration from value, got:\"%s\"",
  98. value);
  99. return false;
  100. }
  101. }
  102. return true;
  103. }
  104. static bool tpm2_tool_onstart(tpm2_options **opts) {
  105. static struct option topts[] = {
  106. { "policy", required_argument, NULL, 'L' },
  107. { "session", required_argument, NULL, 'S' },
  108. { "hash-algorithm", required_argument, NULL, 'g' },
  109. { "signature", required_argument, NULL, 's' },
  110. { "format", required_argument, NULL, 'f' },
  111. { "key-context", required_argument, NULL, 'c' },
  112. { "expiration", required_argument, NULL, 't' },
  113. { "qualification", required_argument, NULL, 'q' },
  114. { "nonce-tpm", no_argument, NULL, 'x' },
  115. { "ticket", required_argument, NULL, 0 },
  116. { "timeout", required_argument, NULL, 1 },
  117. { "raw-data", required_argument, NULL, 2 },
  118. { "cphash-input", required_argument, NULL, 3 },
  119. };
  120. *opts = tpm2_options_new("L:S:g:s:f:c:t:q:x", ARRAY_LEN(topts), topts, on_option,
  121. NULL, 0);
  122. return *opts != NULL;
  123. }
  124. static bool is_input_option_args_valid(void) {
  125. if (!ctx.context_arg) {
  126. LOG_ERR("Must specify verifying key context -c.");
  127. return false;
  128. }
  129. if (ctx.raw_data_path) {
  130. if (ctx.is_nonce_tpm && !ctx.session_path) {
  131. LOG_ERR("Must specify -S session file.");
  132. return false;
  133. }
  134. return true;
  135. }
  136. if (!ctx.session_path) {
  137. LOG_ERR("Must specify -S session file.");
  138. return false;
  139. }
  140. return true;
  141. }
  142. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  143. UNUSED(flags);
  144. bool retval = is_input_option_args_valid();
  145. if (!retval) {
  146. return tool_rc_option_error;
  147. }
  148. if (ctx.flags.sig) {
  149. tpm2_convert_sig_fmt fmt =
  150. ctx.flags.fmt ? signature_format_plain : signature_format_tss;
  151. bool res = tpm2_convert_sig_load(ctx.sig_file_path, fmt, ctx.format,
  152. ctx.halg, &ctx.signature);
  153. if (!res) {
  154. return tool_rc_general_error;
  155. }
  156. }
  157. /*
  158. * For signature verification only object load is needed, not auth.
  159. */
  160. tool_rc tmp_rc = tpm2_util_object_load(ectx, ctx.context_arg,
  161. &ctx.key_context_object,
  162. TPM2_HANDLES_FLAGS_TRANSIENT|TPM2_HANDLES_FLAGS_PERSISTENT);
  163. if (tmp_rc != tool_rc_success) {
  164. return tmp_rc;
  165. }
  166. tool_rc rc = tpm2_session_restore(ectx, ctx.session_path, false,
  167. &ctx.session);
  168. if (rc != tool_rc_success) {
  169. return rc;
  170. }
  171. TPM2B_TIMEOUT *timeout = NULL;
  172. TPMT_TK_AUTH *policy_ticket = NULL;
  173. rc = tpm2_policy_build_policysigned(ectx, ctx.session,
  174. &ctx.key_context_object, &ctx.signature, ctx.expiration, &timeout,
  175. &policy_ticket, ctx.policy_qualifier_data, ctx.is_nonce_tpm,
  176. ctx.raw_data_path, ctx.cphash_path);
  177. if (rc != tool_rc_success) {
  178. LOG_ERR("Could not build policysigned TPM");
  179. goto tpm2_tool_onrun_out;
  180. }
  181. if (ctx.raw_data_path) {
  182. goto tpm2_tool_onrun_out;
  183. }
  184. rc = tpm2_policy_tool_finish(ectx, ctx.session, ctx.policy_digest_path);
  185. if (rc != tool_rc_success) {
  186. goto tpm2_tool_onrun_out;
  187. }
  188. if (ctx.policy_timeout_path) {
  189. if(!timeout->size) {
  190. LOG_WARN("Policy assertion did not produce timeout");
  191. } else {
  192. retval = files_save_bytes_to_file(ctx.policy_timeout_path,
  193. timeout->buffer, timeout->size);
  194. }
  195. }
  196. if (!retval) {
  197. LOG_ERR("Failed to save timeout to file.");
  198. rc = tool_rc_general_error;
  199. goto tpm2_tool_onrun_out;
  200. }
  201. if (ctx.policy_ticket_path) {
  202. if (!policy_ticket->digest.size) {
  203. LOG_WARN("Policy assertion did not produce auth ticket.");
  204. } else {
  205. retval = files_save_authorization_ticket(policy_ticket,
  206. ctx.policy_ticket_path);
  207. }
  208. }
  209. if (!retval) {
  210. LOG_ERR("Failed to save auth ticket");
  211. rc = tool_rc_general_error;
  212. }
  213. tpm2_tool_onrun_out:
  214. free(timeout);
  215. free(policy_ticket);
  216. return rc;
  217. }
  218. static tool_rc tpm2_tool_onstop(ESYS_CONTEXT *ectx) {
  219. UNUSED(ectx);
  220. return tpm2_session_close(&ctx.session);
  221. }
  222. // Register this tool with tpm2_tool.c
  223. TPM2_TOOL_REGISTER("policysigned", tpm2_tool_onstart, tpm2_tool_onrun, tpm2_tool_onstop, NULL)