tpm2_getsessionauditdigest.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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_getsessionauditdigest_ctx tpm_getsessionauditdigest_ctx;
  14. struct tpm_getsessionauditdigest_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. tpm2_session *audit_session;
  34. const char *audit_session_path;
  35. ESYS_TR audit_session_handle;
  36. };
  37. static tpm_getsessionauditdigest_ctx ctx = {
  38. .sig_hash_algorithm = TPM2_ALG_NULL,
  39. .qualification_data = TPM2B_EMPTY_INIT,
  40. .endorsement_hierarchy = {
  41. .ctx_path = "e"
  42. },
  43. .in_scheme = {
  44. .scheme = TPM2_ALG_NULL,
  45. },
  46. .audit_session_handle = ESYS_TR_NONE,
  47. };
  48. static bool on_option(char key, char *value) {
  49. switch (key) {
  50. case 'P':
  51. ctx.endorsement_hierarchy.auth_str = value;
  52. break;
  53. case 'c':
  54. ctx.key.ctx_path = value;
  55. break;
  56. case 'p':
  57. ctx.key.auth_str = value;
  58. break;
  59. case 'q':
  60. ctx.qualification_data.size = sizeof(ctx.qualification_data.buffer);
  61. return tpm2_util_bin_from_hex_or_file(value, &ctx.qualification_data.size,
  62. ctx.qualification_data.buffer);
  63. break;
  64. case 's':
  65. ctx.signature_path = value;
  66. break;
  67. case 'm':
  68. ctx.message_path = value;
  69. break;
  70. case 'f':
  71. ctx.sig_format = tpm2_convert_sig_fmt_from_optarg(value);
  72. if (ctx.sig_format == signature_format_err) {
  73. return false;
  74. }
  75. break;
  76. case 'g':
  77. ctx.sig_hash_algorithm = tpm2_alg_util_from_optarg(value,
  78. tpm2_alg_util_flags_hash);
  79. if (ctx.sig_hash_algorithm == TPM2_ALG_ERROR) {
  80. LOG_ERR(
  81. "Could not convert signature hash algorithm selection, got: \"%s\"",
  82. value);
  83. return false;
  84. }
  85. break;
  86. case 'S':
  87. ctx.audit_session_path = value;
  88. break;
  89. }
  90. return true;
  91. }
  92. static bool tpm2_tool_onstart(tpm2_options **opts) {
  93. static const struct option topts[] = {
  94. { "hierarchy-auth", required_argument, NULL, 'P' },
  95. { "key-context", required_argument, NULL, 'c' },
  96. { "auth", required_argument, NULL, 'p' },
  97. { "qualification", required_argument, NULL, 'q' },
  98. { "signature", required_argument, NULL, 's' },
  99. { "message", required_argument, NULL, 'm' },
  100. { "format", required_argument, NULL, 'f' },
  101. { "hash-algorithm", required_argument, NULL, 'g' },
  102. { "session", required_argument, NULL, 'S' }
  103. };
  104. *opts = tpm2_options_new("S:P:c:p:q:s:m:f:g:", ARRAY_LEN(topts), topts,
  105. on_option, NULL, 0);
  106. return *opts != NULL;
  107. }
  108. static bool check_input_options_and_args(void) {
  109. if (!ctx.key.ctx_path) {
  110. LOG_ERR("Specify the signing key to use for signing attestation.");
  111. return false;
  112. }
  113. if (!ctx.signature_path) {
  114. LOG_ERR("Specify the file path to store the signature of the attestation data.");
  115. return false;
  116. }
  117. if (!ctx.message_path) {
  118. LOG_ERR("Specify the file path to store the attestation data.");
  119. return false;
  120. }
  121. if (!ctx.audit_session_path) {
  122. LOG_ERR("Specify the session to be used to start audit.");
  123. return false;
  124. }
  125. return true;
  126. }
  127. static tool_rc process_inputs(ESYS_CONTEXT *ectx) {
  128. tool_rc rc = tpm2_session_restore(ectx, ctx.audit_session_path,
  129. false, &ctx.audit_session);
  130. if (rc != tool_rc_success) {
  131. LOG_ERR("Could not restore audit session");
  132. return rc;
  133. }
  134. ctx.audit_session_handle = tpm2_session_get_handle(ctx.audit_session);
  135. /*
  136. * Load auths
  137. */
  138. rc = tpm2_util_object_load_auth(ectx,
  139. ctx.endorsement_hierarchy.ctx_path, ctx.endorsement_hierarchy.auth_str,
  140. &ctx.endorsement_hierarchy.object, false, TPM2_HANDLE_FLAGS_E);
  141. if (rc != tool_rc_success) {
  142. LOG_ERR("Invalid endorsement hierarchy authorization");
  143. return rc;
  144. }
  145. rc = tpm2_util_object_load_auth(ectx, ctx.key.ctx_path,
  146. ctx.key.auth_str, &ctx.key.object, false,
  147. TPM2_HANDLES_FLAGS_TRANSIENT|TPM2_HANDLES_FLAGS_PERSISTENT);
  148. if (rc != tool_rc_success) {
  149. LOG_ERR("Invalid key authorization");
  150. return rc;
  151. }
  152. /*
  153. * Setup signature scheme
  154. */
  155. rc = tpm2_alg_util_get_signature_scheme(ectx,
  156. ctx.key.object.tr_handle, &ctx.sig_hash_algorithm, TPM2_ALG_NULL,
  157. &ctx.in_scheme);
  158. if (rc != tool_rc_success) {
  159. return rc;
  160. }
  161. return tool_rc_success;
  162. }
  163. static tool_rc process_outputs(ESYS_CONTEXT *ectx) {
  164. UNUSED(ectx);
  165. bool result = true;
  166. if (ctx.signature_path) {
  167. result = tpm2_convert_sig_save(ctx.signature, ctx.sig_format,
  168. ctx.signature_path);
  169. }
  170. if (!result) {
  171. LOG_ERR("Failed to save the signature data.");
  172. return tool_rc_general_error;
  173. }
  174. if (ctx.message_path) {
  175. result = files_save_bytes_to_file(ctx.message_path,
  176. (UINT8*) ctx.audit_info->attestationData, ctx.audit_info->size);
  177. }
  178. if (!result) {
  179. LOG_ERR("Failed to save the attestation data.");
  180. return tool_rc_general_error;
  181. }
  182. return tool_rc_success;
  183. }
  184. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  185. UNUSED(flags);
  186. //Check input arguments
  187. bool result = check_input_options_and_args();
  188. if (!result) {
  189. return tool_rc_option_error;
  190. }
  191. //Process inputs
  192. tool_rc rc = process_inputs(ectx);
  193. if (rc != tool_rc_success) {
  194. return rc;
  195. }
  196. //ESAPI call
  197. rc = tpm2_getsessionauditdigest(ectx, &ctx.endorsement_hierarchy.object,
  198. &ctx.key.object, &ctx.in_scheme, &ctx.qualification_data, &ctx.audit_info,
  199. &ctx.signature, ctx.audit_session_handle);
  200. if (rc != tool_rc_success) {
  201. return rc;
  202. }
  203. //Process Outputs
  204. rc = process_outputs(ectx);
  205. if (rc != tool_rc_success) {
  206. return rc;
  207. }
  208. return tool_rc_success;
  209. }
  210. static tool_rc tpm2_tool_onstop(ESYS_CONTEXT *ectx) {
  211. UNUSED(ectx);
  212. tool_rc rc = tpm2_session_close(&ctx.audit_session);
  213. if (rc != tool_rc_success) {
  214. LOG_ERR("Failed closing audit session.");
  215. }
  216. rc = tpm2_session_close(&ctx.key.object.session);
  217. if (rc != tool_rc_success) {
  218. LOG_ERR("Failed closing auth session for signing key handle.");
  219. }
  220. rc = tpm2_session_close(&ctx.endorsement_hierarchy.object.session);
  221. if (rc != tool_rc_success) {
  222. LOG_ERR("Failed closing auth session for endorsement hierarchy handle.");
  223. }
  224. return rc;
  225. }
  226. // Register this tool with tpm2_tool.c
  227. TPM2_TOOL_REGISTER("getsessionauditdigest", tpm2_tool_onstart, tpm2_tool_onrun, tpm2_tool_onstop, NULL)