Utility_E3.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright (C) 2011-2017 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 "sgx_eid.h"
  32. #include "EnclaveMessageExchange.h"
  33. #include "error_codes.h"
  34. #include "Utility_E3.h"
  35. #include "stdlib.h"
  36. #include "string.h"
  37. uint32_t marshal_input_parameters_e1_foo1(uint32_t target_fn_id, uint32_t msg_type, external_param_struct_t *p_struct_var, size_t len_data, size_t len_ptr_data, char** marshalled_buff, size_t* marshalled_buff_len)
  38. {
  39. ms_in_msg_exchange_t *ms;
  40. size_t param_len, ms_len;
  41. char *temp_buff;
  42. int* addr;
  43. char* struct_data;
  44. if(!p_struct_var || !marshalled_buff_len)
  45. return INVALID_PARAMETER_ERROR;
  46. struct_data = (char*)p_struct_var;
  47. temp_buff = (char*)malloc(len_data + len_ptr_data);
  48. if(!temp_buff)
  49. return MALLOC_ERROR;
  50. memcpy(temp_buff, struct_data, len_data);
  51. addr = *(int **)(struct_data + len_data);
  52. memcpy(temp_buff + len_data, addr, len_ptr_data); //can be optimized
  53. param_len = len_data + len_ptr_data;
  54. ms_len = sizeof(ms_in_msg_exchange_t) + param_len;
  55. ms = (ms_in_msg_exchange_t *)malloc(ms_len);
  56. if(!ms)
  57. {
  58. SAFE_FREE(temp_buff);
  59. return MALLOC_ERROR;
  60. }
  61. ms->msg_type = msg_type;
  62. ms->target_fn_id = target_fn_id;
  63. ms->inparam_buff_len = (uint32_t)param_len;
  64. memcpy(&ms->inparam_buff, temp_buff, param_len);
  65. *marshalled_buff = (char*)ms;
  66. *marshalled_buff_len = ms_len;
  67. SAFE_FREE(temp_buff);
  68. return SUCCESS;
  69. }
  70. uint32_t marshal_retval_and_output_parameters_e3_foo1(char** resp_buffer, size_t* resp_length, uint32_t retval, param_struct_t *p_struct_var)
  71. {
  72. ms_out_msg_exchange_t *ms;
  73. size_t ret_param_len, ms_len;
  74. char *temp_buff;
  75. size_t retval_len;
  76. if(!resp_length || !p_struct_var)
  77. return INVALID_PARAMETER_ERROR;
  78. retval_len = sizeof(retval);
  79. ret_param_len = sizeof(retval) + sizeof(param_struct_t);
  80. temp_buff = (char*)malloc(ret_param_len);
  81. if(!temp_buff)
  82. return MALLOC_ERROR;
  83. memcpy(temp_buff, &retval, sizeof(retval));
  84. memcpy(temp_buff + sizeof(retval), p_struct_var, sizeof(param_struct_t));
  85. ms_len = sizeof(ms_out_msg_exchange_t) + ret_param_len;
  86. ms = (ms_out_msg_exchange_t *)malloc(ms_len);
  87. if(!ms)
  88. {
  89. SAFE_FREE(temp_buff);
  90. return MALLOC_ERROR;
  91. }
  92. ms->retval_len = (uint32_t)retval_len;
  93. ms->ret_outparam_buff_len = (uint32_t)ret_param_len;
  94. memcpy(&ms->ret_outparam_buff, temp_buff, ret_param_len);
  95. *resp_buffer = (char*)ms;
  96. *resp_length = ms_len;
  97. SAFE_FREE(temp_buff);
  98. return SUCCESS;
  99. }
  100. uint32_t unmarshal_input_parameters_e3_foo1(param_struct_t *pstruct, ms_in_msg_exchange_t* ms)
  101. {
  102. char* buff;
  103. size_t len;
  104. if(!pstruct || !ms)
  105. return INVALID_PARAMETER_ERROR;
  106. buff = ms->inparam_buff;
  107. len = ms->inparam_buff_len;
  108. if(len != (sizeof(pstruct->var1) + sizeof(pstruct->var2)))
  109. return ATTESTATION_ERROR;
  110. memcpy(&pstruct->var1, buff, sizeof(pstruct->var1));
  111. memcpy(&pstruct->var2, buff + sizeof(pstruct->var1), sizeof(pstruct->var2));
  112. return SUCCESS;
  113. }
  114. uint32_t unmarshal_retval_and_output_parameters_e1_foo1(char* out_buff, external_param_struct_t *p_struct_var, char** retval)
  115. {
  116. size_t retval_len;
  117. ms_out_msg_exchange_t *ms;
  118. if(!out_buff || !p_struct_var)
  119. return INVALID_PARAMETER_ERROR;
  120. ms = (ms_out_msg_exchange_t *)out_buff;
  121. retval_len = ms->retval_len;
  122. *retval = (char*)malloc(retval_len);
  123. if(!*retval)
  124. {
  125. return MALLOC_ERROR;
  126. }
  127. memcpy(*retval, ms->ret_outparam_buff, retval_len);
  128. memcpy(&p_struct_var->var1, (ms->ret_outparam_buff) + retval_len, sizeof(p_struct_var->var1));
  129. memcpy(&p_struct_var->var2, (ms->ret_outparam_buff) + retval_len + sizeof(p_struct_var->var1), sizeof(p_struct_var->var2));
  130. memcpy(&p_struct_var->p_internal_struct->ivar1, (ms->ret_outparam_buff) + retval_len + sizeof(p_struct_var->var1)+ sizeof(p_struct_var->var2), sizeof(p_struct_var->p_internal_struct->ivar1));
  131. memcpy(&p_struct_var->p_internal_struct->ivar2, (ms->ret_outparam_buff) + retval_len + sizeof(p_struct_var->var1)+ sizeof(p_struct_var->var2) + sizeof(p_struct_var->p_internal_struct->ivar1), sizeof(p_struct_var->p_internal_struct->ivar2));
  132. return SUCCESS;
  133. }
  134. uint32_t marshal_message_exchange_request(uint32_t target_fn_id, uint32_t msg_type, uint32_t secret_data, char** marshalled_buff, size_t* marshalled_buff_len)
  135. {
  136. ms_in_msg_exchange_t *ms;
  137. size_t secret_data_len, ms_len;
  138. if(!marshalled_buff_len)
  139. return INVALID_PARAMETER_ERROR;
  140. secret_data_len = sizeof(secret_data);
  141. ms_len = sizeof(ms_in_msg_exchange_t) + secret_data_len;
  142. ms = (ms_in_msg_exchange_t *)malloc(ms_len);
  143. if(!ms)
  144. return MALLOC_ERROR;
  145. ms->msg_type = msg_type;
  146. ms->target_fn_id = target_fn_id;
  147. ms->inparam_buff_len = (uint32_t)secret_data_len;
  148. memcpy(&ms->inparam_buff, &secret_data, secret_data_len);
  149. *marshalled_buff = (char*)ms;
  150. *marshalled_buff_len = ms_len;
  151. return SUCCESS;
  152. }
  153. uint32_t umarshal_message_exchange_request(uint32_t* inp_secret_data, ms_in_msg_exchange_t* ms)
  154. {
  155. char* buff;
  156. size_t len;
  157. if(!inp_secret_data || !ms)
  158. return INVALID_PARAMETER_ERROR;
  159. buff = ms->inparam_buff;
  160. len = ms->inparam_buff_len;
  161. if(len != sizeof(uint32_t))
  162. return ATTESTATION_ERROR;
  163. memcpy(inp_secret_data, buff, sizeof(uint32_t));
  164. return SUCCESS;
  165. }
  166. uint32_t marshal_message_exchange_response(char** resp_buffer, size_t* resp_length, uint32_t secret_response)
  167. {
  168. ms_out_msg_exchange_t *ms;
  169. size_t secret_response_len, ms_len;
  170. size_t retval_len, ret_param_len;
  171. if(!resp_length)
  172. return INVALID_PARAMETER_ERROR;
  173. secret_response_len = sizeof(secret_response);
  174. retval_len = secret_response_len;
  175. ret_param_len = secret_response_len;
  176. ms_len = sizeof(ms_out_msg_exchange_t) + ret_param_len;
  177. ms = (ms_out_msg_exchange_t *)malloc(ms_len);
  178. if(!ms)
  179. return MALLOC_ERROR;
  180. ms->retval_len = (uint32_t)retval_len;
  181. ms->ret_outparam_buff_len = (uint32_t)ret_param_len;
  182. memcpy(&ms->ret_outparam_buff, &secret_response, secret_response_len);
  183. *resp_buffer = (char*)ms;
  184. *resp_length = ms_len;
  185. return SUCCESS;
  186. }
  187. uint32_t umarshal_message_exchange_response(char* out_buff, char** secret_response)
  188. {
  189. size_t retval_len;
  190. ms_out_msg_exchange_t *ms;
  191. if(!out_buff)
  192. return INVALID_PARAMETER_ERROR;
  193. ms = (ms_out_msg_exchange_t *)out_buff;
  194. retval_len = ms->retval_len;
  195. *secret_response = (char*)malloc(retval_len);
  196. if(!*secret_response)
  197. {
  198. return MALLOC_ERROR;
  199. }
  200. memcpy(*secret_response, ms->ret_outparam_buff, retval_len);
  201. return SUCCESS;
  202. }