tpm2_unseal.c 7.2 KB

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