tpm2_nvdefine.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <inttypes.h>
  3. #include <stdbool.h>
  4. #include <stdlib.h>
  5. #include "files.h"
  6. #include "log.h"
  7. #include "tpm2.h"
  8. #include "tpm2_alg_util.h"
  9. #include "tpm2_attr_util.h"
  10. #include "tpm2_auth_util.h"
  11. #include "tpm2_nv_util.h"
  12. #include "tpm2_options.h"
  13. #include "tpm2_tool.h"
  14. typedef struct tpm_nvdefine_ctx tpm_nvdefine_ctx;
  15. #define MAX_SESSIONS 3
  16. #define MAX_AUX_SESSIONS 2
  17. struct tpm_nvdefine_ctx {
  18. /*
  19. * Inputs
  20. */
  21. struct {
  22. const char *ctx_path;
  23. const char *auth_str;
  24. tpm2_loaded_object object;
  25. } auth_hierarchy;
  26. TPMI_RH_NV_INDEX nv_index;
  27. bool size_set;
  28. UINT16 size;
  29. TPMA_NV nv_attribute;
  30. TPM2B_AUTH nv_auth;
  31. TPMI_ALG_HASH halg;
  32. char *policy_file;
  33. char *index_auth_str;
  34. TPM2B_NV_PUBLIC public_info;
  35. /*
  36. * Outputs
  37. */
  38. /*
  39. * Parameter hashes
  40. */
  41. const char *cp_hash_path;
  42. TPM2B_DIGEST cp_hash;
  43. const char *rp_hash_path;
  44. TPM2B_DIGEST rp_hash;
  45. bool is_command_dispatch;
  46. TPMI_ALG_HASH parameter_hash_algorithm;
  47. /*
  48. * Aux sessions
  49. */
  50. uint8_t aux_session_cnt;
  51. tpm2_session *aux_session[MAX_AUX_SESSIONS];
  52. const char *aux_session_path[MAX_AUX_SESSIONS];
  53. ESYS_TR aux_session_handle[MAX_AUX_SESSIONS];
  54. };
  55. static tpm_nvdefine_ctx ctx = {
  56. .auth_hierarchy = {
  57. .ctx_path = "o",
  58. },
  59. .nv_auth = TPM2B_EMPTY_INIT,
  60. .halg = TPM2_ALG_SHA256,
  61. .public_info = TPM2B_EMPTY_INIT,
  62. .aux_session_handle[0] = ESYS_TR_NONE,
  63. .aux_session_handle[1] = ESYS_TR_NONE,
  64. .parameter_hash_algorithm = TPM2_ALG_ERROR,
  65. };
  66. static tool_rc nv_space_define(ESYS_CONTEXT *ectx) {
  67. tool_rc rc = tpm2_nv_definespace(ectx, &ctx.auth_hierarchy.object,
  68. &ctx.nv_auth, &ctx.public_info, &ctx.cp_hash, &ctx.rp_hash,
  69. ctx.parameter_hash_algorithm, ctx.aux_session_handle[0],
  70. ctx.aux_session_handle[1]);
  71. if (rc != tool_rc_success) {
  72. if (ctx.is_command_dispatch) {
  73. LOG_ERR("Failed to create NV index 0x%x.", ctx.nv_index);
  74. }
  75. return rc;
  76. }
  77. if (ctx.is_command_dispatch) {
  78. tpm2_tool_output("nv-index: 0x%x\n", ctx.nv_index);
  79. }
  80. return rc;
  81. }
  82. static tool_rc process_output(ESYS_CONTEXT *ectx) {
  83. UNUSED(ectx);
  84. /*
  85. * 1. Outputs that do not require TPM2_CC_<command> dispatch
  86. */
  87. bool is_file_op_success = true;
  88. if (ctx.cp_hash_path) {
  89. is_file_op_success = files_save_digest(&ctx.cp_hash, ctx.cp_hash_path);
  90. if (!is_file_op_success) {
  91. return tool_rc_general_error;
  92. }
  93. }
  94. if (!ctx.is_command_dispatch) {
  95. return tool_rc_success;
  96. }
  97. /*
  98. * 2. Outputs generated after TPM2_CC_<command> dispatch
  99. */
  100. if (ctx.rp_hash_path) {
  101. is_file_op_success = files_save_digest(&ctx.rp_hash, ctx.rp_hash_path);
  102. }
  103. return is_file_op_success ? tool_rc_success : tool_rc_general_error;
  104. }
  105. static void handle_default_attributes(void) {
  106. /* attributes set no need for defaults */
  107. if (ctx.nv_attribute) {
  108. return;
  109. }
  110. ESYS_TR h = ctx.auth_hierarchy.object.tr_handle;
  111. if (h == ESYS_TR_RH_OWNER) {
  112. ctx.nv_attribute |= TPMA_NV_OWNERWRITE | TPMA_NV_OWNERREAD;
  113. } else if (h == ESYS_TR_RH_PLATFORM) {
  114. ctx.nv_attribute |= TPMA_NV_PPWRITE | TPMA_NV_PPREAD;
  115. } /* else it's an nv index for auth */
  116. /* if it has a policy file, set policy read and write vs auth read and write */
  117. if (ctx.policy_file) {
  118. ctx.nv_attribute |= TPMA_NV_POLICYWRITE | TPMA_NV_POLICYREAD;
  119. } else {
  120. ctx.nv_attribute |= TPMA_NV_AUTHWRITE | TPMA_NV_AUTHREAD;
  121. }
  122. }
  123. static void get_max_nv_index_size(ESYS_CONTEXT *ectx, UINT16 *size) {
  124. /* get the max NV index for the TPM */
  125. *size = 0;
  126. TPMS_CAPABILITY_DATA *capabilities = NULL;
  127. tool_rc tmp_rc = tpm2_getcap(ectx, TPM2_CAP_TPM_PROPERTIES, TPM2_PT_FIXED,
  128. TPM2_MAX_TPM_PROPERTIES, NULL, &capabilities);
  129. if (tmp_rc != tool_rc_success) {
  130. *size = TPM2_MAX_NV_BUFFER_SIZE;
  131. LOG_ERR("Could not get fixed TPM properties");
  132. return;
  133. }
  134. TPMS_TAGGED_PROPERTY *properties = capabilities->data.tpmProperties.tpmProperty;
  135. UINT32 count = capabilities->data.tpmProperties.count;
  136. if (!count) {
  137. *size = TPM2_MAX_NV_BUFFER_SIZE;
  138. LOG_ERR("Could not get maximum NV index size");
  139. goto out;
  140. }
  141. UINT32 i;
  142. for (i=0; i < count; i++) {
  143. if (properties[i].property == TPM2_PT_NV_INDEX_MAX) {
  144. *size = properties[i].value;
  145. break;
  146. }
  147. }
  148. if (*size == 0) {
  149. *size = TPM2_MAX_NV_BUFFER_SIZE;
  150. LOG_ERR("Could not find max NV indices in capabilities");
  151. }
  152. out:
  153. free(capabilities);
  154. return;
  155. }
  156. static tool_rc handle_no_index_specified(ESYS_CONTEXT *ectx, TPM2_NV_INDEX *chosen) {
  157. /* get the max NV index for the TPM */
  158. TPMS_CAPABILITY_DATA *capabilities = NULL;
  159. tool_rc rc = tpm2_getcap(ectx, TPM2_CAP_TPM_PROPERTIES, TPM2_PT_FIXED,
  160. TPM2_MAX_TPM_PROPERTIES, NULL, &capabilities);
  161. if (rc != tool_rc_success) {
  162. return rc;
  163. }
  164. TPMS_TAGGED_PROPERTY *properties = capabilities->data.tpmProperties.tpmProperty;
  165. UINT32 count = capabilities->data.tpmProperties.count;
  166. if (!count) {
  167. LOG_ERR("Could not get maximum NV index, try specifying an NV index");
  168. rc = tool_rc_general_error;
  169. goto out;
  170. }
  171. TPM2_NV_INDEX max = 0;
  172. UINT32 i;
  173. for (i=0; i < count; i++) {
  174. if (properties[i].property == TPM2_PT_NV_INDEX_MAX) {
  175. max = TPM2_HR_NV_INDEX | properties[i].value;
  176. }
  177. }
  178. if (!max) {
  179. LOG_ERR("Could not find max NV indices in capabilities");
  180. rc = tool_rc_general_error;
  181. goto out;
  182. }
  183. /* done getting max NV index */
  184. free(capabilities);
  185. capabilities = NULL;
  186. /* now find what NV indexes are in use */
  187. rc = tpm2_getcap(ectx, TPM2_CAP_HANDLES, tpm2_util_hton_32(TPM2_HT_NV_INDEX),
  188. TPM2_PT_NV_INDEX_MAX, NULL, &capabilities);
  189. if (rc != tool_rc_success) {
  190. goto out;
  191. }
  192. /*
  193. * now starting at the first valid index, find one not in use
  194. * The TPM interface makes no guarantee that handles are returned in order
  195. * so we have to do a linear search every attempt for a free handle :-(
  196. */
  197. bool found = false;
  198. TPM2_NV_INDEX choose;
  199. for (choose = TPM2_HR_NV_INDEX; choose < max; choose++) {
  200. /* take the index to guess and check against everything in use */
  201. bool in_use = false;
  202. for (i = 0; i < capabilities->data.handles.count; i++) {
  203. TPMI_RH_NV_INDEX index = capabilities->data.handles.handle[i];
  204. if (index == choose) {
  205. in_use = true;
  206. break;
  207. }
  208. }
  209. if (!in_use) {
  210. /* it's not in use, use the current value of choose */
  211. found = true;
  212. break;
  213. }
  214. }
  215. if (!found) {
  216. LOG_ERR("No free NV index found");
  217. rc = tool_rc_general_error;
  218. goto out;
  219. }
  220. *chosen = choose;
  221. out:
  222. free(capabilities);
  223. return rc;
  224. }
  225. static tool_rc validate_size(ESYS_CONTEXT *ectx) {
  226. UINT16 hash_size = tpm2_alg_util_get_hash_size(ctx.halg);
  227. switch ((ctx.nv_attribute & TPMA_NV_TPM2_NT_MASK) >> TPMA_NV_TPM2_NT_SHIFT) {
  228. case TPM2_NT_ORDINARY:
  229. if (!ctx.size_set) {
  230. get_max_nv_index_size(ectx, &ctx.size);
  231. }
  232. break;
  233. case TPM2_NT_COUNTER:
  234. case TPM2_NT_BITS:
  235. case TPM2_NT_PIN_FAIL:
  236. case TPM2_NT_PIN_PASS:
  237. if (!ctx.size_set) {
  238. ctx.size = 8;
  239. } else if (ctx.size != 8) {
  240. LOG_ERR("Size is invalid for an NV index type,"
  241. " it must be size of 8");
  242. return tool_rc_general_error;
  243. }
  244. break;
  245. case TPM2_NT_EXTEND:
  246. if (!ctx.size_set) {
  247. ctx.size = hash_size;
  248. } else if (ctx.size != hash_size) {
  249. LOG_ERR("Size is invalid for an NV index type: \"extend\","
  250. " it must match the name hash algorithm size of %"
  251. PRIu16, hash_size);
  252. return tool_rc_general_error;
  253. }
  254. break;
  255. }
  256. return tool_rc_success;
  257. }
  258. static tool_rc process_inputs(ESYS_CONTEXT *ectx) {
  259. /*
  260. * 1. Object and auth initializations
  261. */
  262. /*
  263. * 1.a Add the new-auth values to be set for the object.
  264. */
  265. tpm2_session *tmp;
  266. tool_rc rc = tpm2_auth_util_from_optarg(NULL, ctx.index_auth_str, &tmp, true);
  267. if (rc != tool_rc_success) {
  268. LOG_ERR("Invalid index authorization");
  269. return rc;
  270. }
  271. const TPM2B_AUTH *auth = tpm2_session_get_auth_value(tmp);
  272. ctx.nv_auth = *auth;
  273. tpm2_session_close(&tmp);
  274. /*
  275. * 1.b Add object names and their auth sessions
  276. */
  277. rc = tpm2_util_object_load_auth(ectx, ctx.auth_hierarchy.ctx_path,
  278. ctx.auth_hierarchy.auth_str, &ctx.auth_hierarchy.object, false,
  279. TPM2_HANDLE_FLAGS_O | TPM2_HANDLE_FLAGS_P);
  280. if (rc != tool_rc_success) {
  281. LOG_ERR("Invalid authorization");
  282. return rc;
  283. }
  284. /*
  285. * 2. Restore auxiliary sessions
  286. */
  287. rc = tpm2_util_aux_sessions_setup(ectx, ctx.aux_session_cnt,
  288. ctx.aux_session_path, ctx.aux_session_handle, ctx.aux_session);
  289. if (rc != tool_rc_success) {
  290. return rc;
  291. }
  292. /*
  293. * 3. Command specific initializations dependent on loaded objects
  294. */
  295. handle_default_attributes();
  296. if (!ctx.nv_index) {
  297. rc = handle_no_index_specified(ectx, &ctx.nv_index);
  298. if (rc != tool_rc_success) {
  299. return rc;
  300. }
  301. }
  302. rc = validate_size(ectx);
  303. if (rc != tool_rc_success) {
  304. return rc;
  305. }
  306. ctx.public_info.nvPublic.nvIndex = ctx.nv_index;
  307. ctx.public_info.nvPublic.nameAlg = ctx.halg;
  308. ctx.public_info.nvPublic.attributes = ctx.nv_attribute;
  309. ctx.public_info.nvPublic.dataSize = ctx.size;
  310. if (ctx.policy_file) {
  311. ctx.public_info.nvPublic.authPolicy.size = BUFFER_SIZE(TPM2B_DIGEST,
  312. buffer);
  313. bool is_policy_load = files_load_bytes_from_path(ctx.policy_file,
  314. ctx.public_info.nvPublic.authPolicy.buffer,
  315. &ctx.public_info.nvPublic.authPolicy.size);
  316. if (!is_policy_load) {
  317. return tool_rc_general_error;
  318. }
  319. }
  320. /*
  321. * 4. Configuration for calculating the pHash
  322. */
  323. /* 4.a Determine pHash length and alg */
  324. tpm2_session *all_sessions[MAX_SESSIONS] = {
  325. ctx.auth_hierarchy.object.session,
  326. ctx.aux_session[0],
  327. ctx.aux_session[1]
  328. };
  329. const char **cphash_path = ctx.cp_hash_path ? &ctx.cp_hash_path : 0;
  330. const char **rphash_path = ctx.rp_hash_path ? &ctx.rp_hash_path : 0;
  331. ctx.parameter_hash_algorithm = tpm2_util_calculate_phash_algorithm(ectx,
  332. cphash_path, &ctx.cp_hash, rphash_path, &ctx.rp_hash, all_sessions);
  333. /*
  334. * 4.b Determine if TPM2_CC_<command> is to be dispatched
  335. * !rphash && !cphash [Y]
  336. * !rphash && cphash [N]
  337. * rphash && !cphash [Y]
  338. * rphash && cphash [Y]
  339. */
  340. ctx.is_command_dispatch = (ctx.cp_hash_path && !ctx.rp_hash_path) ?
  341. false : true;
  342. return rc;
  343. }
  344. static tool_rc check_options(void) {
  345. if (!ctx.size && ctx.size_set) {
  346. LOG_WARN("Defining an index with size 0");
  347. }
  348. /*
  349. * Todo: Add error checking for NV indices reserved for specific hierarchies
  350. */
  351. return tool_rc_success;
  352. }
  353. static bool on_option(char key, char *value) {
  354. bool result;
  355. switch (key) {
  356. case 'C':
  357. ctx.auth_hierarchy.ctx_path = value;
  358. break;
  359. case 'P':
  360. ctx.auth_hierarchy.auth_str = value;
  361. break;
  362. case 's':
  363. ctx.size_set = true;
  364. result = tpm2_util_string_to_uint16(value, &ctx.size);
  365. if (!result) {
  366. LOG_ERR("Could not convert size to number, got: \"%s\"", value);
  367. return false;
  368. }
  369. break;
  370. case 'a':
  371. result = tpm2_util_string_to_uint32(value, &ctx.nv_attribute);
  372. if (!result) {
  373. result = tpm2_attr_util_nv_strtoattr(value, &ctx.nv_attribute);
  374. if (!result) {
  375. LOG_ERR(
  376. "Could not convert NV attribute to number or keyword, got: \"%s\"",
  377. value);
  378. return false;
  379. }
  380. }
  381. break;
  382. case 'p':
  383. ctx.index_auth_str = value;
  384. break;
  385. case 'L':
  386. ctx.policy_file = value;
  387. break;
  388. case 0:
  389. ctx.cp_hash_path = value;
  390. break;
  391. case 1:
  392. ctx.rp_hash_path = value;
  393. break;
  394. case 'S':
  395. ctx.aux_session_path[ctx.aux_session_cnt] = value;
  396. if (ctx.aux_session_cnt < MAX_AUX_SESSIONS) {
  397. ctx.aux_session_cnt++;
  398. } else {
  399. return false;
  400. }
  401. break;
  402. case 'g':
  403. ctx.halg = tpm2_alg_util_from_optarg(value,
  404. tpm2_alg_util_flags_hash);
  405. if (ctx.halg == TPM2_ALG_ERROR) {
  406. LOG_ERR("Invalid choice for name digest hash algorithm");
  407. return false;
  408. }
  409. break;
  410. }
  411. return true;
  412. }
  413. static bool on_arg(int argc, char **argv) {
  414. return on_arg_nv_index(argc, argv, &ctx.nv_index);
  415. }
  416. static bool tpm2_tool_onstart(tpm2_options **opts) {
  417. const struct option topts[] = {
  418. { "hierarchy", required_argument, NULL, 'C' },
  419. { "size", required_argument, NULL, 's' },
  420. { "attributes", required_argument, NULL, 'a' },
  421. { "hierarchy-auth", required_argument, NULL, 'P' },
  422. { "hash-algorithm", required_argument, NULL, 'g' },
  423. { "index-auth", required_argument, NULL, 'p' },
  424. { "policy", required_argument, NULL, 'L' },
  425. { "cphash", required_argument, NULL, 0 },
  426. {"rphash", required_argument, NULL, 1 },
  427. { "session", required_argument, NULL, 'S' },
  428. };
  429. *opts = tpm2_options_new("S:C:s:a:P:p:L:g:", ARRAY_LEN(topts), topts,
  430. on_option, on_arg, 0);
  431. return *opts != NULL;
  432. }
  433. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  434. UNUSED(flags);
  435. /*
  436. * 1. Process options
  437. */
  438. tool_rc rc = check_options();
  439. if (rc != tool_rc_success) {
  440. return rc;
  441. }
  442. /*
  443. * 2. Process inputs
  444. */
  445. rc = process_inputs(ectx);
  446. if (rc != tool_rc_success) {
  447. return rc;
  448. }
  449. /*
  450. * 3. TPM2_CC_<command> call
  451. */
  452. rc = nv_space_define(ectx);
  453. if (rc != tool_rc_success) {
  454. return rc;
  455. }
  456. /*
  457. * 4. Process outputs
  458. */
  459. return process_output(ectx);
  460. }
  461. static tool_rc tpm2_tool_onstop(ESYS_CONTEXT *ectx) {
  462. UNUSED(ectx);
  463. /*
  464. * 1. Free objects
  465. */
  466. /*
  467. * 2. Close authorization sessions
  468. */
  469. tool_rc rc = tool_rc_success;
  470. tool_rc tmp_rc = tpm2_session_close(&ctx.auth_hierarchy.object.session);
  471. if (tmp_rc != tool_rc_success) {
  472. rc = tmp_rc;
  473. }
  474. /*
  475. * 3. Close auxiliary sessions
  476. */
  477. size_t i = 0;
  478. for(i = 0; i < ctx.aux_session_cnt; i++) {
  479. if (ctx.aux_session_path[i]) {
  480. tmp_rc = tpm2_session_close(&ctx.aux_session[i]);
  481. if (tmp_rc != tool_rc_success) {
  482. rc = tmp_rc;
  483. }
  484. }
  485. }
  486. return rc;
  487. }
  488. // Register this tool with tpm2_tool.c
  489. TPM2_TOOL_REGISTER("nvdefine", tpm2_tool_onstart, tpm2_tool_onrun,
  490. tpm2_tool_onstop, NULL)