tpm2_changeauth.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. #include "tpm2_auth_util.h"
  8. #include "tpm2_options.h"
  9. typedef struct changeauth_ctx changeauth_ctx;
  10. #define MAX_SESSIONS 3
  11. #define MAX_AUX_SESSIONS 2 // It's possible that parent auth may not be needed
  12. struct changeauth_ctx {
  13. /*
  14. * Inputs
  15. */
  16. struct {
  17. const char *ctx;
  18. tpm2_loaded_object obj;
  19. } parent;
  20. struct {
  21. const char *auth_current;
  22. const char *auth_new;
  23. const char *ctx;
  24. tpm2_loaded_object obj;
  25. tpm2_session *new;
  26. /*
  27. * Outputs
  28. */
  29. const char *out_path;
  30. } object;
  31. TPM2B_PRIVATE *out_private;
  32. const TPM2B_AUTH *new_auth;
  33. /*
  34. * Parameter hashes
  35. */
  36. const char *cp_hash_path;
  37. TPM2B_DIGEST cp_hash;
  38. const char *rp_hash_path;
  39. TPM2B_DIGEST rp_hash;
  40. bool is_command_dispatch;
  41. TPMI_ALG_HASH parameter_hash_algorithm;
  42. /*
  43. * Aux sessions
  44. */
  45. uint8_t aux_session_cnt;
  46. tpm2_session *aux_session[MAX_AUX_SESSIONS];
  47. const char *aux_session_path[MAX_AUX_SESSIONS];
  48. ESYS_TR aux_session_handle[MAX_AUX_SESSIONS];
  49. };
  50. static changeauth_ctx ctx = {
  51. .parameter_hash_algorithm = TPM2_ALG_ERROR,
  52. .aux_session_handle[0] = ESYS_TR_NONE,
  53. .aux_session_handle[1] = ESYS_TR_NONE,
  54. };
  55. static tool_rc hierarchy_change_auth(ESYS_CONTEXT *ectx) {
  56. return tpm2_hierarchy_change_auth(ectx, &ctx.object.obj, ctx.new_auth,
  57. &ctx.cp_hash, &ctx.rp_hash, ctx.parameter_hash_algorithm,
  58. ctx.aux_session_handle[0], ctx.aux_session_handle[1]);
  59. }
  60. static tool_rc nv_change_auth(ESYS_CONTEXT *ectx) {
  61. return tpm2_nv_change_auth(ectx, &ctx.object.obj, ctx.new_auth,
  62. &ctx.cp_hash, &ctx.rp_hash, ctx.parameter_hash_algorithm,
  63. ctx.aux_session_handle[0], ctx.aux_session_handle[1]);
  64. }
  65. static tool_rc object_change_auth(ESYS_CONTEXT *ectx) {
  66. if (!ctx.object.out_path) {
  67. LOG_ERR("Require private output file path option -r");
  68. return tool_rc_general_error;
  69. }
  70. return tpm2_object_change_auth(ectx, &ctx.parent.obj, &ctx.object.obj,
  71. ctx.new_auth, &ctx.out_private, &ctx.cp_hash, &ctx.rp_hash,
  72. ctx.parameter_hash_algorithm, ctx.aux_session_handle[0],
  73. ctx.aux_session_handle[1]);
  74. }
  75. static tool_rc change_authorization(ESYS_CONTEXT *ectx) {
  76. /*
  77. * 1. TPM2_CC_<command> OR Retrieve cpHash
  78. */
  79. tool_rc rc = tool_rc_success;
  80. /* invoke the proper changauth command based on object type */
  81. UINT8 tag = (ctx.object.obj.handle & TPM2_HR_RANGE_MASK) >> TPM2_HR_SHIFT;
  82. switch (tag) {
  83. case TPM2_HT_TRANSIENT:
  84. case TPM2_HT_PERSISTENT:
  85. rc = object_change_auth(ectx);
  86. break;
  87. case TPM2_HT_NV_INDEX:
  88. rc = nv_change_auth(ectx);
  89. break;
  90. case TPM2_HT_PERMANENT:
  91. rc = hierarchy_change_auth(ectx);
  92. break;
  93. default:
  94. LOG_ERR("Unsupported object type, got: 0x%x", tag);
  95. rc = tool_rc_general_error;
  96. }
  97. return rc;
  98. }
  99. static tool_rc process_output(ESYS_CONTEXT *ectx) {
  100. UNUSED(ectx);
  101. /*
  102. * 1. Outputs that do not require TPM2_CC_<command> dispatch
  103. */
  104. bool is_file_op_success = true;
  105. if (ctx.cp_hash_path) {
  106. is_file_op_success = files_save_digest(&ctx.cp_hash, ctx.cp_hash_path);
  107. if (!is_file_op_success) {
  108. return tool_rc_general_error;
  109. }
  110. }
  111. if (!ctx.is_command_dispatch) {
  112. return tool_rc_success;
  113. }
  114. /*
  115. * 2. Outputs generated after TPM2_CC_<command> dispatch
  116. */
  117. if (ctx.is_command_dispatch && ctx.out_private) {
  118. is_file_op_success = files_save_private(ctx.out_private,
  119. ctx.object.out_path);
  120. free(ctx.out_private);
  121. if (!is_file_op_success) {
  122. LOG_ERR("Failed to save the sensitive key portion");
  123. return tool_rc_general_error;
  124. }
  125. }
  126. if (ctx.rp_hash_path) {
  127. is_file_op_success = files_save_digest(&ctx.rp_hash, ctx.rp_hash_path);
  128. }
  129. return is_file_op_success ? tool_rc_success : tool_rc_general_error;
  130. }
  131. static inline bool object_needs_parent(tpm2_loaded_object *obj) {
  132. TPM2_HC h = obj->handle & TPM2_HR_RANGE_MASK;
  133. return (h == TPM2_HR_TRANSIENT) || (h == TPM2_HR_PERSISTENT);
  134. }
  135. static tool_rc process_inputs(ESYS_CONTEXT *ectx) {
  136. /*
  137. * 1. Object and auth initializations
  138. */
  139. /*
  140. * 1.a Add the new-auth values to be set for the object.
  141. */
  142. tool_rc rc = tpm2_auth_util_from_optarg(ectx, ctx.object.auth_new,
  143. &ctx.object.new, true);
  144. if (rc != tool_rc_success) {
  145. return rc;
  146. }
  147. ctx.new_auth = tpm2_session_get_auth_value(ctx.object.new);
  148. /*
  149. * 1.b Add object names and their auth sessions
  150. */
  151. /* Note: Old-auth value is ignored when calculating cpHash */
  152. /* Object #1 */
  153. rc = tpm2_util_object_load_auth(ectx, ctx.object.ctx,
  154. ctx.object.auth_current, &ctx.object.obj, false, TPM2_HANDLE_ALL_W_NV);
  155. if (rc != tool_rc_success) {
  156. return rc;
  157. }
  158. if (ctx.object.obj.tr_handle == ESYS_TR_RH_NULL) {
  159. LOG_ERR("Cannot change the null hierarchy authorization");
  160. return tool_rc_general_error;
  161. }
  162. /* transient objects or persistent objects need parents */
  163. bool load_parent = object_needs_parent(&ctx.object.obj);
  164. if (load_parent && !ctx.parent.ctx) {
  165. LOG_ERR("Expected parent object information via -C");
  166. return tool_rc_option_error;
  167. }
  168. /* Object #2 */
  169. if (load_parent) {
  170. rc = tpm2_util_object_load(ectx, ctx.parent.ctx, &ctx.parent.obj,
  171. TPM2_HANDLE_ALL_W_NV);
  172. if (rc != tool_rc_success) {
  173. return rc;
  174. }
  175. }
  176. /*
  177. * 2. Restore auxiliary sessions
  178. */
  179. rc = tpm2_util_aux_sessions_setup(ectx, ctx.aux_session_cnt,
  180. ctx.aux_session_path, ctx.aux_session_handle, ctx.aux_session);
  181. if (rc != tool_rc_success) {
  182. return rc;
  183. }
  184. /*
  185. * 3. Command specific initializations dependent on loaded objects
  186. */
  187. /*
  188. * 4. Configuration for calculating the pHash
  189. */
  190. /* 4.a Determine pHash length and alg */
  191. tpm2_session *all_sessions[MAX_SESSIONS] = {
  192. ctx.object.obj.session,
  193. ctx.aux_session[0],
  194. ctx.aux_session[1]
  195. };
  196. const char **cphash_path = ctx.cp_hash_path ? &ctx.cp_hash_path : 0;
  197. const char **rphash_path = ctx.rp_hash_path ? &ctx.rp_hash_path : 0;
  198. ctx.parameter_hash_algorithm = tpm2_util_calculate_phash_algorithm(ectx,
  199. cphash_path, &ctx.cp_hash, rphash_path, &ctx.rp_hash, all_sessions);
  200. /* 4.b Determine if TPM2_CC_<command> is to be dispatched
  201. * !rphash && !cphash [Y]
  202. * !rphash && cphash [N]
  203. * rphash && !cphash [Y]
  204. * rphash && cphash [Y]
  205. */
  206. ctx.is_command_dispatch = (ctx.cp_hash_path && !ctx.rp_hash_path) ?
  207. false : true;
  208. return tool_rc_success;
  209. }
  210. static tool_rc check_options(void) {
  211. /* load the object to call changeauth on */
  212. if (!ctx.object.ctx) {
  213. LOG_ERR("Expected object information via -c");
  214. return tool_rc_option_error;
  215. }
  216. if (ctx.cp_hash_path && !ctx.rp_hash_path) {
  217. LOG_WARN("Auth not changed. Only cpHash is calculated.");
  218. }
  219. return tool_rc_success;
  220. }
  221. static bool on_arg(int argc, char *argv[]) {
  222. if (argc != 1) {
  223. LOG_ERR("Expected 1 new password argument, got: %d", argc);
  224. return false;
  225. }
  226. ctx.object.auth_new = argv[0];
  227. return true;
  228. }
  229. static bool on_option(char key, char *value) {
  230. switch (key) {
  231. case 'c':
  232. ctx.object.ctx = value;
  233. break;
  234. case 'C':
  235. ctx.parent.ctx = value;
  236. break;
  237. case 'p':
  238. ctx.object.auth_current = value;
  239. break;
  240. case 'n':
  241. ctx.object.auth_new = value;
  242. break;
  243. case 'r':
  244. ctx.object.out_path = value;
  245. break;
  246. case 0:
  247. ctx.cp_hash_path = value;
  248. break;
  249. case 1:
  250. ctx.rp_hash_path = value;
  251. break;
  252. case 'S':
  253. ctx.aux_session_path[ctx.aux_session_cnt] = value;
  254. if (ctx.aux_session_cnt < MAX_AUX_SESSIONS) {
  255. ctx.aux_session_cnt++;
  256. } else {
  257. LOG_ERR("Specify a max of 3 sessions");
  258. return false;
  259. }
  260. break;
  261. /*no default */
  262. }
  263. return true;
  264. }
  265. static bool tpm2_tool_onstart(tpm2_options **opts) {
  266. struct option topts[] = {
  267. { "object-auth", required_argument, NULL, 'p' },
  268. { "object-context", required_argument, NULL, 'c' },
  269. { "parent-context", required_argument, NULL, 'C' },
  270. { "private", required_argument, NULL, 'r' },
  271. { "cphash", required_argument, NULL, 0 },
  272. { "rphash", required_argument, NULL, 1 },
  273. { "session", required_argument, NULL, 'S' },
  274. };
  275. *opts = tpm2_options_new("p:c:C:r:S:", ARRAY_LEN(topts), topts,
  276. on_option, on_arg, 0);
  277. return *opts != NULL;
  278. }
  279. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  280. UNUSED(flags);
  281. /*
  282. * 1. Process options
  283. */
  284. tool_rc rc = check_options();
  285. if (rc != tool_rc_success) {
  286. return rc;
  287. }
  288. /*
  289. * 2. Process inputs
  290. */
  291. rc = process_inputs(ectx);
  292. if (rc != tool_rc_success) {
  293. return rc;
  294. }
  295. /*
  296. * 3. TPM2_CC_<command> call
  297. */
  298. rc = change_authorization(ectx);
  299. if (rc != tool_rc_success) {
  300. return rc;
  301. }
  302. /*
  303. * 4. Process outputs
  304. */
  305. return process_output(ectx);
  306. }
  307. static tool_rc tpm2_tool_onstop(ESYS_CONTEXT *ectx) {
  308. UNUSED(ectx);
  309. /*
  310. * 1. Free objects
  311. */
  312. /*
  313. * 2. Close authorization sessions
  314. */
  315. tool_rc rc = tool_rc_success;
  316. tool_rc tmp_rc = tpm2_session_close(&ctx.object.new);
  317. if (tmp_rc != tool_rc_success) {
  318. rc = tmp_rc;
  319. }
  320. tmp_rc = tpm2_session_close(&ctx.object.obj.session);
  321. if (tmp_rc != tool_rc_success) {
  322. rc = tmp_rc;
  323. }
  324. tmp_rc = tpm2_session_close(&ctx.parent.obj.session);
  325. if (tmp_rc != tool_rc_success) {
  326. rc = tmp_rc;
  327. }
  328. /*
  329. * 3. Close auxiliary sessions
  330. */
  331. size_t i = 0;
  332. for(i = 0; i < ctx.aux_session_cnt; i++) {
  333. if (ctx.aux_session_path[i]) {
  334. tmp_rc = tpm2_session_close(&ctx.aux_session[i]);
  335. if (tmp_rc != tool_rc_success) {
  336. rc = tmp_rc;
  337. }
  338. }
  339. }
  340. return rc;
  341. }
  342. // Register this tool with tpm2_tool.c
  343. TPM2_TOOL_REGISTER("changeauth", tpm2_tool_onstart, tpm2_tool_onrun,
  344. tpm2_tool_onstop, NULL)