123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373 |
- #include "sgx_eid.h"
- #include "Enclave1_t.h"
- #include "EnclaveMessageExchange.h"
- #include "error_codes.h"
- #include "Utility_E1.h"
- #include "sgx_thread.h"
- #include "sgx_dh.h"
- #include <map>
- #define UNUSED(val) (void)(val)
- std::map<sgx_enclave_id_t, dh_session_t>g_src_session_info_map;
- static uint32_t e1_foo1_wrapper(ms_in_msg_exchange_t *ms, size_t param_lenth, char** resp_buffer, size_t* resp_length);
- const struct {
- size_t num_funcs;
- const void* table[1];
- } func_table = {
- 1,
- {
- (const void*)e1_foo1_wrapper,
- }
- };
- uint32_t test_create_session(sgx_enclave_id_t src_enclave_id,
- sgx_enclave_id_t dest_enclave_id)
- {
- ATTESTATION_STATUS ke_status = SUCCESS;
- dh_session_t dest_session_info;
-
- ke_status = create_session(src_enclave_id, dest_enclave_id, &dest_session_info);
-
- if(ke_status == SUCCESS)
- {
- g_src_session_info_map.insert(std::pair<sgx_enclave_id_t, dh_session_t>(dest_enclave_id, dest_session_info));
- }
- memset(&dest_session_info, 0, sizeof(dh_session_t));
- return ke_status;
- }
- uint32_t test_enclave_to_enclave_call(sgx_enclave_id_t src_enclave_id,
- sgx_enclave_id_t dest_enclave_id)
- {
- ATTESTATION_STATUS ke_status = SUCCESS;
- uint32_t var1,var2;
- uint32_t target_fn_id, msg_type;
- char* marshalled_inp_buff;
- size_t marshalled_inp_buff_len;
- char* out_buff;
- size_t out_buff_len;
- dh_session_t *dest_session_info;
- size_t max_out_buff_size;
- char* retval;
- var1 = 0x4;
- var2 = 0x5;
- target_fn_id = 0;
- msg_type = ENCLAVE_TO_ENCLAVE_CALL;
- max_out_buff_size = 50;
-
- ke_status = marshal_input_parameters_e2_foo1(target_fn_id, msg_type, var1, var2, &marshalled_inp_buff, &marshalled_inp_buff_len);
- if(ke_status != SUCCESS)
- {
- return ke_status;
- }
-
- std::map<sgx_enclave_id_t, dh_session_t>::iterator it = g_src_session_info_map.find(dest_enclave_id);
- if(it != g_src_session_info_map.end())
- {
- dest_session_info = &it->second;
- }
- else
- {
- SAFE_FREE(marshalled_inp_buff);
- return INVALID_SESSION;
- }
-
- ke_status = send_request_receive_response(src_enclave_id, dest_enclave_id, dest_session_info, marshalled_inp_buff,
- marshalled_inp_buff_len, max_out_buff_size, &out_buff, &out_buff_len);
- if(ke_status != SUCCESS)
- {
- SAFE_FREE(marshalled_inp_buff);
- SAFE_FREE(out_buff);
- return ke_status;
- }
-
- ke_status = unmarshal_retval_and_output_parameters_e2_foo1(out_buff, &retval);
- if(ke_status != SUCCESS)
- {
- SAFE_FREE(marshalled_inp_buff);
- SAFE_FREE(out_buff);
- return ke_status;
- }
- SAFE_FREE(marshalled_inp_buff);
- SAFE_FREE(out_buff);
- SAFE_FREE(retval);
- return SUCCESS;
- }
- uint32_t test_message_exchange(sgx_enclave_id_t src_enclave_id,
- sgx_enclave_id_t dest_enclave_id)
- {
- ATTESTATION_STATUS ke_status = SUCCESS;
- uint32_t target_fn_id, msg_type;
- char* marshalled_inp_buff;
- size_t marshalled_inp_buff_len;
- char* out_buff;
- size_t out_buff_len;
- dh_session_t *dest_session_info;
- size_t max_out_buff_size;
- char* secret_response;
- uint32_t secret_data;
- target_fn_id = 0;
- msg_type = MESSAGE_EXCHANGE;
- max_out_buff_size = 50;
- secret_data = 0x12345678;
-
- ke_status = marshal_message_exchange_request(target_fn_id, msg_type, secret_data, &marshalled_inp_buff, &marshalled_inp_buff_len);
- if(ke_status != SUCCESS)
- {
- return ke_status;
- }
-
- std::map<sgx_enclave_id_t, dh_session_t>::iterator it = g_src_session_info_map.find(dest_enclave_id);
- if(it != g_src_session_info_map.end())
- {
- dest_session_info = &it->second;
- }
- else
- {
- SAFE_FREE(marshalled_inp_buff);
- return INVALID_SESSION;
- }
-
- ke_status = send_request_receive_response(src_enclave_id, dest_enclave_id, dest_session_info, marshalled_inp_buff,
- marshalled_inp_buff_len, max_out_buff_size, &out_buff, &out_buff_len);
- if(ke_status != SUCCESS)
- {
- SAFE_FREE(marshalled_inp_buff);
- SAFE_FREE(out_buff);
- return ke_status;
- }
-
- ke_status = umarshal_message_exchange_response(out_buff, &secret_response);
- if(ke_status != SUCCESS)
- {
- SAFE_FREE(marshalled_inp_buff);
- SAFE_FREE(out_buff);
- return ke_status;
- }
- SAFE_FREE(marshalled_inp_buff);
- SAFE_FREE(out_buff);
- SAFE_FREE(secret_response);
- return SUCCESS;
- }
- uint32_t test_close_session(sgx_enclave_id_t src_enclave_id,
- sgx_enclave_id_t dest_enclave_id)
- {
- dh_session_t dest_session_info;
- ATTESTATION_STATUS ke_status = SUCCESS;
-
- std::map<sgx_enclave_id_t, dh_session_t>::iterator it = g_src_session_info_map.find(dest_enclave_id);
- if(it != g_src_session_info_map.end())
- {
- dest_session_info = it->second;
- }
- else
- {
- return NULL;
- }
-
- ke_status = close_session(src_enclave_id, dest_enclave_id);
-
- g_src_session_info_map.erase(dest_enclave_id);
- return ke_status;
- }
- extern "C" uint32_t verify_peer_enclave_trust(sgx_dh_session_enclave_identity_t* peer_enclave_identity)
- {
- if(!peer_enclave_identity)
- {
- return INVALID_PARAMETER_ERROR;
- }
- if(peer_enclave_identity->isv_prod_id != 0 || !(peer_enclave_identity->attributes.flags & SGX_FLAGS_INITTED))
-
- {
- return ENCLAVE_TRUST_ERROR;
- }
- else
- {
- return SUCCESS;
- }
- }
- extern "C" uint32_t enclave_to_enclave_call_dispatcher(char* decrypted_data,
- size_t decrypted_data_length,
- char** resp_buffer,
- size_t* resp_length)
- {
- ms_in_msg_exchange_t *ms;
- uint32_t (*fn1)(ms_in_msg_exchange_t *ms, size_t, char**, size_t*);
- if(!decrypted_data || !resp_length)
- {
- return INVALID_PARAMETER_ERROR;
- }
- ms = (ms_in_msg_exchange_t *)decrypted_data;
- if(ms->target_fn_id >= func_table.num_funcs)
- {
- return INVALID_PARAMETER_ERROR;
- }
- fn1 = (uint32_t (*)(ms_in_msg_exchange_t*, size_t, char**, size_t*))func_table.table[ms->target_fn_id];
- return fn1(ms, decrypted_data_length, resp_buffer, resp_length);
- }
- uint32_t get_message_exchange_response(uint32_t inp_secret_data)
- {
- uint32_t secret_response;
-
- secret_response = inp_secret_data & 0x11111111;
- return secret_response;
- }
- extern "C" uint32_t message_exchange_response_generator(char* decrypted_data,
- char** resp_buffer,
- size_t* resp_length)
- {
- ms_in_msg_exchange_t *ms;
- uint32_t inp_secret_data;
- uint32_t out_secret_data;
- if(!decrypted_data || !resp_length)
- {
- return INVALID_PARAMETER_ERROR;
- }
- ms = (ms_in_msg_exchange_t *)decrypted_data;
- if(umarshal_message_exchange_request(&inp_secret_data,ms) != SUCCESS)
- return ATTESTATION_ERROR;
- out_secret_data = get_message_exchange_response(inp_secret_data);
- if(marshal_message_exchange_response(resp_buffer, resp_length, out_secret_data) != SUCCESS)
- return MALLOC_ERROR;
- return SUCCESS;
- }
- static uint32_t e1_foo1(external_param_struct_t *p_struct_var)
- {
- if(!p_struct_var)
- {
- return INVALID_PARAMETER_ERROR;
- }
- (p_struct_var->var1)++;
- (p_struct_var->var2)++;
- (p_struct_var->p_internal_struct->ivar1)++;
- (p_struct_var->p_internal_struct->ivar2)++;
- return (p_struct_var->var1 + p_struct_var->var2 + p_struct_var->p_internal_struct->ivar1 + p_struct_var->p_internal_struct->ivar2);
- }
- static uint32_t e1_foo1_wrapper(ms_in_msg_exchange_t *ms,
- size_t param_lenth,
- char** resp_buffer,
- size_t* resp_length)
- {
- UNUSED(param_lenth);
- uint32_t ret;
- size_t len_data, len_ptr_data;
- external_param_struct_t *p_struct_var;
- internal_param_struct_t internal_struct_var;
- if(!ms || !resp_length)
- {
- return INVALID_PARAMETER_ERROR;
- }
- p_struct_var = (external_param_struct_t*)malloc(sizeof(external_param_struct_t));
- if(!p_struct_var)
- return MALLOC_ERROR;
- p_struct_var->p_internal_struct = &internal_struct_var;
- if(unmarshal_input_parameters_e1_foo1(p_struct_var, ms) != SUCCESS)
- {
- SAFE_FREE(p_struct_var);
- return ATTESTATION_ERROR;
- }
- ret = e1_foo1(p_struct_var);
- len_data = sizeof(external_param_struct_t) - sizeof(p_struct_var->p_internal_struct);
- len_ptr_data = sizeof(internal_struct_var);
- if(marshal_retval_and_output_parameters_e1_foo1(resp_buffer, resp_length, ret, p_struct_var, len_data, len_ptr_data) != SUCCESS)
- {
- SAFE_FREE(p_struct_var);
- return MALLOC_ERROR;
- }
- SAFE_FREE(p_struct_var);
- return SUCCESS;
- }
|