platform_service_sim.cpp 5.8 KB

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