tss2_gettpmblobs.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. char const *path;
  10. char const *tpm2bPublic;
  11. char const *tpm2bPrivate;
  12. char const *policy;
  13. bool overwrite;
  14. } ctx;
  15. /* Parse command line parameters */
  16. static bool on_option(char key, char *value) {
  17. switch (key) {
  18. case 'f':
  19. ctx.overwrite = true;
  20. break;
  21. case 'p':
  22. ctx.path = value;
  23. break;
  24. case 'u':
  25. ctx.tpm2bPublic = value;
  26. break;
  27. case 'r':
  28. ctx.tpm2bPrivate = value;
  29. break;
  30. case 'l':
  31. ctx.policy = value;
  32. break;
  33. }
  34. return true;
  35. }
  36. /* Define possible command line parameters */
  37. static bool tss2_tool_onstart(tpm2_options **opts) {
  38. struct option topts[] = {
  39. {"force" , no_argument , NULL, 'f'},
  40. {"path" , required_argument, NULL, 'p'},
  41. {"tpm2bPublic" , required_argument, NULL, 'u'},
  42. {"tpm2bPrivate" , required_argument, NULL, 'r'},
  43. {"policy" , required_argument, NULL, 'l'},
  44. };
  45. return (*opts = tpm2_options_new ("fp:u:r:l", ARRAY_LEN(topts), topts,
  46. on_option, NULL, 0)) != NULL;
  47. }
  48. /* Execute specific tool */
  49. static int tss2_tool_onrun (FAPI_CONTEXT *fctx) {
  50. /* Check availability of required parameters */
  51. if (!ctx.path) {
  52. fprintf (stderr, "path missing, use --path\n");
  53. return -1;
  54. }
  55. /* Check exclusive access to stdout */
  56. int count_out = 0;
  57. if (ctx.tpm2bPublic && !strcmp (ctx.tpm2bPublic, "-")) count_out +=1;
  58. if (ctx.tpm2bPrivate && !strcmp (ctx.tpm2bPrivate, "-")) count_out +=1;
  59. if (ctx.policy && !strcmp (ctx.policy, "-")) count_out +=1;
  60. if (count_out > 1) {
  61. fprintf (stderr, "Only one of --tpm2bPublic, --tpm2bPrivate and "\
  62. "--policy can print to - (standard output)\n");
  63. return -1;
  64. }
  65. /* Execute FAPI command with passed arguments */
  66. uint8_t *tpm2bPublic;
  67. size_t tpm2bPublicSize;
  68. uint8_t *tpm2bPrivate;
  69. size_t tpm2bPrivateSize;
  70. char *policy;
  71. TSS2_RC r = Fapi_GetTpmBlobs (fctx, ctx.path, &tpm2bPublic,
  72. &tpm2bPublicSize, &tpm2bPrivate, &tpm2bPrivateSize, &policy);
  73. if (r != TSS2_RC_SUCCESS) {
  74. LOG_PERR ("Fapi_GetTpmBlobs", r);
  75. return 1;
  76. }
  77. /* Write returned data to file(s) */
  78. if (ctx.tpm2bPublic) {
  79. r = open_write_and_close (ctx.tpm2bPublic, ctx.overwrite, tpm2bPublic,
  80. tpm2bPublicSize);
  81. if (r) {
  82. Fapi_Free (tpm2bPublic);
  83. return 1;
  84. }
  85. }
  86. if (ctx.tpm2bPrivate) {
  87. r = open_write_and_close (ctx.tpm2bPrivate, ctx.overwrite, tpm2bPrivate,
  88. tpm2bPrivateSize);
  89. if (r) {
  90. Fapi_Free (tpm2bPublic);
  91. Fapi_Free (tpm2bPrivate);
  92. return 1;
  93. }
  94. }
  95. if (ctx.policy) {
  96. r = open_write_and_close (ctx.policy, ctx.overwrite, policy,
  97. strlen(policy));
  98. if (r) {
  99. Fapi_Free (tpm2bPublic);
  100. Fapi_Free (tpm2bPrivate);
  101. Fapi_Free (policy);
  102. return 1;
  103. }
  104. }
  105. Fapi_Free (tpm2bPublic);
  106. Fapi_Free (tpm2bPrivate);
  107. Fapi_Free (policy);
  108. return 0;
  109. }
  110. TSS2_TOOL_REGISTER("gettpmblobs", tss2_tool_onstart, tss2_tool_onrun, NULL)