tss2_pcrextend.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. uint32_t pcr;
  10. char const *data;
  11. char const *logData;
  12. } ctx;
  13. /* Parse command line parameters */
  14. static bool on_option(char key, char *value) {
  15. switch (key) {
  16. case 'x':
  17. if (!tpm2_util_string_to_uint32 (value, &ctx.pcr)) {
  18. fprintf (stderr, "%s cannot be converted to an integer or is"\
  19. "larger than 2**32 - 1\n", value);
  20. return false;
  21. }
  22. break;
  23. case 'i':
  24. ctx.data = value;
  25. break;
  26. case 'l':
  27. ctx.logData = value;
  28. break;
  29. }
  30. return true;
  31. }
  32. /* Define possible command line parameters */
  33. static bool tss2_tool_onstart(tpm2_options **opts) {
  34. struct option topts[] = {
  35. {"pcr" , required_argument, NULL, 'x'},
  36. {"data", required_argument, NULL, 'i'},
  37. {"logData", required_argument, NULL, 'l'}
  38. };
  39. return (*opts = tpm2_options_new ("x:i:l", ARRAY_LEN(topts), topts,
  40. on_option, NULL, 0)) != NULL;
  41. }
  42. /* Execute specific tool */
  43. static int tss2_tool_onrun (FAPI_CONTEXT *fctx) {
  44. /* Check availability of required parameters */
  45. if (!ctx.pcr) {
  46. fprintf (stderr, "No pcr provided, use --pcr\n");
  47. return -1;
  48. }
  49. if (!ctx.data) {
  50. fprintf (stderr, "No event data provided, use --data\n");
  51. return -1;
  52. }
  53. /* Check exclusive access to stdin */
  54. int count_in = 0;
  55. if (ctx.data && !strcmp (ctx.data, "-")) count_in +=1;
  56. if (ctx.logData && !strcmp (ctx.logData, "-")) count_in +=1;
  57. if (count_in > 1) {
  58. fprintf (stderr, "Only one of --data and --logData can read from - "\
  59. "(standard input)\n");
  60. return -1;
  61. }
  62. /* Read event data and log data from file */
  63. uint8_t *data = NULL;
  64. size_t eventDataSize;
  65. TSS2_RC r = open_read_and_close (ctx.data, (void**)&data,
  66. &eventDataSize);
  67. if (r){
  68. return -1;
  69. }
  70. char *logData = NULL;
  71. if (ctx.logData) {
  72. r = open_read_and_close (ctx.logData, (void**)&logData, 0);
  73. if (r) {
  74. free (data);
  75. return -1;
  76. }
  77. }
  78. /* Execute FAPI command with passed arguments */
  79. r = Fapi_PcrExtend(fctx, ctx.pcr, data, eventDataSize, logData);
  80. if (r != TSS2_RC_SUCCESS){
  81. free (data);
  82. free (logData);
  83. LOG_PERR ("Fapi_PcrExtend", r);
  84. return 1;
  85. }
  86. free (data);
  87. free (logData);
  88. return 0;
  89. }
  90. TSS2_TOOL_REGISTER("pcrextend", tss2_tool_onstart, tss2_tool_onrun, NULL)