EnclaveMessageExchange.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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, sgx_measurement_t* measurement);
  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. sgx_ec256_private_t short_term_priv_key;
  61. sgx_ec256_public_t short_term_pub_key; // for testing only: to test verification of signature
  62. uint32_t one_la_done;
  63. sgx_measurement_t verifier_mr_enclave;
  64. sgx_measurement_t apache_mr_signer;
  65. sgx_ecc_state_handle_t ecc_state;
  66. uint32_t session_ids[MAX_SESSION_COUNT];
  67. // Our enclave will not be doing LA with more than 1 decryptor enclave at a time.
  68. // We should not need this.
  69. //std::map<int, dh_session_t>g_dest_session_info_map;
  70. dh_session_t global_session_info;
  71. // TODO: May be we need to store all previously assigned session IDs instead of just the counter; to prevent replay attacks -
  72. uint32_t global_session_id=0;
  73. //Handle the request from Source Enclave for a session
  74. ATTESTATION_STATUS session_request(sgx_dh_msg1_t *dh_msg1,
  75. uint32_t *session_id )
  76. {
  77. // dh_session_t session_info;
  78. sgx_dh_session_t sgx_dh_session;
  79. sgx_status_t status = SGX_SUCCESS;
  80. if(!session_id || !dh_msg1)
  81. {
  82. return INVALID_PARAMETER_ERROR;
  83. }
  84. //Intialize the session as a session responder
  85. status = sgx_dh_init_session(SGX_DH_SESSION_RESPONDER, &sgx_dh_session);
  86. if(SGX_SUCCESS != status)
  87. {
  88. return status;
  89. }
  90. *session_id=1;
  91. global_session_info.status = IN_PROGRESS;
  92. //Generate Message1 that will be returned to Source Enclave
  93. status = sgx_dh_responder_gen_msg1((sgx_dh_msg1_t*)dh_msg1, &sgx_dh_session);
  94. if(SGX_SUCCESS != status)
  95. {
  96. global_session_id--;
  97. // SAFE_FREE(g_session_id_tracker[*session_id]);
  98. return status;
  99. }
  100. memcpy(&global_session_info.in_progress.dh_session, &sgx_dh_session, sizeof(sgx_dh_session_t));
  101. //return sgx_seal_data(0, NULL, 0, NULL, 0, NULL);
  102. //Store the session information under the correspoding source enlave id key
  103. // g_dest_session_info_map.insert(std::pair<sgx_enclave_id_t, dh_session_t>(src_enclave_id, session_info));
  104. return status;
  105. }
  106. //Verify Message 2, generate Message3 and exchange Message 3 with Source Enclave
  107. ATTESTATION_STATUS exchange_report(
  108. sgx_dh_msg2_t *dh_msg2,
  109. sgx_dh_msg3_t *dh_msg3,
  110. uint32_t* session_id, uint8_t* read)
  111. {
  112. sgx_key_128bit_t dh_aek; // Session key
  113. // dh_session_t session_info;
  114. ATTESTATION_STATUS status = SUCCESS;
  115. sgx_dh_session_t sgx_dh_session;
  116. sgx_dh_session_enclave_identity_t initiator_identity;
  117. if(!dh_msg2 || !dh_msg3)
  118. {
  119. return INVALID_PARAMETER_ERROR;
  120. }
  121. memset(&dh_aek,0, sizeof(sgx_key_128bit_t));
  122. // Why is there a do-while loop anyway? It seems like there is no successful exit ...
  123. // do
  124. // {
  125. // TODO: Make sure that this works - pointers
  126. // session_info = global_session_info;
  127. if(global_session_info.status != IN_PROGRESS)
  128. {
  129. status = INVALID_SESSION;
  130. end_session();
  131. }
  132. memcpy(&sgx_dh_session, &global_session_info.in_progress.dh_session, sizeof(sgx_dh_session_t));
  133. dh_msg3->msg3_body.additional_prop_length = 0;
  134. //Process message 2 from source enclave and obtain message 3
  135. sgx_status_t se_ret = sgx_dh_responder_proc_msg2(dh_msg2,
  136. dh_msg3,
  137. &sgx_dh_session,
  138. &dh_aek,
  139. &initiator_identity);
  140. if(SGX_SUCCESS != se_ret)
  141. {
  142. status = se_ret;
  143. end_session();
  144. }
  145. uint32_t hash_count;
  146. // THIS IS WHERE THE DECRYPTOR VERIFIES THE APACHE'S MRSIGNER IS THE PUBLIC KEY GIVEN AFTER THE LOCAL ATTESTATION WITH THE VERIFIER.
  147. //Verify source enclave's trust
  148. if(one_la_done == 0)
  149. {
  150. one_la_done = 1; *read=1;
  151. uint32_t ret = verify_peer_enclave_trust(&initiator_identity, NULL);
  152. if(ret != SUCCESS)
  153. {
  154. return ret; //INVALID_SESSION;
  155. }
  156. for(hash_count=0; hash_count<32; hash_count++)
  157. verifier_mr_enclave.m[hash_count] = initiator_identity.mr_signer.m[hash_count];
  158. }
  159. else
  160. {
  161. one_la_done=0; return 0xFFFFFFFF;
  162. /*
  163. uint32_t ret = verify_peer_enclave_trust(&initiator_identity, &apache_mrsigner);
  164. if(ret != SUCCESS)
  165. {
  166. return ret; //INVALID_SESSION;
  167. }
  168. */
  169. }
  170. // TODO: Verify that these changes will be lost on update.
  171. //save the session ID, status and initialize the session nonce
  172. global_session_info.session_id = *session_id;
  173. global_session_info.status = ACTIVE;
  174. global_session_info.active.counter = 0;
  175. memcpy(&global_session_info.active.AEK, &dh_aek, sizeof(sgx_key_128bit_t));
  176. memset(&dh_aek,0, sizeof(sgx_key_128bit_t));
  177. //g_session_count++;*/
  178. // }while(0);
  179. return status;
  180. }
  181. 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)
  182. {
  183. // return 0;
  184. sgx_status_t status;
  185. if(plaintext == NULL)
  186. {
  187. return INVALID_PARAMETER_ERROR;
  188. }
  189. //Check if the nonce for the session has not exceeded 2^32-2 if so end session and start a new session
  190. if(global_session_info.active.counter == ((uint32_t) - 2))
  191. { return 0xFF; // TODO: DO something here.
  192. // close_session(src_enclave_id);
  193. // create_session(src_enclave_id, session_info);
  194. }
  195. //return plaintext_length ;
  196. uint32_t count;
  197. uint8_t* temp_plaintext = (uint8_t*) malloc(plaintext_length);
  198. for(count=0; count<plaintext_length; count++)
  199. *(temp_plaintext+count)=*(plaintext+count);
  200. secure_message_t* temp_req_message = (secure_message_t*)malloc(sizeof(secure_message_t)+ plaintext_length); // WTF is this even - what happens to padding?
  201. memset(temp_req_message,0,sizeof(secure_message_t)+ plaintext_length);
  202. //Use the session nonce as the payload IV
  203. // memcpy(req_message->message_aes_gcm_data.reserved,&global_session_info.active.counter,sizeof(global_session_info.active.counter));
  204. // *active_counter=global_session_info.active.counter;
  205. memcpy(temp_req_message->message_aes_gcm_data.reserved,&global_session_info.active.counter,sizeof(global_session_info.active.counter));
  206. //Set the session ID of the message to the current session id
  207. // req_message->session_id = global_session_info.session_id;
  208. uint32_t temp_plaintext_length = plaintext_length;
  209. uint8_t* shared_key = (uint8_t*)malloc(16); // 128 bit aes key
  210. for(count=0;count<16;count++)
  211. *(shared_key+count)=global_session_info.active.AEK[count];
  212. // return 0;
  213. //Prepare the request message with the encrypted payload
  214. status =
  215. sgx_rijndael128GCM_encrypt((sgx_key_128bit_t*)shared_key, temp_plaintext, temp_plaintext_length,
  216. reinterpret_cast<uint8_t *>(&(temp_req_message->message_aes_gcm_data.payload)),
  217. reinterpret_cast<uint8_t *>(&(temp_req_message->message_aes_gcm_data.reserved)),
  218. 0xc, NULL, 0,
  219. &(temp_req_message->message_aes_gcm_data.payload_tag));
  220. for(count=0;count<48;count++)
  221. *(ciphertext+count) = temp_req_message->message_aes_gcm_data.payload[count];
  222. // tag length is 16 as per sgx_tseal.h
  223. for(count=0;count<16;count++)
  224. *(payload_tag+count) = temp_req_message->message_aes_gcm_data.payload_tag[count];
  225. // TODO: Should this depend on whether the call has been successful or not?
  226. //Update the value of the session nonce in the source enclave
  227. // global_session_info.active.counter +=1; // TODO: Activate this again.
  228. free(shared_key); free(temp_plaintext); free(temp_req_message);
  229. return status;
  230. //return global_session_info.active.counter-1;
  231. }
  232. /*
  233. 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)
  234. {
  235. uint32_t count; sgx_status_t status;
  236. uint8_t* shared_key = (uint8_t*)malloc(16); // 128 bit aes key
  237. secure_message_t* temp_req_message = (secure_message_t*)malloc(sizeof(secure_message_t)+ plaintext_length); // WTF is this even - what happens to padding?
  238. memset(temp_req_message,0,sizeof(secure_message_t)+ plaintext_length);
  239. for(count=0;count<16;count++)
  240. *(shared_key+count)=global_session_info.active.AEK[count];
  241. uint8_t* temp_ciphertext = (uint8_t*) malloc(ciphertext_length);
  242. for(count=0; count<ciphertext_length; count++)
  243. *(temp_ciphertext+count)=*(ciphertext+count);
  244. uint8_t* temp_plaintext = (uint8_t*) malloc(plaintext_length);
  245. memset(temp_plaintext, 0, plaintext_length);
  246. // uint8_t temp_payload_tag[16];// = (uint8_t*) malloc(16);
  247. // for(count=0; count<16; count++)
  248. // *(temp_payload_tag+count)=*(payload_tag+count);
  249. // const uint8_t expected_payload_tag[16]; // = (uint8_t*) malloc(16);
  250. uint8_t* iv = (uint8_t*) malloc(12);
  251. memset(iv, 0, 12);
  252. memcpy(iv, &global_session_info.active.counter, sizeof(uint32_t));
  253. //Decrypt the response message payload
  254. // 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$
  255. status = sgx_rijndael128GCM_decrypt((sgx_key_128bit_t*) shared_key, temp_ciphertext, ciphertext_length,
  256. temp_plaintext, iv, 0xc, NULL, 0, &(temp_req_message->message_aes_gcm_data.payload_tag));
  257. for(count=0;count<16; count++)
  258. {
  259. // if(temp_req_message->message_aes_gcm_data.payload_tag[count] != *(payload_tag+count))
  260. // return 0x2;
  261. *(payload_tag+count)=temp_req_message->message_aes_gcm_data.payload_tag[count];
  262. }
  263. // for(count=0; count<plaintext_length; count++)
  264. // *(plaintext+count)=*(temp_plaintext+count);
  265. //
  266. free(shared_key); free(temp_ciphertext); free(temp_plaintext); // free(temp_payload_tag);
  267. return status;
  268. return 0;
  269. }
  270. */
  271. uint32_t create_ecdsa_key_pair(sgx_ec256_public_t* pub_key, sgx_ec256_private_t* priv_key)
  272. {
  273. sgx_status_t se_ret; sgx_status_t se_ret2;
  274. //create ECC context
  275. ecc_state = NULL;
  276. se_ret = sgx_ecc256_open_context(&ecc_state);
  277. if(SGX_SUCCESS != se_ret)
  278. return se_ret;
  279. // generate private key and public key
  280. se_ret = sgx_ecc256_create_key_pair(priv_key, pub_key, ecc_state);
  281. se_ret2 = sgx_ecc256_close_context(ecc_state);
  282. if(SGX_SUCCESS != se_ret || se_ret2!= SGX_SUCCESS) // something weird has happened - couldn't shut it down.
  283. return 0xFFFFFFFF;
  284. return SGX_SUCCESS;
  285. }
  286. // todo: set to private
  287. // todo: assumes that the length of the keystring is at least 3*SGX_ECP256_KEY_SIZE
  288. 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)
  289. {
  290. if(private_public_key_string != NULL) // nowhere to serialize to
  291. {
  292. uint32_t counter;
  293. if(pub_key != NULL) // public key to serialize
  294. {
  295. for(counter=0;counter<SGX_ECP256_KEY_SIZE; counter++)
  296. *(private_public_key_string+counter)=pub_key->gx[counter];
  297. for(counter=SGX_ECP256_KEY_SIZE;counter<2*SGX_ECP256_KEY_SIZE; counter++)
  298. *(private_public_key_string+counter)=pub_key->gy[counter-SGX_ECP256_KEY_SIZE];
  299. }
  300. if(signing_priv_key != NULL) // private key to serialize
  301. {
  302. for(counter=2*SGX_ECP256_KEY_SIZE;counter<3*SGX_ECP256_KEY_SIZE; counter++)
  303. *(private_public_key_string+counter)=signing_priv_key->r[counter - 2*SGX_ECP256_KEY_SIZE];
  304. }
  305. /*
  306. if(pub_key != NULL) // public key to serialize
  307. {
  308. for(counter=SGX_ECP256_KEY_SIZE;counter<2*SGX_ECP256_KEY_SIZE; counter++)
  309. *(private_public_key_string+counter)=pub_key->gx[counter-SGX_ECP256_KEY_SIZE];
  310. for(counter=2*SGX_ECP256_KEY_SIZE;counter<3*SGX_ECP256_KEY_SIZE; counter++)
  311. *(private_public_key_string+counter)=pub_key->gy[counter-2*SGX_ECP256_KEY_SIZE];
  312. }*/
  313. }
  314. }
  315. // todo: set to private
  316. 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)
  317. {
  318. if(private_public_key_string != NULL) // nowhere to deserialize from
  319. {
  320. uint32_t counter;
  321. if(signing_priv_key != NULL)
  322. {
  323. for(counter=2*SGX_ECP256_KEY_SIZE;counter<3*SGX_ECP256_KEY_SIZE; counter++)
  324. signing_priv_key->r[counter-2*SGX_ECP256_KEY_SIZE]=*(private_public_key_string+counter);
  325. }
  326. if(pub_key != NULL)
  327. {
  328. for(counter=0;counter<SGX_ECP256_KEY_SIZE; counter++)
  329. pub_key->gx[counter]=*(private_public_key_string+counter);
  330. for(counter=SGX_ECP256_KEY_SIZE;counter<2*SGX_ECP256_KEY_SIZE; counter++)
  331. pub_key->gy[counter-SGX_ECP256_KEY_SIZE]=*(private_public_key_string+counter);
  332. }
  333. }
  334. }
  335. 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)
  336. {
  337. uint32_t ret_status; sgx_ec256_private_t private_key; uint32_t counter;
  338. ret_status=create_ecdsa_key_pair(pub_key, &private_key);
  339. if(ret_status!=SGX_SUCCESS)
  340. return ret_status;
  341. for(counter=0;counter<SGX_ECP256_KEY_SIZE; counter++)
  342. signing_priv_key.r[counter]=private_key.r[counter];
  343. // generating the entire string as there is no SGX function to generate the public key from the private one.
  344. uint8_t* private_public_key_string = (uint8_t*) malloc(3*SGX_ECP256_KEY_SIZE);
  345. uint8_t* sealed_data2 = (uint8_t*) malloc(*sealed_data_length);
  346. // serializing keypair to string
  347. serialize_key_pair_to_string(pub_key, &private_key, private_public_key_string);
  348. uint8_t* private_key_string = (uint8_t*) malloc(SGX_ECP256_KEY_SIZE);
  349. for(counter=0;counter<SGX_ECP256_KEY_SIZE;counter++)
  350. *(private_key_string+counter)=private_key.r[counter];
  351. // return *sealed_data_length;
  352. 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);
  353. for(counter=0;counter<*sealed_data_length;counter++)
  354. *(sealed_data+counter)=*(sealed_data2+counter);
  355. free(sealed_data2);
  356. free(private_key_string); //free(private_key);
  357. free(private_public_key_string);
  358. return ret_status; // SGX_SUCCESS;
  359. }
  360. /*
  361. 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)
  362. {
  363. return SGX_SUCCESS;
  364. }*/
  365. 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)
  366. {
  367. uint32_t expected_plaintext_msg_length; uint8_t* temp_plaintext; uint32_t counter; uint32_t ret_status;
  368. expected_plaintext_msg_length = sgx_get_encrypt_txt_len((sgx_sealed_data_t*)sealed_data);
  369. if(expected_plaintext_msg_length == 0xffffffff)
  370. return 0xFFFFFFFF;
  371. // uint32_t return_status;
  372. uint8_t* sealed_data2 = (uint8_t*) malloc(*sgx_sealed_data_length);
  373. for(counter=0;counter<*sgx_sealed_data_length;counter++)
  374. {
  375. *(sealed_data2+counter)=*(sealed_data+counter);
  376. }
  377. temp_plaintext = (uint8_t*)malloc( expected_plaintext_msg_length );
  378. ret_status = sgx_unseal_data((sgx_sealed_data_t*)sealed_data2, NULL, 0, temp_plaintext, &expected_plaintext_msg_length);
  379. if(ret_status != SGX_SUCCESS)
  380. {
  381. free(temp_plaintext);free(sealed_data2);
  382. switch(ret_status)
  383. {
  384. case SGX_ERROR_MAC_MISMATCH:
  385. // MAC of the sealed data is incorrect. The sealed data has been tampered.
  386. break;
  387. case SGX_ERROR_INVALID_ATTRIBUTE:
  388. // Indicates attribute field of the sealed data is incorrect.
  389. break;
  390. case SGX_ERROR_INVALID_ISVSVN:
  391. // Indicates isv_svn field of the sealed data is greater than the enclave�s ISVSVN. This is a downgraded enclave.
  392. break;
  393. case SGX_ERROR_INVALID_CPUSVN:
  394. // Indicates cpu_svn field of the sealed data is greater than the platform�s cpu_svn. enclave is on a downgraded platform.
  395. break;
  396. case SGX_ERROR_INVALID_KEYNAME:
  397. // Indicates key_name field of the sealed data is incorrect.
  398. break;
  399. default:
  400. // other errors
  401. break;
  402. }
  403. return ret_status;
  404. }
  405. deserialize_string_to_key_pair(temp_plaintext, pub_key, &signing_priv_key);
  406. free(temp_plaintext); free(sealed_data2);
  407. return SGX_SUCCESS;
  408. }
  409. 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)
  410. {
  411. // create key pair
  412. uint32_t ret_status = create_ecdsa_key_pair(&short_term_pub_key, &short_term_priv_key); uint32_t counter;
  413. uint32_t ret_status2;
  414. if(ret_status!=SGX_SUCCESS)
  415. return ret_status;
  416. // serialize public key, append mr_enclave
  417. 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
  418. serialize_key_pair_to_string(&short_term_pub_key, NULL, public_key_string);
  419. for(counter=32*2; counter<32*3; counter++) // appending mr_enclave
  420. *(public_key_string+counter)=mr_enclave->m[counter];
  421. // retrieve long-term private key from global variable - apparently, need to create a local copy or it crashes
  422. sgx_ec256_private_t long_term_priv_key;
  423. for(counter=0; counter<SGX_ECP256_KEY_SIZE; counter++)
  424. long_term_priv_key.r[counter] = signing_priv_key.r[counter];
  425. // sign public key with long-term private key
  426. sgx_ec256_signature_t local_signature; sgx_ecc_state_handle_t ecc_handle;
  427. //// opening context for signature
  428. ret_status = sgx_ecc256_open_context(&ecc_handle);
  429. if(ret_status != SGX_SUCCESS)
  430. return ret_status;
  431. ret_status = sgx_ecdsa_sign(public_key_string,2*SGX_ECP256_KEY_SIZE, &long_term_priv_key, &local_signature, ecc_handle);
  432. ret_status2 = sgx_ecc256_close_context(ecc_handle);
  433. free(public_key_string);
  434. if(ret_status == SGX_SUCCESS)
  435. {
  436. for(counter=0; counter<SGX_ECP256_KEY_SIZE; counter++)
  437. generated_pub_key->gx[counter] = short_term_pub_key.gx[counter];
  438. for(counter=0;counter<SGX_NISTP_ECP256_KEY_SIZE ; counter++)
  439. {
  440. generated_signature->x[counter] = local_signature.x[counter];
  441. generated_signature->y[counter] = local_signature.y[counter];
  442. }
  443. }
  444. if(ret_status != SGX_SUCCESS || ret_status2 != SGX_SUCCESS)
  445. return 0xFFFFFFFF;
  446. return 0;
  447. }
  448. uint32_t calculate_sealed_data_size( uint32_t input_size)
  449. {
  450. // *op_size=sgx_calc_sealed_data_size(0, input_size);
  451. return sgx_calc_sealed_data_size(0, input_size);
  452. }
  453. // TODO: Fix this.
  454. //Respond to the request from the Source Enclave to close the session
  455. ATTESTATION_STATUS end_session(/**/)
  456. {
  457. return SUCCESS;
  458. }
  459. /*
  460. // 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)
  461. // Why can't it just keep a counter that is incremented? What are the values of g_session_id_tracker array?
  462. //Returns a new sessionID for the source destination session
  463. ATTESTATION_STATUS generate_session_id(uint32_t *session_id)
  464. {
  465. ATTESTATION_STATUS status = SUCCESS;
  466. if(!session_id)
  467. {
  468. return INVALID_PARAMETER_ERROR;
  469. }
  470. //if the session structure is untintialized, set that as the next session ID
  471. for (int i = 0; i < MAX_SESSION_COUNT; i++)
  472. {
  473. if (g_session_id_tracker[i] == NULL)
  474. {
  475. *session_id = i;
  476. return status;
  477. }
  478. }
  479. status = NO_AVAILABLE_SESSION_ERROR;
  480. return status;
  481. */
  482. // *session_id=++global_session_id;
  483. //}
  484. uint32_t decrypt(uint8_t* ip_ciphertext, uint32_t ciphertext_len, uint8_t* ip_tag, uint8_t* op_plaintext)
  485. {
  486. uint32_t return_status2; uint32_t count;
  487. // unsigned char key[16]; uint32_t count;
  488. unsigned char key[16];
  489. // copying key to within the enclave as apparently it can't operate on it // TODO: Check if it works now.
  490. for(count=0;count<16;count++)
  491. key[count]=global_session_info.active.AEK[count];
  492. // copying ciphertext to within the enclave (otherwise it crashes with NOT enclave signal)
  493. uint8_t* ciphertext = (uint8_t*) malloc(ciphertext_len);
  494. for(count=0;count<ciphertext_len;count++)
  495. *(ciphertext+count)=*(ip_ciphertext+count);
  496. uint8_t* tag = (uint8_t*) malloc(16);
  497. for(count=0;count<16;count++)
  498. *(tag+count)=*(ip_tag+count);
  499. uint8_t* plaintext = (uint8_t*) malloc(ciphertext_len);
  500. // uint8_t encryption_tag[16]; uint8_t decryption_tag[16];
  501. // int ciphertext_len=48; int plaintext_len=32;
  502. uint8_t iv[12];
  503. // memset(ciphertext, 0, 48); memset(expected_plaintext, 0, 48);
  504. memset(iv, 0, 12); //memcpy(iv, &global_session_info.active.counter, 4);
  505. 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);
  506. /*
  507. // memset(expected_plaintext, 0, 32); memset(encryption_tag, 0, 16); memset(decryption_tag, 0, 16);
  508. return_status=sgx_rijndael128GCM_encrypt((sgx_key_128bit_t*) key, hash, 32, ciphertext, iv, 0xc, NULL, 0, (sgx_aes_gcm_128bit_tag_t*)encryption_tag);
  509. for(count=0;count<32;count++)
  510. *(op_ciphertext+count)=ciphertext[count];
  511. if(return_status == 0)
  512. return_status2=sgx_rijndael128GCM_decrypt((sgx_key_128bit_t*) key, ciphertext, 32, expected_plaintext, iv, 0xc, NULL, 0, (sgx_aes_gcm_128bit_tag_t*)encryption_tag);
  513. else
  514. return 0xffffffff;*/
  515. for(count=0;count<ciphertext_len;count++)
  516. *(op_plaintext+count)=plaintext[count];
  517. return return_status2;
  518. }
  519. uint32_t encrypt()
  520. {
  521. return 0;
  522. }