tss2_pcrread.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 "tools/fapi/tss2_template.h"
  7. /* Context struct used to store passed command line parameters */
  8. static struct cxt {
  9. bool pcr_set;
  10. uint32_t pcrIndex;
  11. char const *pcrValue;
  12. char const *pcrLog;
  13. bool overwrite;
  14. } ctx;
  15. /* Parse command line parameters */
  16. static bool on_option(char key, char *value) {
  17. switch (key) {
  18. case 'o':
  19. ctx.pcrValue = value;
  20. break;
  21. case 'x':
  22. if (!tpm2_util_string_to_uint32 (value, &ctx.pcrIndex)) {
  23. fprintf (stderr, "The PCR index must be an integer less than "\
  24. "2**32-1\n");
  25. return false;
  26. }
  27. ctx.pcr_set = true;
  28. break;
  29. case 'f':
  30. ctx.overwrite = true;
  31. break;
  32. case 'l':
  33. ctx.pcrLog = value;
  34. break;
  35. }
  36. return true;
  37. }
  38. /* Define possible command line parameters */
  39. static bool tss2_tool_onstart(tpm2_options **opts) {
  40. struct option topts[] = {
  41. {"pcrIndex" , required_argument, NULL, 'x'},
  42. {"pcrValue" , required_argument, NULL, 'o'},
  43. {"force" , no_argument , NULL, 'f'},
  44. {"pcrLog" , required_argument, NULL, 'l'}
  45. };
  46. return (*opts = tpm2_options_new ("o:x:fl:", ARRAY_LEN(topts), topts,
  47. on_option, NULL, 0)) != NULL;
  48. }
  49. /* Execute specific tool */
  50. static int tss2_tool_onrun (FAPI_CONTEXT *fctx) {
  51. /* Check availability of required parameters */
  52. if (!ctx.pcr_set) {
  53. fprintf (stderr, "No PCR index provided, use --pcrIndex\n");
  54. return -1;
  55. }
  56. /* Check exclusive access to stdout */
  57. int count_out = 0;
  58. if (ctx.pcrValue && !strcmp (ctx.pcrValue, "-")) count_out +=1;
  59. if (ctx.pcrLog && !strcmp (ctx.pcrLog, "-")) count_out +=1;
  60. if (count_out > 1) {
  61. fprintf (stderr, "Only one of --pcrValue and --pcrLog can print to - "\
  62. "(standard output)\n");
  63. return -1;
  64. }
  65. /* Execute FAPI command with passed arguments */
  66. uint8_t *pcrValue;
  67. size_t pcrValueSize;
  68. char *pcrLog;
  69. TSS2_RC r = Fapi_PcrRead (fctx, ctx.pcrIndex, &pcrValue, &pcrValueSize,
  70. &pcrLog);
  71. if (r != TSS2_RC_SUCCESS) {
  72. LOG_PERR ("Fapi_PcrRead", r);
  73. return 1;
  74. }
  75. /* Write returned data to file(s) */
  76. if (ctx.pcrValue) {
  77. r = open_write_and_close (ctx.pcrValue, ctx.overwrite, pcrValue,
  78. pcrValueSize);
  79. if (r) {
  80. Fapi_Free (pcrLog);
  81. Fapi_Free (pcrValue);
  82. return 1;
  83. }
  84. }
  85. if (ctx.pcrLog) {
  86. r = open_write_and_close (ctx.pcrLog, ctx.overwrite, pcrLog,
  87. strlen(pcrLog));
  88. if (r) {
  89. Fapi_Free (pcrLog);
  90. Fapi_Free (pcrValue);
  91. return 1;
  92. }
  93. }
  94. Fapi_Free (pcrLog);
  95. Fapi_Free (pcrValue);
  96. return 0;
  97. }
  98. TSS2_TOOL_REGISTER("pcrread", tss2_tool_onstart, tss2_tool_onrun, NULL)