tpm2_nvincrement.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <stdlib.h>
  3. #include "files.h"
  4. #include "tpm2_nv_util.h"
  5. #include "tpm2_tool.h"
  6. typedef struct tpm_nvincrement_ctx tpm_nvincrement_ctx;
  7. struct tpm_nvincrement_ctx {
  8. struct {
  9. const char *ctx_path;
  10. const char *auth_str;
  11. tpm2_loaded_object object;
  12. } auth_hierarchy;
  13. TPM2_HANDLE nv_index;
  14. char *cp_hash_path;
  15. };
  16. static tpm_nvincrement_ctx ctx;
  17. static bool on_arg(int argc, char **argv) {
  18. /* If the user doesn't specify an authorization hierarchy use the index
  19. * passed to -x/--index for the authorization index.
  20. */
  21. if (!ctx.auth_hierarchy.ctx_path) {
  22. ctx.auth_hierarchy.ctx_path = argv[0];
  23. }
  24. return on_arg_nv_index(argc, argv, &ctx.nv_index);
  25. }
  26. static bool on_option(char key, char *value) {
  27. switch (key) {
  28. case 'C':
  29. ctx.auth_hierarchy.ctx_path = value;
  30. break;
  31. case 'P':
  32. ctx.auth_hierarchy.auth_str = value;
  33. break;
  34. case 0:
  35. ctx.cp_hash_path = value;
  36. break;
  37. }
  38. return true;
  39. }
  40. static bool tpm2_tool_onstart(tpm2_options **opts) {
  41. const struct option topts[] = {
  42. { "hierarchy", required_argument, NULL, 'C' },
  43. { "auth", required_argument, NULL, 'P' },
  44. { "cphash", required_argument, NULL, 0 },
  45. };
  46. *opts = tpm2_options_new("C:P:", ARRAY_LEN(topts), topts, on_option, on_arg,
  47. 0);
  48. return *opts != NULL;
  49. }
  50. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  51. UNUSED(flags);
  52. tool_rc rc = tpm2_util_object_load_auth(ectx, ctx.auth_hierarchy.ctx_path,
  53. ctx.auth_hierarchy.auth_str, &ctx.auth_hierarchy.object, false,
  54. TPM2_HANDLE_FLAGS_NV | TPM2_HANDLE_FLAGS_O | TPM2_HANDLE_FLAGS_P);
  55. if (rc != tool_rc_success) {
  56. LOG_ERR("Invalid handle authorization");
  57. return rc;
  58. }
  59. if (!ctx.cp_hash_path) {
  60. rc = tpm2_nv_increment(ectx, &ctx.auth_hierarchy.object, ctx.nv_index, NULL);
  61. if (rc != tool_rc_success) {
  62. LOG_ERR("Failed to increment NV counter at index 0x%X", ctx.nv_index);
  63. }
  64. return rc;
  65. }
  66. TPM2B_DIGEST cp_hash = { .size = 0 };
  67. rc = tpm2_nv_increment(ectx, &ctx.auth_hierarchy.object, ctx.nv_index,
  68. &cp_hash);
  69. if (rc != tool_rc_success) {
  70. return rc;
  71. }
  72. bool result = files_save_digest(&cp_hash, ctx.cp_hash_path);
  73. if (!result) {
  74. rc = tool_rc_general_error;
  75. }
  76. return rc;
  77. }
  78. static tool_rc tpm2_tool_onstop(ESYS_CONTEXT *ectx) {
  79. UNUSED(ectx);
  80. if (!ctx.cp_hash_path) {
  81. return tpm2_session_close(&ctx.auth_hierarchy.object.session);
  82. }
  83. return tool_rc_success;
  84. }
  85. // Register this tool with tpm2_tool.c
  86. TPM2_TOOL_REGISTER("nvincrement", tpm2_tool_onstart, tpm2_tool_onrun, tpm2_tool_onstop, NULL)