tss2_sign.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 commandline parameters */
  8. static struct cxt {
  9. char const *keyPath;
  10. char const *digest;
  11. char const *signature;
  12. char const *publicKey;
  13. char const *certificate;
  14. bool overwrite;
  15. char const *padding;
  16. } ctx;
  17. /* Parse command line parameters */
  18. static bool on_option(char key, char *value) {
  19. switch (key) {
  20. case 'c':
  21. ctx.certificate = value;
  22. break;
  23. case 'd':
  24. ctx.digest = value;
  25. break;
  26. case 'f':
  27. ctx.overwrite = true;
  28. break;
  29. case 'p':
  30. ctx.keyPath = value;
  31. break;
  32. case 'k':
  33. ctx.publicKey = value;
  34. break;
  35. case 'o':
  36. ctx.signature = value;
  37. break;
  38. case 's':
  39. ctx.padding = value;
  40. break;
  41. }
  42. return true;
  43. }
  44. /* Define possible command line parameters */
  45. static bool tss2_tool_onstart(tpm2_options **opts) {
  46. struct option topts[] = {
  47. {"keyPath", required_argument, NULL, 'p'},
  48. {"padding", required_argument, NULL, 's'},
  49. {"digest", required_argument, NULL, 'd'},
  50. {"signature", required_argument, NULL, 'o'},
  51. {"publicKey", required_argument, NULL, 'k'},
  52. {"force", no_argument , NULL, 'f'},
  53. {"certificate", required_argument, NULL, 'c'},
  54. };
  55. return (*opts = tpm2_options_new ("c:d:fp:k:o:s:", ARRAY_LEN(topts), topts,
  56. on_option, NULL, 0)) != NULL;
  57. }
  58. /* Execute specific tool */
  59. static int tss2_tool_onrun (FAPI_CONTEXT *fctx) {
  60. /* Check availability of required parameters */
  61. if (!ctx.digest) {
  62. fprintf (stderr, "digest missing, use --digest\n");
  63. return -1;
  64. }
  65. if (!ctx.keyPath) {
  66. fprintf (stderr, "key path missing, use --keyPath\n");
  67. return -1;
  68. }
  69. if (!ctx.signature) {
  70. fprintf (stderr, "signature missing, use --signature\n");
  71. return -1;
  72. }
  73. /* Check exclusive access to stdout */
  74. int count_out = 0;
  75. if (ctx.certificate && !strcmp (ctx.certificate, "-")) count_out +=1;
  76. if (ctx.signature && !strcmp (ctx.signature, "-")) count_out +=1;
  77. if (ctx.publicKey && !strcmp (ctx.publicKey, "-")) count_out +=1;
  78. if (count_out > 1) {
  79. fprintf (stderr, "Only one of --certificate, --signature and "\
  80. "--publicKey can print to - (standard output)\n");
  81. return -1;
  82. }
  83. /* Read data needed to create signature */
  84. uint8_t *digest, *signature;
  85. size_t digestSize, signatureSize;
  86. char *publicKey, *certificate = NULL;
  87. TSS2_RC r = open_read_and_close (ctx.digest, (void**)&digest, &digestSize);
  88. if (r){
  89. return 1;
  90. }
  91. /* Execute FAPI command with passed arguments */
  92. r = Fapi_Sign (fctx, ctx.keyPath, ctx.padding, digest,
  93. digestSize, &signature, &signatureSize, &publicKey, &certificate);
  94. if (r != TSS2_RC_SUCCESS) {
  95. LOG_PERR ("Fapi_Sign", r);
  96. free (digest);
  97. return 1;
  98. }
  99. free (digest);
  100. /* Write returned data to file(s) */
  101. if (ctx.certificate && certificate && strlen(certificate)) {
  102. r = open_write_and_close (ctx.certificate, ctx.overwrite,
  103. certificate, strlen(certificate));
  104. if (r) {
  105. Fapi_Free (certificate);
  106. Fapi_Free (signature);
  107. Fapi_Free (publicKey);
  108. return 1;
  109. }
  110. }
  111. Fapi_Free (certificate);
  112. if (ctx.signature && signature) {
  113. r = open_write_and_close (ctx.signature, ctx.overwrite, signature,
  114. signatureSize);
  115. if (r) {
  116. Fapi_Free (signature);
  117. Fapi_Free (publicKey);
  118. return 1;
  119. }
  120. }
  121. Fapi_Free (signature);
  122. if (ctx.publicKey && publicKey) {
  123. r = open_write_and_close (ctx.publicKey, ctx.overwrite, publicKey,
  124. strlen(publicKey));
  125. if (r) {
  126. Fapi_Free (publicKey);
  127. return 1;
  128. }
  129. }
  130. Fapi_Free (publicKey);
  131. return 0;
  132. }
  133. TSS2_TOOL_REGISTER("sign", tss2_tool_onstart, tss2_tool_onrun, NULL)