tpm2_policylocality.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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_policy.h"
  7. #include "tpm2_tool.h"
  8. typedef struct tpm2_policylocality_ctx tpm2_policylocality_ctx;
  9. struct tpm2_policylocality_ctx {
  10. const char *session_path;
  11. TPMA_LOCALITY locality;
  12. const char *out_policy_dgst_path;
  13. TPM2B_DIGEST *policy_digest;
  14. tpm2_session *session;
  15. };
  16. static tpm2_policylocality_ctx ctx;
  17. static bool on_option(char key, char *value) {
  18. switch (key) {
  19. case 'S':
  20. ctx.session_path = value;
  21. break;
  22. case 'L':
  23. ctx.out_policy_dgst_path = value;
  24. break;
  25. }
  26. return true;
  27. }
  28. static bool is_input_option_args_valid(void) {
  29. if (!ctx.session_path) {
  30. LOG_ERR("Must specify -S session file.");
  31. return false;
  32. }
  33. return true;
  34. }
  35. static bool on_arg(int argc, char **argv) {
  36. if (argc > 1) {
  37. LOG_ERR("Specify only the TPM2 locality.");
  38. return false;
  39. }
  40. if (!argc) {
  41. LOG_ERR("TPM2 locality must be specified.");
  42. return false;
  43. }
  44. if (strcmp(argv[0], "zero")) {
  45. ctx.locality = TPMA_LOCALITY_TPM2_LOC_ZERO;
  46. } else if (strcmp(argv[0], "one")) {
  47. ctx.locality = TPMA_LOCALITY_TPM2_LOC_ONE;
  48. } else if (strcmp(argv[0], "two")) {
  49. ctx.locality = TPMA_LOCALITY_TPM2_LOC_TWO;
  50. } else if (strcmp(argv[0], "three")) {
  51. ctx.locality = TPMA_LOCALITY_TPM2_LOC_THREE;
  52. } else if (strcmp(argv[0], "four")) {
  53. ctx.locality = TPMA_LOCALITY_TPM2_LOC_FOUR;
  54. } else {
  55. bool result = tpm2_util_string_to_uint8(argv[0], &ctx.locality);
  56. if (!result) {
  57. LOG_ERR("Could not convert locality to number, got: \"%s\"",
  58. argv[0]);
  59. return false;
  60. }
  61. }
  62. return true;
  63. }
  64. static bool tpm2_tool_onstart(tpm2_options **opts) {
  65. static struct option topts[] = {
  66. { "session", required_argument, NULL, 'S' },
  67. { "policy", required_argument, NULL, 'L' },
  68. };
  69. *opts = tpm2_options_new("S:L:", ARRAY_LEN(topts), topts, on_option, on_arg,
  70. 0);
  71. return *opts != NULL;
  72. }
  73. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  74. UNUSED(flags);
  75. bool retval = is_input_option_args_valid();
  76. if (!retval) {
  77. return tool_rc_option_error;
  78. }
  79. tool_rc rc = tpm2_session_restore(ectx, ctx.session_path, false,
  80. &ctx.session);
  81. if (rc != tool_rc_success) {
  82. return rc;
  83. }
  84. rc = tpm2_policy_build_policylocality(ectx, ctx.session, ctx.locality);
  85. if (rc != tool_rc_success) {
  86. LOG_ERR("Could not build TPM policy_locality");
  87. return rc;
  88. }
  89. return tpm2_policy_tool_finish(ectx, ctx.session, ctx.out_policy_dgst_path);
  90. }
  91. static tool_rc tpm2_tool_onstop(ESYS_CONTEXT *ectx) {
  92. UNUSED(ectx);
  93. free(ctx.policy_digest);
  94. return tpm2_session_close(&ctx.session);
  95. }
  96. // Register this tool with tpm2_tool.c
  97. TPM2_TOOL_REGISTER("policylocality", tpm2_tool_onstart, tpm2_tool_onrun, tpm2_tool_onstop, NULL)