tpm2_createek.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <stdbool.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <tss2/tss2_mu.h>
  6. #include "files.h"
  7. #include "tpm2_alg_util.h"
  8. #include "tpm2_convert.h"
  9. #include "tpm2_ctx_mgmt.h"
  10. #include "tpm2_nv_util.h"
  11. #include "tpm2_tool.h"
  12. #define RSA_EK_NONCE_NV_INDEX 0x01c00003
  13. #define RSA_EK_TEMPLATE_NV_INDEX 0x01c00004
  14. #define ECC_EK_NONCE_NV_INDEX 0x01c0000b
  15. #define ECC_EK_TEMPLATE_NV_INDEX 0x01c0000c
  16. typedef struct createek_context createek_context;
  17. struct createek_context {
  18. struct {
  19. const char *ctx_path;
  20. const char *auth_str;
  21. tpm2_loaded_object object;
  22. } auth_owner_hierarchy;
  23. struct {
  24. const char *ctx_path;
  25. const char *auth_str;
  26. tpm2_loaded_object object;
  27. } auth_endorse_hierarchy;
  28. struct {
  29. const char *ctx_path;
  30. const char *auth_str;
  31. tpm2_loaded_object object;
  32. } auth_ek;
  33. tpm2_hierarchy_pdata objdata;
  34. char *out_file_path;
  35. tpm2_convert_pubkey_fmt format;
  36. struct {
  37. UINT8 f :1;
  38. UINT8 t :1;
  39. } flags;
  40. bool find_persistent_handle;
  41. };
  42. static createek_context ctx = {
  43. .format = pubkey_format_tss,
  44. .objdata = TPM2_HIERARCHY_DATA_INIT,
  45. .flags = { 0 },
  46. .find_persistent_handle = false
  47. };
  48. static bool set_key_algorithm(TPM2B_PUBLIC *input_public) {
  49. switch (input_public->publicArea.type) {
  50. case TPM2_ALG_RSA:
  51. input_public->publicArea.parameters.rsaDetail.symmetric.algorithm =
  52. TPM2_ALG_AES;
  53. input_public->publicArea.parameters.rsaDetail.symmetric.keyBits.aes = 128;
  54. input_public->publicArea.parameters.rsaDetail.symmetric.mode.aes =
  55. TPM2_ALG_CFB;
  56. input_public->publicArea.parameters.rsaDetail.scheme.scheme = TPM2_ALG_NULL;
  57. input_public->publicArea.parameters.rsaDetail.keyBits = 2048;
  58. input_public->publicArea.parameters.rsaDetail.exponent = 0;
  59. input_public->publicArea.unique.rsa.size = 256;
  60. break;
  61. case TPM2_ALG_KEYEDHASH:
  62. input_public->publicArea.parameters.keyedHashDetail.scheme.scheme =
  63. TPM2_ALG_XOR;
  64. input_public->publicArea.parameters.keyedHashDetail.scheme.details.exclusiveOr.hashAlg =
  65. TPM2_ALG_SHA256;
  66. input_public->publicArea.parameters.keyedHashDetail.scheme.details.exclusiveOr.kdf =
  67. TPM2_ALG_KDF1_SP800_108;
  68. input_public->publicArea.unique.keyedHash.size = 0;
  69. break;
  70. case TPM2_ALG_ECC:
  71. input_public->publicArea.parameters.eccDetail.symmetric.algorithm =
  72. TPM2_ALG_AES;
  73. input_public->publicArea.parameters.eccDetail.symmetric.keyBits.aes = 128;
  74. input_public->publicArea.parameters.eccDetail.symmetric.mode.sym =
  75. TPM2_ALG_CFB;
  76. input_public->publicArea.parameters.eccDetail.scheme.scheme = TPM2_ALG_NULL;
  77. input_public->publicArea.parameters.eccDetail.curveID = TPM2_ECC_NIST_P256;
  78. input_public->publicArea.parameters.eccDetail.kdf.scheme = TPM2_ALG_NULL;
  79. input_public->publicArea.unique.ecc.x.size = 32;
  80. input_public->publicArea.unique.ecc.y.size = 32;
  81. break;
  82. case TPM2_ALG_SYMCIPHER:
  83. input_public->publicArea.parameters.symDetail.sym.algorithm = TPM2_ALG_AES;
  84. input_public->publicArea.parameters.symDetail.sym.keyBits.aes = 128;
  85. input_public->publicArea.parameters.symDetail.sym.mode.sym = TPM2_ALG_CFB;
  86. input_public->publicArea.unique.sym.size = 0;
  87. break;
  88. default:
  89. LOG_ERR("The algorithm type input(%4.4x) is not supported!",
  90. input_public->publicArea.type);
  91. return false;
  92. }
  93. return true;
  94. }
  95. static tool_rc set_ek_template(ESYS_CONTEXT *ectx, TPM2B_PUBLIC *input_public) {
  96. TPM2_HANDLE template_nv_index;
  97. TPM2_HANDLE nonce_nv_index;
  98. switch (input_public->publicArea.type) {
  99. case TPM2_ALG_RSA:
  100. template_nv_index = RSA_EK_TEMPLATE_NV_INDEX;
  101. nonce_nv_index = RSA_EK_NONCE_NV_INDEX;
  102. break;
  103. case TPM2_ALG_ECC:
  104. template_nv_index = ECC_EK_TEMPLATE_NV_INDEX;
  105. nonce_nv_index = ECC_EK_NONCE_NV_INDEX;
  106. break;
  107. default:
  108. LOG_ERR("EK template and EK nonce for algorithm type input(%4.4x)"
  109. " are not supported!", input_public->publicArea.type);
  110. return tool_rc_general_error;
  111. }
  112. UINT8* template = NULL;
  113. UINT8* nonce = NULL;
  114. // Read EK template
  115. UINT16 template_size;
  116. tool_rc rc = tpm2_util_nv_read(ectx, template_nv_index, 0, 0,
  117. &ctx.auth_owner_hierarchy.object, &template, &template_size, NULL);
  118. if (rc != tool_rc_success) {
  119. goto out;
  120. }
  121. TSS2_RC ret = Tss2_MU_TPMT_PUBLIC_Unmarshal(template, template_size,
  122. NULL, &input_public->publicArea);
  123. if (ret != TPM2_RC_SUCCESS) {
  124. LOG_ERR("Failed to unmarshal TPMT_PUBLIC from buffer 0x%p", template);
  125. rc = tool_rc_general_error;
  126. goto out;
  127. }
  128. // Read EK nonce
  129. UINT16 nonce_size;
  130. rc = tpm2_util_nv_read(ectx, nonce_nv_index, 0, 0,
  131. &ctx.auth_owner_hierarchy.object, &nonce, &nonce_size, NULL);
  132. if (rc != tool_rc_success) {
  133. goto out;
  134. }
  135. if (input_public->publicArea.type == TPM2_ALG_RSA) {
  136. memcpy(&input_public->publicArea.unique.rsa.buffer, &nonce, nonce_size);
  137. input_public->publicArea.unique.rsa.size = 256;
  138. } else {
  139. // ECC is only other supported algorithm
  140. memcpy(&input_public->publicArea.unique.ecc.x.buffer, &nonce, nonce_size);
  141. input_public->publicArea.unique.ecc.x.size = 32;
  142. input_public->publicArea.unique.ecc.y.size = 32;
  143. }
  144. out: if (template) {
  145. free(template);
  146. }
  147. if (nonce) {
  148. free(nonce);
  149. }
  150. return rc;
  151. }
  152. static tool_rc create_ek_handle(ESYS_CONTEXT *ectx) {
  153. if (ctx.flags.t) {
  154. tool_rc rc = set_ek_template(ectx, &ctx.objdata.in.public);
  155. if (rc != tool_rc_success) {
  156. return rc;
  157. }
  158. } else {
  159. bool result = set_key_algorithm(&ctx.objdata.in.public);
  160. if (!result) {
  161. return tool_rc_general_error;
  162. }
  163. }
  164. tool_rc rc = tpm2_hierarchy_create_primary(ectx,
  165. ctx.auth_endorse_hierarchy.object.session, &ctx.objdata, NULL);
  166. if (rc != tool_rc_success) {
  167. return rc;
  168. }
  169. if (ctx.auth_ek.object.handle) {
  170. rc = tpm2_ctx_mgmt_evictcontrol(ectx, ESYS_TR_RH_OWNER,
  171. ctx.auth_owner_hierarchy.object.session, ctx.objdata.out.handle,
  172. ctx.auth_ek.object.handle, NULL);
  173. if (rc != tool_rc_success) {
  174. return rc;
  175. }
  176. rc = tpm2_flush_context(ectx, ctx.objdata.out.handle);
  177. if (rc != tool_rc_success) {
  178. return rc;
  179. }
  180. } else {
  181. /* If it wasn't persistent, save a context for future tool interactions */
  182. tool_rc rc = files_save_tpm_context_to_path(ectx,
  183. ctx.objdata.out.handle, ctx.auth_ek.ctx_path);
  184. if (rc != tool_rc_success) {
  185. LOG_ERR("Error saving tpm context for handle");
  186. return rc;
  187. }
  188. }
  189. if (ctx.out_file_path) {
  190. bool ok = tpm2_convert_pubkey_save(ctx.objdata.out.public, ctx.format,
  191. ctx.out_file_path);
  192. if (!ok) {
  193. return tool_rc_general_error;
  194. }
  195. }
  196. return tool_rc_success;
  197. }
  198. static bool on_option(char key, char *value) {
  199. switch (key) {
  200. case 'P':
  201. ctx.auth_endorse_hierarchy.auth_str = value;
  202. break;
  203. case 'w':
  204. ctx.auth_owner_hierarchy.auth_str = value;
  205. break;
  206. case 'p':
  207. ctx.auth_ek.auth_str = value;
  208. break;
  209. case 'G': {
  210. TPMI_ALG_PUBLIC type = tpm2_alg_util_from_optarg(value,
  211. tpm2_alg_util_flags_base);
  212. if (type == TPM2_ALG_ERROR) {
  213. LOG_ERR("Invalid key algorithm, got \"%s\"", value);
  214. return false;
  215. }
  216. ctx.objdata.in.public.publicArea.type = type;
  217. }
  218. break;
  219. case 'u':
  220. if (!value) {
  221. LOG_ERR("Please specify an output file to save the pub ek to.");
  222. return false;
  223. }
  224. ctx.out_file_path = value;
  225. break;
  226. case 'f':
  227. ctx.format = tpm2_convert_pubkey_fmt_from_optarg(value);
  228. if (ctx.format == pubkey_format_err) {
  229. return false;
  230. }
  231. ctx.flags.f = true;
  232. break;
  233. case 'c':
  234. ctx.auth_ek.ctx_path = value;
  235. break;
  236. case 't':
  237. ctx.flags.t = true;
  238. break;
  239. }
  240. return true;
  241. }
  242. static bool tpm2_tool_onstart(tpm2_options **opts) {
  243. const struct option topts[] = {
  244. { "eh-auth", required_argument, NULL, 'P' },
  245. { "owner-auth", required_argument, NULL, 'w' },
  246. { "key-algorithm", required_argument, NULL, 'G' },
  247. { "public", required_argument, NULL, 'u' },
  248. { "format", required_argument, NULL, 'f' },
  249. { "ek-context", required_argument, NULL, 'c' },
  250. { "template", no_argument, NULL, 't' },
  251. };
  252. *opts = tpm2_options_new("P:w:G:u:f:c:t", ARRAY_LEN(topts), topts,
  253. on_option, NULL, 0);
  254. return *opts != NULL;
  255. }
  256. static void set_default_obj_attrs(void) {
  257. ctx.objdata.in.public.publicArea.objectAttributes =
  258. TPMA_OBJECT_RESTRICTED | TPMA_OBJECT_ADMINWITHPOLICY
  259. | TPMA_OBJECT_DECRYPT | TPMA_OBJECT_FIXEDTPM
  260. | TPMA_OBJECT_FIXEDPARENT | TPMA_OBJECT_SENSITIVEDATAORIGIN;
  261. }
  262. static void set_default_auth_policy(void) {
  263. static const TPM2B_DIGEST auth_policy = {
  264. .size = 32,
  265. .buffer = {
  266. 0x83, 0x71, 0x97, 0x67, 0x44, 0x84, 0xB3, 0xF8, 0x1A, 0x90, 0xCC,
  267. 0x8D, 0x46, 0xA5, 0xD7, 0x24, 0xFD, 0x52, 0xD7, 0x6E, 0x06, 0x52,
  268. 0x0B, 0x64, 0xF2, 0xA1, 0xDA, 0x1B, 0x33, 0x14, 0x69, 0xAA
  269. }
  270. };
  271. TPM2B_DIGEST *authp = &ctx.objdata.in.public.publicArea.authPolicy;
  272. *authp = auth_policy;
  273. }
  274. static void set_default_hierarchy(void) {
  275. ctx.objdata.in.hierarchy = TPM2_RH_ENDORSEMENT;
  276. }
  277. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  278. UNUSED(flags);
  279. size_t i;
  280. tool_rc rc = tool_rc_general_error;
  281. tpm2_session **sessions[] = {
  282. #if 0
  283. &ctx.auth.ek.session,
  284. &ctx.auth.endorse.session,
  285. &ctx.auth.owner.session,
  286. #endif
  287. &ctx.auth_owner_hierarchy.object.session,
  288. &ctx.auth_endorse_hierarchy.object.session,
  289. &ctx.auth_ek.object.session,
  290. };
  291. if (ctx.flags.f && !ctx.out_file_path) {
  292. LOG_ERR("Please specify an output file name when specifying a format");
  293. return tool_rc_option_error;
  294. }
  295. if (!ctx.auth_ek.ctx_path) {
  296. LOG_ERR("Expected option -c");
  297. return tool_rc_option_error;
  298. }
  299. bool ret;
  300. if (!strcmp(ctx.auth_ek.ctx_path, "-")) {
  301. /* If user passes a handle of '-' we try and find a vacant slot for
  302. * to use and tell them what it is.
  303. */
  304. rc = tpm2_capability_find_vacant_persistent_handle(ectx,
  305. false, &ctx.auth_ek.object.handle);
  306. if (rc != tool_rc_success) {
  307. LOG_ERR("handle/-H passed with a value '-' but unable to find a"
  308. " vacant persistent handle!");
  309. goto out;
  310. }
  311. tpm2_tool_output("persistent-handle: 0x%x\n", ctx.auth_ek.object.handle);
  312. } else {
  313. /* best attempt to convert what they have us to a handle, if it's not
  314. * a handle then we assume its a path to a context file */
  315. ret = tpm2_util_string_to_uint32(ctx.auth_ek.ctx_path, &ctx.auth_ek.object.handle);
  316. UNUSED(ret);
  317. }
  318. rc = tpm2_util_object_load_auth(ectx, "owner",
  319. ctx.auth_owner_hierarchy.auth_str, &ctx.auth_owner_hierarchy.object,
  320. false, TPM2_HANDLE_FLAGS_O);
  321. if (rc != tool_rc_success) {
  322. LOG_ERR("Invalid owner hierarchy authorization");
  323. return rc;
  324. }
  325. rc = tpm2_util_object_load_auth(ectx, "endorsement",
  326. ctx.auth_endorse_hierarchy.auth_str, &ctx.auth_endorse_hierarchy.object,
  327. false, TPM2_HANDLE_FLAGS_E);
  328. if (rc != tool_rc_success) {
  329. LOG_ERR("Invalid endorsement hierarchy authorization");
  330. return rc;
  331. }
  332. /*
  333. * The ek object is created @create_ek_handle and so it isn't loaded here
  334. * The ek object attributes are setup to policy reference eh-auth
  335. */
  336. rc = tpm2_auth_util_from_optarg(ectx, ctx.auth_ek.auth_str,
  337. &ctx.auth_ek.object.session, false);
  338. if (rc != tool_rc_success) {
  339. LOG_ERR("Invalid EK authorization");
  340. goto out;
  341. }
  342. /* override the default attrs */
  343. set_default_obj_attrs();
  344. /* set the auth policy */
  345. set_default_auth_policy();
  346. /* set the default hierarchy */
  347. set_default_hierarchy();
  348. /* normalize 0 success 1 failure */
  349. rc = create_ek_handle(ectx);
  350. out:
  351. for (i = 0; i < ARRAY_LEN(sessions); i++) {
  352. tpm2_session *s = *sessions[i];
  353. tool_rc tmp_rc = tpm2_session_close(&s);
  354. if (tmp_rc != tool_rc_success) {
  355. rc = tmp_rc;
  356. }
  357. }
  358. return rc;
  359. }
  360. static void tpm2_tool_onexit(void) {
  361. tpm2_hierarchy_pdata_free(&ctx.objdata);
  362. }
  363. // Register this tool with tpm2_tool.c
  364. TPM2_TOOL_REGISTER("createek", tpm2_tool_onstart, tpm2_tool_onrun, NULL, tpm2_tool_onexit)