Utility_E1.cpp 7.8 KB

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