Utility_E2.cpp 7.2 KB

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