tss2_getcertificate.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 *path;
  10. char const *x509cert;
  11. bool overwrite;
  12. } ctx;
  13. /* Parse command line parameters */
  14. static bool on_option(char key, char *value) {
  15. switch (key) {
  16. case 'f':
  17. ctx.overwrite = true;
  18. break;
  19. case 'p':
  20. ctx.path = value;
  21. break;
  22. case 'o':
  23. ctx.x509cert = value;
  24. break;
  25. }
  26. return true;
  27. }
  28. /* Define possible command line parameters */
  29. static bool tss2_tool_onstart(tpm2_options **opts) {
  30. struct option topts[] = {
  31. {"force" , no_argument , NULL, 'f'},
  32. {"path" , required_argument, NULL, 'p'},
  33. {"x509certData", required_argument, NULL, 'o'}
  34. };
  35. return (*opts = tpm2_options_new ("fp:o:", ARRAY_LEN(topts), topts,
  36. on_option, NULL, 0)) != NULL;
  37. }
  38. /* Execute specific tool */
  39. static int tss2_tool_onrun (FAPI_CONTEXT *fctx) {
  40. /* Check availability of required parameters */
  41. if (!ctx.path) {
  42. fprintf (stderr, "path missing, use --path\n");
  43. return -1;
  44. }
  45. if (!ctx.x509cert) {
  46. fprintf (stderr, "x509certData missing, use --x509certData\n");
  47. return -1;
  48. }
  49. /* Execute FAPI command with passed arguments */
  50. char *x509certData;
  51. TSS2_RC r = Fapi_GetCertificate (fctx, ctx.path, &x509certData);
  52. if (r != TSS2_RC_SUCCESS) {
  53. LOG_PERR ("Fapi_GetCertificate", r);
  54. return 1;
  55. }
  56. /* Write returned data to file(s) */
  57. r = open_write_and_close (ctx.x509cert, ctx.overwrite, x509certData,
  58. strlen(x509certData));
  59. if (r){
  60. Fapi_Free (x509certData);
  61. return 1;
  62. }
  63. Fapi_Free (x509certData);
  64. return 0;
  65. }
  66. TSS2_TOOL_REGISTER("getcertificate", tss2_tool_onstart, tss2_tool_onrun, NULL)