platform_service_sim.cpp 6.0 KB

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