tss2_setcertificate.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include "tools/fapi/tss2_template.h"
  5. /* Context struct used to store passed command line parameters */
  6. static struct cxt {
  7. char const *path;
  8. char const *x509cert;
  9. } ctx;
  10. /* Parse command line parameters */
  11. static bool on_option(char key, char *value) {
  12. switch (key) {
  13. case 'p':
  14. ctx.path = value;
  15. break;
  16. case 'i':
  17. ctx.x509cert = value;
  18. break;
  19. }
  20. return true;
  21. }
  22. /* Define possible command line parameters */
  23. static bool tss2_tool_onstart(tpm2_options **opts) {
  24. struct option topts[] = {
  25. {"path" , required_argument, NULL, 'p'},
  26. {"x509certData", required_argument, NULL, 'i'}
  27. };
  28. return (*opts = tpm2_options_new ("p:i", ARRAY_LEN(topts), topts,
  29. on_option, NULL, 0)) != NULL;
  30. }
  31. /* Execute specific tool */
  32. static int tss2_tool_onrun (FAPI_CONTEXT *fctx) {
  33. /* Check availability of required parameters */
  34. if (!ctx.path) {
  35. fprintf (stderr, "path missing, use --path\n");
  36. return -1;
  37. }
  38. /* Read x509 certificate from file */
  39. TSS2_RC r;
  40. char* x509certData = NULL;
  41. size_t x509certSize;
  42. if (ctx.x509cert) {
  43. r = open_read_and_close (ctx.x509cert, (void**)&x509certData,
  44. &x509certSize);
  45. if (r) {
  46. return 1;
  47. }
  48. }
  49. /* Execute FAPI command with passed arguments */
  50. r = Fapi_SetCertificate (fctx, ctx.path, x509certData);
  51. if (r != TSS2_RC_SUCCESS){
  52. free (x509certData);
  53. LOG_PERR("Fapi_SetCertificate", r);
  54. return 1;
  55. }
  56. free (x509certData);
  57. return 0;
  58. }
  59. TSS2_TOOL_REGISTER("setcertificate", tss2_tool_onstart, tss2_tool_onrun, NULL)