@@ -1040,79 +1040,90 @@ void ApiHandler::http_handler(beast::string_view /*doc_root*/,
10401040 // ----- Unseal file ----- //
10411041 crust_status_t crust_status = CRUST_SUCCESS;
10421042 sgx_status_t sgx_status = SGX_SUCCESS;
1043- if (! is_file_exist ( index_path. c_str (), STORE_TYPE_FILE) )
1043+ if (index_path == EMPTY_BLOCK_FLAG )
10441044 {
1045- std::string cid_header = index_path.substr (0 , index_path.find_last_of (' /' ));
1046- if (cid_header.size () <= UUID_LENGTH * 2 )
1047- {
1048- ret_info = " Malwared index path:" + index_path;
1049- ret_code = 404 ;
1050- }
1051- else
1045+ ret_info = " Unseal data successfully!" ;
1046+ ret_code = 200 ;
1047+ res.body ().clear ();
1048+ res.result (ret_code);
1049+ }
1050+ else
1051+ {
1052+ if (!is_file_exist (index_path.c_str (), STORE_TYPE_FILE))
10521053 {
1053- std::string cid = cid_header.substr (UUID_LENGTH * 2 , cid_header.size () - (UUID_LENGTH * 2 ));
1054- std::string type;
1055- bool exist = ed->find_file_type (cid, type);
1056- if (!exist || (exist && type.compare (FILE_TYPE_PENDING) == 0 ))
1054+ std::string cid_header = index_path.substr (0 , index_path.find_last_of (' /' ));
1055+ if (cid_header.size () <= UUID_LENGTH * 2 )
10571056 {
1058- ret_info = " Requested cid:' " + cid + " ' is not existed. " ;
1057+ ret_info = " Malwared index path: " + index_path ;
10591058 ret_code = 404 ;
10601059 }
10611060 else
10621061 {
1063- ret_info = " File block:'" + index_path + " ' is lost" ;
1064- ret_code = 410 ;
1062+ std::string cid = cid_header.substr (UUID_LENGTH * 2 , cid_header.size () - (UUID_LENGTH * 2 ));
1063+ std::string type;
1064+ bool exist = ed->find_file_type (cid, type);
1065+ if (!exist || (exist && type.compare (FILE_TYPE_PENDING) == 0 ))
1066+ {
1067+ ret_info = " Requested cid:'" + cid + " ' is not existed." ;
1068+ ret_code = 404 ;
1069+ }
1070+ else
1071+ {
1072+ ret_info = " File block:'" + index_path + " ' is lost" ;
1073+ ret_code = 410 ;
1074+ }
10651075 }
1066- }
1067- p_log->debug (" %s\n " , ret_info.c_str ());
1068- }
1069- else
1070- {
1071- size_t decrypted_data_sz = get_file_size (index_path.c_str (), STORE_TYPE_FILE);
1072- uint8_t *p_decrypted_data = (uint8_t *)malloc (decrypted_data_sz);
1073- size_t decrypted_data_sz_r = 0 ;
1074- memset (p_decrypted_data, 0 , decrypted_data_sz);
1075- Defer def_decrypted_data ([&p_decrypted_data](void ) { free (p_decrypted_data); });
1076- if (SGX_SUCCESS != (sgx_status = Ecall_unseal_file (global_eid, &crust_status, index_path.c_str (), p_decrypted_data, decrypted_data_sz, &decrypted_data_sz_r)))
1077- {
1078- ret_info = " Unseal failed! Invoke SGX API failed! Error code:" + num_to_hexstring (sgx_status);
1079- p_log->err (" %s\n " , ret_info.c_str ());
1080- ret_code = 500 ;
1076+ p_log->debug (" %s\n " , ret_info.c_str ());
10811077 }
10821078 else
10831079 {
1084- if (CRUST_SUCCESS == crust_status)
1080+ size_t decrypted_data_sz = get_file_size (index_path.c_str (), STORE_TYPE_FILE);
1081+ uint8_t *p_decrypted_data = (uint8_t *)malloc (decrypted_data_sz);
1082+ size_t decrypted_data_sz_r = 0 ;
1083+ memset (p_decrypted_data, 0 , decrypted_data_sz);
1084+ Defer def_decrypted_data ([&p_decrypted_data](void ) { free (p_decrypted_data); });
1085+ if (SGX_SUCCESS != (sgx_status = Ecall_unseal_file (global_eid, &crust_status, index_path.c_str (), p_decrypted_data, decrypted_data_sz, &decrypted_data_sz_r)))
10851086 {
1086- ret_info = " Unseal data successfully!" ;
1087- ret_code = 200 ;
1088- // p_log->info("%s\n", ret_info.c_str());
1089- res.body ().clear ();
1090- res.body ().append (reinterpret_cast <char *>(p_decrypted_data), decrypted_data_sz_r);
1091- res.result (ret_code);
1087+ ret_info = " Unseal failed! Invoke SGX API failed! Error code:" + num_to_hexstring (sgx_status);
1088+ p_log->err (" %s\n " , ret_info.c_str ());
1089+ ret_code = 500 ;
10921090 }
10931091 else
10941092 {
1095- switch ( crust_status)
1093+ if (CRUST_SUCCESS == crust_status)
10961094 {
1097- case CRUST_UNSEAL_DATA_FAILED:
1098- ret_info = " Unseal data failed! SGX unseal data failed!" ;
1099- p_log->err (" %s\n " , ret_info.c_str ());
1100- ret_code = 400 ;
1101- break ;
1102- case CRUST_UPGRADE_IS_UPGRADING:
1103- ret_info = " Unseal file stoped due to upgrading or exiting" ;
1104- p_log->info (" %s\n " , ret_info.c_str ());
1105- ret_code = 503 ;
1106- break ;
1107- default :
1108- ret_info = " Unseal data failed! Error code:" + num_to_hexstring (crust_status);
1109- p_log->err (" %s\n " , ret_info.c_str ());
1110- ret_code = 404 ;
1095+ ret_info = " Unseal data successfully!" ;
1096+ ret_code = 200 ;
1097+ // p_log->info("%s\n", ret_info.c_str());
1098+ res.body ().clear ();
1099+ res.body ().append (reinterpret_cast <char *>(p_decrypted_data), decrypted_data_sz_r);
1100+ res.result (ret_code);
1101+ }
1102+ else
1103+ {
1104+ switch (crust_status)
1105+ {
1106+ case CRUST_UNSEAL_DATA_FAILED:
1107+ ret_info = " Unseal data failed! SGX unseal data failed!" ;
1108+ p_log->err (" %s\n " , ret_info.c_str ());
1109+ ret_code = 400 ;
1110+ break ;
1111+ case CRUST_UPGRADE_IS_UPGRADING:
1112+ ret_info = " Unseal file stoped due to upgrading or exiting" ;
1113+ p_log->info (" %s\n " , ret_info.c_str ());
1114+ ret_code = 503 ;
1115+ break ;
1116+ default :
1117+ ret_info = " Unseal data failed! Error code:" + num_to_hexstring (crust_status);
1118+ p_log->err (" %s\n " , ret_info.c_str ());
1119+ ret_code = 404 ;
1120+ }
11111121 }
11121122 }
11131123 }
11141124 }
11151125 }
1126+
11161127 if (200 != ret_code)
11171128 {
11181129 json::JSON ret_body;
0 commit comments