tpm2_policynamehash.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include "files.h"
  3. #include "log.h"
  4. #include "tpm2.h"
  5. #include "tpm2_policy.h"
  6. #include "tpm2_tool.h"
  7. typedef struct tpm2_policynamehash_ctx tpm2_policynamehash_ctx;
  8. struct tpm2_policynamehash_ctx {
  9. //Input
  10. TPM2B_DIGEST name_hash;
  11. //Input/Output
  12. const char *session_file_path;
  13. tpm2_session *session;
  14. //Output
  15. const char *policy_digest_file_path;
  16. };
  17. static tpm2_policynamehash_ctx ctx;
  18. static bool process_input_name_hash(char *value) {
  19. ctx.name_hash.size = sizeof(ctx.name_hash.buffer);
  20. bool result = files_load_bytes_from_buffer_or_file_or_stdin(NULL, value,
  21. &ctx.name_hash.size, ctx.name_hash.buffer);
  22. if (!result) {
  23. LOG_ERR("Failed loading name hash from file");
  24. return false;
  25. }
  26. return true;
  27. }
  28. static bool on_option(char key, char *value) {
  29. bool result = true;
  30. switch (key) {
  31. case 'L':
  32. ctx.policy_digest_file_path = value;
  33. break;
  34. case 'S':
  35. ctx.session_file_path = value;
  36. break;
  37. case 'n':
  38. result = process_input_name_hash(value);
  39. break;
  40. }
  41. return result;
  42. }
  43. static bool tpm2_tool_onstart(tpm2_options **opts) {
  44. static struct option topts[] = {
  45. { "policy", required_argument, NULL, 'L' },
  46. { "session", required_argument, NULL, 'S' },
  47. { "name", required_argument, NULL, 'n' },
  48. };
  49. *opts = tpm2_options_new("L:S:n:", ARRAY_LEN(topts), topts, on_option, NULL,
  50. 0);
  51. return *opts != NULL;
  52. }
  53. static bool is_input_option_args_valid(void) {
  54. if (!ctx.session_file_path) {
  55. LOG_ERR("Must specify -S session file.");
  56. return false;
  57. }
  58. if (!ctx.name_hash.size) {
  59. LOG_WARN("Name-hash file is of size zero");
  60. }
  61. return true;
  62. }
  63. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  64. UNUSED(flags);
  65. bool retval = is_input_option_args_valid();
  66. if (!retval) {
  67. return tool_rc_option_error;
  68. }
  69. tool_rc rc = tpm2_session_restore(ectx, ctx.session_file_path, false,
  70. &ctx.session);
  71. if (rc != tool_rc_success) {
  72. return rc;
  73. }
  74. rc = tpm2_policy_build_policynamehash(ectx, ctx.session, &ctx.name_hash);
  75. if (rc != tool_rc_success) {
  76. LOG_ERR("Could not build policynamehash TPM");
  77. return rc;
  78. }
  79. return tpm2_policy_tool_finish(ectx, ctx.session, ctx.policy_digest_file_path);
  80. }
  81. static tool_rc tpm2_tool_onstop(ESYS_CONTEXT *ectx) {
  82. UNUSED(ectx);
  83. return tpm2_session_close(&ctx.session);
  84. }
  85. // Register this tool with tpm2_tool.c
  86. TPM2_TOOL_REGISTER("policynamehash", tpm2_tool_onstart, tpm2_tool_onrun, tpm2_tool_onstop, NULL)