EnclaveMessageExchange.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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_trts.h"
  32. #include "sgx_utils.h"
  33. #include "EnclaveMessageExchange.h"
  34. #include "sgx_eid.h"
  35. #include "error_codes.h"
  36. #include "sgx_ecp_types.h"
  37. #include "sgx_thread.h"
  38. #include <map>
  39. #include "dh_session_protocol.h"
  40. #include "sgx_dh.h"
  41. #include "sgx_tcrypto.h"
  42. #include "LocalAttestationCode_t.h"
  43. #include "sgx_tseal.h"
  44. #ifdef __cplusplus
  45. extern "C" {
  46. #endif
  47. uint32_t verify_peer_enclave_trust(sgx_dh_session_enclave_identity_t* peer_enclave_identity);
  48. #ifdef __cplusplus
  49. }
  50. #endif
  51. #define MAX_SESSION_COUNT 16
  52. ATTESTATION_STATUS generate_session_id(uint32_t *session_id);
  53. ATTESTATION_STATUS end_session();
  54. uint32_t one_successful_la_done;
  55. sgx_ecc_state_handle_t ecc_state;
  56. uint32_t session_ids[MAX_SESSION_COUNT];
  57. // Our enclave will not be doing LA with more than 1 decryptor enclave at a time.
  58. // We should not need this.
  59. //std::map<int, dh_session_t>g_dest_session_info_map;
  60. dh_session_t global_session_info;
  61. // TODO: May be we need to store all previously assigned session IDs instead of just the counter; to prevent replay attacks -
  62. uint32_t global_session_id=0;
  63. uint8_t apache_key[16];
  64. uint8_t verifier_key[16];
  65. //Handle the request from Source Enclave for a session
  66. ATTESTATION_STATUS session_request(sgx_dh_msg1_t *dh_msg1,
  67. uint32_t *session_id )
  68. {
  69. // dh_session_t session_info;
  70. sgx_dh_session_t sgx_dh_session;
  71. sgx_status_t status = SGX_SUCCESS;
  72. if(!session_id || !dh_msg1)
  73. {
  74. return INVALID_PARAMETER_ERROR;
  75. }
  76. //Intialize the session as a session responder
  77. status = sgx_dh_init_session(SGX_DH_SESSION_RESPONDER, &sgx_dh_session);
  78. if(SGX_SUCCESS != status)
  79. {
  80. return status;
  81. }
  82. *session_id=1;
  83. global_session_info.status = IN_PROGRESS;
  84. //Generate Message1 that will be returned to Source Enclave
  85. status = sgx_dh_responder_gen_msg1((sgx_dh_msg1_t*)dh_msg1, &sgx_dh_session);
  86. if(SGX_SUCCESS != status)
  87. {
  88. global_session_id--;
  89. // SAFE_FREE(g_session_id_tracker[*session_id]);
  90. return status;
  91. }
  92. memcpy(&global_session_info.in_progress.dh_session, &sgx_dh_session, sizeof(sgx_dh_session_t));
  93. //return sgx_seal_data(0, NULL, 0, NULL, 0, NULL);
  94. //Store the session information under the correspoding source enlave id key
  95. // g_dest_session_info_map.insert(std::pair<sgx_enclave_id_t, dh_session_t>(src_enclave_id, session_info));
  96. return status;
  97. }
  98. //Verify Message 2, generate Message3 and exchange Message 3 with Source Enclave
  99. ATTESTATION_STATUS exchange_report(
  100. sgx_dh_msg2_t *dh_msg2,
  101. sgx_dh_msg3_t *dh_msg3,
  102. uint32_t* session_id, uint8_t* read_or_write)
  103. {
  104. sgx_key_128bit_t dh_aek; // Session key
  105. // dh_session_t session_info;
  106. ATTESTATION_STATUS status = SUCCESS;
  107. sgx_dh_session_t sgx_dh_session;
  108. sgx_dh_session_enclave_identity_t initiator_identity;
  109. if(!dh_msg2 || !dh_msg3)
  110. {
  111. return INVALID_PARAMETER_ERROR;
  112. }
  113. memset(&dh_aek,0, sizeof(sgx_key_128bit_t));
  114. // Why is there a do-while loop anyway? It seems like there is no successful exit ...
  115. // do
  116. // {
  117. // TODO: Make sure that this works - pointers
  118. // session_info = global_session_info;
  119. if(global_session_info.status != IN_PROGRESS)
  120. {
  121. status = INVALID_SESSION;
  122. end_session();
  123. }
  124. memcpy(&sgx_dh_session, &global_session_info.in_progress.dh_session, sizeof(sgx_dh_session_t));
  125. dh_msg3->msg3_body.additional_prop_length = 0;
  126. //Process message 2 from source enclave and obtain message 3
  127. sgx_status_t se_ret = sgx_dh_responder_proc_msg2(dh_msg2,
  128. dh_msg3,
  129. &sgx_dh_session,
  130. &dh_aek,
  131. &initiator_identity);
  132. if(SGX_SUCCESS != se_ret)
  133. {
  134. status = se_ret;
  135. end_session();
  136. }
  137. uint32_t hash_count;
  138. // THIS IS WHERE THE DECRYPTOR VERIFIES THE APACHE'S MRSIGNER IS THE PUBLIC KEY GIVEN AFTER THE LOCAL ATTESTATION WITH THE VERIFIER.
  139. //Verify source enclave's trust
  140. uint32_t verify_return=verify_peer_enclave_trust(&initiator_identity);
  141. if(verify_return!=0)
  142. return verify_return;
  143. if(one_successful_la_done == 0)
  144. {
  145. one_successful_la_done = 1; *read_or_write=1;
  146. memcpy(verifier_key, &dh_aek, 16);
  147. }
  148. else
  149. {
  150. one_successful_la_done=2; *read_or_write=0;
  151. memcpy(apache_key, &dh_aek, 16);
  152. }
  153. // TODO: Verify that these changes will be lost on update.
  154. //save the session ID, status and initialize the session nonce
  155. global_session_info.session_id = *session_id;
  156. global_session_info.status = ACTIVE;
  157. global_session_info.active.counter = 0;
  158. memcpy(&global_session_info.active.AEK, &dh_aek, sizeof(sgx_key_128bit_t));
  159. memset(&dh_aek,0, sizeof(sgx_key_128bit_t));
  160. return status;
  161. }
  162. uint32_t calculate_sealed_data_size( uint32_t input_size)
  163. {
  164. // *op_size=sgx_calc_sealed_data_size(0, input_size);
  165. return sgx_calc_sealed_data_size(0, input_size);
  166. }
  167. // TODO: Fix this.
  168. //Respond to the request from the Source Enclave to close the session
  169. ATTESTATION_STATUS end_session(/**/)
  170. {
  171. return SUCCESS;
  172. }
  173. /*
  174. // Session_id is set to the first index of the pointer array that is non-null.(Not sure how it is ensured that all of them point to NULL at the start)
  175. // Why can't it just keep a counter that is incremented? What are the values of g_session_id_tracker array?
  176. //Returns a new sessionID for the source destination session
  177. ATTESTATION_STATUS generate_session_id(uint32_t *session_id)
  178. {
  179. ATTESTATION_STATUS status = SUCCESS;
  180. if(!session_id)
  181. {
  182. return INVALID_PARAMETER_ERROR;
  183. }
  184. //if the session structure is untintialized, set that as the next session ID
  185. for (int i = 0; i < MAX_SESSION_COUNT; i++)
  186. {
  187. if (g_session_id_tracker[i] == NULL)
  188. {
  189. *session_id = i;
  190. return status;
  191. }
  192. }
  193. status = NO_AVAILABLE_SESSION_ERROR;
  194. return status;
  195. */
  196. // *session_id=++global_session_id;
  197. //}