tpm2_pcrallocate.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include "log.h"
  3. #include "pcr.h"
  4. #include "tpm2.h"
  5. #include "tpm2_tool.h"
  6. #include "tpm2_options.h"
  7. static struct {
  8. TPML_PCR_SELECTION pcr_selection;
  9. struct {
  10. const char *ctx_path;
  11. const char *auth_str;
  12. tpm2_loaded_object object;
  13. } auth_hierarchy;
  14. } ctx = {
  15. .pcr_selection = {
  16. .count = 2,
  17. .pcrSelections = { {
  18. .hash = TPM2_ALG_SHA1,
  19. .sizeofSelect = 3,
  20. .pcrSelect = { 0xff, 0xff, 0xff, }
  21. }, {
  22. .hash = TPM2_ALG_SHA256,
  23. .sizeofSelect = 3,
  24. .pcrSelect = { 0xff, 0xff, 0xff, }
  25. }, }
  26. },
  27. .auth_hierarchy.ctx_path = "platform",
  28. };
  29. static bool on_arg(int argc, char **argv) {
  30. if (argc > 1) {
  31. LOG_ERR("Too many arguments");
  32. return false;
  33. }
  34. if (argc == 1 && !pcr_parse_selections(argv[0], &ctx.pcr_selection)) {
  35. LOG_ERR("Could not parse pcr selections");
  36. return false;
  37. }
  38. return true;
  39. }
  40. static bool on_option(char key, char *value) {
  41. switch (key) {
  42. case 'P':
  43. ctx.auth_hierarchy.auth_str = value;
  44. break;
  45. }
  46. return true;
  47. }
  48. static bool tpm2_tool_onstart(tpm2_options **opts) {
  49. const struct option topts[] = { { "auth", required_argument, NULL, 'P' }, };
  50. *opts = tpm2_options_new("P:", ARRAY_LEN(topts), topts, on_option, on_arg,
  51. 0);
  52. return *opts != NULL;
  53. }
  54. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  55. UNUSED(flags);
  56. tool_rc rc = tpm2_util_object_load_auth(ectx, ctx.auth_hierarchy.ctx_path,
  57. ctx.auth_hierarchy.auth_str, &ctx.auth_hierarchy.object, false,
  58. TPM2_HANDLE_FLAGS_P);
  59. if (rc != tool_rc_success) {
  60. LOG_ERR("Invalid platform authorization format.");
  61. return rc;
  62. }
  63. rc = tpm2_pcr_allocate(ectx, &ctx.auth_hierarchy.object,
  64. &ctx.pcr_selection);
  65. if (rc == tool_rc_success) {
  66. pcr_print_pcr_selections(&ctx.pcr_selection);
  67. }
  68. return rc;
  69. }
  70. static tool_rc tpm2_tool_onstop(ESYS_CONTEXT *ectx) {
  71. UNUSED(ectx);
  72. return tpm2_session_close(&ctx.auth_hierarchy.object.session);
  73. }
  74. // Register this tool with tpm2_tool.c
  75. TPM2_TOOL_REGISTER("pcrallocate", tpm2_tool_onstart, tpm2_tool_onrun, tpm2_tool_onstop, NULL)