tpm2_policyduplicationselect.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <stdlib.h>
  3. #include "files.h"
  4. #include "log.h"
  5. #include "tpm2_policy.h"
  6. #include "tpm2_tool.h"
  7. typedef struct tpm2_policyduplicationselect_ctx tpm2_policyduplicationselect_ctx;
  8. struct tpm2_policyduplicationselect_ctx {
  9. const char *session_path;
  10. const char *obj_name_path;
  11. const char *new_parent_name_path;
  12. const char *out_policy_dgst_path;
  13. TPMI_YES_NO is_include_obj;
  14. TPM2B_DIGEST *policy_digest;
  15. tpm2_session *session;
  16. };
  17. static tpm2_policyduplicationselect_ctx ctx;
  18. static bool on_option(char key, char *value) {
  19. ctx.is_include_obj = 0;
  20. switch (key) {
  21. case 'S':
  22. ctx.session_path = value;
  23. break;
  24. case 'n':
  25. ctx.obj_name_path = value;
  26. break;
  27. case 'N':
  28. ctx.new_parent_name_path = value;
  29. break;
  30. case 'L':
  31. ctx.out_policy_dgst_path = value;
  32. break;
  33. case 0:
  34. ctx.is_include_obj = 1;
  35. break;
  36. }
  37. return true;
  38. }
  39. static bool is_input_option_args_valid(void) {
  40. if (!ctx.session_path) {
  41. LOG_ERR("Must specify -S session file.");
  42. return false;
  43. }
  44. if (!ctx.new_parent_name_path) {
  45. LOG_ERR("Must specify -N object new parent file.");
  46. return false;
  47. }
  48. return true;
  49. }
  50. static bool tpm2_tool_onstart(tpm2_options **opts) {
  51. static struct option topts[] = {
  52. { "session", required_argument, NULL, 'S' },
  53. { "object-name", required_argument, NULL, 'n' },
  54. { "parent-name", required_argument, NULL, 'N' },
  55. { "policy", required_argument, NULL, 'L' },
  56. { "include-object", no_argument, NULL, 0 },
  57. };
  58. *opts = tpm2_options_new("S:n:N:L:", ARRAY_LEN(topts), topts, on_option,
  59. NULL, 0);
  60. return *opts != NULL;
  61. }
  62. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  63. UNUSED(flags);
  64. bool retval = is_input_option_args_valid();
  65. if (!retval) {
  66. return tool_rc_option_error;
  67. }
  68. tool_rc rc = tpm2_session_restore(ectx, ctx.session_path, false,
  69. &ctx.session);
  70. if (rc != tool_rc_success) {
  71. return rc;
  72. }
  73. rc = tpm2_policy_build_policyduplicationselect(ectx, ctx.session,
  74. ctx.obj_name_path, ctx.new_parent_name_path, ctx.is_include_obj);
  75. if (rc != tool_rc_success) {
  76. LOG_ERR("Could not build TPM policy_duplication_select");
  77. return rc;
  78. }
  79. return tpm2_policy_tool_finish(ectx, ctx.session, ctx.out_policy_dgst_path);
  80. }
  81. static tool_rc tpm2_tool_onstop(ESYS_CONTEXT *ectx) {
  82. UNUSED(ectx);
  83. free(ctx.policy_digest);
  84. return tpm2_session_close(&ctx.session);
  85. }
  86. // Register this tool with tpm2_tool.c
  87. TPM2_TOOL_REGISTER("policyduplicationselect", tpm2_tool_onstart, tpm2_tool_onrun, tpm2_tool_onstop, NULL)