tpm2_import.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. //**********************************************************************;
  3. // Copyright 1999-2018 The OpenSSL Project Authors. All Rights Reserved.
  4. // Licensed under the Apache License 2.0 (the "License"). You may not use
  5. // this file except in compliance with the License. You can obtain a copy
  6. // in the file LICENSE in the source distribution or at
  7. // https://www.openssl.org/source/license.html
  8. //
  9. // EME-OAEP as defined in RFC 2437 (PKCS #1 v2.0)
  10. //
  11. // See Victor Shoup, "OAEP reconsidered," Nov. 2000, <URL:
  12. // http://www.shoup.net/papers/oaep.ps.Z> for problems with the security
  13. // proof for the original OAEP scheme, which EME-OAEP is based on. A new
  14. // proof can be found in E. Fujisaki, T. Okamoto, D. Pointcheval, J. Stern,
  15. // "RSA-OEAP is Still Alive!", Dec. 2000, <URL:http://eprint.iacr.org/2000/061/>.
  16. // The new proof has stronger requirements for the underlying permutation:
  17. // "partial-one-wayness" instead of one-wayness. For the RSA function, this
  18. // is an equivalent notion.
  19. //**********************************************************************;
  20. #include <assert.h>
  21. #include <stdbool.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <tss2/tss2_mu.h>
  25. #include <openssl/rand.h>
  26. #include "files.h"
  27. #include "log.h"
  28. #include "tpm2.h"
  29. #include "tpm2_tool.h"
  30. #include "tpm2_alg_util.h"
  31. #include "tpm2_attr_util.h"
  32. #include "tpm2_auth_util.h"
  33. #include "tpm2_errata.h"
  34. #include "tpm2_identity_util.h"
  35. #include "tpm2_openssl.h"
  36. #include "tpm2_options.h"
  37. typedef struct tpm_import_ctx tpm_import_ctx;
  38. struct tpm_import_ctx {
  39. struct {
  40. const char *ctx_path;
  41. const char *auth_str;
  42. tpm2_loaded_object object;
  43. } parent;
  44. char *input_key_file;
  45. char *public_key_file;
  46. char *private_key_file;
  47. char *parent_key_public_file;
  48. char *name_alg;
  49. char *attrs; /* The attributes to use */
  50. char *key_auth_str;
  51. char *auth_key_file; /* an optional auth string for the input key file for OSSL */
  52. char *input_seed_file;
  53. char *input_enc_key_file;
  54. char *policy;
  55. bool import_tpm; /* Any param that is exclusively used by import tpm object sets this flag */
  56. TPMI_ALG_PUBLIC key_type;
  57. char *cp_hash_path;
  58. };
  59. static tpm_import_ctx ctx = {
  60. .key_type = TPM2_ALG_ERROR,
  61. .input_key_file = NULL,
  62. };
  63. static tool_rc readpublic(ESYS_CONTEXT *ectx, ESYS_TR handle,
  64. TPM2B_PUBLIC **public) {
  65. return tpm2_readpublic(ectx, handle, public, NULL, NULL);
  66. }
  67. static bool create_import_key_private_data(TPM2B_PRIVATE *private,
  68. TPMI_ALG_HASH parent_name_alg,
  69. TPM2B_MAX_BUFFER *encrypted_duplicate_sensitive,
  70. TPM2B_DIGEST *outer_hmac) {
  71. //UINT16 hash_size = tpm2_alg_util_get_hash_size(ctx.name_alg);
  72. UINT16 parent_hash_size = tpm2_alg_util_get_hash_size(parent_name_alg);
  73. private->size = sizeof(parent_hash_size) + parent_hash_size
  74. + encrypted_duplicate_sensitive->size;
  75. size_t hmac_size_offset = 0;
  76. TSS2_RC rval = Tss2_MU_UINT16_Marshal(parent_hash_size, private->buffer,
  77. sizeof(parent_hash_size), &hmac_size_offset);
  78. if (rval != TPM2_RC_SUCCESS)
  79. {
  80. LOG_ERR("Error serializing parent hash size");
  81. return false;
  82. }
  83. memcpy(private->buffer + hmac_size_offset, outer_hmac->buffer,
  84. parent_hash_size);
  85. memcpy(private->buffer + hmac_size_offset + parent_hash_size,
  86. encrypted_duplicate_sensitive->buffer,
  87. encrypted_duplicate_sensitive->size);
  88. return true;
  89. }
  90. static tool_rc key_import(ESYS_CONTEXT *ectx, TPM2B_PUBLIC *parent_pub,
  91. TPM2B_SENSITIVE *privkey, TPM2B_PUBLIC *pubkey,
  92. TPM2B_ENCRYPTED_SECRET *encrypted_seed,
  93. TPM2B_PRIVATE **imported_private) {
  94. TPMI_ALG_HASH name_alg = pubkey->publicArea.nameAlg;
  95. TPM2B_DIGEST *seed = &privkey->sensitiveArea.seedValue;
  96. /*
  97. * Create the protection encryption key that gets encrypted with the parents public key.
  98. */
  99. TPM2B_DATA enc_sensitive_key = {
  100. .size = parent_pub->publicArea.parameters.rsaDetail.symmetric.keyBits.sym / 8
  101. };
  102. memset(enc_sensitive_key.buffer, 0xFF, enc_sensitive_key.size);
  103. /*
  104. * Calculate the object name.
  105. */
  106. TPM2B_NAME pubname = TPM2B_TYPE_INIT(TPM2B_NAME, name);
  107. bool res = tpm2_identity_create_name(pubkey, &pubname);
  108. if (!res) {
  109. return false;
  110. }
  111. TPM2B_MAX_BUFFER hmac_key;
  112. TPM2B_MAX_BUFFER enc_key;
  113. tpm2_identity_util_calc_outer_integrity_hmac_key_and_dupsensitive_enc_key(
  114. parent_pub, &pubname, seed, &hmac_key, &enc_key);
  115. TPM2B_MAX_BUFFER encrypted_inner_integrity = TPM2B_EMPTY_INIT;
  116. tpm2_identity_util_calculate_inner_integrity(name_alg, privkey, &pubname,
  117. &enc_sensitive_key,
  118. &parent_pub->publicArea.parameters.rsaDetail.symmetric,
  119. &encrypted_inner_integrity);
  120. TPM2B_DIGEST outer_hmac = TPM2B_EMPTY_INIT;
  121. TPM2B_MAX_BUFFER encrypted_duplicate_sensitive = TPM2B_EMPTY_INIT;
  122. tpm2_identity_util_calculate_outer_integrity(parent_pub->publicArea.nameAlg,
  123. &pubname, &encrypted_inner_integrity, &hmac_key, &enc_key,
  124. &parent_pub->publicArea.parameters.rsaDetail.symmetric,
  125. &encrypted_duplicate_sensitive, &outer_hmac);
  126. TPM2B_PRIVATE private = TPM2B_EMPTY_INIT;
  127. res = create_import_key_private_data(&private, parent_pub->publicArea.nameAlg,
  128. &encrypted_duplicate_sensitive, &outer_hmac);
  129. if (!res) {
  130. return tool_rc_general_error;
  131. }
  132. TPMT_SYM_DEF_OBJECT *sym_alg =
  133. &parent_pub->publicArea.parameters.rsaDetail.symmetric;
  134. if (!ctx.cp_hash_path) {
  135. return tpm2_import(ectx, &ctx.parent.object, &enc_sensitive_key, pubkey,
  136. &private, encrypted_seed, sym_alg, imported_private, NULL);
  137. }
  138. TPM2B_DIGEST cp_hash = { .size = 0 };
  139. tool_rc rc = tpm2_import(ectx, &ctx.parent.object, &enc_sensitive_key, pubkey,
  140. &private, encrypted_seed, sym_alg, imported_private, &cp_hash);
  141. if (rc != tool_rc_success) {
  142. return rc;
  143. }
  144. bool result = files_save_digest(&cp_hash, ctx.cp_hash_path);
  145. if (!result) {
  146. rc = tool_rc_general_error;
  147. }
  148. return rc;
  149. }
  150. static bool on_option(char key, char *value) {
  151. switch (key) {
  152. case 'P':
  153. ctx.parent.auth_str = value;
  154. break;
  155. case 'p':
  156. ctx.key_auth_str = value;
  157. break;
  158. case 'G':
  159. ctx.key_type = tpm2_alg_util_from_optarg(value,
  160. tpm2_alg_util_flags_asymmetric | tpm2_alg_util_flags_symmetric);
  161. if (ctx.key_type == TPM2_ALG_ERROR) {
  162. LOG_ERR("Unsupported key type");
  163. return false;
  164. }
  165. return true;
  166. case 'i':
  167. ctx.input_key_file = value;
  168. break;
  169. case 'C':
  170. ctx.parent.ctx_path = value;
  171. break;
  172. case 'U':
  173. ctx.parent_key_public_file = value;
  174. break;
  175. case 'k':
  176. ctx.import_tpm = true;
  177. ctx.input_enc_key_file = value;
  178. break;
  179. case 'u':
  180. ctx.public_key_file = value;
  181. break;
  182. case 'r':
  183. ctx.private_key_file = value;
  184. break;
  185. case 'a':
  186. ctx.attrs = value;
  187. break;
  188. case 'g':
  189. ctx.name_alg = value;
  190. break;
  191. case 's':
  192. ctx.import_tpm = true;
  193. ctx.input_seed_file = value;
  194. break;
  195. case 'L':
  196. ctx.policy = value;
  197. break;
  198. case 0:
  199. ctx.auth_key_file = value;
  200. break;
  201. case 1:
  202. ctx.cp_hash_path = value;
  203. break;
  204. default:
  205. LOG_ERR("Invalid option");
  206. return false;
  207. }
  208. return true;
  209. }
  210. static bool tpm2_tool_onstart(tpm2_options **opts) {
  211. const struct option topts[] = {
  212. { "parent-auth", required_argument, NULL, 'P'},
  213. { "key-auth", required_argument, NULL, 'p'},
  214. { "key-algorithm", required_argument, NULL, 'G'},
  215. { "input", required_argument, NULL, 'i'},
  216. { "parent-context", required_argument, NULL, 'C'},
  217. { "parent-public", required_argument, NULL, 'U'},
  218. { "private", required_argument, NULL, 'r'},
  219. { "public", required_argument, NULL, 'u'},
  220. { "attributes", required_argument, NULL, 'a'},
  221. { "hash-algorithm", required_argument, NULL, 'g'},
  222. { "seed", required_argument, NULL, 's'},
  223. { "policy", required_argument, NULL, 'L'},
  224. { "encryption-key", required_argument, NULL, 'k'},
  225. { "passin", required_argument, NULL, 0 },
  226. { "cphash", required_argument, NULL, 1 },
  227. };
  228. *opts = tpm2_options_new("P:p:G:i:C:U:u:r:a:g:s:L:k:", ARRAY_LEN(topts),
  229. topts, on_option, NULL, 0);
  230. return *opts != NULL;
  231. }
  232. /**
  233. * Check all options and report as many errors as possible via LOG_ERR.
  234. * @return
  235. * tool_rc indicating error.
  236. */
  237. static tool_rc check_options(void) {
  238. tool_rc rc = tool_rc_success;
  239. /* Check the tpm import specific options */
  240. if (ctx.import_tpm) {
  241. if (!ctx.input_seed_file) {
  242. LOG_ERR("Expected SymSeed to be specified via \"-s\","
  243. " missing option.");
  244. rc = tool_rc_option_error;
  245. }
  246. /* If a key file is specified we choose aes else null
  247. for symmetricAlgdefinition */
  248. if (!ctx.input_enc_key_file) {
  249. ctx.key_type = TPM2_ALG_NULL;
  250. } else {
  251. ctx.key_type = TPM2_ALG_AES;
  252. }
  253. } else { /* Openssl specific option(s) */
  254. if (!ctx.key_type) {
  255. LOG_ERR("Expected key type to be specified via \"-G\","
  256. " missing option.");
  257. rc = tool_rc_option_error;
  258. }
  259. if (ctx.cp_hash_path) {
  260. LOG_WARN("CAUTION CpHash calculation includes parameters that"
  261. "have a derived/random seed!");
  262. }
  263. }
  264. /* Common options */
  265. if (!ctx.input_key_file) {
  266. LOG_ERR("Expected to be imported key data to be specified via \"-i\","
  267. " missing option.");
  268. rc = tool_rc_option_error;
  269. }
  270. if (!ctx.public_key_file) {
  271. LOG_ERR("Expected output public file missing, specify \"-u\","
  272. " missing option.");
  273. rc = tool_rc_option_error;
  274. }
  275. if (!ctx.private_key_file) {
  276. LOG_ERR("Expected output private file missing, specify \"-r\","
  277. " missing option.");
  278. rc = tool_rc_option_error;
  279. }
  280. if (!ctx.parent.ctx_path) {
  281. LOG_ERR("Expected parent key to be specified via \"-C\","
  282. " missing option.");
  283. rc = tool_rc_option_error;
  284. }
  285. return rc;
  286. }
  287. static tool_rc openssl_import(ESYS_CONTEXT *ectx) {
  288. /*
  289. * Load the parent public file, or read it from the TPM if not specified.
  290. * We need this information for encrypting the protection seed.
  291. */
  292. bool free_ppub = false;
  293. tool_rc tmp_rc;
  294. tool_rc rc = tool_rc_general_error;
  295. TPM2B_PUBLIC ppub = TPM2B_EMPTY_INIT;
  296. TPM2B_PUBLIC *parent_pub = NULL;
  297. bool result;
  298. tmp_rc = tool_rc_general_error;
  299. if (ctx.parent_key_public_file) {
  300. result = files_load_public(ctx.parent_key_public_file, &ppub);
  301. parent_pub = &ppub;
  302. } else {
  303. tmp_rc = readpublic(ectx, ctx.parent.object.tr_handle, &parent_pub);
  304. free_ppub = true;
  305. result = tmp_rc == tool_rc_success;
  306. }
  307. if (!result) {
  308. LOG_ERR("Failed loading parent key public.");
  309. return tmp_rc;
  310. }
  311. TPM2B_SENSITIVE private = TPM2B_EMPTY_INIT;
  312. TPM2B_PUBLIC public = TPM2B_EMPTY_INIT;
  313. TPM2B_ENCRYPTED_SECRET encrypted_seed = TPM2B_EMPTY_INIT;
  314. result = tpm2_openssl_import_keys(
  315. parent_pub,
  316. &private,
  317. &public,
  318. &encrypted_seed,
  319. ctx.input_key_file,
  320. ctx.key_type,
  321. ctx.auth_key_file,
  322. ctx.policy,
  323. ctx.key_auth_str,
  324. ctx.attrs,
  325. ctx.name_alg
  326. );
  327. if (!result)
  328. goto out;
  329. TPM2B_PRIVATE *imported_private = NULL;
  330. tmp_rc = key_import(ectx, parent_pub, &private, &public, &encrypted_seed,
  331. &imported_private);
  332. if (tmp_rc != tool_rc_success || ctx.cp_hash_path) {
  333. rc = tmp_rc;
  334. goto keyout;
  335. }
  336. /*
  337. * Save the public and imported_private structure to disk
  338. */
  339. result = files_save_public(&public, ctx.public_key_file);
  340. if (!result) {
  341. goto keyout;
  342. }
  343. result = files_save_private(imported_private, ctx.private_key_file);
  344. if (!result) {
  345. goto keyout;
  346. }
  347. /*
  348. * Output the stats on the created object on Success.
  349. */
  350. tpm2_util_public_to_yaml(&public, NULL);
  351. rc = tool_rc_success;
  352. keyout:
  353. free(imported_private);
  354. out:
  355. if (free_ppub) {
  356. free(parent_pub);
  357. }
  358. return rc;
  359. }
  360. static bool set_key_algorithm(TPMI_ALG_PUBLIC alg, TPMT_SYM_DEF_OBJECT * obj) {
  361. bool result = true;
  362. switch (alg) {
  363. case TPM2_ALG_AES:
  364. obj->algorithm = TPM2_ALG_AES;
  365. obj->keyBits.aes = 128;
  366. obj->mode.aes = TPM2_ALG_CFB;
  367. break;
  368. case TPM2_ALG_NULL:
  369. obj->algorithm = TPM2_ALG_NULL;
  370. break;
  371. default:
  372. LOG_ERR("The algorithm type input(0x%x) is not supported!", alg);
  373. result = false;
  374. break;
  375. }
  376. return result;
  377. }
  378. static tool_rc tpm_import(ESYS_CONTEXT *ectx) {
  379. TPM2B_DATA enc_key = TPM2B_EMPTY_INIT;
  380. TPM2B_PUBLIC public = TPM2B_EMPTY_INIT;
  381. TPM2B_PRIVATE duplicate;
  382. TPM2B_ENCRYPTED_SECRET encrypted_seed;
  383. TPM2B_PRIVATE *imported_private = NULL;
  384. TPMT_SYM_DEF_OBJECT sym_alg;
  385. tool_rc rc;
  386. bool result = set_key_algorithm(ctx.key_type, &sym_alg);
  387. if (!result) {
  388. return tool_rc_general_error;
  389. }
  390. /* Symmetric key */
  391. if (ctx.input_enc_key_file) {
  392. enc_key.size = 16;
  393. result = files_load_bytes_from_path(ctx.input_enc_key_file,
  394. enc_key.buffer, &enc_key.size);
  395. if (!result) {
  396. LOG_ERR("Failed to load symmetric encryption key\"%s\"",
  397. ctx.input_enc_key_file);
  398. return tool_rc_general_error;
  399. }
  400. if (enc_key.size != 16) {
  401. LOG_ERR("Invalid AES key size, got %u bytes, expected 16",
  402. enc_key.size);
  403. return tool_rc_general_error;
  404. }
  405. }
  406. /* Private key */
  407. result = files_load_private(ctx.input_key_file, &duplicate);
  408. if (!result) {
  409. LOG_ERR("Failed to load duplicate \"%s\"", ctx.input_key_file);
  410. return tool_rc_general_error;
  411. }
  412. /* Encrypted seed */
  413. result = files_load_encrypted_seed(ctx.input_seed_file, &encrypted_seed);
  414. if (!result) {
  415. LOG_ERR("Failed to load encrypted seed \"%s\"", ctx.input_seed_file);
  416. return tool_rc_general_error;
  417. }
  418. /* Public key */
  419. result = files_load_public(ctx.public_key_file, &public);
  420. if (!result) {
  421. LOG_ERR(":( Failed to load public key \"%s\"", ctx.public_key_file);
  422. return tool_rc_general_error;
  423. }
  424. if (ctx.policy) {
  425. public.publicArea.authPolicy.size =
  426. sizeof(public.publicArea.authPolicy.buffer);
  427. result = files_load_bytes_from_path(ctx.policy,
  428. public.publicArea.authPolicy.buffer,
  429. &public.publicArea.authPolicy.size);
  430. if (!result) {
  431. LOG_ERR("Failed to copy over the auth policy to the public data");
  432. return tool_rc_general_error;
  433. }
  434. }
  435. if (!ctx.cp_hash_path) {
  436. rc = tpm2_import(ectx, &ctx.parent.object, &enc_key, &public, &duplicate,
  437. &encrypted_seed, &sym_alg, &imported_private, NULL);
  438. if (rc != tool_rc_success) {
  439. return rc;
  440. }
  441. assert(imported_private);
  442. result = files_save_private(imported_private, ctx.private_key_file);
  443. free(imported_private);
  444. if (!result) {
  445. LOG_ERR("Failed to save private key into file \"%s\"",
  446. ctx.private_key_file);
  447. return tool_rc_general_error;
  448. }
  449. return tool_rc_success;
  450. }
  451. TPM2B_DIGEST cp_hash = { .size = 0 };
  452. rc = tpm2_import(ectx, &ctx.parent.object, &enc_key, &public, &duplicate,
  453. &encrypted_seed, &sym_alg, &imported_private, &cp_hash);
  454. if (rc != tool_rc_success) {
  455. return rc;
  456. }
  457. result = files_save_digest(&cp_hash, ctx.cp_hash_path);
  458. if (!result) {
  459. rc = tool_rc_general_error;
  460. }
  461. return rc;
  462. }
  463. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  464. UNUSED(flags);
  465. tool_rc rc = check_options();
  466. if (rc != tool_rc_success) {
  467. return rc;
  468. }
  469. rc = tpm2_util_object_load_auth(ectx, ctx.parent.ctx_path,
  470. ctx.parent.auth_str, &ctx.parent.object, false,
  471. TPM2_HANDLE_ALL_W_NV);
  472. if (rc != tool_rc_success) {
  473. LOG_ERR("Invalid parent key authorization");
  474. return rc;
  475. }
  476. return ctx.import_tpm ? tpm_import(ectx) : openssl_import(ectx);
  477. }
  478. static tool_rc tpm2_tool_onstop(ESYS_CONTEXT *ectx) {
  479. UNUSED(ectx);
  480. if (!ctx.import_tpm) {
  481. return tool_rc_success;
  482. }
  483. return tpm2_session_close(&ctx.parent.object.session);
  484. }
  485. // Register this tool with tpm2_tool.c
  486. TPM2_TOOL_REGISTER("import", tpm2_tool_onstart, tpm2_tool_onrun, tpm2_tool_onstop, NULL)