tpm2_policypcr.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <stdbool.h>
  3. #include <stdlib.h>
  4. #include "files.h"
  5. #include "log.h"
  6. #include "pcr.h"
  7. #include "tpm2_policy.h"
  8. #include "tpm2_tool.h"
  9. typedef struct tpm2_policypcr_ctx tpm2_policypcr_ctx;
  10. struct tpm2_policypcr_ctx {
  11. const char *session_path;
  12. const char *raw_pcrs_file;
  13. TPML_PCR_SELECTION pcr_selection;
  14. const char *policy_out_path;
  15. TPM2B_DIGEST *raw_pcr_digest;
  16. tpm2_session *session;
  17. };
  18. static tpm2_policypcr_ctx ctx;
  19. static bool on_arg(int argc, char **argv) {
  20. if ((ctx.raw_pcrs_file && argv[0]) || argc > 1) {
  21. LOG_ERR("Specify either pcr-digest or pcr data, not both");
  22. return false;
  23. }
  24. ctx.raw_pcr_digest = malloc(sizeof(TPM2B_DIGEST));
  25. if (!ctx.raw_pcr_digest) {
  26. LOG_ERR("oom");
  27. return tool_rc_general_error;
  28. }
  29. int q;
  30. ctx.raw_pcr_digest->size = BUFFER_SIZE(TPM2B_DIGEST, buffer);
  31. if ((q = tpm2_util_hex_to_byte_structure(argv[0], &ctx.raw_pcr_digest->size,
  32. ctx.raw_pcr_digest->buffer)) != 0) {
  33. free(ctx.raw_pcr_digest);
  34. LOG_ERR("FAILED: %d", q);
  35. return false;
  36. }
  37. return true;
  38. }
  39. static bool on_option(char key, char *value) {
  40. switch (key) {
  41. case 'L':
  42. ctx.policy_out_path = value;
  43. break;
  44. case 'f':
  45. ctx.raw_pcrs_file = value;
  46. break;
  47. case 'l': {
  48. bool result = pcr_parse_selections(value, &ctx.pcr_selection);
  49. if (!result) {
  50. LOG_ERR("Could not parse PCR selections");
  51. return false;
  52. }
  53. }
  54. break;
  55. case 'S':
  56. ctx.session_path = value;
  57. break;
  58. }
  59. return true;
  60. }
  61. static bool tpm2_tool_onstart(tpm2_options **opts) {
  62. static struct option topts[] = {
  63. { "policy", required_argument, NULL, 'L' },
  64. { "pcr", required_argument, NULL, 'f' },
  65. { "pcr-list", required_argument, NULL, 'l' },
  66. { "session", required_argument, NULL, 'S' },
  67. };
  68. *opts = tpm2_options_new("L:f:l:S:", ARRAY_LEN(topts), topts, on_option,
  69. on_arg, 0);
  70. return *opts != NULL;
  71. }
  72. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  73. UNUSED(flags);
  74. bool option_fail = false;
  75. if (!ctx.session_path) {
  76. LOG_ERR("Must specify -S session file.");
  77. option_fail = true;
  78. }
  79. if (!ctx.pcr_selection.count) {
  80. LOG_ERR("Must specify -l pcr selection list.");
  81. option_fail = true;
  82. }
  83. if (option_fail) {
  84. return tool_rc_general_error;
  85. }
  86. tool_rc rc = tpm2_session_restore(ectx, ctx.session_path, false,
  87. &ctx.session);
  88. if (rc != tool_rc_success) {
  89. return rc;
  90. }
  91. rc = tpm2_policy_build_pcr(ectx, ctx.session, ctx.raw_pcrs_file,
  92. &ctx.pcr_selection, ctx.raw_pcr_digest);
  93. if (rc != tool_rc_success) {
  94. LOG_ERR("Could not build pcr policy");
  95. return rc;
  96. }
  97. return tpm2_policy_tool_finish(ectx, ctx.session, ctx.policy_out_path);
  98. }
  99. static tool_rc tpm2_tool_onstop(ESYS_CONTEXT *ectx) {
  100. UNUSED(ectx);
  101. free(ctx.raw_pcr_digest);
  102. return tpm2_session_close(&ctx.session);
  103. }
  104. // Register this tool with tpm2_tool.c
  105. TPM2_TOOL_REGISTER("policypcr", tpm2_tool_onstart, tpm2_tool_onrun, tpm2_tool_onstop, NULL)