tpm2_pcrextend.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <stdlib.h>
  3. #include "log.h"
  4. #include "tpm2_tool.h"
  5. #include "tpm2_alg_util.h"
  6. #include "tpm2_options.h"
  7. typedef struct tpm_pcr_extend_ctx tpm_pcr_extend_ctx;
  8. struct tpm_pcr_extend_ctx {
  9. size_t digest_spec_len;
  10. tpm2_pcr_digest_spec *digest_spec;
  11. };
  12. static tpm_pcr_extend_ctx ctx;
  13. static tool_rc pcr_extend_one(ESYS_CONTEXT *ectx,
  14. TPMI_DH_PCR pcr_index, TPML_DIGEST_VALUES *digests) {;
  15. TSS2_RC rval = Esys_PCR_Extend(ectx, pcr_index,
  16. ESYS_TR_PASSWORD, ESYS_TR_NONE, ESYS_TR_NONE,
  17. digests);
  18. if (rval != TSS2_RC_SUCCESS) {
  19. LOG_ERR("Could not extend pcr index: 0x%X", pcr_index);
  20. LOG_PERR(Esys_PCR_Extend, rval);
  21. return tool_rc_from_tpm(rval);
  22. }
  23. return tool_rc_success;
  24. }
  25. static tool_rc pcr_extend(ESYS_CONTEXT *ectx) {
  26. size_t i;
  27. for (i = 0; i < ctx.digest_spec_len; i++) {
  28. tpm2_pcr_digest_spec *dspec = &ctx.digest_spec[i];
  29. tool_rc rc = pcr_extend_one(ectx, dspec->pcr_index,
  30. &dspec->digests);
  31. if (rc != tool_rc_success) {
  32. return rc;
  33. }
  34. }
  35. return tool_rc_success;
  36. }
  37. static bool on_arg(int argc, char **argv) {
  38. if (argc < 1) {
  39. LOG_ERR("Expected at least one PCR Digest specification,"
  40. "ie: <pcr index>:<hash alg>=<hash value>, got: 0");
  41. return false;
  42. }
  43. /* this can never be negative */
  44. ctx.digest_spec_len = (size_t) argc;
  45. ctx.digest_spec = calloc(ctx.digest_spec_len, sizeof(*ctx.digest_spec));
  46. if (!ctx.digest_spec) {
  47. LOG_ERR("oom");
  48. return false;
  49. }
  50. return pcr_parse_digest_list(argv, ctx.digest_spec_len, ctx.digest_spec);
  51. }
  52. static bool tpm2_tool_onstart(tpm2_options **opts) {
  53. *opts = tpm2_options_new(NULL, 0, NULL, NULL, on_arg, 0);
  54. return *opts != NULL;
  55. }
  56. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  57. UNUSED(flags);
  58. return pcr_extend(ectx);
  59. }
  60. static void tpm2_tool_onexit(void) {
  61. free(ctx.digest_spec);
  62. }
  63. // Register this tool with tpm2_tool.c
  64. TPM2_TOOL_REGISTER("pcrextend", tpm2_tool_onstart, tpm2_tool_onrun, NULL, tpm2_tool_onexit)