tss2_authorizepolicy.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 *policyPath;
  9. char const *keyPath; /* the path to the signing key */
  10. char const *policyRef;
  11. } ctx;
  12. /* Parse commandline parameters */
  13. static bool on_option(char key, char *value) {
  14. switch (key) {
  15. case 'P':
  16. ctx.policyPath = value;
  17. break;
  18. case 'p':
  19. ctx.keyPath = value;
  20. break;
  21. case 'r':
  22. ctx.policyRef = value;
  23. break;
  24. }
  25. return true;
  26. }
  27. /* Define possible commandline parameters */
  28. static bool tss2_tool_onstart(tpm2_options **opts) {
  29. struct option topts[] = {
  30. {"policyPath", required_argument, NULL, 'P'},
  31. {"keyPath", required_argument, NULL, 'p'},
  32. {"policyRef", required_argument, NULL, 'r'},
  33. };
  34. return (*opts = tpm2_options_new ("P:p:r.", 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.policyPath) {
  41. fprintf (stderr, "policy path to sign is missing, pass" \
  42. "--policyPath\n");
  43. return -1;
  44. }
  45. if (!ctx.keyPath) {
  46. fprintf (stderr, "key path for signing key is missing, pass" \
  47. "--keyPath\n");
  48. return -1;
  49. }
  50. /* Read ciphertext file */
  51. TSS2_RC r;
  52. uint8_t *policyRef = NULL;
  53. size_t policyRefSize = 0;
  54. if (ctx.policyRef){
  55. r = open_read_and_close (ctx.policyRef, (void**)&policyRef,
  56. &policyRefSize);
  57. if (r){
  58. return 1;
  59. }
  60. }
  61. /* Execute FAPI command with passed arguments */
  62. r = Fapi_AuthorizePolicy (fctx, ctx.policyPath, ctx.keyPath, policyRef,
  63. policyRefSize);
  64. if (r != TSS2_RC_SUCCESS){
  65. LOG_PERR ("Fapi_AuthorizePolicy", r);
  66. free (policyRef);
  67. return 1;
  68. }
  69. free (policyRef);
  70. return 0;
  71. }
  72. TSS2_TOOL_REGISTER("authorizepolicy", tss2_tool_onstart, tss2_tool_onrun, NULL)