endpoint_select_info.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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 "endpoint_select_info.h"
  32. #include "PVEClass.h"
  33. #include "prov_msg_size.h"
  34. #include "network_encoding_wrapper.h"
  35. #include "ipp_wrapper.h"
  36. #include "sgx_tcrypto.h"
  37. #include "ippcp.h"
  38. #include "ippcore.h"
  39. #include "aesm_xegd_blob.h"
  40. #include "peksk_pub.hh"
  41. #include "sgx_read_rand.h"
  42. #include <time.h>
  43. //Function to do basic checking of the endpoint selection blob. Esp to avoid no zero-ending in the input string url
  44. static bool is_valid_endpoint_selection_info(const endpoint_selection_infos_t& es_info)
  45. {
  46. if(es_info.aesm_data_type != AESM_DATA_ENDPOINT_SELECTION_INFOS)
  47. return false;
  48. if(es_info.aesm_data_version != AESM_DATA_ENDPOINT_SELECTION_VERSION)
  49. return false;
  50. if(strnlen(es_info.provision_url,MAX_PATH)>=MAX_PATH)
  51. return false;
  52. return true;
  53. }
  54. ae_error_t EndpointSelectionInfo::read_pek(endpoint_selection_infos_t& es_info)
  55. {
  56. ae_error_t ae_err=AE_SUCCESS;
  57. uint32_t es_info_size = sizeof(es_info);
  58. ae_err = aesm_read_data(FT_PERSISTENT_STORAGE, PROVISION_PEK_BLOB_FID, reinterpret_cast<uint8_t *>(&es_info), &es_info_size);
  59. if(AE_SUCCESS == ae_err && (es_info_size != sizeof(es_info)||!is_valid_endpoint_selection_info(es_info))){
  60. AESM_DBG_ERROR("Invalid ES result in persistent storage:size %d, expected size %d", es_info_size, sizeof(es_info));
  61. ae_err = OAL_FILE_ACCESS_ERROR;
  62. }
  63. if(AE_SUCCESS == ae_err){
  64. AESM_DBG_INFO("Read ES result from persistent storage successfully");
  65. }else{
  66. AESM_DBG_WARN("ES result in persistent storage failed to load:%d", ae_err);
  67. }
  68. return ae_err;
  69. }
  70. ae_error_t EndpointSelectionInfo::write_pek(const endpoint_selection_infos_t& es_info)
  71. {
  72. return aesm_write_data(FT_PERSISTENT_STORAGE, PROVISION_PEK_BLOB_FID, reinterpret_cast<const uint8_t *>(&es_info), sizeof(es_info));
  73. }
  74. static ae_error_t ipp_error_to_ae_error(IppStatus ipp_status)
  75. {
  76. if(ipp_status == ippStsNoErr) return AE_SUCCESS;
  77. else if(ipp_status == ippStsMemAllocErr||
  78. ipp_status == ippStsNoMemErr) return AE_OUT_OF_MEMORY_ERROR;
  79. else return AE_FAILURE;//unknown or unexpected ipp error
  80. }
  81. static bool is_valid_server_url_infos(const aesm_server_url_infos_t& server_urls)
  82. {
  83. if(server_urls.aesm_data_type!=AESM_DATA_SERVER_URL_INFOS||
  84. server_urls.aesm_data_version!=AESM_DATA_SERVER_URL_VERSION&&
  85. server_urls.aesm_data_version != AESM_DATA_SERVER_URL_VERSION_1)//still support version 1 since the first 3 urls in version 1 is still same as the urls in version 2
  86. return false;
  87. if(strnlen(server_urls.endpoint_url,MAX_PATH)>=MAX_PATH)
  88. return false;
  89. if (strnlen(server_urls.pse_rl_url, MAX_PATH) >= MAX_PATH)
  90. return false;
  91. if (strnlen(server_urls.pse_ocsp_url, MAX_PATH) >= MAX_PATH)
  92. return false;
  93. return true;
  94. }
  95. ae_error_t EndpointSelectionInfo::verify_file_by_xgid(uint32_t xgid)
  96. {
  97. if (xgid == DEFAULT_EGID){//always return true for DEFAULT_EGID
  98. return AE_SUCCESS;
  99. }
  100. aesm_server_url_infos_t urls;
  101. uint32_t server_urls_size = sizeof(urls);
  102. ae_error_t ae_err = aesm_read_data(FT_PERSISTENT_STORAGE, AESM_SERVER_URL_FID, reinterpret_cast<uint8_t *>(&urls), &server_urls_size, xgid);
  103. if (AE_SUCCESS != ae_err ||
  104. server_urls_size != sizeof(urls) ||
  105. !is_valid_server_url_infos(urls)){
  106. return OAL_CONFIG_FILE_ERROR;
  107. }
  108. return AE_SUCCESS;
  109. }
  110. //Function to read urls from configure files
  111. ae_error_t EndpointSelectionInfo::get_url_info()
  112. {
  113. ae_error_t ae_err=AE_SUCCESS;
  114. uint32_t server_urls_size = sizeof(_server_urls);
  115. ae_err = aesm_read_data(FT_PERSISTENT_STORAGE, AESM_SERVER_URL_FID, reinterpret_cast<uint8_t *>(&_server_urls), &server_urls_size, AESMLogic::get_active_extended_epid_group_id());
  116. if(AE_SUCCESS != ae_err ||
  117. server_urls_size != sizeof(_server_urls)||
  118. !is_valid_server_url_infos(_server_urls)){ //If fail to read or data format error, use default value
  119. _is_server_url_valid = false;
  120. if(AE_SUCCESS == ae_err){//File available but format error, report ERROR LOG
  121. AESM_LOG_WARN("Server URL Blob file format error");
  122. AESM_DBG_INFO("fail to read server url info from persistent storage, error code (%d), size %d, expected size %d",
  123. ae_err, server_urls_size, sizeof(_server_urls));
  124. ae_err = OAL_CONFIG_FILE_ERROR;
  125. }else{
  126. AESM_DBG_INFO("server url blob file not available in persistent storage");
  127. }
  128. if (AESMLogic::get_active_extended_epid_group_id() == DEFAULT_EGID){
  129. if (strcpy_s(_server_urls.endpoint_url, MAX_PATH, DEFAULT_URL) != 0)
  130. return AE_FAILURE;
  131. if (strcpy_s(_server_urls.pse_rl_url, MAX_PATH, DEFAULT_PSE_RL_URL) != 0)
  132. return AE_FAILURE;
  133. if (strcpy_s(_server_urls.pse_ocsp_url, MAX_PATH, DEFAULT_PSE_OCSP_URL) != 0)
  134. return AE_FAILURE;
  135. _is_server_url_valid = true;
  136. return AE_SUCCESS;
  137. }
  138. else{
  139. return ae_err;
  140. }
  141. }
  142. _is_server_url_valid = true;
  143. return AE_SUCCESS;
  144. }
  145. ae_error_t EndpointSelectionInfo::get_url_info(aesm_server_url_infos_t& server_url)
  146. {
  147. AESMLogicLock lock(_es_lock);
  148. if (!_is_server_url_valid){
  149. (void)get_url_info();
  150. }
  151. if (_is_server_url_valid)
  152. {
  153. if (memcpy_s(&server_url, sizeof(server_url), &_server_urls, sizeof(_server_urls)) != 0){
  154. return AE_FAILURE;
  155. }
  156. }
  157. else
  158. {
  159. return AE_FAILURE;
  160. }
  161. return AE_SUCCESS;
  162. }
  163. ae_error_t aesm_check_pek_signature(const signed_pek_t& signed_pek, const extended_epid_group_blob_t& xegb);
  164. IppStatus get_provision_server_rsa_pub_key_in_ipp_format(const signed_pek_t& pek, IppsRSAPublicKeyState **rsa_pub_key);
  165. //The function is to verify the PEK ECDSA Signature and RSA Signature for ES Msg2
  166. // When PvE uses PEK, it will re-check the ECDSA Signature
  167. //The function will only be called after ES protocol is completed. But it will not be called when reading data back from persitent storage
  168. //@param provision_ttl: The TTL field from ES Msg2 in little endian format
  169. //@param rsa_signature: The RSA Signature in ES Msg2, it is RSA Signature to XID:TTL:provision_url
  170. //@param xid: The transaction id (XID) of the ES Protocol
  171. //@return AE_SUCCESS if signature verification success and passed
  172. //@return PVE_MSG_ERROR if signature verification failed or message error
  173. //other kinds of error code could be returned too due to corresponding error situation
  174. ae_error_t EndpointSelectionInfo::verify_signature(const endpoint_selection_infos_t& es_info, uint8_t xid[XID_SIZE], uint8_t rsa_signature[PVE_RSA_KEY_BYTES], uint16_t provision_ttl)
  175. {
  176. //Do signature verification here
  177. ae_error_t ae_err = AE_SUCCESS;
  178. IppsRSAPublicKeyState *rsa_pub_key = NULL;
  179. Ipp8u *buffer = NULL;
  180. int public_key_buffer_size = 0;
  181. int vr = 0;
  182. uint16_t ttl=_htons(provision_ttl);
  183. IppStatus ipp_status = ippStsNoErr;
  184. uint8_t msg_buf[XID_SIZE + sizeof(ttl) + MAX_PATH];
  185. uint32_t buf_size = 0;
  186. extended_epid_group_blob_t xegb={0};
  187. if (AE_SUCCESS != (ae_err=XEGDBlob::instance().read(xegb))){
  188. return ae_err;
  189. }
  190. ae_err = aesm_check_pek_signature(es_info.pek, xegb);
  191. if(AE_SUCCESS != ae_err){
  192. AESM_DBG_ERROR("PEK Signature verifcation not passed:%d",ae_err);
  193. goto ret_point;
  194. }
  195. AESM_DBG_INFO("PEK signature verified successfully");
  196. buf_size = XID_SIZE +static_cast<uint32_t>(sizeof(ttl) + strnlen(es_info.provision_url, MAX_PATH));
  197. if(0!=memcpy_s(msg_buf,sizeof(msg_buf), xid, XID_SIZE)||
  198. 0!=memcpy_s(msg_buf+XID_SIZE, sizeof(ttl) + MAX_PATH, &ttl, sizeof(ttl))||
  199. 0!=memcpy_s(msg_buf+XID_SIZE+sizeof(ttl), MAX_PATH, es_info.provision_url, buf_size-XID_SIZE-sizeof(ttl))){
  200. ae_err = AE_FAILURE;
  201. AESM_DBG_ERROR("memcpy error");
  202. goto ret_point;
  203. }
  204. ipp_status = get_provision_server_rsa_pub_key_in_ipp_format(es_info.pek, &rsa_pub_key);
  205. if(ippStsNoErr != ipp_status){
  206. AESM_DBG_ERROR("Fail to load rsa public key from PEK:%d", ipp_status);
  207. ae_err = ipp_error_to_ae_error(ipp_status);
  208. goto ret_point;
  209. }
  210. ipp_status = ippsRSA_GetBufferSizePublicKey(&public_key_buffer_size, rsa_pub_key);
  211. if(ippStsNoErr != ipp_status){
  212. AESM_DBG_ERROR("Fail to get rsa public key size:%s", ipp_status);
  213. ae_err = ipp_error_to_ae_error(ipp_status);
  214. goto ret_point;
  215. }
  216. buffer = (Ipp8u *)malloc(public_key_buffer_size);
  217. if(NULL == buffer){
  218. AESM_DBG_ERROR("malloc error");
  219. ae_err = AE_OUT_OF_MEMORY_ERROR;
  220. goto ret_point;
  221. }
  222. ipp_status = ippsRSAVerify_PKCS1v15(msg_buf, buf_size, rsa_signature, &vr, rsa_pub_key, ippHashAlg_SHA256, buffer);
  223. if(ippStsNoErr != ipp_status){
  224. AESM_DBG_ERROR("Fail to verify rsa signature:%d", ipp_status);
  225. ae_err = ipp_error_to_ae_error(ipp_status);
  226. goto ret_point;
  227. }
  228. if(vr == 0){
  229. AESM_DBG_TRACE("rsa signature verification failed");
  230. ae_err = PVE_MSG_ERROR;
  231. goto ret_point;
  232. }else{
  233. AESM_DBG_TRACE("rsa signature verification passed");
  234. ae_err = AE_SUCCESS;
  235. }
  236. ret_point:
  237. if(NULL != rsa_pub_key){
  238. secure_free_rsa_pub_key(PVE_RSA_KEY_BYTES, sizeof(uint32_t), rsa_pub_key);
  239. }
  240. if(NULL != buffer){
  241. free(buffer);
  242. }
  243. return ae_err;
  244. }
  245. #define MAX_ENCLAVE_LOST_RETRY_TIME 1
  246. bool read_aesm_config(aesm_config_infos_t& infos);
  247. //Function to implement the end point selection protocol
  248. ae_error_t EndpointSelectionInfo::start_protocol(endpoint_selection_infos_t& es_info)
  249. {
  250. AESMLogicLock lock(_es_lock);
  251. uint32_t msg_size = 0;
  252. uint8_t *resp = NULL;
  253. uint32_t resp_size = 0;
  254. uint16_t provision_ttl = 0;
  255. uint8_t *msg = NULL;
  256. uint8_t rsa_signature[PVE_RSA_KEY_BYTES];
  257. gen_endpoint_selection_output_t enclave_output;
  258. ae_error_t ae_ret = AE_SUCCESS;
  259. uint32_t enclave_lost_count = 0;
  260. AESM_DBG_DEBUG("enter fun");
  261. memset(&es_info, 0, sizeof(es_info));
  262. memset(&enclave_output, 0, sizeof(enclave_output));
  263. if(!_is_server_url_valid){
  264. ae_ret = get_url_info();
  265. if(AE_SUCCESS != ae_ret){//It is not likely happen, only fail when memcpy_s failed
  266. AESM_DBG_ERROR("Fail to initialize server URL information");
  267. goto final_point;
  268. }
  269. }
  270. do{
  271. if((ae_ret = CPVEClass::instance().load_enclave())!=AE_SUCCESS){
  272. AESM_DBG_ERROR("Fail to load PVE enclave:%d", ae_ret);
  273. goto final_point;
  274. }
  275. //call PvE to generate the partition and xid
  276. ae_ret = static_cast<ae_error_t>(CPVEClass::instance().gen_es_msg1_data(&enclave_output));
  277. if(ae_ret == AE_ENCLAVE_LOST&& (++enclave_lost_count)<=MAX_ENCLAVE_LOST_RETRY_TIME ){
  278. CPVEClass::instance().unload_enclave();//unload and reload PvE when enclave lost encountered
  279. continue;
  280. }else if(ae_ret == AE_SUCCESS){
  281. break;
  282. }else{
  283. AESM_DBG_ERROR("fail to generate parition by PvE");
  284. goto final_point;
  285. }
  286. }while(1);
  287. AESM_DBG_TRACE("use parition %d from PvE", (int)enclave_output.selector_id);
  288. AESM_DBG_INFO("Connect to server url \"%s\" for endpoint selection", _server_urls.endpoint_url);
  289. msg_size = estimate_es_msg1_size();
  290. assert(msg_size>0);
  291. msg = static_cast<uint8_t *>(malloc(msg_size));
  292. if(msg == NULL){
  293. AESM_DBG_ERROR("malloc error");
  294. ae_ret = AE_OUT_OF_MEMORY_ERROR;
  295. goto final_point;
  296. }
  297. memset(msg, 0, msg_size);
  298. ae_ret = static_cast<ae_error_t>(CPVEClass::instance().gen_es_msg1(msg, msg_size, enclave_output));//Generate EndPoint Selection Msg1
  299. if(ae_ret != AE_SUCCESS){
  300. AESM_DBG_ERROR("ES msg1 generation failed:%d",ae_ret);
  301. goto final_point;
  302. }
  303. AESM_DBG_TRACE("ES msg1 generated");
  304. ae_ret = AESMNetworkEncoding::aesm_send_recv_msg_encoding(_server_urls.endpoint_url, msg, msg_size, resp, resp_size);//Encoding/send/receive/Decoding
  305. if(ae_ret != AE_SUCCESS){
  306. AESM_DBG_ERROR("fail to send ES msg1 to backend server:%d",ae_ret);
  307. if(OAL_PROXY_SETTING_ASSIST == ae_ret){//when proxy setting assistant required, return directly
  308. goto final_point;
  309. }
  310. if(read_pek(es_info)==AE_SUCCESS){
  311. ae_ret = AE_SUCCESS;//use es_info inside persistent storage and ignore network error
  312. }
  313. goto final_point;
  314. }
  315. assert(resp != NULL);
  316. AESM_DBG_TRACE("start to process ES msg2");
  317. ae_ret = static_cast<ae_error_t>(CPVEClass::instance().proc_es_msg2(resp, resp_size, es_info.provision_url, provision_ttl, enclave_output.xid, rsa_signature , es_info.pek));
  318. if(AE_SUCCESS != ae_ret){
  319. AESM_DBG_WARN("Fail to process ES msg2 from backend server:%d",ae_ret);
  320. goto final_point;
  321. }
  322. AESM_DBG_TRACE("ES Msg2 decoded successfully, ttl %ds",provision_ttl);
  323. ae_ret = verify_signature(es_info, enclave_output.xid, rsa_signature, provision_ttl);
  324. if(AE_SUCCESS != ae_ret){
  325. AESM_DBG_WARN("Signature verification in ES Msg2 failed");
  326. goto final_point;
  327. }
  328. AESM_DBG_TRACE("Signature in ES Msg2 verified");
  329. es_info.aesm_data_type = AESM_DATA_ENDPOINT_SELECTION_INFOS;
  330. es_info.aesm_data_version = AESM_DATA_ENDPOINT_SELECTION_VERSION;
  331. (void)write_pek(es_info);//ignore file writing error
  332. AESM_DBG_TRACE("end point selection succ, provisioning url: %s",es_info.provision_url);
  333. final_point:
  334. if(msg!=NULL)free(msg);
  335. if(resp!=NULL){
  336. AESMNetworkEncoding::aesm_free_response_msg(resp);
  337. }
  338. return ae_ret;
  339. }
  340. const char *EndpointSelectionInfo::get_server_url(aesm_network_server_enum_type_t type)
  341. {
  342. AESMLogicLock lock(_es_lock);
  343. if (type == SGX_WHITE_LIST_FILE){
  344. if (!_is_white_list_url_valid){
  345. (void)read_aesm_config(_config_urls);
  346. _is_white_list_url_valid = true;
  347. }
  348. return _config_urls.white_list_url;
  349. }
  350. if(!_is_server_url_valid){
  351. (void)get_url_info();
  352. }
  353. if(!_is_server_url_valid){
  354. return NULL;
  355. }
  356. switch(type){
  357. case ENDPOINT_SELECTION:
  358. return _server_urls.endpoint_url;
  359. case REVOCATION_LIST_RETRIEVAL:
  360. return _server_urls.pse_rl_url;
  361. case PSE_OCSP:
  362. return _server_urls.pse_ocsp_url;
  363. default://invalid case
  364. assert(0);
  365. return NULL;
  366. }
  367. }
  368. void EndpointSelectionInfo::get_proxy(uint32_t& proxy_type, char proxy_url[MAX_PATH])
  369. {
  370. AESMLogicLock lock(_es_lock);
  371. if(!_is_white_list_url_valid){
  372. (void)read_aesm_config(_config_urls);
  373. _is_white_list_url_valid=true;
  374. }
  375. proxy_type = _config_urls.proxy_type;
  376. strcpy(proxy_url, _config_urls.aesm_proxy);
  377. }