LEClass.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. 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);
  40. #include "launch_enclave_u.h"
  41. #include "launch_enclave_u.c"
  42. int CLEClass::white_list_register(
  43. const uint8_t *white_list_cert,
  44. uint32_t white_list_cert_size,
  45. bool save_to_persistent_storage)
  46. {
  47. sgx_status_t ret = SGX_SUCCESS;
  48. int retry = 0;
  49. uint32_t status = 0;
  50. AESMLogicLock locker(AESMLogic::_le_mutex);
  51. if (white_list_cert_size < sizeof(wl_cert_chain_t)){
  52. return LE_INVALID_PARAMETER;
  53. }
  54. assert(m_enclave_id);
  55. ret = le_init_white_list_wrapper(m_enclave_id, &status,
  56. const_cast<uint8_t*>(white_list_cert),
  57. white_list_cert_size);
  58. for(; ret == SGX_ERROR_ENCLAVE_LOST && retry < AESM_RETRY_COUNT; retry++)
  59. {
  60. unload_enclave();
  61. if(AE_SUCCESS != load_enclave_only())
  62. return AE_FAILURE;
  63. ret = le_init_white_list_wrapper(m_enclave_id, &status,
  64. const_cast<uint8_t*>(white_list_cert),
  65. white_list_cert_size);
  66. }
  67. if(SGX_SUCCESS!=ret)
  68. return sgx_error_to_ae_error(ret);
  69. AESM_DBG_TRACE("le_init_white_list_wrapper return %d",status);
  70. if(AE_SUCCESS == status&&save_to_persistent_storage){//successfully register the white list cert
  71. 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
  72. AESM_DBG_WARN("Fail to save white list cert in persistent storage");
  73. }
  74. }
  75. if (LE_WHITE_LIST_ALREADY_UPDATED == status) {
  76. status = AE_SUCCESS;
  77. }
  78. return status;
  79. }
  80. void CLEClass::load_white_cert_list()
  81. {
  82. load_verified_white_cert_list();
  83. load_white_cert_list_to_be_verify();//If this version is older than previous one, it will not be loaded
  84. }
  85. #include <time.h>
  86. #include "endpoint_select_info.h"
  87. #include "stdint.h"
  88. #define UPDATE_DURATION (24*3600)
  89. ae_error_t CLEClass::update_white_list_by_url()
  90. {
  91. static time_t last_updated_time = 0;
  92. int i = 0;
  93. ae_error_t ret = AE_FAILURE;
  94. time_t cur_time = time(NULL);
  95. if (last_updated_time + UPDATE_DURATION > cur_time){
  96. return LE_WHITE_LIST_QUERY_BUSY;
  97. }
  98. for (i = 0; i < 2; i++){//at most retry once if network error
  99. uint8_t *resp_buf = NULL;
  100. uint32_t resp_size = 0;
  101. const char *url = EndpointSelectionInfo::instance().get_server_url(SGX_WHITE_LIST_FILE);
  102. if (NULL == url){
  103. return OAL_CONFIG_FILE_ERROR;
  104. }
  105. ret = aesm_network_send_receive(url,
  106. NULL, 0, &resp_buf, &resp_size,GET, false);
  107. if (ret == OAL_NETWORK_UNAVAILABLE_ERROR){
  108. AESM_DBG_WARN("Network failure in getting white list...");
  109. continue;
  110. }
  111. if (ret == AE_SUCCESS){
  112. if (resp_buf != NULL && resp_size > 0){
  113. ret = (ae_error_t)instance().white_list_register(resp_buf, resp_size, true);
  114. #ifdef DBG_LOG
  115. if (AE_SUCCESS == ret&&resp_size >= sizeof(wl_cert_chain_t)){
  116. const wl_cert_chain_t* wl = reinterpret_cast<const wl_cert_chain_t*>(resp_buf);
  117. AESM_DBG_INFO("White list update request successful for Version: %d", _ntohl(wl->wl_cert.wl_version));
  118. }
  119. #endif
  120. }
  121. last_updated_time = cur_time;
  122. aesm_free_network_response_buffer(resp_buf);
  123. }
  124. break;
  125. }
  126. return ret;
  127. }
  128. ae_error_t CLEClass::load_verified_white_cert_list()
  129. {
  130. ae_error_t ae_err;
  131. uint32_t white_cert_size=0;
  132. ae_err = aesm_query_data_size(FT_PERSISTENT_STORAGE, AESM_WHITE_LIST_CERT_FID, &white_cert_size);
  133. if(AE_SUCCESS == ae_err && white_cert_size ==0){//file not existing or 0 size
  134. AESM_DBG_TRACE("no white cert list available in persistent storage");
  135. return AE_SUCCESS;
  136. }
  137. if(AE_SUCCESS != ae_err)
  138. return ae_err;
  139. uint8_t *p = (uint8_t *)malloc(white_cert_size);
  140. if(NULL == p){
  141. AESM_DBG_ERROR("out of memory");
  142. return AE_OUT_OF_MEMORY_ERROR;
  143. }
  144. ae_err = aesm_read_data(FT_PERSISTENT_STORAGE, AESM_WHITE_LIST_CERT_FID, p, &white_cert_size);
  145. if(AE_SUCCESS != ae_err){
  146. AESM_DBG_WARN("Fail to read white cert list file");
  147. free(p);
  148. return ae_err;
  149. }
  150. ae_err = (ae_error_t)white_list_register(p, white_cert_size,false);//Need not save the data to file again
  151. if(AE_SUCCESS!=ae_err){
  152. AESM_DBG_WARN("fail to register white cert list file in persistent storage");
  153. }
  154. free(p);
  155. return ae_err;
  156. }
  157. //This function must be called after white list cert has been verified and the file may overwrite the original one
  158. ae_error_t CLEClass::load_white_cert_list_to_be_verify()
  159. {
  160. ae_error_t ae_err;
  161. uint32_t white_cert_size=0;
  162. ae_err = aesm_query_data_size(FT_PERSISTENT_STORAGE, AESM_WHITE_LIST_CERT_TO_BE_VERIFY_FID, &white_cert_size);
  163. if(AE_SUCCESS != ae_err || white_cert_size ==0){//file not existing or 0 size
  164. AESM_DBG_TRACE("no white cert list to be verify in persistent storage");
  165. return AE_SUCCESS;
  166. }
  167. uint8_t *p = (uint8_t *)malloc(white_cert_size);
  168. if(NULL == p){
  169. AESM_DBG_ERROR("out of memory");
  170. return AE_OUT_OF_MEMORY_ERROR;
  171. }
  172. ae_err = aesm_read_data(FT_PERSISTENT_STORAGE, AESM_WHITE_LIST_CERT_TO_BE_VERIFY_FID, p, &white_cert_size);
  173. if(AE_SUCCESS != ae_err){
  174. AESM_DBG_WARN("Fail to read white cert list file");
  175. free(p);
  176. return ae_err;
  177. }
  178. 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
  179. if(AE_SUCCESS!=ae_err){
  180. AESM_DBG_WARN("fail to register white cert list file in persistent storage");
  181. }
  182. {//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
  183. char white_list_to_be_verify_path_name[MAX_PATH];
  184. 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);
  185. if(AE_SUCCESS == ae_err){
  186. se_delete_tfile(white_list_to_be_verify_path_name);
  187. }
  188. }
  189. free(p);
  190. return ae_err;
  191. }
  192. ae_error_t CLEClass::load_enclave_only()
  193. {
  194. before_enclave_load();
  195. assert(m_enclave_id==0);
  196. sgx_status_t ret;
  197. ae_error_t ae_err;
  198. char prod_css_path[MAX_PATH]={0};
  199. char enclave_path[MAX_PATH]= {0};
  200. char *p_prod_css_path = prod_css_path;
  201. int production_le_loaded = 0;
  202. if((ae_err = aesm_get_pathname(FT_PERSISTENT_STORAGE, LE_PROD_SIG_STRUCT_FID, prod_css_path, MAX_PATH))!=AE_SUCCESS){
  203. AESM_DBG_WARN("fail to get production sig struction of LE");
  204. p_prod_css_path = NULL;
  205. }
  206. if((ae_err = aesm_get_pathname(FT_ENCLAVE_NAME, get_enclave_fid(), enclave_path,//get non-production signed LE pathname
  207. MAX_PATH))
  208. !=AE_SUCCESS){
  209. AESM_DBG_ERROR("fail to get LE pathname");
  210. return ae_err;
  211. }
  212. int launch_token_update;
  213. #ifdef AESM_SIM
  214. UNUSED(p_prod_css_path);
  215. UNUSED(production_le_loaded);
  216. ret = sgx_create_enclave(enclave_path, get_debug_flag(), &m_launch_token,
  217. &launch_token_update, &m_enclave_id,
  218. &m_attributes);//simulation mode has no sgx_create_le function. Use sgx_create_enclave
  219. if(ret != SGX_SUCCESS){
  220. AESM_DBG_ERROR("Fail to load LE");
  221. return AE_FAILURE;
  222. }
  223. return AE_SUCCESS;
  224. #else
  225. ret = sgx_create_le(enclave_path, p_prod_css_path, get_debug_flag(), &m_launch_token,
  226. &launch_token_update, &m_enclave_id,
  227. &m_attributes, &production_le_loaded);
  228. if (ret == SGX_ERROR_NO_DEVICE){
  229. AESM_DBG_ERROR("AE SERVER NOT AVAILABLE in load non-production signed LE: %s",enclave_path);
  230. return AESM_AE_NO_DEVICE;
  231. }
  232. if(ret == SGX_ERROR_OUT_OF_EPC)
  233. {
  234. AESM_DBG_ERROR("Loading LE failed due to out of epc");
  235. return AESM_AE_OUT_OF_EPC;
  236. }
  237. if (ret != SGX_SUCCESS){
  238. AESM_DBG_ERROR("Loading LE failed:%d",ret);
  239. return AE_SERVER_NOT_AVAILABLE;
  240. }else if(production_le_loaded!=0){//production signed LE loaded
  241. m_ufd = false;
  242. AESM_DBG_INFO("Production signed LE loaded, try loading white list now");
  243. }else{
  244. m_ufd = true;
  245. AESM_DBG_INFO("Debug signed LE loaded");
  246. }
  247. return AE_SUCCESS;
  248. #endif
  249. }
  250. ae_error_t CLEClass::load_enclave()
  251. {
  252. if(m_enclave_id){//LE has been loaded before
  253. return AE_SUCCESS;
  254. }
  255. ae_error_t ae_err = load_enclave_only();
  256. if( AE_SUCCESS == ae_err){
  257. load_white_cert_list();
  258. }
  259. return ae_err;
  260. }
  261. int CLEClass::get_launch_token(
  262. uint8_t * mrenclave, uint32_t mrenclave_size,
  263. uint8_t *public_key, uint32_t public_key_size,
  264. uint8_t *se_attributes, uint32_t se_attributes_size,
  265. uint8_t * lictoken, uint32_t lictoken_size,
  266. uint32_t *ae_mrsigner_index
  267. )
  268. {
  269. sgx_status_t ret = SGX_SUCCESS;
  270. int retry = 0;
  271. int status = 0;
  272. assert(m_enclave_id);
  273. sgx_measurement_t mrsigner;
  274. if(mrenclave_size !=sizeof(sgx_measurement_t) ||
  275. SE_KEY_SIZE != public_key_size ||
  276. se_attributes_size != sizeof(sgx_attributes_t) ||
  277. lictoken_size < sizeof(token_t) ||
  278. lictoken == NULL)
  279. return LE_INVALID_PARAMETER;
  280. //set mrsigner based on the hash of isv pub key from enclave signature
  281. IppStatus ipperrorCode = ippStsNoErr;
  282. ipperrorCode = ippsHashMessage(reinterpret_cast<const Ipp8u *>(public_key), public_key_size, reinterpret_cast<Ipp8u *>(&mrsigner), IPP_ALG_HASH_SHA256);
  283. if( ipperrorCode != ippStsNoErr){
  284. return AE_FAILURE;
  285. }
  286. if(ae_mrsigner_index!=NULL){
  287. *ae_mrsigner_index = UINT32_MAX;
  288. for(uint32_t i=0;i<sizeof(G_SERVICE_ENCLAVE_MRSIGNER)/sizeof(G_SERVICE_ENCLAVE_MRSIGNER[0]);i++){
  289. if(memcmp(&G_SERVICE_ENCLAVE_MRSIGNER[i], &mrsigner, sizeof(mrsigner))==0){
  290. *ae_mrsigner_index=i;
  291. break;
  292. }
  293. }
  294. }
  295. #ifdef DBG_LOG
  296. char mrsigner_info[256];
  297. sgx_attributes_t *attr = (sgx_attributes_t *)se_attributes;
  298. aesm_dbg_format_hex((uint8_t *)&mrsigner, sizeof(mrsigner), mrsigner_info, 256);
  299. AESM_DBG_INFO("try to load Enclave with mrsigner:%s , attr %llx, xfrm %llx", mrsigner_info, attr->flags, attr->xfrm);
  300. #endif
  301. //get launch token by ecall into LE
  302. ret = le_get_launch_token_wrapper(m_enclave_id, &status,
  303. reinterpret_cast<sgx_measurement_t*>(mrenclave),
  304. &mrsigner,
  305. reinterpret_cast<sgx_attributes_t*>(se_attributes),
  306. reinterpret_cast<token_t*>(lictoken));
  307. for(; ret == SGX_ERROR_ENCLAVE_LOST && retry < AESM_RETRY_COUNT; retry++)
  308. {
  309. unload_enclave();
  310. if(AE_SUCCESS != load_enclave())
  311. return AE_FAILURE;
  312. ret = le_get_launch_token_wrapper(m_enclave_id, &status,
  313. reinterpret_cast<sgx_measurement_t*>(mrenclave),
  314. &mrsigner,
  315. reinterpret_cast<sgx_attributes_t*>(se_attributes),
  316. reinterpret_cast<token_t*>(lictoken));
  317. }
  318. if(SGX_SUCCESS!=ret)
  319. return sgx_error_to_ae_error(ret);
  320. if (status == LE_WHITELIST_UNINITIALIZED_ERROR || status == LE_INVALID_PRIVILEGE_ERROR){
  321. start_white_list_thread(0);//try to query white list unblocking
  322. }
  323. if(is_ufd()){
  324. reinterpret_cast<token_t*>(lictoken)->body.valid = 0;
  325. }
  326. return status;
  327. }