Decryptor.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. localAttestation.verifier_mr_enclave/*
  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 "sgx_thread.h"
  38. #include "sgx_dh.h"
  39. #include <map>
  40. #include "sgx_tcrypto.h"
  41. #include "LocalAttestationCode_t.h"
  42. #include "sgx_tseal.h"
  43. #include "Openssl_crypto.h"
  44. #include "sgx_tseal.h"
  45. static class Decryptor {
  46. uint8_t apache_mrsigner[32];
  47. ECDSASignatureBox signatureBox;
  48. HybridEncryptionBox hybridEncryptionBoxClient;
  49. LocalAttestationTrusted localAttestation;
  50. uint32_t create_mitigator_token_M(uint8_t* token)
  51. {
  52. uint32_t internal_return_status;
  53. uint32_t counter;
  54. // create short-term ECDH key pair
  55. internal_return_status = hybridEncryptionBoxClient.generate_keypair();
  56. if(internal_return_status == NULL)
  57. return 0xff;
  58. hybridEncryptionBoxClient.get_public_key(token);
  59. // create token: concatenate short-term keypair with verifiers mrenclave.
  60. for(counter=0;counter<32;counter++)
  61. *(token + counter + hybridEncryptionBoxClient.ECDH_PUBLIC_KEY_SIZE) = *(localAttestation.verifier_mr_enclave[counter]);
  62. return 0;
  63. }
  64. uint32_t create_mitigator_header_H(uint8_t* signature_data, uint8_t* signature)
  65. {
  66. uint32_t internal_return_status;
  67. uint8_t local_signature[64];
  68. uint8_t local_signature_data[hybridEncryptionBoxClient.ECDH_PUBLIC_KEY_SIZE + 32];
  69. // 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.
  70. if(one_la_done < 1)
  71. 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
  72. internal_return_status = generate_mitigator_token(local_signature_data);
  73. if(internal_return_status != 0x0)
  74. return internal_return_status;
  75. internal_return_status = signatureBox.sign(local_signature_data, signatureBox.ECDH_PUBLIC_KEY_SIZE + 32, local_signature);
  76. if(internal_return_status != 0x0)
  77. return internal_return_status;
  78. for(counter=0;counter<hybridEncryptionBoxClient.ECDH_PUBLIC_KEY_SIZE + 32;counter++)
  79. signature_data[counter] = local_signature_data[counter];
  80. for(counter=0;counter<64;counter++)
  81. signature[counter] = local_signature[counter];
  82. return 0;
  83. }
  84. // done. But there might be one more return statement for the case when get_keypair returns sth (it is non void).
  85. uint32_t create_long_term_signing_keypair(uint8_t* private_public_key_string)
  86. {
  87. uint32_t internal_return_status;
  88. internal_return_status = signatureBox.generate_keypair();
  89. if(internal_return_status != 0)
  90. return internal_return_status;
  91. private_public_key_string = (uint8_t*) malloc(signatureBox.ECDH_PUBLIC_KEY_SIZE + signatureBox.ECDH_PRIVATE_KEY_SIZE);
  92. if(private_public_key_string == NULL)
  93. return 0xff;
  94. signatureBox.get_keypair(private_public_key_string);
  95. return 0;
  96. }
  97. uint32_t initialize_symmetric_key_decrypt_client_data(uint8_t* plaintext_client_public_key_plus_encrypted_data_plus_tag, uint32_t total_length, uint8_t* plaintext_client_data, uint32_t* plaintext_client_data_length)
  98. {
  99. uint8_t* ciphertext;
  100. uint32_t ciphertext_length;
  101. uint8_t* tag;
  102. // and now I will derive a shared key from the plaintext_client_public_key
  103. internal_return_status = hybridEncryptionBoxClient.initialize_symmetric_key(plaintext_client_public_key_plus_encrypted_data_plus_tag);
  104. if(internal_return_status != 0)
  105. return internal_return_status;
  106. // and then I will decrypt the rest of the client data with that key.
  107. ciphertext = plaintext_client_public_key_plus_encrypted_data_plus_tag + hybridEncryptionBoxClient.ECDH_PUBLIC_KEY_SIZE;
  108. ciphertext_length = total_length - hybridEncryptionBoxClient.ECDH_PUBLIC_KEY_SIZE - 16;
  109. tag = ciphertext + ciphertext_length;
  110. internal_return_status = hybridEncryptionBoxClient.encrypt_decrypt(ciphertext, ciphertext_length, plaintext_client_data, &plaintext_client_data_length, tag);
  111. return internal_return_status;
  112. }
  113. public:
  114. Decryptor(): signatureBox(), hybridEncryptionBoxClient(), localAttestation();
  115. ~Decryptor();
  116. // DONE.
  117. uint32_t create_and_seal_long_term_signing_key_pair(uint32_t* sealed_data_length, uint8_t* sealed_data)
  118. {
  119. uint32_t sgx_libcall_status;
  120. uint32_t internal_return_status;
  121. uint32_t temp_sealed_data_length;
  122. uint8_t* temp_sealed_data, private_public_key_string;
  123. uint32_t counter;
  124. temp_sealed_data_length = sgx_calc_sealed_data_size(0, signatureBox.ECDH_PUBLIC_KEY_SIZE + signatureBox.ECDH_PRIVATE_KEY_SIZE);
  125. if(temp_sealed_data_length == 0xFFFFFFFF)
  126. return 0x01;
  127. internal_return_status = create_long_term_signing_keypair(private_public_key_string);
  128. temp_sealed_data = (uint8_t*) malloc(*temp_sealed_data_length);
  129. sgx_libcall_status = sgx_seal_data(0, NULL, 3*SGX_ECP256_KEY_SIZE, private_public_key_string, *temp_sealed_data_length, (sgx_sealed_data_t*) temp_sealed_data);
  130. free(private_public_key_string); // first_decryption_output data - don't need it anymore, whether the call succeeds or fails.
  131. if(sgx_libcall_status != SGX_SUCCESS)
  132. {
  133. free(temp_sealed_data);
  134. return sgx_libcall_status;
  135. }
  136. for(counter=0;counter<temp_sealed_data_length;counter++)
  137. *(sealed_data + counter)=*(temp_sealed_data + counter);
  138. free(temp_sealed_data);
  139. return 0;
  140. }
  141. // DONE.
  142. uint32_t create_and_encrypt_mitigator_header_H(uint8_t* ciphertext_token_H_plus_tag)
  143. {
  144. uint32_t counter;
  145. uint8_t sign_data_and_sign[signatureBox.ECDH_PUBLIC_KEY_SIZE + 32 + 64];
  146. uint8_t temp_ciphertext_token_H[signatureBox.ECDH_PUBLIC_KEY_SIZE + 32 + 64 + 10];
  147. uint8_t temp_tag[16];
  148. uint32_t temp_ciphertext_token_H_length;
  149. uint32_t internal_return_status;
  150. internal_return_status = create_mitigator_header_value(token_H, sign_data_and_sign + signatureBox.ECDH_PUBLIC_KEY_SIZE + 32);
  151. if(internal_return_status != 0)
  152. return internal_return_status;
  153. internal_return_status = localAttestation.symmetricEncryptionBoxApache.encrypt_decrypt(1, sign_data_and_sign, signatureBox.ECDH_PUBLIC_KEY_SIZE + 32 + 64, temp_ciphertext_token_H, &temp_ciphertext_token_H_length, temp_tag);
  154. if(internal_return_status != 0)
  155. return internal_return_status;
  156. if(temp_ciphertext_token_H_length != 160)
  157. return 0x45;
  158. for(counter=0; counter<160; counter++)
  159. {
  160. *(ciphertext_token_H_plus_tag + counter) = *(temp_ciphertext_token_H + counter);
  161. }
  162. for(counter=0; counter<16; counter++)
  163. {
  164. *(ciphertext_token_H_plus_tag + counter) = *(temp_tag + counter);
  165. }
  166. return 0;
  167. }
  168. // DONE.
  169. uint32_t unseal_and_restore_long_term_signing_key_pair(uint8_t* sealed_data, uint32_t* sgx_sealed_data_length)
  170. {
  171. uint32_t temp_plaintext_length;
  172. uint8_t* temp_plaintext;
  173. uint32_t counter;
  174. uint32_t ret_status;
  175. uint8_t* temp_sealed_data ;
  176. temp_sealed_data = (uint8_t*) malloc(*sgx_sealed_data_length);
  177. for(counter=0;counter<*sgx_sealed_data_length;counter++)
  178. *(temp_sealed_data+counter)=*(sealed_data+counter);
  179. temp_plaintext_length = sgx_get_encrypt_txt_len((sgx_sealed_data_t*)sealed_data);
  180. if(temp_plaintext_length == 0xffffffff)
  181. return 0xFFFFFFFF;
  182. temp_plaintext = (uint8_t*)malloc( temp_plaintext_length );
  183. ret_status = sgx_unseal_data((sgx_sealed_data_t*)temp_sealed_data, NULL, 0, temp_plaintext, &temp_plaintext_length);
  184. free(temp_sealed_data);
  185. if(ret_status != SGX_SUCCESS)
  186. {
  187. free(temp_plaintext);
  188. return ret_status;
  189. }
  190. signatureBox.set_keypair(temp_plaintext);
  191. free(temp_plaintext);
  192. return 0;
  193. }
  194. // DONE.
  195. uint32_t decrypt_verifiers_message_set_apache_mrsigner(uint8_t* ciphertext_plus_tag)
  196. {
  197. uint8_t temp_apache_mrsigner[32+10];
  198. uint32_t temp_apache_mrsigner_length;
  199. uint32_t internal_return_status;
  200. uint32_t counter;
  201. uint8_t* tag;
  202. tag = ciphertext_plus_tag + 32;
  203. internal_return_status = localAttestation.symmetricEncryptionBoxVerifier.encrypt_decrypt(0, ciphertext_plus_tag, 32, temp_apache_mrsigner, &temp_apache_mrsigner_length, tag);
  204. if(internal_return_status != 0)
  205. return internal_return_status;
  206. if(temp_apache_mrsigner_length != 32)
  207. return 0x33;
  208. for(counter=0; counter<32; counter++)
  209. {
  210. apache_mr_signer[counter] = *(temp_apache_mrsigner + counter);
  211. }
  212. return 0;
  213. }
  214. // DONE.
  215. uint32_t process_apache_message_generate_response(uint8_t* input_ciphertext, uint32_t input_ciphertext_plus_tag_length, uint8_t* output_ciphertext, uint32_t* output_ciphertext_plus_tag_length)
  216. {
  217. uint8_t* first_decryption_output, plaintext_client_data, temp_output_ciphertext;
  218. uint32_t first_decryption_output_length, plaintext_client_data_length, temp_output_ciphertext_plus_tag_length, internal_return_status;
  219. uint8_t temp_output_tag[16];
  220. // TODO: May be have temporary variables for input ciphertext as they can't be passed directly to functions?
  221. first_decryption_output = (uint8_t*) malloc(input_ciphertext_length + 10);
  222. internal_return_status = localAttestation.symmetricEncryptionBoxApache.encrypt_decrypt(0, input_ciphertext, input_ciphertext_length, first_decryption_output, &first_decryption_output_length, input_ciphertext + input_ciphertext_length - 16);
  223. if(internal_return_status != 0)
  224. {
  225. free(first_decryption_output);
  226. return internal_return_status;
  227. }
  228. plaintext_client_data = (uint8_t*) malloc(first_decryption_output_length); // you will need less than this coz public key size.
  229. internal_return_status = initialize_symmetric_key_decrypt_client_data(first_decryption_output, first_decryption_output_length, plaintext_client_data, &plaintext_client_data_length);
  230. free(first_decryption_output);
  231. if(internal_return_status != 0)
  232. return internal_return_status;
  233. temp_output_ciphertext = (uint8_t*) malloc(plaintext_client_data_length + 20);
  234. // then I will encrypt the resulting first_decryption_output to the apache enclave.
  235. internal_return_status = localAttestation.symmetricEncryptionBoxApache.encrypt_decrypt(1, plaintext_client_data, plaintext_client_data_length, temp_output_ciphertext, &temp_output_ciphertext_length, temp_output_tag);
  236. free(plaintext_client_data);
  237. if(internal_return_status != 0)
  238. {
  239. free(temp_output_ciphertext);
  240. return internal_return_status;
  241. }
  242. for(counter=0; counter<temp_output_ciphertext_length; counter++)
  243. output_ciphertext[counter] = temp_output_ciphertext[counter];
  244. *output_ciphertext_length = temp_output_ciphertext_length + 16;
  245. free(temp_output_ciphertext);
  246. return 0;
  247. }
  248. };