LEClass.cpp 13 KB

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