tpm2_nvsetbits.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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_nvsetbits_ctx tpm_nvsetbits_ctx;
  10. struct tpm_nvsetbits_ctx {
  11. struct {
  12. const char *ctx_path;
  13. const char *auth_str;
  14. tpm2_loaded_object object;
  15. } auth_hierarchy;
  16. const char *bit_string;
  17. TPM2_HANDLE nv_index;
  18. char *cp_hash_path;
  19. };
  20. static tpm_nvsetbits_ctx ctx;
  21. static bool on_arg(int argc, char **argv) {
  22. /* If the user doesn't specify an authorization hierarchy use the index
  23. * passed to -x/--index for the authorization index.
  24. */
  25. if (!ctx.auth_hierarchy.ctx_path) {
  26. ctx.auth_hierarchy.ctx_path = argv[0];
  27. }
  28. return on_arg_nv_index(argc, argv, &ctx.nv_index);
  29. }
  30. static bool on_option(char key, char *value) {
  31. switch (key) {
  32. case 'C':
  33. ctx.auth_hierarchy.ctx_path = value;
  34. break;
  35. case 'P':
  36. ctx.auth_hierarchy.auth_str = value;
  37. break;
  38. case 'i':
  39. ctx.bit_string = value;
  40. break;
  41. case 0:
  42. ctx.cp_hash_path = value;
  43. break;
  44. }
  45. return true;
  46. }
  47. static bool tpm2_tool_onstart(tpm2_options **opts) {
  48. const struct option topts[] = {
  49. { "hierarchy", required_argument, NULL, 'C' },
  50. { "auth", required_argument, NULL, 'P' },
  51. { "bits", required_argument, NULL, 'i' },
  52. { "cphash", required_argument, NULL, 0 },
  53. };
  54. *opts = tpm2_options_new("C:P:i:", ARRAY_LEN(topts), topts, on_option,
  55. on_arg, 0);
  56. return *opts != NULL;
  57. }
  58. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  59. UNUSED(flags);
  60. if (!ctx.bit_string) {
  61. LOG_ERR("Expected option --bits for specifying the bits to set");
  62. return tool_rc_option_error;
  63. }
  64. UINT64 bits = 0;
  65. bool result = tpm2_util_string_to_uint64(ctx.bit_string, &bits);
  66. if (!result) {
  67. LOG_ERR("Could not convert option argument to number, got: \"%s\"",
  68. ctx.bit_string);
  69. return tool_rc_general_error;
  70. }
  71. tool_rc rc = tpm2_util_object_load_auth(ectx, ctx.auth_hierarchy.ctx_path,
  72. ctx.auth_hierarchy.auth_str, &ctx.auth_hierarchy.object, false,
  73. TPM2_HANDLE_FLAGS_NV | TPM2_HANDLE_FLAGS_O | TPM2_HANDLE_FLAGS_P);
  74. if (rc != tool_rc_success) {
  75. LOG_ERR("Invalid handle authorization");
  76. return rc;
  77. }
  78. if (!ctx.cp_hash_path) {
  79. return tpm2_nvsetbits(ectx, &ctx.auth_hierarchy.object, ctx.nv_index,
  80. bits, NULL);
  81. }
  82. TPM2B_DIGEST cp_hash = { .size = 0 };
  83. rc = tpm2_nvsetbits(ectx, &ctx.auth_hierarchy.object, ctx.nv_index, bits,
  84. &cp_hash);
  85. if (rc != tool_rc_success) {
  86. return rc;
  87. }
  88. 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("nvsetbits", tpm2_tool_onstart, tpm2_tool_onrun, tpm2_tool_onstop, NULL)