PSEClass.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  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 "arch.h"
  32. #include "PSEClass.h"
  33. #include "pse_op_u.h"
  34. #include "pse_op_u.c"
  35. #include "pse_op_psda_ocall.cpp"
  36. #include "pse_op_vmc_sqlite_ocall.cpp"
  37. #include "sgx_tseal.h"
  38. #include "pairing_blob.h"
  39. #include "oal/oal.h"
  40. #include "byte_order.h"
  41. #include "LEClass.h"
  42. #include "ae_ipp.h"
  43. #include "PSDAService.h"
  44. #include "se_wrapper.h"
  45. #include "PSEPRClass.h"
  46. #include "sgx_profile.h"
  47. #include "sgx_uae_service.h"
  48. #include "aesm_pse_status.h"
  49. #include "interface_psda.h"
  50. #define PSDA_CAP_PRTC 0x1
  51. #define PSDA_CAP_PROTECTED_OUTPUT 0x4
  52. #define PSDA_CAP_RPDATA 0x8
  53. #define PS_CAP_PROTECTED_OUTPUT 0x4
  54. #define CHECK_ECALL_RET(status,ret) \
  55. if((status) == SGX_ERROR_ENCLAVE_LOST) \
  56. {retry++;continue;} \
  57. else if ((status) == SGX_ERROR_OUT_OF_MEMORY) { \
  58. (ret) = AE_OUT_OF_MEMORY_ERROR; break; } \
  59. else if ((status) != SGX_SUCCESS) { \
  60. (ret) = AE_FAILURE; break; } \
  61. else if ((ret) != 0) { break; }
  62. extern uint32_t upse_iclsInit();
  63. ae_error_t CPSEClass::init_ps(void)
  64. {
  65. // Try to establish PSDA session during startup
  66. PROFILE_START("PSDAService::start_service()");
  67. bool psda_started = PSDAService::instance().start_service();
  68. PROFILE_END("PSDAService::start_service()");
  69. if (!psda_started)
  70. {
  71. AESM_DBG_ERROR("Psda not available");
  72. AESM_LOG_INFO_ADMIN("%s", g_admin_event_string_table[SGX_ADMIN_EVENT_PS_INIT_START]);
  73. // This is logged as a WARNING here, since the system may not require PS capability
  74. AESM_LOG_WARN_ADMIN("%s", g_admin_event_string_table[SGX_ADMIN_EVENT_PS_INIT_FAIL_DAL]);
  75. // Set state to UNAVAILABLE
  76. m_status = PSE_STATUS_UNAVAILABLE;
  77. PlatformServiceStatus::instance().set_platform_service_status(PLATFORM_SERVICE_NOT_AVAILABLE);
  78. return AESM_PSDA_NOT_AVAILABLE;
  79. }
  80. //Logic here is that ME FW mode is used(Emulator is not running)
  81. //provisioning is attempted using iclsclient and the return code is not verified
  82. //In case of emulator the emulator provisioning tool is used to provision for epid 1.1 and if not long term pairing will return not provisioned error.
  83. pse_pr_interface_psda* pPSDA = new(std::nothrow) pse_pr_interface_psda();
  84. if (pPSDA == NULL) {
  85. return AE_OUT_OF_MEMORY_ERROR;
  86. }
  87. // Probe CSME provisiong status first by calling get_csme_gid()
  88. ae_error_t ret = pPSDA->get_csme_gid(&PSDAService::instance().csme_gid);
  89. if (ret != AE_SUCCESS)
  90. {
  91. // As long as get_csme_gid fails, call iclsInit to trigger provisioning
  92. uint32_t status_provision = upse_iclsInit();
  93. if (status_provision != 0)
  94. {
  95. // Provisioning failed , maybe caused by missing of iCls client, etc.
  96. AESM_LOG_INFO_ADMIN("%s", g_admin_event_string_table[SGX_ADMIN_EVENT_PS_INIT_START]);
  97. // This is logged as a WARNING here, since the system may not require PS capability
  98. AESM_LOG_WARN_ADMIN("%s", g_admin_event_string_table[SGX_ADMIN_EVENT_PS_INIT_FAIL_DAL]);
  99. delete pPSDA;
  100. pPSDA = NULL;
  101. return AESM_PSE_PR_PSDA_PROVISION_ERROR;
  102. }
  103. else
  104. {
  105. // try to get CSME GID again
  106. ret = pPSDA->get_csme_gid(&PSDAService::instance().csme_gid);
  107. if (ret != AE_SUCCESS)
  108. {
  109. // Failed to get CSME GID
  110. AESM_LOG_INFO_ADMIN("%s", g_admin_event_string_table[SGX_ADMIN_EVENT_PS_INIT_START]);
  111. // This is logged as a WARNING here, since the system may not require PS capability
  112. AESM_LOG_WARN_ADMIN("%s", g_admin_event_string_table[SGX_ADMIN_EVENT_PS_INIT_FAIL_DAL]);
  113. delete pPSDA;
  114. pPSDA = NULL;
  115. return ret;
  116. }
  117. }
  118. }
  119. delete pPSDA;
  120. pPSDA = NULL;
  121. // Set state to PROVISIONED
  122. m_status = PSE_STATUS_CSE_PROVISIONED;
  123. // Get platform service capbility
  124. PROFILE_START("get_ps_cap");
  125. ret = get_ps_cap(&m_ps_cap);
  126. PROFILE_END("get_ps_cap");
  127. if (ret != AE_SUCCESS){
  128. AESM_LOG_INFO_ADMIN("%s", g_admin_event_string_table[SGX_ADMIN_EVENT_PS_INIT_START]);
  129. // This is logged as a WARNING here, since the system may not require PS capability
  130. AESM_LOG_WARN_ADMIN("%s", g_admin_event_string_table[SGX_ADMIN_EVENT_PS_INIT_FAIL_DAL]);
  131. AESM_DBG_ERROR("get_ps_cap failed:%d",ret);
  132. return ret;
  133. }
  134. // Try to establish ephemeral session
  135. PROFILE_START("create_ephemeral_session_pse_cse");
  136. ret = create_ephemeral_session_pse_cse(false, false);
  137. PROFILE_END("create_ephemeral_session_pse_cse");
  138. if(ret != AE_SUCCESS)
  139. {
  140. AESM_DBG_ERROR("creatfe_ephemeral_session_pse_cse failed:%d", ret);
  141. if (ret == PSE_OP_LTPB_SEALING_OUT_OF_DATE)
  142. {
  143. AESM_DBG_ERROR("TCB update casued ephemeral session failure, reseal LTP blob now");
  144. // Try to reseal LTP blob
  145. bool is_new_pairing = false;
  146. ae_error_t ltpStatus = CPSEPRClass::instance().long_term_pairing(&is_new_pairing);
  147. if (ltpStatus == AE_SUCCESS)
  148. {
  149. AESM_DBG_INFO("Reseal LTP blob succeeded. Try ephermeal session again.");
  150. ret = create_ephemeral_session_pse_cse(is_new_pairing, false);
  151. if (ret != AE_SUCCESS){
  152. AESM_DBG_ERROR("creatfe_ephemeral_session_pse_cse after ltp blob resealing failed:%d", ret);
  153. }
  154. }
  155. }
  156. }
  157. else {
  158. // If this succeeds we should log PS Init start/success, simply because it won't be repeated and
  159. // logged later. We don't log the error flows here, because we don't consider this the "real"
  160. // PS Init. That will happen the first time create_session(), etc is invoked.
  161. AESM_LOG_INFO_ADMIN("%s", g_admin_event_string_table[SGX_ADMIN_EVENT_PS_INIT_START]);
  162. AESM_LOG_INFO_ADMIN("%s", g_admin_event_string_table[SGX_ADMIN_EVENT_PS_INIT_SUCCESS]);
  163. }
  164. return ret;
  165. }
  166. void CPSEClass::before_enclave_load() {
  167. // always unload pse_pr enclave before loading pse_op enclave
  168. CPSEPRClass::instance().unload_enclave();
  169. }
  170. ae_error_t CPSEClass::create_session(
  171. uint32_t* session_id,
  172. uint8_t* se_dh_msg1, uint32_t se_dh_msg1_size
  173. )
  174. {
  175. sgx_status_t status = SGX_SUCCESS;
  176. ae_error_t ret = AE_SUCCESS;
  177. ae_error_t ret2 = AE_SUCCESS;
  178. // check enclave ID
  179. if(!m_enclave_id)
  180. return AE_FAILURE;
  181. if(sizeof(pse_dh_msg1_t) != se_dh_msg1_size)
  182. return PSE_OP_PARAMETER_ERROR;
  183. uint64_t milliseconds = (uint64_t)(se_get_tick_count() * 1000.0 / m_freq+0.5);
  184. status = create_session_wrapper(m_enclave_id,&ret,milliseconds,session_id,(pse_dh_msg1_t*)se_dh_msg1);
  185. if (status == SGX_ERROR_ENCLAVE_LOST)
  186. {
  187. // unload pse-op enclave
  188. unload_enclave();
  189. // Return PSE_OP_EPHEMERAL_SESSION_INVALID to trigger ephemeral session re-establishment
  190. return PSE_OP_EPHEMERAL_SESSION_INVALID;
  191. }
  192. if(AE_SUCCESS != (ret2=sgx_error_to_ae_error(status)))
  193. return ret2;
  194. return ret;
  195. }
  196. //if ok return 0
  197. ae_error_t CPSEClass::exchange_report(
  198. uint32_t session_id,
  199. uint8_t* se_dh_msg2, uint32_t se_dh_msg2_size,
  200. uint8_t* se_dh_msg3, uint32_t se_dh_msg3_size
  201. )
  202. {
  203. sgx_status_t status = SGX_SUCCESS;
  204. ae_error_t ret = AE_SUCCESS;
  205. ae_error_t ret2 = AE_SUCCESS;
  206. if(!m_enclave_id)
  207. return AE_FAILURE;
  208. if(sizeof(sgx_dh_msg2_t) != se_dh_msg2_size)
  209. return PSE_OP_PARAMETER_ERROR;
  210. if(sizeof(pse_dh_msg3_t) != se_dh_msg3_size)
  211. return PSE_OP_PARAMETER_ERROR;
  212. uint64_t sys_tick = se_get_tick_count();
  213. uint64_t milliseconds = (uint64_t)(sys_tick * 1000.0 / m_freq +0.5);
  214. //ECall
  215. status = exchange_report_wrapper(m_enclave_id, &ret,
  216. milliseconds,
  217. session_id,
  218. (sgx_dh_msg2_t*)se_dh_msg2,
  219. (pse_dh_msg3_t*)se_dh_msg3);
  220. if (status == SGX_ERROR_ENCLAVE_LOST)
  221. {
  222. // unload pse-op enclave
  223. unload_enclave();
  224. // Return PSE_OP_EPHEMERAL_SESSION_INVALID to trigger ephemeral session re-establishment
  225. return PSE_OP_EPHEMERAL_SESSION_INVALID;
  226. }
  227. if(AE_SUCCESS != (ret2 = sgx_error_to_ae_error(status)))
  228. return ret2;
  229. return ret;
  230. }
  231. //if ok return 0
  232. ae_error_t CPSEClass::close_session(uint32_t session_id)
  233. {
  234. sgx_status_t status = SGX_SUCCESS;
  235. ae_error_t ret = AE_SUCCESS;
  236. ae_error_t ret2 = AE_SUCCESS;
  237. if(!m_enclave_id)
  238. return AE_FAILURE;
  239. // ECall
  240. status = close_session_wrapper(m_enclave_id,&ret,session_id);
  241. if (status == SGX_ERROR_ENCLAVE_LOST)
  242. {
  243. // unload pse-op enclave
  244. unload_enclave();
  245. // Return PSE_OP_EPHEMERAL_SESSION_INVALID to trigger ephemeral session re-establishment
  246. return PSE_OP_EPHEMERAL_SESSION_INVALID;
  247. }
  248. if(AE_SUCCESS != (ret2=sgx_error_to_ae_error(status)))
  249. return ret2;
  250. return ret;
  251. }
  252. //if ok return 0
  253. ae_error_t CPSEClass::invoke_service(
  254. uint8_t* pse_message_req, size_t pse_message_req_size,
  255. uint8_t* pse_message_resp, size_t pse_message_resp_size)
  256. {
  257. sgx_status_t status = SGX_SUCCESS;
  258. ae_error_t ret = AE_SUCCESS;
  259. ae_error_t ret2= AE_SUCCESS;
  260. if(!m_enclave_id)
  261. return AE_FAILURE;
  262. uint64_t sys_tick = se_get_tick_count();
  263. uint64_t milliseconds = (uint64_t)(sys_tick * 1000.0 / m_freq + 0.5);
  264. // ECall
  265. PROFILE_START("invoke_service_wrapper");
  266. status = invoke_service_wrapper(m_enclave_id,
  267. &ret,
  268. milliseconds,
  269. pse_message_req,
  270. (uint32_t)pse_message_req_size,
  271. pse_message_resp,
  272. (uint32_t)pse_message_resp_size);
  273. if (status == SGX_ERROR_ENCLAVE_LOST)
  274. {
  275. // unload pse-op enclave
  276. unload_enclave();
  277. // Return PSE_OP_EPHEMERAL_SESSION_INVALID to trigger ephemeral session re-establishment
  278. return PSE_OP_EPHEMERAL_SESSION_INVALID;
  279. }
  280. PROFILE_END("invoke_service_wrapper");
  281. if(AE_SUCCESS != (ret2=sgx_error_to_ae_error(status)))
  282. return ret2;
  283. return ret;
  284. }
  285. ae_error_t CPSEClass::get_ps_cap(uint64_t* ps_cap)
  286. {
  287. if (ps_cap == NULL)
  288. {
  289. AESM_DBG_ERROR("input ps_cap is NULL");
  290. return AE_FAILURE;
  291. }
  292. if (m_ps_cap != PS_CAP_NOT_AVAILABLE)
  293. {
  294. AESM_DBG_TRACE("ps_cap is available:%llu", m_ps_cap);
  295. *ps_cap = m_ps_cap;
  296. return AE_SUCCESS;
  297. }
  298. psda_info_query_msg_t psda_cap_query_msg;
  299. psda_cap_query_msg.msg_hdr.msg_type = _htonl(PSDA_MSG_TYPE_CAP_QUERY);
  300. psda_cap_query_msg.msg_hdr.msg_len = 0;
  301. psda_cap_result_msg_t psda_cap_result_msg;
  302. memset(&psda_cap_result_msg, 0, sizeof(psda_cap_result_msg_t));
  303. JVM_COMM_BUFFER commBuf;
  304. commBuf.TxBuf->buffer = &psda_cap_query_msg;
  305. commBuf.TxBuf->length = sizeof(psda_info_query_msg_t);
  306. commBuf.RxBuf->buffer = &psda_cap_result_msg;
  307. commBuf.RxBuf->length = sizeof(psda_cap_result_msg_t);
  308. int response_code;
  309. ae_error_t ret;
  310. ret = PSDAService::instance().send_and_recv(
  311. PSDA_COMMAND_INFO,
  312. &commBuf,
  313. &response_code,
  314. AUTO_RETRY_ON_SESSION_LOSS);
  315. if (ret != AE_SUCCESS)
  316. {
  317. AESM_DBG_ERROR("JHI_SendAndRecv2 returned (ae%d)",ret);
  318. AESM_LOG_ERROR_UNICODE("%s", g_event_string_table[SGX_EVENT_DAL_COMM_FAILURE]);
  319. return ret;
  320. }
  321. if (response_code != PSDA_SUCCESS)
  322. {
  323. AESM_DBG_ERROR("JHI_SendAndRecv2 response_code is %d", response_code);
  324. return AE_FAILURE;
  325. }
  326. if (_ntohl(psda_cap_result_msg.msg_hdr.msg_type) != PSDA_MSG_TYPE_CAP_RESULT
  327. || _ntohl(psda_cap_result_msg.msg_hdr.msg_len) != PSDA_CAP_RESULT_MSG_LEN) {
  328. AESM_DBG_ERROR("msg_type %d, msg_len %d while expected value type %d, len %d",
  329. _ntohl(psda_cap_result_msg.msg_hdr.msg_type), _ntohl(psda_cap_result_msg.msg_hdr.msg_len),
  330. PSDA_MSG_TYPE_CAP_RESULT, PSDA_CAP_RESULT_MSG_LEN);
  331. return AE_FAILURE;
  332. }
  333. if (_ntohl(psda_cap_result_msg.cap_descriptor_version) != 1)
  334. {
  335. return AE_FAILURE;
  336. }
  337. m_ps_cap = 0;
  338. uint32_t psda_cap0 = _ntohl(psda_cap_result_msg.cap_descriptor0);
  339. if (psda_cap0 & PSDA_CAP_PRTC)
  340. m_ps_cap |= PS_CAP_TRUSTED_TIME; // Trusted time service
  341. if (psda_cap0 & PSDA_CAP_PROTECTED_OUTPUT)
  342. m_ps_cap |= PS_CAP_PROTECTED_OUTPUT; // Trusted display key exchange
  343. if (psda_cap0 & PSDA_CAP_RPDATA) // RPDATA capbility is available
  344. m_ps_cap |= PS_CAP_MONOTONIC_COUNTER; // Monotonic counter service
  345. *ps_cap = m_ps_cap;
  346. return AE_SUCCESS;
  347. }
  348. /**
  349. * @brief Establish an ephemeral session between PSE and CSE if not established yet.
  350. *
  351. * @param is_new_pairing
  352. * @param redo
  353. *
  354. * @return SGX_SUCCESS for success. Other values indicate an error.
  355. */
  356. ae_error_t CPSEClass::create_ephemeral_session_pse_cse(bool is_new_pairing, bool redo)
  357. {
  358. ae_error_t ret = AE_FAILURE;
  359. pse_cse_msg2_t msg2;
  360. pse_cse_msg3_t msg3;
  361. pse_cse_msg4_t msg4;
  362. uint32_t blob_size;
  363. uint8_t* p_sealed_buffer = NULL;
  364. sgx_status_t status = SGX_SUCCESS;
  365. sgx_status_t stat_initdb = SGX_SUCCESS;
  366. int retry = 0;
  367. if (m_status == PSE_STATUS_INIT ||
  368. m_status == PSE_STATUS_UNAVAILABLE)
  369. {
  370. // CSE provisioning failed during initialization.
  371. PlatformServiceStatus::instance().set_platform_service_status(PLATFORM_SERVICE_NOT_AVAILABLE);
  372. return AE_FAILURE;
  373. }
  374. if (!redo)
  375. {
  376. if (m_status == PSE_STATUS_SERVICE_READY) {
  377. PlatformServiceStatus::instance().set_platform_service_status(PLATFORM_SERVICE_READY);
  378. return AE_SUCCESS;
  379. }
  380. }
  381. else
  382. {
  383. // invalidate current session
  384. m_status = PSE_STATUS_CSE_PROVISIONED;
  385. }
  386. // Set to NOT_READY at the beginning.
  387. PlatformServiceStatus::instance().set_platform_service_status(PLATFORM_SERVICE_NOT_READY);
  388. do
  389. {
  390. AESM_DBG_INFO("PSDA started");
  391. // Check LT pairing blob first
  392. blob_size = sizeof(pairing_blob_t);
  393. p_sealed_buffer = (uint8_t*)malloc(blob_size);
  394. if (p_sealed_buffer == NULL)
  395. {
  396. return AE_FAILURE;
  397. }
  398. PROFILE_START("aesm_read_data");
  399. // read sealed blob from persistent storage
  400. ret = aesm_read_data(FT_PERSISTENT_STORAGE, PSE_PR_LT_PAIRING_FID, p_sealed_buffer, &blob_size);
  401. PROFILE_END("aesm_read_data");
  402. if (ret != AE_SUCCESS||blob_size!=sizeof(pairing_blob_t))
  403. {
  404. // Failed to load LT sealed blob
  405. ret = PSE_PAIRING_BLOB_INVALID_ERROR;
  406. // unload pse_op enclave
  407. unload_enclave();
  408. // load pse_pr enclave
  409. CPSEPRClass::instance().load_enclave();
  410. break;
  411. }
  412. AESM_DBG_INFO("LT Paring Blob read");
  413. // load pse-op enclave if it's not loaded yet
  414. if ((ret = load_enclave()) != AE_SUCCESS)
  415. break;
  416. do
  417. {
  418. // create PSDA session if not available
  419. if (!PSDAService::instance().start_service()) {
  420. PlatformServiceStatus::instance().set_platform_service_status(PLATFORM_SERVICE_NOT_AVAILABLE);
  421. ret = AE_FAILURE;
  422. break;
  423. }
  424. if(status == SGX_ERROR_ENCLAVE_LOST)
  425. {
  426. unload_enclave();
  427. // Reload an AE will not fail because of out of EPC, so AESM_AE_OUT_OF_EPC is not checked here
  428. if(AE_SUCCESS != load_enclave())
  429. {
  430. ret = AE_FAILURE;
  431. break;
  432. }
  433. }
  434. AESM_DBG_INFO("PSDA Start Ephemral Session");
  435. // PSE --- M1:StartSession ---> CSE
  436. memset(&msg2, 0, sizeof(msg2));
  437. PROFILE_START("psda_start_ephemeral_session");
  438. ret = psda_start_ephemeral_session(((pairing_blob_t*)p_sealed_buffer)->plaintext.pse_instance_id, &msg2);
  439. PROFILE_END("psda_start_ephemeral_session");
  440. if (ret != AE_SUCCESS)
  441. break;
  442. AESM_DBG_INFO("Ephemral Session M2/M3");
  443. // PSE <--- M2 --- CSE
  444. memset(&msg3, 0, sizeof(msg3));
  445. PROFILE_START("ephemeral_session_m2m3_wrapper");
  446. status = ephemeral_session_m2m3_wrapper(m_enclave_id, &ret, (pairing_blob_t*)p_sealed_buffer, &msg2, &msg3);
  447. PROFILE_END("ephemeral_session_m2m3_wrapper");
  448. CHECK_ECALL_RET(status, ret)
  449. AESM_DBG_INFO("PSDA Finalize Session");
  450. // PSE --- M3 ---> CSE
  451. memset(&msg4, 0, sizeof(msg4));
  452. PROFILE_START("psda_finalize_session");
  453. ret = psda_finalize_session(((pairing_blob_t*)p_sealed_buffer)->plaintext.pse_instance_id, &msg3, &msg4);
  454. PROFILE_END("psda_finalize_session");
  455. if (ret == AESM_PSDA_SESSION_LOST)
  456. {
  457. retry++;
  458. continue;
  459. }
  460. BREAK_IF_FAILED(ret);
  461. AESM_DBG_INFO("Ephemeral Session M4");
  462. // PSE <--- M4 --- CSE
  463. PROFILE_START("ephemeral_session_m4_wrapper");
  464. status = ephemeral_session_m4_wrapper(m_enclave_id, &ret, &msg4);
  465. PROFILE_END("ephemeral_session_m4_wrapper");
  466. CHECK_ECALL_RET(status, ret)
  467. /* the return value of initialize_sqlite_database_file_wrapper is ignored unless it's SGX_ERROR_ENCLAVE_LOST */
  468. AESM_DBG_INFO("initialize vmc database");
  469. PROFILE_START("initialize_sqlite_database_file_wrapper");
  470. ae_error_t ret2;
  471. stat_initdb = initialize_sqlite_database_file_wrapper(m_enclave_id, &ret2, is_new_pairing);
  472. PROFILE_END("initialize_sqlite_database_file_wrapper");
  473. if (stat_initdb == SGX_ERROR_ENCLAVE_LOST)
  474. {
  475. retry++;
  476. continue;
  477. }
  478. else break;
  479. }while(retry < AESM_RETRY_COUNT);
  480. if (status == SGX_ERROR_ENCLAVE_LOST)
  481. {
  482. AESM_DBG_INFO("Enclave Lost");
  483. // maximum retry times reached
  484. ret = AE_FAILURE;
  485. break;
  486. }
  487. else if(ret == AE_SUCCESS)
  488. {
  489. // Set status to READY
  490. m_status = PSE_STATUS_SERVICE_READY;
  491. // Successfully build the ephemeral session
  492. PlatformServiceStatus::instance().set_platform_service_status(PLATFORM_SERVICE_READY);
  493. }
  494. } while(0);
  495. free(p_sealed_buffer);
  496. return ret;
  497. }
  498. ae_error_t CPSEClass::psda_start_ephemeral_session(const uint8_t* pse_instance_id,
  499. pse_cse_msg2_t* cse_msg2)
  500. {
  501. assert(cse_msg2 != NULL);
  502. AESM_DBG_INFO("Enter psda_start_ephemeral_session ...");
  503. eph_session_m1_t eph_session_m1;
  504. memcpy_s(eph_session_m1.msg_hdr.pse_instance_id, SW_INSTANCE_ID_SIZE, pse_instance_id, SW_INSTANCE_ID_SIZE);
  505. eph_session_m1.msg_hdr.msg_type = _htonl(PSDA_MSG_TYPE_EP_M1);
  506. eph_session_m1.msg_hdr.msg_len = 0;
  507. eph_session_m2_t eph_session_m2;
  508. memset(&eph_session_m2, 0, sizeof(eph_session_m2));
  509. JVM_COMM_BUFFER commBuf;
  510. commBuf.TxBuf->buffer = &eph_session_m1;
  511. commBuf.TxBuf->length = sizeof(eph_session_m1_t);
  512. commBuf.RxBuf->buffer = &eph_session_m2;
  513. commBuf.RxBuf->length = sizeof(eph_session_m2_t);
  514. int response_code;
  515. ae_error_t ret;
  516. ret = PSDAService::instance().send_and_recv(
  517. PSDA_COMMAND_EP,
  518. &commBuf,
  519. &response_code,
  520. AUTO_RETRY_ON_SESSION_LOSS);
  521. if (ret != AE_SUCCESS ) {
  522. AESM_LOG_ERROR_UNICODE("%s", g_event_string_table[SGX_EVENT_DAL_COMM_FAILURE]);
  523. return ret;
  524. }
  525. if (response_code == PSDA_LT_PAIRING_NOT_EXIST
  526. || response_code == PSDA_INTEGRITY_ERROR)
  527. {
  528. return AESM_PSDA_NEED_REPAIRING;
  529. }
  530. else if (response_code != PSDA_SUCCESS
  531. || _ntohl(eph_session_m2.msg_hdr.msg_type) != PSDA_MSG_TYPE_EP_M2
  532. || _ntohl(eph_session_m2.msg_hdr.msg_len) != sizeof(pse_cse_msg2_t)) {
  533. AESM_DBG_ERROR("JHI_SendAndRecv2 response_code is %d", response_code);
  534. return AE_FAILURE;
  535. }
  536. memcpy_s(cse_msg2, sizeof(pse_cse_msg2_t), &eph_session_m2.msg2, sizeof(pse_cse_msg2_t));
  537. return AE_SUCCESS;
  538. }
  539. ae_error_t CPSEClass::psda_finalize_session(const uint8_t* pse_instance_id,
  540. const pse_cse_msg3_t* cse_msg3,
  541. pse_cse_msg4_t* cse_msg4)
  542. {
  543. assert(cse_msg3 != NULL && cse_msg4 != NULL);
  544. AESM_DBG_INFO("Enter psda_finalize_session ...");
  545. eph_session_m3_t eph_session_m3;
  546. memcpy_s(eph_session_m3.msg_hdr.pse_instance_id, SW_INSTANCE_ID_SIZE, pse_instance_id, SW_INSTANCE_ID_SIZE);
  547. eph_session_m3.msg_hdr.msg_type = _htonl(PSDA_MSG_TYPE_EP_M3);
  548. eph_session_m3.msg_hdr.msg_len = _htonl(sizeof(pse_cse_msg3_t));
  549. memcpy_s(&eph_session_m3.msg3, sizeof(eph_session_m3.msg3), cse_msg3, sizeof(pse_cse_msg3_t));
  550. eph_session_m4_t eph_session_m4;
  551. memset(&eph_session_m4, 0, sizeof(eph_session_m4));
  552. JVM_COMM_BUFFER commBuf;
  553. commBuf.TxBuf->buffer = &eph_session_m3;
  554. commBuf.TxBuf->length = sizeof(eph_session_m3_t);
  555. commBuf.RxBuf->buffer = &eph_session_m4;
  556. commBuf.RxBuf->length = sizeof(eph_session_m4_t);
  557. int response_code;
  558. ae_error_t ret;
  559. ret = PSDAService::instance().send_and_recv(
  560. PSDA_COMMAND_EP,
  561. &commBuf,
  562. &response_code,
  563. NO_RETRY_ON_SESSION_LOSS);
  564. if (ret != AE_SUCCESS)
  565. {
  566. AESM_LOG_ERROR_UNICODE("%s", g_event_string_table[SGX_EVENT_DAL_COMM_FAILURE]);
  567. return ret;
  568. }
  569. if (response_code == PSDA_INTEGRITY_ERROR)
  570. {
  571. return AESM_PSDA_NEED_REPAIRING;
  572. }
  573. else if (response_code != PSDA_SUCCESS
  574. || _ntohl(eph_session_m4.msg_hdr.msg_type) != PSDA_MSG_TYPE_EP_M4
  575. || _ntohl(eph_session_m4.msg_hdr.msg_len) != sizeof(pse_cse_msg4_t)) {
  576. AESM_DBG_ERROR("JHI_SendAndRecv2 response_code is %d", response_code);
  577. return AE_FAILURE;
  578. }
  579. memcpy_s(cse_msg4, sizeof(pse_cse_msg4_t), &eph_session_m4.msg4, sizeof(pse_cse_msg4_t));
  580. return AE_SUCCESS;
  581. }
  582. ae_error_t CPSEClass::psda_invoke_service(uint8_t* psda_req_msg, uint32_t psda_req_msg_size,
  583. uint8_t* psda_resp_msg, uint32_t psda_resp_msg_size)
  584. {
  585. AESM_DBG_INFO("Enter psda_invoke_service ...");
  586. JVM_COMM_BUFFER commBuf;
  587. commBuf.TxBuf->buffer = psda_req_msg;
  588. commBuf.TxBuf->length = psda_req_msg_size;
  589. commBuf.RxBuf->buffer = psda_resp_msg;
  590. commBuf.RxBuf->length = psda_resp_msg_size;
  591. int response_code;
  592. ae_error_t ret;
  593. PROFILE_START("JHI_SendAndRecv2");
  594. ret = PSDAService::instance().send_and_recv(
  595. PSDA_COMMAND_SERVICE,
  596. &commBuf,
  597. &response_code,
  598. NO_RETRY_ON_SESSION_LOSS);
  599. PROFILE_END("JHI_SendAndRecv2");
  600. if (ret != AE_SUCCESS) {
  601. AESM_LOG_ERROR_UNICODE("%s", g_event_string_table[SGX_EVENT_DAL_COMM_FAILURE]);
  602. return ret;
  603. }
  604. if (PSDA_SUCCESS != response_code) {
  605. AESM_LOG_ERROR_UNICODE("%s", g_event_string_table[SGX_EVENT_DAL_SERVICE_ERROR]);
  606. }
  607. AESM_DBG_INFO("JHI_SendAndRecv2 response_code is %d", response_code);
  608. switch (response_code)
  609. {
  610. case PSDA_SUCCESS: // SGX Platform Service Message form PSE processed successfully
  611. return AE_SUCCESS;
  612. case PSDA_INTERNAL_ERROR: // Internal error, possibly due to unexpected error of the system
  613. return AESM_PSDA_INTERNAL_ERROR;
  614. case PSDA_INVALID_SESSION_STATE: // SGX Platform Service ephemeral session state is invalid
  615. case PSDA_SEQNO_CHECK_FAIL: // SGX Platform Service secure channel message sequence number check failure
  616. case PSDA_INTEGRITY_ERROR: // SGX Platform Service message crypto verification failure
  617. case PSDA_LT_PAIRING_NOT_EXIST: // SGX Platform Service long term pairing session doesn't exist
  618. return AESM_PSDA_NEED_REPAIRING;
  619. case PSDA_INVALID_COMMAND: // SGX Platform Service PS_COMMAND_ID provided by the transport layer is not recognized
  620. case PSDA_BAD_PARAMETER: // SGX Platform Service Message format error detected
  621. default:
  622. return AE_FAILURE;
  623. }
  624. }