tpm2_nvreadpublic.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <stdlib.h>
  3. #include "tpm2_alg_util.h"
  4. #include "tpm2_attr_util.h"
  5. #include "tpm2_nv_util.h"
  6. #include "tpm2_tool.h"
  7. typedef struct tpm2_nvreadpublic_ctx tpm2_nvreadpublic_ctx;
  8. struct tpm2_nvreadpublic_ctx {
  9. TPMI_RH_NV_INDEX nv_index;
  10. };
  11. static tpm2_nvreadpublic_ctx ctx;
  12. static tool_rc print_nv_public(ESYS_CONTEXT *context, TPMI_RH_NV_INDEX index, TPM2B_NV_PUBLIC *nv_public) {
  13. ESYS_TR tr_handle = ESYS_TR_NONE;
  14. tool_rc rc = tpm2_tr_from_tpm_public(context, index,
  15. &tr_handle);
  16. if (rc != tool_rc_success) {
  17. return rc;
  18. }
  19. tpm2_tool_output("0x%x:\n", index);
  20. char *attrs = tpm2_attr_util_nv_attrtostr(nv_public->nvPublic.attributes);
  21. if (!attrs) {
  22. LOG_ERR("Could not convert attributes to string form");
  23. }
  24. const char *alg = tpm2_alg_util_algtostr(nv_public->nvPublic.nameAlg,
  25. tpm2_alg_util_flags_hash);
  26. if (!alg) {
  27. LOG_ERR("Could not convert algorithm to string form");
  28. }
  29. TPM2B_NAME *name = NULL;
  30. rc = tpm2_tr_get_name(context, tr_handle,
  31. &name);
  32. if (rc != tool_rc_success) {
  33. free(attrs);
  34. return rc;
  35. }
  36. tpm2_tool_output(" name: ");
  37. UINT16 i;
  38. for (i = 0; i < name->size; i++) {
  39. tpm2_tool_output("%02x", name->name[i]);
  40. }
  41. tpm2_tool_output("\n");
  42. Esys_Free(name);
  43. tpm2_tool_output(" hash algorithm:\n");
  44. tpm2_tool_output(" friendly: %s\n", alg);
  45. tpm2_tool_output(" value: 0x%X\n", nv_public->nvPublic.nameAlg);
  46. tpm2_tool_output(" attributes:\n");
  47. tpm2_tool_output(" friendly: %s\n", attrs);
  48. tpm2_tool_output(" value: 0x%X\n",
  49. tpm2_util_ntoh_32(nv_public->nvPublic.attributes));
  50. tpm2_tool_output(" size: %d\n", nv_public->nvPublic.dataSize);
  51. if (nv_public->nvPublic.authPolicy.size) {
  52. tpm2_tool_output(" authorization policy: ");
  53. UINT16 i;
  54. for (i = 0; i < nv_public->nvPublic.authPolicy.size; i++) {
  55. tpm2_tool_output("%02X", nv_public->nvPublic.authPolicy.buffer[i]);
  56. }
  57. tpm2_tool_output("\n");
  58. }
  59. free(attrs);
  60. return tool_rc_success;
  61. }
  62. static tool_rc nv_readpublic(ESYS_CONTEXT *context) {
  63. TPMS_CAPABILITY_DATA *capability_data = NULL;
  64. if (ctx.nv_index == 0) {
  65. tool_rc rc = tpm2_getcap(context, TPM2_CAP_HANDLES, TPM2_HT_NV_INDEX << 24,
  66. TPM2_PT_NV_INDEX_MAX, NULL, &capability_data);
  67. if (rc != tool_rc_success) {
  68. return rc;
  69. }
  70. } else {
  71. capability_data = calloc(1, sizeof(*capability_data));
  72. if (!capability_data) {
  73. LOG_ERR("oom");
  74. return tool_rc_general_error;
  75. }
  76. capability_data->data.handles.count = 1;
  77. capability_data->data.handles.handle[0] = ctx.nv_index;
  78. }
  79. UINT32 i;
  80. for (i = 0; i < capability_data->data.handles.count; i++) {
  81. TPMI_RH_NV_INDEX index = capability_data->data.handles.handle[i];
  82. TPM2B_NV_PUBLIC *nv_public;
  83. tool_rc rc = tpm2_util_nv_read_public(context, index, &nv_public);
  84. if (rc != tool_rc_success) {
  85. LOG_ERR("Failed to read the public part of NV index 0x%X", index);
  86. free(capability_data);
  87. return rc;
  88. }
  89. rc = print_nv_public(context, index, nv_public);
  90. free(nv_public);
  91. tpm2_tool_output("\n");
  92. if (rc != tool_rc_success) {
  93. free(capability_data);
  94. return rc;
  95. }
  96. }
  97. free(capability_data);
  98. return tool_rc_success;
  99. }
  100. static bool on_arg(int argc, char **argv) {
  101. return on_arg_nv_index(argc, argv, &ctx.nv_index);
  102. }
  103. static bool tpm2_tool_onstart(tpm2_options **opts) {
  104. *opts = tpm2_options_new(NULL, 0, NULL, NULL,
  105. on_arg, 0);
  106. return *opts != NULL;
  107. }
  108. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *context, tpm2_option_flags flags) {
  109. UNUSED(flags);
  110. return nv_readpublic(context);
  111. }
  112. // Register this tool with tpm2_tool.c
  113. TPM2_TOOL_REGISTER("nvreadpublic", tpm2_tool_onstart, tpm2_tool_onrun, NULL, NULL)