tpm2_session.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #ifndef SRC_TPM2_SESSION_H_
  3. #define SRC_TPM2_SESSION_H_
  4. #include <stdbool.h>
  5. #include <tss2/tss2_esys.h>
  6. #include "tool_rc.h"
  7. typedef struct tpm2_session_data tpm2_session_data;
  8. typedef struct tpm2_session tpm2_session;
  9. /**
  10. * Creates a new session data object, based around the inputs to
  11. * TPM2_StartAuthSession as listed in Section 11.1:
  12. * https://trustedcomputinggroup.org/wp-content/uploads/TPM-Rev-2.0-Part-3-Commands-01.38.pdf
  13. *
  14. * The defaults are set to:
  15. * tpmKey = TPM2_RH_NULL
  16. * bind = TPM2_RH_NULL
  17. * nonceCaller = a SHA1 hash of all 0s.
  18. * symmetric = TPM2_ALG_NULL
  19. * authHash = TPM2_ALG_SHA256
  20. ^ *
  21. * @param type
  22. * The type of policy session, one of:
  23. * - TPM2_SE_HMAC - For an HMAC session.
  24. * - TPM2_SE_POLICY - For a policy session.
  25. * - TPM2_SE_TRIAL - For a trial session, useful for building policies.
  26. * @return
  27. * A tpm2_session_data object on success, NULL on failure.
  28. */
  29. tpm2_session_data *tpm2_session_data_new(TPM2_SE type);
  30. /**
  31. * Sets the tpmKey parameter.
  32. * @param data
  33. * The session data object to modify.
  34. * @param key
  35. * The tpmKey parameter value itself.
  36. */
  37. void tpm2_session_set_key(tpm2_session_data *data, TPMI_DH_OBJECT key);
  38. /**
  39. * Sets the nonceCaller parameter.
  40. * @param data
  41. * The session data object to modify.
  42. * @param nonce
  43. * The nonce parameter value itself.
  44. */
  45. void tpm2_session_set_nonce_caller(tpm2_session_data *data, TPM2B_NONCE *nonce);
  46. /**
  47. * Retrieves the session nonce
  48. *
  49. * @param ectx
  50. * The ESAPI context
  51. * @param session
  52. * The session started
  53. * @param nonce_tpm
  54. * The nonceTPM for the session
  55. *
  56. */
  57. tool_rc tpm2_session_get_noncetpm(ESYS_CONTEXT *ectx, tpm2_session *session,
  58. TPM2B_NONCE **nonce_tpm);
  59. /**
  60. * Sets the bind parameter.
  61. * @param data
  62. * The session data object to modify.
  63. * @param bind
  64. * The bind parameter value itself.
  65. */
  66. void tpm2_session_set_bind(tpm2_session_data *data, TPMI_DH_ENTITY bind);
  67. /**
  68. * Sets the symmetric parameter.
  69. * @param data
  70. * The session data object to modify.
  71. * @param symmetric
  72. * The symmetric parameter value itself.
  73. */
  74. void tpm2_session_set_symmetric(tpm2_session_data *data,
  75. TPMT_SYM_DEF *symmetric);
  76. /**
  77. * Sets the authHash parameter.
  78. * @param data
  79. * The session data object to modify.
  80. * @param auth_hash
  81. * The authHash parameter value itself.
  82. */
  83. void tpm2_session_set_authhash(tpm2_session_data *data, TPMI_ALG_HASH auth_hash);
  84. void tpm2_session_set_path(tpm2_session_data *data, const char *path);
  85. /**
  86. * Set the session attributes
  87. * @param data
  88. * The session data to set
  89. * @param attrs
  90. * The session attributes to use.
  91. */
  92. void tpm2_session_set_attrs(tpm2_session_data *data, TPMA_SESSION attrs);
  93. /**
  94. * Retrieves the authHash parameter used to start the authorization session.
  95. * @param session
  96. * The tpm2_session started with tpm2_session_new().
  97. * @return
  98. * The authHash value.
  99. */
  100. TPMI_ALG_HASH tpm2_session_get_authhash(tpm2_session *session);
  101. /**
  102. * Retrieves the session handle from starting the authorization session
  103. * with tpm2_session_new().
  104. * @param session
  105. * The session started with tpm2_session_new().
  106. * @return
  107. * The session handle.
  108. */
  109. ESYS_TR tpm2_session_get_handle(tpm2_session *session);
  110. /**
  111. * Retrieves the type of session, ie trial or policy session.
  112. * @param session
  113. * @return
  114. * The type of the session, either TPM2_SE_HMAC, TPM2_SE_POLICY or
  115. * TPM2_SE_TRIAL.
  116. */
  117. TPM2_SE tpm2_session_get_type(tpm2_session *session);
  118. /**
  119. * True if a session is of type TPM2_SE_TRIAL
  120. * @param session
  121. * The session to check the type of.
  122. * @return
  123. * True if a session is of type TPM2_SE_TRIAL, false otherwise.
  124. */
  125. static inline bool tpm2_session_is_trial(tpm2_session *session) {
  126. return tpm2_session_get_type(session) == TPM2_SE_TRIAL;
  127. }
  128. /**
  129. * Starts a session with the tpm via StartAuthSession().
  130. * @param context
  131. * The Enhanced System API (ESAPI) context.
  132. * @param data
  133. * A session data object created with tpm2_session_data_new() and potentially
  134. * modified with the tpm2_session_data_set_*() routines.
  135. * This pointer is owned by the tpm2_session object and the caller can
  136. * forget about it at this point.
  137. * @param session
  138. * The output session on success.
  139. * @return
  140. * A tool_rc indicating status.
  141. */
  142. tool_rc tpm2_session_open(ESYS_CONTEXT *context, tpm2_session_data *data,
  143. tpm2_session **session);
  144. /**
  145. * Saves session data to disk allowing tpm2_session_from_file() to
  146. * restore the session if applicable and frees resources.
  147. *
  148. * @Note
  149. * This is accomplished by calling:
  150. * - Eys_ContextSave - marks to some RMs like tpm2-abrmd not to flush this session
  151. * handle on client disconnection.
  152. * - Eys_ContextLoad - restores the session so it can be used.
  153. * - Saving a custom file format at path - records the handle and algorithm.
  154. * @param session
  155. * The session context to save
  156. * @return
  157. * tool_rc indicating status.
  158. */
  159. tool_rc tpm2_session_close(tpm2_session **session);
  160. /**
  161. * Restores a session saved with tpm2_session_save().
  162. * @param context
  163. * The Enhanced System API (ESAPI) context
  164. * @param path
  165. * The path to restore from.
  166. * @param is_final
  167. * True if this is is the last tool to use the session, causes a flush.
  168. * @param session
  169. * The session
  170. * @return
  171. * tool_rc indicating status.
  172. */
  173. tool_rc tpm2_session_restore(ESYS_CONTEXT *ctx, const char *path, bool is_final,
  174. tpm2_session **session);
  175. /**
  176. * restarts the session to it's initial state via a call to
  177. * PolicyRestart().
  178. * @param context
  179. * The Enhanced System API (ESAPI) context
  180. * @param s
  181. * The session
  182. * @return
  183. * tool_rc indicating status.
  184. */
  185. tool_rc tpm2_session_restart(ESYS_CONTEXT *context, tpm2_session *s);
  186. tpm2_session_data *tpm2_hmac_session_data_new(TPM2B_AUTH *auth_value);
  187. void tpm2_session_set_auth_value(tpm2_session *session, TPM2B_AUTH *auth_value);
  188. const TPM2B_AUTH *tpm2_session_get_auth_value(tpm2_session *session);
  189. #endif /* SRC_TPM2_SESSION_H_ */