tss2_createseal.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. /* needed to conditionally free variable authValue */
  7. static bool has_asked_for_password = false;
  8. /* Context struct used to store passed command line parameters */
  9. static struct cxt {
  10. char const *keyPath;
  11. char const *keyType;
  12. char const *policyPath;
  13. char *authValue;
  14. char const *data;
  15. uint32_t size;
  16. } ctx;
  17. /* Parse command line parameters */
  18. static bool on_option(char key, char *value) {
  19. switch (key) {
  20. case 'a':
  21. ctx.authValue = value;
  22. break;
  23. case 'p':
  24. ctx.keyPath = value;
  25. break;
  26. case 'P':
  27. ctx.policyPath = value;
  28. break;
  29. case 't':
  30. ctx.keyType = value;
  31. break;
  32. case 'i':
  33. ctx.data = value;
  34. break;
  35. case 's':
  36. if (!tpm2_util_string_to_uint32 (value, &ctx.size)) {
  37. fprintf (stderr, "%s cannot be converted to an integer or is" \
  38. " larger than 2**32 - 1\n", value);
  39. return false;
  40. }
  41. if (ctx.size == 0) {
  42. LOG_ERR("Size parameter must be larger than 0\n");
  43. return false;
  44. }
  45. break;
  46. }
  47. return true;
  48. }
  49. /* Define possible command line parameters */
  50. static bool tss2_tool_onstart(tpm2_options **opts) {
  51. struct option topts[] = {
  52. {"path", required_argument, NULL, 'p'},
  53. {"type", required_argument, NULL, 't'},
  54. {"policyPath", required_argument, NULL, 'P'},
  55. {"authValue", required_argument, NULL, 'a'},
  56. {"data", required_argument, NULL, 'i'},
  57. {"size", required_argument, NULL, 's'}
  58. };
  59. return (*opts = tpm2_options_new ("a:p:P:t:i:s:", ARRAY_LEN(topts), topts,
  60. on_option, NULL, 0)) != NULL;
  61. }
  62. /* Execute specific tool */
  63. static int tss2_tool_onrun (FAPI_CONTEXT *fctx) {
  64. /* Check availability of required parameters */
  65. if (!ctx.keyPath) {
  66. fprintf (stderr, "key path missing, use --path\n");
  67. return -1;
  68. }
  69. if (!ctx.data && !ctx.size) {
  70. fprintf (stderr, "One of --data or --size "\
  71. "must be used\n");
  72. return -1;
  73. }
  74. if (ctx.data && ctx.size) {
  75. fprintf (stderr, "Only one of --data and --size "\
  76. "can be used\n");
  77. return -1;
  78. }
  79. /* If no authValue was given, prompt the user interactively */
  80. if (!ctx.authValue) {
  81. ctx.authValue = ask_for_password ();
  82. has_asked_for_password = true;
  83. if (!ctx.authValue){
  84. return 1; /* User entered two different passwords */
  85. }
  86. }
  87. /* Read data file */
  88. TSS2_RC r;
  89. uint8_t* data = NULL;
  90. size_t dataSize = 0;
  91. if (ctx.data) {
  92. r = open_read_and_close (ctx.data, (void**)&data, &dataSize);
  93. if (r) {
  94. return 1;
  95. }
  96. }
  97. else {
  98. if (ctx.size) {
  99. dataSize = ctx.size;
  100. }
  101. }
  102. /* Execute FAPI command with passed arguments */
  103. r = Fapi_CreateSeal (fctx, ctx.keyPath, ctx.keyType,
  104. dataSize, ctx.policyPath, ctx.authValue, data);
  105. if (r != TSS2_RC_SUCCESS){
  106. if(has_asked_for_password){
  107. free (ctx.authValue);
  108. }
  109. free (data);
  110. LOG_PERR ("Fapi_CreateSeal", r);
  111. return 1;
  112. }
  113. free (data);
  114. if(has_asked_for_password){
  115. free (ctx.authValue);
  116. }
  117. return 0;
  118. }
  119. TSS2_TOOL_REGISTER("createseal", tss2_tool_onstart, tss2_tool_onrun, NULL)