LEClass.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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 <assert.h>
  32. #include "LEClass.h"
  33. #include "aeerror.h"
  34. #include "arch.h"
  35. #include "ae_ipp.h"
  36. #include "util.h"
  37. #include "service_enclave_mrsigner.hh"
  38. #include "aesm_long_lived_thread.h"
  39. #ifdef REF_LE
  40. #include "ref_le_u.h"
  41. #include "ref_le_u.c"
  42. #else
  43. #include "launch_enclave_u.h"
  44. #include "launch_enclave_u.c"
  45. extern "C" sgx_status_t sgx_create_le(const char *file_name, const char *prd_css_file_name, const int debug, sgx_launch_token_t *launch_token, int *launch_token_updated, sgx_enclave_id_t *enclave_id, sgx_misc_attribute_t *misc_attr, int *production_loaded);
  46. #endif
  47. int CLEClass::white_list_register(
  48. const uint8_t *white_list_cert,
  49. uint32_t white_list_cert_size,
  50. bool save_to_persistent_storage)
  51. {
  52. sgx_status_t ret = SGX_SUCCESS;
  53. int retry = 0;
  54. uint32_t status = 0;
  55. AESMLogicLock locker(AESMLogic::_le_mutex);
  56. assert(m_enclave_id);
  57. #ifdef REF_LE
  58. if (white_list_cert_size < sizeof(ref_le_white_list_t))
  59. {
  60. AESM_DBG_WARN("white list size is smaller than the expected minimum");
  61. return AE_INVALID_PARAMETER;
  62. }
  63. ref_le_white_list_t *p_white_list = (ref_le_white_list_t*)white_list_cert;
  64. uint32_t entries_count = _ntohs(p_white_list->entries_count);
  65. uint32_t white_list_size = REF_LE_WL_SIZE(entries_count);
  66. if ((white_list_size + sizeof(sgx_rsa3072_signature_t)) > white_list_cert_size)
  67. {
  68. AESM_DBG_WARN("white list size for %d recornds - expected: %d + %d = %d, actual: %d", entries_count,
  69. white_list_size, sizeof(sgx_rsa3072_signature_t), white_list_size + sizeof(sgx_rsa3072_signature_t), white_list_cert_size);
  70. return AE_INVALID_PARAMETER;
  71. }
  72. sgx_rsa3072_signature_t* p_white_list_sig = (sgx_rsa3072_signature_t*)((uint64_t)white_list_cert + white_list_size);
  73. ret = ref_le_init_white_list(m_enclave_id, (int*)&status, p_white_list, white_list_size, p_white_list_sig);
  74. #else
  75. if (white_list_cert_size < sizeof(wl_cert_chain_t)) {
  76. return LE_INVALID_PARAMETER;
  77. }
  78. ret = le_init_white_list_wrapper(m_enclave_id, &status,
  79. const_cast<uint8_t*>(white_list_cert),
  80. white_list_cert_size);
  81. #endif // REF_LE
  82. for(; ret == SGX_ERROR_ENCLAVE_LOST && retry < AESM_RETRY_COUNT; retry++)
  83. {
  84. unload_enclave();
  85. if(AE_SUCCESS != load_enclave_only())
  86. return AE_FAILURE;
  87. #ifdef REF_LE
  88. ret = ref_le_init_white_list(m_enclave_id, (int*)&status, p_white_list, white_list_size, p_white_list_sig);
  89. #else
  90. ret = le_init_white_list_wrapper(m_enclave_id, &status,
  91. const_cast<uint8_t*>(white_list_cert),
  92. white_list_cert_size);
  93. #endif // REF_LE
  94. }
  95. if(SGX_SUCCESS!=ret)
  96. return sgx_error_to_ae_error(ret);
  97. AESM_DBG_TRACE("le_init_white_list_wrapper return %d",status);
  98. if(AE_SUCCESS == status&&save_to_persistent_storage){//successfully register the white list cert
  99. if(AE_SUCCESS != aesm_write_data(FT_PERSISTENT_STORAGE,AESM_WHITE_LIST_CERT_FID,white_list_cert, white_list_cert_size)){//ignore error if failed to save in persistent storage
  100. AESM_DBG_WARN("Fail to save white list cert in persistent storage");
  101. }
  102. }
  103. if (LE_WHITE_LIST_ALREADY_UPDATED == status) {
  104. status = AE_SUCCESS;
  105. }
  106. return status;
  107. }
  108. void CLEClass::load_white_cert_list()
  109. {
  110. load_verified_white_cert_list();
  111. load_white_cert_list_to_be_verify();//If this version is older than previous one, it will not be loaded
  112. }
  113. #include <time.h>
  114. #include "endpoint_select_info.h"
  115. #include "stdint.h"
  116. #define UPDATE_DURATION (24*3600)
  117. ae_error_t CLEClass::update_white_list_by_url()
  118. {
  119. // on reference LE we don't support equiring white list from URL
  120. #ifndef REF_LE
  121. static time_t last_updated_time = 0;
  122. int i = 0;
  123. ae_error_t ret = AE_FAILURE;
  124. time_t cur_time = time(NULL);
  125. if (last_updated_time + UPDATE_DURATION > cur_time){
  126. return LE_WHITE_LIST_QUERY_BUSY;
  127. }
  128. AESM_LOG_INFO_ADMIN("%s", g_admin_event_string_table[SGX_ADMIN_EVENT_WL_UPDATE_START]);
  129. for (i = 0; i < 2; i++){//at most retry once if network error
  130. uint8_t *resp_buf = NULL;
  131. uint32_t resp_size = 0;
  132. const char *url = EndpointSelectionInfo::instance().get_server_url(SGX_WHITE_LIST_FILE);
  133. if (NULL == url){
  134. return OAL_CONFIG_FILE_ERROR;
  135. }
  136. ret = aesm_network_send_receive(url,
  137. NULL, 0, &resp_buf, &resp_size,GET, false);
  138. if (ret == OAL_NETWORK_UNAVAILABLE_ERROR){
  139. AESM_DBG_WARN("Network failure in getting white list...");
  140. continue;
  141. }
  142. if (ret == AE_SUCCESS){
  143. if (resp_buf != NULL && resp_size > 0){
  144. ret = (ae_error_t)instance().white_list_register(resp_buf, resp_size, true);
  145. if (AE_SUCCESS == ret&&resp_size >= sizeof(wl_cert_chain_t)){
  146. const wl_cert_chain_t* wl = reinterpret_cast<const wl_cert_chain_t*>(resp_buf);
  147. AESM_LOG_INFO_ADMIN("%s for Version: %d", g_admin_event_string_table[SGX_ADMIN_EVENT_WL_UPDATE_SUCCESS],
  148. _ntohl(wl->wl_cert.wl_version));
  149. }
  150. else if (LE_INVALID_PARAMETER == ret || LE_INVALID_PRIVILEGE_ERROR ==ret){
  151. AESM_LOG_WARN_ADMIN("%s", g_admin_event_string_table[SGX_ADMIN_EVENT_WL_UPDATE_FAIL]);
  152. }else{
  153. ret = AE_FAILURE;//Internal error, maybe LE not consistent with AESM?
  154. }
  155. }
  156. last_updated_time = cur_time;
  157. aesm_free_network_response_buffer(resp_buf);
  158. }
  159. break;
  160. }
  161. if (OAL_NETWORK_UNAVAILABLE_ERROR == ret){
  162. AESM_LOG_WARN_ADMIN("%s", g_admin_event_string_table[SGX_ADMIN_EVENT_WL_UPDATE_NETWORK_FAIL]);
  163. }
  164. return ret;
  165. #else
  166. return AE_SUCCESS;
  167. #endif
  168. }
  169. ae_error_t CLEClass::load_verified_white_cert_list()
  170. {
  171. ae_error_t ae_err;
  172. uint32_t white_cert_size=0;
  173. ae_err = aesm_query_data_size(FT_PERSISTENT_STORAGE, AESM_WHITE_LIST_CERT_FID, &white_cert_size);
  174. if(AE_SUCCESS == ae_err && white_cert_size ==0){//file not existing or 0 size
  175. AESM_DBG_TRACE("no white cert list available in persistent storage");
  176. return AE_SUCCESS;
  177. }
  178. if(AE_SUCCESS != ae_err)
  179. return ae_err;
  180. uint8_t *p = (uint8_t *)malloc(white_cert_size);
  181. if(NULL == p){
  182. AESM_DBG_ERROR("out of memory");
  183. return AE_OUT_OF_MEMORY_ERROR;
  184. }
  185. ae_err = aesm_read_data(FT_PERSISTENT_STORAGE, AESM_WHITE_LIST_CERT_FID, p, &white_cert_size);
  186. if(AE_SUCCESS != ae_err){
  187. AESM_DBG_WARN("Fail to read white cert list file");
  188. free(p);
  189. return ae_err;
  190. }
  191. ae_err = (ae_error_t)white_list_register(p, white_cert_size,false);//Need not save the data to file again
  192. if(AE_SUCCESS!=ae_err){
  193. AESM_DBG_WARN("fail to register white cert list file in persistent storage");
  194. }
  195. free(p);
  196. return ae_err;
  197. }
  198. //This function must be called after white list cert has been verified and the file may overwrite the original one
  199. ae_error_t CLEClass::load_white_cert_list_to_be_verify()
  200. {
  201. ae_error_t ae_err;
  202. uint32_t white_cert_size=0;
  203. ae_err = aesm_query_data_size(FT_PERSISTENT_STORAGE, AESM_WHITE_LIST_CERT_TO_BE_VERIFY_FID, &white_cert_size);
  204. if(AE_SUCCESS != ae_err || white_cert_size ==0){//file not existing or 0 size
  205. AESM_DBG_TRACE("no white cert list to be verify in persistent storage");
  206. return AE_SUCCESS;
  207. }
  208. uint8_t *p = (uint8_t *)malloc(white_cert_size);
  209. if(NULL == p){
  210. AESM_DBG_ERROR("out of memory");
  211. return AE_OUT_OF_MEMORY_ERROR;
  212. }
  213. ae_err = aesm_read_data(FT_PERSISTENT_STORAGE, AESM_WHITE_LIST_CERT_TO_BE_VERIFY_FID, p, &white_cert_size);
  214. if(AE_SUCCESS != ae_err){
  215. AESM_DBG_WARN("Fail to read white cert list file");
  216. free(p);
  217. return ae_err;
  218. }
  219. ae_err = (ae_error_t)white_list_register(p, white_cert_size,true);//We need to overwrite the original white list file if the file is passed
  220. if(AE_SUCCESS!=ae_err){
  221. AESM_DBG_WARN("fail to register white cert list file in persistent storage");
  222. }
  223. {//Always remove the file now. If it is not verified, the file has problem and remove it; otherwise, it has been saved as the AESM_WHITE_LIST_CERT_FID
  224. char white_list_to_be_verify_path_name[MAX_PATH];
  225. ae_err = aesm_get_pathname(FT_PERSISTENT_STORAGE, AESM_WHITE_LIST_CERT_TO_BE_VERIFY_FID, white_list_to_be_verify_path_name, MAX_PATH);
  226. if(AE_SUCCESS == ae_err){
  227. se_delete_tfile(white_list_to_be_verify_path_name);
  228. }
  229. }
  230. free(p);
  231. return ae_err;
  232. }
  233. ae_error_t CLEClass::load_enclave_only()
  234. {
  235. before_enclave_load();
  236. assert(m_enclave_id==0);
  237. sgx_status_t ret;
  238. ae_error_t ae_err;
  239. char prod_css_path[MAX_PATH]={0};
  240. char enclave_path[MAX_PATH]= {0};
  241. char *p_prod_css_path = prod_css_path;
  242. int production_le_loaded = 0;
  243. if((ae_err = aesm_get_pathname(FT_PERSISTENT_STORAGE, LE_PROD_SIG_STRUCT_FID, prod_css_path, MAX_PATH))!=AE_SUCCESS){
  244. AESM_DBG_WARN("fail to get production sig struction of LE");
  245. p_prod_css_path = NULL;
  246. }
  247. if((ae_err = aesm_get_pathname(FT_ENCLAVE_NAME, get_enclave_fid(), enclave_path,//get non-production signed LE pathname
  248. MAX_PATH))
  249. !=AE_SUCCESS){
  250. AESM_DBG_ERROR("fail to get LE pathname");
  251. return ae_err;
  252. }
  253. int launch_token_update;
  254. // in the ref LE we do not support loading non-production signed LE, as it should used with LCP a developer may
  255. // load a non-production launch enclave by setting the non-production provider to the IA32_SGXLEPUBKEYHASH0..3 MSRs.
  256. #if defined(AESM_SIM) || defined(REF_LE)
  257. UNUSED(p_prod_css_path);
  258. UNUSED(production_le_loaded);
  259. ret = sgx_create_enclave(enclave_path, get_debug_flag(), &m_launch_token,
  260. &launch_token_update, &m_enclave_id,
  261. &m_attributes);//simulation or ref_le mode has no sgx_create_le function. Use sgx_create_enclave
  262. if(ret != SGX_SUCCESS){
  263. AESM_DBG_ERROR("Fail to load LE");
  264. return AE_FAILURE;
  265. }
  266. #ifdef REF_LE
  267. AESM_DBG_DEBUG("ref_le loaded succesfully");
  268. #endif
  269. m_ufd = false;
  270. #else
  271. ret = sgx_create_le(enclave_path, p_prod_css_path, get_debug_flag(), &m_launch_token,
  272. &launch_token_update, &m_enclave_id,
  273. &m_attributes, &production_le_loaded);
  274. if (ret == SGX_ERROR_NO_DEVICE){
  275. AESM_DBG_ERROR("AE SERVER NOT AVAILABLE in load non-production signed LE: %s",enclave_path);
  276. return AESM_AE_NO_DEVICE;
  277. }
  278. if(ret == SGX_ERROR_OUT_OF_EPC)
  279. {
  280. AESM_DBG_ERROR("Loading LE failed due to out of epc");
  281. return AESM_AE_OUT_OF_EPC;
  282. }
  283. if (ret != SGX_SUCCESS){
  284. AESM_DBG_ERROR("Loading LE failed:%d",ret);
  285. return AE_SERVER_NOT_AVAILABLE;
  286. }else if(production_le_loaded!=0){//production signed LE loaded
  287. m_ufd = false;
  288. AESM_DBG_INFO("Production signed LE loaded, try loading white list now");
  289. }else{
  290. m_ufd = true;
  291. AESM_DBG_INFO("Debug signed LE loaded");
  292. }
  293. #endif
  294. return AE_SUCCESS;
  295. }
  296. ae_error_t CLEClass::load_enclave()
  297. {
  298. if(m_enclave_id){//LE has been loaded before
  299. return AE_SUCCESS;
  300. }
  301. ae_error_t ae_err = load_enclave_only();
  302. if( AE_SUCCESS == ae_err){
  303. load_white_cert_list();
  304. }
  305. return ae_err;
  306. }
  307. int CLEClass::get_launch_token(
  308. uint8_t * mrenclave, uint32_t mrenclave_size,
  309. uint8_t *public_key, uint32_t public_key_size,
  310. uint8_t *se_attributes, uint32_t se_attributes_size,
  311. uint8_t * lictoken, uint32_t lictoken_size,
  312. uint32_t *ae_mrsigner_index
  313. )
  314. {
  315. sgx_status_t ret = SGX_SUCCESS;
  316. int retry = 0;
  317. int status = 0;
  318. assert(m_enclave_id);
  319. sgx_measurement_t mrsigner;
  320. if(mrenclave_size !=sizeof(sgx_measurement_t) ||
  321. SE_KEY_SIZE != public_key_size ||
  322. se_attributes_size != sizeof(sgx_attributes_t) ||
  323. lictoken_size < sizeof(token_t) ||
  324. lictoken == NULL)
  325. return LE_INVALID_PARAMETER;
  326. //set mrsigner based on the hash of isv pub key from enclave signature
  327. IppStatus ipperrorCode = ippStsNoErr;
  328. ipperrorCode = ippsHashMessage(reinterpret_cast<const Ipp8u *>(public_key), public_key_size, reinterpret_cast<Ipp8u *>(&mrsigner), IPP_ALG_HASH_SHA256);
  329. if( ipperrorCode != ippStsNoErr){
  330. return AE_FAILURE;
  331. }
  332. if(ae_mrsigner_index!=NULL){
  333. *ae_mrsigner_index = UINT32_MAX;
  334. for(uint32_t i=0;i<sizeof(G_SERVICE_ENCLAVE_MRSIGNER)/sizeof(G_SERVICE_ENCLAVE_MRSIGNER[0]);i++){
  335. if(memcmp(&G_SERVICE_ENCLAVE_MRSIGNER[i], &mrsigner, sizeof(mrsigner))==0){
  336. *ae_mrsigner_index=i;
  337. break;
  338. }
  339. }
  340. }
  341. #ifdef DBG_LOG
  342. char mrsigner_info[256];
  343. sgx_attributes_t *attr = (sgx_attributes_t *)se_attributes;
  344. aesm_dbg_format_hex((uint8_t *)&mrsigner, sizeof(mrsigner), mrsigner_info, 256);
  345. AESM_DBG_INFO("try to load Enclave with mrsigner:%s , attr %llx, xfrm %llx", mrsigner_info, attr->flags, attr->xfrm);
  346. #endif
  347. // the interface of the get token API is identical, only the name is different
  348. #ifdef REF_LE
  349. #define le_get_launch_token_wrapper ref_le_get_launch_token
  350. #endif // REF_LE
  351. //get launch token by ecall into LE
  352. ret = le_get_launch_token_wrapper(m_enclave_id, &status,
  353. reinterpret_cast<sgx_measurement_t*>(mrenclave),
  354. &mrsigner,
  355. reinterpret_cast<sgx_attributes_t*>(se_attributes),
  356. reinterpret_cast<token_t*>(lictoken));
  357. for(; ret == SGX_ERROR_ENCLAVE_LOST && retry < AESM_RETRY_COUNT; retry++)
  358. {
  359. unload_enclave();
  360. if(AE_SUCCESS != load_enclave())
  361. return AE_FAILURE;
  362. ret = le_get_launch_token_wrapper(m_enclave_id, &status,
  363. reinterpret_cast<sgx_measurement_t*>(mrenclave),
  364. &mrsigner,
  365. reinterpret_cast<sgx_attributes_t*>(se_attributes),
  366. reinterpret_cast<token_t*>(lictoken));
  367. }
  368. AESM_DBG_INFO("token request returned with ret = %d, status = %d", ret, status);
  369. if(SGX_SUCCESS!=ret)
  370. return sgx_error_to_ae_error(ret);
  371. if (status == LE_WHITELIST_UNINITIALIZED_ERROR || status == LE_INVALID_PRIVILEGE_ERROR){
  372. start_white_list_thread(0);//try to query white list unblocking
  373. }
  374. if(is_ufd()){
  375. reinterpret_cast<token_t*>(lictoken)->body.valid = 0;
  376. }
  377. return status;
  378. }