EnclaveMessageExchange.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. //uint32_t create_ecdsa_key_pair(sgx_ec256_public_t* pub_key, sgx_ec256_private_t* priv_key);
  52. //void serialize_key_pair_to_string( sgx_ec256_public_t* pub_key, sgx_ec256_private_t* signing_priv_key, uint8_t* private_public_key_string);
  53. //void deserialize_string_to_key_pair(uint8_t* private_public_key_string, sgx_ec256_public_t* pub_key, sgx_ec256_private_t* priv_key);
  54. #define MAX_SESSION_COUNT 16
  55. //number of open sessions
  56. // uint32_t g_session_count = 0;
  57. ATTESTATION_STATUS generate_session_id(uint32_t *session_id);
  58. ATTESTATION_STATUS end_session();
  59. //sgx_ec256_private_t signing_priv_key;
  60. uint32_t one_successful_la_done;
  61. sgx_ecc_state_handle_t ecc_state;
  62. uint32_t session_ids[MAX_SESSION_COUNT];
  63. // Our enclave will not be doing LA with more than 1 decryptor enclave at a time.
  64. // We should not need this.
  65. //std::map<int, dh_session_t>g_dest_session_info_map;
  66. dh_session_t global_session_info;
  67. // TODO: May be we need to store all previously assigned session IDs instead of just the counter; to prevent replay attacks -
  68. uint32_t global_session_id=0;
  69. //Handle the request from Source Enclave for a session
  70. ATTESTATION_STATUS session_request(sgx_dh_msg1_t *dh_msg1,
  71. uint32_t *session_id )
  72. {
  73. // dh_session_t session_info;
  74. sgx_dh_session_t sgx_dh_session;
  75. sgx_status_t status = SGX_SUCCESS;
  76. if(!session_id || !dh_msg1)
  77. {
  78. return INVALID_PARAMETER_ERROR;
  79. }
  80. //Intialize the session as a session responder
  81. status = sgx_dh_init_session(SGX_DH_SESSION_RESPONDER, &sgx_dh_session);
  82. if(SGX_SUCCESS != status)
  83. {
  84. return status;
  85. }
  86. *session_id=1;
  87. global_session_info.status = IN_PROGRESS;
  88. //Generate Message1 that will be returned to Source Enclave
  89. status = sgx_dh_responder_gen_msg1((sgx_dh_msg1_t*)dh_msg1, &sgx_dh_session);
  90. if(SGX_SUCCESS != status)
  91. {
  92. global_session_id--;
  93. // SAFE_FREE(g_session_id_tracker[*session_id]);
  94. return status;
  95. }
  96. memcpy(&global_session_info.in_progress.dh_session, &sgx_dh_session, sizeof(sgx_dh_session_t));
  97. //return sgx_seal_data(0, NULL, 0, NULL, 0, NULL);
  98. //Store the session information under the correspoding source enlave id key
  99. // g_dest_session_info_map.insert(std::pair<sgx_enclave_id_t, dh_session_t>(src_enclave_id, session_info));
  100. return status;
  101. }
  102. //Verify Message 2, generate Message3 and exchange Message 3 with Source Enclave
  103. ATTESTATION_STATUS exchange_report(
  104. sgx_dh_msg2_t *dh_msg2,
  105. sgx_dh_msg3_t *dh_msg3,
  106. uint32_t* session_id, uint8_t* read_or_write)
  107. {
  108. sgx_key_128bit_t dh_aek; // Session key
  109. // dh_session_t session_info;
  110. ATTESTATION_STATUS status = SUCCESS;
  111. sgx_dh_session_t sgx_dh_session;
  112. sgx_dh_session_enclave_identity_t initiator_identity;
  113. if(!dh_msg2 || !dh_msg3)
  114. {
  115. return INVALID_PARAMETER_ERROR;
  116. }
  117. memset(&dh_aek,0, sizeof(sgx_key_128bit_t));
  118. // Why is there a do-while loop anyway? It seems like there is no successful exit ...
  119. // do
  120. // {
  121. // TODO: Make sure that this works - pointers
  122. // session_info = global_session_info;
  123. if(global_session_info.status != IN_PROGRESS)
  124. {
  125. status = INVALID_SESSION;
  126. end_session();
  127. }
  128. memcpy(&sgx_dh_session, &global_session_info.in_progress.dh_session, sizeof(sgx_dh_session_t));
  129. dh_msg3->msg3_body.additional_prop_length = 0;
  130. //Process message 2 from source enclave and obtain message 3
  131. sgx_status_t se_ret = sgx_dh_responder_proc_msg2(dh_msg2,
  132. dh_msg3,
  133. &sgx_dh_session,
  134. &dh_aek,
  135. &initiator_identity);
  136. if(SGX_SUCCESS != se_ret)
  137. {
  138. status = se_ret;
  139. end_session();
  140. }
  141. uint32_t hash_count;
  142. // THIS IS WHERE THE DECRYPTOR VERIFIES THE APACHE'S MRSIGNER IS THE PUBLIC KEY GIVEN AFTER THE LOCAL ATTESTATION WITH THE VERIFIER.
  143. //Verify source enclave's trust
  144. uint32_t verify_return=verify_peer_enclave_trust(&initiator_identity);
  145. if(verify_return!=0)
  146. return verify_return;
  147. if(one_successful_la_done == 0)
  148. {
  149. one_successful_la_done = 1; *read_or_write=1;
  150. }
  151. else
  152. {
  153. one_successful_la_done=2; *read_or_write=0;
  154. }
  155. // TODO: Verify that these changes will be lost on update.
  156. //save the session ID, status and initialize the session nonce
  157. global_session_info.session_id = *session_id;
  158. global_session_info.status = ACTIVE;
  159. global_session_info.active.counter = 0;
  160. memcpy(&global_session_info.active.AEK, &dh_aek, sizeof(sgx_key_128bit_t));
  161. memset(&dh_aek,0, sizeof(sgx_key_128bit_t));
  162. //g_session_count++;*/
  163. // }while(0);
  164. return status;
  165. }
  166. uint32_t calculate_sealed_data_size( uint32_t input_size)
  167. {
  168. // *op_size=sgx_calc_sealed_data_size(0, input_size);
  169. return sgx_calc_sealed_data_size(0, input_size);
  170. }
  171. // TODO: Fix this.
  172. //Respond to the request from the Source Enclave to close the session
  173. ATTESTATION_STATUS end_session(/**/)
  174. {
  175. return SUCCESS;
  176. }
  177. /*
  178. // 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)
  179. // Why can't it just keep a counter that is incremented? What are the values of g_session_id_tracker array?
  180. //Returns a new sessionID for the source destination session
  181. ATTESTATION_STATUS generate_session_id(uint32_t *session_id)
  182. {
  183. ATTESTATION_STATUS status = SUCCESS;
  184. if(!session_id)
  185. {
  186. return INVALID_PARAMETER_ERROR;
  187. }
  188. //if the session structure is untintialized, set that as the next session ID
  189. for (int i = 0; i < MAX_SESSION_COUNT; i++)
  190. {
  191. if (g_session_id_tracker[i] == NULL)
  192. {
  193. *session_id = i;
  194. return status;
  195. }
  196. }
  197. status = NO_AVAILABLE_SESSION_ERROR;
  198. return status;
  199. */
  200. // *session_id=++global_session_id;
  201. //}
  202. uint32_t decrypt(uint8_t* ip_ciphertext, uint32_t ciphertext_len, uint8_t* ip_tag, uint8_t* op_plaintext)
  203. {
  204. // if(one_successful_la_done < 2) // doesn't matter as confidentiality of the signer is not a problem - changing it in memory so that the decryptor accepts another mrsigner is a problem.
  205. // return 0xfe; // will not return plaintext to the caller as this is the apache's mrsigner, sent to the verifier (value 1) or no LA done yet (value 0)
  206. uint32_t return_status2; uint32_t count;
  207. unsigned char key[16];
  208. // copying key to within the enclave as apparently it can't operate on it // TODO: Check if it works now.
  209. for(count=0;count<16;count++)
  210. key[count]=global_session_info.active.AEK[count];
  211. // copying ciphertext to within the enclave (otherwise it crashes with NOT enclave signal)
  212. uint8_t* ciphertext = (uint8_t*) malloc(ciphertext_len);
  213. for(count=0;count<ciphertext_len;count++)
  214. *(ciphertext+count)=*(ip_ciphertext+count);
  215. uint8_t* tag = (uint8_t*) malloc(16);
  216. for(count=0;count<16;count++)
  217. *(tag+count)=*(ip_tag+count);
  218. uint8_t* plaintext = (uint8_t*) malloc(ciphertext_len);
  219. uint8_t iv[12];
  220. memset(iv, 0, 12); //memcpy(iv, &global_session_info.active.counter, 4);
  221. return_status2=sgx_rijndael128GCM_decrypt((sgx_key_128bit_t*) key, ciphertext, ciphertext_len, plaintext, iv, 0xc, NULL, 0, (sgx_aes_gcm_128bit_tag_t*)tag);
  222. for(count=0;count<ciphertext_len;count++)
  223. *(op_plaintext+count)=plaintext[count];
  224. free(plaintext); free (ciphertext); free(tag);
  225. return return_status2;
  226. }
  227. uint32_t encrypt(uint8_t* ip_ciphertext, uint32_t ciphertext_len, uint8_t* ip_tag, uint8_t* op_plaintext)
  228. {
  229. uint32_t return_status2; uint32_t count;
  230. unsigned char key[16];
  231. // copying key to within the enclave as apparently it can't operate on it // TODO: Check if it works now.
  232. for(count=0;count<16;count++)
  233. key[count]=global_session_info.active.AEK[count];
  234. // copying ciphertext to within the enclave (otherwise it crashes with NOT enclave signal)
  235. uint8_t* ciphertext = (uint8_t*) malloc(ciphertext_len);
  236. for(count=0;count<ciphertext_len;count++)
  237. *(ciphertext+count)=*(ip_ciphertext+count);
  238. uint8_t* tag = (uint8_t*) malloc(16);
  239. for(count=0;count<16;count++)
  240. *(tag+count)=*(ip_tag+count);
  241. uint8_t* plaintext = (uint8_t*) malloc(ciphertext_len);
  242. uint8_t iv[12];
  243. memset(iv, 0, 12); //memcpy(iv, &global_session_info.active.counter, 4);
  244. return_status2=sgx_rijndael128GCM_encrypt((sgx_key_128bit_t*) key, ciphertext, ciphertext_len, plaintext, iv, 0xc, NULL, 0, (sgx_aes_gcm_128bit_tag_t*)tag);
  245. for(count=0;count<ciphertext_len;count++)
  246. *(op_plaintext+count)=plaintext[count];
  247. free(plaintext); free (ciphertext); free(tag);
  248. return return_status2;
  249. }
  250. uint32_t encrypt_internal(uint8_t* ip_ciphertext, uint32_t ciphertext_len, uint8_t* op_tag, uint8_t* op_plaintext)
  251. {
  252. //return encrypt(ip_ciphertext, ciphertext_len, ip_tag, op_plaintext);
  253. uint32_t return_status2; uint32_t count;
  254. unsigned char key[16];
  255. // copying key to within the enclave as apparently it can't operate on it // TODO: Check if it works now.
  256. for(count=0;count<16;count++)
  257. key[count]=global_session_info.active.AEK[count];
  258. // copying ciphertext to within the enclave (otherwise it crashes with NOT enclave signal)
  259. uint8_t* ciphertext = (uint8_t*) malloc(ciphertext_len);
  260. for(count=0;count<ciphertext_len;count++)
  261. *(ciphertext+count)=*(ip_ciphertext+count);
  262. uint8_t tag[16];
  263. uint8_t* plaintext = (uint8_t*) malloc(ciphertext_len);
  264. uint8_t iv[12];
  265. memset(iv, 0, 12); //memcpy(iv, &global_session_info.active.counter, 4);
  266. return_status2=sgx_rijndael128GCM_encrypt((sgx_key_128bit_t*) key, ciphertext, ciphertext_len, plaintext, iv, 0xc, NULL, 0, (sgx_aes_gcm_128bit_tag_t*)tag);
  267. for(count=0;count<ciphertext_len;count++)
  268. *(op_plaintext+count)=plaintext[count];
  269. for(count=0;count<16;count++)
  270. op_tag[count]=tag[count];
  271. free(plaintext); free (ciphertext);
  272. return return_status2;
  273. }