1 //**********************************************************************;
2 // Copyright (c) 2017, Intel Corporation
3 // All rights reserved.
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
8 // 1. Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
11 // 2. Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution.
15 // 3. Neither the name of Intel Corporation nor the names of its contributors
16 // may be used to endorse or promote products derived from this software without
17 // specific prior written permission.
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29 // THE POSSIBILITY OF SUCH DAMAGE.
30 //**********************************************************************;
37 #include <tss2/tss2_sys.h>
40 * Reads a series of bytes from a file as a byte array. This is similar to files_read_bytes(),
41 * but opens and closes the FILE for the caller. Size is both an input and output value where
42 * the size value is the max buffer size on call and the returned size is how much was read.
44 * This interface could be cleaned up in a later revision.
46 * The path to the file to open.
48 * The buffer to read the data into
50 * The max size of the buffer on call, and the size of the data read on return.
52 * True on success, false otherwise.
54 bool files_load_bytes_from_path(const char *path, UINT8 *buf, UINT16 *size);
57 * Loads data from a file path or stdin enforcing an upper bound on size.
59 * The path to load data from, NULL means stdin.
63 * The buffer to write the data into.
65 * True on success or false otherwise.
67 bool files_load_bytes_from_file_or_stdin(const char *path, UINT16 *size, BYTE *buf);
70 * Similar to files_write_bytes(), in that it writes an array of bytes to disk,
71 * but this routine opens and closes the file on the callers behalf.
73 * The path to the file to write the data to.
75 * The buffer of data to write
77 * The size of the data to write in bytes.
79 * True on success, false otherwise.
81 bool files_save_bytes_to_file(const char *path, UINT8 *buf, UINT16 size);
84 * Saves the TPM context for an object handle to disk by calling Tss2_Sys_ContextSave() and serializing the
85 * resulting TPMS_CONTEXT structure to disk.
87 * The system api context
89 * The object handle for the object to save.
91 * The output path of the file.
94 * True on success, False on error.
96 bool files_save_tpm_context_to_path(TSS2_SYS_CONTEXT *sapi_context, TPM2_HANDLE handle, const char *path);
99 * Like files_save_tpm_context_to_path() but saves a tpm session to a FILE stream.
100 * @param sapi_context
101 * The system api context
103 * The object handle for the object to save.
105 * The FILE stream to save too.
107 * True on success, False on error.
109 bool files_save_tpm_context_to_file(TSS2_SYS_CONTEXT *sapi_context, TPM2_HANDLE handle,
113 * Loads a TPM object context from disk.
114 * @param sapi_context
115 * The system API context
117 * The object handle that was saved.
119 * The path to the input file.
121 * True on Success, false on error.
123 bool files_load_tpm_context_from_path(TSS2_SYS_CONTEXT *sapi_context, TPM2_HANDLE *handle, const char *path);
126 * Like files_load_tpm_context_from_path() but loads the context from a FILE stream.
127 * @param sapi_context
128 * The system API context
130 * The object handle that was saved.
132 * The FILE stream to read from.
134 * True on success, False on error.
136 bool files_load_tpm_context_from_file(TSS2_SYS_CONTEXT *sapi_context,
137 TPM2_HANDLE *handle, FILE *stream);
140 * Serializes a TPM2B_PUBLIC to the file path provided.
142 * The TPM2B_PUBLIC to save to disk.
144 * The path to save to.
146 * true on success, false on error.
148 bool files_save_public(TPM2B_PUBLIC *public, const char *path);
151 * Loads a TPM2B_PUBLIC from disk that was saved with files_save_pubkey()
153 * The path to load from.
155 * The TPM2B_PUBLIC to load.
157 * true on success, false on error.
159 bool files_load_public(const char *path, TPM2B_PUBLIC *public);
162 * Serializes a TPMT_SIGNATURE to the file path provided.
164 * The TPMT_SIGNATURE to save to disk.
166 * The path to save to.
168 * true on success, false on error.
170 bool files_save_signature(TPMT_SIGNATURE *signature, const char *path);
173 * Loads a TPMT_SIGNATURE from disk that was saved with files_save_signature()
175 * The path to load from.
177 * The TPMT_SIGNATURE to load.
179 * true on success, false on error.
181 bool files_load_signature(const char *path, TPMT_SIGNATURE *signature);
184 * Serializes a TPMT_TK_VERIFIED to the file path provided.
186 * The TPMT_SIGNATURE to save to disk.
188 * The path to save to.
190 * true on success, false on error.
192 bool files_save_ticket(TPMT_TK_VERIFIED *ticket, const char *path);
195 * Loads a TPMT_TK_VERIFIED from disk that was saved with files_save_ticket()
197 * The path to load from.
199 * The TPMT_TK_VERIFIED to load.
201 * true on success, false on error.
203 bool files_load_ticket(const char *path, TPMT_TK_VERIFIED *ticket);
206 * Loads a TPM2B_SENSITIVE from disk.
208 * The path to load from.
210 * The TPM2B_SENSITIVE to load.
212 * true on success, false on error.
214 bool files_load_sensitive(const char *path, TPM2B_SENSITIVE *sensitive);
217 * Serializes a TPM2B_SENSITIVE to the file path provided.
219 * The TPM2B_SENSITIVE to save to disk.
221 * The path to save to.
223 * true on success, false on error.
225 bool files_save_sensitive(TPM2B_SENSITIVE *sensitive, const char *path);
227 * Serializes a TPMT_TK_HASHCHECK to the file path provided.
229 * The TPMT_TK_HASHCHECK to save to disk.
231 * The path to save to.
233 * true on success, false on error.
235 bool files_save_validation(TPMT_TK_HASHCHECK *validation, const char *path);
238 * Loads a TPMT_TK_HASHCHECK from disk.
240 * The path to load from.
242 * The TPMT_TK_HASHCHECK to load.
244 * true on success, false on error.
246 bool files_load_validation(const char *path, TPMT_TK_HASHCHECK *validation);
249 * Serializes a TPM2B_PRIVATE to the file path provided.
251 * The TPM2B_PRIVATE to save to disk.
253 * The path to save to.
255 * true on success, false on error.
257 bool files_save_private(TPM2B_PRIVATE *private, const char *path);
260 * Loads a TPM2B_PRIVATE from disk.
262 * The path to load from.
264 * The TPM2B_PRIVATE to load.
266 * true on success, false on error.
268 bool files_load_private(const char *path, TPM2B_PRIVATE *private);
271 * Checks a file for existence.
273 * The file to check for existence.
275 * true if a file exists with read permissions, false if it doesn't exist or an error occurs.
278 bool files_does_file_exist(const char *path);
281 * Retrieves a files size given a file path.
283 * The path of the file to retreive the size of.
285 * A pointer to an unsigned long to return the file size. The
286 * pointed to value is valid only on a true return.
289 * True for success or False for error.
291 bool files_get_file_size_path(const char *path, unsigned long *file_size);
294 * Similar to files_get_file_size_path(), but uses an already opened FILE object.
296 * The file pointer to query the size of.
298 * Output of the file size.
300 * An optional path used for error reporting, a NULL path disables error logging.
302 * True on success, False otherwise.
304 bool files_get_file_size(FILE *fp, unsigned long *file_size, const char *path);
307 * Writes a TPM2.0 header to a file.
309 * The file to write to.
311 * The version number of the format of the file.
313 * True on success, false on error.
315 bool files_write_header(FILE *f, UINT32 version);
318 * Reads a TPM2.0 header from a file.
322 * The version that was found.
324 * True on Success, False on error.
326 bool files_read_header(FILE *f, UINT32 *version);
329 * Writes a 16 bit value to the file in big endian, converting
334 * The 16 bit value to write.
336 * True on success, False on error.
338 bool files_write_16(FILE *out, UINT16 data);
341 * Same as files_write_16 but for 32 bit values.
343 bool files_write_32(FILE *out, UINT32 data);
346 * Same as files_write_16 but for 64 bit values.
348 bool files_write_64(FILE *out, UINT64 data);
351 * Writes a byte array out to a file.
353 * The file to write to.
357 * The size of the data to write in bytes.
359 * True on success, False otherwise.
361 bool files_write_bytes(FILE *out, UINT8 data[], size_t size);
364 * Reads a 16 bit value from a file converting from big endian to host
367 * The file to read from.
369 * The data that is read, valid on a true return.
371 * True on success, False on error.
373 bool files_read_16(FILE *out, UINT16 *data);
376 * Same as files_read_16 but for 32 bit values.
378 bool files_read_32(FILE *out, UINT32 *data);
381 * Same as files_read_16 but for 64 bit values.
383 bool files_read_64(FILE *out, UINT64 *data);
386 * Reads len bytes from a file.
388 * The file to read from.
390 * The buffer to read into, only valid on a True return.
392 * The number of bytes to read.
394 * True on success, False otherwise.
396 bool files_read_bytes(FILE *out, UINT8 data[], size_t size);