EnclaveMessageExchange.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  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 enclave_to_enclave_call_dispatcher(char* decrypted_data, size_t decrypted_data_length, char** resp_buffer, size_t* resp_length);
  48. uint32_t message_exchange_response_generator(char* decrypted_data, char** resp_buffer, size_t* resp_length);
  49. uint32_t verify_peer_enclave_trust(sgx_dh_session_enclave_identity_t* peer_enclave_identity);
  50. #ifdef __cplusplus
  51. }
  52. #endif
  53. #define MAX_SESSION_COUNT 16
  54. //number of open sessions
  55. // uint32_t g_session_count = 0;
  56. ATTESTATION_STATUS generate_session_id(uint32_t *session_id);
  57. ATTESTATION_STATUS end_session();
  58. sgx_ec256_private_t signing_priv_key;
  59. sgx_ecc_state_handle_t ecc_state;
  60. uint32_t session_ids[MAX_SESSION_COUNT];
  61. // Our enclave will not be doing LA with more than 1 decryptor enclave at a time.
  62. // We should not need this.
  63. //std::map<int, dh_session_t>g_dest_session_info_map;
  64. dh_session_t global_session_info;
  65. // TODO: May be we need to store all previously assigned session IDs instead of just the counter; to prevent replay attacks -
  66. uint32_t global_session_id=0;
  67. //Handle the request from Source Enclave for a session
  68. ATTESTATION_STATUS session_request(sgx_dh_msg1_t *dh_msg1,
  69. uint32_t *session_id )
  70. {
  71. // dh_session_t session_info;
  72. sgx_dh_session_t sgx_dh_session;
  73. sgx_status_t status = SGX_SUCCESS;
  74. if(!session_id || !dh_msg1)
  75. {
  76. return INVALID_PARAMETER_ERROR;
  77. }
  78. //Intialize the session as a session responder
  79. status = sgx_dh_init_session(SGX_DH_SESSION_RESPONDER, &sgx_dh_session);
  80. if(SGX_SUCCESS != status)
  81. {
  82. return status;
  83. }
  84. *session_id=1;
  85. global_session_info.status = IN_PROGRESS;
  86. //Generate Message1 that will be returned to Source Enclave
  87. status = sgx_dh_responder_gen_msg1((sgx_dh_msg1_t*)dh_msg1, &sgx_dh_session);
  88. if(SGX_SUCCESS != status)
  89. {
  90. global_session_id--;
  91. // SAFE_FREE(g_session_id_tracker[*session_id]);
  92. return status;
  93. }
  94. memcpy(&global_session_info.in_progress.dh_session, &sgx_dh_session, sizeof(sgx_dh_session_t));
  95. //return sgx_seal_data(0, NULL, 0, NULL, 0, NULL);
  96. //Store the session information under the correspoding source enlave id key
  97. // g_dest_session_info_map.insert(std::pair<sgx_enclave_id_t, dh_session_t>(src_enclave_id, session_info));
  98. return status;
  99. }
  100. //Verify Message 2, generate Message3 and exchange Message 3 with Source Enclave
  101. ATTESTATION_STATUS exchange_report(
  102. sgx_dh_msg2_t *dh_msg2,
  103. sgx_dh_msg3_t *dh_msg3,
  104. uint32_t* session_id)
  105. {
  106. sgx_key_128bit_t dh_aek; // Session key
  107. // dh_session_t session_info;
  108. ATTESTATION_STATUS status = SUCCESS;
  109. sgx_dh_session_t sgx_dh_session;
  110. sgx_dh_session_enclave_identity_t initiator_identity;
  111. if(!dh_msg2 || !dh_msg3)
  112. {
  113. return INVALID_PARAMETER_ERROR;
  114. }
  115. memset(&dh_aek,0, sizeof(sgx_key_128bit_t));
  116. // Why is there a do-while loop anyway? It seems like there is no successful exit ...
  117. // do
  118. // {
  119. // TODO: Make sure that this works - pointers
  120. // session_info = global_session_info;
  121. if(global_session_info.status != IN_PROGRESS)
  122. {
  123. status = INVALID_SESSION;
  124. end_session();
  125. }
  126. memcpy(&sgx_dh_session, &global_session_info.in_progress.dh_session, sizeof(sgx_dh_session_t));
  127. dh_msg3->msg3_body.additional_prop_length = 0;
  128. //Process message 2 from source enclave and obtain message 3
  129. sgx_status_t se_ret = sgx_dh_responder_proc_msg2(dh_msg2,
  130. dh_msg3,
  131. &sgx_dh_session,
  132. &dh_aek,
  133. &initiator_identity);
  134. if(SGX_SUCCESS != se_ret)
  135. {
  136. status = se_ret;
  137. end_session();
  138. }
  139. // THIS IS WHERE THE DECRYPTOR VERIFIES THE APACHE'S MRSIGNER IS THE PUBLIC KEY GIVEN AFTER THE LOCAL ATTESTATION WITH THE VERIFIER.
  140. //Verify source enclave's trust
  141. uint32_t ret = verify_peer_enclave_trust(&initiator_identity);
  142. if(ret != SUCCESS)
  143. {
  144. return ret; //INVALID_SESSION;
  145. }
  146. // TODO: Verify that these changes will be lost on update.
  147. //save the session ID, status and initialize the session nonce
  148. global_session_info.session_id = *session_id;
  149. global_session_info.status = ACTIVE;
  150. global_session_info.active.counter = 0;
  151. memcpy(&global_session_info.active.AEK, &dh_aek, sizeof(sgx_key_128bit_t));
  152. memset(&dh_aek,0, sizeof(sgx_key_128bit_t));
  153. //g_session_count++;*/
  154. // }while(0);
  155. return status;
  156. }
  157. uint32_t create_ecdsa_key_pair(sgx_ec256_public_t* pub_key, sgx_ec256_private_t* priv_key)
  158. {
  159. sgx_status_t se_ret; sgx_status_t se_ret2;
  160. //create ECC context
  161. ecc_state = NULL;
  162. se_ret = sgx_ecc256_open_context(&ecc_state);
  163. if(SGX_SUCCESS != se_ret)
  164. return se_ret;
  165. // generate private key and public key
  166. se_ret = sgx_ecc256_create_key_pair(priv_key, pub_key, ecc_state);
  167. se_ret2 = sgx_ecc256_close_context(ecc_state);
  168. if(SGX_SUCCESS != se_ret && se_ret2!= SGX_SUCCESS) // something weird has happened - couldn't shut it down.
  169. return 0xFFFFFFFF;
  170. return SGX_SUCCESS;
  171. }
  172. // todo: set to private
  173. void serialize_signing_key_pair_to_string(sgx_ec256_public_t* pub_key, sgx_ec256_private_t* signing_priv_key, uint8_t* private_public_key_string)
  174. {
  175. uint32_t counter;
  176. for(counter=0;counter<SGX_ECP256_KEY_SIZE; counter++)
  177. *(private_public_key_string+counter)=signing_priv_key->r[counter];
  178. for(counter=SGX_ECP256_KEY_SIZE;counter<2*SGX_ECP256_KEY_SIZE; counter++)
  179. *(private_public_key_string+counter)=pub_key->gx[counter-SGX_ECP256_KEY_SIZE];
  180. for(counter=2*SGX_ECP256_KEY_SIZE;counter<3*SGX_ECP256_KEY_SIZE; counter++)
  181. *(private_public_key_string+counter)=pub_key->gy[counter-2*SGX_ECP256_KEY_SIZE];
  182. }
  183. // todo: set to private
  184. void deserialize_string_to_public_private_key_pair(uint8_t* private_public_key_string, sgx_ec256_public_t* pub_key, sgx_ec256_private_t* signing_priv_key)
  185. {
  186. uint32_t counter;
  187. for(counter=0;counter<SGX_ECP256_KEY_SIZE; counter++)
  188. signing_priv_key->r[counter]=*(private_public_key_string+counter);
  189. for(counter=SGX_ECP256_KEY_SIZE;counter<2*SGX_ECP256_KEY_SIZE; counter++)
  190. pub_key->gx[counter-SGX_ECP256_KEY_SIZE]=*(private_public_key_string+counter);
  191. for(counter=2*SGX_ECP256_KEY_SIZE;counter<3*SGX_ECP256_KEY_SIZE; counter++)
  192. pub_key->gy[counter-2*SGX_ECP256_KEY_SIZE]=*(private_public_key_string+counter);
  193. }
  194. uint32_t create_and_seal_ecdsa_signing_key_pair(__attribute__((unused)) sgx_ec256_public_t* pub_key, __attribute__((unused)) uint32_t* sealed_data_length, __attribute__((unused)) uint8_t* sealed_data)
  195. {
  196. uint32_t ret_status;
  197. ret_status=create_ecdsa_key_pair(pub_key, &signing_priv_key);
  198. if(ret_status!=SGX_SUCCESS)
  199. return ret_status;
  200. // generating the entire string as there is no SGX function to generate the public key from the private one.
  201. // uint8_t* private_public_key_string = (uint8_t*) malloc(3*SGX_ECP256_KEY_SIZE);
  202. uint8_t* sealed_data2 = (uint8_t*) malloc(*sealed_data_length);
  203. // serializing keypair to string
  204. // serialize_signing_key_pair_to_string(pub_key, &signing_priv_key, private_public_key_string);
  205. // sealing serialized keypair to disk
  206. // ret_status = sgx_unseal_data(NULL /*(sgx_sealed_data_t*)sealed_data2*/, NULL, 0, NULL /*temp_plaintext*/ ,0 /*&expected_plaintext_msg_length*/);
  207. // sgx_seal_data(0, NULL, 3*SGX_ECP256_KEY_SIZE, private_public_key_string, *sealed_data_length, (sgx_sealed_data_t*) sealed_data2);
  208. uint8_t mydata[32]={1};
  209. sgx_seal_data(0, NULL, SGX_ECP256_KEY_SIZE,mydata, *sealed_data_length, (sgx_sealed_data_t*) sealed_data2);
  210. free(sealed_data2);
  211. // free(private_public_key_string);
  212. // return ret_status;
  213. return SGX_SUCCESS;
  214. }
  215. uint32_t unseal_and_restore_sealed_signing_key_pair(__attribute__((unused)) sgx_ec256_public_t* pub_key, __attribute__((unused)) uint8_t* sealed_data, __attribute__((unused)) uint32_t* sgx_sealed_data_length)
  216. {
  217. return SGX_SUCCESS;
  218. }
  219. uint32_t calculate_sealed_data_size( uint32_t input_size)
  220. {
  221. // *op_size=sgx_calc_sealed_data_size(0, input_size);
  222. return sgx_calc_sealed_data_size(0, input_size);
  223. }
  224. /*
  225. uint32_t create_ecdsa_key_pair( sgx_ec256_public_t* pub_key )
  226. {
  227. //sgx_ec256_public_t pub_key;
  228. sgx_status_t se_ret;
  229. //create ECC context
  230. ecc_state = NULL;
  231. se_ret = sgx_ecc256_open_context(&ecc_state);
  232. if(SGX_SUCCESS != se_ret)
  233. {
  234. return se_ret;
  235. }
  236. // generate private key and public key
  237. se_ret = sgx_ecc256_create_key_pair(&signing_priv_key, pub_key, ecc_state);
  238. if(SGX_SUCCESS != se_ret)
  239. return se_ret;
  240. se_ret = sgx_ecc256_close_context(ecc_state);
  241. // if(SGX_SUCCESS != se_ret)
  242. // return se_ret;
  243. return se_ret;
  244. }
  245. */
  246. /*
  247. uint32_t generate_and_seal_signing_private_key(uint8_t* pub_key, )
  248. {
  249. uint32_t ret_status;
  250. ret_status=create_ecdsa_key_pair(pub_key);
  251. if(ret_status!=SGX_SUCCESS)
  252. return ret_status;
  253. uint8_t* public_key_string = (uint8_t*) malloc(2*SGX_ECP256_KEY_SIZE);
  254. uint32_t counter;
  255. for(counter=0;counter<SGX_ECP256_KEY_SIZE; counter++)
  256. {
  257. *(public_key_string+counter)=pub_key->gx[counter];
  258. }
  259. for(counter=SGX_ECP256_KEY_SIZE;counter<2*SGX_ECP256_KEY_SIZE; counter++)
  260. {
  261. *(public_key_string+counter)=pub_key->gy[counter];
  262. }
  263. // sgx_seal_data() call
  264. uint32_t expected_sealed_data_length=sgx_calc_sealed_data_size(0,2*ECP256_KEY_SIZE);
  265. if(expected_sealed_data_length == 0xFFFFFFFF)
  266. return 0xFFFFFFFF;
  267. uint8_t* sealed_data=(uint8_t*) malloc(expected_sealed_data_length);
  268. sgx_sealed_data_t sealed_data;
  269. sgx_seal_data(0, NULL, public_key_string, 2*ECP256_KEY_SIZE, );
  270. free(public_key_string);
  271. }
  272. */
  273. /*
  274. uint32_t sign_with_signing_private_key(uint8_t* data, uint8_t* length)
  275. {
  276. }
  277. */
  278. // uint32_t create_
  279. /*
  280. //Request for the response size, send the request message to the destination enclave and receive the response message back
  281. ATTESTATION_STATUS send_request_receive_response(
  282. sgx_enclave_id_t dest_enclave_id,
  283. dh_session_t *session_info,
  284. char *inp_buff,
  285. size_t inp_buff_len,
  286. size_t max_out_buff_size,
  287. char **out_buff,
  288. size_t* out_buff_len)
  289. {
  290. const uint8_t* plaintext;
  291. uint32_t plaintext_length;
  292. sgx_status_t status;
  293. uint32_t retstatus;
  294. secure_message_t* req_message;
  295. secure_message_t* resp_message;
  296. uint8_t *decrypted_data;
  297. uint32_t decrypted_data_length;
  298. uint32_t plain_text_offset;
  299. uint8_t l_tag[TAG_SIZE];
  300. size_t max_resp_message_length;
  301. plaintext = (const uint8_t*)(" ");
  302. plaintext_length = 0;
  303. if(!session_info || !inp_buff)
  304. {
  305. return INVALID_PARAMETER_ERROR;
  306. }
  307. // TODO: Figure out what this was supposed to be for.
  308. //Check if the nonce for the session has not exceeded 2^32-2 if so end session and start a new session
  309. if(session_info->active.counter == ((uint32_t) - 2))
  310. {
  311. close_session(src_enclave_id, dest_enclave_id);
  312. create_session(src_enclave_id, dest_enclave_id, session_info);
  313. }
  314. //Allocate memory for the AES-GCM request message
  315. req_message = (secure_message_t*)malloc(sizeof(secure_message_t)+ inp_buff_len);
  316. if(!req_message)
  317. {
  318. return MALLOC_ERROR;
  319. }
  320. memset(req_message,0,sizeof(secure_message_t)+ inp_buff_len);
  321. const uint32_t data2encrypt_length = (uint32_t)inp_buff_len;
  322. //Set the payload size to data to encrypt length
  323. req_message->message_aes_gcm_data.payload_size = data2encrypt_length;
  324. //Use the session nonce as the payload IV
  325. memcpy(req_message->message_aes_gcm_data.reserved,&session_info->active.counter,sizeof(session_info->active.counter));
  326. //Set the session ID of the message to the current session id
  327. req_message->session_id = session_info->session_id;
  328. //Prepare the request message with the encrypted payload
  329. status = sgx_rijndael128GCM_encrypt(&session_info->active.AEK, (uint8_t*)inp_buff, data2encrypt_length,
  330. reinterpret_cast<uint8_t *>(&(req_message->message_aes_gcm_data.payload)),
  331. reinterpret_cast<uint8_t *>(&(req_message->message_aes_gcm_data.reserved)),
  332. sizeof(req_message->message_aes_gcm_data.reserved), plaintext, plaintext_length,
  333. &(req_message->message_aes_gcm_data.payload_tag));
  334. if(SGX_SUCCESS != status)
  335. {
  336. SAFE_FREE(req_message);
  337. return status;
  338. }
  339. //Allocate memory for the response payload to be copied
  340. *out_buff = (char*)malloc(max_out_buff_size);
  341. if(!*out_buff)
  342. {
  343. SAFE_FREE(req_message);
  344. return MALLOC_ERROR;
  345. }
  346. memset(*out_buff, 0, max_out_buff_size);
  347. //Allocate memory for the response message
  348. resp_message = (secure_message_t*)malloc(sizeof(secure_message_t)+ max_out_buff_size);
  349. if(!resp_message)
  350. {
  351. SAFE_FREE(req_message);
  352. return MALLOC_ERROR;
  353. }
  354. memset(resp_message, 0, sizeof(secure_message_t)+ max_out_buff_size);
  355. // TODO: This should not exist.
  356. //Ocall to send the request to the Destination Enclave and get the response message back
  357. status = send_request_ocall(&retstatus, src_enclave_id, dest_enclave_id, req_message,
  358. (sizeof(secure_message_t)+ inp_buff_len), max_out_buff_size,
  359. resp_message, (sizeof(secure_message_t)+ max_out_buff_size));
  360. if (status == SGX_SUCCESS)
  361. {
  362. if ((ATTESTATION_STATUS)retstatus != SUCCESS)
  363. {
  364. SAFE_FREE(req_message);
  365. SAFE_FREE(resp_message);
  366. return ((ATTESTATION_STATUS)retstatus);
  367. }
  368. }
  369. else
  370. {
  371. SAFE_FREE(req_message);
  372. SAFE_FREE(resp_message);
  373. return ATTESTATION_SE_ERROR;
  374. }
  375. max_resp_message_length = sizeof(secure_message_t)+ max_out_buff_size;
  376. if(sizeof(resp_message) > max_resp_message_length)
  377. {
  378. SAFE_FREE(req_message);
  379. SAFE_FREE(resp_message);
  380. return INVALID_PARAMETER_ERROR;
  381. }
  382. //Code to process the response message from the Destination Enclave
  383. decrypted_data_length = resp_message->message_aes_gcm_data.payload_size;
  384. plain_text_offset = decrypted_data_length;
  385. decrypted_data = (uint8_t*)malloc(decrypted_data_length);
  386. if(!decrypted_data)
  387. {
  388. SAFE_FREE(req_message);
  389. SAFE_FREE(resp_message);
  390. return MALLOC_ERROR;
  391. }
  392. memset(&l_tag, 0, 16);
  393. memset(decrypted_data, 0, decrypted_data_length);
  394. //Decrypt the response message payload
  395. status = sgx_rijndael128GCM_decrypt(&session_info->active.AEK, resp_message->message_aes_gcm_data.payload,
  396. decrypted_data_length, decrypted_data,
  397. reinterpret_cast<uint8_t *>(&(resp_message->message_aes_gcm_data.reserved)),
  398. sizeof(resp_message->message_aes_gcm_data.reserved), &(resp_message->message_aes_gcm_data.payload[plain_text_offset]), plaintext_length,
  399. &resp_message->message_aes_gcm_data.payload_tag);
  400. if(SGX_SUCCESS != status)
  401. {
  402. SAFE_FREE(req_message);
  403. SAFE_FREE(decrypted_data);
  404. SAFE_FREE(resp_message);
  405. return status;
  406. }
  407. // Verify if the nonce obtained in the response is equal to the session nonce + 1 (Prevents replay attacks)
  408. if(*(resp_message->message_aes_gcm_data.reserved) != (session_info->active.counter + 1 ))
  409. {
  410. SAFE_FREE(req_message);
  411. SAFE_FREE(resp_message);
  412. SAFE_FREE(decrypted_data);
  413. return INVALID_PARAMETER_ERROR;
  414. }
  415. //Update the value of the session nonce in the source enclave
  416. session_info->active.counter = session_info->active.counter + 1;
  417. memcpy(out_buff_len, &decrypted_data_length, sizeof(decrypted_data_length));
  418. memcpy(*out_buff, decrypted_data, decrypted_data_length);
  419. SAFE_FREE(decrypted_data);
  420. SAFE_FREE(req_message);
  421. SAFE_FREE(resp_message);
  422. return SUCCESS;
  423. }
  424. */
  425. /*
  426. //Process the request from the Source enclave and send the response message back to the Source enclave
  427. ATTESTATION_STATUS generate_response(sgx_enclave_id_t src_enclave_id,
  428. secure_message_t* req_message,
  429. size_t req_message_size,
  430. size_t max_payload_size,
  431. secure_message_t* resp_message,
  432. size_t resp_message_size)
  433. {
  434. const uint8_t* plaintext;
  435. uint32_t plaintext_length;
  436. uint8_t *decrypted_data;
  437. uint32_t decrypted_data_length;
  438. uint32_t plain_text_offset;
  439. ms_in_msg_exchange_t * ms;
  440. size_t resp_data_length;
  441. size_t resp_message_calc_size;
  442. char* resp_data;
  443. uint8_t l_tag[TAG_SIZE];
  444. size_t header_size, expected_payload_size;
  445. dh_session_t *session_info;
  446. secure_message_t* temp_resp_message;
  447. uint32_t ret;
  448. sgx_status_t status;
  449. plaintext = (const uint8_t*)(" ");
  450. plaintext_length = 0;
  451. if(!req_message || !resp_message)
  452. {
  453. return INVALID_PARAMETER_ERROR;
  454. }
  455. // TODO: Set session_info from somewhere.
  456. //Get the session information from the map corresponding to the source enclave id
  457. std::map<sgx_enclave_id_t, dh_session_t>::iterator it = g_dest_session_info_map.find(src_enclave_id);
  458. if(it != g_dest_session_info_map.end())
  459. {
  460. session_info = &it->second;
  461. }
  462. else
  463. {
  464. return INVALID_SESSION;
  465. }
  466. if(session_info->status != ACTIVE)
  467. {
  468. return INVALID_SESSION;
  469. }
  470. //Set the decrypted data length to the payload size obtained from the message
  471. decrypted_data_length = req_message->message_aes_gcm_data.payload_size;
  472. header_size = sizeof(secure_message_t);
  473. expected_payload_size = req_message_size - header_size;
  474. //Verify the size of the payload
  475. if(expected_payload_size != decrypted_data_length)
  476. return INVALID_PARAMETER_ERROR;
  477. memset(&l_tag, 0, 16);
  478. plain_text_offset = decrypted_data_length;
  479. decrypted_data = (uint8_t*)malloc(decrypted_data_length);
  480. if(!decrypted_data)
  481. {
  482. return MALLOC_ERROR;
  483. }
  484. memset(decrypted_data, 0, decrypted_data_length);
  485. //Decrypt the request message payload from source enclave
  486. status = sgx_rijndael128GCM_decrypt(&session_info->active.AEK, req_message->message_aes_gcm_data.payload,
  487. decrypted_data_length, decrypted_data,
  488. reinterpret_cast<uint8_t *>(&(req_message->message_aes_gcm_data.reserved)),
  489. sizeof(req_message->message_aes_gcm_data.reserved), &(req_message->message_aes_gcm_data.payload[plain_text_offset]), plaintext_length,
  490. &req_message->message_aes_gcm_data.payload_tag);
  491. if(SGX_SUCCESS != status)
  492. {
  493. SAFE_FREE(decrypted_data);
  494. return status;
  495. }
  496. //Casting the decrypted data to the marshaling structure type to obtain type of request (generic message exchange/enclave to enclave call)
  497. ms = (ms_in_msg_exchange_t *)decrypted_data;
  498. // Verify if the nonce obtained in the request is equal to the session nonce
  499. if((uint32_t)*(req_message->message_aes_gcm_data.reserved) != session_info->active.counter || *(req_message->message_aes_gcm_data.reserved) > ((2^32)-2))
  500. {
  501. SAFE_FREE(decrypted_data);
  502. return INVALID_PARAMETER_ERROR;
  503. }
  504. if(ms->msg_type == MESSAGE_EXCHANGE)
  505. {
  506. //Call the generic secret response generator for message exchange
  507. ret = message_exchange_response_generator((char*)decrypted_data, &resp_data, &resp_data_length);
  508. if(ret !=0)
  509. {
  510. SAFE_FREE(decrypted_data);
  511. SAFE_FREE(resp_data);
  512. return INVALID_SESSION;
  513. }
  514. }
  515. else if(ms->msg_type == ENCLAVE_TO_ENCLAVE_CALL)
  516. {
  517. //Call the destination enclave's dispatcher to call the appropriate function in the destination enclave
  518. ret = enclave_to_enclave_call_dispatcher((char*)decrypted_data, decrypted_data_length, &resp_data, &resp_data_length);
  519. if(ret !=0)
  520. {
  521. SAFE_FREE(decrypted_data);
  522. SAFE_FREE(resp_data);
  523. return INVALID_SESSION;
  524. }
  525. }
  526. else
  527. {
  528. SAFE_FREE(decrypted_data);
  529. return INVALID_REQUEST_TYPE_ERROR;
  530. }
  531. if(resp_data_length > max_payload_size)
  532. {
  533. SAFE_FREE(resp_data);
  534. SAFE_FREE(decrypted_data);
  535. return OUT_BUFFER_LENGTH_ERROR;
  536. }
  537. resp_message_calc_size = sizeof(secure_message_t)+ resp_data_length;
  538. if(resp_message_calc_size > resp_message_size)
  539. {
  540. SAFE_FREE(resp_data);
  541. SAFE_FREE(decrypted_data);
  542. return OUT_BUFFER_LENGTH_ERROR;
  543. }
  544. //Code to build the response back to the Source Enclave
  545. temp_resp_message = (secure_message_t*)malloc(resp_message_calc_size);
  546. if(!temp_resp_message)
  547. {
  548. SAFE_FREE(resp_data);
  549. SAFE_FREE(decrypted_data);
  550. return MALLOC_ERROR;
  551. }
  552. memset(temp_resp_message,0,sizeof(secure_message_t)+ resp_data_length);
  553. const uint32_t data2encrypt_length = (uint32_t)resp_data_length;
  554. temp_resp_message->session_id = session_info->session_id;
  555. temp_resp_message->message_aes_gcm_data.payload_size = data2encrypt_length;
  556. //Increment the Session Nonce (Replay Protection)
  557. session_info->active.counter = session_info->active.counter + 1;
  558. //Set the response nonce as the session nonce
  559. memcpy(&temp_resp_message->message_aes_gcm_data.reserved,&session_info->active.counter,sizeof(session_info->active.counter));
  560. //Prepare the response message with the encrypted payload
  561. status = sgx_rijndael128GCM_encrypt(&session_info->active.AEK, (uint8_t*)resp_data, data2encrypt_length,
  562. reinterpret_cast<uint8_t *>(&(temp_resp_message->message_aes_gcm_data.payload)),
  563. reinterpret_cast<uint8_t *>(&(temp_resp_message->message_aes_gcm_data.reserved)),
  564. sizeof(temp_resp_message->message_aes_gcm_data.reserved), plaintext, plaintext_length,
  565. &(temp_resp_message->message_aes_gcm_data.payload_tag));
  566. if(SGX_SUCCESS != status)
  567. {
  568. SAFE_FREE(resp_data);
  569. SAFE_FREE(decrypted_data);
  570. SAFE_FREE(temp_resp_message);
  571. return status;
  572. }
  573. memset(resp_message, 0, sizeof(secure_message_t)+ resp_data_length);
  574. memcpy(resp_message, temp_resp_message, sizeof(secure_message_t)+ resp_data_length);
  575. SAFE_FREE(decrypted_data);
  576. SAFE_FREE(resp_data);
  577. SAFE_FREE(temp_resp_message);
  578. return SUCCESS;
  579. }
  580. */
  581. /*
  582. //Close a current session
  583. ATTESTATION_STATUS close_session(sgx_enclave_id_t src_enclave_id,
  584. sgx_enclave_id_t dest_enclave_id)
  585. {
  586. sgx_status_t status;
  587. uint32_t retstatus;
  588. //Ocall to ask the destination enclave to end the session
  589. status = end_session_ocall(&retstatus, src_enclave_id, dest_enclave_id);
  590. if (status == SGX_SUCCESS)
  591. {
  592. if ((ATTESTATION_STATUS)retstatus != SUCCESS)
  593. return ((ATTESTATION_STATUS)retstatus);
  594. }
  595. else
  596. {
  597. return ATTESTATION_SE_ERROR;
  598. }
  599. return SUCCESS;
  600. }
  601. */
  602. // TODO: Fix this.
  603. //Respond to the request from the Source Enclave to close the session
  604. ATTESTATION_STATUS end_session(/**/)
  605. {
  606. return SUCCESS;
  607. }
  608. /*
  609. // 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)
  610. // Why can't it just keep a counter that is incremented? What are the values of g_session_id_tracker array?
  611. //Returns a new sessionID for the source destination session
  612. ATTESTATION_STATUS generate_session_id(uint32_t *session_id)
  613. {
  614. ATTESTATION_STATUS status = SUCCESS;
  615. if(!session_id)
  616. {
  617. return INVALID_PARAMETER_ERROR;
  618. }
  619. //if the session structure is untintialized, set that as the next session ID
  620. for (int i = 0; i < MAX_SESSION_COUNT; i++)
  621. {
  622. if (g_session_id_tracker[i] == NULL)
  623. {
  624. *session_id = i;
  625. return status;
  626. }
  627. }
  628. status = NO_AVAILABLE_SESSION_ERROR;
  629. return status;
  630. */
  631. // *session_id=++global_session_id;
  632. //}