tpm2_send.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <errno.h>
  3. #include <inttypes.h>
  4. #include <signal.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include "files.h"
  9. #include "log.h"
  10. #include "tpm2_header.h"
  11. #include "tpm2_tool.h"
  12. typedef struct tpm2_send_ctx tpm2_send_ctx;
  13. struct tpm2_send_ctx {
  14. FILE *input;
  15. FILE *output;
  16. tpm2_command_header *command;
  17. };
  18. typedef void (*sighandler_t)(int);
  19. static tpm2_send_ctx ctx;
  20. static void sig_handler(int signum) {
  21. UNUSED(signum);
  22. exit (tool_rc_success);
  23. }
  24. static int read_command_from_file(FILE *f, tpm2_command_header **c,
  25. UINT32 *size) {
  26. UINT8 buffer[TPM2_COMMAND_HEADER_SIZE];
  27. size_t ret = fread(buffer, TPM2_COMMAND_HEADER_SIZE, 1, f);
  28. if (ret != 1 && ferror(f) && errno != EINTR) {
  29. LOG_ERR("Failed to read command header: %s", strerror (errno));
  30. return -1;
  31. }
  32. if (feof(f) || ferror(f)) {
  33. return 0;
  34. }
  35. tpm2_command_header *header = tpm2_command_header_from_bytes(buffer);
  36. UINT32 command_size = tpm2_command_header_get_size(header, true);
  37. UINT32 data_size = tpm2_command_header_get_size(header, false);
  38. if (command_size > TPM2_MAX_SIZE || command_size < data_size) {
  39. LOG_ERR("Command buffer %"PRIu32" bytes cannot be smaller then the "
  40. "encapsulated data %"PRIu32" bytes, and can not be bigger than"
  41. " the maximum buffer size", command_size, data_size);
  42. return -1;
  43. }
  44. tpm2_command_header *command = (tpm2_command_header *) malloc(command_size);
  45. if (!command) {
  46. LOG_ERR("oom");
  47. return -1;
  48. }
  49. /* copy the header into the struct */
  50. memcpy(command, buffer, sizeof(buffer));
  51. LOG_INFO("command tag: 0x%04x", tpm2_command_header_get_tag(command));
  52. LOG_INFO("command size: 0x%08x", command_size);
  53. LOG_INFO("command code: 0x%08x", tpm2_command_header_get_code(command));
  54. ret = fread(command->data, data_size, 1, f);
  55. if (ret != 1 && ferror(f)) {
  56. LOG_ERR("Failed to read command body: %s", strerror (errno));
  57. free(command);
  58. return -1;
  59. }
  60. *c = command;
  61. *size = command_size;
  62. return 1;
  63. }
  64. static bool write_response_to_file(FILE *f, UINT8 *rbuf) {
  65. tpm2_response_header *r = tpm2_response_header_from_bytes(rbuf);
  66. UINT32 size = tpm2_response_header_get_size(r, true);
  67. LOG_INFO("response tag: 0x%04x", tpm2_response_header_get_tag(r));
  68. LOG_INFO("response size: 0x%08x", size);
  69. LOG_INFO("response code: 0x%08x", tpm2_response_header_get_code(r));
  70. bool rc = files_write_bytes(f, r->bytes, size);
  71. fflush(f);
  72. return rc;
  73. }
  74. static FILE *open_file(const char *path, const char *mode) {
  75. FILE *f = fopen(path, mode);
  76. if (!f) {
  77. LOG_ERR("Could not open \"%s\", error: \"%s\"", path, strerror(errno));
  78. }
  79. return f;
  80. }
  81. static void close_file(FILE *f) {
  82. if (f && (f != stdin || f != stdout)) {
  83. fclose(f);
  84. }
  85. }
  86. static bool on_option(char key, char *value) {
  87. switch (key) {
  88. case 'o':
  89. ctx.output = open_file(value, "wb");
  90. if (!ctx.output) {
  91. return false;
  92. }
  93. break;
  94. /* no break */
  95. }
  96. return true;
  97. }
  98. static bool on_args(int argc, char **argv) {
  99. if (argc > 1) {
  100. LOG_ERR("Expected 1 tpm buffer input file, got: %d", argc);
  101. return false;
  102. }
  103. ctx.input = fopen(argv[0], "rb");
  104. if (!ctx.input) {
  105. LOG_ERR("Error opening file \"%s\", error: %s", argv[0],
  106. strerror(errno));
  107. return false;
  108. }
  109. return true;
  110. }
  111. static bool tpm2_tool_onstart(tpm2_options **opts) {
  112. static const struct option topts[] = {
  113. { "output", required_argument, NULL, 'o' },
  114. };
  115. *opts = tpm2_options_new("o:", ARRAY_LEN(topts), topts, on_option, on_args,
  116. 0);
  117. ctx.input = stdin;
  118. ctx.output = stdout;
  119. return *opts != NULL;
  120. }
  121. /*
  122. * This program reads a TPM command buffer from stdin then dumps it out
  123. * to a tabd TCTI. It then reads the response from the TCTI and writes it
  124. * to stdout. Like the TCTI, we expect the input TPM command buffer to be
  125. * in network byte order (big-endian). We output the response in the same
  126. * form.
  127. */
  128. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *context, tpm2_option_flags flags) {
  129. UNUSED(flags);
  130. sighandler_t old_handler = signal(SIGINT, sig_handler);
  131. if(old_handler == SIG_ERR) {
  132. LOG_WARN("Could not set SIGINT handler: %s", strerror(errno));
  133. }
  134. TSS2_TCTI_CONTEXT *tcti_context;
  135. TSS2_RC rval = Esys_GetTcti(context, &tcti_context);
  136. if (rval != TPM2_RC_SUCCESS) {
  137. LOG_PERR(Esys_GetTctiContext, rval);
  138. return tool_rc_from_tpm(rval);
  139. }
  140. while (1) {
  141. UINT32 size;
  142. int result = read_command_from_file(ctx.input, &ctx.command, &size);
  143. if (result < 0) {
  144. LOG_ERR("failed to read TPM2 command buffer from file");
  145. return tool_rc_general_error;
  146. } else if (result == 0) {
  147. return tool_rc_success;
  148. }
  149. rval = Tss2_Tcti_Transmit(tcti_context, size, ctx.command->bytes);
  150. if (rval != TPM2_RC_SUCCESS) {
  151. LOG_ERR("tss2_tcti_transmit failed: 0x%x", rval);
  152. return tool_rc_from_tpm(rval);
  153. }
  154. size_t rsize = TPM2_MAX_SIZE;
  155. UINT8 rbuf[TPM2_MAX_SIZE];
  156. rval = Tss2_Tcti_Receive(tcti_context, &rsize, rbuf,
  157. TSS2_TCTI_TIMEOUT_BLOCK);
  158. if (rval != TPM2_RC_SUCCESS) {
  159. LOG_ERR("tss2_tcti_receive failed: 0x%x", rval);
  160. return tool_rc_from_tpm(rval);
  161. }
  162. /*
  163. * The response buffer, rbuf, all fields are in big-endian, and we save
  164. * in big-endian.
  165. */
  166. result = write_response_to_file(ctx.output, rbuf);
  167. if (!result) {
  168. LOG_ERR("Failed writing response to output file.");
  169. return tool_rc_general_error;
  170. }
  171. free(ctx.command);
  172. ctx.command = NULL;
  173. }
  174. /* shouldn't be possible */
  175. return tool_rc_success;
  176. }
  177. static void tpm2_tool_onexit(void) {
  178. close_file(ctx.input);
  179. close_file(ctx.output);
  180. free(ctx.command);
  181. }
  182. // Register this tool with tpm2_tool.c
  183. TPM2_TOOL_REGISTER("send", tpm2_tool_onstart, tpm2_tool_onrun, NULL, tpm2_tool_onexit)