App.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. #include "LocalAttestationUntrusted.h"
  43. // For reading from/writing to file -sealing.
  44. #include <fcntl.h>
  45. #include <sys/types.h>
  46. #include <sys/stat.h>
  47. #include <errno.h>
  48. //#define UNUSED(val) (void)(val)
  49. #define TCHAR char
  50. #define _TCHAR char
  51. #define _T(str) str
  52. #define scanf_s scanf
  53. // 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.
  54. //extern std::map<sgx_enclave_id_t, uint32_t>g_enclave_id_map;
  55. //int __ImageBase=0;
  56. sgx_enclave_id_t e2_enclave_id = 0;
  57. #define Decryptor_PATH "libDecryptor.so"
  58. //////////////////////////////////////////////////
  59. #include <stdio.h>
  60. uint32_t write_to_fd(int fd, uint8_t* msg, uint32_t* expected_msg_length)
  61. {
  62. lseek(fd, 0, SEEK_SET);
  63. ssize_t bytes_written;
  64. bytes_written = write(fd, msg, *expected_msg_length);
  65. if(bytes_written <= 0)
  66. return 0xFFFFFFFF;
  67. fsync(fd);
  68. *expected_msg_length = bytes_written;
  69. return 0;
  70. }
  71. uint32_t read_from_fd(int fd, uint8_t* msg, uint32_t* expected_msg_length)
  72. {
  73. ssize_t bytes_read;
  74. lseek(fd, 0, SEEK_SET);
  75. bytes_read = read(fd, msg, *expected_msg_length);
  76. if(bytes_read <= 0)
  77. return 0xFFFFFFFF;
  78. *expected_msg_length = bytes_read;
  79. return 0;
  80. }
  81. uint32_t unseal_signing_key_pair_from_disk(int fd, size_t sealed_msg_length_in_file)
  82. {
  83. uint32_t ret_status=0, length=sealed_msg_length_in_file, counter=0;
  84. uint8_t* sealed_data;
  85. sealed_data = (uint8_t*) malloc(0x300); //TODO: Get length of the sealed msg and try to read that much from the file.
  86. // May be pass the length of the file as input to this function and check that it is at least as much as the output of the sgx call.
  87. ret_status = read_from_fd(fd, sealed_data, &length);
  88. if(ret_status != 0)
  89. {
  90. free(sealed_data);
  91. return 0xFFFFFFFF;
  92. }
  93. for(counter=0;counter<length;counter++)
  94. printf("%x ", *(sealed_data+counter));
  95. printf("\n"); fflush(stdout);
  96. Decryptor_unseal_and_restore_long_term_signing_key_pair_wrapper(e2_enclave_id, &ret_status, sealed_data, &length);
  97. free(sealed_data);
  98. return ret_status;
  99. }
  100. uint32_t create_and_seal_signing_key_pair_to_disk(int fd)
  101. {
  102. uint32_t ret_status=0, length=0, counter=0;
  103. uint8_t* sealed_data;
  104. Decryptor_calculate_sealed_keypair_size_wrapper(e2_enclave_id, &length);
  105. if(length == 0xFFFFFFFF)
  106. return 0xFFFFFFFF;
  107. sealed_data=(uint8_t*) malloc(length); // 0x300); // TODO: Shouldn't it be malloc of length?
  108. printf("length: %d\n", length); fflush(stdout);
  109. Decryptor_create_and_seal_long_term_signing_key_pair_wrapper(e2_enclave_id, &ret_status, &length, sealed_data);
  110. if(ret_status != SGX_SUCCESS)
  111. {
  112. printf("create_and_seal called returned an error: %x", ret_status);
  113. free(sealed_data);
  114. return 0xFFFFFFFF;
  115. }
  116. printf("It returned sgx_success\n"); fflush(stdout);
  117. for(counter=0; counter<length; counter++)
  118. printf("%02x ", sealed_data[counter]);
  119. ret_status = write_to_fd(fd, sealed_data, &length);
  120. free(sealed_data);
  121. return ret_status;
  122. }
  123. int main(__attribute__((unused)) int argc, __attribute__((unused)) char* argv[])
  124. {
  125. uint32_t ret_status;
  126. sgx_status_t status;
  127. // For sgx setup
  128. int launch_token_updated;
  129. sgx_launch_token_t launch_token;
  130. sgx_ec256_public_t pub_key; uint32_t counter;
  131. size_t sealed_msg_length_in_file;
  132. status = sgx_create_enclave(Decryptor_PATH, SGX_DEBUG_FLAG, &launch_token, &launch_token_updated, &e2_enclave_id, NULL);
  133. if(status != SGX_SUCCESS)
  134. {
  135. printf("\nLoad Enclave Failure");
  136. return -1;
  137. }
  138. printf("\nDecryptor - EnclaveID %" PRIx64, e2_enclave_id);
  139. fflush(stdout);
  140. int sealed_signing_key_fd = open("sealed_signing_key.txt", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
  141. if(sealed_signing_key_fd == -1)
  142. {
  143. perror("\nError in opening the file sealed_signing_key.txt - ");
  144. fflush(stderr);
  145. return 0xFFFFFFFF;
  146. }
  147. printf("\nSuccessfully opened a file to seal the signing key pair for the client.\n");
  148. fflush(stdout);
  149. struct stat st; ret_status = fstat(sealed_signing_key_fd, &st);
  150. if(ret_status != 0)
  151. {
  152. perror("error in finding the file size. - ");
  153. fflush(stderr);
  154. return 0xffffffff;
  155. }
  156. sealed_msg_length_in_file = st.st_size;
  157. if(sealed_msg_length_in_file == 0) //if(start == end && start != -1)
  158. // TODO: file is empty. create signing key pair.
  159. // int start = lseek(sealed_signing_key_fd, 0, SEEK_SET);
  160. { printf("Creating new keypair.\n"); fflush(stdout); ret_status = create_and_seal_signing_key_pair_to_disk(sealed_signing_key_fd); }
  161. else
  162. { printf("Unsealing keypair.\n"); fflush(stdout); ret_status = unseal_signing_key_pair_from_disk(sealed_signing_key_fd, sealed_msg_length_in_file); }
  163. if(ret_status != 0)
  164. {
  165. printf("Some error \n");
  166. // printf("\n %d error in generating the ecdsa signing key pair \n", errno);
  167. fflush(stdout);
  168. sgx_destroy_enclave(e2_enclave_id);
  169. return 0xFFFFFFFF;
  170. }
  171. close(sealed_signing_key_fd);
  172. int server_fd;
  173. ret_status = LocalAttestationUntrusted::prepare_local_attestation_as_responder_msg1(e2_enclave_id);
  174. if(ret_status !=0)
  175. {
  176. printf("Could not prepare_local_attestation_as_responder_msg1"); fflush(stdout); sgx_destroy_enclave(e2_enclave_id);
  177. return ret_status;
  178. }
  179. server_fd=LocalAttestationUntrusted::setup_socket_for_local_attestation_requests(3824)
  180. if(server_fd <=0)
  181. {
  182. printf("Error in setting up server socket."); fflush(stdout);
  183. sgx_destroy_enclave(e2_enclave_id);
  184. return server_fd;
  185. }
  186. printf("Successfully set up a socket to communicate with the Apache enclave.\n");
  187. fflush(stdout);
  188. // LA with the verifier
  189. ret_status = LocalAttestationUntrusted::local_attestation_as_responder_msg2_msg3(e2_enclave_id, server_fd);
  190. if(ret_status!=0)
  191. {
  192. printf("local attestation - with the verifier - did not successfully return: %x\n", ret_status); fflush(stdout);
  193. sgx_destroy_enclave(e2_enclave_id);
  194. return 0xFFFFFFFF;
  195. }
  196. ret_status = LocalAttestationUntrusted::post_local_attestation_with_verifier(e2_enclave_id, server_fd);
  197. if(ret_status!=0)
  198. {
  199. printf("post local attestation - with the verifier - did not successfully return: %x\n", ret_status); fflush(stdout);
  200. sgx_destroy_enclave(e2_enclave_id);
  201. return 0xFFFFFF01;
  202. }
  203. server_fd = LocalAttestationUntrusted::local_attestation_as_responder_msg2_msg3(e2_enclave_id,server_fd);
  204. if(ret_status<=0)
  205. {
  206. printf("local attestation - with the apache - did not successfully return: %x\n", ret_status); fflush(stdout);
  207. sgx_destroy_enclave(e2_enclave_id);
  208. return 0xFFFFFFFF;
  209. }
  210. ret_status = LocalAttestationUntrusted::post_local_attestation_with_apache(e2_enclave_id,server_fd);
  211. if(ret_status!=0)
  212. {
  213. printf("post local attestation - with the apache - did not successfully return: %x\n", ret_status); fflush(stdout);
  214. sgx_destroy_enclave(e2_enclave_id);
  215. return 0xFFFFFF01;
  216. }
  217. sgx_destroy_enclave(e2_enclave_id);
  218. return 0;
  219. }