tpm2_changepps.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include "files.h"
  3. #include "log.h"
  4. #include "tpm2.h"
  5. #include "tpm2_auth_util.h"
  6. #include "tpm2_options.h"
  7. #include "tpm2_tool.h"
  8. typedef struct changepps_ctx changepps_ctx;
  9. #define MAX_SESSIONS 3
  10. #define MAX_AUX_SESSIONS 2
  11. struct changepps_ctx {
  12. /*
  13. * Inputs
  14. */
  15. const char *auth_str;
  16. tpm2_session *auth_session;
  17. /*
  18. * Outputs
  19. */
  20. /*
  21. * Parameter hashes
  22. */
  23. const char *cp_hash_path;
  24. TPM2B_DIGEST cp_hash;
  25. const char *rp_hash_path;
  26. TPM2B_DIGEST rp_hash;
  27. bool is_command_dispatch;
  28. TPMI_ALG_HASH parameter_hash_algorithm;
  29. /*
  30. * Aux sessions
  31. */
  32. uint8_t aux_session_cnt;
  33. tpm2_session *aux_session[MAX_AUX_SESSIONS];
  34. const char *aux_session_path[MAX_AUX_SESSIONS];
  35. ESYS_TR aux_session_handle[MAX_AUX_SESSIONS];
  36. };
  37. static changepps_ctx ctx = {
  38. .parameter_hash_algorithm = TPM2_ALG_ERROR,
  39. .aux_session_handle[0] = ESYS_TR_NONE,
  40. .aux_session_handle[1] = ESYS_TR_NONE,
  41. };
  42. static tool_rc process_output(ESYS_CONTEXT *ectx) {
  43. UNUSED(ectx);
  44. /*
  45. * 1. Outputs that do not require TPM2_CC_<command> dispatch
  46. */
  47. bool is_file_op_success = true;
  48. if (ctx.cp_hash_path) {
  49. is_file_op_success = files_save_digest(&ctx.cp_hash, ctx.cp_hash_path);
  50. if (!is_file_op_success) {
  51. LOG_ERR("Failed to save cpHash");
  52. return tool_rc_general_error;
  53. }
  54. }
  55. if (!ctx.is_command_dispatch) {
  56. return tool_rc_success;
  57. }
  58. /*
  59. * 2. Outputs generated after TPM2_CC_<command> dispatch
  60. */
  61. if (ctx.rp_hash_path) {
  62. is_file_op_success = files_save_digest(&ctx.rp_hash, ctx.rp_hash_path);
  63. }
  64. return is_file_op_success ? tool_rc_success : tool_rc_general_error;
  65. }
  66. static tool_rc process_inputs(ESYS_CONTEXT *ectx) {
  67. /*
  68. * 1. Object and auth initializations
  69. */
  70. /*
  71. * 1.a Add the new-auth values to be set for the object.
  72. */
  73. /*
  74. * 1.b Add object names and their auth sessions
  75. */
  76. tool_rc rc = tpm2_auth_util_from_optarg(ectx, ctx.auth_str,
  77. &ctx.auth_session, false);
  78. if (rc != tool_rc_success) {
  79. LOG_ERR("Failed loading platform auth.");
  80. return rc;
  81. }
  82. /*
  83. * 2. Restore auxiliary sessions
  84. */
  85. rc = tpm2_util_aux_sessions_setup(ectx, ctx.aux_session_cnt,
  86. ctx.aux_session_path, ctx.aux_session_handle, ctx.aux_session);
  87. if (rc != tool_rc_success) {
  88. return rc;
  89. }
  90. /*
  91. * 3. Command specific initializations dependent on loaded objects
  92. */
  93. /*
  94. * 4. Configuration for calculating the pHash
  95. */
  96. tpm2_session *all_sessions[MAX_SESSIONS] = {
  97. ctx.auth_session,
  98. ctx.aux_session[0],
  99. ctx.aux_session[1]
  100. };
  101. /*
  102. * 4.a Determine pHash length and alg
  103. */
  104. const char **cphash_path = ctx.cp_hash_path ? &ctx.cp_hash_path : 0;
  105. const char **rphash_path = ctx.rp_hash_path ? &ctx.rp_hash_path : 0;
  106. ctx.parameter_hash_algorithm = tpm2_util_calculate_phash_algorithm(ectx,
  107. cphash_path, &ctx.cp_hash, rphash_path, &ctx.rp_hash, all_sessions);
  108. /*
  109. * 4.b Determine if TPM2_CC_<command> is to be dispatched
  110. * !rphash && !cphash [Y]
  111. * !rphash && cphash [N]
  112. * rphash && !cphash [Y]
  113. * rphash && cphash [Y]
  114. */
  115. ctx.is_command_dispatch = (ctx.cp_hash_path && !ctx.rp_hash_path) ?
  116. false : true;
  117. return tool_rc_success;
  118. }
  119. static tool_rc check_options(void) {
  120. if (ctx.cp_hash_path && !ctx.rp_hash_path) {
  121. LOG_WARN("EPS not changed. Only cpHash is calculated.");
  122. }
  123. return tool_rc_success;
  124. }
  125. static bool on_option(char key, char *value) {
  126. switch (key) {
  127. case 'p':
  128. ctx.auth_str = value;
  129. break;
  130. case 0:
  131. ctx.cp_hash_path = value;
  132. break;
  133. case 1:
  134. ctx.rp_hash_path = value;
  135. break;
  136. case 'S':
  137. ctx.aux_session_path[ctx.aux_session_cnt] = value;
  138. if (ctx.aux_session_cnt < MAX_AUX_SESSIONS) {
  139. ctx.aux_session_cnt++;
  140. } else {
  141. LOG_ERR("Specify a max of 3 sessions");
  142. return false;
  143. }
  144. break;
  145. }
  146. return true;
  147. }
  148. static bool tpm2_tool_onstart(tpm2_options **opts) {
  149. static struct option topts[] = {
  150. { "auth", required_argument, NULL, 'p' },
  151. { "cphash", required_argument, NULL, 0 },
  152. { "rphash", required_argument, NULL, 1 },
  153. { "session",required_argument, NULL, 'S' },
  154. };
  155. *opts = tpm2_options_new("p:S:", ARRAY_LEN(topts), topts, on_option, NULL, 0);
  156. return *opts != NULL;
  157. }
  158. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  159. UNUSED(flags);
  160. /*
  161. * 1. Process options
  162. */
  163. tool_rc rc = check_options();
  164. if (rc != tool_rc_success) {
  165. return rc;
  166. }
  167. /*
  168. * 2. Process inputs
  169. */
  170. rc = process_inputs(ectx);
  171. if (rc != tool_rc_success) {
  172. return rc;
  173. }
  174. /*
  175. * 3. TPM2_CC_<command> call
  176. */
  177. rc = tpm2_changepps(ectx, ctx.auth_session, &ctx.cp_hash, &ctx.rp_hash,
  178. ctx.parameter_hash_algorithm, ctx.aux_session_handle[0],
  179. ctx.aux_session_handle[1]);
  180. if (rc != tool_rc_success) {
  181. return rc;
  182. }
  183. /*
  184. * 4. Process outputs
  185. */
  186. return process_output(ectx);
  187. }
  188. static tool_rc tpm2_tool_onstop(ESYS_CONTEXT *ectx) {
  189. UNUSED(ectx);
  190. /*
  191. * 1. Free objects
  192. */
  193. /*
  194. * 2. Close authorization sessions
  195. */
  196. tool_rc rc = tool_rc_success;
  197. tool_rc tmp_rc = tpm2_session_close(&ctx.auth_session);
  198. if (tmp_rc != tool_rc_success) {
  199. rc = tmp_rc;
  200. }
  201. /*
  202. * 3. Close auxiliary sessions
  203. */
  204. size_t i = 0;
  205. for(i = 0; i < ctx.aux_session_cnt; i++) {
  206. if (ctx.aux_session_path[i]) {
  207. tmp_rc = tpm2_session_close(&ctx.aux_session[i]);
  208. if (tmp_rc != tool_rc_success) {
  209. rc = tmp_rc;
  210. }
  211. }
  212. }
  213. return rc;
  214. }
  215. // Register this tool with tpm2_tool.c
  216. TPM2_TOOL_REGISTER("changepps", tpm2_tool_onstart, tpm2_tool_onrun,
  217. tpm2_tool_onstop, NULL)