tpm2_getcommandauditdigest.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <stdbool.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "files.h"
  7. #include "log.h"
  8. #include "tpm2.h"
  9. #include "tpm2_alg_util.h"
  10. #include "tpm2_convert.h"
  11. #include "tpm2_openssl.h"
  12. #include "tpm2_tool.h"
  13. typedef struct tpm_getcommandauditdigest_ctx tpm_getcommandauditdigest_ctx;
  14. struct tpm_getcommandauditdigest_ctx {
  15. struct {
  16. const char *ctx_path;
  17. const char *auth_str;
  18. tpm2_loaded_object object;
  19. } key;
  20. struct {
  21. const char *ctx_path;
  22. const char *auth_str;
  23. tpm2_loaded_object object;
  24. } endorsement_hierarchy;
  25. char *signature_path;
  26. char *message_path;
  27. tpm2_convert_sig_fmt sig_format;
  28. TPMI_ALG_HASH sig_hash_algorithm;
  29. TPM2B_DATA qualification_data;
  30. TPM2B_ATTEST *audit_info;
  31. TPMT_SIGNATURE *signature;
  32. TPMT_SIG_SCHEME in_scheme;
  33. };
  34. static tpm_getcommandauditdigest_ctx ctx = {
  35. .sig_hash_algorithm = TPM2_ALG_NULL,
  36. .qualification_data = TPM2B_EMPTY_INIT,
  37. .endorsement_hierarchy = {
  38. .ctx_path = "e"
  39. },
  40. .in_scheme = {
  41. .scheme = TPM2_ALG_NULL,
  42. }
  43. };
  44. static bool on_option(char key, char *value) {
  45. switch (key) {
  46. case 'P':
  47. ctx.endorsement_hierarchy.auth_str = value;
  48. break;
  49. case 'c':
  50. ctx.key.ctx_path = value;
  51. break;
  52. case 'p':
  53. ctx.key.auth_str = value;
  54. break;
  55. case 'q':
  56. ctx.qualification_data.size = sizeof(ctx.qualification_data.buffer);
  57. return tpm2_util_bin_from_hex_or_file(value, &ctx.qualification_data.size,
  58. ctx.qualification_data.buffer);
  59. break;
  60. case 's':
  61. ctx.signature_path = value;
  62. break;
  63. case 'm':
  64. ctx.message_path = value;
  65. break;
  66. case 'f':
  67. ctx.sig_format = tpm2_convert_sig_fmt_from_optarg(value);
  68. if (ctx.sig_format == signature_format_err) {
  69. return false;
  70. }
  71. break;
  72. case 'g':
  73. ctx.sig_hash_algorithm = tpm2_alg_util_from_optarg(value,
  74. tpm2_alg_util_flags_hash);
  75. if (ctx.sig_hash_algorithm == TPM2_ALG_ERROR) {
  76. LOG_ERR(
  77. "Could not convert signature hash algorithm selection, got: \"%s\"",
  78. value);
  79. return false;
  80. }
  81. break;
  82. }
  83. return true;
  84. }
  85. static bool tpm2_tool_onstart(tpm2_options **opts) {
  86. static const struct option topts[] = {
  87. { "hierarchy-auth", required_argument, NULL, 'P' },
  88. { "key-context", required_argument, NULL, 'c' },
  89. { "auth", required_argument, NULL, 'p' },
  90. { "qualification", required_argument, NULL, 'q' },
  91. { "signature", required_argument, NULL, 's' },
  92. { "message", required_argument, NULL, 'm' },
  93. { "format", required_argument, NULL, 'f' },
  94. { "hash-algorithm", required_argument, NULL, 'g' }
  95. };
  96. *opts = tpm2_options_new("P:c:p:q:s:m:f:g:", ARRAY_LEN(topts), topts,
  97. on_option, NULL, 0);
  98. return *opts != NULL;
  99. }
  100. static bool check_input_options_and_args(void) {
  101. if (!ctx.key.ctx_path) {
  102. LOG_ERR("Specify the signing key to use for signing attestation.");
  103. return false;
  104. }
  105. if (!ctx.signature_path) {
  106. LOG_ERR("Specify the file path to store the signature of the attestation data.");
  107. return false;
  108. }
  109. if (!ctx.message_path) {
  110. LOG_ERR("Specify the file path to store the attestation data.");
  111. return false;
  112. }
  113. return true;
  114. }
  115. static tool_rc process_inputs(ESYS_CONTEXT *ectx) {
  116. /*
  117. * Load auths
  118. */
  119. tool_rc rc = tpm2_util_object_load_auth(ectx,
  120. ctx.endorsement_hierarchy.ctx_path, ctx.endorsement_hierarchy.auth_str,
  121. &ctx.endorsement_hierarchy.object, false, TPM2_HANDLE_FLAGS_E);
  122. if (rc != tool_rc_success) {
  123. LOG_ERR("Invalid endorsement hierarchy authorization");
  124. return rc;
  125. }
  126. rc = tpm2_util_object_load_auth(ectx, ctx.key.ctx_path,
  127. ctx.key.auth_str, &ctx.key.object, false,
  128. TPM2_HANDLES_FLAGS_TRANSIENT|TPM2_HANDLES_FLAGS_PERSISTENT);
  129. if (rc != tool_rc_success) {
  130. LOG_ERR("Invalid key authorization");
  131. return rc;
  132. }
  133. /*
  134. * Setup signature scheme
  135. */
  136. rc = tpm2_alg_util_get_signature_scheme(ectx,
  137. ctx.key.object.tr_handle, &ctx.sig_hash_algorithm, TPM2_ALG_NULL,
  138. &ctx.in_scheme);
  139. if (rc != tool_rc_success) {
  140. return rc;
  141. }
  142. return tool_rc_success;
  143. }
  144. static tool_rc process_outputs(ESYS_CONTEXT *ectx) {
  145. UNUSED(ectx);
  146. bool result = true;
  147. if (ctx.signature_path) {
  148. result = tpm2_convert_sig_save(ctx.signature, ctx.sig_format,
  149. ctx.signature_path);
  150. }
  151. if (!result) {
  152. LOG_ERR("Failed to save the signature data.");
  153. return tool_rc_general_error;
  154. }
  155. if (ctx.message_path) {
  156. result = files_save_bytes_to_file(ctx.message_path,
  157. (UINT8*) ctx.audit_info->attestationData, ctx.audit_info->size);
  158. }
  159. if (!result) {
  160. LOG_ERR("Failed to save the attestation data.");
  161. return tool_rc_general_error;
  162. }
  163. return tool_rc_success;
  164. }
  165. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  166. UNUSED(flags);
  167. //Check input arguments
  168. bool result = check_input_options_and_args();
  169. if (!result) {
  170. return tool_rc_option_error;
  171. }
  172. //Process inputs
  173. tool_rc rc = process_inputs(ectx);
  174. if (rc != tool_rc_success) {
  175. return rc;
  176. }
  177. //ESAPI call
  178. rc = tpm2_getcommandauditdigest(ectx, &ctx.endorsement_hierarchy.object,
  179. &ctx.key.object, &ctx.in_scheme, &ctx.qualification_data, &ctx.audit_info,
  180. &ctx.signature);
  181. if (rc != tool_rc_success) {
  182. return rc;
  183. }
  184. //Process Outputs
  185. rc = process_outputs(ectx);
  186. if (rc != tool_rc_success) {
  187. return rc;
  188. }
  189. return tool_rc_success;
  190. }
  191. static tool_rc tpm2_tool_onstop(ESYS_CONTEXT *ectx) {
  192. UNUSED(ectx);
  193. tool_rc rc = tpm2_session_close(&ctx.key.object.session);
  194. if (rc != tool_rc_success) {
  195. LOG_ERR("Failed closing auth session for signing key handle.");
  196. }
  197. tool_rc tmp_rc = tpm2_session_close(
  198. &ctx.endorsement_hierarchy.object.session);
  199. if (rc != tool_rc_success) {
  200. LOG_ERR("Failed closing auth session for endorsement hierarchy handle.");
  201. return tmp_rc;
  202. }
  203. return rc;
  204. }
  205. // Register this tool with tpm2_tool.c
  206. TPM2_TOOL_REGISTER("getcommandauditdigest", tpm2_tool_onstart, tpm2_tool_onrun, tpm2_tool_onstop, NULL)