tss2_getplatformcertificates.c 2.0 KB

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