App.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 "LocalAttestationUntrusted.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. 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. ret_status = unseal_signing_key_pair_from_disk(sealed_signing_key_fd, &pub_key, &sealed_msg_length_in_file);
  188. if(ret_status != SGX_SUCCESS)
  189. {
  190. printf("\n error in unsealing the ecdsa signing key pair:%d \n", ret_status);
  191. fflush(stdout); sgx_destroy_enclave(e2_enclave_id);
  192. return 0xFFFFFFFF;
  193. }
  194. printf("\n Recovered the ecdsa key pair successfully - gx, gy\n");
  195. for(counter=0;counter<32;counter++)
  196. printf("%02x",pub_key.gx[31-counter]);
  197. // printf("\n");
  198. for(counter=0;counter<32;counter++)
  199. printf("%02x",pub_key.gy[31-counter]);
  200. printf("\n");
  201. fflush(stdout);
  202. }
  203. close(sealed_signing_key_fd);
  204. int server_fd;
  205. server_fd = prepare_local_attestation_as_responder_fd_and_msg1(own_enclave_id, 3824);
  206. if(server_fd <=0)
  207. {
  208. printf("Error in setting up server socket."); fflush(stdout);
  209. sgx_destroy_enclave(e2_enclave_id);
  210. return server_fd;
  211. }
  212. printf("Successfully set up a socket to communicate with the Apache enclave.\n");
  213. fflush(stdout);
  214. // LA with the verifier
  215. ret_status = local_attestation_as_responder_msg2_msg3(e2_enclave_id);
  216. if(ret_status!=0)
  217. {
  218. printf("local attestation - with the verifier - did not successfully return: %x\n", ret_status); fflush(stdout);
  219. sgx_destroy_enclave(e2_enclave_id);
  220. return 0xFFFFFFFF;
  221. }
  222. ret_status = post_local_attestation_with_verifier(e2_enclave_id);
  223. if(ret_status!=0)
  224. {
  225. printf("post local attestation - with the verifier - did not successfully return: %x\n", ret_status); fflush(stdout);
  226. sgx_destroy_enclave(e2_enclave_id);
  227. return 0xFFFFFF01;
  228. }
  229. ret_status = local_attestation_as_responder_msg2_msg3(e2_enclave_id);
  230. if(ret_status!=0)
  231. {
  232. printf("local attestation - with the verifier - did not successfully return: %x\n", ret_status); fflush(stdout);
  233. sgx_destroy_enclave(e2_enclave_id);
  234. return 0xFFFFFFFF;
  235. }
  236. ret_status = post_local_attestation_with_apache(e2_enclave_id);
  237. if(ret_status!=0)
  238. {
  239. printf("post local attestation - with the verifier - did not successfully return: %x\n", ret_status); fflush(stdout);
  240. sgx_destroy_enclave(e2_enclave_id);
  241. return 0xFFFFFF01;
  242. }
  243. sgx_destroy_enclave(e2_enclave_id);
  244. return 0;
  245. }