App.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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.challa
  29. *
  30. */
  31. // App.cpp : Defines the entry point for the console application.
  32. #include <stdio.h>
  33. #include <map>
  34. #include "../Decryptor/Decryptor_u.h"
  35. #include "sgx_eid.h"
  36. #include "sgx_urts.h"
  37. #define __STDC_FORMAT_MACROS
  38. #include <inttypes.h>
  39. #include<unistd.h>
  40. // for sealing - sgx_calc_sealed_data_size
  41. #include "sgx_tseal.h"
  42. // For reading from/writing to file -sealing.
  43. #include <fcntl.h>
  44. #include <sys/types.h>
  45. #include <sys/stat.h>
  46. #include "systemLA.h"
  47. //#define UNUSED(val) (void)(val)
  48. #define TCHAR char
  49. #define _TCHAR char
  50. #define _T(str) str
  51. #define scanf_s scanf
  52. // Not sure if I need this later - as such, I (decryptor app) will only ever need to talk to 1 enclave at a time - verifier enclave first and then the apache enclave.
  53. //extern std::map<sgx_enclave_id_t, uint32_t>g_enclave_id_map;
  54. //int __ImageBase=0;
  55. sgx_enclave_id_t e2_enclave_id = 0;
  56. #define Decryptor_PATH "libDecryptor.so"
  57. //////////////////////////////////////////////////
  58. #include <stdio.h>
  59. uint32_t write_to_fd(int fd, uint8_t* msg, uint32_t* expected_msg_length)
  60. {
  61. lseek(fd, 0, SEEK_SET);
  62. ssize_t bytes_written;
  63. bytes_written = write(fd, msg, *expected_msg_length);
  64. if(bytes_written <= 0)
  65. return 0xFFFFFFFF;
  66. fsync(fd);
  67. *expected_msg_length = bytes_written;
  68. return 0;
  69. }
  70. uint32_t read_from_fd(int fd, uint8_t* msg, size_t* expected_msg_length)
  71. {
  72. ssize_t bytes_read;
  73. lseek(fd, 0, SEEK_SET);
  74. bytes_read = read(fd, msg, *expected_msg_length);
  75. if(bytes_read <= 0)
  76. return 0xFFFFFFFF;
  77. *expected_msg_length = bytes_read;
  78. return 0;
  79. }
  80. uint32_t unseal_signing_key_pair_from_disk(int fd, __attribute__((unused)) sgx_ec256_public_t* pub_key, size_t* actual_sealed_msg_length)
  81. {
  82. uint32_t ret_status;
  83. uint8_t* sgx_sealed_msg;
  84. printf("expected read 0x%ld\n", *actual_sealed_msg_length);
  85. sgx_sealed_msg = (uint8_t*) malloc(0x300); // malloc(*actual_sealed_msg_length); (0x300 for EDL)
  86. ret_status = read_from_fd(fd, sgx_sealed_msg, actual_sealed_msg_length);
  87. if(ret_status != 0)
  88. {
  89. free(sgx_sealed_msg);
  90. return 0xFFFFFFFF;
  91. }
  92. printf("actual read 0x%ld and ret_status 0x%x\n", *actual_sealed_msg_length, ret_status); fflush(stdout);
  93. size_t counter;
  94. for(counter=0;counter<*actual_sealed_msg_length;counter++)
  95. printf("%x ", *(sgx_sealed_msg+counter));
  96. printf("\n"); fflush(stdout);
  97. Decryptor_unseal_and_restore_sealed_signing_key_pair(e2_enclave_id, &ret_status, pub_key, sgx_sealed_msg, actual_sealed_msg_length);
  98. free(sgx_sealed_msg);
  99. return ret_status;
  100. }
  101. uint32_t create_and_seal_signing_key_pair_to_disk( __attribute__((unused)) int fd, __attribute__((unused)) sgx_ec256_public_t* pub_key)
  102. {
  103. uint32_t ret_status;
  104. // Generating a signing ECDSA key to sign the encryption key.
  105. uint32_t length;
  106. Decryptor_calculate_sealed_data_size(e2_enclave_id, &length, 3*SGX_ECP256_KEY_SIZE); // sgx_calc_sealed_data_size(0,3*SGX_ECP256_KEY_SIZE);
  107. if(length == 0xFFFFFFFF)
  108. return 0xFFFFFFFF;
  109. printf("0x%x input msg, 0x%x bytes for sealed msg in parameter value\n", 3*SGX_ECP256_KEY_SIZE, length); fflush(stdout);
  110. uint8_t* sealed_data=(uint8_t*) malloc(0x300);
  111. printf("Made call to sgx_calc_sealed_data_size\n"); fflush(stdout);
  112. Decryptor_create_and_seal_ecdsa_signing_key_pair(e2_enclave_id, &ret_status, pub_key, &length, sealed_data);
  113. if(ret_status != SGX_SUCCESS)
  114. {
  115. printf("create_and_seal called returned an error: %x", ret_status);
  116. free(sealed_data);
  117. return 0xFFFFFFFF;
  118. }
  119. printf("It returned sgx_success\n"); fflush(stdout); // *actual_sealed_msg_length=length;
  120. ret_status = write_to_fd(fd, sealed_data, &length);
  121. free(sealed_data);
  122. // if(ret_status > 0)
  123. // *actual_sealed_msg_length = ret_status;
  124. // return 0;
  125. return ret_status;
  126. }
  127. int main(__attribute__((unused)) int argc, __attribute__((unused)) char* argv[])
  128. {
  129. uint32_t ret_status;
  130. sgx_status_t status;
  131. // For sgx setup
  132. int launch_token_updated;
  133. sgx_launch_token_t launch_token;
  134. // uint8_t* pub_key = (uint8_t*) malloc(2*SGX_ECP256_KEY_SIZE);
  135. sgx_ec256_public_t pub_key; uint32_t counter;
  136. size_t sealed_msg_length_in_file;
  137. status = sgx_create_enclave(Decryptor_PATH, SGX_DEBUG_FLAG, &launch_token, &launch_token_updated, &e2_enclave_id, NULL);
  138. if(status != SGX_SUCCESS)
  139. {
  140. printf("\nLoad Enclave Failure");
  141. return -1;
  142. }
  143. printf("\nDecryptor - EnclaveID %" PRIx64, e2_enclave_id);
  144. fflush(stdout);
  145. int sealed_signing_key_fd = open("sealed_signing_key.txt", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
  146. if(sealed_signing_key_fd == -1)
  147. {
  148. perror("\nError in opening the file sealed_signing_key.txt - ");
  149. fflush(stderr);
  150. return 0xFFFFFFFF;
  151. }
  152. printf("\nSuccessfully opened a file to seal the signing key pair for the client.\n");
  153. fflush(stdout);
  154. // int start = lseek(sealed_signing_key_fd, 0, SEEK_SET);
  155. // int end = lseek(sealed_signing_key_fd, 0, SEEK_END);
  156. struct stat st; ret_status = fstat(sealed_signing_key_fd, &st);
  157. //sealed_msg_length_in_file = st.st_size;
  158. if(ret_status != 0)
  159. {
  160. perror("error in finding the file size. - ");
  161. fflush(stderr);
  162. return 0xffffffff;
  163. }
  164. sealed_msg_length_in_file = st.st_size;
  165. if(sealed_msg_length_in_file == 0) //if(start == end && start != -1)
  166. {
  167. // TODO: file is empty. create signing key pair.
  168. // int start = lseek(sealed_signing_key_fd, 0, SEEK_SET);
  169. ret_status = create_and_seal_signing_key_pair_to_disk(sealed_signing_key_fd, &pub_key);
  170. if(ret_status != 0)
  171. {
  172. printf("\n error in generating the ecdsa signing key pair \n");
  173. fflush(stdout); sgx_destroy_enclave(e2_enclave_id);
  174. return 0xFFFFFFFF;
  175. }
  176. fflush(stdout);
  177. printf("\n Generated the ecdsa key pair successfully - gx, gy\n");
  178. for(counter=0;counter<32;counter++)
  179. printf("0x%x ",pub_key.gx[counter]);
  180. printf("\n");
  181. for(counter=0;counter<32;counter++)
  182. printf("0x%x ",pub_key.gy[counter]);
  183. printf("\n");
  184. fflush(stdout);
  185. // fflush(stdout);
  186. }
  187. else {
  188. // start = lseek(sealed_signing_key_fd, 0, SEEK_CUR);
  189. // if(actual_sealed_msg_length == 0)
  190. // actual_sealed_msg_length = size; // - start;
  191. ret_status = unseal_signing_key_pair_from_disk(sealed_signing_key_fd, &pub_key, &sealed_msg_length_in_file);
  192. if(ret_status != SGX_SUCCESS)
  193. {
  194. printf("\n error in unsealing the ecdsa signing key pair:%d \n", ret_status);
  195. fflush(stdout); sgx_destroy_enclave(e2_enclave_id);
  196. return 0xFFFFFFFF;
  197. }
  198. printf("\n Recovered the ecdsa key pair successfully - gx, gy\n");
  199. for(counter=0;counter<32;counter++)
  200. printf("%02x",pub_key.gx[31-counter]);
  201. // printf("\n");
  202. for(counter=0;counter<32;counter++)
  203. printf("%02x",pub_key.gy[31-counter]);
  204. printf("\n");
  205. fflush(stdout);
  206. }
  207. close(sealed_signing_key_fd);
  208. // LA with the verifier
  209. ret_status=local_attestation_initiator(3824, e2_enclave_id);
  210. if(ret_status!=0)
  211. {
  212. printf("local attestation - with the verifier - did not successfully return: %x\n", ret_status); fflush(stdout); sgx_destroy_enclave(e2_enclave_id);
  213. return 0xFFFFFFFF;
  214. }
  215. // Does decryption too - should probs rename it
  216. ret_status=local_attestation_initiator(3825, e2_enclave_id);
  217. if(ret_status!=0)
  218. {
  219. printf("local attestation - with the apache - did not successfully return: %x\n", ret_status); fflush(stdout); sgx_destroy_enclave(e2_enclave_id);
  220. return 0xFFFFFFFF;
  221. }
  222. /* // LA with the apache - currently set to return failure - should change it to success when the code to send the mrsigner from the verifier to the decryptor is added- TODO: <--- that
  223. ret_status=local_attestation_initiator(3826, e2_enclave_id); // TODO: Change port or sth
  224. if(ret_status!=0)
  225. {
  226. printf("local attestation - with apache - did not successfully return: %x\n", ret_status); fflush(stdout); sgx_destroy_enclave(e2_enclave_id);
  227. return 0xFFFFFFFF;
  228. }
  229. */ // TODO: Continue with other msgs - send sign(enc | verifier mr_enclave)
  230. sgx_destroy_enclave(e2_enclave_id);
  231. return 0;
  232. }