tpm2_hash.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <errno.h>
  3. #include <stdbool.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include "files.h"
  8. #include "log.h"
  9. #include "tpm2_alg_util.h"
  10. #include "tpm2_hash.h"
  11. #include "tpm2_hierarchy.h"
  12. #include "tpm2_tool.h"
  13. typedef struct tpm_hash_ctx tpm_hash_ctx;
  14. struct tpm_hash_ctx {
  15. TPMI_RH_HIERARCHY hierarchy_value;
  16. FILE *input_file;
  17. TPMI_ALG_HASH halg;
  18. char *output_hash_path;
  19. char *output_ticket_path;
  20. bool hex;
  21. };
  22. static tpm_hash_ctx ctx = {
  23. .hierarchy_value = TPM2_RH_OWNER,
  24. .halg = TPM2_ALG_SHA1,
  25. };
  26. static tool_rc hash_and_save(ESYS_CONTEXT *context) {
  27. TPM2B_DIGEST *out_hash;
  28. TPMT_TK_HASHCHECK *validation;
  29. FILE *out = stdout;
  30. tool_rc rc = tpm2_hash_file(context, ctx.halg, ctx.hierarchy_value,
  31. ctx.input_file, &out_hash, &validation);
  32. if (rc != tool_rc_success) {
  33. return rc;
  34. }
  35. if (ctx.output_ticket_path) {
  36. bool res = files_save_validation(validation, ctx.output_ticket_path);
  37. if (!res) {
  38. rc = tool_rc_general_error;
  39. goto out;
  40. }
  41. }
  42. rc = tool_rc_general_error;
  43. if (ctx.output_hash_path) {
  44. out = fopen(ctx.output_hash_path, "wb+");
  45. if (!out) {
  46. LOG_ERR("Could not open output file \"%s\", error: %s",
  47. ctx.output_hash_path, strerror(errno));
  48. goto out;
  49. }
  50. } else if (!output_enabled) {
  51. rc = tool_rc_success;
  52. goto out;
  53. }
  54. if (ctx.hex) {
  55. tpm2_util_print_tpm2b2(out, out_hash);
  56. } else {
  57. bool res = files_write_bytes(out, out_hash->buffer, out_hash->size);
  58. if (!res) {
  59. goto out;
  60. }
  61. }
  62. rc = tool_rc_success;
  63. out:
  64. if (out && out != stdout) {
  65. fclose(out);
  66. }
  67. free(out_hash);
  68. free(validation);
  69. return rc;
  70. }
  71. static bool on_args(int argc, char **argv) {
  72. if (argc > 1) {
  73. LOG_ERR("Only supports one hash input file, got: %d", argc);
  74. return false;
  75. }
  76. ctx.input_file = fopen(argv[0], "rb");
  77. if (!ctx.input_file) {
  78. LOG_ERR("Could not open input file \"%s\", error: %s", argv[0],
  79. strerror(errno));
  80. return false;
  81. }
  82. return true;
  83. }
  84. static bool on_option(char key, char *value) {
  85. bool res;
  86. switch (key) {
  87. case 'C':
  88. res = tpm2_util_handle_from_optarg(value, &ctx.hierarchy_value,
  89. TPM2_HANDLE_FLAGS_ALL_HIERACHIES);
  90. if (!res) {
  91. return false;
  92. }
  93. break;
  94. case 'g':
  95. ctx.halg = tpm2_alg_util_from_optarg(value, tpm2_alg_util_flags_hash);
  96. if (ctx.halg == TPM2_ALG_ERROR) {
  97. return false;
  98. }
  99. break;
  100. case 'o':
  101. ctx.output_hash_path = value;
  102. break;
  103. case 't':
  104. ctx.output_ticket_path = value;
  105. break;
  106. case 0:
  107. ctx.hex = true;
  108. break;
  109. }
  110. return true;
  111. }
  112. static bool tpm2_tool_onstart(tpm2_options **opts) {
  113. static struct option topts[] = {
  114. {"hierarchy", required_argument, NULL, 'C'},
  115. {"hash-algorithm", required_argument, NULL, 'g'},
  116. {"output", required_argument, NULL, 'o'},
  117. {"ticket", required_argument, NULL, 't'},
  118. {"hex", no_argument, NULL, 0 },
  119. };
  120. /* set up non-static defaults here */
  121. ctx.input_file = stdin;
  122. *opts = tpm2_options_new("C:g:o:t:", ARRAY_LEN(topts), topts, on_option,
  123. on_args, 0);
  124. return *opts != NULL;
  125. }
  126. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *context, tpm2_option_flags flags) {
  127. UNUSED(flags);
  128. return hash_and_save(context);
  129. }
  130. static tool_rc tpm2_tool_onstop(ESYS_CONTEXT *context) {
  131. UNUSED(context);
  132. if (ctx.input_file) {
  133. fclose(ctx.input_file);
  134. }
  135. return tool_rc_success;
  136. }
  137. // Register this tool with tpm2_tool.c
  138. TPM2_TOOL_REGISTER("hash", tpm2_tool_onstart, tpm2_tool_onrun, tpm2_tool_onstop, NULL)