tss2_verifysignature.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 commandline parameters */
  7. static struct cxt {
  8. char const *digest;
  9. char const *publicKeyPath;
  10. char const *signature;
  11. } ctx;
  12. /* Parse command line parameters */
  13. static bool on_option(char key, char *value) {
  14. switch (key) {
  15. case 'd':
  16. ctx.digest = value;
  17. break;
  18. case 'p':
  19. ctx.publicKeyPath = value;
  20. break;
  21. case 'i':
  22. ctx.signature = 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. {"keyPath", required_argument, NULL, 'p'},
  31. {"digest", required_argument, NULL, 'd'},
  32. {"signature", required_argument, NULL, 'i'}
  33. };
  34. return (*opts = tpm2_options_new ("d:p:i:", 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.publicKeyPath) {
  41. fprintf (stderr, "public key path parameter not provided, use " \
  42. "--keyPath\n");
  43. return -1;
  44. }
  45. if (!ctx.digest) {
  46. fprintf (stderr, "digest parameter not provided, use --digest\n");
  47. return -1;
  48. }
  49. if (!ctx.signature) {
  50. fprintf (stderr, "signature parameter not provided, use "\
  51. "--signature\n");
  52. return -1;
  53. }
  54. /* Check exclusive access to stdin */
  55. int count_in = 0;
  56. if (ctx.digest && !strcmp (ctx.digest, "-")) count_in +=1;
  57. if (ctx.signature && !strcmp (ctx.signature, "-")) count_in +=1;
  58. if (count_in > 1) {
  59. fprintf (stderr, "Only one of --digest and --signature can read from -"\
  60. "(standard input)\n");
  61. return -1;
  62. }
  63. /* Read data needed for signature verification */
  64. uint8_t *digest, *signature;
  65. size_t digestSize, signatureSize;
  66. TSS2_RC r = open_read_and_close (ctx.digest, (void**)&digest, &digestSize);
  67. if (r){
  68. return 1;
  69. }
  70. r = open_read_and_close (ctx.signature, (void**)&signature, &signatureSize);
  71. if (r) {
  72. free (digest);
  73. return 1;
  74. }
  75. /* Execute FAPI command with passed arguments */
  76. r = Fapi_VerifySignature (fctx, ctx.publicKeyPath,
  77. digest, digestSize, signature, signatureSize);
  78. if (r != TSS2_RC_SUCCESS){
  79. free (digest);
  80. free (signature);
  81. LOG_PERR("Fapi_Key_VerifySignature", r);
  82. return 1;
  83. }
  84. free (digest);
  85. free (signature);
  86. return 0;
  87. }
  88. TSS2_TOOL_REGISTER("verifysignature", tss2_tool_onstart, tss2_tool_onrun, NULL)