tpm2_policynvwritten.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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_cc_util.h"
  7. #include "tpm2_policy.h"
  8. #include "tpm2_tool.h"
  9. typedef struct tpm2_policynvwritten_ctx tpm2_policynvwritten_ctx;
  10. struct tpm2_policynvwritten_ctx {
  11. const char *session_path;
  12. const char *policy_digest_path;
  13. tpm2_session *session;
  14. TPMI_YES_NO written_set;
  15. };
  16. static tpm2_policynvwritten_ctx ctx = {
  17. .written_set = TPM2_NO,
  18. };
  19. static bool on_option(char key, char *value) {
  20. switch (key) {
  21. case 'S':
  22. ctx.session_path = value;
  23. break;
  24. case 'L':
  25. ctx.policy_digest_path = value;
  26. break;
  27. }
  28. return true;
  29. }
  30. static bool is_input_option_args_valid(void) {
  31. if (!ctx.session_path) {
  32. LOG_ERR("Must specify -S session file.");
  33. return false;
  34. }
  35. return true;
  36. }
  37. static bool on_arg(int argc, char **argv) {
  38. if (argc > 1) {
  39. LOG_ERR("Specify single NV written SET/CLEAR operation as s|c|0|1.");
  40. return false;
  41. }
  42. if (!argc) {
  43. LOG_ERR("Disable NV written SET/CLEAR operation must be specified.");
  44. return false;
  45. }
  46. if (!strcmp(argv[0], "s")) {
  47. ctx.written_set = TPM2_YES;
  48. return true;
  49. }
  50. if (!strcmp(argv[0], "c")) {
  51. ctx.written_set = TPM2_NO;
  52. return true;
  53. }
  54. uint32_t value;
  55. bool result = tpm2_util_string_to_uint32(argv[0], &value);
  56. if (!result) {
  57. LOG_ERR("Please specify 0|1|s|c. Could not convert string, got: \"%s\"",
  58. argv[0]);
  59. return false;
  60. }
  61. if (value != TPM2_NO && value != TPM2_YES) {
  62. LOG_ERR("Please use 0|1|s|c as the argument to specify operation");
  63. return false;
  64. }
  65. ctx.written_set = value;
  66. return true;
  67. }
  68. static bool tpm2_tool_onstart(tpm2_options **opts) {
  69. static struct option topts[] = {
  70. { "session", required_argument, NULL, 'S' },
  71. { "policy", required_argument, NULL, 'L' },
  72. };
  73. *opts = tpm2_options_new("S:L:", ARRAY_LEN(topts), topts, on_option, on_arg,
  74. 0);
  75. return *opts != NULL;
  76. }
  77. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  78. UNUSED(flags);
  79. bool retval = is_input_option_args_valid();
  80. if (!retval) {
  81. return tool_rc_option_error;
  82. }
  83. tool_rc rc = tpm2_session_restore(ectx, ctx.session_path, false,
  84. &ctx.session);
  85. if (rc != tool_rc_success) {
  86. return rc;
  87. }
  88. rc = tpm2_policy_build_policynvwritten(ectx, ctx.session, ctx.written_set);
  89. if (rc != tool_rc_success) {
  90. LOG_ERR("Could not build policy_nv_written!");
  91. return rc;
  92. }
  93. return tpm2_policy_tool_finish(ectx, ctx.session, ctx.policy_digest_path);
  94. }
  95. static tool_rc tpm2_tool_onstop(ESYS_CONTEXT *ectx) {
  96. UNUSED(ectx);
  97. return tpm2_session_close(&ctx.session);
  98. }
  99. // Register this tool with tpm2_tool.c
  100. TPM2_TOOL_REGISTER("policynvwritten", tpm2_tool_onstart, tpm2_tool_onrun, tpm2_tool_onstop, NULL)