tss2_verifyquote.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "tools/fapi/tss2_template.h"
  6. /* Context struct used to store passed command line parameters */
  7. static struct cxt {
  8. char *publicKeyPath;
  9. char const *qualifyingData;
  10. char const *quoteInfo;
  11. char const *signature;
  12. char const *pcrLog;
  13. } ctx;
  14. /* Parse command line parameters */
  15. static bool on_option(char key, char *value) {
  16. switch (key) {
  17. case 'Q':
  18. ctx.qualifyingData = value;
  19. break;
  20. case 'l':
  21. ctx.pcrLog = value;
  22. break;
  23. case 'q':
  24. ctx.quoteInfo = value;
  25. break;
  26. case 'k':
  27. ctx.publicKeyPath = value;
  28. break;
  29. case 'i':
  30. ctx.signature = value;
  31. break;
  32. }
  33. return true;
  34. }
  35. /* Define possible command line parameters */
  36. static bool tss2_tool_onstart(tpm2_options **opts) {
  37. struct option topts[] = {
  38. {"publicKeyPath", required_argument, NULL, 'k'},
  39. {"qualifyingData", required_argument, NULL, 'Q'},
  40. {"quoteInfo", required_argument, NULL, 'q'},
  41. {"signature", required_argument, NULL, 'i'},
  42. {"pcrLog", required_argument, NULL, 'l'}
  43. };
  44. return (*opts = tpm2_options_new ("k:Q:q:i:l:", ARRAY_LEN(topts), topts,
  45. on_option, NULL, 0)) != NULL;
  46. }
  47. /* Execute specific tool */
  48. static int tss2_tool_onrun (FAPI_CONTEXT *fctx) {
  49. /* Check availability of required parameters */
  50. if (!ctx.quoteInfo) {
  51. fprintf (stderr, "quote info parameter not provided, use "\
  52. "--quoteInfo\n");
  53. return -1;
  54. }
  55. if (!ctx.publicKeyPath) {
  56. fprintf (stderr, "publicKeyPath parameter not provided, use "\
  57. "--publicKeyPath\n");
  58. return -1;
  59. }
  60. if (!ctx.signature) {
  61. fprintf (stderr, "signature parameter not provided, use"\
  62. " --signature\n");
  63. return -1;
  64. }
  65. /* Check exclusive access to stdin */
  66. int count_in = 0;
  67. if (ctx.qualifyingData && !strcmp (ctx.qualifyingData, "-")) count_in +=1;
  68. if (ctx.signature && !strcmp (ctx.signature, "-")) count_in +=1;
  69. if (ctx.quoteInfo && !strcmp (ctx.quoteInfo, "-")) count_in +=1;
  70. if (ctx.pcrLog && !strcmp (ctx.pcrLog, "-")) count_in +=1;
  71. if (count_in > 1) {
  72. fprintf (stderr, "Only one of --qualifyingData, --signature, "\
  73. " --quoteInfo and --pcrLog can read from - (standard input)\n");
  74. return -1;
  75. }
  76. /* Read qualifyingData, signature, quoteInfo and pcrLog from file */
  77. TSS2_RC r;
  78. uint8_t *qualifyingData = NULL;
  79. size_t qualifyingDataSize = 0;
  80. if (ctx.qualifyingData) {
  81. r = open_read_and_close (ctx.qualifyingData,
  82. (void**)&qualifyingData, &qualifyingDataSize);
  83. if (r) {
  84. return -1;
  85. }
  86. }
  87. uint8_t *signature = NULL;
  88. size_t signatureSize = 0;
  89. if (ctx.signature) {
  90. r = open_read_and_close (ctx.signature, (void**)&signature, &signatureSize);
  91. if (r) {
  92. free (qualifyingData);
  93. return -1;
  94. }
  95. }
  96. char *quoteInfo = NULL;
  97. if (ctx.quoteInfo) {
  98. r = open_read_and_close (ctx.quoteInfo, (void**)&quoteInfo, NULL);
  99. if (r) {
  100. free (qualifyingData);
  101. free (signature);
  102. return -1;
  103. }
  104. }
  105. char *pcrLog = NULL;
  106. if (ctx.pcrLog) {
  107. r = open_read_and_close (ctx.pcrLog, (void**)&pcrLog, NULL);
  108. if (r) {
  109. free (qualifyingData);
  110. free (signature);
  111. free (quoteInfo);
  112. return -1;
  113. }
  114. }
  115. /* Execute FAPI command with passed arguments */
  116. r = Fapi_VerifyQuote (fctx, ctx.publicKeyPath, qualifyingData,
  117. qualifyingDataSize, quoteInfo, signature, signatureSize,
  118. pcrLog);
  119. if (r != TSS2_RC_SUCCESS){
  120. free (qualifyingData);
  121. free (signature);
  122. free (quoteInfo);
  123. free (pcrLog);
  124. LOG_PERR ("Fapi_VerifyQuote", r);
  125. return 1;
  126. }
  127. free (qualifyingData);
  128. free (signature);
  129. free (quoteInfo);
  130. free (pcrLog);
  131. return 0;
  132. }
  133. TSS2_TOOL_REGISTER("verifyquote", tss2_tool_onstart, tss2_tool_onrun, NULL)