NingSun | 535535b | 2018-02-28 18:24:31 -0800 | [diff] [blame^] | 1 | //**********************************************************************; |
| 2 | // Copyright (c) 2017, Intel Corporation |
| 3 | // All rights reserved. |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are met: |
| 7 | // |
| 8 | // 1. Redistributions of source code must retain the above copyright notice, |
| 9 | // this list of conditions and the following disclaimer. |
| 10 | // |
| 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. |
| 14 | // |
| 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. |
| 18 | // |
| 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 | //**********************************************************************; |
| 31 | #ifndef FILES_H |
| 32 | #define FILES_H |
| 33 | |
| 34 | #include <stdbool.h> |
| 35 | #include <stdio.h> |
| 36 | |
| 37 | #include <sapi/tpm20.h> |
| 38 | |
| 39 | /** |
| 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. |
| 43 | * |
| 44 | * This interface could be cleaned up in a later revision. |
| 45 | * @param path |
| 46 | * The path to the file to open. |
| 47 | * @param buf |
| 48 | * The buffer to read the data into |
| 49 | * @param size |
| 50 | * The max size of the buffer on call, and the size of the data read on return. |
| 51 | * @return |
| 52 | * True on success, false otherwise. |
| 53 | */ |
| 54 | bool files_load_bytes_from_path(const char *path, UINT8 *buf, UINT16 *size); |
| 55 | |
| 56 | /** |
| 57 | * Loads data from a file path or stdin enforcing an upper bound on size. |
| 58 | * @param path |
| 59 | * The path to load data from, NULL means stdin. |
| 60 | * @param size |
| 61 | * The maximum size. |
| 62 | * @param buf |
| 63 | * The buffer to write the data into. |
| 64 | * @return |
| 65 | * True on success or false otherwise. |
| 66 | */ |
| 67 | bool files_load_bytes_from_file_or_stdin(const char *path, UINT16 *size, BYTE *buf); |
| 68 | |
| 69 | /** |
| 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. |
| 72 | * @param path |
| 73 | * The path to the file to write the data to. |
| 74 | * @param buf |
| 75 | * The buffer of data to write |
| 76 | * @param size |
| 77 | * The size of the data to write in bytes. |
| 78 | * @return |
| 79 | * True on success, false otherwise. |
| 80 | */ |
| 81 | bool files_save_bytes_to_file(const char *path, UINT8 *buf, UINT16 size); |
| 82 | |
| 83 | /** |
| 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. |
| 86 | * @param sapi_context |
| 87 | * The system api context |
| 88 | * @param handle |
| 89 | * The object handle for the object to save. |
| 90 | * @param path |
| 91 | * The output path of the file. |
| 92 | * |
| 93 | * @return |
| 94 | * True on success, False on error. |
| 95 | */ |
| 96 | bool files_save_tpm_context_to_path(TSS2_SYS_CONTEXT *sapi_context, TPM2_HANDLE handle, const char *path); |
| 97 | |
| 98 | /** |
| 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 |
| 102 | * @param handle |
| 103 | * The object handle for the object to save. |
| 104 | * @param stream |
| 105 | * The FILE stream to save too. |
| 106 | * @return |
| 107 | * True on success, False on error. |
| 108 | */ |
| 109 | bool files_save_tpm_context_to_file(TSS2_SYS_CONTEXT *sapi_context, TPM2_HANDLE handle, |
| 110 | FILE *stream); |
| 111 | |
| 112 | /** |
| 113 | * Loads a TPM object context from disk. |
| 114 | * @param sapi_context |
| 115 | * The system API context |
| 116 | * @param handle |
| 117 | * The object handle that was saved. |
| 118 | * @param path |
| 119 | * The path to the input file. |
| 120 | * @return |
| 121 | * True on Success, false on error. |
| 122 | */ |
| 123 | bool files_load_tpm_context_from_path(TSS2_SYS_CONTEXT *sapi_context, TPM2_HANDLE *handle, const char *path); |
| 124 | |
| 125 | /** |
| 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 |
| 129 | * @param handle |
| 130 | * The object handle that was saved. |
| 131 | * @param stream |
| 132 | * The FILE stream to read from. |
| 133 | * @return |
| 134 | * True on success, False on error. |
| 135 | */ |
| 136 | bool files_load_tpm_context_from_file(TSS2_SYS_CONTEXT *sapi_context, |
| 137 | TPM2_HANDLE *handle, FILE *stream); |
| 138 | |
| 139 | /** |
| 140 | * Serializes a TPM2B_PUBLIC to the file path provided. |
| 141 | * @param public |
| 142 | * The TPM2B_PUBLIC to save to disk. |
| 143 | * @param path |
| 144 | * The path to save to. |
| 145 | * @return |
| 146 | * true on success, false on error. |
| 147 | */ |
| 148 | bool files_save_public(TPM2B_PUBLIC *public, const char *path); |
| 149 | |
| 150 | /** |
| 151 | * Loads a TPM2B_PUBLIC from disk that was saved with files_save_pubkey() |
| 152 | * @param path |
| 153 | * The path to load from. |
| 154 | * @param public |
| 155 | * The TPM2B_PUBLIC to load. |
| 156 | * @return |
| 157 | * true on success, false on error. |
| 158 | */ |
| 159 | bool files_load_public(const char *path, TPM2B_PUBLIC *public); |
| 160 | |
| 161 | /** |
| 162 | * Serializes a TPMT_SIGNATURE to the file path provided. |
| 163 | * @param signature |
| 164 | * The TPMT_SIGNATURE to save to disk. |
| 165 | * @param path |
| 166 | * The path to save to. |
| 167 | * @return |
| 168 | * true on success, false on error. |
| 169 | */ |
| 170 | bool files_save_signature(TPMT_SIGNATURE *signature, const char *path); |
| 171 | |
| 172 | /** |
| 173 | * Loads a TPMT_SIGNATURE from disk that was saved with files_save_signature() |
| 174 | * @param path |
| 175 | * The path to load from. |
| 176 | * @param signature |
| 177 | * The TPMT_SIGNATURE to load. |
| 178 | * @return |
| 179 | * true on success, false on error. |
| 180 | */ |
| 181 | bool files_load_signature(const char *path, TPMT_SIGNATURE *signature); |
| 182 | |
| 183 | /** |
| 184 | * Serializes a TPMT_TK_VERIFIED to the file path provided. |
| 185 | * @param signature |
| 186 | * The TPMT_SIGNATURE to save to disk. |
| 187 | * @param path |
| 188 | * The path to save to. |
| 189 | * @return |
| 190 | * true on success, false on error. |
| 191 | */ |
| 192 | bool files_save_ticket(TPMT_TK_VERIFIED *ticket, const char *path); |
| 193 | |
| 194 | /** |
| 195 | * Loads a TPMT_TK_VERIFIED from disk that was saved with files_save_ticket() |
| 196 | * @param path |
| 197 | * The path to load from. |
| 198 | * @param signature |
| 199 | * The TPMT_TK_VERIFIED to load. |
| 200 | * @return |
| 201 | * true on success, false on error. |
| 202 | */ |
| 203 | bool files_load_ticket(const char *path, TPMT_TK_VERIFIED *ticket); |
| 204 | |
| 205 | /** |
| 206 | * Loads a TPM2B_SENSITIVE from disk. |
| 207 | * @param path |
| 208 | * The path to load from. |
| 209 | * @param signature |
| 210 | * The TPM2B_SENSITIVE to load. |
| 211 | * @return |
| 212 | * true on success, false on error. |
| 213 | */ |
| 214 | bool files_load_sensitive(const char *path, TPM2B_SENSITIVE *sensitive); |
| 215 | |
| 216 | /** |
| 217 | * Serializes a TPMT_TK_HASHCHECK to the file path provided. |
| 218 | * @param validation |
| 219 | * The TPMT_TK_HASHCHECK to save to disk. |
| 220 | * @param path |
| 221 | * The path to save to. |
| 222 | * @return |
| 223 | * true on success, false on error. |
| 224 | */ |
| 225 | bool files_save_validation(TPMT_TK_HASHCHECK *validation, const char *path); |
| 226 | |
| 227 | /** |
| 228 | * Loads a TPMT_TK_HASHCHECK from disk. |
| 229 | * @param path |
| 230 | * The path to load from. |
| 231 | * @param validation |
| 232 | * The TPMT_TK_HASHCHECK to load. |
| 233 | * @return |
| 234 | * true on success, false on error. |
| 235 | */ |
| 236 | bool files_load_validation(const char *path, TPMT_TK_HASHCHECK *validation); |
| 237 | |
| 238 | /** |
| 239 | * Checks a file for existence. |
| 240 | * @param path |
| 241 | * The file to check for existence. |
| 242 | * @return |
| 243 | * true if a file exists with read permissions, false if it doesn't exist or an error occurs. |
| 244 | * |
| 245 | */ |
| 246 | bool files_does_file_exist(const char *path); |
| 247 | |
| 248 | /** |
| 249 | * Retrieves a files size given a file path. |
| 250 | * @param path |
| 251 | * The path of the file to retreive the size of. |
| 252 | * @param file_size |
| 253 | * A pointer to an unsigned long to return the file size. The |
| 254 | * pointed to value is valid only on a true return. |
| 255 | * |
| 256 | * @return |
| 257 | * True for success or False for error. |
| 258 | */ |
| 259 | bool files_get_file_size_path(const char *path, unsigned long *file_size); |
| 260 | |
| 261 | /** |
| 262 | * Similar to files_get_file_size_path(), but uses an already opened FILE object. |
| 263 | * @param fp |
| 264 | * The file pointer to query the size of. |
| 265 | * @param file_size |
| 266 | * Output of the file size. |
| 267 | * @param path |
| 268 | * An optional path used for error reporting, a NULL path disables error logging. |
| 269 | * @return |
| 270 | * True on success, False otherwise. |
| 271 | */ |
| 272 | bool files_get_file_size(FILE *fp, unsigned long *file_size, const char *path); |
| 273 | |
| 274 | /** |
| 275 | * Writes a TPM2.0 header to a file. |
| 276 | * @param f |
| 277 | * The file to write to. |
| 278 | * @param version |
| 279 | * The version number of the format of the file. |
| 280 | * @return |
| 281 | * True on success, false on error. |
| 282 | */ |
| 283 | bool files_write_header(FILE *f, UINT32 version); |
| 284 | |
| 285 | /** |
| 286 | * Reads a TPM2.0 header from a file. |
| 287 | * @param f |
| 288 | * The file to read. |
| 289 | * @param version |
| 290 | * The version that was found. |
| 291 | * @return |
| 292 | * True on Success, False on error. |
| 293 | */ |
| 294 | bool files_read_header(FILE *f, UINT32 *version); |
| 295 | |
| 296 | /** |
| 297 | * Writes a 16 bit value to the file in big endian, converting |
| 298 | * if needed. |
| 299 | * @param out |
| 300 | * The file to write. |
| 301 | * @param data |
| 302 | * The 16 bit value to write. |
| 303 | * @return |
| 304 | * True on success, False on error. |
| 305 | */ |
| 306 | bool files_write_16(FILE *out, UINT16 data); |
| 307 | |
| 308 | /** |
| 309 | * Same as files_write_16 but for 32 bit values. |
| 310 | */ |
| 311 | bool files_write_32(FILE *out, UINT32 data); |
| 312 | |
| 313 | /** |
| 314 | * Same as files_write_16 but for 64 bit values. |
| 315 | */ |
| 316 | bool files_write_64(FILE *out, UINT64 data); |
| 317 | |
| 318 | /** |
| 319 | * Writes a byte array out to a file. |
| 320 | * @param out |
| 321 | * The file to write to. |
| 322 | * @param data |
| 323 | * The data to write. |
| 324 | * @param size |
| 325 | * The size of the data to write in bytes. |
| 326 | * @return |
| 327 | * True on success, False otherwise. |
| 328 | */ |
| 329 | bool files_write_bytes(FILE *out, UINT8 data[], size_t size); |
| 330 | |
| 331 | /** |
| 332 | * Reads a 16 bit value from a file converting from big endian to host |
| 333 | * endianess. |
| 334 | * @param out |
| 335 | * The file to read from. |
| 336 | * @param data |
| 337 | * The data that is read, valid on a true return. |
| 338 | * @return |
| 339 | * True on success, False on error. |
| 340 | */ |
| 341 | bool files_read_16(FILE *out, UINT16 *data); |
| 342 | |
| 343 | /** |
| 344 | * Same as files_read_16 but for 32 bit values. |
| 345 | */ |
| 346 | bool files_read_32(FILE *out, UINT32 *data); |
| 347 | |
| 348 | /** |
| 349 | * Same as files_read_16 but for 64 bit values. |
| 350 | */ |
| 351 | bool files_read_64(FILE *out, UINT64 *data); |
| 352 | |
| 353 | /** |
| 354 | * Reads len bytes from a file. |
| 355 | * @param out |
| 356 | * The file to read from. |
| 357 | * @param data |
| 358 | * The buffer to read into, only valid on a True return. |
| 359 | * @param size |
| 360 | * The number of bytes to read. |
| 361 | * @return |
| 362 | * True on success, False otherwise. |
| 363 | */ |
| 364 | bool files_read_bytes(FILE *out, UINT8 data[], size_t size); |
| 365 | |
| 366 | #endif /* FILES_H */ |