tpm2_openssl.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <stdbool.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <openssl/pem.h>
  7. #include <openssl/rand.h>
  8. #include "files.h"
  9. #include "log.h"
  10. #include "tpm2_alg_util.h"
  11. #include "tpm2_auth_util.h"
  12. #include "tpm2_attr_util.h"
  13. #include "tpm2_identity_util.h"
  14. #include "tpm2_openssl.h"
  15. #include "tpm2_errata.h"
  16. #include "tpm2_systemdeps.h"
  17. /* compatibility function for OpenSSL versions < 1.1.0 */
  18. #if OPENSSL_VERSION_NUMBER < 0x10100000L
  19. static int BN_bn2binpad(const BIGNUM *a, unsigned char *to, int tolen) {
  20. int r;
  21. int topad;
  22. int islen;
  23. islen = BN_num_bytes(a);
  24. if (tolen < islen)
  25. return -1;
  26. topad = tolen - islen;
  27. memset(to, 0x00, topad);
  28. r = BN_bn2bin(a, to + topad);
  29. if (r == 0) {
  30. return -1;
  31. }
  32. return tolen;
  33. }
  34. #endif
  35. int tpm2_openssl_halgid_from_tpmhalg(TPMI_ALG_HASH algorithm) {
  36. switch (algorithm) {
  37. case TPM2_ALG_SHA1:
  38. return NID_sha1;
  39. case TPM2_ALG_SHA256:
  40. return NID_sha256;
  41. case TPM2_ALG_SHA384:
  42. return NID_sha384;
  43. case TPM2_ALG_SHA512:
  44. return NID_sha512;
  45. default:
  46. return NID_sha256;
  47. }
  48. /* no return, not possible */
  49. }
  50. const EVP_MD *tpm2_openssl_halg_from_tpmhalg(TPMI_ALG_HASH algorithm) {
  51. switch (algorithm) {
  52. case TPM2_ALG_SHA1:
  53. return EVP_sha1();
  54. case TPM2_ALG_SHA256:
  55. return EVP_sha256();
  56. case TPM2_ALG_SHA384:
  57. return EVP_sha384();
  58. case TPM2_ALG_SHA512:
  59. return EVP_sha512();
  60. default:
  61. return NULL;
  62. }
  63. /* no return, not possible */
  64. }
  65. #if defined(LIB_TPM2_OPENSSL_OPENSSL_PRE11)
  66. int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d) {
  67. if ((r->n == NULL && n == NULL) || (r->e == NULL && e == NULL)) {
  68. return 0;
  69. }
  70. if (n != NULL) {
  71. BN_free(r->n);
  72. r->n = n;
  73. }
  74. if (e != NULL) {
  75. BN_free(r->e);
  76. r->e = e;
  77. }
  78. if (d != NULL) {
  79. BN_free(r->d);
  80. r->d = d;
  81. }
  82. return 1;
  83. }
  84. void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q) {
  85. if(p) {
  86. *p = r->p;
  87. }
  88. if (q) {
  89. *q = r->q;
  90. }
  91. }
  92. int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s) {
  93. if (!r || !s) {
  94. return 0;
  95. }
  96. BN_clear_free(sig->r);
  97. BN_clear_free(sig->s);
  98. sig->r = r;
  99. sig->s = s;
  100. return 1;
  101. }
  102. EVP_ENCODE_CTX *EVP_ENCODE_CTX_new(void) {
  103. EVP_ENCODE_CTX *ctx = OPENSSL_malloc(sizeof(EVP_ENCODE_CTX));
  104. if (ctx) {
  105. memset(ctx, 0, sizeof(*ctx));
  106. }
  107. return ctx;
  108. }
  109. void EVP_ENCODE_CTX_free(EVP_ENCODE_CTX *ctx) {
  110. OPENSSL_free(ctx);
  111. }
  112. #endif
  113. bool tpm2_openssl_hash_compute_data(TPMI_ALG_HASH halg, BYTE *buffer,
  114. UINT16 length, TPM2B_DIGEST *digest) {
  115. bool result = false;
  116. const EVP_MD *md = tpm2_openssl_halg_from_tpmhalg(halg);
  117. if (!md) {
  118. return false;
  119. }
  120. EVP_MD_CTX *mdctx = EVP_MD_CTX_create();
  121. if (!mdctx) {
  122. LOG_ERR("%s", tpm2_openssl_get_err());
  123. return false;
  124. }
  125. int rc = EVP_DigestInit_ex(mdctx, md, NULL);
  126. if (!rc) {
  127. LOG_ERR("%s", tpm2_openssl_get_err());
  128. goto out;
  129. }
  130. rc = EVP_DigestUpdate(mdctx, buffer, length);
  131. if (!rc) {
  132. LOG_ERR("%s", tpm2_openssl_get_err());
  133. goto out;
  134. }
  135. unsigned size = EVP_MD_size(md);
  136. rc = EVP_DigestFinal_ex(mdctx, digest->buffer, &size);
  137. if (!rc) {
  138. LOG_ERR("%s", tpm2_openssl_get_err());
  139. goto out;
  140. }
  141. digest->size = size;
  142. result = true;
  143. out:
  144. EVP_MD_CTX_destroy(mdctx);
  145. return result;
  146. }
  147. bool tpm2_openssl_pcr_extend(TPMI_ALG_HASH halg, BYTE *pcr,
  148. const BYTE *data, UINT16 length) {
  149. bool result = false;
  150. const EVP_MD *md = tpm2_openssl_halg_from_tpmhalg(halg);
  151. if (!md) {
  152. return false;
  153. }
  154. EVP_MD_CTX *mdctx = EVP_MD_CTX_create();
  155. if (!mdctx) {
  156. LOG_ERR("%s", tpm2_openssl_get_err());
  157. return false;
  158. }
  159. int rc = EVP_DigestInit_ex(mdctx, md, NULL);
  160. if (!rc) {
  161. LOG_ERR("%s", tpm2_openssl_get_err());
  162. goto out;
  163. }
  164. // extend operation is pcr = HASH(pcr + data)
  165. unsigned size = EVP_MD_size(md);
  166. rc = EVP_DigestUpdate(mdctx, pcr, size);
  167. if (!rc) {
  168. LOG_ERR("%s", tpm2_openssl_get_err());
  169. goto out;
  170. }
  171. rc = EVP_DigestUpdate(mdctx, data, length);
  172. if (!rc) {
  173. LOG_ERR("%s", tpm2_openssl_get_err());
  174. goto out;
  175. }
  176. rc = EVP_DigestFinal_ex(mdctx, pcr, &size);
  177. if (!rc) {
  178. LOG_ERR("%s", tpm2_openssl_get_err());
  179. goto out;
  180. }
  181. result = true;
  182. out:
  183. EVP_MD_CTX_destroy(mdctx);
  184. return result;
  185. }
  186. bool tpm2_openssl_hash_pcr_values(TPMI_ALG_HASH halg, TPML_DIGEST *digests,
  187. TPM2B_DIGEST *digest) {
  188. bool result = false;
  189. const EVP_MD *md = tpm2_openssl_halg_from_tpmhalg(halg);
  190. if (!md) {
  191. return false;
  192. }
  193. EVP_MD_CTX *mdctx = EVP_MD_CTX_create();
  194. if (!mdctx) {
  195. LOG_ERR("%s", tpm2_openssl_get_err());
  196. return false;
  197. }
  198. int rc = EVP_DigestInit_ex(mdctx, md, NULL);
  199. if (!rc) {
  200. LOG_ERR("%s", tpm2_openssl_get_err());
  201. goto out;
  202. }
  203. size_t i;
  204. for (i = 0; i < digests->count; i++) {
  205. TPM2B_DIGEST *b = &digests->digests[i];
  206. rc = EVP_DigestUpdate(mdctx, b->buffer, b->size);
  207. if (!rc) {
  208. LOG_ERR("%s", tpm2_openssl_get_err());
  209. goto out;
  210. }
  211. }
  212. unsigned size = EVP_MD_size(EVP_sha256());
  213. rc = EVP_DigestFinal_ex(mdctx, digest->buffer, &size);
  214. if (!rc) {
  215. LOG_ERR("%s", tpm2_openssl_get_err());
  216. goto out;
  217. }
  218. digest->size = size;
  219. result = true;
  220. out:
  221. EVP_MD_CTX_destroy(mdctx);
  222. return result;
  223. }
  224. // show all PCR banks according to g_pcrSelection & g_pcrs->
  225. bool tpm2_openssl_hash_pcr_banks(TPMI_ALG_HASH hash_alg,
  226. TPML_PCR_SELECTION *pcr_select, tpm2_pcrs *pcrs, TPM2B_DIGEST *digest) {
  227. UINT32 vi = 0, di = 0, i;
  228. bool result = false;
  229. const EVP_MD *md = tpm2_openssl_halg_from_tpmhalg(hash_alg);
  230. if (!md) {
  231. return false;
  232. }
  233. EVP_MD_CTX *mdctx = EVP_MD_CTX_create();
  234. if (!mdctx) {
  235. LOG_ERR("%s", tpm2_openssl_get_err());
  236. return false;
  237. }
  238. int rc = EVP_DigestInit_ex(mdctx, md, NULL);
  239. if (!rc) {
  240. LOG_ERR("%s", tpm2_openssl_get_err());
  241. goto out;
  242. }
  243. // Loop through all PCR/hash banks
  244. for (i = 0; i < pcr_select->count; i++) {
  245. // Loop through all PCRs in this bank
  246. unsigned int pcr_id;
  247. for (pcr_id = 0; pcr_id < pcr_select->pcrSelections[i].sizeofSelect * 8u;
  248. pcr_id++) {
  249. if (!tpm2_util_is_pcr_select_bit_set(&pcr_select->pcrSelections[i],
  250. pcr_id)) {
  251. // skip non-selected banks
  252. continue;
  253. }
  254. if (vi >= pcrs->count || di >= pcrs->pcr_values[vi].count) {
  255. LOG_ERR("Something wrong, trying to print but nothing more");
  256. goto out;
  257. }
  258. // Update running digest (to compare with quote)
  259. TPM2B_DIGEST *b = &pcrs->pcr_values[vi].digests[di];
  260. rc = EVP_DigestUpdate(mdctx, b->buffer, b->size);
  261. if (!rc) {
  262. LOG_ERR("%s", tpm2_openssl_get_err());
  263. goto out;
  264. }
  265. if (++di < pcrs->pcr_values[vi].count) {
  266. continue;
  267. }
  268. di = 0;
  269. if (++vi < pcrs->count) {
  270. continue;
  271. }
  272. }
  273. }
  274. // Finalize running digest
  275. unsigned size = EVP_MD_size(md);
  276. rc = EVP_DigestFinal_ex(mdctx, digest->buffer, &size);
  277. if (!rc) {
  278. LOG_ERR("%s", tpm2_openssl_get_err());
  279. goto out;
  280. }
  281. digest->size = size;
  282. result = true;
  283. out:
  284. EVP_MD_CTX_destroy(mdctx);
  285. return result;
  286. }
  287. /* show all PCR banks according to g_pcrSelection & g_pcrs-> */
  288. bool tpm2_openssl_hash_pcr_banks_le(TPMI_ALG_HASH hash_alg,
  289. TPML_PCR_SELECTION *pcr_select, tpm2_pcrs *pcrs, TPM2B_DIGEST *digest) {
  290. UINT32 vi = 0, di = 0, i;
  291. bool result = false;
  292. const EVP_MD *md = tpm2_openssl_halg_from_tpmhalg(hash_alg);
  293. if (!md) {
  294. return false;
  295. }
  296. EVP_MD_CTX *mdctx = EVP_MD_CTX_create();
  297. if (!mdctx) {
  298. LOG_ERR("%s", tpm2_openssl_get_err());
  299. return false;
  300. }
  301. int rc = EVP_DigestInit_ex(mdctx, md, NULL);
  302. if (!rc) {
  303. LOG_ERR("%s", tpm2_openssl_get_err());
  304. goto out;
  305. }
  306. /* Loop through all PCR/hash banks */
  307. for (i = 0; i < le32toh(pcr_select->count); i++) {
  308. /* Loop through all PCRs in this bank */
  309. unsigned int pcr_id;
  310. for (pcr_id = 0; pcr_id < pcr_select->pcrSelections[i].sizeofSelect * 8u;
  311. pcr_id++) {
  312. if (!tpm2_util_is_pcr_select_bit_set(&pcr_select->pcrSelections[i],
  313. pcr_id)) {
  314. continue; // skip non-selected banks
  315. }
  316. if (vi >= le64toh(pcrs->count) || di >= le32toh(pcrs->pcr_values[vi].count)) {
  317. LOG_ERR("Something wrong, trying to print but nothing more");
  318. goto out;
  319. }
  320. /* Update running digest (to compare with quote) */
  321. TPM2B_DIGEST *b = &pcrs->pcr_values[vi].digests[di];
  322. rc = EVP_DigestUpdate(mdctx, b->buffer, le16toh(b->size));
  323. if (!rc) {
  324. LOG_ERR("%s", tpm2_openssl_get_err());
  325. goto out;
  326. }
  327. if (++di < le32toh(pcrs->pcr_values[vi].count)) {
  328. continue;
  329. }
  330. di = 0;
  331. if (++vi < le64toh(pcrs->count)) {
  332. continue;
  333. }
  334. }
  335. }
  336. /* Finalize running digest */
  337. unsigned size = EVP_MD_size(md);
  338. rc = EVP_DigestFinal_ex(mdctx, digest->buffer, &size);
  339. if (!rc) {
  340. LOG_ERR("%s", tpm2_openssl_get_err());
  341. goto out;
  342. }
  343. digest->size = size;
  344. result = true;
  345. out:
  346. EVP_MD_CTX_destroy(mdctx);
  347. return result;
  348. }
  349. HMAC_CTX *tpm2_openssl_hmac_new() {
  350. HMAC_CTX *ctx;
  351. #if defined(LIB_TPM2_OPENSSL_OPENSSL_PRE11)
  352. ctx = malloc(sizeof(*ctx));
  353. #else
  354. ctx = HMAC_CTX_new();
  355. #endif
  356. if (!ctx)
  357. return NULL;
  358. #if defined(LIB_TPM2_OPENSSL_OPENSSL_PRE11)
  359. HMAC_CTX_init(ctx);
  360. #endif
  361. return ctx;
  362. }
  363. void tpm2_openssl_hmac_free(HMAC_CTX *ctx) {
  364. #if defined(LIB_TPM2_OPENSSL_OPENSSL_PRE11)
  365. HMAC_CTX_cleanup(ctx);
  366. free(ctx);
  367. #else
  368. HMAC_CTX_free(ctx);
  369. #endif
  370. }
  371. EVP_CIPHER_CTX *tpm2_openssl_cipher_new(void) {
  372. EVP_CIPHER_CTX *ctx;
  373. #if defined(LIB_TPM2_OPENSSL_OPENSSL_PRE11)
  374. ctx = malloc(sizeof(*ctx));
  375. #else
  376. ctx = EVP_CIPHER_CTX_new();
  377. #endif
  378. if (!ctx)
  379. return NULL;
  380. #if defined(LIB_TPM2_OPENSSL_OPENSSL_PRE11)
  381. EVP_CIPHER_CTX_init(ctx);
  382. #endif
  383. return ctx;
  384. }
  385. void tpm2_openssl_cipher_free(EVP_CIPHER_CTX *ctx) {
  386. #if defined(LIB_TPM2_OPENSSL_OPENSSL_PRE11)
  387. EVP_CIPHER_CTX_cleanup(ctx);
  388. free(ctx);
  389. #else
  390. EVP_CIPHER_CTX_free(ctx);
  391. #endif
  392. }
  393. digester tpm2_openssl_halg_to_digester(TPMI_ALG_HASH halg) {
  394. switch (halg) {
  395. case TPM2_ALG_SHA1:
  396. return SHA1;
  397. case TPM2_ALG_SHA256:
  398. return SHA256;
  399. case TPM2_ALG_SHA384:
  400. return SHA384;
  401. case TPM2_ALG_SHA512:
  402. return SHA512;
  403. /* no default */
  404. }
  405. return NULL;
  406. }
  407. /*
  408. * Per man openssl(1), handle the following --passin formats:
  409. * pass:password
  410. * the actual password is password. Since the password is visible to utilities (like 'ps' under Unix) this form should only be used where security is not
  411. * important.
  412. *
  413. * env:var obtain the password from the environment variable var. Since the environment of other processes is visible on certain platforms (e.g. ps under certain
  414. * Unix OSes) this option should be used with caution.
  415. *
  416. * file:pathname
  417. * the first line of pathname is the password. If the same pathname argument is supplied to -passin and -passout arguments then the first line will be used
  418. * for the input password and the next line for the output password. pathname need not refer to a regular file: it could for example refer to a device or
  419. * named pipe.
  420. *
  421. * fd:number read the password from the file descriptor number. This can be used to send the data via a pipe for example.
  422. *
  423. * stdin read the password from standard input.
  424. *
  425. */
  426. typedef bool (*pfn_ossl_pw_handler)(const char *passin, char **pass);
  427. static bool do_pass(const char *passin, char **pass) {
  428. char *tmp = strdup(passin);
  429. if (!tmp) {
  430. LOG_ERR("oom");
  431. return false;
  432. }
  433. *pass = tmp;
  434. return true;
  435. }
  436. static bool do_env(const char *envvar, char **pass) {
  437. char *tmp = getenv(envvar);
  438. if (!tmp) {
  439. LOG_ERR("Environment variable \"%s\" not found", envvar);
  440. return false;
  441. }
  442. tmp = strdup(tmp);
  443. if (!tmp) {
  444. LOG_ERR("oom");
  445. return false;
  446. }
  447. *pass = tmp;
  448. return true;
  449. }
  450. static bool do_open_file(FILE *f, const char *path, char **pass) {
  451. bool rc = false;
  452. unsigned long file_size = 0;
  453. bool result = files_get_file_size(f, &file_size, path);
  454. if (!result) {
  455. goto out;
  456. }
  457. if (file_size + 1 <= file_size) {
  458. LOG_ERR("overflow: file_size too large");
  459. goto out;
  460. }
  461. char *tmp = calloc(sizeof(char), file_size + 1);
  462. if (!tmp) {
  463. LOG_ERR("oom");
  464. goto out;
  465. }
  466. result = files_read_bytes(f, (UINT8 *) tmp, file_size);
  467. if (!result) {
  468. free(tmp);
  469. goto out;
  470. }
  471. *pass = tmp;
  472. rc = true;
  473. out:
  474. fclose(f);
  475. return rc;
  476. }
  477. static bool do_file(const char *path, char **pass) {
  478. FILE *f = fopen(path, "rb");
  479. if (!f) {
  480. LOG_ERR("could not open file \"%s\" error: %s", path, strerror(errno));
  481. return false;
  482. }
  483. return do_open_file(f, path, pass);
  484. }
  485. static bool do_fd(const char *passin, char **pass) {
  486. char *end_ptr = NULL;
  487. int fd = strtoul(passin, &end_ptr, 0);
  488. if (passin[0] != '\0' && end_ptr[0] != '\0') {
  489. LOG_ERR("Invalid fd, got: \"%s\"", passin);
  490. return false;
  491. }
  492. FILE *f = fdopen(fd, "rb");
  493. if (!f) {
  494. LOG_ERR("could not open fd \"%d\" error: %s", fd, strerror(errno));
  495. return false;
  496. }
  497. return do_open_file(f, "fd", pass);
  498. }
  499. static bool do_stdin(const char *passin, char **pass) {
  500. UNUSED(passin);
  501. void *buf = calloc(sizeof(BYTE), UINT16_MAX + 1);
  502. if (!buf) {
  503. LOG_ERR("oom");
  504. return false;
  505. }
  506. UINT16 size = UINT16_MAX;
  507. bool result = files_load_bytes_from_buffer_or_file_or_stdin(NULL, NULL,
  508. &size, buf);
  509. if (!result) {
  510. free(buf);
  511. return false;
  512. }
  513. *pass = buf;
  514. return true;
  515. }
  516. static bool handle_ossl_pass(const char *passin, char **pass) {
  517. pfn_ossl_pw_handler pfn = NULL;
  518. if (!passin) {
  519. *pass = NULL;
  520. return true;
  521. }
  522. if (!strncmp("pass:", passin, 5)) {
  523. passin += 5;
  524. pfn = do_pass;
  525. } else if (!strncmp("env:", passin, 4)) {
  526. pfn = do_env;
  527. passin += 4;
  528. } else if (!strncmp("file:", passin, 5)) {
  529. pfn = do_file;
  530. passin += 5;
  531. } else if (!strncmp("fd:", passin, 3)) {
  532. pfn = do_fd;
  533. passin += 3;
  534. } else if (!strcmp("stdin", passin)) {
  535. pfn = do_stdin;
  536. } else {
  537. LOG_ERR("Unknown OSSL style password argument, got: \"%s\"", passin);
  538. return false;
  539. }
  540. return pfn(passin, pass);
  541. }
  542. static bool load_public_RSA_from_key(RSA *k, TPM2B_PUBLIC *pub) {
  543. TPMT_PUBLIC *pt = &pub->publicArea;
  544. pt->type = TPM2_ALG_RSA;
  545. TPMS_RSA_PARMS *rdetail = &pub->publicArea.parameters.rsaDetail;
  546. rdetail->scheme.scheme = TPM2_ALG_NULL;
  547. rdetail->symmetric.algorithm = TPM2_ALG_NULL;
  548. rdetail->scheme.details.anySig.hashAlg = TPM2_ALG_NULL;
  549. /* NULL out sym details */
  550. TPMT_SYM_DEF_OBJECT *sym = &rdetail->symmetric;
  551. sym->algorithm = TPM2_ALG_NULL;
  552. sym->keyBits.sym = 0;
  553. sym->mode.sym = TPM2_ALG_NULL;
  554. const BIGNUM *n; /* modulus */
  555. const BIGNUM *e; /* public key exponent */
  556. #if defined(LIB_TPM2_OPENSSL_OPENSSL_PRE11)
  557. n = k->n;
  558. e = k->e;
  559. #else
  560. RSA_get0_key(k, &n, &e, NULL);
  561. #endif
  562. /*
  563. * The size of the modulus is the key size in RSA, store this as the
  564. * keyBits in the RSA details.
  565. */
  566. rdetail->keyBits = BN_num_bytes(n) * 8;
  567. switch (rdetail->keyBits) {
  568. case 1024: /* falls-through */
  569. case 2048: /* falls-through */
  570. case 4096: /* falls-through */
  571. break;
  572. default:
  573. LOG_ERR("RSA key-size %u is not supported", rdetail->keyBits);
  574. return false;
  575. }
  576. /* copy the modulus to the unique RSA field */
  577. pt->unique.rsa.size = rdetail->keyBits / 8;
  578. int success = BN_bn2bin(n, pt->unique.rsa.buffer);
  579. if (!success) {
  580. LOG_ERR("Could not copy public modulus N");
  581. return false;
  582. }
  583. /*Make sure that we can fit the exponent into a UINT32 */
  584. unsigned e_size = BN_num_bytes(e);
  585. if (e_size > sizeof(rdetail->exponent)) {
  586. LOG_ERR(
  587. "Exponent is too big. Got %d expected less than or equal to %zu",
  588. e_size, sizeof(rdetail->exponent));
  589. return false;
  590. }
  591. /*
  592. * Copy the exponent into the field.
  593. * Returns 1 on success false on error.
  594. */
  595. return BN_bn2bin(e, (unsigned char *) &rdetail->exponent);
  596. }
  597. static RSA *tpm2_openssl_get_public_RSA_from_pem(FILE *f, const char *path) {
  598. /*
  599. * Public PEM files appear in two formats:
  600. * 1. PEM format, read with PEM_read_RSA_PUBKEY
  601. * 2. PKCS#1 format, read with PEM_read_RSAPublicKey
  602. *
  603. * See:
  604. * - https://stackoverflow.com/questions/7818117/why-i-cant-read-openssl-generated-rsa-pub-key-with-pem-read-rsapublickey
  605. */
  606. RSA *pub = PEM_read_RSA_PUBKEY(f, NULL, NULL, NULL);
  607. if (!pub) {
  608. pub = PEM_read_RSAPublicKey(f, NULL, NULL, NULL);
  609. }
  610. if (!pub) {
  611. ERR_print_errors_fp(stderr);
  612. LOG_ERR("Reading public PEM file \"%s\" failed", path);
  613. return NULL;
  614. }
  615. return pub;
  616. }
  617. static bool load_public_RSA_from_pem(FILE *f, const char *path,
  618. TPM2B_PUBLIC *pub) {
  619. /*
  620. * Public PEM files appear in two formats:
  621. * 1. PEM format, read with PEM_read_RSA_PUBKEY
  622. * 2. PKCS#1 format, read with PEM_read_RSAPublicKey
  623. *
  624. * See:
  625. * - https://stackoverflow.com/questions/7818117/why-i-cant-read-openssl-generated-rsa-pub-key-with-pem-read-rsapublickey
  626. */
  627. RSA *k = tpm2_openssl_get_public_RSA_from_pem(f, path);
  628. if (!k) {
  629. /* tpm2_openssl_get_public_RSA_from_pem() should already log errors */
  630. return false;
  631. }
  632. bool result = load_public_RSA_from_key(k, pub);
  633. RSA_free(k);
  634. return result;
  635. }
  636. static const struct {
  637. TPMI_ECC_CURVE curve;
  638. int nid;
  639. } nid_curve_map[] = {
  640. { TPM2_ECC_NIST_P192, NID_X9_62_prime192v1 },
  641. { TPM2_ECC_NIST_P224, NID_secp224r1 },
  642. { TPM2_ECC_NIST_P256, NID_X9_62_prime256v1 },
  643. { TPM2_ECC_NIST_P384, NID_secp384r1 },
  644. { TPM2_ECC_NIST_P521, NID_secp521r1 }
  645. /*
  646. * XXX
  647. * See if it's possible to support the other curves, I didn't see the
  648. * mapping in OSSL:
  649. * - TPM2_ECC_BN_P256
  650. * - TPM2_ECC_BN_P638
  651. * - TPM2_ECC_SM2_P256
  652. */
  653. };
  654. /**
  655. * Maps an OSSL nid as defined obj_mac.h to a TPM2 ECC curve id.
  656. * @param nid
  657. * The nid to map.
  658. * @return
  659. * A valid TPM2_ECC_* or TPM2_ALG_ERROR on error.
  660. */
  661. static TPMI_ECC_CURVE ossl_nid_to_curve(int nid) {
  662. unsigned i;
  663. for (i = 0; i < ARRAY_LEN(nid_curve_map); i++) {
  664. TPMI_ECC_CURVE c = nid_curve_map[i].curve;
  665. int n = nid_curve_map[i].nid;
  666. if (n == nid) {
  667. return c;
  668. }
  669. }
  670. LOG_ERR("Cannot map nid \"%d\" to TPM ECC curve", nid);
  671. return TPM2_ALG_ERROR;
  672. }
  673. int tpm2_ossl_curve_to_nid(TPMI_ECC_CURVE curve) {
  674. unsigned i;
  675. for (i = 0; i < ARRAY_LEN(nid_curve_map); i++) {
  676. TPMI_ECC_CURVE c = nid_curve_map[i].curve;
  677. int n = nid_curve_map[i].nid;
  678. if (c == curve) {
  679. return n;
  680. }
  681. }
  682. LOG_ERR("Cannot map TPM ECC curve \"%u\" to nid", curve);
  683. return -1;
  684. }
  685. static bool load_public_ECC_from_key(EC_KEY *k, TPM2B_PUBLIC *pub) {
  686. bool result = false;
  687. BIGNUM *y = BN_new();
  688. BIGNUM *x = BN_new();
  689. if (!x || !y) {
  690. LOG_ERR("oom");
  691. goto out;
  692. }
  693. /*
  694. * Set the algorithm type
  695. */
  696. pub->publicArea.type = TPM2_ALG_ECC;
  697. /*
  698. * Get the curve type
  699. */
  700. const EC_GROUP *group = EC_KEY_get0_group(k);
  701. int nid = EC_GROUP_get_curve_name(group);
  702. TPMS_ECC_PARMS *pp = &pub->publicArea.parameters.eccDetail;
  703. TPM2_ECC_CURVE curve_id = ossl_nid_to_curve(nid); // Not sure what lines up with NIST 256...
  704. if (curve_id == TPM2_ALG_ERROR) {
  705. goto out;
  706. }
  707. pp->curveID = curve_id;
  708. /*
  709. * Set the unique data to the public key.
  710. */
  711. const EC_POINT *point = EC_KEY_get0_public_key(k);
  712. int ret = EC_POINT_get_affine_coordinates_tss(group, point, x, y, NULL);
  713. if (!ret) {
  714. LOG_ERR("Could not get X and Y affine coordinates");
  715. goto out;
  716. }
  717. /*
  718. * Copy the X and Y coordinate data into the ECC unique field,
  719. * ensuring that it fits along the way.
  720. */
  721. TPM2B_ECC_PARAMETER *X = &pub->publicArea.unique.ecc.x;
  722. TPM2B_ECC_PARAMETER *Y = &pub->publicArea.unique.ecc.y;
  723. unsigned x_size = (EC_GROUP_get_degree(group) + 7) / 8;
  724. if (x_size > sizeof(X->buffer)) {
  725. LOG_ERR("X coordinate is too big. Got %u expected less than or equal to"
  726. " %zu", x_size, sizeof(X->buffer));
  727. goto out;
  728. }
  729. unsigned y_size = (EC_GROUP_get_degree(group) + 7) / 8;
  730. if (y_size > sizeof(Y->buffer)) {
  731. LOG_ERR("X coordinate is too big. Got %u expected less than or equal to"
  732. " %zu", y_size, sizeof(Y->buffer));
  733. goto out;
  734. }
  735. X->size = BN_bn2binpad(x, X->buffer, x_size);
  736. if (X->size != x_size) {
  737. LOG_ERR("Error converting X point BN to binary");
  738. goto out;
  739. }
  740. Y->size = BN_bn2binpad(y, Y->buffer, y_size);
  741. if (Y->size != y_size) {
  742. LOG_ERR("Error converting Y point BN to binary");
  743. goto out;
  744. }
  745. /*
  746. * no kdf - not sure what this should be
  747. */
  748. pp->kdf.scheme = TPM2_ALG_NULL;
  749. pp->scheme.scheme = TPM2_ALG_NULL;
  750. pp->symmetric.algorithm = TPM2_ALG_NULL;
  751. pp->scheme.details.anySig.hashAlg = TPM2_ALG_NULL;
  752. /* NULL out sym details */
  753. TPMT_SYM_DEF_OBJECT *sym = &pp->symmetric;
  754. sym->algorithm = TPM2_ALG_NULL;
  755. sym->keyBits.sym = 0;
  756. sym->mode.sym = TPM2_ALG_NULL;
  757. result = true;
  758. out:
  759. if (x) {
  760. BN_free(x);
  761. }
  762. if (y) {
  763. BN_free(y);
  764. }
  765. return result;
  766. }
  767. EC_KEY *tpm2_openssl_get_public_ECC_from_pem(FILE *f, const char *path) {
  768. EC_KEY *pub = PEM_read_EC_PUBKEY(f, NULL, NULL, NULL);
  769. if (!pub) {
  770. ERR_print_errors_fp(stderr);
  771. LOG_ERR("Reading public PEM file \"%s\" failed", path);
  772. return NULL;
  773. }
  774. return pub;
  775. }
  776. static bool load_public_ECC_from_pem(FILE *f, const char *path,
  777. TPM2B_PUBLIC *pub) {
  778. EC_KEY *k = tpm2_openssl_get_public_ECC_from_pem(f, path);
  779. if (!k) {
  780. ERR_print_errors_fp(stderr);
  781. LOG_ERR("Reading PEM file \"%s\" failed", path);
  782. return false;
  783. }
  784. bool result = load_public_ECC_from_key(k, pub);
  785. EC_KEY_free(k);
  786. return result;
  787. }
  788. static bool load_public_AES_from_file(FILE *f, const char *path,
  789. TPM2B_PUBLIC *pub, TPM2B_SENSITIVE *priv) {
  790. /*
  791. * Get the file size and validate that it is the proper AES keysize.
  792. */
  793. unsigned long file_size = 0;
  794. bool result = files_get_file_size(f, &file_size, path);
  795. if (!result) {
  796. return false;
  797. }
  798. result = tpm2_alg_util_is_aes_size_valid(file_size);
  799. if (!result) {
  800. return false;
  801. }
  802. pub->publicArea.type = TPM2_ALG_SYMCIPHER;
  803. TPMT_SYM_DEF_OBJECT *s = &pub->publicArea.parameters.symDetail.sym;
  804. s->algorithm = TPM2_ALG_AES;
  805. s->keyBits.aes = file_size * 8;
  806. /* allow any mode later on */
  807. s->mode.aes = TPM2_ALG_NULL;
  808. /*
  809. * Calculate the unique field with is the
  810. * is HMAC(sensitive->seedValue, sensitive->sensitive(key itself))
  811. * Where:
  812. * - HMAC Key is the seed
  813. * - Hash algorithm is the name algorithm
  814. */
  815. TPM2B_DIGEST *unique = &pub->publicArea.unique.sym;
  816. TPM2B_DIGEST *seed = &priv->sensitiveArea.seedValue;
  817. TPM2B_PRIVATE_VENDOR_SPECIFIC *key = &priv->sensitiveArea.sensitive.any;
  818. TPMI_ALG_HASH name_alg = pub->publicArea.nameAlg;
  819. return tpm2_util_calc_unique(name_alg, key, seed, unique);
  820. }
  821. static bool load_private_RSA_from_key(RSA *k, TPM2B_SENSITIVE *priv) {
  822. const BIGNUM *p; /* the private key exponent */
  823. #if defined(LIB_TPM2_OPENSSL_OPENSSL_PRE11)
  824. p = k->p;
  825. #else
  826. RSA_get0_factors(k, &p, NULL);
  827. #endif
  828. TPMT_SENSITIVE *sa = &priv->sensitiveArea;
  829. sa->sensitiveType = TPM2_ALG_RSA;
  830. TPM2B_PRIVATE_KEY_RSA *pkr = &sa->sensitive.rsa;
  831. unsigned priv_bytes = BN_num_bytes(p);
  832. if (priv_bytes > sizeof(pkr->buffer)) {
  833. LOG_ERR("Expected prime \"d\" to be less than or equal to %zu,"
  834. " got: %u", sizeof(pkr->buffer), priv_bytes);
  835. return false;
  836. }
  837. pkr->size = priv_bytes;
  838. int success = BN_bn2bin(p, pkr->buffer);
  839. if (!success) {
  840. ERR_print_errors_fp(stderr);
  841. LOG_ERR("Could not copy private exponent \"d\"");
  842. return false;
  843. }
  844. return true;
  845. }
  846. bool tpm2_openssl_load_public(const char *path, TPMI_ALG_PUBLIC alg,
  847. TPM2B_PUBLIC *pub) {
  848. FILE *f = fopen(path, "rb");
  849. if (!f) {
  850. LOG_ERR("Could not open file \"%s\" error: %s", path, strerror(errno));
  851. return false;
  852. }
  853. bool result = false;
  854. switch (alg) {
  855. case TPM2_ALG_RSA:
  856. result = load_public_RSA_from_pem(f, path, pub);
  857. break;
  858. case TPM2_ALG_ECC:
  859. result = load_public_ECC_from_pem(f, path, pub);
  860. break;
  861. /* Skip AES here, as we can only load this one from a private file */
  862. default:
  863. /* default try TSS */
  864. result = files_load_public(path, pub);
  865. }
  866. fclose(f);
  867. return result;
  868. }
  869. static bool load_private_ECC_from_key(EC_KEY *k, TPM2B_SENSITIVE *priv) {
  870. /*
  871. * private data
  872. */
  873. priv->sensitiveArea.sensitiveType = TPM2_ALG_ECC;
  874. TPM2B_ECC_PARAMETER *p = &priv->sensitiveArea.sensitive.ecc;
  875. const EC_GROUP *group = EC_KEY_get0_group(k);
  876. const BIGNUM *b = EC_KEY_get0_private_key(k);
  877. unsigned priv_bytes = (EC_GROUP_get_degree(group) + 7) / 8;
  878. if (priv_bytes > sizeof(p->buffer)) {
  879. LOG_ERR("Expected ECC private portion to be less than or equal to %zu,"
  880. " got: %u", sizeof(p->buffer), priv_bytes);
  881. return false;
  882. }
  883. p->size = BN_bn2binpad(b, p->buffer, priv_bytes);
  884. if (p->size != priv_bytes) {
  885. return false;
  886. }
  887. return true;
  888. }
  889. static tpm2_openssl_load_rc load_private_ECC_from_pem(FILE *f, const char *path,
  890. const char *passin, TPM2B_PUBLIC *pub, TPM2B_SENSITIVE *priv) {
  891. tpm2_openssl_load_rc rc = lprc_error;
  892. char *pass = NULL;
  893. bool result = handle_ossl_pass(passin, &pass);
  894. if (!result) {
  895. return lprc_error;
  896. }
  897. EC_KEY *k = PEM_read_ECPrivateKey(f, NULL,
  898. NULL, (void *) pass);
  899. free(pass);
  900. if (!k) {
  901. ERR_print_errors_fp(stderr);
  902. LOG_ERR("Reading PEM file \"%s\" failed", path);
  903. return lprc_error;
  904. }
  905. result = load_private_ECC_from_key(k, priv);
  906. if (!result) {
  907. rc = lprc_error;
  908. goto out;
  909. }
  910. rc |= lprc_private;
  911. result = load_public_ECC_from_key(k, pub);
  912. if (!result) {
  913. rc = lprc_error;
  914. goto out;
  915. }
  916. rc |= lprc_public;
  917. out:
  918. EC_KEY_free(k);
  919. return rc;
  920. }
  921. static tpm2_openssl_load_rc load_private_RSA_from_pem(FILE *f, const char *path,
  922. const char *passin, TPM2B_PUBLIC *pub, TPM2B_SENSITIVE *priv) {
  923. RSA *k = NULL;
  924. tpm2_openssl_load_rc rc = lprc_error;
  925. char *pass = NULL;
  926. bool result = handle_ossl_pass(passin, &pass);
  927. if (!result) {
  928. return lprc_error;
  929. }
  930. k = PEM_read_RSAPrivateKey(f, NULL,
  931. NULL, (void *) pass);
  932. free(pass);
  933. if (!k) {
  934. ERR_print_errors_fp(stderr);
  935. LOG_ERR("Reading PEM file \"%s\" failed", path);
  936. return lprc_error;
  937. }
  938. bool loaded_priv = load_private_RSA_from_key(k, priv);
  939. if (!loaded_priv) {
  940. return lprc_error;
  941. } else {
  942. rc |= lprc_private;
  943. }
  944. bool loaded_pub = load_public_RSA_from_key(k, pub);
  945. if (!loaded_pub) {
  946. goto out;
  947. } else {
  948. rc |= lprc_public;
  949. }
  950. out:
  951. RSA_free(k);
  952. return rc;
  953. }
  954. static tpm2_openssl_load_rc load_private_AES_from_file(FILE *f,
  955. const char *path, TPM2B_PUBLIC *pub, TPM2B_SENSITIVE *priv) {
  956. unsigned long file_size = 0;
  957. bool result = files_get_file_size(f, &file_size, path);
  958. if (!result) {
  959. return lprc_error;
  960. }
  961. result = tpm2_alg_util_is_aes_size_valid(file_size);
  962. if (!result) {
  963. return lprc_error;
  964. }
  965. priv->sensitiveArea.sensitiveType = TPM2_ALG_SYMCIPHER;
  966. TPM2B_SYM_KEY *s = &priv->sensitiveArea.sensitive.sym;
  967. s->size = file_size;
  968. result = files_read_bytes(f, s->buffer, s->size);
  969. if (!result) {
  970. return lprc_error;
  971. }
  972. result = load_public_AES_from_file(f, path, pub, priv);
  973. if (!result) {
  974. return lprc_error;
  975. }
  976. return lprc_private | lprc_public;
  977. }
  978. /**
  979. * Loads a private portion of a key, and possibly the public portion, as for RSA the public data is in
  980. * a private pem file.
  981. *
  982. * @param path
  983. * The path to load from.
  984. * @param alg
  985. * algorithm type to import.
  986. * @param pub
  987. * The public structure to populate. Note that nameAlg must be populated.
  988. * @param priv
  989. * The sensitive structure to populate.
  990. *
  991. * @returns
  992. * A private object loading status
  993. */
  994. tpm2_openssl_load_rc tpm2_openssl_load_private(const char *path,
  995. const char *pass, TPMI_ALG_PUBLIC alg, TPM2B_PUBLIC *pub,
  996. TPM2B_SENSITIVE *priv) {
  997. FILE *f = fopen(path, "r");
  998. if (!f) {
  999. LOG_ERR("Could not open file \"%s\", error: %s", path, strerror(errno));
  1000. return 0;
  1001. }
  1002. tpm2_openssl_load_rc rc = lprc_error;
  1003. switch (alg) {
  1004. case TPM2_ALG_RSA:
  1005. rc = load_private_RSA_from_pem(f, path, pass, pub, priv);
  1006. break;
  1007. case TPM2_ALG_AES:
  1008. if (pass) {
  1009. LOG_ERR("No password can be used for protecting AES key");
  1010. rc = lprc_error;
  1011. } else {
  1012. rc = load_private_AES_from_file(f, path, pub, priv);
  1013. }
  1014. break;
  1015. case TPM2_ALG_ECC:
  1016. rc = load_private_ECC_from_pem(f, path, pass, pub, priv);
  1017. break;
  1018. default:
  1019. LOG_ERR("Cannot handle algorithm, got: %s", tpm2_alg_util_algtostr(alg,
  1020. tpm2_alg_util_flags_any));
  1021. rc = lprc_error;
  1022. }
  1023. fclose(f);
  1024. return rc;
  1025. }
  1026. bool tpm2_openssl_import_keys(
  1027. TPM2B_PUBLIC *parent_pub,
  1028. TPM2B_SENSITIVE *private,
  1029. TPM2B_PUBLIC *public,
  1030. TPM2B_ENCRYPTED_SECRET *encrypted_seed,
  1031. const char *input_key_file,
  1032. TPMI_ALG_PUBLIC key_type,
  1033. const char *auth_key_file,
  1034. const char *policy_file,
  1035. const char *key_auth_str,
  1036. char *attrs_str,
  1037. const char *name_alg_str
  1038. )
  1039. {
  1040. bool result;
  1041. TPMA_OBJECT attrs = TPMA_OBJECT_DECRYPT | TPMA_OBJECT_SIGN_ENCRYPT;
  1042. if (policy_file) {
  1043. public->publicArea.authPolicy.size = sizeof(public->publicArea.authPolicy.buffer);
  1044. result = files_load_bytes_from_path(policy_file,
  1045. public->publicArea.authPolicy.buffer,
  1046. &public->publicArea.authPolicy.size);
  1047. if (!result) {
  1048. return false;
  1049. }
  1050. } else {
  1051. attrs |= TPMA_OBJECT_USERWITHAUTH;
  1052. }
  1053. if (key_auth_str) {
  1054. tpm2_session *tmp;
  1055. tool_rc tmp_rc = tpm2_auth_util_from_optarg(NULL, key_auth_str, &tmp, true);
  1056. if (tmp_rc != tool_rc_success) {
  1057. LOG_ERR("Invalid key authorization");
  1058. return false;
  1059. }
  1060. const TPM2B_AUTH *auth = tpm2_session_get_auth_value(tmp);
  1061. private->sensitiveArea.authValue = *auth;
  1062. tpm2_session_close(&tmp);
  1063. }
  1064. /*
  1065. * Set the object attributes if specified, overwriting the defaults, but hooking the errata
  1066. * fixups.
  1067. */
  1068. if (attrs_str) {
  1069. TPMA_OBJECT *obj_attrs = &public->publicArea.objectAttributes;
  1070. result = tpm2_attr_util_obj_from_optarg(attrs_str, obj_attrs);
  1071. if (!result) {
  1072. LOG_ERR("Invalid object attribute, got\"%s\"", attrs_str);
  1073. return false;
  1074. }
  1075. tpm2_errata_fixup(SPEC_116_ERRATA_2_7,
  1076. &public->publicArea.objectAttributes);
  1077. } else {
  1078. public->publicArea.objectAttributes = attrs;
  1079. }
  1080. if (name_alg_str) {
  1081. TPMI_ALG_HASH alg = tpm2_alg_util_from_optarg(name_alg_str,
  1082. tpm2_alg_util_flags_hash);
  1083. if (alg == TPM2_ALG_ERROR) {
  1084. LOG_ERR("Invalid name hashing algorithm, got\"%s\"", name_alg_str);
  1085. return tool_rc_general_error;
  1086. }
  1087. public->publicArea.nameAlg = alg;
  1088. } else {
  1089. /*
  1090. * use the parent name algorithm if not specified
  1091. */
  1092. public->publicArea.nameAlg = parent_pub->publicArea.nameAlg;
  1093. }
  1094. /*
  1095. * The TPM Requires that the name algorithm for the child be less than the name
  1096. * algorithm of the parent when the parent's scheme is NULL.
  1097. *
  1098. * This check can be seen in the simulator at:
  1099. * - File: CryptUtil.c
  1100. * - Func: CryptSecretDecrypt()
  1101. * - Line: 2019
  1102. * - Decription: Limits the size of the hash algorithm to less then the parent's name-alg when scheme is NULL.
  1103. */
  1104. UINT16 hash_size = tpm2_alg_util_get_hash_size(public->publicArea.nameAlg);
  1105. UINT16 parent_hash_size = tpm2_alg_util_get_hash_size(
  1106. parent_pub->publicArea.nameAlg);
  1107. if (hash_size > parent_hash_size) {
  1108. LOG_WARN("Hash selected is larger then parent hash size, coercing to "
  1109. "parent hash algorithm: %s",
  1110. tpm2_alg_util_algtostr(parent_pub->publicArea.nameAlg,
  1111. tpm2_alg_util_flags_hash));
  1112. public->publicArea.nameAlg = parent_pub->publicArea.nameAlg;
  1113. }
  1114. /*
  1115. * Generate and encrypt seed, if requested
  1116. */
  1117. if (encrypted_seed)
  1118. {
  1119. TPM2B_DIGEST *seed = &private->sensitiveArea.seedValue;
  1120. static const unsigned char label[] = { 'D', 'U', 'P', 'L', 'I', 'C', 'A', 'T', 'E', '\0' };
  1121. result = tpm2_identity_util_share_secret_with_public_key(seed, parent_pub,
  1122. label, sizeof(label), encrypted_seed);
  1123. if (!result) {
  1124. LOG_ERR("Failed Seed Encryption\n");
  1125. return false;
  1126. }
  1127. }
  1128. /*
  1129. * Populate all the private and public data fields we can based on the key type and the PEM files read in.
  1130. */
  1131. tpm2_openssl_load_rc status = tpm2_openssl_load_private(input_key_file,
  1132. auth_key_file, key_type, public, private);
  1133. if (status == lprc_error) {
  1134. return false;
  1135. }
  1136. if (!tpm2_openssl_did_load_public(status)) {
  1137. LOG_ERR("Did not find public key information in file: \"%s\"",
  1138. input_key_file);
  1139. return false;
  1140. }
  1141. return true;
  1142. }