EnclaveMessageExchange.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  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_ec256_private_t short_term_priv_key;
  60. sgx_ec256_public_t short_term_pub_key; // for testing only: to test verification of signature
  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)
  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. // THIS IS WHERE THE DECRYPTOR VERIFIES THE APACHE'S MRSIGNER IS THE PUBLIC KEY GIVEN AFTER THE LOCAL ATTESTATION WITH THE VERIFIER.
  142. //Verify source enclave's trust
  143. uint32_t ret = verify_peer_enclave_trust(&initiator_identity);
  144. if(ret != SUCCESS)
  145. {
  146. return ret; //INVALID_SESSION;
  147. }
  148. // TODO: Verify that these changes will be lost on update.
  149. //save the session ID, status and initialize the session nonce
  150. global_session_info.session_id = *session_id;
  151. global_session_info.status = ACTIVE;
  152. global_session_info.active.counter = 0;
  153. memcpy(&global_session_info.active.AEK, &dh_aek, sizeof(sgx_key_128bit_t));
  154. memset(&dh_aek,0, sizeof(sgx_key_128bit_t));
  155. //g_session_count++;*/
  156. // }while(0);
  157. return status;
  158. }
  159. uint32_t create_ecdsa_key_pair(sgx_ec256_public_t* pub_key, sgx_ec256_private_t* priv_key)
  160. {
  161. sgx_status_t se_ret; sgx_status_t se_ret2;
  162. //create ECC context
  163. ecc_state = NULL;
  164. se_ret = sgx_ecc256_open_context(&ecc_state);
  165. if(SGX_SUCCESS != se_ret)
  166. return se_ret;
  167. // generate private key and public key
  168. se_ret = sgx_ecc256_create_key_pair(priv_key, pub_key, ecc_state);
  169. se_ret2 = sgx_ecc256_close_context(ecc_state);
  170. if(SGX_SUCCESS != se_ret || se_ret2!= SGX_SUCCESS) // something weird has happened - couldn't shut it down.
  171. return 0xFFFFFFFF;
  172. return SGX_SUCCESS;
  173. }
  174. // todo: set to private
  175. // todo: assumes that the length of the keystring is at least 3*SGX_ECP256_KEY_SIZE
  176. 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)
  177. {
  178. if(private_public_key_string != NULL) // nowhere to serialize to
  179. {
  180. uint32_t counter;
  181. if(pub_key != NULL) // public key to serialize
  182. {
  183. for(counter=0;counter<SGX_ECP256_KEY_SIZE; counter++)
  184. *(private_public_key_string+counter)=pub_key->gx[counter];
  185. for(counter=SGX_ECP256_KEY_SIZE;counter<2*SGX_ECP256_KEY_SIZE; counter++)
  186. *(private_public_key_string+counter)=pub_key->gy[counter-SGX_ECP256_KEY_SIZE];
  187. }
  188. if(signing_priv_key != NULL) // private key to serialize
  189. {
  190. for(counter=2*SGX_ECP256_KEY_SIZE;counter<3*SGX_ECP256_KEY_SIZE; counter++)
  191. *(private_public_key_string+counter)=signing_priv_key->r[counter - 2*SGX_ECP256_KEY_SIZE];
  192. }
  193. /*
  194. if(pub_key != NULL) // public key to serialize
  195. {
  196. for(counter=SGX_ECP256_KEY_SIZE;counter<2*SGX_ECP256_KEY_SIZE; counter++)
  197. *(private_public_key_string+counter)=pub_key->gx[counter-SGX_ECP256_KEY_SIZE];
  198. for(counter=2*SGX_ECP256_KEY_SIZE;counter<3*SGX_ECP256_KEY_SIZE; counter++)
  199. *(private_public_key_string+counter)=pub_key->gy[counter-2*SGX_ECP256_KEY_SIZE];
  200. }*/
  201. }
  202. }
  203. // todo: set to private
  204. void deserialize_string_to_key_pair(uint8_t* private_public_key_string, sgx_ec256_public_t* pub_key, sgx_ec256_private_t* signing_priv_key)
  205. {
  206. if(private_public_key_string != NULL) // nowhere to deserialize from
  207. {
  208. uint32_t counter;
  209. if(signing_priv_key != NULL)
  210. {
  211. for(counter=2*SGX_ECP256_KEY_SIZE;counter<3*SGX_ECP256_KEY_SIZE; counter++)
  212. signing_priv_key->r[counter-2*SGX_ECP256_KEY_SIZE]=*(private_public_key_string+counter);
  213. }
  214. if(pub_key != NULL)
  215. {
  216. for(counter=0;counter<SGX_ECP256_KEY_SIZE; counter++)
  217. pub_key->gx[counter]=*(private_public_key_string+counter);
  218. for(counter=SGX_ECP256_KEY_SIZE;counter<2*SGX_ECP256_KEY_SIZE; counter++)
  219. pub_key->gy[counter-SGX_ECP256_KEY_SIZE]=*(private_public_key_string+counter);
  220. }
  221. }
  222. }
  223. 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)
  224. {
  225. uint32_t ret_status; sgx_ec256_private_t private_key; uint32_t counter;
  226. ret_status=create_ecdsa_key_pair(pub_key, &private_key);
  227. if(ret_status!=SGX_SUCCESS)
  228. return ret_status;
  229. for(counter=0;counter<SGX_ECP256_KEY_SIZE; counter++)
  230. signing_priv_key.r[counter]=private_key.r[counter];
  231. // generating the entire string as there is no SGX function to generate the public key from the private one.
  232. uint8_t* private_public_key_string = (uint8_t*) malloc(3*SGX_ECP256_KEY_SIZE);
  233. uint8_t* sealed_data2 = (uint8_t*) malloc(*sealed_data_length);
  234. // serializing keypair to string
  235. serialize_key_pair_to_string(pub_key, &private_key, private_public_key_string);
  236. uint8_t* private_key_string = (uint8_t*) malloc(SGX_ECP256_KEY_SIZE);
  237. for(counter=0;counter<SGX_ECP256_KEY_SIZE;counter++)
  238. *(private_key_string+counter)=private_key.r[counter];
  239. // return *sealed_data_length;
  240. ret_status = sgx_seal_data(0, NULL, 3*SGX_ECP256_KEY_SIZE, private_public_key_string, *sealed_data_length, (sgx_sealed_data_t*) sealed_data2);
  241. for(counter=0;counter<*sealed_data_length;counter++)
  242. *(sealed_data+counter)=*(sealed_data2+counter);
  243. free(sealed_data2);
  244. free(private_key_string); //free(private_key);
  245. free(private_public_key_string);
  246. return ret_status; // SGX_SUCCESS;
  247. }
  248. /*
  249. 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)
  250. {
  251. return SGX_SUCCESS;
  252. }*/
  253. uint32_t unseal_and_restore_sealed_signing_key_pair(__attribute__((unused)) sgx_ec256_public_t* pub_key, uint8_t* sealed_data, size_t* sgx_sealed_data_length)
  254. {
  255. uint32_t expected_plaintext_msg_length; uint8_t* temp_plaintext; uint32_t counter; uint32_t ret_status;
  256. expected_plaintext_msg_length = sgx_get_encrypt_txt_len((sgx_sealed_data_t*)sealed_data);
  257. if(expected_plaintext_msg_length == 0xffffffff)
  258. return 0xFFFFFFFF;
  259. // uint32_t return_status;
  260. uint8_t* sealed_data2 = (uint8_t*) malloc(*sgx_sealed_data_length);
  261. for(counter=0;counter<*sgx_sealed_data_length;counter++)
  262. {
  263. *(sealed_data2+counter)=*(sealed_data+counter);
  264. }
  265. temp_plaintext = (uint8_t*)malloc( expected_plaintext_msg_length );
  266. ret_status = sgx_unseal_data((sgx_sealed_data_t*)sealed_data2, NULL, 0, temp_plaintext, &expected_plaintext_msg_length);
  267. if(ret_status != SGX_SUCCESS)
  268. {
  269. free(temp_plaintext);free(sealed_data2);
  270. switch(ret_status)
  271. {
  272. case SGX_ERROR_MAC_MISMATCH:
  273. // MAC of the sealed data is incorrect. The sealed data has been tampered.
  274. break;
  275. case SGX_ERROR_INVALID_ATTRIBUTE:
  276. // Indicates attribute field of the sealed data is incorrect.
  277. break;
  278. case SGX_ERROR_INVALID_ISVSVN:
  279. // Indicates isv_svn field of the sealed data is greater than the enclave�s ISVSVN. This is a downgraded enclave.
  280. break;
  281. case SGX_ERROR_INVALID_CPUSVN:
  282. // Indicates cpu_svn field of the sealed data is greater than the platform�s cpu_svn. enclave is on a downgraded platform.
  283. break;
  284. case SGX_ERROR_INVALID_KEYNAME:
  285. // Indicates key_name field of the sealed data is incorrect.
  286. break;
  287. default:
  288. // other errors
  289. break;
  290. }
  291. return ret_status;
  292. }
  293. deserialize_string_to_key_pair(temp_plaintext, pub_key, &signing_priv_key);
  294. free(temp_plaintext); free(sealed_data2);
  295. return SGX_SUCCESS;
  296. }
  297. uint32_t create_and_sign_client_side_pub_key(sgx_measurement_t* mr_enclave, sgx_ec256_public_t* generated_pub_key, sgx_ec256_signature_t* generated_signature)
  298. {
  299. // create key pair
  300. uint32_t ret_status = create_ecdsa_key_pair(&short_term_pub_key, &short_term_priv_key); uint32_t counter;
  301. uint32_t ret_status2;
  302. if(ret_status!=SGX_SUCCESS)
  303. return ret_status;
  304. // serialize public key, append mr_enclave
  305. uint8_t* public_key_string = (uint8_t*) malloc(3*SGX_ECP256_KEY_SIZE); // for .edl file - size parameter for serialize is 96 and this fits coz we need to append the mr_enclave to the pub key
  306. serialize_key_pair_to_string(&short_term_pub_key, NULL, public_key_string);
  307. for(counter=32*2; counter<32*3; counter++) // appending mr_enclave
  308. *(public_key_string+counter)=mr_enclave->m[counter];
  309. // retrieve long-term private key from global variable - apparently, need to create a local copy or it crashes
  310. sgx_ec256_private_t long_term_priv_key;
  311. for(counter=0; counter<SGX_ECP256_KEY_SIZE; counter++)
  312. long_term_priv_key.r[counter] = signing_priv_key.r[counter];
  313. // sign public key with long-term private key
  314. sgx_ec256_signature_t local_signature; sgx_ecc_state_handle_t ecc_handle;
  315. //// opening context for signature
  316. ret_status = sgx_ecc256_open_context(&ecc_handle);
  317. if(ret_status != SGX_SUCCESS)
  318. return ret_status;
  319. ret_status = sgx_ecdsa_sign(public_key_string,2*SGX_ECP256_KEY_SIZE, &long_term_priv_key, &local_signature, ecc_handle);
  320. ret_status2 = sgx_ecc256_close_context(ecc_handle);
  321. free(public_key_string);
  322. if(ret_status == SGX_SUCCESS)
  323. {
  324. for(counter=0; counter<SGX_ECP256_KEY_SIZE; counter++)
  325. generated_pub_key->gx[counter] = short_term_pub_key.gx[counter];
  326. for(counter=0;counter<SGX_NISTP_ECP256_KEY_SIZE ; counter++)
  327. {
  328. generated_signature->x[counter] = local_signature.x[counter];
  329. generated_signature->y[counter] = local_signature.y[counter];
  330. }
  331. }
  332. if(ret_status != SGX_SUCCESS || ret_status2 != SGX_SUCCESS)
  333. return 0xFFFFFFFF;
  334. return 0;
  335. }
  336. uint32_t calculate_sealed_data_size( uint32_t input_size)
  337. {
  338. // *op_size=sgx_calc_sealed_data_size(0, input_size);
  339. return sgx_calc_sealed_data_size(0, input_size);
  340. }
  341. /*
  342. uint32_t create_ecdsa_key_pair( sgx_ec256_public_t* pub_key )
  343. {
  344. //sgx_ec256_public_t pub_key;
  345. sgx_status_t se_ret;
  346. //create ECC context
  347. ecc_state = NULL;
  348. se_ret = sgx_ecc256_open_context(&ecc_state);
  349. if(SGX_SUCCESS != se_ret)
  350. {
  351. return se_ret;
  352. }
  353. // generate private key and public key
  354. se_ret = sgx_ecc256_create_key_pair(&signing_priv_key, pub_key, ecc_state);
  355. if(SGX_SUCCESS != se_ret)
  356. return se_ret;
  357. se_ret = sgx_ecc256_close_context(ecc_state);
  358. // if(SGX_SUCCESS != se_ret)
  359. // return se_ret;
  360. return se_ret;
  361. }
  362. */
  363. /*
  364. uint32_t generate_and_seal_signing_private_key(uint8_t* pub_key, )
  365. {
  366. uint32_t ret_status;
  367. ret_status=create_ecdsa_key_pair(pub_key);
  368. if(ret_status!=SGX_SUCCESS)
  369. return ret_status;
  370. uint8_t* public_key_string = (uint8_t*) malloc(2*SGX_ECP256_KEY_SIZE);
  371. uint32_t counter;
  372. for(counter=0;counter<SGX_ECP256_KEY_SIZE; counter++)
  373. {
  374. *(public_key_string+counter)=pub_key->gx[counter];
  375. }
  376. for(counter=SGX_ECP256_KEY_SIZE;counter<2*SGX_ECP256_KEY_SIZE; counter++)
  377. {
  378. *(public_key_string+counter)=pub_key->gy[counter];
  379. }
  380. // sgx_seal_data() call
  381. uint32_t expected_sealed_data_length=sgx_calc_sealed_data_size(0,2*ECP256_KEY_SIZE);
  382. if(expected_sealed_data_length == 0xFFFFFFFF)
  383. return 0xFFFFFFFF;
  384. uint8_t* sealed_data=(uint8_t*) malloc(expected_sealed_data_length);
  385. sgx_sealed_data_t sealed_data;
  386. sgx_seal_data(0, NULL, public_key_string, 2*ECP256_KEY_SIZE, );
  387. free(public_key_string);
  388. }
  389. */
  390. /*
  391. uint32_t sign_with_signing_private_key(uint8_t* data, uint8_t* length)
  392. {
  393. }
  394. */
  395. // uint32_t create_
  396. /*
  397. //Request for the response size, send the request message to the destination enclave and receive the response message back
  398. ATTESTATION_STATUS send_request_receive_response(
  399. sgx_enclave_id_t dest_enclave_id,
  400. dh_session_t *session_info,
  401. char *inp_buff,
  402. size_t inp_buff_len,
  403. size_t max_out_buff_size,
  404. char **out_buff,
  405. size_t* out_buff_len)
  406. {
  407. const uint8_t* plaintext;
  408. uint32_t plaintext_length;
  409. sgx_status_t status;
  410. uint32_t retstatus;
  411. secure_message_t* req_message;
  412. secure_message_t* resp_message;
  413. uint8_t *decrypted_data;
  414. uint32_t decrypted_data_length;
  415. uint32_t plain_text_offset;
  416. uint8_t l_tag[TAG_SIZE];
  417. size_t max_resp_message_length;
  418. plaintext = (const uint8_t*)(" ");
  419. plaintext_length = 0;
  420. if(!session_info || !inp_buff)
  421. {
  422. return INVALID_PARAMETER_ERROR;
  423. }
  424. // TODO: Figure out what this was supposed to be for.
  425. //Check if the nonce for the session has not exceeded 2^32-2 if so end session and start a new session
  426. if(session_info->active.counter == ((uint32_t) - 2))
  427. {
  428. close_session(src_enclave_id, dest_enclave_id);
  429. create_session(src_enclave_id, dest_enclave_id, session_info);
  430. }
  431. //Allocate memory for the AES-GCM request message
  432. req_message = (secure_message_t*)malloc(sizeof(secure_message_t)+ inp_buff_len);
  433. if(!req_message)
  434. {
  435. return MALLOC_ERROR;
  436. }
  437. memset(req_message,0,sizeof(secure_message_t)+ inp_buff_len);
  438. const uint32_t data2encrypt_length = (uint32_t)inp_buff_len;
  439. //Set the payload size to data to encrypt length
  440. req_message->message_aes_gcm_data.payload_size = data2encrypt_length;
  441. //Use the session nonce as the payload IV
  442. memcpy(req_message->message_aes_gcm_data.reserved,&session_info->active.counter,sizeof(session_info->active.counter));
  443. //Set the session ID of the message to the current session id
  444. req_message->session_id = session_info->session_id;
  445. //Prepare the request message with the encrypted payload
  446. status = sgx_rijndael128GCM_encrypt(&session_info->active.AEK, (uint8_t*)inp_buff, data2encrypt_length,
  447. reinterpret_cast<uint8_t *>(&(req_message->message_aes_gcm_data.payload)),
  448. reinterpret_cast<uint8_t *>(&(req_message->message_aes_gcm_data.reserved)),
  449. sizeof(req_message->message_aes_gcm_data.reserved), plaintext, plaintext_length,
  450. &(req_message->message_aes_gcm_data.payload_tag));
  451. if(SGX_SUCCESS != status)
  452. {
  453. SAFE_FREE(req_message);
  454. return status;
  455. }
  456. //Allocate memory for the response payload to be copied
  457. *out_buff = (char*)malloc(max_out_buff_size);
  458. if(!*out_buff)
  459. {
  460. SAFE_FREE(req_message);
  461. return MALLOC_ERROR;
  462. }
  463. memset(*out_buff, 0, max_out_buff_size);
  464. //Allocate memory for the response message
  465. resp_message = (secure_message_t*)malloc(sizeof(secure_message_t)+ max_out_buff_size);
  466. if(!resp_message)
  467. {
  468. SAFE_FREE(req_message);
  469. return MALLOC_ERROR;
  470. }
  471. memset(resp_message, 0, sizeof(secure_message_t)+ max_out_buff_size);
  472. // TODO: This should not exist.
  473. //Ocall to send the request to the Destination Enclave and get the response message back
  474. status = send_request_ocall(&retstatus, src_enclave_id, dest_enclave_id, req_message,
  475. (sizeof(secure_message_t)+ inp_buff_len), max_out_buff_size,
  476. resp_message, (sizeof(secure_message_t)+ max_out_buff_size));
  477. if (status == SGX_SUCCESS)
  478. {
  479. if ((ATTESTATION_STATUS)retstatus != SUCCESS)
  480. {
  481. SAFE_FREE(req_message);
  482. SAFE_FREE(resp_message);
  483. return ((ATTESTATION_STATUS)retstatus);
  484. }
  485. }
  486. else
  487. {
  488. SAFE_FREE(req_message);
  489. SAFE_FREE(resp_message);
  490. return ATTESTATION_SE_ERROR;
  491. }
  492. max_resp_message_length = sizeof(secure_message_t)+ max_out_buff_size;
  493. if(sizeof(resp_message) > max_resp_message_length)
  494. {
  495. SAFE_FREE(req_message);
  496. SAFE_FREE(resp_message);
  497. return INVALID_PARAMETER_ERROR;
  498. }
  499. //Code to process the response message from the Destination Enclave
  500. decrypted_data_length = resp_message->message_aes_gcm_data.payload_size;
  501. plain_text_offset = decrypted_data_length;
  502. decrypted_data = (uint8_t*)malloc(decrypted_data_length);
  503. if(!decrypted_data)
  504. {
  505. SAFE_FREE(req_message);
  506. SAFE_FREE(resp_message);
  507. return MALLOC_ERROR;
  508. }
  509. memset(&l_tag, 0, 16);
  510. memset(decrypted_data, 0, decrypted_data_length);
  511. //Decrypt the response message payload
  512. status = sgx_rijndael128GCM_decrypt(&session_info->active.AEK, resp_message->message_aes_gcm_data.payload,
  513. decrypted_data_length, decrypted_data,
  514. reinterpret_cast<uint8_t *>(&(resp_message->message_aes_gcm_data.reserved)),
  515. sizeof(resp_message->message_aes_gcm_data.reserved), &(resp_message->message_aes_gcm_data.payload[plain_text_offset]), plaintext_length,
  516. &resp_message->message_aes_gcm_data.payload_tag);
  517. if(SGX_SUCCESS != status)
  518. {
  519. SAFE_FREE(req_message);
  520. SAFE_FREE(decrypted_data);
  521. SAFE_FREE(resp_message);
  522. return status;
  523. }
  524. // Verify if the nonce obtained in the response is equal to the session nonce + 1 (Prevents replay attacks)
  525. if(*(resp_message->message_aes_gcm_data.reserved) != (session_info->active.counter + 1 ))
  526. {
  527. SAFE_FREE(req_message);
  528. SAFE_FREE(resp_message);
  529. SAFE_FREE(decrypted_data);
  530. return INVALID_PARAMETER_ERROR;
  531. }
  532. //Update the value of the session nonce in the source enclave
  533. session_info->active.counter = session_info->active.counter + 1;
  534. memcpy(out_buff_len, &decrypted_data_length, sizeof(decrypted_data_length));
  535. memcpy(*out_buff, decrypted_data, decrypted_data_length);
  536. SAFE_FREE(decrypted_data);
  537. SAFE_FREE(req_message);
  538. SAFE_FREE(resp_message);
  539. return SUCCESS;
  540. }
  541. */
  542. /*
  543. //Process the request from the Source enclave and send the response message back to the Source enclave
  544. ATTESTATION_STATUS generate_response(sgx_enclave_id_t src_enclave_id,
  545. secure_message_t* req_message,
  546. size_t req_message_size,
  547. size_t max_payload_size,
  548. secure_message_t* resp_message,
  549. size_t resp_message_size)
  550. {
  551. const uint8_t* plaintext;
  552. uint32_t plaintext_length;
  553. uint8_t *decrypted_data;
  554. uint32_t decrypted_data_length;
  555. uint32_t plain_text_offset;
  556. ms_in_msg_exchange_t * ms;
  557. size_t resp_data_length;
  558. size_t resp_message_calc_size;
  559. char* resp_data;
  560. uint8_t l_tag[TAG_SIZE];
  561. size_t header_size, expected_payload_size;
  562. dh_session_t *session_info;
  563. secure_message_t* temp_resp_message;
  564. uint32_t ret;
  565. sgx_status_t status;
  566. plaintext = (const uint8_t*)(" ");
  567. plaintext_length = 0;
  568. if(!req_message || !resp_message)
  569. {
  570. return INVALID_PARAMETER_ERROR;
  571. }
  572. // TODO: Set session_info from somewhere.
  573. //Get the session information from the map corresponding to the source enclave id
  574. std::map<sgx_enclave_id_t, dh_session_t>::iterator it = g_dest_session_info_map.find(src_enclave_id);
  575. if(it != g_dest_session_info_map.end())
  576. {
  577. session_info = &it->second;
  578. }
  579. else
  580. {
  581. return INVALID_SESSION;
  582. }
  583. if(session_info->status != ACTIVE)
  584. {
  585. return INVALID_SESSION;
  586. }
  587. //Set the decrypted data length to the payload size obtained from the message
  588. decrypted_data_length = req_message->message_aes_gcm_data.payload_size;
  589. header_size = sizeof(secure_message_t);
  590. expected_payload_size = req_message_size - header_size;
  591. //Verify the size of the payload
  592. if(expected_payload_size != decrypted_data_length)
  593. return INVALID_PARAMETER_ERROR;
  594. memset(&l_tag, 0, 16);
  595. plain_text_offset = decrypted_data_length;
  596. decrypted_data = (uint8_t*)malloc(decrypted_data_length);
  597. if(!decrypted_data)
  598. {
  599. return MALLOC_ERROR;
  600. }
  601. memset(decrypted_data, 0, decrypted_data_length);
  602. //Decrypt the request message payload from source enclave
  603. status = sgx_rijndael128GCM_decrypt(&session_info->active.AEK, req_message->message_aes_gcm_data.payload,
  604. decrypted_data_length, decrypted_data,
  605. reinterpret_cast<uint8_t *>(&(req_message->message_aes_gcm_data.reserved)),
  606. sizeof(req_message->message_aes_gcm_data.reserved), &(req_message->message_aes_gcm_data.payload[plain_text_offset]), plaintext_length,
  607. &req_message->message_aes_gcm_data.payload_tag);
  608. if(SGX_SUCCESS != status)
  609. {
  610. SAFE_FREE(decrypted_data);
  611. return status;
  612. }
  613. //Casting the decrypted data to the marshaling structure type to obtain type of request (generic message exchange/enclave to enclave call)
  614. ms = (ms_in_msg_exchange_t *)decrypted_data;
  615. // Verify if the nonce obtained in the request is equal to the session nonce
  616. if((uint32_t)*(req_message->message_aes_gcm_data.reserved) != session_info->active.counter || *(req_message->message_aes_gcm_data.reserved) > ((2^32)-2))
  617. {
  618. SAFE_FREE(decrypted_data);
  619. return INVALID_PARAMETER_ERROR;
  620. }
  621. if(ms->msg_type == MESSAGE_EXCHANGE)
  622. {
  623. //Call the generic secret response generator for message exchange
  624. ret = message_exchange_response_generator((char*)decrypted_data, &resp_data, &resp_data_length);
  625. if(ret !=0)
  626. {
  627. SAFE_FREE(decrypted_data);
  628. SAFE_FREE(resp_data);
  629. return INVALID_SESSION;
  630. }
  631. }
  632. else if(ms->msg_type == ENCLAVE_TO_ENCLAVE_CALL)
  633. {
  634. //Call the destination enclave's dispatcher to call the appropriate function in the destination enclave
  635. ret = enclave_to_enclave_call_dispatcher((char*)decrypted_data, decrypted_data_length, &resp_data, &resp_data_length);
  636. if(ret !=0)
  637. {
  638. SAFE_FREE(decrypted_data);
  639. SAFE_FREE(resp_data);
  640. return INVALID_SESSION;
  641. }
  642. }
  643. else
  644. {
  645. SAFE_FREE(decrypted_data);
  646. return INVALID_REQUEST_TYPE_ERROR;
  647. }
  648. if(resp_data_length > max_payload_size)
  649. {
  650. SAFE_FREE(resp_data);
  651. SAFE_FREE(decrypted_data);
  652. return OUT_BUFFER_LENGTH_ERROR;
  653. }
  654. resp_message_calc_size = sizeof(secure_message_t)+ resp_data_length;
  655. if(resp_message_calc_size > resp_message_size)
  656. {
  657. SAFE_FREE(resp_data);
  658. SAFE_FREE(decrypted_data);
  659. return OUT_BUFFER_LENGTH_ERROR;
  660. }
  661. //Code to build the response back to the Source Enclave
  662. temp_resp_message = (secure_message_t*)malloc(resp_message_calc_size);
  663. if(!temp_resp_message)
  664. {
  665. SAFE_FREE(resp_data);
  666. SAFE_FREE(decrypted_data);
  667. return MALLOC_ERROR;
  668. }
  669. memset(temp_resp_message,0,sizeof(secure_message_t)+ resp_data_length);
  670. const uint32_t data2encrypt_length = (uint32_t)resp_data_length;
  671. temp_resp_message->session_id = session_info->session_id;
  672. temp_resp_message->message_aes_gcm_data.payload_size = data2encrypt_length;
  673. //Increment the Session Nonce (Replay Protection)
  674. session_info->active.counter = session_info->active.counter + 1;
  675. //Set the response nonce as the session nonce
  676. memcpy(&temp_resp_message->message_aes_gcm_data.reserved,&session_info->active.counter,sizeof(session_info->active.counter));
  677. //Prepare the response message with the encrypted payload
  678. status = sgx_rijndael128GCM_encrypt(&session_info->active.AEK, (uint8_t*)resp_data, data2encrypt_length,
  679. reinterpret_cast<uint8_t *>(&(temp_resp_message->message_aes_gcm_data.payload)),
  680. reinterpret_cast<uint8_t *>(&(temp_resp_message->message_aes_gcm_data.reserved)),
  681. sizeof(temp_resp_message->message_aes_gcm_data.reserved), plaintext, plaintext_length,
  682. &(temp_resp_message->message_aes_gcm_data.payload_tag));
  683. if(SGX_SUCCESS != status)
  684. {
  685. SAFE_FREE(resp_data);
  686. SAFE_FREE(decrypted_data);
  687. SAFE_FREE(temp_resp_message);
  688. return status;
  689. }
  690. memset(resp_message, 0, sizeof(secure_message_t)+ resp_data_length);
  691. memcpy(resp_message, temp_resp_message, sizeof(secure_message_t)+ resp_data_length);
  692. SAFE_FREE(decrypted_data);
  693. SAFE_FREE(resp_data);
  694. SAFE_FREE(temp_resp_message);
  695. return SUCCESS;
  696. }
  697. */
  698. /*
  699. //Close a current session
  700. ATTESTATION_STATUS close_session(sgx_enclave_id_t src_enclave_id,
  701. sgx_enclave_id_t dest_enclave_id)
  702. {
  703. sgx_status_t status;
  704. uint32_t retstatus;
  705. //Ocall to ask the destination enclave to end the session
  706. status = end_session_ocall(&retstatus, src_enclave_id, dest_enclave_id);
  707. if (status == SGX_SUCCESS)
  708. {
  709. if ((ATTESTATION_STATUS)retstatus != SUCCESS)
  710. return ((ATTESTATION_STATUS)retstatus);
  711. }
  712. else
  713. {
  714. return ATTESTATION_SE_ERROR;
  715. }
  716. return SUCCESS;
  717. }
  718. */
  719. // TODO: Fix this.
  720. //Respond to the request from the Source Enclave to close the session
  721. ATTESTATION_STATUS end_session(/**/)
  722. {
  723. return SUCCESS;
  724. }
  725. /*
  726. // 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)
  727. // Why can't it just keep a counter that is incremented? What are the values of g_session_id_tracker array?
  728. //Returns a new sessionID for the source destination session
  729. ATTESTATION_STATUS generate_session_id(uint32_t *session_id)
  730. {
  731. ATTESTATION_STATUS status = SUCCESS;
  732. if(!session_id)
  733. {
  734. return INVALID_PARAMETER_ERROR;
  735. }
  736. //if the session structure is untintialized, set that as the next session ID
  737. for (int i = 0; i < MAX_SESSION_COUNT; i++)
  738. {
  739. if (g_session_id_tracker[i] == NULL)
  740. {
  741. *session_id = i;
  742. return status;
  743. }
  744. }
  745. status = NO_AVAILABLE_SESSION_ERROR;
  746. return status;
  747. */
  748. // *session_id=++global_session_id;
  749. //}