tpm2_nvwritelock.c 3.4 KB

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