files.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #ifndef FILES_H
  3. #define FILES_H
  4. #include <stdbool.h>
  5. #include <stdio.h>
  6. #include <tss2/tss2_esys.h>
  7. #include "tool_rc.h"
  8. /**
  9. * Reads a series of bytes from a file as a byte array. This is similar to files_read_bytes(),
  10. * but opens and closes the FILE for the caller. Size is both an input and output value where
  11. * the size value is the max buffer size on call and the returned size is how much was read.
  12. *
  13. * This interface could be cleaned up in a later revision.
  14. * @param path
  15. * The path to the file to open.
  16. * @param buf
  17. * The buffer to read the data into
  18. * @param size
  19. * The max size of the buffer on call, and the size of the data read on return.
  20. * @return
  21. * True on success, false otherwise.
  22. */
  23. bool files_load_bytes_from_path(const char *path, UINT8 *buf, UINT16 *size);
  24. /**
  25. * Like files_load_bytes_from_path() but uses a FILE pointer.
  26. * @param f
  27. * The FILE pointer to read from.
  28. * @param buf
  29. * The buffer to store the data.
  30. * @param size
  31. * On input the max size of the buffer, on success the actual count of bytes read.
  32. * @param path
  33. * A possible path for error reporting, can be NULL to silence error reporting.
  34. * @return
  35. * True on success, false otherwise.
  36. */
  37. bool file_read_bytes_from_file(FILE *f, UINT8 *buf, UINT16 *size,
  38. const char *path);
  39. /**
  40. * Loads data from an input buffer or file path or stdin enforcing an upper bound on size.
  41. * @param input_buffer
  42. * The buffer to read the input data from, NULL means either specified by path or stdin
  43. * @param path
  44. * The path to load data from, NULL means stdin.
  45. * @param size
  46. * The maximum size.
  47. * @param buf
  48. * The buffer to write the data into.
  49. * @return
  50. * True on success or false otherwise.
  51. */
  52. bool files_load_bytes_from_buffer_or_file_or_stdin(const char *input_buffer,
  53. const char *path, UINT16 *size, BYTE *buf);
  54. /**
  55. * Similar to files_write_bytes(), in that it writes an array of bytes to disk,
  56. * but this routine opens and closes the file on the callers behalf. If the path
  57. * is NULL and silent output has not been enabled, then stdout is used.
  58. * @param path
  59. * The path to the file to write the data to.
  60. * @param buf
  61. * The buffer of data to write
  62. * @param size
  63. * The size of the data to write in bytes.
  64. * @return
  65. * True on success, false otherwise.
  66. */
  67. bool files_save_bytes_to_file(const char *path, UINT8 *buf, UINT16 size);
  68. /**
  69. * Saves the TPM ESAPI context for an object handle to disk by calling
  70. * ContextSave() and serializing the resulting TPMS_CONTEXT structure
  71. * to disk.
  72. * @param context
  73. * The Enhances System API (ESAPI) context
  74. * @param handle
  75. * The object handle for the object to save.
  76. * @param path
  77. * The output path of the file.
  78. *
  79. * @return
  80. * tool_rc indicating status.
  81. */
  82. tool_rc files_save_tpm_context_to_path(ESYS_CONTEXT *context, ESYS_TR handle,
  83. const char *path);
  84. /**
  85. * Like files_save_tpm_context_to_path() but saves a tpm session to a FILE stream.
  86. * @param context
  87. * The Enhances System API (ESAPI) context
  88. * @param handle
  89. * The object handle for the object to save.
  90. * @param stream
  91. * The FILE stream to save too.
  92. * @return
  93. * tool_rc indicating status.
  94. */
  95. tool_rc files_save_tpm_context_to_file(ESYS_CONTEXT *context, ESYS_TR handle,
  96. FILE *stream);
  97. /**
  98. * Loads a ESAPI TPM object context from disk or an ESAPI serialized ESYS_TR object.
  99. * @param context
  100. * The Enhanced System API (ESAPI) context
  101. * @param tr_handle
  102. * Optional. The Esys handle for the TPM2 object.
  103. * @param path
  104. * The path to the input file.
  105. * @return
  106. * tool_rc status indicating success.
  107. */
  108. tool_rc files_load_tpm_context_from_path(ESYS_CONTEXT *context,
  109. ESYS_TR *tr_handle, const char *path);
  110. /**
  111. * Like files_load_tpm_context_from_path() but loads the context from a FILE stream.
  112. * @param context
  113. * The Enhanced System API (ESAPI) context
  114. * @param tr_handle
  115. * Optional. The Esys handle for the TPM2 object
  116. * @param stream
  117. * The FILE stream to read from.
  118. * @return
  119. * tool_rc status indicating success.
  120. */
  121. tool_rc files_load_tpm_context_from_file(ESYS_CONTEXT *context,
  122. ESYS_TR *tr_handle, FILE *stream);
  123. /**
  124. * Save an ESYS_TR to disk.
  125. * @param ectx
  126. * The ESAPI context
  127. * @param handle
  128. * The handle to serialize.
  129. * @param path
  130. * The path to save to.
  131. * @return
  132. * A tool_rc indicating status.
  133. */
  134. tool_rc files_save_ESYS_TR(ESYS_CONTEXT *ectx, ESYS_TR handle, const char *path);
  135. /**
  136. * Serializes a TPM2B_PUBLIC to the file path provided.
  137. * @param public
  138. * The TPM2B_PUBLIC to save to disk.
  139. * @param path
  140. * The path to save to.
  141. * @return
  142. * true on success, false on error.
  143. */
  144. bool files_save_public(TPM2B_PUBLIC *public, const char *path);
  145. /**
  146. * Serializes a TPMT_PUBLIC to the file path provided.
  147. * @param template
  148. * The TPMT_PUBLIC to save to disk.
  149. * @param path
  150. * The path to save to.
  151. * @return
  152. * true on success, false on error.
  153. */
  154. bool files_save_template(TPMT_PUBLIC *template, const char *path);
  155. /**
  156. * Like files_load_template(), but doesn't report errors.
  157. * @param path
  158. * The path containing the TPMT_PUBLIC to load from.
  159. * @param public
  160. * The destination for the TPMT_PUBLIC.
  161. * @return
  162. * true on success, false otherwise.
  163. */
  164. bool files_load_template_silent(const char *path, TPMT_PUBLIC *public);
  165. /**
  166. * Loads a TPM2B_PUBLIC from disk that was saved with files_save_pubkey()
  167. * @param path
  168. * The path to load from.
  169. * @param public
  170. * The TPM2B_PUBLIC to load.
  171. * @return
  172. * true on success, false on error.
  173. */
  174. bool files_load_public(const char *path, TPM2B_PUBLIC *public);
  175. /**
  176. * Like files_load_public(), but doesn't report errors.
  177. * @param path
  178. * The path containing the TP2B_PUBLIC to load from.
  179. * @param public
  180. * The destination for the TP2B_PUBLIC.
  181. * @return
  182. * true on success, false otherwise.
  183. */
  184. bool files_load_public_file(FILE *f, const char *path, TPM2B_PUBLIC *public);
  185. bool files_load_template(const char *path, TPMT_PUBLIC *public);
  186. bool files_load_template_file(FILE *f, const char *path, TPMT_PUBLIC *public);
  187. /**
  188. * Like files_load_public(), but doesn't report errors.
  189. * @param path
  190. * The path containing the TP2B_PUBLIC to load from.
  191. * @param public
  192. * The destination for the TP2B_PUBLIC.
  193. * @return
  194. * true on success, false otherwise.
  195. */
  196. bool files_load_public_silent(const char *path, TPM2B_PUBLIC *public);
  197. /**
  198. * Serializes a TPMT_SIGNATURE to the file path provided.
  199. * @param signature
  200. * The TPMT_SIGNATURE to save to disk.
  201. * @param path
  202. * The path to save to.
  203. * @return
  204. * true on success, false on error.
  205. */
  206. bool files_save_signature(TPMT_SIGNATURE *signature, const char *path);
  207. /**
  208. * Loads a TPMT_SIGNATURE from disk that was saved with files_save_signature()
  209. * @param path
  210. * The path to load from.
  211. * @param signature
  212. * The TPMT_SIGNATURE to load.
  213. * @return
  214. * true on success, false on error.
  215. */
  216. bool files_load_signature(const char *path, TPMT_SIGNATURE *signature);
  217. /**
  218. * Like files_save)signature() but doesn't complain about libmu failures.
  219. * Useful if you're trying to probe if its a plain or tss format signature.
  220. * @param path
  221. * The path to load from.
  222. * @param signature
  223. * The TPMT_SIGNATURE to load.
  224. * @return
  225. * true on success, false on error.
  226. */
  227. bool files_load_signature_silent(const char *path, TPMT_SIGNATURE *signature);
  228. /**
  229. * Serializes a TPMT_TK_VERIFIED to the file path provided.
  230. * @param signature
  231. * The TPMT_SIGNATURE to save to disk.
  232. * @param path
  233. * The path to save to.
  234. * @return
  235. * true on success, false on error.
  236. */
  237. bool files_save_ticket(TPMT_TK_VERIFIED *ticket, const char *path);
  238. /**
  239. * Loads a TPMT_TK_VERIFIED from disk that was saved with files_save_ticket()
  240. * @param path
  241. * The path to load from.
  242. * @param signature
  243. * The TPMT_TK_VERIFIED to load.
  244. * @return
  245. * true on success, false on error.
  246. */
  247. bool files_load_ticket(const char *path, TPMT_TK_VERIFIED *ticket);
  248. /**
  249. * Serializes a TPMT_TK_AUTH to the file path provided.
  250. * @param signature
  251. * The TPMT_SIGNATURE to save to disk.
  252. * @param path
  253. * The path to save to.
  254. * @return
  255. * true on success, false on error.
  256. */
  257. bool files_save_authorization_ticket(TPMT_TK_AUTH *authorization_ticket,
  258. const char *path);
  259. /**
  260. * Loads a TPMT_TK_AUTH from disk that was saved with
  261. * files_save_authorization_ticket()
  262. * @param path
  263. * The path to load from.
  264. * @param signature
  265. * The TPMT_TK_AUTH to load.
  266. * @return
  267. * true on success, false on error.
  268. */
  269. bool files_load_authorization_ticket(const char *path,
  270. TPMT_TK_AUTH *authorization_ticket);
  271. bool files_load_creation_data(const char *path,
  272. TPM2B_CREATION_DATA *creation_data);
  273. bool files_save_creation_data(TPM2B_CREATION_DATA *creation_data,
  274. const char *path);
  275. bool files_load_creation_ticket(const char *path,
  276. TPMT_TK_CREATION *creation_ticket);
  277. bool files_save_creation_ticket(TPMT_TK_CREATION *creation_ticket,
  278. const char *path);
  279. bool files_load_digest(const char *path, TPM2B_DIGEST *digest);
  280. bool files_save_digest(TPM2B_DIGEST *digest, const char *path);
  281. /**
  282. * Loads a TPM2B_SENSITIVE from disk.
  283. * @param path
  284. * The path to load from.
  285. * @param signature
  286. * The TPM2B_SENSITIVE to load.
  287. * @return
  288. * true on success, false on error.
  289. */
  290. bool files_load_sensitive(const char *path, TPM2B_SENSITIVE *sensitive);
  291. /**
  292. * Serializes a TPM2B_SENSITIVE to the file path provided.
  293. * @param sensitive
  294. * The TPM2B_SENSITIVE to save to disk.
  295. * @param path
  296. * The path to save to.
  297. * @return
  298. * true on success, false on error.
  299. */
  300. bool files_save_sensitive(TPM2B_SENSITIVE *sensitive, const char *path);
  301. /**
  302. * Serializes a TPMT_TK_HASHCHECK to the file path provided.
  303. * @param validation
  304. * The TPMT_TK_HASHCHECK to save to disk.
  305. * @param path
  306. * The path to save to.
  307. * @return
  308. * true on success, false on error.
  309. */
  310. bool files_save_validation(TPMT_TK_HASHCHECK *validation, const char *path);
  311. /**
  312. * Loads a TPMT_TK_HASHCHECK from disk.
  313. * @param path
  314. * The path to load from.
  315. * @param validation
  316. * The TPMT_TK_HASHCHECK to load.
  317. * @return
  318. * true on success, false on error.
  319. */
  320. bool files_load_validation(const char *path, TPMT_TK_HASHCHECK *validation);
  321. /**
  322. * Serializes a TPM2B_PRIVATE to the file path provided.
  323. * @param private
  324. * The TPM2B_PRIVATE to save to disk.
  325. * @param path
  326. * The path to save to.
  327. * @return
  328. * true on success, false on error.
  329. */
  330. bool files_save_private(TPM2B_PRIVATE *private, const char *path);
  331. /**
  332. * Loads a TPM2B_PRIVATE from disk.
  333. * @param private
  334. * The path to load from.
  335. * @param validation
  336. * The TPM2B_PRIVATE to load.
  337. * @return
  338. * true on success, false on error.
  339. */
  340. bool files_load_private(const char *path, TPM2B_PRIVATE *private);
  341. /**
  342. * Serializes a TPM2B_ENCRYPTED_SECRET to the file path provided.
  343. * @param encrypted_seed
  344. * The TPM2B_ENCRYPTED_SECRET to save to disk.
  345. * @param path
  346. * The path to save to.
  347. * @return
  348. * true on success, false on error.
  349. */
  350. bool files_save_encrypted_seed(TPM2B_ENCRYPTED_SECRET *encrypted_seed,
  351. const char *path);
  352. /**
  353. * Serializes a TPM2B_ECC_POINT to the file path provided.
  354. * @param Q
  355. * The TPM2B_ECC_POINT to save to disk.
  356. * @param path
  357. * The path to save to.
  358. * @return
  359. * true on success, false on error.
  360. */
  361. bool files_save_ecc_point(TPM2B_ECC_POINT *Q, const char *path);
  362. /**
  363. * Loads a TPM2B_ECC_POINT from disk.
  364. * @param path
  365. * The path to load from.
  366. * @param Q
  367. * The TPM2B_ECC_POINT data to load.
  368. */
  369. bool files_load_ecc_point(const char *path, TPM2B_ECC_POINT *Q);
  370. /**
  371. * Loads a TPM2B_ECC_PARAMETER from disk
  372. * @param path
  373. * The path to load from.
  374. * @param parameter
  375. * The TPM2B_ECC_PARAMETER data to load.
  376. */
  377. bool files_load_ecc_parameter(const char *path, TPM2B_ECC_PARAMETER *parameter);
  378. /**
  379. * Loads a TPM2B_ENCRYPTED_SECRET from disk.
  380. * @param encrypted_seed
  381. * The path to load from.
  382. * @param validation
  383. * The TPM2B_ENCRYPTED_SECRET to load.
  384. * @return
  385. * true on success, false on error.
  386. */
  387. bool files_load_encrypted_seed(const char *path,
  388. TPM2B_ENCRYPTED_SECRET *encrypted_seed);
  389. /**
  390. * Serializes a TPMS_ALGORITHM_DETAIL_ECC to the file path provided.
  391. * @param parameters
  392. * The TPMS_ALGORITHM_DETAIL_ECC to save to disk.
  393. * @param path
  394. * The path to save to.
  395. * @return
  396. * true on success, false on error.
  397. */
  398. bool files_save_ecc_details(TPMS_ALGORITHM_DETAIL_ECC *parameters,
  399. const char *path);
  400. /**
  401. * Checks a file for existence.
  402. * @param path
  403. * The file to check for existence.
  404. * @return
  405. * true if a file exists with read permissions, false if it doesn't exist or an error occurs.
  406. *
  407. */
  408. bool files_does_file_exist(const char *path);
  409. /**
  410. * Retrieves a files size given a file path.
  411. * @param path
  412. * The path of the file to retrieve the size of.
  413. * @param file_size
  414. * A pointer to an unsigned long to return the file size. The
  415. * pointed to value is valid only on a true return.
  416. *
  417. * @return
  418. * True for success or False for error.
  419. */
  420. bool files_get_file_size_path(const char *path, unsigned long *file_size);
  421. /**
  422. * Similar to files_get_file_size_path(), but uses an already opened FILE object.
  423. * @param fp
  424. * The file pointer to query the size of.
  425. * @param file_size
  426. * Output of the file size.
  427. * @param path
  428. * An optional path used for error reporting, a NULL path disables error logging.
  429. * @return
  430. * True on success, False otherwise.
  431. */
  432. bool files_get_file_size(FILE *fp, unsigned long *file_size, const char *path);
  433. /**
  434. * Writes a TPM2.0 header to a file.
  435. * @param f
  436. * The file to write to.
  437. * @param version
  438. * The version number of the format of the file.
  439. * @return
  440. * True on success, false on error.
  441. */
  442. bool files_write_header(FILE *f, UINT32 version);
  443. /**
  444. * Reads a TPM2.0 header from a file.
  445. * @param f
  446. * The file to read.
  447. * @param version
  448. * The version that was found.
  449. * @return
  450. * True on Success, False on error.
  451. */
  452. bool files_read_header(FILE *f, UINT32 *version);
  453. /**
  454. * Writes a 16 bit value to the file in big endian, converting
  455. * if needed.
  456. * @param out
  457. * The file to write.
  458. * @param data
  459. * The 16 bit value to write.
  460. * @return
  461. * True on success, False on error.
  462. */
  463. bool files_write_16(FILE *out, UINT16 data);
  464. /**
  465. * Same as files_write_16 but for 32 bit values.
  466. */
  467. bool files_write_32(FILE *out, UINT32 data);
  468. /**
  469. * Same as files_write_16 but for 64 bit values.
  470. */
  471. bool files_write_64(FILE *out, UINT64 data);
  472. /**
  473. * Writes a byte array out to a file.
  474. * @param out
  475. * The file to write to.
  476. * @param data
  477. * The data to write.
  478. * @param size
  479. * The size of the data to write in bytes.
  480. * @return
  481. * True on success, False otherwise.
  482. */
  483. bool files_write_bytes(FILE *out, UINT8 data[], size_t size);
  484. /**
  485. * Reads a 16 bit value from a file converting from big endian to host
  486. * endianess.
  487. * @param out
  488. * The file to read from.
  489. * @param data
  490. * The data that is read, valid on a true return.
  491. * @return
  492. * True on success, False on error.
  493. */
  494. bool files_read_16(FILE *out, UINT16 *data);
  495. /**
  496. * Same as files_read_16 but for 32 bit values.
  497. */
  498. bool files_read_32(FILE *out, UINT32 *data);
  499. /**
  500. * Same as files_read_16 but for 64 bit values.
  501. */
  502. bool files_read_64(FILE *out, UINT64 *data);
  503. /**
  504. * Reads len bytes from a file.
  505. * @param out
  506. * The file to read from.
  507. * @param data
  508. * The buffer to read into, only valid on a True return.
  509. * @param size
  510. * The number of bytes to read.
  511. * @return
  512. * True on success, False otherwise.
  513. */
  514. bool files_read_bytes(FILE *out, UINT8 data[], size_t size);
  515. /**
  516. * Converts a TPM2B_ATTEST to a TPMS_ATTEST using libmu.
  517. * @param quoted
  518. * The attestation quote structure.
  519. * @param attest
  520. * The TPMS_ATTEST to populate.
  521. * @return
  522. * tool_rc_success on success, false otherwise.
  523. */
  524. tool_rc files_tpm2b_attest_to_tpms_attest(TPM2B_ATTEST *quoted, TPMS_ATTEST *attest);
  525. /**
  526. * Loads a TPMS_ATTEST from disk.
  527. * @param f
  528. * The file to load.
  529. * @param path
  530. * The path to load from.
  531. * @param attest
  532. * The attest structure to fill up.
  533. * @return
  534. * True on success, false otherwise.
  535. */
  536. bool files_load_attest_file(FILE *f, const char *path, TPMS_ATTEST *attest);
  537. /**
  538. * @brief
  539. * Parse the key type and load the unique data in the object's
  540. * TPM2B_PUBLIC area.
  541. *
  542. * @param file_path
  543. * The file to read the unique data from. This can be NULL to indicate stdin
  544. * @param public_data
  545. * The TPM2B public structure to parse the object type and also to update the
  546. * unique data as read from the file or stdin.
  547. *
  548. * @return
  549. * tool_rc type signaling the status at the end of read attempt.
  550. *
  551. */
  552. tool_rc files_load_unique_data(const char *file_path,
  553. TPM2B_PUBLIC *public_data);
  554. #endif /* FILES_H */