tae_service_sim.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. //tae_service_sim.cpp : Defines the exported functions
  32. //
  33. #include "sgx_tae_service.h"
  34. #include "tae_service_internal.h"
  35. #include "pse_types.h"
  36. #include "stdlib.h"
  37. #include "string.h"
  38. #include "sgx_spinlock.h"
  39. #include "sgx_tae_service_t.h"
  40. #define RETRY_TIMES 2
  41. #define DEFAULT_VMC_ATTRIBUTE_MASK 0xFFFFFFFFFFFFFFCB
  42. #define DEFAULT_VMC_XFRM_MASK 0x0
  43. #ifdef _DEBUG
  44. //wait for 10min at most for debug
  45. #define DEFAULT_AESM_TIMEOUT 600000
  46. #else
  47. //wait for 10sec at most
  48. #define DEFAULT_AESM_TIMEOUT 10000
  49. #endif
  50. static sgx_spinlock_t g_spin_lock;
  51. static bool g_b_session_established = false;
  52. sgx_status_t sgx_create_pse_session()
  53. {
  54. sgx_spin_lock(&g_spin_lock);
  55. if(!g_b_session_established)
  56. g_b_session_established = true;
  57. sgx_spin_unlock(&g_spin_lock);
  58. return SGX_SUCCESS;
  59. }
  60. sgx_status_t sgx_close_pse_session()
  61. {
  62. sgx_spin_lock(&g_spin_lock);
  63. if(g_b_session_established)
  64. g_b_session_established = false;
  65. sgx_spin_unlock(&g_spin_lock);
  66. return SGX_SUCCESS;
  67. }
  68. sgx_status_t sgx_get_ps_sec_prop(sgx_ps_sec_prop_desc_t *p_security_property)
  69. {
  70. sgx_status_t ret = SGX_SUCCESS;
  71. if(!p_security_property)
  72. return SGX_ERROR_INVALID_PARAMETER;
  73. if (!g_b_session_established){
  74. return SGX_ERROR_AE_SESSION_INVALID;
  75. }
  76. else
  77. {
  78. se_ps_sec_prop_desc_internal_t *p_security_property_internal = (se_ps_sec_prop_desc_internal_t*)p_security_property;
  79. p_security_property_internal->desc_type = 0;
  80. p_security_property_internal->pse_miscselect = 0;
  81. p_security_property_internal->reserved1 = 0;
  82. memset(p_security_property_internal->reserved2, 0, sizeof(p_security_property_internal->reserved2));
  83. p_security_property_internal->pse_prod_id = 2;
  84. p_security_property_internal->pse_isvsvn = 1;
  85. p_security_property_internal->pse_attributes.flags = SGX_FLAGS_INITTED;
  86. p_security_property_internal->pse_attributes.xfrm = SGX_XFRM_LEGACY;
  87. memset(&(p_security_property_internal->pse_mr_signer), 0xEE,
  88. sizeof(p_security_property_internal->pse_mr_signer));
  89. p_security_property_internal->cse_sec_prop.gid_cse = 0;
  90. p_security_property_internal->cse_sec_prop.prvrl_version = 1;
  91. p_security_property_internal->cse_sec_prop.sigrl_version = 1;
  92. p_security_property_internal->cse_sec_prop.sec_info_type = 0;
  93. memset(&p_security_property_internal->cse_sec_prop.ca_id_cse, 0, sizeof(p_security_property_internal->cse_sec_prop.ca_id_cse));
  94. memset(&p_security_property_internal->cse_sec_prop.sec_info, 0, sizeof(p_security_property_internal->cse_sec_prop.sec_info));
  95. }
  96. return ret;
  97. }
  98. sgx_status_t sgx_get_trusted_time
  99. (
  100. sgx_time_t *p_current_time,
  101. sgx_time_source_nonce_t *p_time_source_nonce)
  102. {
  103. if(!p_current_time || !p_time_source_nonce)
  104. return SGX_ERROR_INVALID_PARAMETER;
  105. if (!g_b_session_established){
  106. return SGX_ERROR_AE_SESSION_INVALID;
  107. }
  108. pse_message_t *p_req_msg =
  109. (pse_message_t *)malloc(PSE_TIMER_READ_REQ_SIZE);
  110. if(!p_req_msg){
  111. return SGX_ERROR_OUT_OF_MEMORY;
  112. }
  113. pse_message_t *p_resp_msg =
  114. (pse_message_t *)malloc(PSE_TIMER_READ_RESP_SIZE);
  115. if(!p_resp_msg){
  116. free(p_req_msg);
  117. return SGX_ERROR_OUT_OF_MEMORY;
  118. }
  119. p_req_msg->exp_resp_size = sizeof(pse_timer_read_resp_t);
  120. p_req_msg->payload_size = sizeof(pse_timer_read_req_t);
  121. pse_timer_read_req_t *p_timer_req
  122. = (pse_timer_read_req_t *)p_req_msg->payload;
  123. p_timer_req->req_hdr.service_id = PSE_TRUSTED_TIME_SERVICE;
  124. p_timer_req->req_hdr.service_cmd = PSE_TIMER_READ;
  125. pse_timer_read_resp_t *p_timer_resp
  126. = (pse_timer_read_resp_t *)p_resp_msg->payload;
  127. sgx_status_t status = SGX_SUCCESS;
  128. sgx_status_t ret = SGX_SUCCESS;
  129. int retry = RETRY_TIMES;
  130. do {
  131. status = invoke_service_ocall(&ret,
  132. (uint8_t *)p_req_msg,
  133. PSE_TIMER_READ_REQ_SIZE,
  134. (uint8_t *)p_resp_msg,
  135. PSE_TIMER_READ_RESP_SIZE,
  136. DEFAULT_AESM_TIMEOUT);
  137. if(status != SGX_SUCCESS || ret != SGX_SUCCESS){
  138. status = SGX_ERROR_UNEXPECTED;
  139. continue;
  140. }
  141. if(p_timer_resp->resp_hdr.service_id != PSE_TRUSTED_TIME_SERVICE
  142. || p_timer_resp->resp_hdr.service_cmd != PSE_TIMER_READ
  143. || p_timer_resp->resp_hdr.status != PSE_SUCCESS){
  144. status = SGX_ERROR_UNEXPECTED;
  145. } else {
  146. memcpy(p_current_time, &p_timer_resp->timestamp, sizeof(sgx_time_t));
  147. memcpy(p_time_source_nonce, p_timer_resp->time_source_nonce,
  148. sizeof(sgx_time_source_nonce_t));
  149. status = SGX_SUCCESS;
  150. break;
  151. }
  152. } while(retry--);
  153. free(p_req_msg);
  154. free(p_resp_msg);
  155. return status;
  156. }
  157. sgx_status_t sgx_create_monotonic_counter_ex(
  158. uint16_t owner_policy,
  159. const sgx_attributes_t* owner_attribute_mask,
  160. sgx_mc_uuid_t *p_counter_uuid,
  161. uint32_t *p_counter_value)
  162. {
  163. if(!p_counter_value || !p_counter_uuid || !owner_attribute_mask){
  164. return SGX_ERROR_INVALID_PARAMETER;
  165. }
  166. if (0!= (~(MC_POLICY_SIGNER | MC_POLICY_ENCLAVE) & owner_policy)
  167. || 0 == ((MC_POLICY_SIGNER | MC_POLICY_ENCLAVE)& owner_policy))
  168. {
  169. return SGX_ERROR_INVALID_PARAMETER;
  170. }
  171. if (!g_b_session_established){
  172. return SGX_ERROR_AE_SESSION_INVALID;
  173. }
  174. pse_message_t *p_req_msg = (pse_message_t *)malloc(PSE_CREATE_MC_REQ_SIZE);
  175. if(!p_req_msg){
  176. return SGX_ERROR_OUT_OF_MEMORY;
  177. }
  178. pse_message_t *p_resp_msg = (pse_message_t *)malloc(PSE_CREATE_MC_RESP_SIZE);
  179. if(!p_resp_msg){
  180. free(p_req_msg);
  181. return SGX_ERROR_OUT_OF_MEMORY;
  182. }
  183. p_req_msg->exp_resp_size = sizeof(pse_mc_create_resp_t);
  184. p_req_msg->payload_size = sizeof(pse_mc_create_req_t);
  185. pse_mc_create_req_t *p_mc_req
  186. = (pse_mc_create_req_t *)p_req_msg->payload;
  187. p_mc_req->req_hdr.service_id = PSE_MC_SERVICE;
  188. p_mc_req->req_hdr.service_cmd = PSE_MC_CREATE;
  189. p_mc_req->policy = owner_policy;
  190. memcpy(p_mc_req->attr_mask , owner_attribute_mask, sizeof(p_mc_req->attr_mask));
  191. pse_mc_create_resp_t *p_mc_resp
  192. = (pse_mc_create_resp_t *)p_resp_msg->payload;
  193. sgx_status_t status = SGX_SUCCESS;
  194. sgx_status_t ret = SGX_SUCCESS;
  195. int retry = RETRY_TIMES;
  196. do {
  197. sgx_spin_lock(&g_spin_lock);
  198. status = invoke_service_ocall(&ret,
  199. (uint8_t *)p_req_msg,
  200. PSE_CREATE_MC_REQ_SIZE,
  201. (uint8_t *)p_resp_msg,
  202. PSE_CREATE_MC_RESP_SIZE,
  203. DEFAULT_AESM_TIMEOUT);
  204. sgx_spin_unlock(&g_spin_lock);
  205. if(status != SGX_SUCCESS || ret != SGX_SUCCESS){
  206. status = SGX_ERROR_UNEXPECTED;
  207. continue;
  208. }
  209. if(p_mc_resp->resp_hdr.service_id != PSE_MC_SERVICE
  210. || p_mc_resp->resp_hdr.service_cmd != PSE_MC_CREATE
  211. || p_mc_resp->resp_hdr.status != PSE_SUCCESS){
  212. status = SGX_ERROR_UNEXPECTED;
  213. } else {
  214. memcpy(p_counter_uuid->counter_id,
  215. &p_mc_resp->counter_id,
  216. sizeof(p_counter_uuid->counter_id));
  217. memcpy(p_counter_uuid->nonce,
  218. &p_mc_resp->nonce,
  219. sizeof(p_counter_uuid->nonce));
  220. *p_counter_value = 0;
  221. status = SGX_SUCCESS;
  222. break;
  223. }
  224. } while(retry--);
  225. free(p_req_msg);
  226. free(p_resp_msg);
  227. return status;
  228. }
  229. sgx_status_t sgx_increment_monotonic_counter(
  230. const sgx_mc_uuid_t *p_counter_uuid,
  231. uint32_t *p_counter_value)
  232. {
  233. if(!p_counter_value || !p_counter_uuid ){
  234. return SGX_ERROR_INVALID_PARAMETER;
  235. }
  236. if (!g_b_session_established){
  237. return SGX_ERROR_AE_SESSION_INVALID;
  238. }
  239. pse_message_t *p_req_msg = (pse_message_t *)malloc(PSE_INC_MC_REQ_SIZE);
  240. if(!p_req_msg){
  241. return SGX_ERROR_OUT_OF_MEMORY;
  242. }
  243. pse_message_t *p_resp_msg = (pse_message_t *)malloc(PSE_INC_MC_RESP_SIZE);
  244. if(!p_resp_msg){
  245. free(p_req_msg);
  246. return SGX_ERROR_OUT_OF_MEMORY;
  247. }
  248. p_req_msg->exp_resp_size = sizeof(pse_mc_inc_resp_t);
  249. p_req_msg->payload_size = sizeof(pse_mc_inc_req_t);
  250. pse_mc_inc_req_t *p_mc_req
  251. = (pse_mc_inc_req_t *)p_req_msg->payload;
  252. memcpy(p_mc_req->counter_id,
  253. p_counter_uuid->counter_id,
  254. sizeof(p_mc_req->counter_id));
  255. memcpy(p_mc_req->nonce,
  256. p_counter_uuid->nonce,
  257. sizeof(p_mc_req->nonce));
  258. p_mc_req->req_hdr.service_id = PSE_MC_SERVICE;
  259. p_mc_req->req_hdr.service_cmd = PSE_MC_INC;
  260. pse_mc_inc_resp_t *p_mc_resp
  261. = (pse_mc_inc_resp_t *)p_resp_msg->payload;
  262. sgx_status_t status = SGX_SUCCESS;
  263. sgx_status_t ret = SGX_SUCCESS;
  264. int retry = RETRY_TIMES;
  265. do {
  266. sgx_spin_lock(&g_spin_lock);
  267. status = invoke_service_ocall(&ret,
  268. (uint8_t *)p_req_msg,
  269. PSE_INC_MC_REQ_SIZE,
  270. (uint8_t *)p_resp_msg,
  271. PSE_INC_MC_RESP_SIZE,
  272. DEFAULT_AESM_TIMEOUT);
  273. sgx_spin_unlock(&g_spin_lock);
  274. if(status != SGX_SUCCESS || ret != SGX_SUCCESS){
  275. if(SGX_ERROR_MC_NOT_FOUND != ret)
  276. status = SGX_ERROR_UNEXPECTED;
  277. else
  278. status = SGX_ERROR_MC_NOT_FOUND;
  279. continue;
  280. }
  281. if(p_mc_resp->resp_hdr.service_id != PSE_MC_SERVICE
  282. || p_mc_resp->resp_hdr.service_cmd != PSE_MC_INC
  283. || p_mc_resp->resp_hdr.status != PSE_SUCCESS){
  284. if(PSE_ERROR_MC_NOT_FOUND == p_mc_resp->resp_hdr.status)
  285. status = SGX_ERROR_MC_NOT_FOUND;
  286. else
  287. status = SGX_ERROR_UNEXPECTED;
  288. } else
  289. {
  290. *p_counter_value = p_mc_resp->counter_value;
  291. status = SGX_SUCCESS;
  292. break;
  293. }
  294. } while(retry--);
  295. free(p_req_msg);
  296. free(p_resp_msg);
  297. return status;
  298. }
  299. sgx_status_t sgx_read_monotonic_counter(
  300. const sgx_mc_uuid_t *p_counter_uuid,
  301. uint32_t *p_counter_value)
  302. {
  303. if(!p_counter_value || !p_counter_uuid){
  304. return SGX_ERROR_INVALID_PARAMETER;
  305. }
  306. if (!g_b_session_established){
  307. return SGX_ERROR_AE_SESSION_INVALID;
  308. }
  309. pse_message_t *p_req_msg = (pse_message_t *)malloc(PSE_READ_MC_REQ_SIZE);
  310. if(!p_req_msg){
  311. return SGX_ERROR_OUT_OF_MEMORY;
  312. }
  313. pse_message_t *p_resp_msg = (pse_message_t *)malloc(PSE_READ_MC_RESP_SIZE);
  314. if(!p_resp_msg){
  315. free(p_req_msg);
  316. return SGX_ERROR_OUT_OF_MEMORY;
  317. }
  318. p_req_msg->exp_resp_size = sizeof(pse_mc_read_resp_t);
  319. p_req_msg->payload_size = sizeof(pse_mc_read_req_t);
  320. pse_mc_read_req_t *p_mc_req
  321. = (pse_mc_read_req_t *)p_req_msg->payload;
  322. memcpy(p_mc_req->counter_id,
  323. p_counter_uuid->counter_id,
  324. sizeof(p_mc_req->counter_id));
  325. memcpy(p_mc_req->nonce,
  326. p_counter_uuid->nonce,
  327. sizeof(p_mc_req->nonce));
  328. p_mc_req->req_hdr.service_id = PSE_MC_SERVICE;
  329. p_mc_req->req_hdr.service_cmd = PSE_MC_READ;
  330. pse_mc_read_resp_t *p_mc_resp
  331. = (pse_mc_read_resp_t *)p_resp_msg->payload;
  332. sgx_status_t status = SGX_SUCCESS;
  333. sgx_status_t ret = SGX_SUCCESS;
  334. int retry = RETRY_TIMES;
  335. do {
  336. sgx_spin_lock(&g_spin_lock);
  337. status = invoke_service_ocall(&ret,
  338. (uint8_t *)p_req_msg,
  339. PSE_READ_MC_REQ_SIZE,
  340. (uint8_t *)p_resp_msg,
  341. PSE_READ_MC_RESP_SIZE,
  342. DEFAULT_AESM_TIMEOUT);
  343. sgx_spin_unlock(&g_spin_lock);
  344. if(status != SGX_SUCCESS || ret != SGX_SUCCESS){
  345. if(SGX_ERROR_MC_NOT_FOUND != ret)
  346. status = SGX_ERROR_UNEXPECTED;
  347. else
  348. status = SGX_ERROR_MC_NOT_FOUND;
  349. continue;
  350. }
  351. if(p_mc_resp->resp_hdr.service_id != PSE_MC_SERVICE
  352. || p_mc_resp->resp_hdr.service_cmd != PSE_MC_READ
  353. || p_mc_resp->resp_hdr.status != PSE_SUCCESS){
  354. if(PSE_ERROR_MC_NOT_FOUND == p_mc_resp->resp_hdr.status)
  355. status = SGX_ERROR_MC_NOT_FOUND;
  356. else
  357. status = SGX_ERROR_UNEXPECTED;
  358. } else {
  359. *p_counter_value = p_mc_resp->counter_value;
  360. status = SGX_SUCCESS;
  361. break;
  362. }
  363. } while(retry--);
  364. free(p_req_msg);
  365. free(p_resp_msg);
  366. return status;
  367. }
  368. sgx_status_t sgx_create_monotonic_counter(
  369. sgx_mc_uuid_t *p_counter_uuid,
  370. uint32_t *p_counter_value)
  371. {
  372. //Default attribute mask
  373. sgx_attributes_t attr_mask;
  374. attr_mask.flags = DEFAULT_VMC_ATTRIBUTE_MASK;
  375. attr_mask.xfrm = DEFAULT_VMC_XFRM_MASK;
  376. return sgx_create_monotonic_counter_ex(MC_POLICY_SIGNER,
  377. &attr_mask,
  378. p_counter_uuid,
  379. p_counter_value);
  380. }
  381. sgx_status_t sgx_destroy_monotonic_counter(const sgx_mc_uuid_t *p_counter_uuid)
  382. {
  383. if(!p_counter_uuid){
  384. return SGX_ERROR_INVALID_PARAMETER;
  385. }
  386. if (!g_b_session_established){
  387. return SGX_ERROR_AE_SESSION_INVALID;
  388. }
  389. pse_message_t *p_req_msg = (pse_message_t *)malloc(PSE_DEL_MC_REQ_SIZE);
  390. if(!p_req_msg){
  391. return SGX_ERROR_OUT_OF_MEMORY;
  392. }
  393. pse_message_t *p_resp_msg = (pse_message_t *)malloc(PSE_DEL_MC_RESP_SIZE);
  394. if(!p_resp_msg){
  395. free(p_req_msg);
  396. return SGX_ERROR_OUT_OF_MEMORY;
  397. }
  398. p_req_msg->exp_resp_size = sizeof(pse_mc_del_resp_t);
  399. p_req_msg->payload_size = sizeof(pse_mc_del_req_t);
  400. pse_mc_del_req_t *p_mc_req
  401. = (pse_mc_del_req_t *)p_req_msg->payload;
  402. memcpy(p_mc_req->counter_id,
  403. p_counter_uuid->counter_id,
  404. sizeof(p_mc_req->counter_id));
  405. memcpy(p_mc_req->nonce,
  406. p_counter_uuid->nonce,
  407. sizeof(p_mc_req->nonce));
  408. p_mc_req->req_hdr.service_id = PSE_MC_SERVICE;
  409. p_mc_req->req_hdr.service_cmd = PSE_MC_DEL;
  410. pse_mc_del_resp_t *p_mc_resp
  411. = (pse_mc_del_resp_t *)p_resp_msg->payload;
  412. sgx_status_t status = SGX_SUCCESS;
  413. sgx_status_t ret = SGX_SUCCESS;
  414. int retry = RETRY_TIMES;
  415. do {
  416. status = invoke_service_ocall(&ret,
  417. (uint8_t *)p_req_msg,
  418. PSE_DEL_MC_REQ_SIZE,
  419. (uint8_t *)p_resp_msg,
  420. PSE_DEL_MC_RESP_SIZE,
  421. DEFAULT_AESM_TIMEOUT);
  422. if(status != SGX_SUCCESS || ret != SGX_SUCCESS){
  423. if(SGX_ERROR_MC_NOT_FOUND != ret)
  424. status = SGX_ERROR_UNEXPECTED;
  425. else
  426. status = SGX_ERROR_MC_NOT_FOUND;
  427. continue;
  428. }
  429. if(p_mc_resp->resp_hdr.service_id != PSE_MC_SERVICE
  430. || p_mc_resp->resp_hdr.service_cmd != PSE_MC_DEL
  431. || p_mc_resp->resp_hdr.status != PSE_SUCCESS){
  432. if(PSE_ERROR_MC_NOT_FOUND == p_mc_resp->resp_hdr.status)
  433. status = SGX_ERROR_MC_NOT_FOUND;
  434. else
  435. status = SGX_ERROR_UNEXPECTED;
  436. } else {
  437. status = SGX_SUCCESS;
  438. break;
  439. }
  440. } while(retry--);
  441. free(p_req_msg);
  442. free(p_resp_msg);
  443. return status;
  444. }