tpm2_createak.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <stdbool.h>
  3. #include <stdlib.h>
  4. #include "files.h"
  5. #include "log.h"
  6. #include "object.h"
  7. #include "tpm2_alg_util.h"
  8. #include "tpm2_auth_util.h"
  9. #include "tpm2_convert.h"
  10. #include "tpm2_tool.h"
  11. typedef struct createak_context createak_context;
  12. struct createak_context {
  13. struct {
  14. const char *ctx_arg;
  15. tpm2_loaded_object ek_ctx;
  16. tpm2_session *session;
  17. char *auth_str;
  18. } ek;
  19. struct {
  20. struct {
  21. TPM2B_SENSITIVE_CREATE in_sensitive;
  22. struct {
  23. TPM2_ALG_ID type;
  24. TPM2_ALG_ID digest;
  25. TPM2_ALG_ID sign;
  26. } alg;
  27. } in;
  28. struct {
  29. const char *ctx_file;
  30. tpm2_convert_pubkey_fmt pub_fmt;
  31. const char *pub_file;
  32. const char *name_file;
  33. const char *priv_file;
  34. const char *qname_file;
  35. } out;
  36. char *auth_str;
  37. } ak;
  38. struct {
  39. UINT8 f :1;
  40. } flags;
  41. };
  42. static createak_context ctx = {
  43. .ak = {
  44. .in = {
  45. .alg = {
  46. .type = TPM2_ALG_RSA,
  47. .digest = TPM2_ALG_SHA256,
  48. .sign = TPM2_ALG_NULL
  49. },
  50. },
  51. .out = {
  52. .pub_fmt = pubkey_format_tss
  53. },
  54. },
  55. .flags = { 0 },
  56. };
  57. /*
  58. * TODO: All these set_xxx_signing_algorithm() routines could likely somehow be refactored into one.
  59. */
  60. static bool set_rsa_signing_algorithm(UINT32 sign_alg, UINT32 digest_alg,
  61. TPM2B_PUBLIC *in_public) {
  62. if (sign_alg == TPM2_ALG_NULL) {
  63. sign_alg = TPM2_ALG_RSASSA;
  64. }
  65. in_public->publicArea.parameters.rsaDetail.scheme.scheme = sign_alg;
  66. switch (sign_alg) {
  67. case TPM2_ALG_RSASSA:
  68. case TPM2_ALG_RSAPSS:
  69. in_public->publicArea.parameters.rsaDetail.scheme.details.anySig.hashAlg =
  70. digest_alg;
  71. break;
  72. default:
  73. LOG_ERR("The RSA signing algorithm type input(%4.4x) is not supported!",
  74. sign_alg);
  75. return false;
  76. }
  77. return true;
  78. }
  79. static bool set_ecc_signing_algorithm(UINT32 sign_alg, UINT32 digest_alg,
  80. TPM2B_PUBLIC *in_public) {
  81. if (sign_alg == TPM2_ALG_NULL) {
  82. sign_alg = TPM2_ALG_ECDSA;
  83. }
  84. in_public->publicArea.parameters.eccDetail.scheme.scheme = sign_alg;
  85. switch (sign_alg) {
  86. case TPM2_ALG_ECDSA:
  87. case TPM2_ALG_SM2:
  88. case TPM2_ALG_ECSCHNORR:
  89. case TPM2_ALG_ECDAA:
  90. in_public->publicArea.parameters.eccDetail.scheme.details.anySig.hashAlg =
  91. digest_alg;
  92. break;
  93. default:
  94. LOG_ERR("The ECC signing algorithm type input(%4.4x) is not supported!",
  95. sign_alg);
  96. return false;
  97. }
  98. return true;
  99. }
  100. static bool set_keyed_hash_signing_algorithm(UINT32 sign_alg, UINT32 digest_alg,
  101. TPM2B_PUBLIC *in_public) {
  102. if (sign_alg == TPM2_ALG_NULL) {
  103. sign_alg = TPM2_ALG_HMAC;
  104. }
  105. in_public->publicArea.parameters.keyedHashDetail.scheme.scheme = sign_alg;
  106. switch (sign_alg) {
  107. case TPM2_ALG_HMAC:
  108. in_public->publicArea.parameters.keyedHashDetail.scheme.details.hmac.hashAlg =
  109. digest_alg;
  110. break;
  111. default:
  112. LOG_ERR(
  113. "The Keyedhash signing algorithm type input(%4.4x) is not supported!",
  114. sign_alg);
  115. return false;
  116. }
  117. return true;
  118. }
  119. static bool set_key_algorithm(TPM2B_PUBLIC *in_public) {
  120. in_public->publicArea.nameAlg = TPM2_ALG_SHA256;
  121. // First clear attributes bit field.
  122. in_public->publicArea.objectAttributes = 0;
  123. in_public->publicArea.objectAttributes |= TPMA_OBJECT_RESTRICTED;
  124. in_public->publicArea.objectAttributes |= TPMA_OBJECT_USERWITHAUTH;
  125. in_public->publicArea.objectAttributes |= TPMA_OBJECT_SIGN_ENCRYPT;
  126. in_public->publicArea.objectAttributes &= ~TPMA_OBJECT_DECRYPT;
  127. in_public->publicArea.objectAttributes |= TPMA_OBJECT_FIXEDTPM;
  128. in_public->publicArea.objectAttributes |= TPMA_OBJECT_FIXEDPARENT;
  129. in_public->publicArea.objectAttributes |= TPMA_OBJECT_SENSITIVEDATAORIGIN;
  130. in_public->publicArea.authPolicy.size = 0;
  131. in_public->publicArea.type = ctx.ak.in.alg.type;
  132. switch (ctx.ak.in.alg.type) {
  133. case TPM2_ALG_RSA:
  134. in_public->publicArea.parameters.rsaDetail.symmetric.algorithm =
  135. TPM2_ALG_NULL;
  136. in_public->publicArea.parameters.rsaDetail.symmetric.keyBits.aes = 0;
  137. in_public->publicArea.parameters.rsaDetail.symmetric.mode.aes =
  138. TPM2_ALG_NULL;
  139. in_public->publicArea.parameters.rsaDetail.keyBits = 2048;
  140. in_public->publicArea.parameters.rsaDetail.exponent = 0;
  141. in_public->publicArea.unique.rsa.size = 0;
  142. return set_rsa_signing_algorithm(ctx.ak.in.alg.sign,
  143. ctx.ak.in.alg.digest, in_public);
  144. case TPM2_ALG_ECC:
  145. in_public->publicArea.parameters.eccDetail.symmetric.algorithm =
  146. TPM2_ALG_NULL;
  147. in_public->publicArea.parameters.eccDetail.symmetric.mode.sym =
  148. TPM2_ALG_NULL;
  149. in_public->publicArea.parameters.eccDetail.symmetric.keyBits.sym = 0;
  150. in_public->publicArea.parameters.eccDetail.curveID = TPM2_ECC_NIST_P256;
  151. in_public->publicArea.parameters.eccDetail.kdf.scheme = TPM2_ALG_NULL;
  152. in_public->publicArea.unique.ecc.x.size = 0;
  153. in_public->publicArea.unique.ecc.y.size = 0;
  154. return set_ecc_signing_algorithm(ctx.ak.in.alg.sign,
  155. ctx.ak.in.alg.digest, in_public);
  156. case TPM2_ALG_KEYEDHASH:
  157. in_public->publicArea.unique.keyedHash.size = 0;
  158. return set_keyed_hash_signing_algorithm(ctx.ak.in.alg.sign,
  159. ctx.ak.in.alg.digest, in_public);
  160. case TPM2_ALG_SYMCIPHER:
  161. default:
  162. LOG_ERR("The algorithm type input(%4.4x) is not supported!",
  163. ctx.ak.in.alg.type);
  164. return false;
  165. }
  166. return true;
  167. }
  168. static tool_rc create_ak(ESYS_CONTEXT *ectx) {
  169. tool_rc rc = tool_rc_general_error;
  170. TPML_PCR_SELECTION creation_pcr = { .count = 0 };
  171. TPM2B_DATA outside_info = TPM2B_EMPTY_INIT;
  172. TPM2B_PUBLIC *out_public;
  173. TPM2B_PRIVATE *out_private;
  174. TPM2B_PUBLIC in_public = TPM2B_EMPTY_INIT;
  175. bool result = set_key_algorithm(&in_public);
  176. if (!result) {
  177. return tool_rc_general_error;
  178. }
  179. tpm2_session_data *data = tpm2_session_data_new(TPM2_SE_POLICY);
  180. if (!data) {
  181. LOG_ERR("oom");
  182. return tool_rc_general_error;
  183. }
  184. tpm2_session *session = NULL;
  185. tool_rc tmp_rc = tpm2_session_open(ectx, data, &session);
  186. if (tmp_rc != tool_rc_success) {
  187. LOG_ERR("Could not start tpm session");
  188. return tmp_rc;
  189. }
  190. LOG_INFO("tpm_session_start_auth_with_params succ");
  191. ESYS_TR sess_handle = tpm2_session_get_handle(session);
  192. ESYS_TR shandle = ESYS_TR_NONE;
  193. tmp_rc = tpm2_auth_util_get_shandle(ectx, ESYS_TR_RH_ENDORSEMENT,
  194. ctx.ek.session, &shandle);
  195. if (tmp_rc != tool_rc_success) {
  196. rc = tmp_rc;
  197. goto out_session;
  198. }
  199. TPM2_RC rval = Esys_PolicySecret(ectx, ESYS_TR_RH_ENDORSEMENT, sess_handle,
  200. shandle, ESYS_TR_NONE, ESYS_TR_NONE,
  201. NULL, NULL, NULL, 0, NULL, NULL);
  202. if (rval != TPM2_RC_SUCCESS) {
  203. LOG_PERR(Esys_PolicySecret, rval);
  204. goto out_session;
  205. }
  206. LOG_INFO("Esys_PolicySecret success");
  207. TPM2B_CREATION_DATA *creation_data = NULL;
  208. rval = Esys_Create(ectx, ctx.ek.ek_ctx.tr_handle, sess_handle, ESYS_TR_NONE,
  209. ESYS_TR_NONE, &ctx.ak.in.in_sensitive, &in_public, &outside_info,
  210. &creation_pcr, &out_private, &out_public, &creation_data, NULL, NULL);
  211. if (rval != TPM2_RC_SUCCESS) {
  212. LOG_PERR(Esys_Create, rval);
  213. goto out;
  214. }
  215. LOG_INFO("Esys_Create success");
  216. rc = tpm2_session_close(&session);
  217. if (rc != tool_rc_success) {
  218. goto out;
  219. }
  220. data = tpm2_session_data_new(TPM2_SE_POLICY);
  221. if (!data) {
  222. LOG_ERR("oom");
  223. goto out;
  224. }
  225. tmp_rc = tpm2_session_open(ectx, data, &session);
  226. if (tmp_rc != tool_rc_success) {
  227. LOG_ERR("Could not start tpm session");
  228. rc = tmp_rc;
  229. goto out;
  230. }
  231. LOG_INFO("tpm_session_start_auth_with_params succ");
  232. sess_handle = tpm2_session_get_handle(session);
  233. tmp_rc = tpm2_auth_util_get_shandle(ectx, sess_handle, ctx.ek.session,
  234. &shandle);
  235. if (tmp_rc != tool_rc_success) {
  236. rc = tmp_rc;
  237. goto out;
  238. }
  239. rval = Esys_PolicySecret(ectx, ESYS_TR_RH_ENDORSEMENT, sess_handle, shandle,
  240. ESYS_TR_NONE, ESYS_TR_NONE, NULL, NULL, NULL, 0, NULL, NULL);
  241. if (rval != TPM2_RC_SUCCESS) {
  242. LOG_PERR(Esys_PolicySecret, rval);
  243. goto out;
  244. }
  245. LOG_INFO("Esys_PolicySecret success");
  246. ESYS_TR loaded_sha1_key_handle;
  247. rval = Esys_Load(ectx, ctx.ek.ek_ctx.tr_handle, sess_handle, ESYS_TR_NONE,
  248. ESYS_TR_NONE, out_private, out_public, &loaded_sha1_key_handle);
  249. if (rval != TPM2_RC_SUCCESS) {
  250. LOG_PERR(Esys_Load, rval);
  251. rc = tool_rc_from_tpm(rval);
  252. goto out;
  253. }
  254. // Load the TPM2 handle so that we can print it
  255. TPM2B_NAME *key_name;
  256. rval = Esys_TR_GetName(ectx, loaded_sha1_key_handle, &key_name);
  257. if (rval != TPM2_RC_SUCCESS) {
  258. LOG_PERR(Esys_TR_GetName, rval);
  259. rc = tool_rc_from_tpm(rval);
  260. goto nameout;
  261. }
  262. rc = tpm2_session_close(&session);
  263. if (rc != tool_rc_success) {
  264. goto out;
  265. }
  266. /* generation qualified name */
  267. TPM2B_NAME *p_qname = &creation_data->creationData.parentQualifiedName;
  268. TPM2B_NAME qname = { 0 };
  269. rc = tpm2_calq_qname(p_qname,
  270. in_public.publicArea.nameAlg, key_name, &qname) ?
  271. tool_rc_success : tool_rc_general_error;
  272. if (rc != tool_rc_success) {
  273. goto out;
  274. }
  275. /* Output in YAML format */
  276. tpm2_tool_output("loaded-key:\n name: ");
  277. tpm2_util_print_tpm2b(key_name);
  278. tpm2_tool_output("\n");
  279. tpm2_tool_output(" qualified name: ");
  280. tpm2_util_print_tpm2b(&qname);
  281. tpm2_tool_output("\n");
  282. // write name to ak.name file
  283. if (ctx.ak.out.name_file) {
  284. result = files_save_bytes_to_file(ctx.ak.out.name_file, key_name->name,
  285. key_name->size);
  286. if (!result) {
  287. LOG_ERR("Failed to save AK name into file \"%s\"",
  288. ctx.ak.out.name_file);
  289. goto nameout;
  290. }
  291. }
  292. if (ctx.ak.out.qname_file) {
  293. result = files_save_bytes_to_file(ctx.ak.out.qname_file, qname.name,
  294. qname.size);
  295. if (!result) {
  296. LOG_ERR("Failed to save AK qualified name into file \"%s\"",
  297. ctx.ak.out.name_file);
  298. goto nameout;
  299. }
  300. }
  301. // If the AK isn't persisted we always save a context file of the
  302. // transient AK handle for future tool interactions.
  303. tmp_rc = files_save_tpm_context_to_path(ectx, loaded_sha1_key_handle,
  304. ctx.ak.out.ctx_file);
  305. if (tmp_rc != tool_rc_success) {
  306. rc = tmp_rc;
  307. LOG_ERR("Error saving tpm context for handle");
  308. goto nameout;
  309. }
  310. if (ctx.ak.out.pub_file) {
  311. result = tpm2_convert_pubkey_save(out_public, ctx.ak.out.pub_fmt,
  312. ctx.ak.out.pub_file);
  313. if (!result) {
  314. goto nameout;
  315. }
  316. }
  317. if (ctx.ak.out.priv_file) {
  318. result = files_save_private(out_private, ctx.ak.out.priv_file);
  319. if (!result) {
  320. goto nameout;
  321. }
  322. }
  323. rc = tool_rc_success;
  324. nameout:
  325. free(key_name);
  326. out:
  327. free(out_public);
  328. free(out_private);
  329. Esys_Free(creation_data);
  330. out_session:
  331. tpm2_session_close(&session);
  332. return rc;
  333. }
  334. static bool on_option(char key, char *value) {
  335. switch (key) {
  336. case 'C':
  337. ctx.ek.ctx_arg = value;
  338. break;
  339. case 'G':
  340. ctx.ak.in.alg.type = tpm2_alg_util_from_optarg(value,
  341. tpm2_alg_util_flags_base);
  342. if (ctx.ak.in.alg.type == TPM2_ALG_ERROR) {
  343. LOG_ERR("Could not convert algorithm. got: \"%s\".", value);
  344. return false;
  345. }
  346. break;
  347. case 'g':
  348. ctx.ak.in.alg.digest = tpm2_alg_util_from_optarg(value,
  349. tpm2_alg_util_flags_hash);
  350. if (ctx.ak.in.alg.digest == TPM2_ALG_ERROR) {
  351. LOG_ERR("Could not convert digest algorithm.");
  352. return false;
  353. }
  354. break;
  355. case 's':
  356. ctx.ak.in.alg.sign = tpm2_alg_util_from_optarg(value,
  357. tpm2_alg_util_flags_sig);
  358. if (ctx.ak.in.alg.sign == TPM2_ALG_ERROR) {
  359. LOG_ERR("Could not convert signing algorithm.");
  360. return false;
  361. }
  362. break;
  363. case 'P':
  364. ctx.ek.auth_str = value;
  365. break;
  366. case 'p':
  367. ctx.ak.auth_str = value;
  368. break;
  369. case 'u':
  370. ctx.ak.out.pub_file = value;
  371. break;
  372. case 'n':
  373. ctx.ak.out.name_file = value;
  374. break;
  375. case 'f':
  376. ctx.ak.out.pub_fmt = tpm2_convert_pubkey_fmt_from_optarg(value);
  377. if (ctx.ak.out.pub_fmt == pubkey_format_err) {
  378. return false;
  379. }
  380. ctx.flags.f = true;
  381. break;
  382. case 'c':
  383. ctx.ak.out.ctx_file = value;
  384. break;
  385. case 'r':
  386. ctx.ak.out.priv_file = value;
  387. break;
  388. case 'q':
  389. ctx.ak.out.qname_file = value;
  390. break;
  391. }
  392. return true;
  393. }
  394. static bool tpm2_tool_onstart(tpm2_options **opts) {
  395. const struct option topts[] = {
  396. { "eh-auth", required_argument, NULL, 'P' },
  397. { "ak-auth", required_argument, NULL, 'p' },
  398. { "ek-context", required_argument, NULL, 'C' },
  399. { "ak-context", required_argument, NULL, 'c' },
  400. { "ak-name", required_argument, NULL, 'n' },
  401. { "key-algorithm", required_argument, NULL, 'G' },
  402. { "hash-algorithm", required_argument, NULL, 'g' },
  403. { "signing-algorithm", required_argument, NULL, 's' },
  404. { "format", required_argument, NULL, 'f' },
  405. { "public", required_argument, NULL, 'u' },
  406. { "private", required_argument, NULL, 'r' },
  407. { "ak-qualified-name", required_argument, NULL, 'q' },
  408. };
  409. *opts = tpm2_options_new("P:p:C:c:n:G:g:s:f:u:r:q:", ARRAY_LEN(topts), topts,
  410. on_option, NULL, 0);
  411. return *opts != NULL;
  412. }
  413. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  414. UNUSED(flags);
  415. if (ctx.flags.f && !ctx.ak.out.pub_file) {
  416. LOG_ERR("Please specify an output file name when specifying a format");
  417. return tool_rc_option_error;
  418. }
  419. if (!ctx.ak.out.ctx_file) {
  420. LOG_ERR("Expected option -c");
  421. return tool_rc_option_error;
  422. }
  423. tool_rc rc = tpm2_util_object_load(ectx, ctx.ek.ctx_arg, &ctx.ek.ek_ctx,
  424. TPM2_HANDLE_ALL_W_NV);
  425. if (rc != tool_rc_success) {
  426. return rc;
  427. }
  428. if (!ctx.ek.ek_ctx.tr_handle) {
  429. rc = tpm2_util_sys_handle_to_esys_handle(ectx, ctx.ek.ek_ctx.handle,
  430. &ctx.ek.ek_ctx.tr_handle);
  431. if (rc != tool_rc_success) {
  432. LOG_ERR("Converting ek_ctx TPM2_HANDLE to ESYS_TR");
  433. return rc;
  434. }
  435. }
  436. rc = tpm2_auth_util_from_optarg(NULL, ctx.ek.auth_str, &ctx.ek.session,
  437. true);
  438. if (rc != tool_rc_success) {
  439. LOG_ERR("Invalid endorse authorization");
  440. return rc;
  441. }
  442. tpm2_session *tmp;
  443. rc = tpm2_auth_util_from_optarg(NULL, ctx.ak.auth_str, &tmp, true);
  444. if (rc != tool_rc_success) {
  445. LOG_ERR("Invalid AK authorization");
  446. return rc;
  447. }
  448. const TPM2B_AUTH *auth = tpm2_session_get_auth_value(tmp);
  449. ctx.ak.in.in_sensitive.sensitive.userAuth = *auth;
  450. tpm2_session_close(&tmp);
  451. return create_ak(ectx);
  452. }
  453. // Register this tool with tpm2_tool.c
  454. TPM2_TOOL_REGISTER("createak", tpm2_tool_onstart, tpm2_tool_onrun, NULL, NULL)