sgx_tprotected_fs.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Copyright (C) 2011-2018 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #include "sgx_tprotected_fs.h"
  32. #include "sgx_tprotected_fs_t.h"
  33. #include "protected_fs_file.h"
  34. #include <errno.h>
  35. static SGX_FILE* sgx_fopen_internal(const char* filename, const char* mode, const sgx_key_128bit_t *auto_key, const sgx_key_128bit_t *kdk_key)
  36. {
  37. protected_fs_file* file = NULL;
  38. if (filename == NULL || mode == NULL)
  39. {
  40. errno = EINVAL;
  41. return NULL;
  42. }
  43. try {
  44. file = new protected_fs_file(filename, mode, auto_key, kdk_key);
  45. }
  46. catch (std::bad_alloc& e) {
  47. (void)e; // remove warning
  48. errno = ENOMEM;
  49. return NULL;
  50. }
  51. if (file->get_error() != SGX_FILE_STATUS_OK)
  52. {
  53. errno = file->get_error();
  54. delete file;
  55. file = NULL;
  56. }
  57. return (SGX_FILE*)file;
  58. }
  59. SGX_FILE* sgx_fopen_auto_key(const char* filename, const char* mode)
  60. {
  61. return sgx_fopen_internal(filename, mode, NULL, NULL);
  62. }
  63. SGX_FILE* sgx_fopen(const char* filename, const char* mode, const sgx_key_128bit_t *key)
  64. {
  65. return sgx_fopen_internal(filename, mode, NULL, key);
  66. }
  67. size_t sgx_fwrite(const void* ptr, size_t size, size_t count, SGX_FILE* stream)
  68. {
  69. if (ptr == NULL || stream == NULL || size == 0 || count == 0)
  70. return 0;
  71. protected_fs_file* file = (protected_fs_file*)stream;
  72. return file->write(ptr, size, count);
  73. }
  74. size_t sgx_fread(void* ptr, size_t size, size_t count, SGX_FILE* stream)
  75. {
  76. if (ptr == NULL || stream == NULL || size == 0 || count == 0)
  77. return 0;
  78. protected_fs_file* file = (protected_fs_file*)stream;
  79. return file->read(ptr, size, count);
  80. }
  81. int64_t sgx_ftell(SGX_FILE* stream)
  82. {
  83. if (stream == NULL)
  84. return -1;
  85. protected_fs_file* file = (protected_fs_file*)stream;
  86. return file->tell();
  87. }
  88. int32_t sgx_fseek(SGX_FILE* stream, int64_t offset, int origin)
  89. {
  90. if (stream == NULL)
  91. return -1;
  92. protected_fs_file* file = (protected_fs_file*)stream;
  93. return file->seek(offset, origin);
  94. }
  95. int32_t sgx_fflush(SGX_FILE* stream)
  96. {
  97. if (stream == NULL)
  98. return EOPNOTSUPP; // TBD - currently we don't support NULL as fflush input parameter
  99. protected_fs_file* file = (protected_fs_file*)stream;
  100. return file->flush(/*false*/) == true ? 0 : EOF;
  101. }
  102. /* sgx_fflush_and_increment_mc
  103. * Purpose: force actual write of all the cached data to the disk (see c++ fflush documentation for more details).
  104. * in addition, in the first time this function is called, it adds a monotonic counter to the file
  105. * in subsequent calls, the monotonic counter is incremented by one every time this function is called
  106. * the monotonic counter is a limited resource, please read the SGX documentation for more details
  107. *
  108. * Parameters:
  109. * stream - [IN] the file handle (opened with sgx_fopen or sgx_fopen_auto_key)
  110. *
  111. * Return value:
  112. * int32_t - result, 0 on success, 1 in case of an error - check sgx_ferror for error code
  113. *
  114. int32_t sgx_fflush_and_increment_mc(SGX_FILE* stream)
  115. {
  116. if (stream == NULL)
  117. return 1;
  118. protected_fs_file* file = (protected_fs_file*)stream;
  119. return file->flush(true) == true ? 0 : 1;
  120. }
  121. */
  122. int32_t sgx_ferror(SGX_FILE* stream)
  123. {
  124. if (stream == NULL)
  125. return -1;
  126. protected_fs_file* file = (protected_fs_file*)stream;
  127. return file->get_error();
  128. }
  129. int32_t sgx_feof(SGX_FILE* stream)
  130. {
  131. if (stream == NULL)
  132. return -1;
  133. protected_fs_file* file = (protected_fs_file*)stream;
  134. return ((file->get_eof() == true) ? 1 : 0);
  135. }
  136. void sgx_clearerr(SGX_FILE* stream)
  137. {
  138. if (stream == NULL)
  139. return;
  140. protected_fs_file* file = (protected_fs_file*)stream;
  141. file->clear_error();
  142. }
  143. static int32_t sgx_fclose_internal(SGX_FILE* stream, sgx_key_128bit_t *key, bool import)
  144. {
  145. int32_t retval = 0;
  146. if (stream == NULL)
  147. return EOF;
  148. protected_fs_file* file = (protected_fs_file*)stream;
  149. if (file->pre_close(key, import) == false)
  150. retval = 1;
  151. delete file;
  152. return retval;
  153. }
  154. int32_t sgx_fclose(SGX_FILE* stream)
  155. {
  156. return sgx_fclose_internal(stream, NULL, false);
  157. }
  158. int32_t sgx_remove(const char* filename)
  159. {
  160. return protected_fs_file::remove(filename);
  161. }
  162. int32_t sgx_fexport_auto_key(const char* filename, sgx_key_128bit_t *key)
  163. {
  164. SGX_FILE* stream = sgx_fopen_internal(filename, "r", NULL, NULL);
  165. if (stream == NULL)
  166. return 1;
  167. return sgx_fclose_internal(stream, key, false);
  168. }
  169. int32_t sgx_fimport_auto_key(const char* filename, const sgx_key_128bit_t *key)
  170. {
  171. SGX_FILE* stream = sgx_fopen_internal(filename, "r+", key, NULL);
  172. if (stream == NULL)
  173. return 1;
  174. return sgx_fclose_internal(stream, NULL, true);
  175. }
  176. int32_t sgx_fclear_cache(SGX_FILE* stream)
  177. {
  178. if (stream == NULL)
  179. return 1;
  180. protected_fs_file* file = (protected_fs_file*)stream;
  181. return file->clear_cache();
  182. }