tpm2_policyor.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <stdbool.h>
  3. #include <stdlib.h>
  4. #include "files.h"
  5. #include "log.h"
  6. #include "tpm2_alg_util.h"
  7. #include "tpm2_policy.h"
  8. #include "tpm2_tool.h"
  9. typedef struct tpm2_policyor_ctx tpm2_policyor_ctx;
  10. struct tpm2_policyor_ctx {
  11. //File path for the session context data
  12. const char *session_path;
  13. //List of policy digests that will be compounded
  14. TPML_DIGEST *policy_list;
  15. //File path for storing the policy digest output
  16. const char *out_policy_dgst_path;
  17. TPM2B_DIGEST *policy_digest;
  18. tpm2_session *session;
  19. };
  20. static tpm2_policyor_ctx ctx;
  21. static bool on_option(char key, char *value) {
  22. bool result = true;
  23. switch (key) {
  24. case 'L':
  25. ctx.out_policy_dgst_path = value;
  26. break;
  27. case 'S':
  28. ctx.session_path = value;
  29. break;
  30. case 'l':
  31. ctx.policy_list = calloc(1, sizeof(TPML_DIGEST));
  32. result = tpm2_policy_parse_policy_list(value, ctx.policy_list);
  33. if (!result) {
  34. return false;
  35. }
  36. break;
  37. }
  38. return result;
  39. }
  40. static bool on_arg(int argc, char **argv) {
  41. if (argc > 1) {
  42. LOG_ERR("specify single argument for policy list.");
  43. return false;
  44. }
  45. ctx.policy_list = calloc(1, sizeof(TPML_DIGEST));
  46. bool result = tpm2_policy_parse_policy_list(argv[0], ctx.policy_list);
  47. if (!result) {
  48. return false;
  49. }
  50. return true;
  51. }
  52. static bool tpm2_tool_onstart(tpm2_options **opts) {
  53. static struct option topts[] = {
  54. { "policy", required_argument, NULL, 'L' },
  55. { "session", required_argument, NULL, 'S' },
  56. //Option retained for backwards compatibility - See issue#1894
  57. { "policy-list", required_argument, NULL, 'l' },
  58. };
  59. *opts = tpm2_options_new("L:S:l:", ARRAY_LEN(topts), topts, on_option,
  60. on_arg, 0);
  61. return *opts != NULL;
  62. }
  63. static bool is_input_option_args_valid(void) {
  64. if (!ctx.session_path) {
  65. LOG_ERR("Must specify -S session file.");
  66. return false;
  67. }
  68. //Minimum two policies needed to be specified for compounding
  69. if (ctx.policy_list->count < 1) {
  70. LOG_ERR("Must specify at least 2 policy digests for compounding.");
  71. return false;
  72. }
  73. return true;
  74. }
  75. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  76. UNUSED(flags);
  77. bool retval = is_input_option_args_valid();
  78. if (!retval) {
  79. return tool_rc_option_error;
  80. }
  81. tool_rc rc = tpm2_session_restore(ectx, ctx.session_path, false,
  82. &ctx.session);
  83. if (rc != tool_rc_success) {
  84. return rc;
  85. }
  86. /* Policy digest hash alg should match that of the session */
  87. if (ctx.policy_list->digests[0].size
  88. != tpm2_alg_util_get_hash_size(
  89. tpm2_session_get_authhash(ctx.session))) {
  90. LOG_ERR("Policy digest hash alg should match that of the session.");
  91. return tool_rc_general_error;
  92. }
  93. rc = tpm2_policy_build_policyor(ectx, ctx.session, ctx.policy_list);
  94. if (rc != tool_rc_success) {
  95. LOG_ERR("Could not build policyor TPM");
  96. return rc;
  97. }
  98. return tpm2_policy_tool_finish(ectx, ctx.session, ctx.out_policy_dgst_path);
  99. }
  100. static tool_rc tpm2_tool_onstop(ESYS_CONTEXT *ectx) {
  101. UNUSED(ectx);
  102. free(ctx.policy_list);
  103. free(ctx.policy_digest);
  104. return tpm2_session_close(&ctx.session);
  105. }
  106. // Register this tool with tpm2_tool.c
  107. TPM2_TOOL_REGISTER("policyor", tpm2_tool_onstart, tpm2_tool_onrun, tpm2_tool_onstop, NULL)