endpoint_select_info.cpp 16 KB

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