UntrustedEnclaveMessageExchange.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 "error_codes.h"
  33. #include "datatypes.h"
  34. #include "sgx_urts.h"
  35. #include "UntrustedEnclaveMessageExchange.h"
  36. #include "sgx_dh.h"
  37. #include <map>
  38. std::map<sgx_enclave_id_t, uint32_t>g_enclave_id_map;
  39. //Makes an sgx_ecall to the destination enclave to get session id and message1
  40. ATTESTATION_STATUS session_request_ocall(sgx_enclave_id_t src_enclave_id, sgx_enclave_id_t dest_enclave_id, sgx_dh_msg1_t* dh_msg1, uint32_t* session_id)
  41. {
  42. uint32_t status = 0;
  43. sgx_status_t ret = SGX_SUCCESS;
  44. uint32_t temp_enclave_no;
  45. std::map<sgx_enclave_id_t, uint32_t>::iterator it = g_enclave_id_map.find(dest_enclave_id);
  46. if(it != g_enclave_id_map.end())
  47. {
  48. temp_enclave_no = it->second;
  49. }
  50. else
  51. {
  52. return INVALID_SESSION;
  53. }
  54. switch(temp_enclave_no)
  55. {
  56. case 1:
  57. ret = Enclave1_session_request(dest_enclave_id, &status, src_enclave_id, dh_msg1, session_id);
  58. break;
  59. case 2:
  60. ret = Enclave2_session_request(dest_enclave_id, &status, src_enclave_id, dh_msg1, session_id);
  61. break;
  62. case 3:
  63. ret = Enclave3_session_request(dest_enclave_id, &status, src_enclave_id, dh_msg1, session_id);
  64. break;
  65. }
  66. if (ret == SGX_SUCCESS)
  67. return (ATTESTATION_STATUS)status;
  68. else
  69. return INVALID_SESSION;
  70. }
  71. //Makes an sgx_ecall to the destination enclave sends message2 from the source enclave and gets message 3 from the destination enclave
  72. ATTESTATION_STATUS exchange_report_ocall(sgx_enclave_id_t src_enclave_id, sgx_enclave_id_t dest_enclave_id, sgx_dh_msg2_t *dh_msg2, sgx_dh_msg3_t *dh_msg3, uint32_t session_id)
  73. {
  74. uint32_t status = 0;
  75. sgx_status_t ret = SGX_SUCCESS;
  76. uint32_t temp_enclave_no;
  77. std::map<sgx_enclave_id_t, uint32_t>::iterator it = g_enclave_id_map.find(dest_enclave_id);
  78. if(it != g_enclave_id_map.end())
  79. {
  80. temp_enclave_no = it->second;
  81. }
  82. else
  83. {
  84. return INVALID_SESSION;
  85. }
  86. switch(temp_enclave_no)
  87. {
  88. case 1:
  89. ret = Enclave1_exchange_report(dest_enclave_id, &status, src_enclave_id, dh_msg2, dh_msg3, session_id);
  90. break;
  91. case 2:
  92. ret = Enclave2_exchange_report(dest_enclave_id, &status, src_enclave_id, dh_msg2, dh_msg3, session_id);
  93. break;
  94. case 3:
  95. ret = Enclave3_exchange_report(dest_enclave_id, &status, src_enclave_id, dh_msg2, dh_msg3, session_id);
  96. break;
  97. }
  98. if (ret == SGX_SUCCESS)
  99. return (ATTESTATION_STATUS)status;
  100. else
  101. return INVALID_SESSION;
  102. }
  103. //Make an sgx_ecall to the destination enclave function that generates the actual response
  104. ATTESTATION_STATUS send_request_ocall(sgx_enclave_id_t src_enclave_id, sgx_enclave_id_t dest_enclave_id,secure_message_t* req_message, size_t req_message_size, size_t max_payload_size, secure_message_t* resp_message, size_t resp_message_size)
  105. {
  106. uint32_t status = 0;
  107. sgx_status_t ret = SGX_SUCCESS;
  108. uint32_t temp_enclave_no;
  109. std::map<sgx_enclave_id_t, uint32_t>::iterator it = g_enclave_id_map.find(dest_enclave_id);
  110. if(it != g_enclave_id_map.end())
  111. {
  112. temp_enclave_no = it->second;
  113. }
  114. else
  115. {
  116. return INVALID_SESSION;
  117. }
  118. switch(temp_enclave_no)
  119. {
  120. case 1:
  121. ret = Enclave1_generate_response(dest_enclave_id, &status, src_enclave_id, req_message, req_message_size, max_payload_size, resp_message, resp_message_size);
  122. break;
  123. case 2:
  124. ret = Enclave2_generate_response(dest_enclave_id, &status, src_enclave_id, req_message, req_message_size, max_payload_size, resp_message, resp_message_size);
  125. break;
  126. case 3:
  127. ret = Enclave3_generate_response(dest_enclave_id, &status, src_enclave_id, req_message, req_message_size, max_payload_size, resp_message, resp_message_size);
  128. break;
  129. }
  130. if (ret == SGX_SUCCESS)
  131. return (ATTESTATION_STATUS)status;
  132. else
  133. return INVALID_SESSION;
  134. }
  135. //Make an sgx_ecall to the destination enclave to close the session
  136. ATTESTATION_STATUS end_session_ocall(sgx_enclave_id_t src_enclave_id, sgx_enclave_id_t dest_enclave_id)
  137. {
  138. uint32_t status = 0;
  139. sgx_status_t ret = SGX_SUCCESS;
  140. uint32_t temp_enclave_no;
  141. std::map<sgx_enclave_id_t, uint32_t>::iterator it = g_enclave_id_map.find(dest_enclave_id);
  142. if(it != g_enclave_id_map.end())
  143. {
  144. temp_enclave_no = it->second;
  145. }
  146. else
  147. {
  148. return INVALID_SESSION;
  149. }
  150. switch(temp_enclave_no)
  151. {
  152. case 1:
  153. ret = Enclave1_end_session(dest_enclave_id, &status, src_enclave_id);
  154. break;
  155. case 2:
  156. ret = Enclave2_end_session(dest_enclave_id, &status, src_enclave_id);
  157. break;
  158. case 3:
  159. ret = Enclave3_end_session(dest_enclave_id, &status, src_enclave_id);
  160. break;
  161. }
  162. if (ret == SGX_SUCCESS)
  163. return (ATTESTATION_STATUS)status;
  164. else
  165. return INVALID_SESSION;
  166. }