Decryptor.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. // Enclave2.cpp : Defines the exported functions for the DLL application
  32. #include "sgx_eid.h"
  33. #include "sgx_tcrypto.h"
  34. #include "Decryptor_t.h"
  35. #include "EnclaveMessageExchange.h"
  36. #include "error_codes.h"
  37. #include "Utility_Decryptor.h"
  38. #include "sgx_thread.h"
  39. #include "sgx_dh.h"
  40. #include <map>
  41. #include "sgx_tcrypto.h"
  42. #include "LocalAttestationCode_t.h"
  43. #include "sgx_tseal.h"
  44. // internal-internal
  45. uint32_t create_ecdsa_key_pair(sgx_ec256_public_t* pub_key, sgx_ec256_private_t* priv_key);
  46. 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);
  47. void deserialize_string_to_key_pair(uint8_t* private_public_key_string, sgx_ec256_public_t* pub_key, sgx_ec256_private_t* priv_key);
  48. uint32_t create_and_sign_mitigator_header_value();
  49. uint32_t calculate_sealed_data_size( uint32_t input_size) ;
  50. 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);
  51. 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);
  52. //uint32_t create_and_sign_mitigator_header_value();
  53. uint32_t decrypt_verifiers_message_set_apache_mrsigner(uint8_t* ciphertext, uint8_t* tag);
  54. uint32_t encrypt_mitigator_header_to_apache(uint8_t* ciphertext, uint8_t* tag);
  55. uint32_t one_la_done=0;
  56. sgx_ec256_public_t short_term_pub_key;
  57. sgx_ec256_private_t short_term_priv_key;
  58. sgx_ec256_signature_t generated_signature;
  59. sgx_measurement_t apache_mr_signer;
  60. sgx_measurement_t verifier_mr_enclave;
  61. sgx_ec256_private_t signing_priv_key;
  62. extern "C" uint32_t verify_peer_enclave_trust(__attribute__((unused)) sgx_dh_session_enclave_identity_t* peer_enclave_identity)
  63. {
  64. uint32_t count;
  65. if(!peer_enclave_identity)
  66. {
  67. return INVALID_PARAMETER_ERROR;
  68. }
  69. if(one_la_done==0)
  70. {
  71. for(count=0; count<SGX_HASH_SIZE; count++)
  72. verifier_mr_enclave.m[count]=peer_enclave_identity->mr_enclave.m[count];
  73. memset(&(apache_mr_signer.m),0x0,SGX_HASH_SIZE); // "initialization"
  74. one_la_done=1;
  75. }
  76. else // apache enclave
  77. {
  78. sgx_measurement_t actual_mr_signer = peer_enclave_identity->mr_signer;
  79. // verifier's mrsigner
  80. // uint8_t expected_mr_signer[32] ={0xdf, 0xd7, 0x3b, 0x93, 0xea, 0x39, 0x02, 0x02, 0x3c, 0xd0, 0x52, 0x1a, 0xbd, 0x00, 0xaf, 0xb9, 0xa6, 0x54, 0x57, 0x3e, 0xe5, 0xef, 0x36, 0xf4, 0x8c, 0xc2, 0x4d, 0x92, 0x70, 0xae, 0xd4, 0x7c};
  81. int count;
  82. for(count=0; count<SGX_HASH_SIZE; count++)
  83. {
  84. if( actual_mr_signer.m[count] != apache_mr_signer.m[count] )
  85. return ENCLAVE_TRUST_ERROR;
  86. }
  87. }
  88. return SGX_SUCCESS;
  89. }
  90. // This needs to be called after the first local attestation is successful - otherwise, the internal apache_mr_signer.m will not be set properly for the comparison of the mrsigner for the 2nd LA in verify_peer_enclave_trust.
  91. // (I.e. if it is not called then DoS
  92. uint32_t decrypt_verifiers_message_set_apache_mrsigner(uint8_t* ciphertext, uint8_t* tag)
  93. {
  94. return decrypt(ciphertext, 32, tag, (uint8_t*) &(apache_mr_signer.m));
  95. // if(mrsigner_decryption_successful !=0)
  96. // return mrsigner_decryption_successful;
  97. }
  98. // ciphertext assumed to be at least 64 bytes, tag - at least 16 bytes
  99. uint32_t encrypt_mitigator_header_to_apache(uint8_t* ciphertext, uint8_t* tag)
  100. {
  101. uint32_t ret_status=create_and_sign_mitigator_header_value();
  102. if(ret_status != SGX_SUCCESS)
  103. {
  104. // printf("Could not generate or sign another keypair for client-side, error:%x.\n", ret_status); fflush(stdout);
  105. return 0xFFFFFFDD;
  106. }
  107. // serializing the weird signature string
  108. uint8_t mitigator_header_unencrypted[64];
  109. uint8_t *current_sig_byte = (uint8_t*)(&(generated_signature.x));
  110. uint32_t ecdsa_sig_count;
  111. for(ecdsa_sig_count=0;ecdsa_sig_count<32;ecdsa_sig_count++)
  112. mitigator_header_unencrypted[ecdsa_sig_count]=*(current_sig_byte+ecdsa_sig_count);
  113. current_sig_byte = (uint8_t*)(&(generated_signature.y));
  114. for(ecdsa_sig_count=0;ecdsa_sig_count<32;ecdsa_sig_count++)
  115. mitigator_header_unencrypted[ecdsa_sig_count+32]=*(current_sig_byte+ecdsa_sig_count);
  116. ret_status = encrypt_internal(mitigator_header_unencrypted, 64, tag, ciphertext);
  117. return ret_status;
  118. }
  119. uint32_t create_ecdsa_key_pair(sgx_ec256_public_t* pub_key, sgx_ec256_private_t* priv_key)
  120. {
  121. sgx_status_t se_ret; sgx_status_t se_ret2;
  122. //create ECC context
  123. sgx_ecc_state_handle_t ecc_state = NULL;
  124. se_ret = sgx_ecc256_open_context(&ecc_state);
  125. if(SGX_SUCCESS != se_ret)
  126. return se_ret;
  127. // generate private key and public key
  128. se_ret = sgx_ecc256_create_key_pair(priv_key, pub_key, ecc_state);
  129. se_ret2 = sgx_ecc256_close_context(ecc_state);
  130. if(SGX_SUCCESS != se_ret || se_ret2!= SGX_SUCCESS) // something weird has happened - couldn't shut it down.
  131. return 0xFFFFFFFF;
  132. return SGX_SUCCESS;
  133. }
  134. // todo: set to private
  135. // todo: assumes that the length of the keystring is at least 3*SGX_ECP256_KEY_SIZE
  136. 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)
  137. {
  138. if(private_public_key_string != NULL) // nowhere to serialize to
  139. {
  140. uint32_t counter;
  141. if(pub_key != NULL) // public key to serialize
  142. {
  143. for(counter=0;counter<SGX_ECP256_KEY_SIZE; counter++)
  144. *(private_public_key_string+counter)=pub_key->gx[counter];
  145. for(counter=SGX_ECP256_KEY_SIZE;counter<2*SGX_ECP256_KEY_SIZE; counter++)
  146. *(private_public_key_string+counter)=pub_key->gy[counter-SGX_ECP256_KEY_SIZE];
  147. }
  148. if(signing_priv_key != NULL) // private key to serialize
  149. {
  150. for(counter=2*SGX_ECP256_KEY_SIZE;counter<3*SGX_ECP256_KEY_SIZE; counter++)
  151. *(private_public_key_string+counter)=signing_priv_key->r[counter - 2*SGX_ECP256_KEY_SIZE];
  152. }
  153. }
  154. }
  155. // todo: set to private
  156. 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)
  157. {
  158. if(private_public_key_string != NULL) // nowhere to deserialize from
  159. {
  160. uint32_t counter;
  161. if(signing_priv_key != NULL)
  162. {
  163. for(counter=2*SGX_ECP256_KEY_SIZE;counter<3*SGX_ECP256_KEY_SIZE; counter++)
  164. signing_priv_key->r[counter-2*SGX_ECP256_KEY_SIZE]=*(private_public_key_string+counter);
  165. }
  166. if(pub_key != NULL)
  167. {
  168. for(counter=0;counter<SGX_ECP256_KEY_SIZE; counter++)
  169. pub_key->gx[counter]=*(private_public_key_string+counter);
  170. for(counter=SGX_ECP256_KEY_SIZE;counter<2*SGX_ECP256_KEY_SIZE; counter++)
  171. pub_key->gy[counter-SGX_ECP256_KEY_SIZE]=*(private_public_key_string+counter);
  172. }
  173. }
  174. }
  175. uint32_t create_and_seal_ecdsa_signing_key_pair(__attribute__((unused)) sgx_ec256_public_t* pub_key, __attribute__((unused)) uint32_t* sealed_data_length,
  176. __attribute__((unused)) uint8_t* sealed_data)
  177. {
  178. uint32_t ret_status; sgx_ec256_private_t private_key; uint32_t counter;
  179. ret_status=create_ecdsa_key_pair(pub_key, &private_key);
  180. if(ret_status!=SGX_SUCCESS)
  181. return ret_status;
  182. for(counter=0;counter<SGX_ECP256_KEY_SIZE; counter++)
  183. signing_priv_key.r[counter]=private_key.r[counter];
  184. // generating the entire string as there is no SGX function to generate the public key from the private one.
  185. uint8_t* private_public_key_string = (uint8_t*) malloc(3*SGX_ECP256_KEY_SIZE);
  186. uint8_t* sealed_data2 = (uint8_t*) malloc(*sealed_data_length);
  187. // serializing keypair to string
  188. serialize_key_pair_to_string(pub_key, &private_key, private_public_key_string);
  189. uint8_t* private_key_string = (uint8_t*) malloc(SGX_ECP256_KEY_SIZE);
  190. for(counter=0;counter<SGX_ECP256_KEY_SIZE;counter++)
  191. *(private_key_string+counter)=private_key.r[counter];
  192. // return *sealed_data_length;
  193. 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);
  194. for(counter=0;counter<*sealed_data_length;counter++)
  195. *(sealed_data+counter)=*(sealed_data2+counter);
  196. free(sealed_data2);
  197. free(private_key_string); //free(private_key);
  198. free(private_public_key_string);
  199. return ret_status; // SGX_SUCCESS;
  200. }
  201. /*
  202. 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)
  203. {
  204. return SGX_SUCCESS;
  205. }*/
  206. 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)
  207. {
  208. uint32_t expected_plaintext_msg_length; uint8_t* temp_plaintext; uint32_t counter; uint32_t ret_status;
  209. expected_plaintext_msg_length = sgx_get_encrypt_txt_len((sgx_sealed_data_t*)sealed_data);
  210. if(expected_plaintext_msg_length == 0xffffffff)
  211. return 0xFFFFFFFF;
  212. // uint32_t return_status;
  213. uint8_t* sealed_data2 = (uint8_t*) malloc(*sgx_sealed_data_length);
  214. for(counter=0;counter<*sgx_sealed_data_length;counter++)
  215. {
  216. *(sealed_data2+counter)=*(sealed_data+counter);
  217. }
  218. temp_plaintext = (uint8_t*)malloc( expected_plaintext_msg_length );
  219. ret_status = sgx_unseal_data((sgx_sealed_data_t*)sealed_data2, NULL, 0, temp_plaintext, &expected_plaintext_msg_length);
  220. if(ret_status != SGX_SUCCESS)
  221. {
  222. free(temp_plaintext);free(sealed_data2);
  223. switch(ret_status)
  224. {
  225. case SGX_ERROR_MAC_MISMATCH:
  226. // MAC of the sealed data is incorrect. The sealed data has been tampered.
  227. break;
  228. case SGX_ERROR_INVALID_ATTRIBUTE:
  229. // Indicates attribute field of the sealed data is incorrect.
  230. break;
  231. case SGX_ERROR_INVALID_ISVSVN:
  232. // Indicates isv_svn field of the sealed data is greater than the enclave�s ISVSVN. This is a downgraded enclave.
  233. break;
  234. case SGX_ERROR_INVALID_CPUSVN:
  235. // Indicates cpu_svn field of the sealed data is greater than the platform�s cpu_svn. enclave is on a downgraded platform.
  236. break;
  237. case SGX_ERROR_INVALID_KEYNAME:
  238. // Indicates key_name field of the sealed data is incorrect.
  239. break;
  240. default:
  241. // other errors
  242. break;
  243. }
  244. return ret_status;
  245. }
  246. deserialize_string_to_key_pair(temp_plaintext, pub_key, &signing_priv_key);
  247. free(temp_plaintext); free(sealed_data2);
  248. return SGX_SUCCESS;
  249. }
  250. uint32_t create_and_sign_mitigator_header_value()
  251. {
  252. // Otherwise: DoS or possible bypass (fake verifier does LA but real verifier mrenclave is given out by decryptor) - signature with junk verifier mrenclave or whatever is in the memory.
  253. if(one_la_done < 1)
  254. return 0xde; // This needs to be called at any point after the first local attestation is done - else, a junk verifier mrenclave will be included in the signature
  255. // TODO: Comment this
  256. memset(&(verifier_mr_enclave.m), 0x01, 32);
  257. // create key pair
  258. uint32_t ret_status = create_ecdsa_key_pair(&short_term_pub_key, &short_term_priv_key); uint32_t counter;
  259. uint32_t ret_status2;
  260. if(ret_status!=SGX_SUCCESS)
  261. return ret_status;
  262. // serialize public key, append mr_enclave
  263. 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
  264. serialize_key_pair_to_string(&short_term_pub_key, NULL, public_key_string);
  265. for(counter=32*2; counter<32*3; counter++) // appending mr_enclave
  266. *(public_key_string+counter)=verifier_mr_enclave.m[counter];
  267. // retrieve long-term private key from global variable - apparently, need to create a local copy or it crashes
  268. sgx_ec256_private_t long_term_priv_key;
  269. for(counter=0; counter<SGX_ECP256_KEY_SIZE; counter++)
  270. long_term_priv_key.r[counter] = signing_priv_key.r[counter];
  271. // sign public key with long-term private key
  272. sgx_ec256_signature_t local_signature; sgx_ecc_state_handle_t ecc_handle;
  273. //// opening context for signature
  274. ret_status = sgx_ecc256_open_context(&ecc_handle);
  275. if(ret_status != SGX_SUCCESS)
  276. return ret_status;
  277. ret_status = sgx_ecdsa_sign(public_key_string,2*SGX_ECP256_KEY_SIZE, &long_term_priv_key, &local_signature, ecc_handle);
  278. ret_status2 = sgx_ecc256_close_context(ecc_handle);
  279. free(public_key_string);
  280. if(ret_status == SGX_SUCCESS)
  281. {
  282. for(counter=0;counter<SGX_NISTP_ECP256_KEY_SIZE/sizeof(uint32_t); counter++)
  283. {
  284. generated_signature.x[counter] = local_signature.x[counter];
  285. generated_signature.y[counter] = local_signature.y[counter];
  286. }
  287. }
  288. if(ret_status != SGX_SUCCESS || ret_status2 != SGX_SUCCESS)
  289. return 0xFFFFFFFF;
  290. return 0;
  291. }
  292. uint32_t calculate_sealed_data_size( uint32_t input_size)
  293. {
  294. // *op_size=sgx_calc_sealed_data_size(0, input_size);
  295. return sgx_calc_sealed_data_size(0, input_size);
  296. }