tss2_exportkey.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 *pathOfKeyToDuplicate;
  10. char const *pathToPublicKeyOfNewParent;
  11. char const *exportedData;
  12. bool overwrite;
  13. } ctx;
  14. /* Parse command line parameters */
  15. static bool on_option(char key, char *value) {
  16. switch (key) {
  17. case 'f':
  18. ctx.overwrite = true;
  19. break;
  20. case 'e':
  21. ctx.pathToPublicKeyOfNewParent = value;
  22. break;
  23. case 'o':
  24. ctx.exportedData = value;
  25. break;
  26. case 'p':
  27. ctx.pathOfKeyToDuplicate = 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. {"pathToPublicKeyOfNewParent", required_argument, NULL, 'e'},
  36. {"force", no_argument , NULL, 'f'},
  37. {"exportedData", required_argument, NULL, 'o'},
  38. {"pathOfKeyToDuplicate", required_argument, NULL, 'p'}
  39. };
  40. return (*opts = tpm2_options_new ("fe:o:p:", ARRAY_LEN(topts), topts,
  41. on_option, NULL, 0)) != NULL;
  42. }
  43. /* Execute specific tool */
  44. static int tss2_tool_onrun (FAPI_CONTEXT *fctx) {
  45. /* Check availability of required parameters */
  46. if (!ctx.exportedData) {
  47. fprintf (stderr, "exported data missing, use --output\n");
  48. return -1;
  49. }
  50. if (!ctx.pathOfKeyToDuplicate) {
  51. fprintf (stderr, "path of key to duplicate missing, use --path\n");
  52. return -1;
  53. }
  54. /* Execute FAPI command with passed arguments */
  55. char *exportedData;
  56. TSS2_RC r = Fapi_ExportKey (fctx, ctx.pathOfKeyToDuplicate,
  57. ctx.pathToPublicKeyOfNewParent, &exportedData);
  58. if (r != TSS2_RC_SUCCESS) {
  59. LOG_PERR ("Fapi_ExportKey", r);
  60. return 1;
  61. }
  62. /* Write returned data to file(s) */
  63. r = open_write_and_close (ctx.exportedData, ctx.overwrite, exportedData,
  64. strlen(exportedData));
  65. if (r){
  66. Fapi_Free (exportedData);
  67. return 1;
  68. }
  69. Fapi_Free (exportedData);
  70. return 0;
  71. }
  72. TSS2_TOOL_REGISTER("exportkey", tss2_tool_onstart, tss2_tool_onrun, NULL)