EnclaveMessageExchange.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  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. /*
  167. ATTESTATION_STATUS encrypt(__attribute__((unused)) uint8_t *plaintext, __attribute__((unused)) size_t plaintext_length, __attribute__((unused)) uint8_t* payload_tag, __attribute__((unused)) uint8_t* ciphertext, __attribute__((unused)) uint32_t* active_counter)
  168. {
  169. // return 0;
  170. sgx_status_t status;
  171. if(plaintext == NULL)
  172. {
  173. return INVALID_PARAMETER_ERROR;
  174. }
  175. //Check if the nonce for the session has not exceeded 2^32-2 if so end session and start a new session
  176. if(global_session_info.active.counter == ((uint32_t) - 2))
  177. { return 0xFF; // TODO: DO something here.
  178. // close_session(src_enclave_id);
  179. // create_session(src_enclave_id, session_info);
  180. }
  181. //return plaintext_length ;
  182. uint32_t count;
  183. uint8_t* temp_plaintext = (uint8_t*) malloc(plaintext_length);
  184. for(count=0; count<plaintext_length; count++)
  185. *(temp_plaintext+count)=*(plaintext+count);
  186. secure_message_t* temp_req_message = (secure_message_t*)malloc(sizeof(secure_message_t)+ plaintext_length); // WTF is this even - what happens to padding?
  187. memset(temp_req_message,0,sizeof(secure_message_t)+ plaintext_length);
  188. //Use the session nonce as the payload IV
  189. // memcpy(req_message->message_aes_gcm_data.reserved,&global_session_info.active.counter,sizeof(global_session_info.active.counter));
  190. // *active_counter=global_session_info.active.counter;
  191. memcpy(temp_req_message->message_aes_gcm_data.reserved,&global_session_info.active.counter,sizeof(global_session_info.active.counter));
  192. //Set the session ID of the message to the current session id
  193. // req_message->session_id = global_session_info.session_id;
  194. uint32_t temp_plaintext_length = plaintext_length;
  195. uint8_t* shared_key = (uint8_t*)malloc(16); // 128 bit aes key
  196. for(count=0;count<16;count++)
  197. *(shared_key+count)=global_session_info.active.AEK[count];
  198. // return 0;
  199. //Prepare the request message with the encrypted payload
  200. status =
  201. sgx_rijndael128GCM_encrypt((sgx_key_128bit_t*)shared_key, temp_plaintext, temp_plaintext_length,
  202. reinterpret_cast<uint8_t *>(&(temp_req_message->message_aes_gcm_data.payload)),
  203. reinterpret_cast<uint8_t *>(&(temp_req_message->message_aes_gcm_data.reserved)),
  204. 0xc, NULL, 0,
  205. &(temp_req_message->message_aes_gcm_data.payload_tag));
  206. for(count=0;count<48;count++)
  207. *(ciphertext+count) = temp_req_message->message_aes_gcm_data.payload[count];
  208. // tag length is 16 as per sgx_tseal.h
  209. for(count=0;count<16;count++)
  210. *(payload_tag+count) = temp_req_message->message_aes_gcm_data.payload_tag[count];
  211. // TODO: Should this depend on whether the call has been successful or not?
  212. //Update the value of the session nonce in the source enclave
  213. // global_session_info.active.counter +=1; // TODO: Activate this again.
  214. free(shared_key); free(temp_plaintext); free(temp_req_message);
  215. return status;
  216. //return global_session_info.active.counter-1;
  217. }
  218. uint32_t decrypt(__attribute__((unused)) uint8_t* ciphertext, __attribute__((unused)) size_t ciphertext_length, __attribute__((unused)) uint8_t* payload_tag, __attribute__((unused)) uint8_t* plaintext, __attribute__((unused)) size_t plaintext_length)
  219. {
  220. uint32_t count; sgx_status_t status;
  221. uint8_t* shared_key = (uint8_t*)malloc(16); // 128 bit aes key
  222. secure_message_t* temp_req_message = (secure_message_t*)malloc(sizeof(secure_message_t)+ plaintext_length); // WTF is this even - what happens to padding?
  223. memset(temp_req_message,0,sizeof(secure_message_t)+ plaintext_length);
  224. for(count=0;count<16;count++)
  225. *(shared_key+count)=global_session_info.active.AEK[count];
  226. uint8_t* temp_ciphertext = (uint8_t*) malloc(ciphertext_length);
  227. for(count=0; count<ciphertext_length; count++)
  228. *(temp_ciphertext+count)=*(ciphertext+count);
  229. uint8_t* temp_plaintext = (uint8_t*) malloc(plaintext_length);
  230. memset(temp_plaintext, 0, plaintext_length);
  231. // uint8_t temp_payload_tag[16];// = (uint8_t*) malloc(16);
  232. // for(count=0; count<16; count++)
  233. // *(temp_payload_tag+count)=*(payload_tag+count);
  234. // const uint8_t expected_payload_tag[16]; // = (uint8_t*) malloc(16);
  235. uint8_t* iv = (uint8_t*) malloc(12);
  236. memset(iv, 0, 12);
  237. memcpy(iv, &global_session_info.active.counter, sizeof(uint32_t));
  238. //Decrypt the response message payload
  239. // status = sgx_rijndael128GCM_decrypt(&global_session_info.active.AEK, resp_message->message_aes_gcm_data.payload, resp_message->message_aes_gcm_data.payload_size, pl$reinterpret_cast<uint8_t *>(&(resp_message->message_aes_gcm_data.reserved)), sizeof(resp_message->message_aes_gcm_data.reserved), NULL, 0, &resp_message$
  240. status = sgx_rijndael128GCM_decrypt((sgx_key_128bit_t*) shared_key, temp_ciphertext, ciphertext_length,
  241. temp_plaintext, iv, 0xc, NULL, 0, &(temp_req_message->message_aes_gcm_data.payload_tag));
  242. for(count=0;count<16; count++)
  243. {
  244. // if(temp_req_message->message_aes_gcm_data.payload_tag[count] != *(payload_tag+count))
  245. // return 0x2;
  246. *(payload_tag+count)=temp_req_message->message_aes_gcm_data.payload_tag[count];
  247. }
  248. // for(count=0; count<plaintext_length; count++)
  249. // *(plaintext+count)=*(temp_plaintext+count);
  250. //
  251. free(shared_key); free(temp_ciphertext); free(temp_plaintext); // free(temp_payload_tag);
  252. return status;
  253. return 0;
  254. }
  255. */
  256. /*
  257. uint32_t create_ecdsa_key_pair(sgx_ec256_public_t* pub_key, sgx_ec256_private_t* priv_key)
  258. {
  259. sgx_status_t se_ret; sgx_status_t se_ret2;
  260. //create ECC context
  261. ecc_state = NULL;
  262. se_ret = sgx_ecc256_open_context(&ecc_state);
  263. if(SGX_SUCCESS != se_ret)
  264. return se_ret;
  265. // generate private key and public key
  266. se_ret = sgx_ecc256_create_key_pair(priv_key, pub_key, ecc_state);
  267. se_ret2 = sgx_ecc256_close_context(ecc_state);
  268. if(SGX_SUCCESS != se_ret || se_ret2!= SGX_SUCCESS) // something weird has happened - couldn't shut it down.
  269. return 0xFFFFFFFF;
  270. return SGX_SUCCESS;
  271. }
  272. // todo: set to private
  273. // todo: assumes that the length of the keystring is at least 3*SGX_ECP256_KEY_SIZE
  274. 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)
  275. {
  276. if(private_public_key_string != NULL) // nowhere to serialize to
  277. {
  278. uint32_t counter;
  279. if(pub_key != NULL) // public key to serialize
  280. {
  281. for(counter=0;counter<SGX_ECP256_KEY_SIZE; counter++)
  282. *(private_public_key_string+counter)=pub_key->gx[counter];
  283. for(counter=SGX_ECP256_KEY_SIZE;counter<2*SGX_ECP256_KEY_SIZE; counter++)
  284. *(private_public_key_string+counter)=pub_key->gy[counter-SGX_ECP256_KEY_SIZE];
  285. }
  286. if(signing_priv_key != NULL) // private key to serialize
  287. {
  288. for(counter=2*SGX_ECP256_KEY_SIZE;counter<3*SGX_ECP256_KEY_SIZE; counter++)
  289. *(private_public_key_string+counter)=signing_priv_key->r[counter - 2*SGX_ECP256_KEY_SIZE];
  290. }
  291. }
  292. }
  293. // todo: set to private
  294. 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)
  295. {
  296. if(private_public_key_string != NULL) // nowhere to deserialize from
  297. {
  298. uint32_t counter;
  299. if(signing_priv_key != NULL)
  300. {
  301. for(counter=2*SGX_ECP256_KEY_SIZE;counter<3*SGX_ECP256_KEY_SIZE; counter++)
  302. signing_priv_key->r[counter-2*SGX_ECP256_KEY_SIZE]=*(private_public_key_string+counter);
  303. }
  304. if(pub_key != NULL)
  305. {
  306. for(counter=0;counter<SGX_ECP256_KEY_SIZE; counter++)
  307. pub_key->gx[counter]=*(private_public_key_string+counter);
  308. for(counter=SGX_ECP256_KEY_SIZE;counter<2*SGX_ECP256_KEY_SIZE; counter++)
  309. pub_key->gy[counter-SGX_ECP256_KEY_SIZE]=*(private_public_key_string+counter);
  310. }
  311. }
  312. }
  313. 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)
  314. {
  315. uint32_t ret_status; sgx_ec256_private_t private_key; uint32_t counter;
  316. ret_status=create_ecdsa_key_pair(pub_key, &private_key);
  317. if(ret_status!=SGX_SUCCESS)
  318. return ret_status;
  319. for(counter=0;counter<SGX_ECP256_KEY_SIZE; counter++)
  320. signing_priv_key.r[counter]=private_key.r[counter];
  321. // generating the entire string as there is no SGX function to generate the public key from the private one.
  322. uint8_t* private_public_key_string = (uint8_t*) malloc(3*SGX_ECP256_KEY_SIZE);
  323. uint8_t* sealed_data2 = (uint8_t*) malloc(*sealed_data_length);
  324. // serializing keypair to string
  325. serialize_key_pair_to_string(pub_key, &private_key, private_public_key_string);
  326. uint8_t* private_key_string = (uint8_t*) malloc(SGX_ECP256_KEY_SIZE);
  327. for(counter=0;counter<SGX_ECP256_KEY_SIZE;counter++)
  328. *(private_key_string+counter)=private_key.r[counter];
  329. // return *sealed_data_length;
  330. 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);
  331. for(counter=0;counter<*sealed_data_length;counter++)
  332. *(sealed_data+counter)=*(sealed_data2+counter);
  333. free(sealed_data2);
  334. free(private_key_string); //free(private_key);
  335. free(private_public_key_string);
  336. return ret_status; // SGX_SUCCESS;
  337. }
  338. 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)
  339. {
  340. return SGX_SUCCESS;
  341. }
  342. 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)
  343. {
  344. uint32_t expected_plaintext_msg_length; uint8_t* temp_plaintext; uint32_t counter; uint32_t ret_status;
  345. expected_plaintext_msg_length = sgx_get_encrypt_txt_len((sgx_sealed_data_t*)sealed_data);
  346. if(expected_plaintext_msg_length == 0xffffffff)
  347. return 0xFFFFFFFF;
  348. // uint32_t return_status;
  349. uint8_t* sealed_data2 = (uint8_t*) malloc(*sgx_sealed_data_length);
  350. for(counter=0;counter<*sgx_sealed_data_length;counter++)
  351. {
  352. *(sealed_data2+counter)=*(sealed_data+counter);
  353. }
  354. temp_plaintext = (uint8_t*)malloc( expected_plaintext_msg_length );
  355. ret_status = sgx_unseal_data((sgx_sealed_data_t*)sealed_data2, NULL, 0, temp_plaintext, &expected_plaintext_msg_length);
  356. if(ret_status != SGX_SUCCESS)
  357. {
  358. free(temp_plaintext);free(sealed_data2);
  359. switch(ret_status)
  360. {
  361. case SGX_ERROR_MAC_MISMATCH:
  362. // MAC of the sealed data is incorrect. The sealed data has been tampered.
  363. break;
  364. case SGX_ERROR_INVALID_ATTRIBUTE:
  365. // Indicates attribute field of the sealed data is incorrect.
  366. break;
  367. case SGX_ERROR_INVALID_ISVSVN:
  368. // Indicates isv_svn field of the sealed data is greater than the enclave�s ISVSVN. This is a downgraded enclave.
  369. break;
  370. case SGX_ERROR_INVALID_CPUSVN:
  371. // Indicates cpu_svn field of the sealed data is greater than the platform�s cpu_svn. enclave is on a downgraded platform.
  372. break;
  373. case SGX_ERROR_INVALID_KEYNAME:
  374. // Indicates key_name field of the sealed data is incorrect.
  375. break;
  376. default:
  377. // other errors
  378. break;
  379. }
  380. return ret_status;
  381. }
  382. deserialize_string_to_key_pair(temp_plaintext, pub_key, &signing_priv_key);
  383. free(temp_plaintext); free(sealed_data2);
  384. return SGX_SUCCESS;
  385. }
  386. 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)
  387. {
  388. // create key pair
  389. uint32_t ret_status = create_ecdsa_key_pair(&short_term_pub_key, &short_term_priv_key); uint32_t counter;
  390. uint32_t ret_status2;
  391. if(ret_status!=SGX_SUCCESS)
  392. return ret_status;
  393. // serialize public key, append mr_enclave
  394. 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
  395. serialize_key_pair_to_string(&short_term_pub_key, NULL, public_key_string);
  396. for(counter=32*2; counter<32*3; counter++) // appending mr_enclave
  397. *(public_key_string+counter)=mr_enclave->m[counter];
  398. // retrieve long-term private key from global variable - apparently, need to create a local copy or it crashes
  399. sgx_ec256_private_t long_term_priv_key;
  400. for(counter=0; counter<SGX_ECP256_KEY_SIZE; counter++)
  401. long_term_priv_key.r[counter] = signing_priv_key.r[counter];
  402. // sign public key with long-term private key
  403. sgx_ec256_signature_t local_signature; sgx_ecc_state_handle_t ecc_handle;
  404. //// opening context for signature
  405. ret_status = sgx_ecc256_open_context(&ecc_handle);
  406. if(ret_status != SGX_SUCCESS)
  407. return ret_status;
  408. ret_status = sgx_ecdsa_sign(public_key_string,2*SGX_ECP256_KEY_SIZE, &long_term_priv_key, &local_signature, ecc_handle);
  409. ret_status2 = sgx_ecc256_close_context(ecc_handle);
  410. free(public_key_string);
  411. if(ret_status == SGX_SUCCESS)
  412. {
  413. for(counter=0; counter<SGX_ECP256_KEY_SIZE; counter++)
  414. generated_pub_key->gx[counter] = short_term_pub_key.gx[counter];
  415. for(counter=0;counter<SGX_NISTP_ECP256_KEY_SIZE ; counter++)
  416. {
  417. generated_signature->x[counter] = local_signature.x[counter];
  418. generated_signature->y[counter] = local_signature.y[counter];
  419. }
  420. }
  421. if(ret_status != SGX_SUCCESS || ret_status2 != SGX_SUCCESS)
  422. return 0xFFFFFFFF;
  423. return 0;
  424. }
  425. */
  426. uint32_t calculate_sealed_data_size( uint32_t input_size)
  427. {
  428. // *op_size=sgx_calc_sealed_data_size(0, input_size);
  429. return sgx_calc_sealed_data_size(0, input_size);
  430. }
  431. // TODO: Fix this.
  432. //Respond to the request from the Source Enclave to close the session
  433. ATTESTATION_STATUS end_session(/**/)
  434. {
  435. return SUCCESS;
  436. }
  437. /*
  438. // 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)
  439. // Why can't it just keep a counter that is incremented? What are the values of g_session_id_tracker array?
  440. //Returns a new sessionID for the source destination session
  441. ATTESTATION_STATUS generate_session_id(uint32_t *session_id)
  442. {
  443. ATTESTATION_STATUS status = SUCCESS;
  444. if(!session_id)
  445. {
  446. return INVALID_PARAMETER_ERROR;
  447. }
  448. //if the session structure is untintialized, set that as the next session ID
  449. for (int i = 0; i < MAX_SESSION_COUNT; i++)
  450. {
  451. if (g_session_id_tracker[i] == NULL)
  452. {
  453. *session_id = i;
  454. return status;
  455. }
  456. }
  457. status = NO_AVAILABLE_SESSION_ERROR;
  458. return status;
  459. */
  460. // *session_id=++global_session_id;
  461. //}
  462. uint32_t decrypt(uint8_t* ip_ciphertext, uint32_t ciphertext_len, uint8_t* ip_tag, uint8_t* op_plaintext)
  463. {
  464. // 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.
  465. // 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)
  466. uint32_t return_status2; uint32_t count;
  467. unsigned char key[16];
  468. // copying key to within the enclave as apparently it can't operate on it // TODO: Check if it works now.
  469. for(count=0;count<16;count++)
  470. key[count]=global_session_info.active.AEK[count];
  471. // copying ciphertext to within the enclave (otherwise it crashes with NOT enclave signal)
  472. uint8_t* ciphertext = (uint8_t*) malloc(ciphertext_len);
  473. for(count=0;count<ciphertext_len;count++)
  474. *(ciphertext+count)=*(ip_ciphertext+count);
  475. uint8_t* tag = (uint8_t*) malloc(16);
  476. for(count=0;count<16;count++)
  477. *(tag+count)=*(ip_tag+count);
  478. uint8_t* plaintext = (uint8_t*) malloc(ciphertext_len);
  479. uint8_t iv[12];
  480. memset(iv, 0, 12); //memcpy(iv, &global_session_info.active.counter, 4);
  481. 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);
  482. for(count=0;count<ciphertext_len;count++)
  483. *(op_plaintext+count)=plaintext[count];
  484. free(plaintext); free (ciphertext); free(tag);
  485. return return_status2;
  486. }
  487. uint32_t encrypt(uint8_t* ip_ciphertext, uint32_t ciphertext_len, uint8_t* ip_tag, uint8_t* op_plaintext)
  488. {
  489. uint32_t return_status2; uint32_t count;
  490. unsigned char key[16];
  491. // copying key to within the enclave as apparently it can't operate on it // TODO: Check if it works now.
  492. for(count=0;count<16;count++)
  493. key[count]=global_session_info.active.AEK[count];
  494. // copying ciphertext to within the enclave (otherwise it crashes with NOT enclave signal)
  495. uint8_t* ciphertext = (uint8_t*) malloc(ciphertext_len);
  496. for(count=0;count<ciphertext_len;count++)
  497. *(ciphertext+count)=*(ip_ciphertext+count);
  498. uint8_t* tag = (uint8_t*) malloc(16);
  499. for(count=0;count<16;count++)
  500. *(tag+count)=*(ip_tag+count);
  501. uint8_t* plaintext = (uint8_t*) malloc(ciphertext_len);
  502. uint8_t iv[12];
  503. memset(iv, 0, 12); //memcpy(iv, &global_session_info.active.counter, 4);
  504. 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);
  505. for(count=0;count<ciphertext_len;count++)
  506. *(op_plaintext+count)=plaintext[count];
  507. free(plaintext); free (ciphertext); free(tag);
  508. return return_status2;
  509. }
  510. uint32_t encrypt_internal(uint8_t* ip_ciphertext, uint32_t ciphertext_len, uint8_t* op_tag, uint8_t* op_plaintext)
  511. {
  512. //return encrypt(ip_ciphertext, ciphertext_len, ip_tag, op_plaintext);
  513. uint32_t return_status2; uint32_t count;
  514. unsigned char key[16];
  515. // copying key to within the enclave as apparently it can't operate on it // TODO: Check if it works now.
  516. for(count=0;count<16;count++)
  517. key[count]=global_session_info.active.AEK[count];
  518. // copying ciphertext to within the enclave (otherwise it crashes with NOT enclave signal)
  519. uint8_t* ciphertext = (uint8_t*) malloc(ciphertext_len);
  520. for(count=0;count<ciphertext_len;count++)
  521. *(ciphertext+count)=*(ip_ciphertext+count);
  522. uint8_t tag[16];
  523. uint8_t* plaintext = (uint8_t*) malloc(ciphertext_len);
  524. uint8_t iv[12];
  525. memset(iv, 0, 12); //memcpy(iv, &global_session_info.active.counter, 4);
  526. 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);
  527. for(count=0;count<ciphertext_len;count++)
  528. *(op_plaintext+count)=plaintext[count];
  529. for(count=0;count<16;count++)
  530. op_tag[count]=tag[count];
  531. free(plaintext); free (ciphertext);
  532. return return_status2;
  533. }