tpm2_pcrevent.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <assert.h>
  3. #include <errno.h>
  4. #include <stdbool.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include "files.h"
  9. #include "log.h"
  10. #include "tpm2.h"
  11. #include "tpm2_alg_util.h"
  12. #include "tpm2_hierarchy.h"
  13. #include "tpm2_auth_util.h"
  14. #include "tpm2_tool.h"
  15. typedef struct tpm_pcrevent_ctx tpm_pcrevent_ctx;
  16. struct tpm_pcrevent_ctx {
  17. struct {
  18. const char *auth_str;
  19. tpm2_session *session;
  20. } auth;
  21. ESYS_TR pcr;
  22. FILE *input;
  23. };
  24. static tpm_pcrevent_ctx ctx = {
  25. .pcr = ESYS_TR_RH_NULL,
  26. };
  27. static tool_rc tpm_pcrevent_file(ESYS_CONTEXT *ectx,
  28. TPML_DIGEST_VALUES **result) {
  29. unsigned long file_size = 0;
  30. FILE *input = ctx.input;
  31. /* Suppress error reporting with NULL path */
  32. bool res = files_get_file_size(input, &file_size, NULL);
  33. /* If we can get the file size and its less than 1024, just do it in one hash invocation */
  34. if (res && file_size <= BUFFER_SIZE(TPM2B_EVENT, buffer)) {
  35. TPM2B_EVENT buffer = TPM2B_INIT(file_size);
  36. res = files_read_bytes(ctx.input, buffer.buffer, buffer.size);
  37. if (!res) {
  38. LOG_ERR("Error reading input file!");
  39. return tool_rc_general_error;
  40. }
  41. return tpm2_pcr_event(ectx, ctx.pcr, ctx.auth.session, &buffer, result);
  42. }
  43. ESYS_TR sequence_handle;
  44. TPM2B_AUTH null_auth = TPM2B_EMPTY_INIT;
  45. /*
  46. * Size is either unknown because the FILE * is a fifo, or it's too big
  47. * to do in a single hash call. Based on the size figure out the chunks
  48. * to loop over, if possible. This way we can call Complete with data.
  49. */
  50. tool_rc rc = tpm2_hash_sequence_start(ectx, &null_auth, TPM2_ALG_NULL,
  51. &sequence_handle);
  52. if (rc != tool_rc_success) {
  53. return rc;
  54. }
  55. /* If we know the file size, we decrement the amount read and terminate the
  56. * loop when 1 block is left, else we go till feof.
  57. */
  58. size_t left = file_size;
  59. bool use_left = !!res;
  60. TPM2B_MAX_BUFFER data;
  61. bool done = false;
  62. while (!done) {
  63. size_t bytes_read = fread(data.buffer, 1,
  64. BUFFER_SIZE(typeof(data), buffer), input);
  65. if (ferror(input)) {
  66. LOG_ERR("Error reading from input file");
  67. return tool_rc_general_error;
  68. }
  69. data.size = bytes_read;
  70. /* if data was read, update the sequence */
  71. rc = tpm2_sequence_update(ectx, sequence_handle, &data);
  72. if (rc != tool_rc_success) {
  73. return rc;
  74. }
  75. if (use_left) {
  76. left -= bytes_read;
  77. if (left <= TPM2_MAX_DIGEST_BUFFER) {
  78. done = true;
  79. continue;
  80. }
  81. } else if (feof(input)) {
  82. done = true;
  83. }
  84. } /* end file read/hash update loop */
  85. if (use_left) {
  86. data.size = left;
  87. bool res = files_read_bytes(input, data.buffer, left);
  88. if (!res) {
  89. LOG_ERR("Error reading from input file.");
  90. return tool_rc_general_error;
  91. }
  92. } else {
  93. data.size = 0;
  94. }
  95. return tpm2_event_sequence_complete(ectx, ctx.pcr, sequence_handle,
  96. ctx.auth.session, &data, result);
  97. }
  98. static tool_rc do_pcrevent_and_output(ESYS_CONTEXT *ectx) {
  99. TPML_DIGEST_VALUES *digests = NULL;
  100. tool_rc rc = tpm_pcrevent_file(ectx, &digests);
  101. if (rc != tool_rc_success) {
  102. return rc;
  103. }
  104. assert(digests);
  105. UINT32 i;
  106. for (i = 0; i < digests->count; i++) {
  107. TPMT_HA *d = &digests->digests[i];
  108. tpm2_tool_output("%s: ",
  109. tpm2_alg_util_algtostr(d->hashAlg, tpm2_alg_util_flags_hash));
  110. const BYTE *bytes;
  111. size_t size;
  112. switch (d->hashAlg) {
  113. case TPM2_ALG_SHA1:
  114. bytes = d->digest.sha1;
  115. size = sizeof(d->digest.sha1);
  116. break;
  117. case TPM2_ALG_SHA256:
  118. bytes = d->digest.sha256;
  119. size = sizeof(d->digest.sha256);
  120. break;
  121. case TPM2_ALG_SHA384:
  122. bytes = d->digest.sha384;
  123. size = sizeof(d->digest.sha384);
  124. break;
  125. case TPM2_ALG_SHA512:
  126. bytes = d->digest.sha512;
  127. size = sizeof(d->digest.sha512);
  128. break;
  129. case TPM2_ALG_SM3_256:
  130. bytes = d->digest.sm3_256;
  131. size = sizeof(d->digest.sm3_256);
  132. break;
  133. default: {
  134. LOG_WARN("Unknown digest to convert!");
  135. // print something so the format doesn't change
  136. // on this case.
  137. static const BYTE byte = 0;
  138. bytes = &byte;
  139. size = sizeof(byte);
  140. }
  141. }
  142. size_t j;
  143. for (j = 0; j < size; j++) {
  144. tpm2_tool_output("%02x", bytes[j]);
  145. }
  146. tpm2_tool_output("\n");
  147. }
  148. free(digests);
  149. return tool_rc_success;
  150. }
  151. static bool on_arg(int argc, char **argv) {
  152. if (argc > 2) {
  153. LOG_ERR("Expected FILE and PCR index at most, got %d", argc);
  154. return false;
  155. }
  156. FILE *f = NULL;
  157. const char *pcr = NULL;
  158. unsigned i;
  159. /* argc can never be negative so cast is safe */
  160. for (i = 0; i < (unsigned) argc; i++) {
  161. FILE *x = fopen(argv[i], "rb");
  162. /* file already found but got another file */
  163. if (f && x) {
  164. LOG_ERR("Only expected one file input");
  165. fclose(x);
  166. goto error;
  167. /* looking for file and got a file so assign */
  168. } else if (x && !f) {
  169. f = ctx.input = x;
  170. /* looking for pcr and not a file */
  171. } else if (!pcr) {
  172. pcr = argv[i];
  173. bool result = tpm2_util_handle_from_optarg(pcr, &ctx.pcr,
  174. TPM2_HANDLE_FLAGS_PCR);
  175. if (!result) {
  176. LOG_ERR("Could not convert \"%s\", to a pcr index.", pcr);
  177. return false;
  178. }
  179. /* got pcr and not a file (another pcr) */
  180. } else {
  181. LOG_ERR("Already got PCR index.");
  182. goto error;
  183. }
  184. }
  185. return true;
  186. error:
  187. if (f) {
  188. fclose(f);
  189. }
  190. return false;
  191. }
  192. static bool on_option(char key, char *value) {
  193. switch (key) {
  194. case 'P':
  195. ctx.auth.auth_str = value;
  196. break;
  197. /* no default */
  198. }
  199. return true;
  200. }
  201. static bool tpm2_tool_onstart(tpm2_options **opts) {
  202. static const struct option topts[] = {
  203. { "auth", required_argument, NULL, 'P' },
  204. };
  205. *opts = tpm2_options_new("P:", ARRAY_LEN(topts), topts, on_option, on_arg,
  206. 0);
  207. return *opts != NULL;
  208. }
  209. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  210. UNUSED(flags);
  211. ctx.input = ctx.input ? ctx.input : stdin;
  212. tool_rc rc = tpm2_auth_util_from_optarg(ectx, ctx.auth.auth_str,
  213. &ctx.auth.session, false);
  214. if (rc != tool_rc_success) {
  215. LOG_ERR("Invalid key handle authorization");
  216. return rc;
  217. }
  218. return do_pcrevent_and_output(ectx);
  219. }
  220. static tool_rc tpm2_tool_onstop(ESYS_CONTEXT *ectx) {
  221. UNUSED(ectx);
  222. return tpm2_session_close(&ctx.auth.session);
  223. }
  224. static void tpm2_tool_onexit(void) {
  225. if (ctx.input && ctx.input != stdin) {
  226. fclose(ctx.input);
  227. }
  228. }
  229. // Register this tool with tpm2_tool.c
  230. TPM2_TOOL_REGISTER("pcrevent", tpm2_tool_onstart, tpm2_tool_onrun, tpm2_tool_onstop, tpm2_tool_onexit)