App.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. /*
  32. #define SGX_DH_MAC_SIZE 16
  33. #define SGX_TARGET_INFO_RESERVED1_BYTES 4
  34. #define SGX_TARGET_INFO_RESERVED2_BYTES 456
  35. #define SGX_ECP256_KEY_SIZE 32
  36. #define SGX_CPUSVN_SIZE 16
  37. #define SGX_REPORT_DATA_SIZE 64
  38. #define SGX_MAC_SIZE 16 // Message Authentication Code - 16 bytes
  39. #define SGX_KEYID_SIZE 32
  40. #define SGX_HASH_SIZE 32 // SHA256
  41. #define SGX_REPORT_BODY_RESERVED1 28
  42. #define SGX_REPORT_BODY_RESERVED2 32
  43. #define SGX_REPORT_BODY_RESERVED3 96
  44. #define SGX_REPORT_BODY_RESERVED4 60
  45. */
  46. // App.cpp : Defines the entry point for the console application.
  47. #include <stdio.h>
  48. #include <map>
  49. #include "../Decryptor/Decryptor_u.h"
  50. #include "sgx_eid.h"
  51. #include "sgx_urts.h"
  52. #define __STDC_FORMAT_MACROS
  53. #include <inttypes.h>
  54. #include<unistd.h>
  55. // for sealing - sgx_calc_sealed_data_size
  56. #include "sgx_tseal.h"
  57. // For reading from/writing to file -sealing.
  58. #include <fcntl.h>
  59. #include <sys/types.h>
  60. #include <sys/stat.h>
  61. /*
  62. // For google proto buffers
  63. #include "dhmsgs.pb.h"
  64. #include <inttypes.h>
  65. #include <google/protobuf/io/coded_stream.h>
  66. #include <google/protobuf/io/zero_copy_stream_impl.h>
  67. #include "SgxProtobufLATransforms_initiator.h"
  68. using namespace google::protobuf::io;
  69. */
  70. #include "systemLA.h"
  71. //#define UNUSED(val) (void)(val)
  72. #define TCHAR char
  73. #define _TCHAR char
  74. #define _T(str) str
  75. #define scanf_s scanf
  76. // 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.
  77. //extern std::map<sgx_enclave_id_t, uint32_t>g_enclave_id_map;
  78. sgx_enclave_id_t e2_enclave_id = 0;
  79. #define Decryptor_PATH "libDecryptor.so"
  80. //////////////////////////////////////////////////
  81. #include <stdio.h>
  82. uint32_t write_to_fd(int fd, uint8_t* msg, uint32_t* expected_msg_length)
  83. {
  84. lseek(fd, 0, SEEK_SET);
  85. ssize_t bytes_written;
  86. bytes_written = write(fd, msg, *expected_msg_length);
  87. if(bytes_written <= 0)
  88. return 0xFFFFFFFF;
  89. fsync(fd);
  90. *expected_msg_length = bytes_written;
  91. return 0;
  92. }
  93. uint32_t read_from_fd(int fd, uint8_t* msg, size_t* expected_msg_length)
  94. {
  95. ssize_t bytes_read;
  96. lseek(fd, 0, SEEK_SET);
  97. bytes_read = read(fd, msg, *expected_msg_length);
  98. if(bytes_read <= 0)
  99. return 0xFFFFFFFF;
  100. *expected_msg_length = bytes_read;
  101. return 0;
  102. }
  103. uint32_t unseal_signing_key_pair_from_disk(int fd, __attribute__((unused)) sgx_ec256_public_t* pub_key, size_t* actual_sealed_msg_length)
  104. {
  105. uint32_t ret_status;
  106. uint8_t* sgx_sealed_msg;
  107. printf("expected read 0x%ld\n", *actual_sealed_msg_length);
  108. sgx_sealed_msg = (uint8_t*) malloc(0x300); // malloc(*actual_sealed_msg_length); (0x300 for EDL)
  109. ret_status = read_from_fd(fd, sgx_sealed_msg, actual_sealed_msg_length);
  110. if(ret_status != 0)
  111. {
  112. free(sgx_sealed_msg);
  113. return 0xFFFFFFFF;
  114. }
  115. printf("actual read 0x%ld and ret_status 0x%x\n", *actual_sealed_msg_length, ret_status); fflush(stdout);
  116. size_t counter;
  117. for(counter=0;counter<*actual_sealed_msg_length;counter++)
  118. printf("%x ", *(sgx_sealed_msg+counter));
  119. printf("\n"); fflush(stdout);
  120. Decryptor_unseal_and_restore_sealed_signing_key_pair(e2_enclave_id, &ret_status, pub_key, sgx_sealed_msg, actual_sealed_msg_length);
  121. free(sgx_sealed_msg);
  122. return ret_status;
  123. }
  124. uint32_t create_and_seal_signing_key_pair_to_disk( __attribute__((unused)) int fd, __attribute__((unused)) sgx_ec256_public_t* pub_key)
  125. {
  126. uint32_t ret_status;
  127. // Generating a signing ECDSA key to sign the encryption key.
  128. uint32_t length;
  129. 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);
  130. if(length == 0xFFFFFFFF)
  131. return 0xFFFFFFFF;
  132. printf("0x%x input msg, 0x%x bytes for sealed msg in parameter value\n", 3*SGX_ECP256_KEY_SIZE, length); fflush(stdout);
  133. uint8_t* sealed_data=(uint8_t*) malloc(0x300);
  134. printf("Made call to sgx_calc_sealed_data_size\n"); fflush(stdout);
  135. Decryptor_create_and_seal_ecdsa_signing_key_pair(e2_enclave_id, &ret_status, pub_key, &length, sealed_data);
  136. if(ret_status != SGX_SUCCESS)
  137. {
  138. printf("create_and_seal called returned an error: %x", ret_status);
  139. free(sealed_data);
  140. return 0xFFFFFFFF;
  141. }
  142. printf("It returned sgx_success\n"); fflush(stdout); // *actual_sealed_msg_length=length;
  143. ret_status = write_to_fd(fd, sealed_data, &length);
  144. free(sealed_data);
  145. // if(ret_status > 0)
  146. // *actual_sealed_msg_length = ret_status;
  147. // return 0;
  148. return ret_status;
  149. }
  150. int main(__attribute__((unused)) int argc, __attribute__((unused)) char* argv[])
  151. {
  152. uint32_t ret_status;
  153. sgx_status_t status;
  154. // For sgx setup
  155. int launch_token_updated;
  156. sgx_launch_token_t launch_token;
  157. // uint8_t* pub_key = (uint8_t*) malloc(2*SGX_ECP256_KEY_SIZE);
  158. sgx_ec256_public_t pub_key; uint32_t counter;
  159. size_t sealed_msg_length_in_file;
  160. status = sgx_create_enclave(Decryptor_PATH, SGX_DEBUG_FLAG, &launch_token, &launch_token_updated, &e2_enclave_id, NULL);
  161. if(status != SGX_SUCCESS)
  162. {
  163. printf("\nLoad Enclave Failure");
  164. return -1;
  165. }
  166. printf("\nDecryptor - EnclaveID %" PRIx64, e2_enclave_id);
  167. fflush(stdout);
  168. int sealed_signing_key_fd = open("sealed_signing_key.txt", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
  169. if(sealed_signing_key_fd == -1)
  170. {
  171. perror("\nError in opening the file sealed_signing_key.txt - ");
  172. fflush(stderr);
  173. return 0xFFFFFFFF;
  174. }
  175. printf("\nSuccessfully opened a file to seal the signing key pair for the client.\n");
  176. fflush(stdout);
  177. // int start = lseek(sealed_signing_key_fd, 0, SEEK_SET);
  178. // int end = lseek(sealed_signing_key_fd, 0, SEEK_END);
  179. struct stat st; ret_status = fstat(sealed_signing_key_fd, &st);
  180. //sealed_msg_length_in_file = st.st_size;
  181. if(ret_status != 0)
  182. {
  183. perror("error in finding the file size. - ");
  184. fflush(stderr);
  185. return 0xffffffff;
  186. }
  187. sealed_msg_length_in_file = st.st_size;
  188. if(sealed_msg_length_in_file == 0) //if(start == end && start != -1)
  189. {
  190. // TODO: file is empty. create signing key pair.
  191. // int start = lseek(sealed_signing_key_fd, 0, SEEK_SET);
  192. ret_status = create_and_seal_signing_key_pair_to_disk(sealed_signing_key_fd, &pub_key);
  193. if(ret_status != 0)
  194. {
  195. printf("\n error in generating the ecdsa signing key pair \n");
  196. fflush(stdout); sgx_destroy_enclave(e2_enclave_id);
  197. return 0xFFFFFFFF;
  198. }
  199. fflush(stdout);
  200. printf("\n Generated the ecdsa key pair successfully - gx, gy\n");
  201. for(counter=0;counter<32;counter++)
  202. printf("0x%x ",pub_key.gx[counter]);
  203. printf("\n");
  204. for(counter=0;counter<32;counter++)
  205. printf("0x%x ",pub_key.gy[counter]);
  206. printf("\n");
  207. fflush(stdout);
  208. // fflush(stdout);
  209. }
  210. else {
  211. // start = lseek(sealed_signing_key_fd, 0, SEEK_CUR);
  212. // if(actual_sealed_msg_length == 0)
  213. // actual_sealed_msg_length = size; // - start;
  214. ret_status = unseal_signing_key_pair_from_disk(sealed_signing_key_fd, &pub_key, &sealed_msg_length_in_file);
  215. if(ret_status != SGX_SUCCESS)
  216. {
  217. printf("\n error in unsealing the ecdsa signing key pair:%d \n", ret_status);
  218. fflush(stdout); sgx_destroy_enclave(e2_enclave_id);
  219. return 0xFFFFFFFF;
  220. }
  221. printf("\n Recovered the ecdsa key pair successfully - gx, gy\n");
  222. for(counter=0;counter<32;counter++)
  223. printf("0x%x ",pub_key.gx[counter]);
  224. printf("\n");
  225. for(counter=0;counter<32;counter++)
  226. printf("0x%x ",pub_key.gy[counter]);
  227. printf("\n");
  228. fflush(stdout);
  229. }
  230. close(sealed_signing_key_fd);
  231. ret_status=local_attestation_initiator(3825, e2_enclave_id, NULL);
  232. if(ret_status!=0)
  233. {
  234. printf("local attestation did not successfully return: %x\n", ret_status); fflush(stdout); sgx_destroy_enclave(e2_enclave_id);
  235. return 0xFFFFFFFF;
  236. }
  237. /* sgx_ec256_public_t short_term_pub_key;
  238. sgx_ec256_signature_t generated_signature;
  239. Decryptor_create_and_sign_client_side_pub_key(e2_enclave_id, &ret_status,&short_term_pub_key, &generated_signature);
  240. if(ret_status != SGX_SUCCESS)
  241. {
  242. printf("Could not generate or sign another keypair for client-side, error:%x.\n", ret_status); fflush(stdout);
  243. return 0xFFFFFFFF;
  244. }
  245. printf("Generated signature and pub key.\n");fflush(stdout);
  246. for(counter=0; counter<SGX_ECP256_KEY_SIZE; counter++)
  247. printf("0x%x ", short_term_pub_key.gx[counter]); printf("\n"); fflush(stdout);
  248. for(counter=0;counter<SGX_NISTP_ECP256_KEY_SIZE ; counter++)
  249. {
  250. printf("0x%x", generated_signature.x[counter]);
  251. printf("0x%x", generated_signature.y[counter]);
  252. }
  253. printf("\n"); fflush(stdout);
  254. */
  255. // TODO: Continue with other msgs - send sign(enc | verifier mr_enclave)
  256. sgx_destroy_enclave(e2_enclave_id);
  257. return 0;
  258. }