LEClass.cpp 14 KB

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