aesm_logic.cpp 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  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 "QEClass.h"
  32. #include "LEClass.h"
  33. #include "PSEClass.h"
  34. #include "PVEClass.h"
  35. #include "PCEClass.h"
  36. #include "PSEPRClass.h"
  37. #include "arch.h"
  38. #include "sgx_report.h"
  39. #include "sgx_tseal.h"
  40. #include "epid_pve_type.h"
  41. #include "util.h"
  42. #include <assert.h>
  43. #include <time.h>
  44. #include "oal/oal.h"
  45. #include "oal/aesm_thread.h"
  46. #include "aesm_encode.h"
  47. #include "pairing_blob.h"
  48. #include "aesm_epid_blob.h"
  49. #include "aesm_xegd_blob.h"
  50. #include "aesm_logic.h"
  51. #include "pve_logic.h"
  52. #include "qe_logic.h"
  53. #include "pse_op_logic.h"
  54. #include "platform_info_logic.h"
  55. #include "prov_msg_size.h"
  56. #include "se_sig_rl.h"
  57. #include "se_quote_internal.h"
  58. #include "endpoint_select_info.h"
  59. #include "se_wrapper.h"
  60. #include "PSDAService.h"
  61. #include "ippcp.h"
  62. #include <time.h>
  63. #include <string>
  64. #include "prof_fun.h"
  65. #include "upse/helper.h"
  66. #include "aesm_long_lived_thread.h"
  67. #include "sgx_profile.h"
  68. #include "service_enclave_mrsigner.hh"
  69. #define __STDC_FORMAT_MACROS
  70. #include <inttypes.h>
  71. #define CHECK_SERVICE_STATUS if (!is_service_running()) return AESM_SERVICE_STOPPED;
  72. #define CHECK_SGX_STATUS if (g_sgx_device_status != SGX_ENABLED) return AESM_SGX_DEVICE_NOT_AVAILABLE;
  73. AESMLogicMutex AESMLogic::_qe_pve_mutex;
  74. AESMLogicMutex AESMLogic::_pse_mutex;
  75. AESMLogicMutex AESMLogic::_le_mutex;
  76. bool AESMLogic::_is_qe_psvn_set;
  77. bool AESMLogic::_is_pse_psvn_set;
  78. bool AESMLogic::_is_pce_psvn_set;
  79. psvn_t AESMLogic::_qe_psvn;
  80. psvn_t AESMLogic::_pce_psvn;
  81. psvn_t AESMLogic::_pse_psvn;
  82. aesm_thread_t AESMLogic::qe_thread = NULL;
  83. aesm_thread_t AESMLogic::pse_thread = NULL;
  84. uint32_t AESMLogic::active_extended_epid_group_id;
  85. extern "C" bool is_in_kernel_driver();
  86. static ae_error_t read_global_extended_epid_group_id(uint32_t *xeg_id)
  87. {
  88. char path_name[MAX_PATH];
  89. ae_error_t ae_ret = aesm_get_pathname(FT_PERSISTENT_STORAGE, EXTENDED_EPID_GROUP_ID_FID, path_name, MAX_PATH);
  90. if(AE_SUCCESS != ae_ret){
  91. return ae_ret;
  92. }
  93. FILE * f = fopen(path_name, "r");
  94. if( f == NULL){
  95. return OAL_CONFIG_FILE_ERROR;
  96. }
  97. ae_ret = OAL_CONFIG_FILE_ERROR;
  98. if(fscanf(f, "%" SCNu32, xeg_id)==1){
  99. ae_ret = AE_SUCCESS;
  100. }
  101. fclose(f);
  102. return ae_ret;
  103. }
  104. static ae_error_t set_global_extended_epid_group_id(uint32_t xeg_id)
  105. {
  106. char path_name[MAX_PATH];
  107. ae_error_t ae_ret = aesm_get_pathname(FT_PERSISTENT_STORAGE, EXTENDED_EPID_GROUP_ID_FID, path_name, MAX_PATH);
  108. if(AE_SUCCESS != ae_ret){
  109. return ae_ret;
  110. }
  111. FILE *f = fopen(path_name, "w");
  112. if(f == NULL){
  113. return OAL_CONFIG_FILE_ERROR;
  114. }
  115. ae_ret = OAL_CONFIG_FILE_ERROR;
  116. if(fprintf(f, "%" PRIu32, xeg_id)>0){
  117. ae_ret = AE_SUCCESS;
  118. }
  119. fclose(f);
  120. return ae_ret;
  121. }
  122. uint32_t AESMLogic::get_active_extended_epid_group_id()
  123. {
  124. return active_extended_epid_group_id;
  125. }
  126. static ae_error_t thread_to_load_qe(aesm_thread_arg_type_t arg)
  127. {
  128. epid_blob_with_cur_psvn_t epid_data;
  129. ae_error_t ae_ret = AE_FAILURE;
  130. UNUSED(arg);
  131. AESM_DBG_TRACE("start to load qe");
  132. memset(&epid_data, 0, sizeof(epid_data));
  133. AESMLogicLock lock(AESMLogic::_qe_pve_mutex);
  134. if((ae_ret = EPIDBlob::instance().read(epid_data)) == AE_SUCCESS)
  135. {
  136. AESM_DBG_TRACE("EPID blob is read successfully, loading QE ...");
  137. ae_ret = CQEClass::instance().load_enclave();
  138. if(AE_SUCCESS != ae_ret)
  139. {
  140. AESM_DBG_WARN("fail to load QE: %d", ae_ret);
  141. }else{
  142. AESM_DBG_TRACE("QE loaded successfully");
  143. bool resealed = false;
  144. se_static_assert(SGX_TRUSTED_EPID_BLOB_SIZE_SDK>=SGX_TRUSTED_EPID_BLOB_SIZE_SIK);
  145. // Just take this chance to reseal EPID blob in case TCB is
  146. // upgraded, return value is ignored and no provisioning is
  147. // triggered.
  148. ae_ret = static_cast<ae_error_t>(CQEClass::instance().verify_blob(
  149. epid_data.trusted_epid_blob,
  150. SGX_TRUSTED_EPID_BLOB_SIZE_SDK,
  151. &resealed));
  152. if(AE_SUCCESS != ae_ret)
  153. {
  154. AESM_DBG_WARN("Failed to verify EPID blob: %d", ae_ret);
  155. EPIDBlob::instance().remove();
  156. }else{
  157. uint32_t epid_xeid;
  158. // Check whether EPID blob XEGDID is aligned with active extended group id if it exists.
  159. if ((EPIDBlob::instance().get_extended_epid_group_id(&epid_xeid) == AE_SUCCESS) && (epid_xeid == AESMLogic::get_active_extended_epid_group_id())) {
  160. AESM_DBG_TRACE("EPID blob Verified");
  161. // XEGDID is aligned
  162. if (true == resealed)
  163. {
  164. AESM_DBG_TRACE("EPID blob is resealed");
  165. if ((ae_ret = EPIDBlob::instance().write(epid_data))
  166. != AE_SUCCESS)
  167. {
  168. AESM_DBG_WARN("Failed to update epid blob: %d", ae_ret);
  169. }
  170. }
  171. }
  172. else { // XEGDID is NOT aligned
  173. AESM_DBG_TRACE("XEGDID mismatch in EPIDBlob, remove it...");
  174. EPIDBlob::instance().remove();
  175. }
  176. }
  177. }
  178. }else{
  179. AESM_DBG_TRACE("Fail to read EPID Blob");
  180. }
  181. AESM_DBG_TRACE("QE Thread finished succ");
  182. return AE_SUCCESS;
  183. }
  184. static ae_error_t thread_to_init_pse(aesm_thread_arg_type_t arg)
  185. {
  186. UNUSED(arg);
  187. AESM_DBG_INFO("start to init_ps");
  188. AESMLogicLock lock(AESMLogic::_pse_mutex);
  189. ae_error_t psError = CPSEClass::instance().init_ps();
  190. UNUSED(psError);// To ignore the error that the symbol not used in release mode
  191. AESM_DBG_INFO("init_ps return ( ae %d)", psError);
  192. return AE_SUCCESS;
  193. }
  194. // Must be called when AESM starts
  195. ae_error_t AESMLogic::service_start()
  196. {
  197. AESM_PROFILE_INIT;
  198. ae_error_t ae_ret = AE_SUCCESS;
  199. AESM_LOG_INIT();
  200. #ifdef SGX_USE_OPT_LIB
  201. ippcpInit();
  202. #endif
  203. AESM_DBG_INFO("aesm service is starting");
  204. // Try to read active extended epid group id from data file
  205. ae_ret = read_global_extended_epid_group_id(&AESMLogic::active_extended_epid_group_id);
  206. if (AE_SUCCESS != ae_ret){
  207. AESM_DBG_INFO("Fail to read extended epid group id, default extended epid group used");
  208. AESMLogic::active_extended_epid_group_id = DEFAULT_EGID; // Use default extended epid group id 0 if it is not available from data file
  209. }
  210. else{
  211. AESM_DBG_INFO("active extended group id %d used", AESMLogic::active_extended_epid_group_id);
  212. }
  213. if (AE_SUCCESS != (XEGDBlob::verify_xegd_by_xgid(active_extended_epid_group_id)) ||
  214. AE_SUCCESS != (EndpointSelectionInfo::verify_file_by_xgid(active_extended_epid_group_id))){// Try to load XEGD and URL file to make sure it is valid
  215. AESM_LOG_WARN_ADMIN("%s", g_admin_event_string_table[SGX_ADMIN_EVENT_PCD_NOT_AVAILABLE]);
  216. AESM_LOG_WARN("%s: original extended epid group id = %d", g_event_string_table[SGX_EVENT_PCD_NOT_AVAILABLE], AESMLogic::active_extended_epid_group_id);
  217. AESMLogic::active_extended_epid_group_id = DEFAULT_EGID;// If the active extended epid group id read from data file is not valid, switch to default extended epid group id
  218. }
  219. ae_ret = CLEClass::instance().load_enclave();
  220. if(AE_SUCCESS != ae_ret)
  221. {
  222. AESM_DBG_INFO("fail to load LE: %d", ae_ret);
  223. AESM_LOG_FATAL("%s", g_event_string_table[SGX_EVENT_SERVICE_UNAVAILABLE]);
  224. return ae_ret;
  225. }
  226. ae_error_t aesm_ret1 = aesm_create_thread(thread_to_load_qe, 0,&qe_thread);
  227. ae_error_t aesm_ret2 = aesm_create_thread(thread_to_init_pse, 0, &pse_thread);
  228. if(AE_SUCCESS != aesm_ret1 ){
  229. AESM_DBG_WARN("Fail to create thread to preload QE:(ae %d)",aesm_ret1);
  230. }
  231. if(AE_SUCCESS != aesm_ret2 ){
  232. AESM_DBG_WARN("Fail to create thread to init PSE:( ae %d)",aesm_ret2);
  233. }
  234. start_white_list_thread();
  235. AESM_DBG_TRACE("aesm service is started");
  236. return AE_SUCCESS;
  237. }
  238. void AESMLogic::service_stop()
  239. {
  240. ae_error_t ae_ret, thread_ret;
  241. // Waiting for QE and PSE thread
  242. ae_ret = aesm_wait_thread(qe_thread, &thread_ret, AESM_STOP_TIMEOUT);
  243. if (ae_ret != AE_SUCCESS || thread_ret != AE_SUCCESS)
  244. {
  245. AESM_DBG_INFO("aesm_wait_thread failed(qe_thread):(ae %d) (%d)", ae_ret, thread_ret);
  246. }
  247. (void)aesm_free_thread(qe_thread);//release thread handle to free memory
  248. ae_ret = aesm_wait_thread(pse_thread, &thread_ret, AESM_STOP_TIMEOUT);
  249. if (ae_ret != AE_SUCCESS || thread_ret != AE_SUCCESS)
  250. {
  251. AESM_DBG_INFO("aesm_wait_thread failed(pse_thread):(ae %d) (%d)", ae_ret, thread_ret);
  252. }
  253. (void)aesm_free_thread(pse_thread);//release thread handle to free memory
  254. //waiting for pending threads util timeout
  255. stop_all_long_lived_threads(0);//waiting for pending threads util timeout
  256. CPVEClass::instance().unload_enclave();
  257. CPCEClass::instance().unload_enclave();
  258. CPSEClass::instance().unload_enclave();
  259. CQEClass::instance().unload_enclave();
  260. CLEClass::instance().unload_enclave();
  261. CPSEPRClass::instance().unload_enclave();
  262. AESM_DBG_INFO("start to stop psda service");
  263. PSDAService::instance().stop_service();
  264. AESM_DBG_INFO("aesm service down");
  265. AESM_LOG_FINI();
  266. AESM_PROFILE_OUTPUT;
  267. }
  268. bool AESMLogic::is_service_running()
  269. {
  270. return true;
  271. }
  272. ae_error_t AESMLogic::save_unverified_white_list(const uint8_t *white_list_cert, uint32_t white_list_cert_size)
  273. {
  274. wl_cert_chain_t old_cert;
  275. const wl_cert_chain_t *p_new_cert = reinterpret_cast<const wl_cert_chain_t *>(white_list_cert);
  276. uint32_t old_cert_size = sizeof(old_cert);
  277. memset(&old_cert, 0, sizeof(old_cert));
  278. if((aesm_read_data(FT_PERSISTENT_STORAGE, AESM_WHITE_LIST_CERT_TO_BE_VERIFY_FID, reinterpret_cast<uint8_t *>(&old_cert), &old_cert_size) == AE_SUCCESS)
  279. && (old_cert_size == sizeof(old_cert)) && (white_list_cert_size >= sizeof(wl_cert_chain_t)))
  280. {
  281. if(_ntohl(p_new_cert->wl_cert.wl_version) <= _ntohl(old_cert.wl_cert.wl_version))
  282. {
  283. AESM_DBG_WARN("White list version downgraded! current version is %d, new version is %d",
  284. _ntohl(old_cert.wl_cert.wl_version), _ntohl(p_new_cert->wl_cert.wl_version));
  285. return OAL_PARAMETER_ERROR; // OAL_PARAMETER_ERROR used here is to indicate the white list is incorrect
  286. }
  287. }
  288. return aesm_write_data(FT_PERSISTENT_STORAGE, AESM_WHITE_LIST_CERT_TO_BE_VERIFY_FID, white_list_cert, white_list_cert_size);
  289. }
  290. aesm_error_t AESMLogic::white_list_register(
  291. const uint8_t *white_list_cert, uint32_t white_list_cert_size)
  292. {
  293. AESM_DBG_INFO("enter function");
  294. AESM_LOG_INFO_ADMIN("%s", g_admin_event_string_table[SGX_ADMIN_EVENT_WL_UPDATE_START]);
  295. CHECK_SERVICE_STATUS;
  296. AESMLogicLock lock(_le_mutex);
  297. CHECK_SERVICE_STATUS;
  298. ae_error_t ret_le = AE_SUCCESS;
  299. aesm_error_t ret = AESM_UNEXPECTED_ERROR;
  300. if (NULL == white_list_cert||0==white_list_cert_size){
  301. AESM_DBG_TRACE("Invalid parameter");
  302. AESM_LOG_ERROR_ADMIN("%s", g_admin_event_string_table[SGX_ADMIN_EVENT_WL_UPDATE_FAIL]);
  303. return AESM_PARAMETER_ERROR;
  304. }
  305. if (is_in_kernel_driver())
  306. {
  307. AESM_DBG_INFO("InKernel LE loaded");
  308. return AESM_SERVICE_UNAVAILABLE;
  309. }
  310. ae_error_t ae_ret = CLEClass::instance().load_enclave();
  311. if (AE_FAILED(ae_ret))
  312. {
  313. switch (ae_ret)
  314. {
  315. case AE_SERVER_NOT_AVAILABLE:
  316. AESM_DBG_WARN("LE not loaded due to AE_SERVER_NOT_AVAILABLE, possible SGX Env Not Ready");
  317. ret_le = save_unverified_white_list(white_list_cert, white_list_cert_size);
  318. break;
  319. case AESM_AE_OUT_OF_EPC:
  320. AESM_DBG_WARN("LE not loaded due to out of EPC");
  321. ret = AESM_OUT_OF_EPC;
  322. goto exit;
  323. default:
  324. AESM_DBG_ERROR("LE not loaded:(ae%d)", ae_ret);
  325. ret = AESM_UNEXPECTED_ERROR;
  326. goto exit;
  327. }
  328. }
  329. else
  330. {
  331. ret_le = static_cast<ae_error_t>(CLEClass::instance().white_list_register(
  332. white_list_cert, white_list_cert_size));
  333. }
  334. switch (ret_le)
  335. {
  336. case AE_SUCCESS:
  337. ret = AESM_SUCCESS;
  338. break;
  339. case LE_INVALID_PARAMETER:
  340. AESM_DBG_TRACE("Invalid parameter");
  341. ret = AESM_PARAMETER_ERROR;
  342. break;
  343. default:
  344. AESM_DBG_WARN("unexpeted error (ae %d)", ret_le);
  345. ret = AESM_UNEXPECTED_ERROR;
  346. break;
  347. }
  348. exit:
  349. // Always log success or failure to the Admin log before returning
  350. if (AE_FAILED(ae_ret) || AE_FAILED(ret_le)) {
  351. AESM_LOG_ERROR_ADMIN("%s", g_admin_event_string_table[SGX_ADMIN_EVENT_WL_UPDATE_FAIL]);
  352. } else {
  353. const wl_cert_chain_t* wl = reinterpret_cast<const wl_cert_chain_t*>(white_list_cert);
  354. AESM_LOG_INFO_ADMIN("%s for Version: %d", g_admin_event_string_table[SGX_ADMIN_EVENT_WL_UPDATE_SUCCESS],
  355. _ntohl(wl->wl_cert.wl_version));
  356. }
  357. return ret;
  358. }
  359. aesm_error_t AESMLogic::get_launch_token(
  360. const uint8_t * mrenclave, uint32_t mrenclave_size,
  361. const uint8_t *public_key, uint32_t public_key_size,
  362. const uint8_t *se_attributes, uint32_t se_attributes_size,
  363. uint8_t * lictoken, uint32_t lictoken_size)
  364. {
  365. AESM_DBG_INFO("enter function");
  366. CHECK_SERVICE_STATUS;
  367. AESMLogicLock lock(_le_mutex);
  368. CHECK_SERVICE_STATUS;
  369. ae_error_t ret_le = AE_SUCCESS;
  370. if (NULL == mrenclave ||
  371. NULL == public_key ||
  372. NULL == se_attributes ||
  373. NULL == lictoken)
  374. {
  375. // Sizes are checked in CLEClass::get_launch_token()
  376. AESM_DBG_TRACE("Invalid parameter");
  377. return AESM_PARAMETER_ERROR;
  378. }
  379. if (is_in_kernel_driver())
  380. {
  381. //Should not be called
  382. AESM_LOG_ERROR("InKernel LE loaded");
  383. return AESM_SERVICE_UNAVAILABLE;
  384. }
  385. ae_error_t ae_ret = CLEClass::instance().load_enclave();
  386. if(ae_ret == AE_SERVER_NOT_AVAILABLE)
  387. {
  388. AESM_LOG_ERROR("%s", g_event_string_table[SGX_EVENT_SERVICE_UNAVAILABLE]);
  389. AESM_DBG_FATAL("LE not loaded due to AE_SERVER_NOT_AVAILABLE, possible SGX Env Not Ready");
  390. return AESM_NO_DEVICE_ERROR;
  391. }
  392. else if(ae_ret == AESM_AE_OUT_OF_EPC)
  393. {
  394. AESM_DBG_ERROR("LE not loaded due to out of EPC");
  395. return AESM_OUT_OF_EPC;
  396. }
  397. else if(AE_FAILED(ae_ret))
  398. {
  399. AESM_DBG_ERROR("LE not loaded:%d", ae_ret);
  400. return AESM_SERVICE_UNAVAILABLE;
  401. }
  402. ret_le = static_cast<ae_error_t>(CLEClass::instance().get_launch_token(
  403. const_cast<uint8_t *>(mrenclave), mrenclave_size,
  404. const_cast<uint8_t *>(public_key), public_key_size,
  405. const_cast<uint8_t *>(se_attributes), se_attributes_size,
  406. lictoken, lictoken_size));
  407. switch (ret_le)
  408. {
  409. case AE_SUCCESS:
  410. return AESM_SUCCESS;
  411. case LE_INVALID_PARAMETER:
  412. AESM_DBG_TRACE("Invalid parameter");
  413. return AESM_PARAMETER_ERROR;
  414. case LE_INVALID_ATTRIBUTE:
  415. case LE_INVALID_PRIVILEGE_ERROR:
  416. AESM_DBG_TRACE("Launch token error");
  417. return AESM_GET_LICENSETOKEN_ERROR;
  418. case LE_WHITELIST_UNINITIALIZED_ERROR:
  419. AESM_DBG_TRACE("LE whitelist uninitialized error");
  420. return AESM_UNEXPECTED_ERROR;
  421. default:
  422. AESM_DBG_WARN("unexpeted error (ae %d)", ret_le);
  423. return AESM_UNEXPECTED_ERROR;
  424. }
  425. }
  426. /* This function will be called outside aesm(from urts_internal) */
  427. extern "C" sgx_status_t get_launch_token(const enclave_css_t* signature,
  428. const sgx_attributes_t* attribute,
  429. sgx_launch_token_t* launch_token)
  430. {
  431. AESM_DBG_INFO("enter function");
  432. return AESMLogic::get_launch_token(signature, attribute, launch_token);
  433. }
  434. ae_error_t AESMLogic::get_qe_isv_svn(uint16_t& isv_svn)
  435. {
  436. if(!_is_qe_psvn_set){
  437. ae_error_t ae_err = CQEClass::instance().load_enclave();
  438. if(AE_SUCCESS != ae_err){
  439. AESM_DBG_ERROR("Fail to load QE Enclave:(ae %d)",ae_err);
  440. return ae_err;
  441. }
  442. }
  443. assert(_is_qe_psvn_set);
  444. if(0!=memcpy_s(&isv_svn, sizeof(isv_svn), &_qe_psvn.isv_svn, sizeof(_qe_psvn.isv_svn))){
  445. AESM_DBG_ERROR("memcpy failed");
  446. return AE_FAILURE;
  447. }
  448. return AE_SUCCESS;
  449. }
  450. ae_error_t AESMLogic::get_pce_isv_svn(uint16_t& isv_svn)
  451. {
  452. if(!_is_pce_psvn_set){
  453. ae_error_t ae_err = CPCEClass::instance().load_enclave();
  454. if(AE_SUCCESS != ae_err){
  455. AESM_DBG_ERROR("Fail to load PCE Enclave:(ae %d)",ae_err);
  456. return ae_err;
  457. }
  458. }
  459. assert(_is_pce_psvn_set);
  460. if(0!=memcpy_s(&isv_svn, sizeof(isv_svn), &_pce_psvn.isv_svn, sizeof(_pce_psvn.isv_svn))){
  461. AESM_DBG_ERROR("memcpy failed");
  462. return AE_FAILURE;
  463. }
  464. return AE_SUCCESS;
  465. }
  466. ae_error_t AESMLogic::get_pse_isv_svn(uint16_t& isv_svn)
  467. {
  468. ae_error_t retVal = AE_SUCCESS;
  469. if(!_is_pse_psvn_set){
  470. ae_error_t ae_err = CPSEClass::instance().load_enclave();
  471. if(AE_SUCCESS != ae_err){
  472. AESM_DBG_ERROR("Fail to load PSEOP Enclave:(ae %d)",ae_err);
  473. retVal = ae_err;
  474. }
  475. }
  476. if (AE_SUCCESS == retVal) {
  477. assert(_is_pse_psvn_set);
  478. if(0!=memcpy_s(&isv_svn, sizeof(isv_svn), &_pse_psvn.isv_svn, sizeof(_pse_psvn.isv_svn))){
  479. AESM_DBG_ERROR("memcpy failed");
  480. retVal = AE_FAILURE;
  481. }
  482. }
  483. SGX_DBGPRINT_ONE_STRING_TWO_INTS_CREATE_SESSION(__FUNCTION__" returning ", retVal, isv_svn);
  484. return retVal;
  485. }
  486. ae_error_t AESMLogic::get_qe_cpu_svn(sgx_cpu_svn_t& cpu_svn)
  487. {
  488. if(!_is_qe_psvn_set){
  489. ae_error_t ae_err = CQEClass::instance().load_enclave();
  490. if(AE_SUCCESS != ae_err){
  491. AESM_DBG_ERROR("Fail to load QE Enclave:( ae %d)",ae_err);
  492. return ae_err;
  493. }
  494. }
  495. assert(_is_qe_psvn_set);
  496. if(0!=memcpy_s(&cpu_svn, sizeof(sgx_cpu_svn_t), &_qe_psvn.cpu_svn, sizeof(_qe_psvn.cpu_svn))){
  497. AESM_DBG_ERROR("memcpy failed");
  498. return AE_FAILURE;
  499. }
  500. return AE_SUCCESS;
  501. }
  502. ae_error_t AESMLogic::set_psvn(uint16_t prod_id, uint16_t isv_svn, sgx_cpu_svn_t cpu_svn, uint32_t mrsigner_index)
  503. {
  504. if(prod_id == QE_PROD_ID){
  505. if(mrsigner_index == AE_MR_SIGNER){
  506. if(_is_qe_psvn_set){
  507. if(0!=memcmp(&_qe_psvn.isv_svn, &isv_svn, sizeof(isv_svn))||
  508. 0!=memcmp(&_qe_psvn.cpu_svn, &cpu_svn, sizeof(sgx_cpu_svn_t))){
  509. AESM_DBG_ERROR("PSVN unmatched for QE/PVE");
  510. return AE_PSVN_UNMATCHED_ERROR;
  511. }
  512. }else{
  513. if(0!=memcpy_s(&_qe_psvn.isv_svn, sizeof(_qe_psvn.isv_svn), &isv_svn, sizeof(isv_svn))||
  514. 0!=memcpy_s(&_qe_psvn.cpu_svn, sizeof(_qe_psvn.cpu_svn), &cpu_svn, sizeof(sgx_cpu_svn_t))){
  515. AESM_DBG_ERROR("memcpy failed");
  516. return AE_FAILURE;
  517. }
  518. AESM_DBG_TRACE("get QE or PvE isv_svn=%d",(int)isv_svn);
  519. _is_qe_psvn_set = true;
  520. return AE_SUCCESS;
  521. }
  522. }else if(mrsigner_index==PCE_MR_SIGNER){
  523. if(_is_pce_psvn_set){
  524. if(0!=memcmp(&_pce_psvn.isv_svn, &isv_svn, sizeof(isv_svn))||
  525. 0!=memcmp(&_pce_psvn.cpu_svn, &cpu_svn, sizeof(sgx_cpu_svn_t))){
  526. AESM_DBG_ERROR("PSVN unmatched for PCE");
  527. return AE_PSVN_UNMATCHED_ERROR;
  528. }
  529. }else{
  530. if(0!=memcpy_s(&_pce_psvn.isv_svn, sizeof(_pce_psvn.isv_svn), &isv_svn, sizeof(isv_svn))||
  531. 0!=memcpy_s(&_pce_psvn.cpu_svn, sizeof(_pce_psvn.cpu_svn), &cpu_svn, sizeof(sgx_cpu_svn_t))){
  532. AESM_DBG_ERROR("memcpy failed");
  533. return AE_FAILURE;
  534. }
  535. AESM_DBG_TRACE("get PCE isv_svn=%d", (int)isv_svn);
  536. _is_pce_psvn_set = true;
  537. return AE_SUCCESS;
  538. }
  539. }
  540. }else if(prod_id == PSE_PROD_ID){
  541. if(mrsigner_index == AE_MR_SIGNER){
  542. if(_is_pse_psvn_set){
  543. if(0!=memcmp(&_pse_psvn.isv_svn, &isv_svn, sizeof(isv_svn))||
  544. 0!=memcmp(&_pse_psvn.cpu_svn, &cpu_svn, sizeof(sgx_cpu_svn_t))){
  545. AESM_DBG_ERROR("PSVN unmatched for PSE");
  546. return AE_PSVN_UNMATCHED_ERROR;
  547. }
  548. }else{
  549. if(0!=memcpy_s(&_pse_psvn.isv_svn, sizeof(_pse_psvn.isv_svn), &isv_svn, sizeof(isv_svn))||
  550. 0!=memcpy_s(&_pse_psvn.cpu_svn, sizeof(_pse_psvn.cpu_svn), &cpu_svn, sizeof(sgx_cpu_svn_t))){
  551. AESM_DBG_ERROR("memcpy failed");
  552. return AE_FAILURE;
  553. }
  554. AESM_DBG_TRACE("get PSE isv_svn=%d", (int)isv_svn);
  555. _is_pse_psvn_set = true;
  556. return AE_SUCCESS;
  557. }
  558. }
  559. }
  560. return AE_SUCCESS;
  561. }
  562. sgx_status_t AESMLogic::get_launch_token(const enclave_css_t* signature,
  563. const sgx_attributes_t* attribute,
  564. sgx_launch_token_t* launch_token)
  565. {
  566. AESM_DBG_INFO("enter function");
  567. AESMLogicLock lock(_le_mutex);
  568. ae_error_t ret_le = AE_SUCCESS;
  569. uint32_t mrsigner_index = UINT32_MAX;
  570. if (is_in_kernel_driver())
  571. {
  572. //Should not be called
  573. AESM_LOG_ERROR("InKernel LE loaded");
  574. return SGX_ERROR_SERVICE_UNAVAILABLE;
  575. }
  576. // load LE to get launch token
  577. if((ret_le=CLEClass::instance().load_enclave()) != AE_SUCCESS)
  578. {
  579. if(ret_le == AESM_AE_NO_DEVICE)
  580. {
  581. AESM_DBG_FATAL("LE not loaded due to no SGX device available, possible SGX Env Not Ready");
  582. return SGX_ERROR_NO_DEVICE;
  583. }
  584. else if(ret_le == AESM_AE_OUT_OF_EPC)
  585. {
  586. AESM_DBG_FATAL("LE not loaded due to out of EPC");
  587. return SGX_ERROR_OUT_OF_EPC;
  588. }
  589. else
  590. {
  591. AESM_DBG_FATAL("fail to load LE:%d",ret_le);
  592. return SGX_ERROR_SERVICE_UNAVAILABLE;
  593. }
  594. }
  595. ret_le = static_cast<ae_error_t>(CLEClass::instance().get_launch_token(
  596. const_cast<uint8_t*>(reinterpret_cast<const uint8_t *>(&signature->body.enclave_hash)),
  597. sizeof(sgx_measurement_t),
  598. const_cast<uint8_t*>(reinterpret_cast<const uint8_t *>(&signature->key.modulus)),
  599. sizeof(signature->key.modulus),
  600. const_cast<uint8_t*>(reinterpret_cast<const uint8_t *>(attribute)),
  601. sizeof(sgx_attributes_t),
  602. reinterpret_cast<uint8_t*>(launch_token),
  603. sizeof(token_t),
  604. &mrsigner_index));
  605. switch (ret_le)
  606. {
  607. case AE_SUCCESS:
  608. break;
  609. case LE_INVALID_PARAMETER:
  610. AESM_DBG_TRACE("Invalid parameter");
  611. return SGX_ERROR_INVALID_PARAMETER;
  612. case LE_INVALID_ATTRIBUTE:
  613. case LE_INVALID_PRIVILEGE_ERROR:
  614. AESM_DBG_TRACE("Launch token error");
  615. return SGX_ERROR_SERVICE_INVALID_PRIVILEGE;
  616. case LE_WHITELIST_UNINITIALIZED_ERROR:
  617. AESM_DBG_TRACE("LE whitelist uninitialized error");
  618. return SGX_ERROR_UNEXPECTED;
  619. default:
  620. AESM_DBG_WARN("unexpected error (ae%d)", ret_le);
  621. return SGX_ERROR_UNEXPECTED;
  622. }
  623. token_t *lt = reinterpret_cast<token_t *>(launch_token);
  624. ret_le = set_psvn(signature->body.isv_prod_id, signature->body.isv_svn, lt->cpu_svn_le, mrsigner_index);
  625. if(AE_PSVN_UNMATCHED_ERROR == ret_le)
  626. {
  627. //QE or PSE has been changed, but AESM doesn't restart. Will not provide service.
  628. return SGX_ERROR_SERVICE_UNAVAILABLE;
  629. }else if(AE_SUCCESS != ret_le) {
  630. AESM_DBG_ERROR("fail to save psvn:(ae%d)", ret_le);
  631. return SGX_ERROR_UNEXPECTED;
  632. }
  633. return SGX_SUCCESS;
  634. }
  635. #define CHECK_LONG_TERM_PAIRING_STATUS \
  636. if(!query_pse_thread_status()){\
  637. return AESM_BUSY; \
  638. }
  639. aesm_error_t AESMLogic::create_session(
  640. uint32_t* session_id,
  641. uint8_t* se_dh_msg1, uint32_t se_dh_msg1_size)
  642. {
  643. AESM_DBG_INFO("AESMLogic::create_session");
  644. CHECK_SERVICE_STATUS;
  645. AESMLogicLock lock(_pse_mutex);
  646. CHECK_SERVICE_STATUS;
  647. CHECK_LONG_TERM_PAIRING_STATUS;
  648. ae_error_t psStatus;
  649. // If PSDA not loaded or CSE not provisioned
  650. if (CPSEClass::instance().get_status() == PSE_STATUS_INIT ||
  651. CPSEClass::instance().get_status() == PSE_STATUS_UNAVAILABLE )
  652. {
  653. AESM_DBG_ERROR("unexpected status PSE_STATUS_INIT : PSDA not loaded or CSE not provisioned.");
  654. return AESM_PSDA_UNAVAILABLE;
  655. }
  656. else
  657. psStatus = PlatformInfoLogic::create_session_pre_internal();
  658. if (OAL_THREAD_TIMEOUT_ERROR == psStatus){
  659. AESM_DBG_INFO("AESM is busy in intializing for pse");
  660. return AESM_BUSY;
  661. }
  662. if (PVE_PROV_ATTEST_KEY_NOT_FOUND == psStatus) {
  663. AESM_DBG_INFO("Key not found reported by Provisioning backend");
  664. return AESM_UNRECOGNIZED_PLATFORM;
  665. }
  666. if(OAL_PROXY_SETTING_ASSIST == psStatus){
  667. AESM_DBG_INFO("Proxy assist required in initializing for pse");
  668. return AESM_PROXY_SETTING_ASSIST;
  669. }
  670. if(PSW_UPDATE_REQUIRED == psStatus){
  671. AESM_DBG_INFO("PSW software update required");
  672. return AESM_UPDATE_AVAILABLE;
  673. }
  674. if(AESM_AE_OUT_OF_EPC == psStatus)
  675. return AESM_OUT_OF_EPC;
  676. if (AE_SUCCESS != psStatus) {
  677. AESM_DBG_ERROR("psStatus = 0x%X in create_session", psStatus);
  678. }
  679. return PSEOPAESMLogic::create_session(session_id, se_dh_msg1, se_dh_msg1_size);
  680. }
  681. aesm_error_t AESMLogic::exchange_report(
  682. uint32_t session_id,
  683. const uint8_t* se_dh_msg2, uint32_t se_dh_msg2_size,
  684. uint8_t* se_dh_msg3, uint32_t se_dh_msg3_size)
  685. {
  686. AESM_DBG_INFO("AESMLogic::exchange_report");
  687. CHECK_SERVICE_STATUS;
  688. AESMLogicLock lock(_pse_mutex);
  689. CHECK_SERVICE_STATUS;
  690. CHECK_LONG_TERM_PAIRING_STATUS;
  691. return PSEOPAESMLogic::exchange_report(session_id,
  692. se_dh_msg2,
  693. se_dh_msg2_size,
  694. se_dh_msg3,
  695. se_dh_msg3_size);
  696. }
  697. aesm_error_t AESMLogic::close_session(
  698. uint32_t session_id)
  699. {
  700. AESM_DBG_INFO("AESMLogic::close_session");
  701. CHECK_SERVICE_STATUS;
  702. AESMLogicLock lock(_pse_mutex);
  703. CHECK_SERVICE_STATUS;
  704. return PSEOPAESMLogic::close_session(session_id);
  705. }
  706. aesm_error_t AESMLogic::invoke_service(
  707. const uint8_t* pse_message_req, uint32_t pse_message_req_size,
  708. uint8_t* pse_message_resp, uint32_t pse_message_resp_size)
  709. {
  710. AESM_DBG_INFO("AESMLogic::invoke_service");
  711. CHECK_SERVICE_STATUS;
  712. AESMLogicLock lock(_pse_mutex);
  713. CHECK_SERVICE_STATUS;
  714. CHECK_LONG_TERM_PAIRING_STATUS;
  715. return PSEOPAESMLogic::invoke_service(pse_message_req,
  716. pse_message_req_size,
  717. pse_message_resp,
  718. pse_message_resp_size);
  719. }
  720. aesm_error_t AESMLogic::get_ps_cap(
  721. uint64_t* ps_cap)
  722. {
  723. AESM_DBG_INFO("AESMLogic::get_ps_cap");
  724. CHECK_SERVICE_STATUS;
  725. AESMLogicLock lock(_pse_mutex);
  726. CHECK_SERVICE_STATUS;
  727. return PSEOPAESMLogic::get_ps_cap(ps_cap);
  728. }
  729. #define CHECK_EPID_PROVISIONG_STATUS \
  730. if(!query_pve_thread_status()){\
  731. return AESM_BUSY;\
  732. }
  733. aesm_error_t AESMLogic::init_quote(
  734. uint8_t *target_info, uint32_t target_info_size,
  735. uint8_t *gid, uint32_t gid_size)
  736. {
  737. ae_error_t ret = AE_SUCCESS;
  738. uint16_t qe_isv_svn = 0xFFFF;
  739. uint16_t pce_isv_svn = 0xFFFF;
  740. sgx_cpu_svn_t qe_cpu_svn;
  741. memset(&qe_cpu_svn, 0, sizeof(qe_cpu_svn));
  742. AESM_DBG_INFO("init_quote");
  743. if(sizeof(sgx_target_info_t) != target_info_size ||
  744. sizeof(sgx_epid_group_id_t) != gid_size)
  745. {
  746. return AESM_PARAMETER_ERROR;
  747. }
  748. AESMLogicLock lock(_qe_pve_mutex);
  749. CHECK_EPID_PROVISIONG_STATUS;
  750. ret = get_pce_isv_svn(pce_isv_svn);
  751. if(AE_SUCCESS != ret)
  752. {
  753. if(AESM_AE_OUT_OF_EPC == ret)
  754. return AESM_OUT_OF_EPC;
  755. else if(AESM_AE_NO_DEVICE == ret)
  756. return AESM_NO_DEVICE_ERROR;
  757. else if(AE_SERVER_NOT_AVAILABLE == ret)
  758. return AESM_SERVICE_UNAVAILABLE;
  759. return AESM_UNEXPECTED_ERROR;
  760. }
  761. ret = get_qe_cpu_svn(qe_cpu_svn);
  762. if(AE_SUCCESS != ret)
  763. {
  764. if(AESM_AE_OUT_OF_EPC == ret)
  765. return AESM_OUT_OF_EPC;
  766. else if(AESM_AE_NO_DEVICE == ret)
  767. return AESM_NO_DEVICE_ERROR;
  768. else if(AE_SERVER_NOT_AVAILABLE == ret)
  769. return AESM_SERVICE_UNAVAILABLE;
  770. return AESM_UNEXPECTED_ERROR;
  771. }
  772. ret = get_qe_isv_svn(qe_isv_svn);
  773. if(AE_SUCCESS != ret)
  774. {
  775. if(AESM_AE_OUT_OF_EPC == ret)
  776. return AESM_OUT_OF_EPC;
  777. else if(AESM_AE_NO_DEVICE == ret)
  778. return AESM_NO_DEVICE_ERROR;
  779. else if(AE_SERVER_NOT_AVAILABLE == ret)
  780. return AESM_SERVICE_UNAVAILABLE;
  781. return AESM_UNEXPECTED_ERROR;
  782. }
  783. return QEAESMLogic::init_quote(
  784. reinterpret_cast<sgx_target_info_t *>(target_info),
  785. gid, gid_size, pce_isv_svn, qe_isv_svn, qe_cpu_svn);
  786. }
  787. aesm_error_t AESMLogic::get_quote(const uint8_t *report, uint32_t report_size,
  788. uint32_t quote_type,
  789. const uint8_t *spid, uint32_t spid_size,
  790. const uint8_t *nonce, uint32_t nonce_size,
  791. const uint8_t *sigrl, uint32_t sigrl_size,
  792. uint8_t *qe_report, uint32_t qe_report_size,
  793. uint8_t *quote, uint32_t buf_size)
  794. {
  795. ae_error_t ret = AE_SUCCESS;
  796. uint16_t pce_isv_svn = 0xFFFF;
  797. AESM_DBG_INFO("get_quote");
  798. if(sizeof(sgx_report_t) != report_size ||
  799. sizeof(sgx_spid_t) != spid_size)
  800. {
  801. return AESM_PARAMETER_ERROR;
  802. }
  803. if((nonce && sizeof(sgx_quote_nonce_t) != nonce_size)
  804. || (qe_report && sizeof(sgx_report_t) != qe_report_size))
  805. {
  806. return AESM_PARAMETER_ERROR;
  807. }
  808. AESMLogicLock lock(_qe_pve_mutex);
  809. CHECK_EPID_PROVISIONG_STATUS;
  810. ret = get_pce_isv_svn(pce_isv_svn);
  811. if(AE_SUCCESS != ret)
  812. {
  813. if(AESM_AE_OUT_OF_EPC == ret)
  814. return AESM_OUT_OF_EPC;
  815. else if(AESM_AE_NO_DEVICE == ret)
  816. return AESM_NO_DEVICE_ERROR;
  817. else if(AE_SERVER_NOT_AVAILABLE == ret)
  818. return AESM_SERVICE_UNAVAILABLE;
  819. return AESM_UNEXPECTED_ERROR;
  820. }
  821. return QEAESMLogic::get_quote(report, quote_type, spid, nonce, sigrl,
  822. sigrl_size, qe_report, quote, buf_size, pce_isv_svn);
  823. }
  824. uint32_t AESMLogic::endpoint_selection(endpoint_selection_infos_t& es_info)
  825. {
  826. AESMLogicLock lock(_qe_pve_mutex);
  827. SGX_DBGPRINT_ONE_STRING_TWO_INTS_ENDPOINT_SELECTION(__FUNCTION__" (line, 0)", __LINE__, 0);
  828. return EndpointSelectionInfo::instance().start_protocol(es_info);
  829. }
  830. uint32_t AESMLogic::is_gid_matching_result_in_epid_blob(const GroupId& gid)
  831. {
  832. AESMLogicLock lock(_qe_pve_mutex);
  833. EPIDBlob& epid_blob = EPIDBlob::instance();
  834. uint32_t le_gid;
  835. if(epid_blob.get_sgx_gid(&le_gid)!=AE_SUCCESS){//get littlen endian gid
  836. return GIDMT_UNEXPECTED_ERROR;
  837. }
  838. le_gid=_htonl(le_gid);//use bigendian gid
  839. se_static_assert(sizeof(le_gid)==sizeof(gid));
  840. if(memcmp(&le_gid,&gid,sizeof(gid))!=0){
  841. return GIDMT_UNMATCHED;
  842. }
  843. return GIDMT_MATCHED;
  844. }
  845. aesm_error_t AESMLogic::report_attestation_status(
  846. uint8_t* platform_info, uint32_t platform_info_size,
  847. uint32_t attestation_status,
  848. uint8_t* update_info, uint32_t update_info_size)
  849. {
  850. AESM_DBG_INFO("report_attestation_status");
  851. AESMLogicLock lock(_pse_mutex);
  852. CHECK_LONG_TERM_PAIRING_STATUS;
  853. return PlatformInfoLogic::report_attestation_status(platform_info, platform_info_size, attestation_status, update_info, update_info_size);
  854. }
  855. ae_error_t AESMLogic::get_white_list_size_without_lock(uint32_t *white_list_cert_size)
  856. {
  857. uint32_t white_cert_size = 0;
  858. ae_error_t ae_ret = aesm_query_data_size(FT_PERSISTENT_STORAGE, AESM_WHITE_LIST_CERT_FID, &white_cert_size);
  859. if (AE_SUCCESS == ae_ret)
  860. {
  861. if (white_cert_size != 0){//file existing and not 0 size
  862. *white_list_cert_size = white_cert_size;
  863. return AE_SUCCESS;
  864. }
  865. else
  866. return AE_FAILURE;
  867. }
  868. else
  869. {
  870. return ae_ret;
  871. }
  872. }
  873. aesm_error_t AESMLogic::get_white_list_size(
  874. uint32_t* white_list_cert_size)
  875. {
  876. if (NULL == white_list_cert_size){
  877. return AESM_PARAMETER_ERROR;
  878. }
  879. CHECK_SERVICE_STATUS;
  880. AESMLogicLock lock(_le_mutex);
  881. CHECK_SERVICE_STATUS;
  882. if (is_in_kernel_driver())
  883. {
  884. AESM_LOG_INFO("InKernel LE loaded");
  885. return AESM_SERVICE_UNAVAILABLE;
  886. }
  887. ae_error_t ae_ret = get_white_list_size_without_lock(white_list_cert_size);
  888. if (AE_SUCCESS == ae_ret)
  889. return AESM_SUCCESS;
  890. else
  891. return AESM_UNEXPECTED_ERROR;
  892. }
  893. aesm_error_t AESMLogic::get_white_list(
  894. uint8_t *white_list_cert, uint32_t buf_size)
  895. {
  896. uint32_t white_cert_size=0;
  897. if (NULL == white_list_cert){
  898. return AESM_PARAMETER_ERROR;
  899. }
  900. CHECK_SERVICE_STATUS;
  901. AESMLogicLock lock(_le_mutex);
  902. CHECK_SERVICE_STATUS;
  903. if (is_in_kernel_driver())
  904. {
  905. AESM_LOG_INFO("InKernel LE loaded");
  906. return AESM_SERVICE_UNAVAILABLE;
  907. }
  908. ae_error_t ae_ret = get_white_list_size_without_lock(&white_cert_size);
  909. if (AE_SUCCESS != ae_ret)
  910. return AESM_UNEXPECTED_ERROR;
  911. if (white_cert_size != buf_size)
  912. {
  913. return AESM_PARAMETER_ERROR;
  914. }
  915. ae_ret = aesm_read_data(FT_PERSISTENT_STORAGE, AESM_WHITE_LIST_CERT_FID, white_list_cert, &white_cert_size);
  916. if (AE_SUCCESS != ae_ret){
  917. AESM_DBG_WARN("Fail to read white cert list file");
  918. return AESM_UNEXPECTED_ERROR;
  919. }
  920. return AESM_SUCCESS;
  921. }
  922. ae_error_t sgx_error_to_ae_error(sgx_status_t status)
  923. {
  924. if(SGX_ERROR_OUT_OF_MEMORY == status)
  925. return AE_OUT_OF_MEMORY_ERROR;
  926. if(SGX_SUCCESS == status)
  927. return AE_SUCCESS;
  928. return AE_FAILURE;
  929. }
  930. aesm_error_t AESMLogic::switch_extended_epid_group(
  931. uint32_t extended_epid_group_id
  932. )
  933. {
  934. AESM_DBG_INFO("AESMLogic::switch_extended_epid_group");
  935. ae_error_t ae_ret;
  936. if ((ae_ret = XEGDBlob::verify_xegd_by_xgid(extended_epid_group_id)) != AE_SUCCESS ||
  937. (ae_ret = EndpointSelectionInfo::verify_file_by_xgid(extended_epid_group_id)) != AE_SUCCESS){
  938. AESM_DBG_INFO("Fail to switch to extended epid group to %d due to XEGD blob for URL blob not available", extended_epid_group_id);
  939. return AESM_PARAMETER_ERROR;
  940. }
  941. ae_ret = set_global_extended_epid_group_id(extended_epid_group_id);
  942. if (ae_ret != AE_SUCCESS){
  943. AESM_DBG_INFO("Fail to switch to extended epid group %d", extended_epid_group_id);
  944. return AESM_UNEXPECTED_ERROR;
  945. }
  946. AESM_DBG_INFO("Succ to switch to extended epid group %d in data file, restart aesm required to use it", extended_epid_group_id);
  947. return AESM_SUCCESS;
  948. }
  949. aesm_error_t AESMLogic::get_extended_epid_group_id(
  950. uint32_t* extended_epid_group_id)
  951. {
  952. AESM_DBG_INFO("AESMLogic::get_extended_epid_group");
  953. if (NULL == extended_epid_group_id)
  954. {
  955. return AESM_PARAMETER_ERROR;
  956. }
  957. *extended_epid_group_id = get_active_extended_epid_group_id();
  958. return AESM_SUCCESS;
  959. }