platform_service_sim_com.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35. static const uint8_t TIME_SOURCE_NONCE_SIM[32] = {
  36. 0x9d, 0x7c, 0x25, 0x07, 0x38, 0x53, 0x23, 0xb1,
  37. 0x9f, 0xba, 0xc8, 0x7b, 0xc0, 0x89, 0xde, 0x2d,
  38. 0x2b, 0x5f, 0x34, 0x6d, 0x9c, 0x35, 0xf5, 0xbc,
  39. 0xcd, 0x34, 0x7f, 0x75, 0x96, 0xc8, 0x27, 0xcc};
  40. sgx_status_t pse_mc_create_sim(
  41. uint8_t *p_req_payload,
  42. uint8_t *p_resp_payload)
  43. {
  44. UNUSED(p_req_payload);
  45. sgx_status_t ret = SGX_ERROR_UNEXPECTED;
  46. pse_mc_create_resp_t *p_create_resp
  47. = (pse_mc_create_resp_t *)p_resp_payload;
  48. //initial create_resp
  49. p_create_resp->resp_hdr.service_id = PSE_MC_SERVICE;
  50. p_create_resp->resp_hdr.service_cmd = PSE_MC_CREATE;
  51. p_create_resp->resp_hdr.status = PSE_ERROR_INTERNAL;
  52. memset(p_create_resp->counter_id, 0xFF, sizeof(p_create_resp->counter_id));
  53. memset(p_create_resp->nonce, 0xFF, sizeof(p_create_resp->nonce));
  54. vmc_sim_t temp_vmc = {{0}, {0}, 0};
  55. ret = get_counter_id(&temp_vmc);
  56. if(SGX_SUCCESS != ret){
  57. return ret;
  58. }
  59. ret = store_vmc_sim(&temp_vmc);
  60. if(SGX_SUCCESS != ret){
  61. return ret;
  62. }
  63. p_create_resp->resp_hdr.status = PSE_SUCCESS;
  64. if(memcpy_s(p_create_resp->counter_id,
  65. sizeof(p_create_resp->counter_id),
  66. temp_vmc.counter_id,
  67. sizeof(temp_vmc.counter_id))){
  68. return SGX_ERROR_UNEXPECTED;
  69. }
  70. if(memcpy_s(p_create_resp->nonce,
  71. sizeof(p_create_resp->nonce),
  72. temp_vmc.nonce,
  73. sizeof(temp_vmc.nonce))){
  74. return SGX_ERROR_UNEXPECTED;
  75. }
  76. return ret;
  77. }
  78. sgx_status_t pse_mc_read_sim(
  79. uint8_t *p_req_payload,
  80. uint8_t *p_resp_payload)
  81. {
  82. sgx_status_t ret = SGX_ERROR_UNEXPECTED;
  83. pse_mc_read_req_t *p_read_req = (pse_mc_read_req_t *)p_req_payload;
  84. pse_mc_read_resp_t *p_read_resp = (pse_mc_read_resp_t *)p_resp_payload;
  85. //initial create_resp
  86. p_read_resp->counter_value = 0;
  87. p_read_resp->resp_hdr.service_id = PSE_MC_SERVICE;
  88. p_read_resp->resp_hdr.service_cmd = PSE_MC_READ;
  89. p_read_resp->resp_hdr.status = PSE_ERROR_INTERNAL;
  90. vmc_sim_t temp_vmc = {{0}, {0}, 0};
  91. if(memcpy_s(&temp_vmc.counter_id,
  92. sizeof(temp_vmc.counter_id),
  93. p_read_req->counter_id,
  94. sizeof(p_read_req->counter_id))){
  95. return SGX_ERROR_UNEXPECTED;
  96. }
  97. if(memcpy_s(&temp_vmc.nonce,
  98. sizeof(temp_vmc.nonce),
  99. p_read_req->nonce,
  100. sizeof(p_read_req->nonce))){
  101. return SGX_ERROR_UNEXPECTED;
  102. }
  103. ret = load_vmc_sim(&temp_vmc);
  104. if(SGX_SUCCESS != ret){
  105. memset(&p_read_resp->counter_value, 0xFF,
  106. sizeof(p_read_resp->counter_value));
  107. return ret;
  108. }
  109. p_read_resp->resp_hdr.status = PSE_SUCCESS;
  110. if(memcpy_s(&p_read_resp->counter_value,
  111. sizeof(p_read_resp->counter_value),
  112. &temp_vmc.counter_value,
  113. sizeof(temp_vmc.counter_value))){
  114. return SGX_ERROR_UNEXPECTED;
  115. }
  116. return ret;
  117. }
  118. sgx_status_t pse_mc_inc_sim(
  119. uint8_t *p_req_payload,
  120. uint8_t *p_resp_payload)
  121. {
  122. sgx_status_t ret = SGX_ERROR_UNEXPECTED;
  123. pse_mc_inc_req_t *p_inc_req = (pse_mc_inc_req_t *)p_req_payload;
  124. pse_mc_inc_resp_t *p_inc_resp = (pse_mc_inc_resp_t *)p_resp_payload;
  125. //initial create_resp
  126. p_inc_resp->counter_value = 0;
  127. p_inc_resp->resp_hdr.service_id = PSE_MC_SERVICE;
  128. p_inc_resp->resp_hdr.service_cmd = PSE_MC_INC;
  129. p_inc_resp->resp_hdr.status = PSE_ERROR_INTERNAL;
  130. vmc_sim_t temp_vmc = {{0}, {0}, 0};
  131. if(memcpy_s(&temp_vmc.counter_id,
  132. sizeof(temp_vmc.counter_id),
  133. p_inc_req->counter_id,
  134. sizeof(p_inc_req->counter_id))){
  135. return SGX_ERROR_UNEXPECTED;
  136. }
  137. if(memcpy_s(&temp_vmc.nonce,
  138. sizeof(temp_vmc.nonce),
  139. p_inc_req->nonce,
  140. sizeof(p_inc_req->nonce))){
  141. return SGX_ERROR_UNEXPECTED;
  142. }
  143. ret = load_vmc_sim(&temp_vmc);
  144. if(SGX_SUCCESS != ret){
  145. return ret;
  146. }
  147. temp_vmc.counter_value++;
  148. ret = store_vmc_sim(&temp_vmc);
  149. if(SGX_SUCCESS != ret){
  150. return ret;
  151. }
  152. p_inc_resp->resp_hdr.status = PSE_SUCCESS;
  153. if(memcpy_s(&p_inc_resp->counter_value,
  154. sizeof(p_inc_resp->counter_value),
  155. &temp_vmc.counter_value,
  156. sizeof(temp_vmc.counter_value))){
  157. return SGX_ERROR_UNEXPECTED;
  158. }
  159. return ret;
  160. }
  161. sgx_status_t pse_mc_del_sim(
  162. uint8_t *p_req_payload,
  163. uint8_t *p_resp_payload)
  164. {
  165. sgx_status_t ret = SGX_ERROR_UNEXPECTED;
  166. pse_mc_del_req_t *p_del_req = (pse_mc_del_req_t *)p_req_payload;
  167. pse_mc_del_resp_t *p_del_resp = (pse_mc_del_resp_t *)p_resp_payload;
  168. //initial create_resp
  169. p_del_resp->resp_hdr.service_id = PSE_MC_SERVICE;
  170. p_del_resp->resp_hdr.service_cmd = PSE_MC_DEL;
  171. p_del_resp->resp_hdr.status = PSE_ERROR_INTERNAL;
  172. vmc_sim_t temp_vmc = {{0}, {0}, 0};
  173. if(memcpy_s(&temp_vmc.counter_id,
  174. sizeof(temp_vmc.counter_id),
  175. p_del_req->counter_id,
  176. sizeof(p_del_req->counter_id))){
  177. return SGX_ERROR_UNEXPECTED;
  178. }
  179. if(memcpy_s(&temp_vmc.nonce,
  180. sizeof(temp_vmc.nonce),
  181. p_del_req->nonce,
  182. sizeof(p_del_req->nonce))){
  183. return SGX_ERROR_UNEXPECTED;
  184. }
  185. ret = del_vmc_sim(&temp_vmc);
  186. if(SGX_SUCCESS != ret){
  187. return ret;
  188. }
  189. p_del_resp->resp_hdr.status = PSE_SUCCESS;
  190. return ret;
  191. }
  192. sgx_status_t pse_read_timer_sim(
  193. uint8_t *p_req_payload,
  194. uint8_t *p_resp_payload)
  195. {
  196. UNUSED(p_req_payload);
  197. pse_timer_read_resp_t *p_time_resp
  198. = (pse_timer_read_resp_t *)p_resp_payload;
  199. time_t t = time(0);
  200. p_time_resp->resp_hdr.service_id = PSE_TRUSTED_TIME_SERVICE;
  201. p_time_resp->resp_hdr.service_cmd = PSE_TIMER_READ;
  202. p_time_resp->timestamp = (uint64_t)t;
  203. if(memcpy_s(p_time_resp->time_source_nonce,
  204. sizeof(p_time_resp->time_source_nonce),
  205. TIME_SOURCE_NONCE_SIM, sizeof(p_time_resp->time_source_nonce))){
  206. return SGX_ERROR_UNEXPECTED;
  207. }
  208. p_time_resp->resp_hdr.status = PSE_SUCCESS;
  209. return SGX_SUCCESS;
  210. }
  211. typedef sgx_status_t (*srv_pfn_t)(uint8_t *, uint8_t *);
  212. static const struct service_handler_t {
  213. uint16_t service_id;
  214. uint16_t service_cmd;
  215. uint16_t req_msg_size;
  216. uint16_t resp_msg_size;
  217. srv_pfn_t srv_pfn;
  218. } SERVICE_HANDLER[] = {
  219. {PSE_MC_SERVICE, PSE_MC_CREATE, sizeof(pse_mc_create_req_t),
  220. sizeof(pse_mc_create_resp_t), pse_mc_create_sim},
  221. {PSE_MC_SERVICE, PSE_MC_READ, sizeof(pse_mc_read_req_t),
  222. sizeof(pse_mc_read_resp_t), pse_mc_read_sim},
  223. {PSE_MC_SERVICE, PSE_MC_INC, sizeof(pse_mc_inc_req_t),
  224. sizeof(pse_mc_inc_resp_t), pse_mc_inc_sim},
  225. {PSE_MC_SERVICE, PSE_MC_DEL, sizeof(pse_mc_del_req_t),
  226. sizeof(pse_mc_del_resp_t), pse_mc_del_sim},
  227. {PSE_TRUSTED_TIME_SERVICE, PSE_TIMER_READ, sizeof(pse_timer_read_req_t),
  228. sizeof(pse_timer_read_resp_t), pse_read_timer_sim}
  229. };
  230. static sgx_status_t invoke_service(
  231. const uint8_t *p_pse_message_req,
  232. size_t pse_message_req_size,
  233. uint8_t *p_pse_message_resp,
  234. size_t pse_message_resp_size,
  235. unsigned long timeout)
  236. {
  237. UNUSED(timeout);
  238. sgx_status_t ret = SGX_SUCCESS;
  239. if(!p_pse_message_req || !p_pse_message_resp){
  240. return SGX_ERROR_INVALID_PARAMETER;
  241. }
  242. const pse_message_t *p_req_msg = (const pse_message_t *)p_pse_message_req;
  243. pse_message_t *p_resp_msg = (pse_message_t *)p_pse_message_resp;
  244. if(pse_message_req_size != sizeof(pse_message_t)
  245. + p_req_msg->payload_size){
  246. return SGX_ERROR_INVALID_PARAMETER;
  247. }
  248. if(pse_message_resp_size < sizeof(pse_message_t)
  249. + p_resp_msg->exp_resp_size){
  250. return SGX_ERROR_INVALID_PARAMETER;
  251. }
  252. const uint8_t *p_req_payload = p_req_msg->payload;
  253. uint8_t *p_resp_payload = p_resp_msg->payload;
  254. const pse_req_hdr_t *p_req_hdr = (const pse_req_hdr_t *)p_req_payload;
  255. for(unsigned int i = 0;
  256. i < sizeof(SERVICE_HANDLER) / sizeof(service_handler_t); i++)
  257. {
  258. if(p_req_hdr->service_id == SERVICE_HANDLER[i].service_id
  259. && p_req_hdr->service_cmd == SERVICE_HANDLER[i].service_cmd){
  260. if(p_req_msg->payload_size != SERVICE_HANDLER[i].req_msg_size
  261. || p_req_msg->exp_resp_size != SERVICE_HANDLER[i].resp_msg_size)
  262. {
  263. ret = SGX_ERROR_UNEXPECTED;
  264. break;
  265. }
  266. memset(p_resp_payload, 0, p_req_msg->exp_resp_size);
  267. ret = SERVICE_HANDLER[i].srv_pfn(const_cast<uint8_t*>(p_req_payload), p_resp_payload);
  268. p_resp_msg->payload_size = p_req_msg->exp_resp_size;
  269. break;
  270. }
  271. }
  272. return ret;
  273. }
  274. sgx_status_t create_session_ocall(uint32_t *p_sid, uint8_t *p_dh_msg1,
  275. uint32_t dh_msg1_size, uint32_t timeout)
  276. {
  277. UNUSED(p_sid);
  278. UNUSED(p_dh_msg1);
  279. UNUSED(dh_msg1_size);
  280. UNUSED(timeout);
  281. return SGX_SUCCESS;
  282. }
  283. sgx_status_t exchange_report_ocall(uint32_t sid,
  284. const uint8_t *p_dh_msg2,
  285. uint32_t dh_msg2_size,
  286. uint8_t *p_dh_msg3,
  287. uint32_t dh_msg3_size,
  288. uint32_t timeout)
  289. {
  290. UNUSED(sid);
  291. UNUSED(p_dh_msg2);
  292. UNUSED(dh_msg2_size);
  293. UNUSED(p_dh_msg3);
  294. UNUSED(dh_msg3_size);
  295. UNUSED(timeout);
  296. return SGX_SUCCESS;
  297. }
  298. sgx_status_t close_session_ocall(uint32_t sid, uint32_t timeout)
  299. {
  300. UNUSED(sid);
  301. UNUSED(timeout);
  302. return SGX_SUCCESS;
  303. }
  304. sgx_status_t invoke_service_ocall(
  305. const uint8_t* pse_message_req, uint32_t pse_message_req_size,
  306. uint8_t* pse_message_resp, uint32_t pse_message_resp_size,
  307. uint32_t timeout
  308. )
  309. {
  310. return invoke_service(pse_message_req, pse_message_req_size,
  311. pse_message_resp, pse_message_resp_size, timeout);
  312. }
  313. sgx_status_t sgx_get_ps_cap(
  314. sgx_ps_cap_t* p_sgx_ps_cap)
  315. {
  316. if (!p_sgx_ps_cap)
  317. return SGX_ERROR_INVALID_PARAMETER;
  318. p_sgx_ps_cap->ps_cap0 = 0x7;
  319. p_sgx_ps_cap->ps_cap1 = 0;
  320. return SGX_SUCCESS;
  321. }
  322. #ifdef __cplusplus
  323. }
  324. #endif