tss2_createnv.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. /* needed to conditionally free variable authValue */
  8. static bool has_asked_for_password = false;
  9. /* Context struct used to store passed commandline parameters */
  10. static struct cxt {
  11. char const *nvPath;
  12. char const *nvTemplate;
  13. char *authValue;
  14. uint32_t size;
  15. char const *policyPath;
  16. } ctx;
  17. /* Parse commandline 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.policyPath = value;
  25. break;
  26. case 'p':
  27. ctx.nvPath = value;
  28. break;
  29. case 's':
  30. if (!tpm2_util_string_to_uint32 (value, &ctx.size)) {
  31. fprintf (stderr, "%s cannot be converted to an integer or is" \
  32. " larger than 2**32 - 1\n", value);
  33. return false;
  34. }
  35. break;
  36. case 't':
  37. ctx.nvTemplate = value;
  38. break;
  39. }
  40. return true;
  41. }
  42. /* Define possible commandline parameters */
  43. static bool tss2_tool_onstart(tpm2_options **opts) {
  44. struct option topts[] = {
  45. {"path", required_argument, NULL, 'p'},
  46. {"type", required_argument, NULL, 't'},
  47. {"size", required_argument, NULL, 's'},
  48. {"policyPath", required_argument, NULL, 'P'},
  49. {"authValue", required_argument, NULL, 'a'},
  50. };
  51. return (*opts = tpm2_options_new ("P:a:p:s:t:", ARRAY_LEN(topts), topts,
  52. on_option, NULL, 0)) != NULL;
  53. }
  54. /* Execute specific tool */
  55. static int tss2_tool_onrun (FAPI_CONTEXT *fctx) {
  56. /* Check availability of required parameters */
  57. if (!ctx.nvPath) {
  58. fprintf (stderr, "No NV path provided, use --path\n");
  59. return -1;
  60. }
  61. uint32_t size = 0;
  62. if (!ctx.size) {
  63. /* ctx.size is allowed to be zero if type is bitfield, pcr or
  64. * counter
  65. */
  66. if (!ctx.nvTemplate || !(strstr(ctx.nvTemplate, "bitfield") ||
  67. strstr(ctx.nvTemplate, "pcr") || strstr(ctx.nvTemplate, "counter"))) {
  68. fprintf (stderr, "Error: Either provide a type of \"bitfield\", "\
  69. "pcr\" or \"counter\" with --type or provide a size > 0 with "\
  70. "--size.\n");
  71. return -1;
  72. }
  73. }
  74. else {
  75. size = ctx.size;
  76. }
  77. /* If no authValue was given, prompt the user interactively */
  78. if (!ctx.authValue) {
  79. ctx.authValue = ask_for_password ();
  80. has_asked_for_password = true;
  81. if (!ctx.authValue){
  82. return 1; /* User entered two different passwords */
  83. }
  84. }
  85. /* Execute FAPI command with passed arguments */
  86. TSS2_RC r = Fapi_CreateNv(fctx, ctx.nvPath, ctx.nvTemplate,
  87. size, ctx.policyPath, ctx.authValue);
  88. if (r != TSS2_RC_SUCCESS){
  89. if(has_asked_for_password){
  90. free (ctx.authValue);
  91. }
  92. LOG_PERR ("Fapi_CreateNv", r);
  93. return 1;
  94. }
  95. if(has_asked_for_password){
  96. free (ctx.authValue);
  97. }
  98. return 0;
  99. }
  100. TSS2_TOOL_REGISTER("createnv", tss2_tool_onstart, tss2_tool_onrun, NULL)