App.cpp 9.6 KB

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