files.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <errno.h>
  3. #include <inttypes.h>
  4. #include <libgen.h>
  5. #include <limits.h>
  6. #include <stdbool.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <strings.h>
  11. #include <tss2/tss2_mu.h>
  12. #include "files.h"
  13. #include "log.h"
  14. #include "tpm2.h"
  15. #include "tpm2_tool.h"
  16. /**
  17. * This is the magic for the file header. The header is organized
  18. * as a big endian U32 (BEU32) of MAGIC followed by a BEU32 of the
  19. * version number. Tools can define their own, individual file
  20. * formats as they make sense, but they should always have the header.
  21. */
  22. static const UINT32 MAGIC = 0xBADCC0DE;
  23. #define BAIL_ON_NULL(param, x) \
  24. do { \
  25. if (!x) { \
  26. LOG_ERR(param" must be specified"); \
  27. return false; \
  28. } \
  29. } while(0)
  30. /**
  31. * Writes size bytes to a file, continuing on EINTR short writes.
  32. * @param f
  33. * The file to write to.
  34. * @param data
  35. * The data to write.
  36. * @param size
  37. * The size, in bytes, of that data.
  38. * @return
  39. * True on success, False otherwise.
  40. */
  41. static bool writex(FILE *f, UINT8 *data, size_t size) {
  42. size_t wrote = 0;
  43. size_t index = 0;
  44. do {
  45. wrote = fwrite(&data[index], 1, size, f);
  46. if (wrote != size) {
  47. if (errno != EINTR) {
  48. return false;
  49. }
  50. /* continue on EINTR */
  51. }
  52. size -= wrote;
  53. index += wrote;
  54. } while (size > 0);
  55. return true;
  56. }
  57. /**
  58. * Reads size bytes from a file, continuing on EINTR short reads.
  59. * @param f
  60. * The file to read from.
  61. * @param data
  62. * The data buffer to read into.
  63. * @param size
  64. * The size of the buffer, which is also the amount of bytes to read.
  65. * @return
  66. * The number of bytes that have been read.
  67. */
  68. static size_t readx(FILE *f, UINT8 *data, size_t size) {
  69. size_t bread = 0;
  70. do {
  71. bread += fread(&data[bread], 1, size-bread, f);
  72. } while (bread < size && !feof(f) && errno == EINTR);
  73. return bread;
  74. }
  75. bool files_get_file_size(FILE *fp, unsigned long *file_size, const char *path) {
  76. long current = ftell(fp);
  77. if (current < 0) {
  78. if (path) {
  79. LOG_ERR("Error getting current file offset for file \"%s\" error: "
  80. "%s", path, strerror(errno));
  81. }
  82. return false;
  83. }
  84. int rc = fseek(fp, 0, SEEK_END);
  85. if (rc < 0) {
  86. if (path) {
  87. LOG_ERR("Error seeking to end of file \"%s\" error: %s", path,
  88. strerror(errno));
  89. }
  90. return false;
  91. }
  92. long size = ftell(fp);
  93. if (size < 0) {
  94. if (path) {
  95. LOG_ERR("ftell on file \"%s\" failed: %s", path, strerror(errno));
  96. }
  97. return false;
  98. }
  99. rc = fseek(fp, current, SEEK_SET);
  100. if (rc < 0) {
  101. if (path) {
  102. LOG_ERR(
  103. "Could not restore initial stream position for file \"%s\" "
  104. "failed: %s", path, strerror(errno));
  105. }
  106. return false;
  107. }
  108. /* size cannot be negative at this point */
  109. *file_size = (unsigned long) size;
  110. return true;
  111. }
  112. bool file_read_bytes_from_file(FILE *f, UINT8 *buf, UINT16 *size,
  113. const char *path) {
  114. unsigned long file_size;
  115. bool result = files_get_file_size(f, &file_size, path);
  116. if (!result) {
  117. /* get_file_size() logs errors */
  118. return false;
  119. }
  120. /* max is bounded on *size */
  121. if (file_size > *size) {
  122. if (path) {
  123. LOG_ERR(
  124. "File \"%s\" size is larger than buffer, got %lu expected "
  125. "less than or equal to %u", path, file_size, *size);
  126. }
  127. return false;
  128. }
  129. /* The reported file size is not always correct, e.g. for sysfs files
  130. generated on the fly by the kernel when they are read, which appear as
  131. having size 0. Read as many bytes as we can until EOF is reached or the
  132. provided buffer is full. As a small sanity check, fail if the number of
  133. bytes read is smaller than the reported file size. */
  134. *size = readx(f, buf, *size);
  135. if (*size < file_size) {
  136. if (path) {
  137. LOG_ERR("Could not read data from file \"%s\"", path);
  138. }
  139. return false;
  140. }
  141. return true;
  142. }
  143. bool files_load_bytes_from_path(const char *path, UINT8 *buf, UINT16 *size) {
  144. if (!buf || !size || !path) {
  145. return false;
  146. }
  147. FILE *f = fopen(path, "rb");
  148. if (!f) {
  149. LOG_ERR("Could not open file \"%s\" error %s", path, strerror(errno));
  150. return false;
  151. }
  152. bool result = file_read_bytes_from_file(f, buf, size, path);
  153. fclose(f);
  154. return result;
  155. }
  156. bool files_save_bytes_to_file(const char *path, UINT8 *buf, UINT16 size) {
  157. if (!buf) {
  158. return false;
  159. }
  160. if (!path && !output_enabled) {
  161. return true;
  162. }
  163. FILE *fp = path ? fopen(path, "wb+") : stdout;
  164. if (!fp) {
  165. LOG_ERR("Could not open file \"%s\", error: %s", path, strerror(errno));
  166. return false;
  167. }
  168. bool result = files_write_bytes(fp, buf, size);
  169. if (!result) {
  170. LOG_ERR("Could not write data to file \"%s\"", path);
  171. }
  172. if (fp != stdout) {
  173. fclose(fp);
  174. }
  175. return result;
  176. }
  177. /*
  178. * Current version to write TPMS_CONTEXT to disk.
  179. */
  180. #define CONTEXT_VERSION 1
  181. bool files_save_context(TPMS_CONTEXT *context, FILE *stream) {
  182. /*
  183. * Saving the TPMS_CONTEXT structure to disk, format:
  184. * TPM2.0-TOOLS HEADER
  185. * U32 hierarchy
  186. * U32 savedHandle
  187. * U64 sequence
  188. * U16 contextBlobLength
  189. * BYTE[] contextBlob
  190. */
  191. bool result = files_write_header(stream, CONTEXT_VERSION);
  192. if (!result) {
  193. LOG_ERR("Could not write context file header");
  194. goto out;
  195. }
  196. // UINT32
  197. result = files_write_32(stream, context->hierarchy);
  198. if (!result) {
  199. LOG_ERR("Could not write hierarchy");
  200. goto out;
  201. }
  202. result = files_write_32(stream, context->savedHandle);
  203. if (!result) {
  204. LOG_ERR("Could not write savedHandle");
  205. goto out;
  206. }
  207. LOG_INFO("Save TPMS_CONTEXT->savedHandle: 0x%x", context->savedHandle);
  208. // UINT64
  209. result = files_write_64(stream, context->sequence);
  210. if (!result) {
  211. LOG_ERR("Could not write sequence");
  212. goto out;
  213. }
  214. // U16 LENGTH
  215. result = files_write_16(stream, context->contextBlob.size);
  216. if (!result) {
  217. LOG_ERR("Could not write contextBob size");
  218. goto out;
  219. }
  220. // BYTE[] contextBlob
  221. result = files_write_bytes(stream, context->contextBlob.buffer,
  222. context->contextBlob.size);
  223. if (!result) {
  224. LOG_ERR("Could not write contextBlob buffer");
  225. }
  226. /* result is set by file_write_bytes() */
  227. out:
  228. return result;
  229. }
  230. tool_rc files_save_tpm_context_to_file(ESYS_CONTEXT *ectx, ESYS_TR handle,
  231. FILE *stream) {
  232. TPMS_CONTEXT *context = NULL;
  233. tool_rc rc = tpm2_context_save(ectx, handle, &context);
  234. if (rc != tool_rc_success) {
  235. return rc;
  236. }
  237. bool result = files_save_context(context, stream);
  238. free(context);
  239. return result ? tool_rc_success : tool_rc_general_error;
  240. }
  241. tool_rc files_save_tpm_context_to_path(ESYS_CONTEXT *context, ESYS_TR handle,
  242. const char *path) {
  243. FILE *f = fopen(path, "w+b");
  244. if (!f) {
  245. LOG_ERR("Error opening file \"%s\" due to error: %s", path,
  246. strerror(errno));
  247. return tool_rc_general_error;
  248. }
  249. tool_rc rc = files_save_tpm_context_to_file(context, handle, f);
  250. fclose(f);
  251. return rc;
  252. }
  253. static bool load_tpm_context_file(FILE *fstream, TPMS_CONTEXT *context) {
  254. /*
  255. * Reading the TPMS_CONTEXT structure to disk, format:
  256. * TPM2.0-TOOLS HEADER
  257. * U32 hierarchy
  258. * U32 savedHandle
  259. * U64 sequence
  260. * U16 contextBlobLength
  261. * BYTE[] contextBlob
  262. */
  263. UINT32 version;
  264. bool result = files_read_header(fstream, &version);
  265. if (!result) {
  266. LOG_WARN("The loaded tpm context does not appear to be in the proper "
  267. "format, assuming old format, this will be converted on the "
  268. "next save.");
  269. rewind(fstream);
  270. result = files_read_bytes(fstream, (UINT8 *) context, sizeof(*context));
  271. if (!result) {
  272. LOG_ERR("Could not load tpm context file");
  273. goto out;
  274. }
  275. /* Success load the context into the TPM */
  276. goto out;
  277. }
  278. if (version != CONTEXT_VERSION) {
  279. LOG_ERR("Unsupported context file format version found, got: %"PRIu32,
  280. version);
  281. result = false;
  282. goto out;
  283. }
  284. result = files_read_32(fstream, &context->hierarchy);
  285. if (!result) {
  286. LOG_ERR("Error reading hierarchy!");
  287. goto out;
  288. }
  289. result = files_read_32(fstream, &context->savedHandle);
  290. if (!result) {
  291. LOG_ERR("Error reading savedHandle!");
  292. goto out;
  293. }
  294. LOG_INFO("load: TPMS_CONTEXT->savedHandle: 0x%x", context->savedHandle);
  295. result = files_read_64(fstream, &context->sequence);
  296. if (!result) {
  297. LOG_ERR("Error reading sequence!");
  298. goto out;
  299. }
  300. result = files_read_16(fstream, &context->contextBlob.size);
  301. if (!result) {
  302. LOG_ERR("Error reading contextBlob.size!");
  303. goto out;
  304. }
  305. if (context->contextBlob.size > sizeof(context->contextBlob.buffer)) {
  306. LOG_ERR("Size mismatch found on contextBlob, got %"PRIu16" expected "
  307. "less than or equal to %zu", context->contextBlob.size,
  308. sizeof(context->contextBlob.buffer));
  309. result = false;
  310. goto out;
  311. }
  312. result = files_read_bytes(fstream, context->contextBlob.buffer,
  313. context->contextBlob.size);
  314. if (!result) {
  315. LOG_ERR("Error reading contextBlob.size!");
  316. goto out;
  317. }
  318. out:
  319. return result;
  320. }
  321. static bool check_magic(FILE *fstream, bool seek_reset) {
  322. BAIL_ON_NULL("FILE", fstream);
  323. UINT32 magic = 0;
  324. bool res = files_read_32(fstream, &magic);
  325. if (!res) {
  326. return false;
  327. }
  328. bool match = magic == MAGIC;
  329. if (seek_reset) {
  330. int rc = fseek(fstream, -sizeof(magic), SEEK_CUR);
  331. if (rc != 0) {
  332. LOG_ERR("fseek failed: %s", strerror(errno));
  333. return false;
  334. }
  335. return match;
  336. }
  337. if (!match) {
  338. LOG_ERR("Found magic 0x%x did not match expected magic of 0x%x!", magic,
  339. MAGIC);
  340. }
  341. return match;
  342. }
  343. tool_rc files_load_tpm_context_from_file(ESYS_CONTEXT *context,
  344. ESYS_TR *tr_handle, FILE *fstream) {
  345. TPMS_CONTEXT tpms_context;
  346. tool_rc rc = tool_rc_general_error;
  347. bool result = check_magic(fstream, true);
  348. if (result) {
  349. LOG_INFO("Assuming tpm context file");
  350. result = load_tpm_context_file(fstream, &tpms_context);
  351. if (!result) {
  352. LOG_ERR("Failed to load_tpm_context_file()");
  353. goto out;
  354. }
  355. return tpm2_context_load(context, &tpms_context, tr_handle);
  356. }
  357. ESYS_TR loaded_handle;
  358. LOG_INFO("Assuming tpm context file");
  359. /* try ESYS TR deserialize */
  360. unsigned long size = 0;
  361. result = files_get_file_size(fstream, &size, NULL);
  362. if (!result) {
  363. LOG_ERR("Failed to get file size: %s", strerror(ferror(fstream)));
  364. goto out;
  365. }
  366. if (size < 1) {
  367. LOG_ERR("Invalid serialized ESYS_TR size, got: %lu", size);
  368. goto out;
  369. }
  370. uint8_t *buffer = calloc(1, size);
  371. if (!buffer) {
  372. LOG_ERR("oom");
  373. goto out;
  374. }
  375. result = files_read_bytes(fstream, buffer, size);
  376. if (!result) {
  377. LOG_ERR("Could not read serialized ESYS_TR from disk");
  378. free(buffer);
  379. goto out;
  380. }
  381. rc = tpm2_tr_deserialize(context, buffer, size, &loaded_handle);
  382. free(buffer);
  383. if (rc == tool_rc_success) {
  384. *tr_handle = loaded_handle;
  385. }
  386. out: return rc;
  387. }
  388. tool_rc files_load_tpm_context_from_path(ESYS_CONTEXT *context,
  389. ESYS_TR *tr_handle, const char *path) {
  390. FILE *f = fopen(path, "rb");
  391. if (!f) {
  392. LOG_WARN("Error opening file \"%s\" due to error: %s", path,
  393. strerror(errno));
  394. return false;
  395. }
  396. tool_rc rc = files_load_tpm_context_from_file(context, tr_handle, f);
  397. fclose(f);
  398. return rc;
  399. }
  400. bool files_does_file_exist(const char *path) {
  401. if (!path) {
  402. LOG_ERR("Path cannot be NULL");
  403. return false;
  404. }
  405. FILE *fp = fopen(path, "rb");
  406. if (fp) {
  407. fclose(fp);
  408. LOG_WARN("Path: %s already exists. Please rename or delete the file!",
  409. path);
  410. return true;
  411. }
  412. return false;
  413. }
  414. bool files_get_file_size_path(const char *path, unsigned long *file_size) {
  415. bool result = false;
  416. if (!path) {
  417. LOG_ERR("Must specify a path argument, cannot be NULL!");
  418. return false;
  419. }
  420. if (!file_size) {
  421. LOG_ERR("Must specify a file size argument, cannot be NULL!");
  422. return false;
  423. }
  424. FILE *fp = fopen(path, "rb");
  425. if (!fp) {
  426. LOG_ERR("Could not open file: \"%s\" error: %s", path, strerror(errno));
  427. return false;
  428. }
  429. result = files_get_file_size(fp, file_size, path);
  430. fclose(fp);
  431. return result;
  432. }
  433. #define BE_CONVERT(value, size) \
  434. do { \
  435. if (!tpm2_util_is_big_endian()) { \
  436. value = tpm2_util_endian_swap_##size(value); \
  437. } \
  438. } while (0)
  439. #define FILE_WRITE(size) \
  440. bool files_write_##size(FILE *out, UINT##size data) { \
  441. BAIL_ON_NULL("FILE", out); \
  442. BE_CONVERT(data, size); \
  443. return writex(out, (UINT8 *)&data, sizeof(data)); \
  444. }
  445. #define FILE_READ(size) \
  446. bool files_read_##size(FILE *out, UINT##size *data) { \
  447. BAIL_ON_NULL("FILE", out); \
  448. BAIL_ON_NULL("data", data); \
  449. bool res = (readx(out, (UINT8 *)data, sizeof(*data)) == sizeof(*data)); \
  450. if (res) { \
  451. BE_CONVERT(*data, size); \
  452. } \
  453. return res; \
  454. }
  455. /*
  456. * all the files_read|write_bytes_16|32|64 functions
  457. */
  458. FILE_READ(16);
  459. FILE_WRITE(16)
  460. FILE_READ(32);
  461. FILE_WRITE(32)
  462. FILE_READ(64)
  463. FILE_WRITE(64)
  464. bool files_read_bytes(FILE *out, UINT8 bytes[], size_t len) {
  465. BAIL_ON_NULL("FILE", out);
  466. BAIL_ON_NULL("bytes", bytes);
  467. return (readx(out, bytes, len) == len);
  468. }
  469. bool files_write_bytes(FILE *out, uint8_t bytes[], size_t len) {
  470. BAIL_ON_NULL("FILE", out);
  471. BAIL_ON_NULL("bytes", bytes);
  472. return writex(out, bytes, len);
  473. }
  474. bool files_write_header(FILE *out, UINT32 version) {
  475. BAIL_ON_NULL("FILE", out);
  476. bool res = files_write_32(out, MAGIC);
  477. if (!res) {
  478. return false;
  479. }
  480. return files_write_32(out, version);
  481. }
  482. bool files_read_header(FILE *out, uint32_t *version) {
  483. BAIL_ON_NULL("FILE", out);
  484. BAIL_ON_NULL("version", version);
  485. bool result = check_magic(out, false);
  486. if (!result) {
  487. return false;
  488. }
  489. return files_read_32(out, version);
  490. }
  491. bool files_load_bytes_from_buffer_or_file_or_stdin(const char *input_buffer,
  492. const char *path, UINT16 *size, BYTE *buf) {
  493. UINT16 upper_bound = *size;
  494. if (!upper_bound) {
  495. return true;
  496. }
  497. bool retval = true;
  498. /* Read from stdin */
  499. if (!input_buffer && !path) {
  500. UINT16 read_bytes = 0;
  501. while (1) {
  502. read_bytes += fread(buf, 1, upper_bound, stdin);
  503. if (feof(stdin)) {
  504. *size = read_bytes;
  505. return true;
  506. }
  507. if (ferror(stdin)) {
  508. LOG_ERR("Failed read from stdin.");
  509. return false;
  510. }
  511. }
  512. /* read from a buffer */
  513. } else if (input_buffer) {
  514. size_t input_buffer_size = strlen(input_buffer);
  515. if (path) {
  516. LOG_ERR("Specify either the input buffer or file path to load data,"
  517. " not both");
  518. }
  519. if (input_buffer_size != (size_t) upper_bound) {
  520. LOG_ERR("Unexpected data size. Got %u expected %u", upper_bound,
  521. (unsigned )input_buffer_size);
  522. return false;
  523. } else {
  524. memcpy(buf, input_buffer, input_buffer_size);
  525. return true;
  526. }
  527. } else if (path) {
  528. retval = files_load_bytes_from_path(path, buf, size);
  529. }
  530. return retval;
  531. }
  532. tool_rc files_save_ESYS_TR(ESYS_CONTEXT *ectx, ESYS_TR handle, const char *path) {
  533. size_t size;
  534. uint8_t *buffer;
  535. tool_rc rc = tpm2_tr_serialize(ectx, handle, &buffer, &size);
  536. if (rc != tool_rc_success) {
  537. return rc;
  538. }
  539. bool result = files_save_bytes_to_file(path, buffer, size);
  540. free(buffer);
  541. return result ? tool_rc_success : tool_rc_general_error;
  542. }
  543. #define SAVE_TYPE(type, name) \
  544. bool files_save_##name(type *name, const char *path) { \
  545. \
  546. size_t offset = 0; \
  547. UINT8 buffer[sizeof(*name)]; \
  548. TSS2_RC rc = Tss2_MU_##type##_Marshal(name, buffer, sizeof(buffer), &offset); \
  549. if (rc != TSS2_RC_SUCCESS) { \
  550. LOG_ERR("Error serializing "str(name)" structure: 0x%x", rc); \
  551. return false; \
  552. } \
  553. \
  554. return files_save_bytes_to_file(path, buffer, offset); \
  555. }
  556. #define LOAD_TYPE(type, name) \
  557. bool files_load_##name(const char *path, type *name) { \
  558. \
  559. UINT8 buffer[sizeof(*name)]; \
  560. UINT16 size = sizeof(buffer); \
  561. bool res = files_load_bytes_from_path(path, buffer, &size); \
  562. if (!res) { \
  563. return false; \
  564. } \
  565. \
  566. size_t offset = 0; \
  567. TSS2_RC rc = Tss2_MU_##type##_Unmarshal(buffer, size, &offset, name); \
  568. if (rc != TSS2_RC_SUCCESS) { \
  569. LOG_ERR("Error deserializing "str(name)" structure: 0x%x", rc); \
  570. LOG_ERR("The input file needs to be a valid "xstr(type)" data structure"); \
  571. return false; \
  572. } \
  573. \
  574. return rc == TPM2_RC_SUCCESS; \
  575. }
  576. #define LOAD_TYPE_FILE(type, name) \
  577. bool files_load_##name##_file(FILE *f, const char *path, type *name) { \
  578. \
  579. UINT8 buffer[sizeof(*name)]; \
  580. UINT16 size = sizeof(buffer); \
  581. bool res = file_read_bytes_from_file(f, buffer, &size, path); \
  582. if (!res) { \
  583. return false; \
  584. } \
  585. \
  586. size_t offset = 0; \
  587. TSS2_RC rc = Tss2_MU_##type##_Unmarshal(buffer, size, &offset, name); \
  588. if (rc != TSS2_RC_SUCCESS) { \
  589. LOG_ERR("Error deserializing "str(name)" structure: 0x%x", rc); \
  590. LOG_ERR("The input file needs to be a valid "xstr(type)" data structure"); \
  591. return false; \
  592. } \
  593. \
  594. return rc == TPM2_RC_SUCCESS; \
  595. }
  596. #define LOAD_TYPE_SILENT(type, name) \
  597. bool files_load_##name##_silent(const char *path, type *name) { \
  598. \
  599. UINT8 buffer[sizeof(*name)]; \
  600. UINT16 size = sizeof(buffer); \
  601. bool res = files_load_bytes_from_path(path, buffer, &size); \
  602. if (!res) { \
  603. return false; \
  604. } \
  605. \
  606. size_t offset = 0; \
  607. TSS2_RC rc = Tss2_MU_##type##_Unmarshal(buffer, size, &offset, name); \
  608. if (rc != TSS2_RC_SUCCESS) { \
  609. return false; \
  610. } \
  611. \
  612. return rc == TPM2_RC_SUCCESS; \
  613. }
  614. SAVE_TYPE(TPM2B_PUBLIC, public)
  615. LOAD_TYPE(TPM2B_PUBLIC, public)
  616. LOAD_TYPE_FILE(TPM2B_PUBLIC, public)
  617. LOAD_TYPE_SILENT(TPM2B_PUBLIC, public)
  618. SAVE_TYPE(TPMT_PUBLIC, template)
  619. LOAD_TYPE(TPMT_PUBLIC, template)
  620. LOAD_TYPE_FILE(TPMT_PUBLIC, template)
  621. LOAD_TYPE_SILENT(TPMT_PUBLIC, template)
  622. SAVE_TYPE(TPMT_SIGNATURE, signature)
  623. LOAD_TYPE(TPMT_SIGNATURE, signature)
  624. LOAD_TYPE_SILENT(TPMT_SIGNATURE, signature)
  625. SAVE_TYPE(TPMT_TK_VERIFIED, ticket)
  626. LOAD_TYPE(TPMT_TK_VERIFIED, ticket)
  627. SAVE_TYPE(TPMT_TK_AUTH, authorization_ticket)
  628. LOAD_TYPE(TPMT_TK_AUTH, authorization_ticket)
  629. SAVE_TYPE(TPMT_TK_CREATION, creation_ticket)
  630. LOAD_TYPE(TPMT_TK_CREATION, creation_ticket)
  631. SAVE_TYPE(TPM2B_DIGEST, digest)
  632. LOAD_TYPE(TPM2B_DIGEST, digest)
  633. SAVE_TYPE(TPM2B_CREATION_DATA, creation_data)
  634. LOAD_TYPE(TPM2B_CREATION_DATA, creation_data)
  635. SAVE_TYPE(TPM2B_SENSITIVE, sensitive)
  636. LOAD_TYPE(TPM2B_SENSITIVE, sensitive)
  637. SAVE_TYPE(TPMT_TK_HASHCHECK, validation)
  638. LOAD_TYPE(TPMT_TK_HASHCHECK, validation)
  639. SAVE_TYPE(TPM2B_PRIVATE, private)
  640. LOAD_TYPE(TPM2B_PRIVATE, private)
  641. SAVE_TYPE(TPM2B_ENCRYPTED_SECRET, encrypted_seed)
  642. LOAD_TYPE(TPM2B_ENCRYPTED_SECRET, encrypted_seed)
  643. SAVE_TYPE(TPMS_ALGORITHM_DETAIL_ECC, ecc_details)
  644. SAVE_TYPE(TPM2B_ECC_POINT, ecc_point)
  645. LOAD_TYPE(TPM2B_ECC_POINT, ecc_point)
  646. LOAD_TYPE(TPM2B_ECC_PARAMETER, ecc_parameter)
  647. LOAD_TYPE_FILE(TPMS_ATTEST, attest)
  648. tool_rc files_tpm2b_attest_to_tpms_attest(TPM2B_ATTEST *quoted, TPMS_ATTEST *attest) {
  649. size_t offset = 0;
  650. TSS2_RC rval = Tss2_MU_TPMS_ATTEST_Unmarshal(quoted->attestationData,
  651. quoted->size, &offset, attest);
  652. if (rval != TSS2_RC_SUCCESS) {
  653. LOG_PERR(Tss2_MU_TPM2B_ATTEST_Unmarshal, rval);
  654. return tool_rc_from_tpm(rval);
  655. }
  656. return tool_rc_success;
  657. }
  658. tool_rc files_load_unique_data(const char *file_path,
  659. TPM2B_PUBLIC *public_data) {
  660. /*
  661. * TPM2_MAX_RSA_KEY_BYTES which expands to 512 bytes is the maximum unique
  662. * size for RSA and other key types.
  663. *
  664. * Additionally this may still prove to be a bigger value than what has been
  665. * implemented in a TPM. For example the value of MAX_RSA_KEY_BYTES is 256
  666. * for ibmSimulator as specified in implementation.h
  667. */
  668. UINT16 unique_size = TPM2_MAX_RSA_KEY_BYTES;
  669. uint8_t file_data[TPM2_MAX_RSA_KEY_BYTES];
  670. bool result = files_load_bytes_from_buffer_or_file_or_stdin(NULL,
  671. file_path, &unique_size, file_data);
  672. if (!result) {
  673. LOG_ERR("Failed to load unique data from file/ stdin.");
  674. return tool_rc_general_error;
  675. }
  676. /*
  677. * If the data is specified as a file, the user is responsible for ensuring
  678. * that that this buffer is formatted as TPMU_PUBLIC_ID union.
  679. */
  680. if (file_path) {
  681. memcpy(&public_data->publicArea.unique, file_data, unique_size);
  682. return tool_rc_success;
  683. }
  684. /* Unstructured stdin unique data paired with a RSA object */
  685. if (public_data->publicArea.type == TPM2_ALG_RSA) {
  686. public_data->publicArea.unique.rsa.size = unique_size;
  687. memcpy(&public_data->publicArea.unique.rsa.buffer, file_data,
  688. unique_size);
  689. }
  690. /* Unstructured stdin unique data paired with an ECC object */
  691. if (public_data->publicArea.type == TPM2_ALG_ECC) {
  692. /* The size limitation is TPM implementation dependent. */
  693. if((unique_size / 2) > TPM2_MAX_ECC_KEY_BYTES) {
  694. LOG_ERR("Unique data too big for ECC object's allowed unique size");
  695. return tool_rc_general_error;
  696. }
  697. /* Copy data into X and Y coordinates unique buffers */
  698. public_data->publicArea.unique.ecc.x.size = (unique_size / 2);
  699. memcpy(&public_data->publicArea.unique.ecc.x.buffer, file_data,
  700. (unique_size / 2));
  701. /* Copy the rest of buffer to y and also adjust size if input is odd */
  702. public_data->publicArea.unique.ecc.y.size = (unique_size / 2) +
  703. (unique_size % 2);
  704. memcpy(&public_data->publicArea.unique.ecc.y.buffer,
  705. file_data + (unique_size / 2), (unique_size / 2));
  706. }
  707. /* Unstructured stdin unique data paired with an Keyedhash object */
  708. if (public_data->publicArea.type == TPM2_ALG_KEYEDHASH) {
  709. /* The size limitation is TPM implementation dependent. */
  710. if (unique_size > sizeof(TPMU_HA)) {
  711. LOG_ERR("Unique data too big for keyedhash object's allowed unique "
  712. "size.");
  713. return tool_rc_general_error;
  714. }
  715. public_data->publicArea.unique.keyedHash.size = unique_size;
  716. memcpy(&public_data->publicArea.unique.keyedHash.buffer, file_data,
  717. unique_size);
  718. }
  719. /* Unstructured stdin unique data paired with an Symcipher object */
  720. if (public_data->publicArea.type == TPM2_ALG_SYMCIPHER) {
  721. /* The size limitation is TPM implementation dependent. */
  722. if (unique_size > sizeof(TPMU_HA)) {
  723. LOG_ERR("Unique data too big for TPM2_ALG_SYMCIPHER object's "
  724. "allowed unique size.");
  725. return tool_rc_general_error;
  726. }
  727. public_data->publicArea.unique.sym.size = unique_size;
  728. memcpy(&public_data->publicArea.unique.sym.buffer, file_data,
  729. unique_size);
  730. }
  731. return tool_rc_success;
  732. }