tss2_unseal.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 const *path;
  9. char const *data;
  10. bool overwrite;
  11. } ctx;
  12. /* Parse command line parameters */
  13. static bool on_option(char key, char *value) {
  14. switch (key) {
  15. case 'f':
  16. ctx.overwrite = true;
  17. break;
  18. case 'p':
  19. ctx.path = value;
  20. break;
  21. case 'o':
  22. ctx.data = value;
  23. break;
  24. }
  25. return true;
  26. }
  27. /* Define possible command line parameters */
  28. static bool tss2_tool_onstart(tpm2_options **opts) {
  29. struct option topts[] = {
  30. {"path", required_argument, NULL, 'p'},
  31. {"data", required_argument, NULL, 'o'},
  32. {"force", no_argument, NULL, 'f'}
  33. };
  34. return (*opts = tpm2_options_new ("p:o:f", ARRAY_LEN(topts), topts,
  35. on_option, NULL, 0)) != NULL;
  36. }
  37. /* Execute specific tool */
  38. static int tss2_tool_onrun (FAPI_CONTEXT *fctx) {
  39. /* Check availability of required parameters */
  40. if (!ctx.path) {
  41. fprintf (stderr, "path to the sealed data missing, use --path\n");
  42. return -1;
  43. }
  44. /* Execute FAPI command with passed arguments */
  45. uint8_t *data;
  46. size_t size;
  47. TSS2_RC r = Fapi_Unseal (fctx, ctx.path, &data, &size);
  48. if (r != TSS2_RC_SUCCESS){
  49. LOG_PERR ("Fapi_Unseal", r);
  50. return 1;
  51. }
  52. /* Write returned data to file(s) */
  53. if (ctx.data && data) {
  54. r = open_write_and_close (ctx.data, ctx.overwrite, data, size);
  55. if (r) {
  56. Fapi_Free (data);
  57. return 1;
  58. }
  59. }
  60. Fapi_Free (data);
  61. return 0;
  62. }
  63. TSS2_TOOL_REGISTER("unseal", tss2_tool_onstart, tss2_tool_onrun, NULL)