tpm2_duplicate.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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 "log.h"
  8. #include "tpm2.h"
  9. #include "tpm2_tool.h"
  10. #include "tpm2_alg_util.h"
  11. #include "tpm2_options.h"
  12. #include "tpm2_openssl.h"
  13. #include "tpm2_identity_util.h"
  14. typedef struct tpm_duplicate_ctx tpm_duplicate_ctx;
  15. struct tpm_duplicate_ctx {
  16. struct {
  17. const char *ctx_path;
  18. const char *auth_str;
  19. tpm2_loaded_object object;
  20. } duplicable_key;
  21. struct {
  22. const char *ctx_path;
  23. tpm2_loaded_object object;
  24. } new_parent_key;
  25. const char *duplicate_key_public_file;
  26. const char *duplicate_key_private_file;
  27. const char *private_key_file;
  28. const char *parent_public_key_file;
  29. TPMI_ALG_PUBLIC key_type;
  30. char *sym_key_in;
  31. char *sym_key_out;
  32. char *enc_seed_out;
  33. struct {
  34. UINT16 c :1;
  35. UINT16 C :1;
  36. UINT16 G :1;
  37. UINT16 i :1;
  38. UINT16 o :1;
  39. UINT16 r :1;
  40. UINT16 s :1;
  41. UINT16 U :1;
  42. UINT16 k :1;
  43. UINT16 u :1;
  44. } flags;
  45. char *cp_hash_path;
  46. };
  47. static tpm_duplicate_ctx ctx = {
  48. .key_type = TPM2_ALG_ERROR,
  49. };
  50. static tool_rc do_duplicate(ESYS_CONTEXT *ectx, TPM2B_DATA *in_key,
  51. TPMT_SYM_DEF_OBJECT *sym_alg, TPM2B_DATA **out_key,
  52. TPM2B_PRIVATE **duplicate, TPM2B_ENCRYPTED_SECRET **encrypted_seed) {
  53. if (ctx.cp_hash_path) {
  54. TPM2B_DIGEST cp_hash = { .size = 0 };
  55. tool_rc rc = tpm2_duplicate(ectx, &ctx.duplicable_key.object,
  56. &ctx.new_parent_key.object, in_key, sym_alg, out_key, duplicate,
  57. encrypted_seed, &cp_hash);
  58. if (rc != tool_rc_success) {
  59. return rc;
  60. }
  61. bool result = files_save_digest(&cp_hash, ctx.cp_hash_path);
  62. if (!result) {
  63. rc = tool_rc_general_error;
  64. }
  65. return rc;
  66. }
  67. return tpm2_duplicate(ectx, &ctx.duplicable_key.object,
  68. &ctx.new_parent_key.object, in_key, sym_alg, out_key, duplicate,
  69. encrypted_seed, NULL);
  70. }
  71. static bool on_option(char key, char *value) {
  72. switch (key) {
  73. case 'p':
  74. ctx.duplicable_key.auth_str = value;
  75. break;
  76. case 'G':
  77. ctx.key_type = tpm2_alg_util_from_optarg(value,
  78. tpm2_alg_util_flags_asymmetric |
  79. tpm2_alg_util_flags_symmetric | tpm2_alg_util_flags_misc);
  80. if (ctx.key_type != TPM2_ALG_ERROR) {
  81. ctx.flags.G = 1;
  82. }
  83. break;
  84. case 'i':
  85. ctx.sym_key_in = value;
  86. ctx.flags.i = 1;
  87. break;
  88. case 'o':
  89. ctx.sym_key_out = value;
  90. ctx.flags.o = 1;
  91. break;
  92. case 'C':
  93. ctx.new_parent_key.ctx_path = value;
  94. ctx.flags.C = 1;
  95. break;
  96. case 'c':
  97. ctx.duplicable_key.ctx_path = value;
  98. ctx.flags.c = 1;
  99. break;
  100. case 'r':
  101. ctx.duplicate_key_private_file = value;
  102. ctx.flags.r = 1;
  103. break;
  104. case 'u':
  105. ctx.duplicate_key_public_file = value;
  106. ctx.flags.u = 1;
  107. break;
  108. case 's':
  109. ctx.enc_seed_out = value;
  110. ctx.flags.s = 1;
  111. break;
  112. case 'U':
  113. ctx.parent_public_key_file = value;
  114. ctx.flags.U = 1;
  115. break;
  116. case 'k':
  117. ctx.private_key_file = value;
  118. ctx.flags.k = 1;
  119. break;
  120. case 0:
  121. ctx.cp_hash_path = value;
  122. break;
  123. default:
  124. LOG_ERR("Invalid option");
  125. return false;
  126. }
  127. return true;
  128. }
  129. static bool tpm2_tool_onstart(tpm2_options **opts) {
  130. const struct option topts[] = {
  131. { "auth", required_argument, NULL, 'p'},
  132. { "wrapper-algorithm", required_argument, NULL, 'G'},
  133. { "private", required_argument, NULL, 'r'},
  134. { "public", required_argument, NULL, 'u'},
  135. { "private-key", required_argument, NULL, 'k'},
  136. { "encryptionkey-in", required_argument, NULL, 'i'},
  137. { "encryptionkey-out", required_argument, NULL, 'o'},
  138. { "encrypted-seed", required_argument, NULL, 's'},
  139. { "parent-context", required_argument, NULL, 'C'},
  140. { "parent-public", required_argument, NULL, 'U'},
  141. { "key-context", required_argument, NULL, 'c'},
  142. { "cphash", required_argument, NULL, 0 },
  143. };
  144. *opts = tpm2_options_new("p:G:i:C:o:s:r:c:U:k:u:", ARRAY_LEN(topts), topts,
  145. on_option, NULL, 0);
  146. return *opts != NULL;
  147. }
  148. /**
  149. * Check all options and report as many errors as possible via LOG_ERR.
  150. * @return
  151. * true on success, false on failure.
  152. */
  153. static bool check_options(void) {
  154. bool result = true;
  155. /* Check for NULL alg & (keyin | keyout) */
  156. if (ctx.flags.G == 0) {
  157. LOG_ERR("Expected key type to be specified via \"-G\","
  158. " missing option.");
  159. result = false;
  160. }
  161. if (ctx.key_type != TPM2_ALG_NULL && !ctx.flags.U) {
  162. if ((ctx.flags.i == 0) && (ctx.flags.o == 0)) {
  163. LOG_ERR("Expected in or out encryption key file \"-i/o\","
  164. " missing option.");
  165. result = false;
  166. }
  167. if (ctx.flags.i && ctx.flags.o) {
  168. LOG_ERR("Expected either in or out encryption key file \"-i/o\","
  169. " conflicting options.");
  170. result = false;
  171. }
  172. } else {
  173. if (ctx.flags.i || ctx.flags.o) {
  174. LOG_ERR("Expected neither in nor out encryption key file \"-i/o\","
  175. " conflicting options.");
  176. result = false;
  177. }
  178. }
  179. if (ctx.flags.U != ctx.flags.k)
  180. {
  181. LOG_ERR("Conflicting options: remote public key and local private key must both be specified");
  182. result = false;
  183. } else
  184. if (!ctx.flags.U) {
  185. if (ctx.flags.C == 0) {
  186. LOG_ERR("Expected new parent object to be specified via \"-C\","
  187. " missing option.");
  188. result = false;
  189. }
  190. if (ctx.flags.c == 0) {
  191. LOG_ERR("Expected object to be specified via \"-c\","
  192. " missing option.");
  193. result = false;
  194. }
  195. if (ctx.flags.s == 0) {
  196. LOG_ERR(
  197. "Expected encrypted seed out filename to be specified via \"-S\","
  198. " missing option.");
  199. result = false;
  200. }
  201. if (ctx.flags.r == 0) {
  202. LOG_ERR("Expected private key out filename to be specified via \"-r\","
  203. " missing option.");
  204. result = false;
  205. }
  206. }
  207. return result;
  208. }
  209. static bool set_key_algorithm(TPMI_ALG_PUBLIC alg, TPMT_SYM_DEF_OBJECT * obj) {
  210. bool result = true;
  211. switch (alg) {
  212. case TPM2_ALG_AES:
  213. obj->algorithm = TPM2_ALG_AES;
  214. obj->keyBits.aes = 128;
  215. obj->mode.aes = TPM2_ALG_CFB;
  216. break;
  217. case TPM2_ALG_NULL:
  218. obj->algorithm = TPM2_ALG_NULL;
  219. break;
  220. default:
  221. LOG_ERR("The algorithm type input(0x%x) is not supported!", alg);
  222. result = false;
  223. break;
  224. }
  225. return result;
  226. }
  227. static tool_rc tpm2_create_duplicate(
  228. TPM2B_PUBLIC *parent_pub,
  229. TPM2B_SENSITIVE *privkey,
  230. TPM2B_PUBLIC *public,
  231. TPM2B_ENCRYPTED_SECRET *encrypted_seed)
  232. {
  233. bool result;
  234. tool_rc rc = tool_rc_success;
  235. TSS2_RC rval;
  236. /*
  237. * Calculate the object name.
  238. */
  239. TPM2B_NAME pubname = TPM2B_TYPE_INIT(TPM2B_NAME, name);
  240. result = tpm2_identity_create_name(public, &pubname);
  241. if (!result) {
  242. return false;
  243. }
  244. TPM2B_DIGEST * seed = &privkey->sensitiveArea.seedValue;
  245. TPM2B_MAX_BUFFER hmac_key;
  246. TPM2B_MAX_BUFFER enc_key;
  247. tpm2_identity_util_calc_outer_integrity_hmac_key_and_dupsensitive_enc_key(
  248. parent_pub, &pubname, seed, &hmac_key, &enc_key);
  249. /*
  250. * Marshall the private key into a buffer
  251. */
  252. TPM2B_MAX_BUFFER marshalled_sensitive = TPM2B_EMPTY_INIT;
  253. size_t marshalled_sensitive_size = 0;
  254. rval = Tss2_MU_TPMT_SENSITIVE_Marshal(&privkey->sensitiveArea,
  255. marshalled_sensitive.buffer + sizeof(marshalled_sensitive.size),
  256. TPM2_MAX_DIGEST_BUFFER, &marshalled_sensitive_size);
  257. if (rval != TPM2_RC_SUCCESS) {
  258. LOG_ERR("Error serializing sensitive area");
  259. return false;
  260. }
  261. size_t marshalled_sensitive_size_info = 0;
  262. rval = Tss2_MU_UINT16_Marshal(marshalled_sensitive_size,
  263. marshalled_sensitive.buffer, sizeof(marshalled_sensitive.size),
  264. &marshalled_sensitive_size_info);
  265. if (rval != TPM2_RC_SUCCESS) {
  266. LOG_ERR("Error serializing sensitive area size");
  267. return false;
  268. }
  269. marshalled_sensitive.size = marshalled_sensitive_size
  270. + marshalled_sensitive_size_info;
  271. /*
  272. * Compute the outer HMAC over the marshalled sensitive area
  273. * and encrypt it with the seed value.
  274. */
  275. TPM2B_DIGEST outer_hmac = TPM2B_EMPTY_INIT;
  276. TPM2B_MAX_BUFFER encrypted_duplicate_sensitive = TPM2B_EMPTY_INIT;
  277. tpm2_identity_util_calculate_outer_integrity(parent_pub->publicArea.nameAlg,
  278. &pubname, &marshalled_sensitive, &hmac_key, &enc_key,
  279. &parent_pub->publicArea.parameters.rsaDetail.symmetric,
  280. &encrypted_duplicate_sensitive, &outer_hmac);
  281. /*
  282. * Build the private data structure for writing out
  283. */
  284. TPM2B_PRIVATE private = TPM2B_EMPTY_INIT;
  285. UINT16 parent_hash_size = tpm2_alg_util_get_hash_size(parent_pub->publicArea.nameAlg);
  286. private.size = sizeof(parent_hash_size) + parent_hash_size
  287. + encrypted_duplicate_sensitive.size;
  288. size_t hmac_size_offset = 0;
  289. rval = Tss2_MU_UINT16_Marshal(parent_hash_size, private.buffer,
  290. sizeof(parent_hash_size), &hmac_size_offset);
  291. if (rval != TPM2_RC_SUCCESS) {
  292. LOG_ERR("Error serializing hmac size");
  293. return false;
  294. }
  295. memcpy(private.buffer + hmac_size_offset, outer_hmac.buffer,
  296. parent_hash_size);
  297. memcpy(private.buffer + hmac_size_offset + parent_hash_size,
  298. encrypted_duplicate_sensitive.buffer,
  299. encrypted_duplicate_sensitive.size);
  300. /*
  301. * Write out the generated files
  302. */
  303. result = files_save_encrypted_seed(encrypted_seed, ctx.enc_seed_out);
  304. if (!result) {
  305. LOG_ERR("Failed to save encryption seed into file \"%s\"",
  306. ctx.enc_seed_out);
  307. rc = tool_rc_general_error;
  308. goto out;
  309. }
  310. result = files_save_private(&private, ctx.duplicate_key_private_file);
  311. if (!result) {
  312. LOG_ERR("Failed to save private key into file \"%s\"",
  313. ctx.duplicate_key_private_file);
  314. rc = tool_rc_general_error;
  315. goto out;
  316. }
  317. result = files_save_public(public, ctx.duplicate_key_public_file);
  318. if (!result) {
  319. LOG_ERR("Failed to save public key into file \"%s\"",
  320. ctx.duplicate_key_public_file);
  321. rc = tool_rc_general_error;
  322. goto out;
  323. }
  324. out:
  325. return rc;
  326. }
  327. static tool_rc openssl_duplicate(void)
  328. {
  329. bool result;
  330. TPM2B_PUBLIC parent_public = TPM2B_EMPTY_INIT;
  331. TPM2B_PUBLIC public = TPM2B_EMPTY_INIT;
  332. TPM2B_SENSITIVE private = TPM2B_EMPTY_INIT;
  333. TPM2B_ENCRYPTED_SECRET encrypted_seed = TPM2B_EMPTY_INIT;
  334. result = files_load_public(ctx.parent_public_key_file, &parent_public);
  335. if (!result)
  336. return tool_rc_general_error;
  337. result = tpm2_openssl_import_keys(
  338. &parent_public,
  339. &private,
  340. &public,
  341. &encrypted_seed,
  342. ctx.private_key_file,
  343. ctx.key_type,
  344. NULL, // auth_key_file
  345. NULL, // policy_file
  346. NULL, // key_auth_str,
  347. NULL, // attrs_str
  348. NULL // name_alg_str
  349. );
  350. if (!result)
  351. return tool_rc_general_error;
  352. return tpm2_create_duplicate(&parent_public, &private, &public, &encrypted_seed);
  353. }
  354. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  355. UNUSED(flags);
  356. tool_rc rc = tool_rc_general_error;
  357. TPMT_SYM_DEF_OBJECT sym_alg;
  358. TPM2B_DATA in_key;
  359. TPM2B_DATA* out_key = NULL;
  360. TPM2B_PRIVATE* duplicate;
  361. TPM2B_ENCRYPTED_SECRET* out_sym_seed;
  362. bool result = check_options();
  363. if (!result) {
  364. return tool_rc_option_error;
  365. }
  366. if (ctx.flags.U) {
  367. return openssl_duplicate();
  368. }
  369. rc = tpm2_util_object_load(ectx, ctx.new_parent_key.ctx_path,
  370. &ctx.new_parent_key.object, TPM2_HANDLE_ALL_W_NV);
  371. if (rc != tool_rc_success) {
  372. return rc;
  373. }
  374. rc = tpm2_util_object_load_auth(ectx, ctx.duplicable_key.ctx_path,
  375. ctx.duplicable_key.auth_str, &ctx.duplicable_key.object, false,
  376. TPM2_HANDLE_ALL_W_NV);
  377. if (rc != tool_rc_success) {
  378. LOG_ERR("Invalid authorization");
  379. return rc;
  380. }
  381. result = set_key_algorithm(ctx.key_type, &sym_alg);
  382. if (!result) {
  383. return tool_rc_general_error;
  384. }
  385. if (ctx.flags.i) {
  386. in_key.size = 16;
  387. result = files_load_bytes_from_path(ctx.sym_key_in, in_key.buffer,
  388. &in_key.size);
  389. if (!result) {
  390. return tool_rc_general_error;
  391. }
  392. if (in_key.size != 16) {
  393. LOG_ERR("Invalid AES key size, got %u bytes, expected 16",
  394. in_key.size);
  395. return tool_rc_general_error;
  396. }
  397. }
  398. rc = do_duplicate(ectx, ctx.flags.i ? &in_key : NULL, &sym_alg,
  399. ctx.flags.o ? &out_key : NULL, &duplicate, &out_sym_seed);
  400. if (rc != tool_rc_success || ctx.cp_hash_path) {
  401. return rc;
  402. }
  403. /* Maybe a false positive from scan-build but we'll check out_key anyway */
  404. if (ctx.flags.o) {
  405. if (out_key == NULL) {
  406. LOG_ERR("No encryption key from TPM ");
  407. rc = tool_rc_general_error;
  408. goto out;
  409. }
  410. result = files_save_bytes_to_file(ctx.sym_key_out, out_key->buffer,
  411. out_key->size);
  412. if (!result) {
  413. LOG_ERR("Failed to save encryption key out into file \"%s\"",
  414. ctx.sym_key_out);
  415. rc = tool_rc_general_error;
  416. goto out;
  417. }
  418. }
  419. result = files_save_encrypted_seed(out_sym_seed, ctx.enc_seed_out);
  420. if (!result) {
  421. LOG_ERR("Failed to save encryption seed into file \"%s\"",
  422. ctx.enc_seed_out);
  423. rc = tool_rc_general_error;
  424. goto out;
  425. }
  426. result = files_save_private(duplicate, ctx.duplicate_key_private_file);
  427. if (!result) {
  428. LOG_ERR("Failed to save private key into file \"%s\"",
  429. ctx.duplicate_key_private_file);
  430. rc = tool_rc_general_error;
  431. goto out;
  432. }
  433. rc = tool_rc_success;
  434. out:
  435. free(out_key);
  436. free(out_sym_seed);
  437. free(duplicate);
  438. return rc;
  439. }
  440. static tool_rc tpm2_tool_onstop(ESYS_CONTEXT *ectx) {
  441. UNUSED(ectx);
  442. return tpm2_session_close(&ctx.duplicable_key.object.session);
  443. }
  444. // Register this tool with tpm2_tool.c
  445. TPM2_TOOL_REGISTER("duplicate", tpm2_tool_onstart, tpm2_tool_onrun, tpm2_tool_onstop, NULL)