tpm2_sessionconfig.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <inttypes.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "files.h"
  7. #include "log.h"
  8. #include "tpm2.h"
  9. #include "tpm2_tool.h"
  10. typedef struct tpm_sessionconfig_ctx tpm_sessionconfig_ctx;
  11. struct tpm_sessionconfig_ctx {
  12. tpm2_session *session;
  13. const char *session_path;
  14. TPMA_SESSION bmask;
  15. TPMA_SESSION flags;
  16. };
  17. static tpm_sessionconfig_ctx ctx;
  18. #define PRINT_SESSION_ATTRIBUTE(ATTR, attr) \
  19. do { \
  20. if (attrs & ATTR) { \
  21. if (is_attr_set) { \
  22. tpm2_tool_output("|"); \
  23. } else { \
  24. is_attr_set = true; \
  25. } \
  26. tpm2_tool_output(attr); \
  27. } \
  28. } while(false)
  29. static tool_rc process_output(ESYS_CONTEXT *esys_context) {
  30. ESYS_TR sessionhandle = tpm2_session_get_handle(ctx.session);
  31. if (!sessionhandle) {
  32. LOG_ERR("Session handle cannot be null");
  33. return tool_rc_general_error;
  34. }
  35. if (ctx.bmask) {
  36. return Esys_TRSess_SetAttributes(esys_context, sessionhandle, ctx.flags,
  37. ctx.bmask);
  38. }
  39. TPM2_HANDLE tpm_handle;
  40. TSS2_RC rv = Esys_TR_GetTpmHandle(esys_context, sessionhandle, &tpm_handle);
  41. if (rv != TSS2_RC_SUCCESS) {
  42. return tool_rc_general_error;
  43. }
  44. /*
  45. * Describe session attributes
  46. */
  47. TPMA_SESSION attrs = 0;
  48. tool_rc rc = tpm2_sess_get_attributes(esys_context, sessionhandle,
  49. &attrs);
  50. if (rc != tool_rc_success) {
  51. return rc;
  52. }
  53. tpm2_tool_output("Session-Handle: 0x%.8"PRIx32"\n", tpm_handle);
  54. bool is_attr_set = false;
  55. tpm2_tool_output("Session-Attributes: ");
  56. PRINT_SESSION_ATTRIBUTE(TPMA_SESSION_CONTINUESESSION, "continuesession");
  57. PRINT_SESSION_ATTRIBUTE(TPMA_SESSION_AUDITEXCLUSIVE, "auditexclusive");
  58. PRINT_SESSION_ATTRIBUTE(TPMA_SESSION_AUDITRESET, "auditreset");
  59. PRINT_SESSION_ATTRIBUTE(TPMA_SESSION_DECRYPT, "decrypt");
  60. PRINT_SESSION_ATTRIBUTE(TPMA_SESSION_ENCRYPT, "encrypt");
  61. PRINT_SESSION_ATTRIBUTE(TPMA_SESSION_AUDIT, "audit");
  62. tpm2_tool_output("\n");
  63. return tool_rc_success;
  64. }
  65. static tool_rc process_input(ESYS_CONTEXT *esys_context) {
  66. tool_rc rc = tpm2_session_restore(esys_context, ctx.session_path, false,
  67. &ctx.session);
  68. if (rc != tool_rc_success) {
  69. LOG_ERR("Could not restore session from the specified file");
  70. return rc;
  71. }
  72. return tool_rc_success;
  73. }
  74. static bool on_option(char key, char *value) {
  75. UNUSED(value);
  76. switch (key) {
  77. case 0:
  78. ctx.bmask |= TPMA_SESSION_CONTINUESESSION;
  79. ctx.flags |= TPMA_SESSION_CONTINUESESSION;
  80. break;
  81. case 1:
  82. ctx.bmask |= TPMA_SESSION_CONTINUESESSION;
  83. break;
  84. case 2:
  85. ctx.bmask |= TPMA_SESSION_AUDITEXCLUSIVE;
  86. ctx.flags |= TPMA_SESSION_AUDITEXCLUSIVE;
  87. break;
  88. case 3:
  89. ctx.bmask |= TPMA_SESSION_AUDITEXCLUSIVE;
  90. break;
  91. case 4:
  92. ctx.bmask |= TPMA_SESSION_AUDITRESET;
  93. ctx.flags |= TPMA_SESSION_AUDITRESET;
  94. break;
  95. case 5:
  96. ctx.bmask |= TPMA_SESSION_AUDITRESET;
  97. break;
  98. case 6:
  99. ctx.bmask |= TPMA_SESSION_DECRYPT;
  100. ctx.flags |= TPMA_SESSION_DECRYPT;
  101. break;
  102. case 7:
  103. ctx.bmask |= TPMA_SESSION_DECRYPT;
  104. break;
  105. case 8:
  106. ctx.bmask |= TPMA_SESSION_ENCRYPT;
  107. ctx.flags |= TPMA_SESSION_ENCRYPT;
  108. break;
  109. case 9:
  110. ctx.bmask |= TPMA_SESSION_ENCRYPT;
  111. break;
  112. case 10:
  113. ctx.bmask |= TPMA_SESSION_AUDIT;
  114. ctx.flags |= TPMA_SESSION_AUDIT;
  115. break;
  116. case 11:
  117. ctx.bmask |= TPMA_SESSION_AUDIT;
  118. break;
  119. }
  120. return true;
  121. }
  122. static bool on_args(int argc, char **argv) {
  123. if (argc > 1) {
  124. LOG_ERR("Argument takes one file name for session data");
  125. return false;
  126. }
  127. ctx.session_path = argv[0];
  128. return true;
  129. }
  130. static bool is_input_option_args_valid(void) {
  131. if (!ctx.session_path) {
  132. LOG_ERR("Must specify session file as an argument.");
  133. return false;
  134. }
  135. return true;
  136. }
  137. static bool tpm2_tool_onstart(tpm2_options **opts) {
  138. const struct option topts[] = {
  139. { "enable-continuesession", no_argument, NULL, 0 },
  140. { "disable-continuesession", no_argument, NULL, 1 },
  141. { "enable-auditexclusive", no_argument, NULL, 2 },
  142. { "disable-auditexclusive", no_argument, NULL, 3 },
  143. { "enable-auditreset", no_argument, NULL, 4 },
  144. { "disable-auditreset", no_argument, NULL, 5 },
  145. { "enable-decrypt", no_argument, NULL, 6 },
  146. { "disable-decrypt", no_argument, NULL, 7 },
  147. { "enable-encrypt", no_argument, NULL, 8 },
  148. { "disable-encrypt", no_argument, NULL, 9 },
  149. { "enable-audit", no_argument, NULL, 10 },
  150. { "disable-audit", no_argument, NULL, 11 },
  151. };
  152. *opts = tpm2_options_new(NULL, ARRAY_LEN(topts), topts, on_option, on_args,
  153. 0);
  154. return *opts != NULL;
  155. }
  156. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *esys_context,
  157. tpm2_option_flags flags) {
  158. UNUSED(flags);
  159. bool retval = is_input_option_args_valid();
  160. if (!retval) {
  161. return tool_rc_option_error;
  162. }
  163. tool_rc rc = process_input(esys_context);
  164. if(rc != tool_rc_success) {
  165. return rc;
  166. }
  167. return process_output(esys_context);
  168. /*
  169. * Disabling continuesession should flush the session after use.
  170. */
  171. }
  172. static tool_rc tpm2_tool_onstop(ESYS_CONTEXT *esys_context) {
  173. UNUSED(esys_context);
  174. return tpm2_session_close(&ctx.session);
  175. }
  176. // Register this tool with tpm2_tool.c
  177. TPM2_TOOL_REGISTER("sessionconfig", tpm2_tool_onstart, tpm2_tool_onrun,
  178. tpm2_tool_onstop, NULL)