tpm2_setcommandauditstatus.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <stdlib.h>
  3. #include "files.h"
  4. #include "log.h"
  5. #include "tpm2.h"
  6. #include "tpm2_alg_util.h"
  7. #include "tpm2_cc_util.h"
  8. #include "tpm2_policy.h"
  9. #include "tpm2_tool.h"
  10. typedef struct tpm2_setcommandauditstatus_ctx tpm2_setcommandauditstatus_ctx;
  11. struct tpm2_setcommandauditstatus_ctx {
  12. struct {
  13. const char *ctx_path;
  14. const char *auth_str;
  15. tpm2_loaded_object object;
  16. } hierarchy;
  17. TPML_CC command_code_list;
  18. TPMI_ALG_HASH hash_algorithm;
  19. bool clear_list;
  20. };
  21. static tpm2_setcommandauditstatus_ctx ctx = {
  22. .hierarchy = {
  23. .ctx_path = "o",
  24. },
  25. .hash_algorithm = TPM2_ALG_SHA256,
  26. .clear_list = false
  27. };
  28. static bool on_option(char key, char *value) {
  29. switch (key) {
  30. case 'C':
  31. ctx.hierarchy.ctx_path = value;
  32. break;
  33. case 'P':
  34. ctx.hierarchy.auth_str = value;
  35. break;
  36. case 'c':
  37. ctx.clear_list = true;
  38. break;
  39. case 'g':
  40. ctx.hash_algorithm = tpm2_alg_util_from_optarg(value,
  41. tpm2_alg_util_flags_hash);
  42. if (ctx.hash_algorithm == TPM2_ALG_ERROR) {
  43. return false;
  44. }
  45. break;
  46. default:
  47. LOG_ERR("Unknown option");
  48. return false;
  49. }
  50. return true;
  51. }
  52. static bool on_arg(int argc, char **argv) {
  53. if (argc > 1 || !argc) {
  54. LOG_ERR("Specify a TPM2 command to add/ remove from audit list.");
  55. return false;
  56. }
  57. if (ctx.command_code_list.count > TPM2_MAX_CAP_CC) {
  58. LOG_ERR("List of commands exceeds maximum supported command count");
  59. return false;
  60. }
  61. bool result = tpm2_cc_util_from_str(argv[0],
  62. &ctx.command_code_list.commandCodes[ctx.command_code_list.count]);
  63. if (!result) {
  64. return false;
  65. }
  66. ctx.command_code_list.count+=1;
  67. return true;
  68. }
  69. static bool tpm2_tool_onstart(tpm2_options **opts) {
  70. static struct option topts[] = {
  71. { "hierarchy", required_argument, NULL, 'C' },
  72. { "hierarchy-auth", required_argument, NULL, 'P' },
  73. { "clear-list", no_argument, NULL, 'c' },
  74. { "hash-algorithm", required_argument, NULL, 'g' },
  75. };
  76. *opts = tpm2_options_new("C:P:g:c", ARRAY_LEN(topts), topts, on_option,
  77. on_arg, 0);
  78. return *opts != NULL;
  79. }
  80. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  81. UNUSED(flags);
  82. tool_rc rc = tpm2_util_object_load_auth(ectx, ctx.hierarchy.ctx_path,
  83. ctx.hierarchy.auth_str , &ctx.hierarchy.object, false,
  84. TPM2_HANDLE_FLAGS_O|TPM2_HANDLE_FLAGS_P);
  85. if (rc != tool_rc_success) {
  86. return rc;
  87. }
  88. TPML_CC empty_list = { 0 };
  89. /*
  90. * TPM does not allow to set commandaudit digest and commands to audit
  91. * simultaneously. So first set the command audit digest.
  92. */
  93. rc = tpm2_setcommandcodeaudit(ectx, &ctx.hierarchy.object, ctx.hash_algorithm,
  94. &empty_list, &empty_list);
  95. if (rc != tool_rc_success) {
  96. LOG_ERR("Failed to set command audit digest.");
  97. return rc;
  98. }
  99. rc = ctx.clear_list ?
  100. tpm2_setcommandcodeaudit(ectx, &ctx.hierarchy.object, ctx.hash_algorithm,
  101. &empty_list, &ctx.command_code_list) :
  102. tpm2_setcommandcodeaudit(ectx, &ctx.hierarchy.object, ctx.hash_algorithm,
  103. &ctx.command_code_list, &empty_list);
  104. return rc;
  105. }
  106. static tool_rc tpm2_tool_onstop(ESYS_CONTEXT *ectx) {
  107. UNUSED(ectx);
  108. return tpm2_session_close(&ctx.hierarchy.object.session);
  109. }
  110. // Register this tool with tpm2_tool.c
  111. TPM2_TOOL_REGISTER("setcommandauditstatus", tpm2_tool_onstart, tpm2_tool_onrun, tpm2_tool_onstop, NULL)