aesm_logic.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. /*
  2. * Copyright (C) 2011-2016 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #include "QEClass.h"
  32. #include "LEClass.h"
  33. #include "PVEClass.h"
  34. #include "PCEClass.h"
  35. #include "arch.h"
  36. #include "sgx_report.h"
  37. #include "sgx_tseal.h"
  38. #include "epid_pve_type.h"
  39. #include "util.h"
  40. #include <assert.h>
  41. #include <time.h>
  42. #include "oal/oal.h"
  43. #include "oal/aesm_thread.h"
  44. #include "aesm_encode.h"
  45. #include "aesm_epid_blob.h"
  46. #include "aesm_xegd_blob.h"
  47. #include "aesm_logic.h"
  48. #include "pve_logic.h"
  49. #include "qe_logic.h"
  50. #include "platform_info_logic.h"
  51. #include "prov_msg_size.h"
  52. #include "se_sig_rl.h"
  53. #include "se_quote_internal.h"
  54. #include "endpoint_select_info.h"
  55. #include "se_wrapper.h"
  56. #include "ippcp.h"
  57. #include "ippcore.h"
  58. #include <time.h>
  59. #include <string>
  60. #include "ippcp.h"
  61. #include "ippcore.h"
  62. #include "prof_fun.h"
  63. #include "aesm_long_lived_thread.h"
  64. #include "sgx_profile.h"
  65. #include "service_enclave_mrsigner.hh"
  66. #define CHECK_SERVICE_STATUS if (!is_service_running()) return AESM_SERVICE_STOPPED;
  67. #define CHECK_SGX_STATUS if (g_sgx_device_status != SGX_ENABLED) return AESM_SGX_DEVICE_NOT_AVAILABLE;
  68. AESMLogicMutex AESMLogic::_qe_pve_mutex;
  69. AESMLogicMutex AESMLogic::_pse_mutex;
  70. AESMLogicMutex AESMLogic::_le_mutex;
  71. bool AESMLogic::_is_qe_psvn_set;
  72. bool AESMLogic::_is_pse_psvn_set;
  73. bool AESMLogic::_is_pce_psvn_set;
  74. psvn_t AESMLogic::_qe_psvn;
  75. psvn_t AESMLogic::_pce_psvn;
  76. psvn_t AESMLogic::_pse_psvn;
  77. uint32_t AESMLogic::active_extended_epid_group_id;
  78. static ae_error_t read_global_extended_epid_group_id(uint32_t *xeg_id)
  79. {
  80. char path_name[MAX_PATH];
  81. ae_error_t ae_ret = aesm_get_pathname(FT_PERSISTENT_STORAGE, EXTENDED_EPID_GROUP_ID_FID, path_name, MAX_PATH);
  82. if(AE_SUCCESS != ae_ret){
  83. return ae_ret;
  84. }
  85. FILE * f = fopen(path_name, "r");
  86. if( f == NULL){
  87. return OAL_CONFIG_FILE_ERROR;
  88. }
  89. ae_ret = OAL_CONFIG_FILE_ERROR;
  90. if(fscanf(f, "%u", xeg_id)==1){
  91. ae_ret = AE_SUCCESS;
  92. }
  93. fclose(f);
  94. return ae_ret;
  95. }
  96. static ae_error_t set_global_extended_epid_group_id(uint32_t xeg_id)
  97. {
  98. char path_name[MAX_PATH];
  99. ae_error_t ae_ret = aesm_get_pathname(FT_PERSISTENT_STORAGE, EXTENDED_EPID_GROUP_ID_FID, path_name, MAX_PATH);
  100. if(AE_SUCCESS != ae_ret){
  101. return ae_ret;
  102. }
  103. FILE *f = fopen(path_name, "w");
  104. if(f == NULL){
  105. return OAL_CONFIG_FILE_ERROR;
  106. }
  107. ae_ret = OAL_CONFIG_FILE_ERROR;
  108. if(fprintf(f, "%u", xeg_id)>0){
  109. ae_ret = AE_SUCCESS;
  110. }
  111. fclose(f);
  112. return ae_ret;
  113. }
  114. uint32_t AESMLogic::get_active_extended_epid_group_id()
  115. {
  116. return active_extended_epid_group_id;
  117. }
  118. static ae_error_t thread_to_load_qe(aesm_thread_arg_type_t arg)
  119. {
  120. epid_blob_with_cur_psvn_t epid_data;
  121. ae_error_t ae_ret = AE_FAILURE;
  122. uint32_t epid_xeid = 0;
  123. UNUSED(arg);
  124. AESM_DBG_TRACE("start to load qe");
  125. memset(&epid_data, 0, sizeof(epid_data));
  126. AESMLogicLock lock(AESMLogic::_qe_pve_mutex);
  127. if((ae_ret = EPIDBlob::instance().read(epid_data)) == AE_SUCCESS)
  128. {
  129. AESM_DBG_TRACE("EPID blob is read successfully, loading QE ...");
  130. ae_ret = CQEClass::instance().load_enclave();
  131. if(AE_SUCCESS != ae_ret)
  132. {
  133. AESM_DBG_WARN("fail to load QE: %d", ae_ret);
  134. }else{
  135. AESM_DBG_TRACE("QE loaded successfully");
  136. bool resealed = false;
  137. se_static_assert(SGX_TRUSTED_EPID_BLOB_SIZE_SDK>=SGX_TRUSTED_EPID_BLOB_SIZE_SIK);
  138. // Just take this chance to reseal EPID blob in case TCB is
  139. // upgraded, return value is ignored and no provisioning is
  140. // triggered.
  141. ae_ret = static_cast<ae_error_t>(CQEClass::instance().verify_blob(
  142. epid_data.trusted_epid_blob,
  143. SGX_TRUSTED_EPID_BLOB_SIZE_SDK,
  144. &resealed));
  145. if(AE_SUCCESS != ae_ret)
  146. {
  147. AESM_DBG_WARN("Failed to verify EPID blob: %d", ae_ret);
  148. // The EPID blob is invalid.
  149. EPIDBlob::instance().remove();
  150. }else{
  151. // Check whether EPID blob XEGDID is aligned with active extended group id if it exists.
  152. if ((EPIDBlob::instance().get_extended_epid_group_id(&epid_xeid) == AE_SUCCESS) && (epid_xeid == AESMLogic::get_active_extended_epid_group_id())) {
  153. AESM_DBG_TRACE("EPID blob Verified");
  154. // XEGDID is aligned
  155. if (true == resealed)
  156. {
  157. AESM_DBG_TRACE("EPID blob is resealed");
  158. if ((ae_ret = EPIDBlob::instance().write(epid_data))
  159. != AE_SUCCESS)
  160. {
  161. AESM_DBG_WARN("Failed to update epid blob: %d", ae_ret);
  162. }
  163. }
  164. }
  165. else { // XEGDID is NOT aligned
  166. AESM_DBG_TRACE("XEGDID mismatch in EPIDBlob, loading PCE ...");
  167. EPIDBlob::instance().remove();
  168. ae_ret = CPCEClass::instance().load_enclave();
  169. if (AE_SUCCESS != ae_ret)
  170. {
  171. AESM_DBG_WARN("fail to load PCE: %d", ae_ret);
  172. }
  173. else{
  174. AESM_DBG_TRACE("PCE loaded successfully");
  175. }
  176. }
  177. }
  178. }
  179. }else{
  180. AESM_DBG_TRACE("Fail to read EPID Blob");
  181. }
  182. AESM_DBG_TRACE("QE Thread finished succ");
  183. return AE_SUCCESS;
  184. }
  185. // Must be called when AESM starts
  186. ae_error_t AESMLogic::service_start()
  187. {
  188. AESM_PROFILE_INIT;
  189. ae_error_t ae_ret = AE_SUCCESS;
  190. AESM_LOG_INIT();
  191. #ifdef SGX_USE_OPT_LIB
  192. ippInit();
  193. #endif
  194. AESM_DBG_INFO("aesm service is starting");
  195. //Try to read current active extended epid group id
  196. ae_ret = read_global_extended_epid_group_id(&AESMLogic::active_extended_epid_group_id);
  197. if (AE_SUCCESS != ae_ret){
  198. AESM_DBG_INFO("Fail to read extended epid group id, default extended epid group used");
  199. AESMLogic::active_extended_epid_group_id = DEFAULT_EGID; //use default extended epid group id 0 if it is not available from data file
  200. }
  201. else{
  202. AESM_DBG_INFO("active extended group id %d used", AESMLogic::active_extended_epid_group_id);
  203. }
  204. extended_epid_group_blob_t xegb;
  205. aesm_server_url_infos_t urls;
  206. if (AE_SUCCESS != (XEGDBlob::verify_xegd_by_xgid(active_extended_epid_group_id)) ||
  207. 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
  208. 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
  209. }
  210. ae_ret = CLEClass::instance().load_enclave();
  211. if(AE_SUCCESS != ae_ret)
  212. {
  213. AESM_DBG_INFO("fail to load LE: %d", ae_ret);
  214. AESM_LOG_FATAL("%s", g_event_string_table[SGX_EVENT_SERVICE_UNAVAILABLE]);
  215. return ae_ret;
  216. }
  217. aesm_thread_t qe_thread=NULL;
  218. ae_error_t aesm_ret1 = aesm_create_thread(thread_to_load_qe, 0,&qe_thread);
  219. if(AE_SUCCESS != aesm_ret1 ){
  220. AESM_DBG_WARN("Fail to create thread to preload QE:%d",aesm_ret1);
  221. }else{
  222. (void)aesm_free_thread(qe_thread);//release thread handle to free memory
  223. }
  224. start_white_list_thread();
  225. AESM_DBG_TRACE("aesm service is started");
  226. return AE_SUCCESS;
  227. }
  228. void AESMLogic::service_stop()
  229. {
  230. stop_all_long_lived_threads();//waiting for pending threads util timeout
  231. CPVEClass::instance().unload_enclave();
  232. CPCEClass::instance().unload_enclave();
  233. CQEClass::instance().unload_enclave();
  234. CLEClass::instance().unload_enclave();
  235. stop_all_long_lived_threads();
  236. AESM_DBG_INFO("aesm service down");
  237. AESM_LOG_FINI();
  238. AESM_PROFILE_OUTPUT;
  239. }
  240. bool AESMLogic::is_service_running()
  241. {
  242. return true;
  243. }
  244. ae_error_t AESMLogic::save_unverified_white_list(const uint8_t *white_list_cert, uint32_t white_list_cert_size)
  245. {
  246. wl_cert_chain_t old_cert;
  247. const wl_cert_chain_t *p_new_cert = reinterpret_cast<const wl_cert_chain_t *>(white_list_cert);
  248. uint32_t old_cert_size = sizeof(old_cert);
  249. memset(&old_cert, 0, sizeof(old_cert));
  250. 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)
  251. && (old_cert_size == sizeof(old_cert)) && (white_list_cert_size >= sizeof(wl_cert_chain_t)))
  252. {
  253. if(_ntohl(p_new_cert->wl_cert.wl_version) <= _ntohl(old_cert.wl_cert.wl_version))
  254. {
  255. AESM_DBG_WARN("White list version downgraded! current version is %d, new version is %d",
  256. _ntohl(old_cert.wl_cert.wl_version), _ntohl(p_new_cert->wl_cert.wl_version));
  257. return OAL_PARAMETER_ERROR; // OAL_PARAMETER_ERROR used here is to indicate the white list is incorrect
  258. }
  259. }
  260. return aesm_write_data(FT_PERSISTENT_STORAGE, AESM_WHITE_LIST_CERT_TO_BE_VERIFY_FID, white_list_cert, white_list_cert_size);
  261. }
  262. aesm_error_t AESMLogic::white_list_register(
  263. const uint8_t *white_list_cert, uint32_t white_list_cert_size)
  264. {
  265. AESM_DBG_INFO("enter function");
  266. CHECK_SERVICE_STATUS;
  267. AESMLogicLock lock(_le_mutex);
  268. CHECK_SERVICE_STATUS;
  269. ae_error_t ret_le = AE_SUCCESS;
  270. if (NULL == white_list_cert||0==white_list_cert_size){
  271. AESM_DBG_TRACE("Invalid parameter");
  272. return AESM_PARAMETER_ERROR;
  273. }
  274. ae_error_t ae_ret = CLEClass::instance().load_enclave();
  275. if(ae_ret == AE_SERVER_NOT_AVAILABLE)
  276. {
  277. AESM_DBG_WARN("LE not loaded due to AE_SERVER_NOT_AVAILABLE, possible SGX Env Not Ready");
  278. ret_le = save_unverified_white_list(white_list_cert, white_list_cert_size);
  279. }
  280. else if(AE_FAILED(ae_ret))
  281. {
  282. AESM_DBG_ERROR("LE not loaded:%d", ae_ret);
  283. return AESM_UNEXPECTED_ERROR;
  284. }else{
  285. ret_le = static_cast<ae_error_t>(CLEClass::instance().white_list_register(
  286. white_list_cert, white_list_cert_size));
  287. }
  288. switch (ret_le)
  289. {
  290. case AE_SUCCESS:
  291. return AESM_SUCCESS;
  292. case LE_INVALID_PARAMETER:
  293. AESM_DBG_TRACE("Invalid parameter");
  294. return AESM_PARAMETER_ERROR;
  295. case LE_INVALID_ATTRIBUTE:
  296. AESM_DBG_TRACE("Launch token error");
  297. return AESM_GET_LICENSETOKEN_ERROR;
  298. default:
  299. AESM_DBG_WARN("unexpeted error %d", ret_le);
  300. return AESM_UNEXPECTED_ERROR;
  301. }
  302. }
  303. aesm_error_t AESMLogic::get_launch_token(
  304. const uint8_t * mrenclave, uint32_t mrenclave_size,
  305. const uint8_t *public_key, uint32_t public_key_size,
  306. const uint8_t *se_attributes, uint32_t se_attributes_size,
  307. uint8_t * lictoken, uint32_t lictoken_size)
  308. {
  309. AESM_DBG_INFO("enter function");
  310. CHECK_SERVICE_STATUS;
  311. AESMLogicLock lock(_le_mutex);
  312. CHECK_SERVICE_STATUS;
  313. ae_error_t ret_le = AE_SUCCESS;
  314. if (NULL == mrenclave ||
  315. NULL == public_key ||
  316. NULL == se_attributes ||
  317. NULL == lictoken)
  318. {
  319. //sizes are checked in CLEClass::get_launch_token()
  320. AESM_DBG_TRACE("Invalid parameter");
  321. return AESM_PARAMETER_ERROR;
  322. }
  323. ae_error_t ae_ret = CLEClass::instance().load_enclave();
  324. if(ae_ret == AESM_AE_NO_DEVICE)
  325. {
  326. AESM_LOG_ERROR("%s", g_event_string_table[SGX_EVENT_SERVICE_UNAVAILABLE]);
  327. AESM_DBG_FATAL("LE not loaded due to AE_SERVER_NOT_AVAILABLE, possible SGX Env Not Ready");
  328. return AESM_NO_DEVICE_ERROR;
  329. }
  330. else if(ae_ret == AESM_AE_OUT_OF_EPC)
  331. {
  332. AESM_DBG_WARN("LE not loaded due to out of EPC", ae_ret);
  333. return AESM_OUT_OF_EPC;
  334. }
  335. else if(AE_FAILED(ae_ret))
  336. {
  337. AESM_DBG_ERROR("LE not loaded:%d", ae_ret);
  338. return AESM_SERVICE_UNAVAILABLE;
  339. }
  340. ret_le = static_cast<ae_error_t>(CLEClass::instance().get_launch_token(
  341. const_cast<uint8_t *>(mrenclave), mrenclave_size,
  342. const_cast<uint8_t *>(public_key), public_key_size,
  343. const_cast<uint8_t *>(se_attributes), se_attributes_size,
  344. lictoken, lictoken_size));
  345. switch (ret_le)
  346. {
  347. case AE_SUCCESS:
  348. return AESM_SUCCESS;
  349. case LE_INVALID_PARAMETER:
  350. AESM_DBG_TRACE("Invalid parameter");
  351. return AESM_PARAMETER_ERROR;
  352. case LE_INVALID_ATTRIBUTE:
  353. case LE_INVALID_PRIVILEGE_ERROR:
  354. AESM_DBG_TRACE("Launch token error");
  355. return AESM_GET_LICENSETOKEN_ERROR;
  356. case LE_WHITELIST_UNINITIALIZED_ERROR:
  357. AESM_DBG_TRACE("LE whitelist uninitialized error");
  358. return AESM_UNEXPECTED_ERROR;
  359. default:
  360. AESM_DBG_WARN("unexpeted error %d", ret_le);
  361. return AESM_UNEXPECTED_ERROR;
  362. }
  363. }
  364. /* This function will be called outside aesm(from urts_internal) */
  365. extern "C" sgx_status_t get_launch_token(const enclave_css_t* signature,
  366. const sgx_attributes_t* attribute,
  367. sgx_launch_token_t* launch_token)
  368. {
  369. AESM_DBG_INFO("enter function");
  370. return AESMLogic::get_launch_token(signature, attribute, launch_token);
  371. }
  372. ae_error_t AESMLogic::get_qe_isv_svn(uint16_t& isv_svn)
  373. {
  374. if(!_is_qe_psvn_set){
  375. ae_error_t ae_err = CQEClass::instance().load_enclave();
  376. if(AE_SUCCESS != ae_err){
  377. AESM_DBG_ERROR("Fail to load QE Enclave:%d",ae_err);
  378. return ae_err;
  379. }
  380. }
  381. assert(_is_qe_psvn_set);
  382. if(0!=memcpy_s(&isv_svn, sizeof(isv_svn), &_qe_psvn.isv_svn, sizeof(_qe_psvn.isv_svn))){
  383. AESM_DBG_ERROR("memcpy failed");
  384. return AE_FAILURE;
  385. }
  386. return AE_SUCCESS;
  387. }
  388. ae_error_t AESMLogic::get_pce_isv_svn(uint16_t& isv_svn)
  389. {
  390. if(!_is_pce_psvn_set){
  391. ae_error_t ae_err = CPCEClass::instance().load_enclave();
  392. if(AE_SUCCESS != ae_err){
  393. AESM_DBG_ERROR("Fail to load PCE Enclave:%d",ae_err);
  394. return ae_err;
  395. }
  396. }
  397. assert(_is_pce_psvn_set);
  398. if(0!=memcpy_s(&isv_svn, sizeof(isv_svn), &_pce_psvn.isv_svn, sizeof(_pce_psvn.isv_svn))){
  399. AESM_DBG_ERROR("memcpy failed");
  400. return AE_FAILURE;
  401. }
  402. return AE_SUCCESS;
  403. }
  404. ae_error_t AESMLogic::get_pse_isv_svn(uint16_t& isv_svn)
  405. {
  406. return AE_FAILURE;
  407. }
  408. ae_error_t AESMLogic::get_qe_cpu_svn(sgx_cpu_svn_t& cpu_svn)
  409. {
  410. if(!_is_qe_psvn_set){
  411. ae_error_t ae_err = CQEClass::instance().load_enclave();
  412. if(AE_SUCCESS != ae_err){
  413. AESM_DBG_ERROR("Fail to load QE Enclave:%d",ae_err);
  414. return ae_err;
  415. }
  416. }
  417. assert(_is_qe_psvn_set);
  418. if(0!=memcpy_s(&cpu_svn, sizeof(sgx_cpu_svn_t), &_qe_psvn.cpu_svn, sizeof(_qe_psvn.cpu_svn))){
  419. AESM_DBG_ERROR("memcpy failed");
  420. return AE_FAILURE;
  421. }
  422. return AE_SUCCESS;
  423. }
  424. ae_error_t AESMLogic::set_psvn(uint16_t prod_id, uint16_t isv_svn, sgx_cpu_svn_t cpu_svn, uint32_t mrsigner_index)
  425. {
  426. if(prod_id == QE_PROD_ID){
  427. if(mrsigner_index == AE_MR_SIGNER){
  428. if(_is_qe_psvn_set){
  429. if(0!=memcmp(&_qe_psvn.isv_svn, &isv_svn, sizeof(isv_svn))||
  430. 0!=memcmp(&_qe_psvn.cpu_svn, &cpu_svn, sizeof(sgx_cpu_svn_t))){
  431. AESM_DBG_ERROR("PSVN unmatched for QE/PVE");
  432. return AE_PSVN_UNMATCHED_ERROR;
  433. }
  434. }else{
  435. if(0!=memcpy_s(&_qe_psvn.isv_svn, sizeof(_qe_psvn.isv_svn), &isv_svn, sizeof(isv_svn))||
  436. 0!=memcpy_s(&_qe_psvn.cpu_svn, sizeof(_qe_psvn.cpu_svn), &cpu_svn, sizeof(sgx_cpu_svn_t))){
  437. AESM_DBG_ERROR("memcpy failed");
  438. return AE_FAILURE;
  439. }
  440. AESM_DBG_TRACE("get QE or PvE isv_svn=%d",(int)isv_svn);
  441. _is_qe_psvn_set = true;
  442. return AE_SUCCESS;
  443. }
  444. }else if(mrsigner_index==PCE_MR_SIGNER){
  445. if(_is_pce_psvn_set){
  446. if(0!=memcmp(&_pce_psvn.isv_svn, &isv_svn, sizeof(isv_svn))||
  447. 0!=memcmp(&_pce_psvn.cpu_svn, &cpu_svn, sizeof(sgx_cpu_svn_t))){
  448. AESM_DBG_ERROR("PSVN unmatched for PCE");
  449. return AE_PSVN_UNMATCHED_ERROR;
  450. }
  451. }else{
  452. if(0!=memcpy_s(&_pce_psvn.isv_svn, sizeof(_pce_psvn.isv_svn), &isv_svn, sizeof(isv_svn))||
  453. 0!=memcpy_s(&_pce_psvn.cpu_svn, sizeof(_pce_psvn.cpu_svn), &cpu_svn, sizeof(sgx_cpu_svn_t))){
  454. AESM_DBG_ERROR("memcpy failed");
  455. return AE_FAILURE;
  456. }
  457. AESM_DBG_TRACE("get PCE isv_svn=%d", (int)isv_svn);
  458. _is_pce_psvn_set = true;
  459. return AE_SUCCESS;
  460. }
  461. }
  462. }else if(prod_id == PSE_PROD_ID){
  463. if(mrsigner_index == AE_MR_SIGNER){
  464. if(_is_pse_psvn_set){
  465. if(0!=memcmp(&_pse_psvn.isv_svn, &isv_svn, sizeof(isv_svn))||
  466. 0!=memcmp(&_pse_psvn.cpu_svn, &cpu_svn, sizeof(sgx_cpu_svn_t))){
  467. AESM_DBG_ERROR("PSVN unmatched for PSE");
  468. return AE_PSVN_UNMATCHED_ERROR;
  469. }
  470. }else{
  471. if(0!=memcpy_s(&_pse_psvn.isv_svn, sizeof(_pse_psvn.isv_svn), &isv_svn, sizeof(isv_svn))||
  472. 0!=memcpy_s(&_pse_psvn.cpu_svn, sizeof(_pse_psvn.cpu_svn), &cpu_svn, sizeof(sgx_cpu_svn_t))){
  473. AESM_DBG_ERROR("memcpy failed");
  474. return AE_FAILURE;
  475. }
  476. AESM_DBG_TRACE("get PSE isv_svn=%d", (int)isv_svn);
  477. _is_pse_psvn_set = true;
  478. return AE_SUCCESS;
  479. }
  480. }
  481. }
  482. return AE_SUCCESS;
  483. }
  484. sgx_status_t AESMLogic::get_launch_token(const enclave_css_t* signature,
  485. const sgx_attributes_t* attribute,
  486. sgx_launch_token_t* launch_token)
  487. {
  488. AESM_DBG_INFO("enter function");
  489. AESMLogicLock lock(_le_mutex);
  490. ae_error_t ret_le = AE_SUCCESS;
  491. uint32_t mrsigner_index = UINT32_MAX;
  492. // load LE to get launch token
  493. if((ret_le=CLEClass::instance().load_enclave()) != AE_SUCCESS)
  494. {
  495. if(ret_le == AESM_AE_NO_DEVICE)
  496. {
  497. AESM_DBG_FATAL("LE not loaded due to no SGX device available, possible SGX Env Not Ready");
  498. return SGX_ERROR_NO_DEVICE;
  499. }
  500. else if(ret_le == AESM_AE_OUT_OF_EPC)
  501. {
  502. AESM_DBG_FATAL("LE not loaded due to out of EPC");
  503. return SGX_ERROR_OUT_OF_EPC;
  504. }
  505. else
  506. {
  507. AESM_DBG_FATAL("fail to load LE:%d",ret_le);
  508. return SGX_ERROR_SERVICE_UNAVAILABLE;
  509. }
  510. }
  511. ret_le = static_cast<ae_error_t>(CLEClass::instance().get_launch_token(
  512. const_cast<uint8_t*>(reinterpret_cast<const uint8_t *>(&signature->body.enclave_hash)),
  513. sizeof(sgx_measurement_t),
  514. const_cast<uint8_t*>(reinterpret_cast<const uint8_t *>(&signature->key.modulus)),
  515. sizeof(signature->key.modulus),
  516. const_cast<uint8_t*>(reinterpret_cast<const uint8_t *>(attribute)),
  517. sizeof(sgx_attributes_t),
  518. reinterpret_cast<uint8_t*>(launch_token),
  519. sizeof(token_t),
  520. &mrsigner_index));
  521. switch (ret_le)
  522. {
  523. case AE_SUCCESS:
  524. break;
  525. case LE_INVALID_PARAMETER:
  526. AESM_DBG_TRACE("Invalid parameter");
  527. return SGX_ERROR_INVALID_PARAMETER;
  528. case LE_INVALID_ATTRIBUTE:
  529. case LE_INVALID_PRIVILEGE_ERROR:
  530. AESM_DBG_TRACE("Launch token error");
  531. return SGX_ERROR_SERVICE_INVALID_PRIVILEGE;
  532. case LE_WHITELIST_UNINITIALIZED_ERROR:
  533. AESM_DBG_TRACE("LE whitelist uninitialized error");
  534. return SGX_ERROR_UNEXPECTED;
  535. default:
  536. AESM_DBG_WARN("unexpeted error %d", ret_le);
  537. return SGX_ERROR_UNEXPECTED;
  538. }
  539. token_t *lt = reinterpret_cast<token_t *>(launch_token);
  540. ret_le = set_psvn(signature->body.isv_prod_id, signature->body.isv_svn, lt->cpu_svn_le, mrsigner_index);
  541. if(AE_PSVN_UNMATCHED_ERROR == ret_le)
  542. {
  543. //QE or PSE has been changed, but AESM doesn't restart. Will not provide service.
  544. return SGX_ERROR_SERVICE_UNAVAILABLE;
  545. }else if(AE_SUCCESS != ret_le) {
  546. AESM_DBG_ERROR("fail to save psvn:%d", ret_le);
  547. return SGX_ERROR_UNEXPECTED;
  548. }
  549. return SGX_SUCCESS;
  550. }
  551. aesm_error_t AESMLogic::create_session(
  552. uint32_t* session_id,
  553. uint8_t* se_dh_msg1, uint32_t se_dh_msg1_size)
  554. {
  555. return AESM_SERVICE_UNAVAILABLE;
  556. }
  557. aesm_error_t AESMLogic::exchange_report(
  558. uint32_t session_id,
  559. const uint8_t* se_dh_msg2, uint32_t se_dh_msg2_size,
  560. uint8_t* se_dh_msg3, uint32_t se_dh_msg3_size)
  561. {
  562. return AESM_SERVICE_UNAVAILABLE;
  563. }
  564. aesm_error_t AESMLogic::close_session(
  565. uint32_t session_id)
  566. {
  567. return AESM_SERVICE_UNAVAILABLE;
  568. }
  569. aesm_error_t AESMLogic::invoke_service(
  570. const uint8_t* pse_message_req, uint32_t pse_message_req_size,
  571. uint8_t* pse_message_resp, uint32_t pse_message_resp_size)
  572. {
  573. return AESM_SERVICE_UNAVAILABLE;;
  574. }
  575. aesm_error_t AESMLogic::get_ps_cap(
  576. uint64_t* ps_cap)
  577. {
  578. return AESM_PSDA_UNAVAILABLE;
  579. }
  580. #define CHECK_EPID_PROVISIONG_STATUS \
  581. if(!query_pve_thread_status()){\
  582. return AESM_BUSY;\
  583. }
  584. aesm_error_t AESMLogic::init_quote(
  585. uint8_t *target_info, uint32_t target_info_size,
  586. uint8_t *gid, uint32_t gid_size)
  587. {
  588. ae_error_t ret = AE_SUCCESS;
  589. uint16_t qe_isv_svn = 0xFFFF;
  590. uint16_t pce_isv_svn = 0xFFFF;
  591. sgx_cpu_svn_t qe_cpu_svn;
  592. memset(&qe_cpu_svn, 0, sizeof(qe_cpu_svn));
  593. AESM_DBG_INFO("init_quote");
  594. if(sizeof(sgx_target_info_t) != target_info_size ||
  595. sizeof(sgx_epid_group_id_t) != gid_size)
  596. {
  597. return AESM_PARAMETER_ERROR;
  598. }
  599. AESMLogicLock lock(_qe_pve_mutex);
  600. CHECK_EPID_PROVISIONG_STATUS;
  601. ret = get_pce_isv_svn(pce_isv_svn);
  602. if(AE_SUCCESS != ret)
  603. {
  604. if(AESM_AE_OUT_OF_EPC == ret)
  605. return AESM_OUT_OF_EPC;
  606. else if(AESM_AE_NO_DEVICE == ret)
  607. return AESM_NO_DEVICE_ERROR;
  608. else if(AE_SERVER_NOT_AVAILABLE == ret)
  609. return AESM_SERVICE_UNAVAILABLE;
  610. return AESM_UNEXPECTED_ERROR;
  611. }
  612. ret = get_qe_cpu_svn(qe_cpu_svn);
  613. if(AE_SUCCESS != ret)
  614. {
  615. if(AESM_AE_OUT_OF_EPC == ret)
  616. return AESM_OUT_OF_EPC;
  617. else if(AESM_AE_NO_DEVICE == ret)
  618. return AESM_NO_DEVICE_ERROR;
  619. else if(AE_SERVER_NOT_AVAILABLE == ret)
  620. return AESM_SERVICE_UNAVAILABLE;
  621. return AESM_UNEXPECTED_ERROR;
  622. }
  623. ret = get_qe_isv_svn(qe_isv_svn);
  624. if(AE_SUCCESS != ret)
  625. {
  626. if(AESM_AE_OUT_OF_EPC == ret)
  627. return AESM_OUT_OF_EPC;
  628. else if(AESM_AE_NO_DEVICE == ret)
  629. return AESM_NO_DEVICE_ERROR;
  630. else if(AE_SERVER_NOT_AVAILABLE == ret)
  631. return AESM_SERVICE_UNAVAILABLE;
  632. return AESM_UNEXPECTED_ERROR;
  633. }
  634. return QEAESMLogic::init_quote(
  635. reinterpret_cast<sgx_target_info_t *>(target_info),
  636. gid, gid_size, pce_isv_svn, qe_isv_svn, qe_cpu_svn);
  637. }
  638. aesm_error_t AESMLogic::get_quote(const uint8_t *report, uint32_t report_size,
  639. uint32_t quote_type,
  640. const uint8_t *spid, uint32_t spid_size,
  641. const uint8_t *nonce, uint32_t nonce_size,
  642. const uint8_t *sigrl, uint32_t sigrl_size,
  643. uint8_t *qe_report, uint32_t qe_report_size,
  644. uint8_t *quote, uint32_t buf_size)
  645. {
  646. ae_error_t ret = AE_SUCCESS;
  647. uint16_t pce_isv_svn = 0xFFFF;
  648. AESM_DBG_INFO("get_quote");
  649. if(sizeof(sgx_report_t) != report_size ||
  650. sizeof(sgx_spid_t) != spid_size)
  651. {
  652. return AESM_PARAMETER_ERROR;
  653. }
  654. if((nonce && sizeof(sgx_quote_nonce_t) != nonce_size)
  655. || (qe_report && sizeof(sgx_report_t) != qe_report_size))
  656. {
  657. return AESM_PARAMETER_ERROR;
  658. }
  659. AESMLogicLock lock(_qe_pve_mutex);
  660. CHECK_EPID_PROVISIONG_STATUS;
  661. ret = get_pce_isv_svn(pce_isv_svn);
  662. if(AE_SUCCESS != ret)
  663. {
  664. if(AESM_AE_OUT_OF_EPC == ret)
  665. return AESM_OUT_OF_EPC;
  666. else if(AESM_AE_NO_DEVICE == ret)
  667. return AESM_NO_DEVICE_ERROR;
  668. else if(AE_SERVER_NOT_AVAILABLE == ret)
  669. return AESM_SERVICE_UNAVAILABLE;
  670. return AESM_UNEXPECTED_ERROR;
  671. }
  672. return QEAESMLogic::get_quote(report, quote_type, spid, nonce, sigrl,
  673. sigrl_size, qe_report, quote, buf_size, pce_isv_svn);
  674. }
  675. uint32_t AESMLogic::endpoint_selection(endpoint_selection_infos_t& es_info)
  676. {
  677. AESMLogicLock lock(_qe_pve_mutex);
  678. SGX_DBGPRINT_ONE_STRING_TWO_INTS_ENDPOINT_SELECTION(__FUNCTION__" (line, 0)", __LINE__, 0);
  679. return EndpointSelectionInfo::instance().start_protocol(es_info);
  680. }
  681. aesm_error_t AESMLogic::report_attestation_status(
  682. uint8_t* platform_info, uint32_t platform_info_size,
  683. uint32_t attestation_status,
  684. uint8_t* update_info, uint32_t update_info_size)
  685. {
  686. AESM_DBG_INFO("report_attestation_status");
  687. AESMLogicLock lock(_pse_mutex);
  688. return PlatformInfoLogic::report_attestation_status(platform_info, platform_info_size, attestation_status, update_info, update_info_size);
  689. }
  690. uint32_t AESMLogic::is_gid_matching_result_in_epid_blob(const GroupId& gid)
  691. {
  692. AESMLogicLock lock(_qe_pve_mutex);
  693. EPIDBlob& epid_blob = EPIDBlob::instance();
  694. uint32_t le_gid;
  695. if(epid_blob.get_sgx_gid(&le_gid)!=AE_SUCCESS){//get littlen endian gid
  696. return GIDMT_UNEXPECTED_ERROR;
  697. }
  698. le_gid=_htonl(le_gid);//use bigendian gid
  699. se_static_assert(sizeof(le_gid)==sizeof(gid));
  700. if(memcmp(&le_gid,&gid,sizeof(gid))!=0){
  701. return GIDMT_UNMATCHED;
  702. }
  703. return GIDMT_MATCHED;
  704. }
  705. ae_error_t AESMLogic::get_white_list_size_without_lock(uint32_t *white_list_cert_size)
  706. {
  707. uint32_t white_cert_size = 0;
  708. ae_error_t ae_ret = aesm_query_data_size(FT_PERSISTENT_STORAGE, AESM_WHITE_LIST_CERT_FID, &white_cert_size);
  709. if (AE_SUCCESS == ae_ret)
  710. {
  711. if (white_cert_size != 0){//file existing and not 0 size
  712. *white_list_cert_size = white_cert_size;
  713. return AE_SUCCESS;
  714. }
  715. else
  716. return AE_FAILURE;
  717. }
  718. else
  719. {
  720. return ae_ret;
  721. }
  722. }
  723. aesm_error_t AESMLogic::get_white_list_size(
  724. uint32_t* white_list_cert_size)
  725. {
  726. if (NULL == white_list_cert_size){
  727. return AESM_PARAMETER_ERROR;
  728. }
  729. CHECK_SERVICE_STATUS;
  730. AESMLogicLock lock(_le_mutex);
  731. CHECK_SERVICE_STATUS;
  732. ae_error_t ae_ret = get_white_list_size_without_lock(white_list_cert_size);
  733. if (AE_SUCCESS == ae_ret)
  734. return AESM_SUCCESS;
  735. else
  736. return AESM_UNEXPECTED_ERROR;
  737. }
  738. aesm_error_t AESMLogic::get_white_list(
  739. uint8_t *white_list_cert, uint32_t buf_size)
  740. {
  741. uint32_t white_cert_size=0;
  742. if (NULL == white_list_cert){
  743. return AESM_PARAMETER_ERROR;
  744. }
  745. CHECK_SERVICE_STATUS;
  746. AESMLogicLock lock(_le_mutex);
  747. CHECK_SERVICE_STATUS;
  748. ae_error_t ae_ret = get_white_list_size_without_lock(&white_cert_size);
  749. if (AE_SUCCESS != ae_ret)
  750. return AESM_UNEXPECTED_ERROR;
  751. if (white_cert_size != buf_size)
  752. {
  753. return AESM_PARAMETER_ERROR;
  754. }
  755. ae_ret = aesm_read_data(FT_PERSISTENT_STORAGE, AESM_WHITE_LIST_CERT_FID, white_list_cert, &white_cert_size);
  756. if (AE_SUCCESS != ae_ret){
  757. AESM_DBG_WARN("Fail to read white cert list file");
  758. return AESM_UNEXPECTED_ERROR;
  759. }
  760. return AESM_SUCCESS;
  761. }
  762. ae_error_t sgx_error_to_ae_error(sgx_status_t status)
  763. {
  764. if(SGX_ERROR_OUT_OF_MEMORY == status)
  765. return AE_OUT_OF_MEMORY_ERROR;
  766. if(SGX_SUCCESS == status)
  767. return AE_SUCCESS;
  768. return AE_FAILURE;
  769. }
  770. aesm_error_t AESMLogic::switch_extended_epid_group(
  771. uint32_t extended_epid_group_id
  772. )
  773. {
  774. AESM_DBG_INFO("AESMLogic::switch_extended_epid_group");
  775. ae_error_t ae_ret;
  776. extended_epid_group_blob_t xegb;
  777. aesm_server_url_infos_t urls;
  778. if ((ae_ret = XEGDBlob::verify_xegd_by_xgid(extended_epid_group_id)) != AE_SUCCESS ||
  779. (ae_ret = EndpointSelectionInfo::verify_file_by_xgid(extended_epid_group_id)) != AE_SUCCESS){
  780. 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);
  781. return AESM_PARAMETER_ERROR;
  782. }
  783. ae_ret = set_global_extended_epid_group_id(extended_epid_group_id);
  784. if (ae_ret != AE_SUCCESS){
  785. AESM_DBG_INFO("Fail to switch to extended epid group %d", extended_epid_group_id);
  786. return AESM_UNEXPECTED_ERROR;
  787. }
  788. AESM_DBG_INFO("Succ to switch to extended epid group %d in data file, restart aesm required to use it", extended_epid_group_id);
  789. return AESM_SUCCESS;
  790. }
  791. aesm_error_t AESMLogic::get_extended_epid_group_id(
  792. uint32_t* extended_epid_group_id)
  793. {
  794. AESM_DBG_INFO("AESMLogic::get_extended_epid_group");
  795. if (NULL == extended_epid_group_id)
  796. {
  797. return AESM_PARAMETER_ERROR;
  798. }
  799. *extended_epid_group_id = get_active_extended_epid_group_id();
  800. return AESM_SUCCESS;
  801. }