platform_service_sim.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright (C) 2011-2017 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 "../uae_service_sim.h"
  32. #define SE_DATA_FOLDER1 "intel/"
  33. #define SE_DATA_FOLDER2 "intelsgxpsw/"
  34. #define MAX_PATH 260
  35. #include <sys/stat.h>
  36. #include <sys/types.h>
  37. #include <stdlib.h>
  38. #include <pwd.h>
  39. #define __STDC_FORMAT_MACROS
  40. #include <inttypes.h>
  41. #include "ae_ipp.h"
  42. static Mutex g_pse_sim_lock;
  43. __attribute__((constructor))
  44. // Initializer of the uae_service_sim.
  45. static void init_ipp(void)
  46. {
  47. #ifdef SGX_USE_OPT_LIB
  48. // The return value of ippInit is discarded.
  49. ippInit();
  50. #endif
  51. }
  52. static char g_vmc_base_path[] = "/var/tmp/";
  53. sgx_status_t get_counter_id(vmc_sim_t *p_vmc_sim)
  54. {
  55. uint32_t i = 0;
  56. int random_int = 0;
  57. unsigned int a, d;
  58. asm volatile("rdtsc" : "=a" (a), "=d" (d));
  59. for(i = 0; i < sizeof(p_vmc_sim->counter_id); i++, random_int >>= 8)
  60. {
  61. if(!(i % 4))
  62. {
  63. random_int = rand_r(&a);
  64. }
  65. p_vmc_sim->counter_id[i] = (uint8_t)(random_int & 0xFF);
  66. }
  67. for(i = 0; i < sizeof(p_vmc_sim->nonce); i++, random_int >>= 8)
  68. {
  69. if(!(i % 4))
  70. {
  71. random_int = rand_r(&a);
  72. }
  73. p_vmc_sim->nonce[i] = (uint8_t)(random_int & 0xFF);
  74. }
  75. return SGX_SUCCESS;
  76. }
  77. sgx_status_t del_vmc_sim(const vmc_sim_t *p_vmc_sim)
  78. {
  79. char path[MAX_PATH] = {0};
  80. int ret = 0;
  81. uint64_t temp_value = 0;
  82. memcpy_s(&temp_value, sizeof(temp_value),
  83. p_vmc_sim->nonce, sizeof(temp_value));
  84. char *p_buf = g_vmc_base_path;
  85. ret = snprintf(path, sizeof(path), "%s%s%s%" PRIx64".dat",
  86. p_buf, SE_DATA_FOLDER1, SE_DATA_FOLDER2, temp_value);
  87. if(-1 == ret){
  88. return SGX_ERROR_UNEXPECTED;
  89. }
  90. if(remove(path)){
  91. return SGX_ERROR_MC_NOT_FOUND;
  92. }
  93. return SGX_SUCCESS;
  94. }
  95. sgx_status_t store_vmc_sim(const vmc_sim_t *p_vmc_sim)
  96. {
  97. char path[MAX_PATH] = {0};
  98. int ret = 0;
  99. uint64_t temp_value = 0;
  100. memcpy_s(&temp_value, sizeof(temp_value),
  101. p_vmc_sim->nonce, sizeof(temp_value));
  102. char *p_buf = g_vmc_base_path;
  103. ret = snprintf(path, sizeof(path), "%s%s",
  104. p_buf,
  105. SE_DATA_FOLDER1);
  106. if(-1 == ret){
  107. return SGX_ERROR_UNEXPECTED;
  108. }
  109. g_pse_sim_lock.lock();
  110. ret = mkdir(path, S_IRUSR|S_IWUSR|S_IXUSR);
  111. if(0 == ret){
  112. ret = chmod(path, S_IRUSR|S_IWUSR|S_IXUSR
  113. |S_IRGRP|S_IWGRP|S_IXGRP
  114. |S_IROTH|S_IWOTH|S_IXOTH);
  115. if(0 != ret){
  116. g_pse_sim_lock.unlock();
  117. return SGX_ERROR_UNEXPECTED;
  118. }
  119. }else if(EEXIST != errno){
  120. g_pse_sim_lock.unlock();
  121. return SGX_ERROR_UNEXPECTED;
  122. }
  123. g_pse_sim_lock.unlock();
  124. ret = snprintf(path, sizeof(path), "%s%s%s",
  125. p_buf,
  126. SE_DATA_FOLDER1,
  127. SE_DATA_FOLDER2);
  128. if(-1 == ret){
  129. return SGX_ERROR_UNEXPECTED;
  130. }
  131. g_pse_sim_lock.lock();
  132. ret = mkdir(path, S_IRUSR|S_IWUSR|S_IXUSR);
  133. if(0 == ret){
  134. ret = chmod(path, S_IRUSR|S_IWUSR|S_IXUSR
  135. |S_IRGRP|S_IWGRP|S_IXGRP
  136. |S_IROTH|S_IWOTH|S_IXOTH);
  137. if(0 != ret){
  138. g_pse_sim_lock.unlock();
  139. return SGX_ERROR_UNEXPECTED;
  140. }
  141. }else if(EEXIST != errno){
  142. g_pse_sim_lock.unlock();
  143. return SGX_ERROR_UNEXPECTED;
  144. }
  145. g_pse_sim_lock.unlock();
  146. ret = snprintf(path, sizeof(path), "%s%s%s%" PRIx64".dat",
  147. p_buf, SE_DATA_FOLDER1, SE_DATA_FOLDER2, temp_value);
  148. if(-1 == ret){
  149. return SGX_ERROR_UNEXPECTED;
  150. }
  151. FILE *fp = NULL;
  152. fp = fopen(path, "wb");
  153. if(!fp){
  154. return SGX_ERROR_MC_NOT_FOUND;
  155. }
  156. size_t wt_count = 0;
  157. wt_count = fwrite(p_vmc_sim, sizeof(vmc_sim_t), 1, fp);
  158. if(!wt_count){
  159. fclose(fp);
  160. return SGX_ERROR_UNEXPECTED;
  161. }
  162. fclose(fp);
  163. return SGX_SUCCESS;
  164. }
  165. sgx_status_t load_vmc_sim(vmc_sim_t *p_vmc_sim)
  166. {
  167. char path[MAX_PATH] = {0};
  168. int ret = 0;
  169. uint64_t temp_value = 0;
  170. memcpy_s(&temp_value, sizeof(temp_value),
  171. p_vmc_sim->nonce, sizeof(temp_value));
  172. char *p_buf = g_vmc_base_path;
  173. ret = snprintf(path, sizeof(path), "%s%s%s%" PRIx64".dat",
  174. p_buf, SE_DATA_FOLDER1, SE_DATA_FOLDER2, temp_value);
  175. if(-1 == ret){
  176. return SGX_ERROR_UNEXPECTED;
  177. }
  178. FILE *fp = NULL;
  179. fp = fopen(path, "rb");
  180. if(!fp){
  181. return SGX_ERROR_MC_NOT_FOUND;
  182. }
  183. size_t rd_count = 0;
  184. rd_count = fread(p_vmc_sim, sizeof(vmc_sim_t), 1, fp);
  185. if(!rd_count){
  186. fclose(fp);
  187. return SGX_ERROR_UNEXPECTED;
  188. }
  189. fclose(fp);
  190. return SGX_SUCCESS;
  191. }