App_old.txt 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  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. #define SGX_DH_MAC_SIZE 16
  32. #define SGX_TARGET_INFO_RESERVED1_BYTES 4
  33. #define SGX_TARGET_INFO_RESERVED2_BYTES 456
  34. #define SGX_ECP256_KEY_SIZE 32
  35. #define SGX_CPUSVN_SIZE 16
  36. #define SGX_REPORT_DATA_SIZE 64
  37. #define SGX_MAC_SIZE 16 /* Message Authentication Code - 16 bytes */
  38. #define SGX_KEYID_SIZE 32
  39. #define SGX_HASH_SIZE 32 /* SHA256 */
  40. #define SGX_REPORT_BODY_RESERVED1 28
  41. #define SGX_REPORT_BODY_RESERVED2 32
  42. #define SGX_REPORT_BODY_RESERVED3 96
  43. #define SGX_REPORT_BODY_RESERVED4 60
  44. // App.cpp : Defines the entry point for the console application.
  45. #include <stdio.h>
  46. #include <map>
  47. #include "../Decryptor/Decryptor_u.h"
  48. #include "sgx_eid.h"
  49. #include "sgx_urts.h"
  50. #define __STDC_FORMAT_MACROS
  51. #include <inttypes.h>
  52. #include<unistd.h>
  53. // For ecdsa signing key generation
  54. #include "sgx_tcrypto.h"
  55. // For socket programming
  56. #include <sys/socket.h>
  57. #include <stdlib.h>
  58. #include <netinet/in.h>
  59. #include <string.h>
  60. #include <errno.h>
  61. #include<unistd.h>
  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. using namespace google::protobuf::io;
  68. #define UNUSED(val) (void)(val)
  69. #define TCHAR char
  70. #define _TCHAR char
  71. #define _T(str) str
  72. #define scanf_s scanf
  73. #define _tmain main
  74. // 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.
  75. //extern std::map<sgx_enclave_id_t, uint32_t>g_enclave_id_map;
  76. sgx_enclave_id_t e2_enclave_id = 0;
  77. #define Decryptor_PATH "libDecryptor.so"
  78. int fit_32_into_uint8_t(google::protobuf::uint32 temp32, uint8_t* temp8)
  79. {
  80. if(temp32 > UINT8_MAX)
  81. return -1;
  82. else
  83. {
  84. // *temp8 = *(uint8_t*)&temp32; // Probably works irrespective of endianness but not sure.
  85. *temp8 = (uint8_t)temp32;
  86. return 0;
  87. }
  88. }
  89. int fit_32_into_uint16_t(google::protobuf::uint32 temp32, uint16_t* temp16)
  90. {
  91. if(temp32 > UINT16_MAX)
  92. return -1;
  93. else
  94. {
  95. // *temp8 = *(uint8_t*)&temp32; // Probably works irrespective of endianness but not sure.
  96. *temp16 = (uint16_t)temp32;
  97. return 0;
  98. }
  99. }
  100. // Sets up a socket to bind and listen to the given port. Returns FD of the socket on success, -1 on failure (and prints a msg to stdout with the errno)
  101. int set_up_socket(int port, sockaddr_in* address)
  102. {
  103. int server_fd = 0;
  104. // Creating socket file descriptor for listening for attestation requests.
  105. server_fd = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
  106. if (server_fd == -1)
  107. {
  108. printf("Error in creating a socket - %d", errno);
  109. return -1;
  110. }
  111. // Preparing the address struct for binding
  112. address->sin_family = AF_INET;
  113. address->sin_addr.s_addr = INADDR_ANY; // Todo: should this be localhost?
  114. address->sin_port = htons(port);
  115. // memset(address->sin_zero,0,sizeof(address->sin_zero));
  116. socklen_t addrlen = sizeof(*address);
  117. // Binding
  118. if (bind(server_fd, (sockaddr*)address, addrlen)<0)
  119. {
  120. printf("Error in binding %d - port was %d - ", errno, port);
  121. return -1;
  122. }
  123. // Listening
  124. if (listen(server_fd, 128) < 0)
  125. {
  126. printf("Error in listening %d", errno);
  127. return -1;
  128. }
  129. return server_fd;
  130. }
  131. void encode_ec256_public_key_to_protobuf(protobuf_sgx_ec256_public_t* protobuf_g_a , sgx_ec256_public_t* g_a)
  132. {
  133. int counter; google::protobuf::uint32 temp32;
  134. for(counter=0;counter<SGX_ECP256_KEY_SIZE;counter++)
  135. {
  136. temp32 = g_a->gx[counter];
  137. protobuf_g_a->add_gx(temp32);
  138. temp32 = g_a->gy[counter];
  139. protobuf_g_a->add_gy(temp32);
  140. }
  141. }
  142. int decode_ec256_public_key_from_protobuf(protobuf_sgx_ec256_public_t* protobuf_g_a , sgx_ec256_public_t* g_a)
  143. {
  144. printf("\n ec256 pub key\n");
  145. int counter; google::protobuf::uint32 temp32;
  146. for(counter=0;counter<SGX_ECP256_KEY_SIZE;counter++)
  147. {
  148. temp32 = protobuf_g_a->gx(counter);
  149. if(fit_32_into_uint8_t(temp32, &(g_a->gx[counter]))!=0)
  150. return -1;
  151. printf("%d ",g_a->gx[counter]);
  152. temp32 = protobuf_g_a->gy(counter);
  153. if(fit_32_into_uint8_t(temp32, &(g_a->gy[counter]))!=0)
  154. return -1;
  155. printf("%d ",g_a->gy[counter]);
  156. }
  157. return 0;
  158. }
  159. void encode_attributes_to_protobuf(protobuf_sgx_attributes_t* protobuf_attributes, sgx_attributes_t* attributes)
  160. {
  161. protobuf_attributes->set_flags(attributes->flags); // 64 bit
  162. protobuf_attributes->set_xfrm(attributes->xfrm); // 64 bit
  163. }
  164. int decode_attributes_from_protobuf(protobuf_sgx_attributes_t* protobuf_attributes, sgx_attributes_t* attributes)
  165. {
  166. attributes->flags = protobuf_attributes->flags();
  167. printf("\n flags %" PRIu64 " \n", attributes->flags);
  168. attributes->xfrm = protobuf_attributes->xfrm();
  169. printf("\n xfrm %" PRIu64 " \n", attributes->xfrm);
  170. return 0;
  171. }
  172. void encode_report_to_protobuf(protobuf_sgx_report_t* protobuf_report, sgx_report_t* report)
  173. {
  174. int counter; google::protobuf::uint32 temp32;
  175. for(counter=0;counter<SGX_KEYID_SIZE;counter++)
  176. {
  177. temp32=report->key_id.id[counter];
  178. protobuf_report->add_key_id(temp32);
  179. }
  180. for(counter=0;counter<SGX_MAC_SIZE;counter++)
  181. {
  182. temp32=report->mac[counter];
  183. protobuf_report->add_mac(temp32);
  184. }
  185. protobuf_report->mutable_body()->set_misc_select(report->body.misc_select); // 32 bit
  186. protobuf_report->mutable_body()->set_isv_svn(report->body.isv_svn); // 16 bit
  187. protobuf_report->mutable_body()->set_isv_prod_id(report->body.isv_prod_id); // 16 bit
  188. encode_attributes_to_protobuf(protobuf_report->mutable_body()->mutable_attributes(), &(report->body.attributes));
  189. for(counter=0;counter<SGX_CPUSVN_SIZE;counter++)
  190. {
  191. temp32=report->body.cpu_svn.svn[counter];
  192. protobuf_report->mutable_body()->add_cpu_svn(temp32);
  193. }
  194. for(counter=0;counter<SGX_REPORT_BODY_RESERVED1;counter++)
  195. {
  196. temp32=report->body.reserved1[counter]; // TODO: Could be optimized out - if these are determined to be 0s.
  197. protobuf_report->mutable_body()->add_reserved1(temp32);
  198. }
  199. for(counter=0;counter<SGX_REPORT_BODY_RESERVED2;counter++)
  200. {
  201. temp32=report->body.reserved2[counter]; // TODO: Could be optimized out - if these are determined to be 0s.
  202. protobuf_report->mutable_body()->add_reserved2(temp32);
  203. }
  204. for(counter=0;counter<SGX_REPORT_BODY_RESERVED3;counter++)
  205. {
  206. temp32=report->body.reserved3[counter]; // TODO: Could be optimized out - if these are determined to be 0s.
  207. protobuf_report->mutable_body()->add_reserved3(temp32);
  208. }
  209. for(counter=0;counter<SGX_REPORT_BODY_RESERVED4;counter++)
  210. {
  211. temp32=report->body.reserved4[counter]; // TODO: Could be optimized out - if these are determined to be 0s.
  212. protobuf_report->mutable_body()->add_reserved4(temp32);
  213. }
  214. for(counter=0;counter<SGX_HASH_SIZE;counter++)
  215. {
  216. temp32=report->body.mr_enclave.m[counter];
  217. protobuf_report->mutable_body()->add_mr_enclave(temp32);
  218. }
  219. for(counter=0;counter<SGX_HASH_SIZE;counter++)
  220. {
  221. temp32=report->body.mr_signer.m[counter];
  222. protobuf_report->mutable_body()->add_mr_signer(temp32);
  223. }
  224. for(counter=0;counter<SGX_REPORT_DATA_SIZE;counter++)
  225. {
  226. temp32=report->body.report_data.d[counter];
  227. protobuf_report->mutable_body()->add_report_data(temp32);
  228. }
  229. }
  230. int decode_report_from_protobuf(protobuf_sgx_report_t* protobuf_report, sgx_report_t* report)
  231. {
  232. int counter; google::protobuf::uint32 temp32;
  233. printf("\nreport body keyid\n");
  234. for(counter=0;counter<SGX_KEYID_SIZE;counter++)
  235. {
  236. temp32=protobuf_report->key_id(counter);
  237. if(fit_32_into_uint8_t(temp32, &(report->key_id.id[counter]))!=0)
  238. return -1;
  239. printf("%d ", report->key_id.id[counter]);
  240. }
  241. printf("\nreport mac\n");
  242. for(counter=0;counter<SGX_MAC_SIZE;counter++)
  243. {
  244. temp32=protobuf_report->mac(counter);
  245. if(fit_32_into_uint8_t(temp32, &(report->mac[counter]))!=0)
  246. return -1;
  247. printf("%d ", report->mac[counter]);
  248. }
  249. report->body.misc_select=protobuf_report->mutable_body()->misc_select(); // 32 bit
  250. temp32=protobuf_report->mutable_body()->isv_svn();
  251. if(fit_32_into_uint16_t(temp32, &(report->body.isv_svn))!=0)
  252. return -1;
  253. printf("\nmisc select %d \n", report->body.misc_select);
  254. temp32=protobuf_report->mutable_body()->isv_prod_id();
  255. if(fit_32_into_uint16_t(temp32, &(report->body.isv_prod_id))!=0)
  256. return -1;
  257. printf("\nprod id %d \n", report->body.isv_prod_id);
  258. decode_attributes_from_protobuf(protobuf_report->mutable_body()->mutable_attributes(), &(report->body.attributes));
  259. printf("\n cpu svn\n");
  260. for(counter=0;counter<SGX_CPUSVN_SIZE;counter++)
  261. {
  262. temp32=protobuf_report->mutable_body()->cpu_svn(counter);
  263. if(fit_32_into_uint8_t(temp32, &(report->body.cpu_svn.svn[counter]))!=0)
  264. return -1;
  265. printf("%d ", report->body.cpu_svn.svn[counter]);
  266. }
  267. printf("\n reserved1 \n");
  268. for(counter=0;counter<SGX_REPORT_BODY_RESERVED1;counter++)
  269. {
  270. temp32=protobuf_report->mutable_body()->reserved1(counter);
  271. if(fit_32_into_uint8_t(temp32, &(report->body.reserved1[counter]))!=0)
  272. return -1;
  273. printf("%d ", report->body.reserved1[counter]);
  274. }
  275. printf("\n reserved2 \n");
  276. for(counter=0;counter<SGX_REPORT_BODY_RESERVED2;counter++)
  277. {
  278. temp32=protobuf_report->mutable_body()->reserved2(counter);
  279. if(fit_32_into_uint8_t(temp32, &(report->body.reserved2[counter]))!=0)
  280. return -1;
  281. printf("%d ", report->body.reserved2[counter]);
  282. }
  283. printf("\n reserved3 \n");
  284. for(counter=0;counter<SGX_REPORT_BODY_RESERVED3;counter++)
  285. {
  286. temp32=protobuf_report->mutable_body()->reserved3(counter);
  287. if(fit_32_into_uint8_t(temp32, &(report->body.reserved3[counter]))!=0)
  288. return -1;
  289. printf("%d ", report->body.reserved3[counter]);
  290. }
  291. printf("\n reserved4 \n");
  292. for(counter=0;counter<SGX_REPORT_BODY_RESERVED4;counter++)
  293. {
  294. temp32=protobuf_report->mutable_body()->reserved4(counter);
  295. if(fit_32_into_uint8_t(temp32, &(report->body.reserved4[counter]))!=0)
  296. return -1;
  297. printf("%d ", report->body.reserved4[counter]);
  298. }
  299. printf("\n mrenclave \n");
  300. for(counter=0;counter<SGX_HASH_SIZE;counter++)
  301. {
  302. temp32=protobuf_report->mutable_body()->mr_enclave(counter);
  303. if(fit_32_into_uint8_t(temp32, &(report->body.mr_enclave.m[counter]))!=0)
  304. return -1;
  305. printf("%x ", report->body.mr_enclave.m[counter]);
  306. }
  307. printf("\n mrsigner \n");
  308. for(counter=0;counter<SGX_HASH_SIZE;counter++)
  309. {
  310. temp32=protobuf_report->mutable_body()->mr_signer(counter);
  311. if(fit_32_into_uint8_t(temp32, &(report->body.mr_signer.m[counter]))!=0)
  312. return -1;
  313. printf("%x ", report->body.mr_signer.m[counter]);
  314. }
  315. printf("\n report data\n");
  316. for(counter=0;counter<SGX_REPORT_DATA_SIZE;counter++)
  317. {
  318. temp32=protobuf_report->mutable_body()->report_data(counter);
  319. if(fit_32_into_uint8_t(temp32, &(report->body.report_data.d[counter]))!=0)
  320. return -1;
  321. printf("%d ", report->body.report_data.d[counter]);
  322. }
  323. return 0;
  324. }
  325. void encode_msg1_to_protobuf( protobuf_sgx_dh_msg1_t& protobuf_dhmsg1, sgx_dh_msg1_t* native_dhmsg1)
  326. {
  327. int counter; google::protobuf::uint32 temp32; // google::protobuf::uint64 temp64;
  328. encode_ec256_public_key_to_protobuf(protobuf_dhmsg1.mutable_g_a(), &(native_dhmsg1->g_a));
  329. for(counter=0;counter<SGX_HASH_SIZE;counter++)
  330. {
  331. temp32=native_dhmsg1->target.mr_enclave.m[counter];
  332. protobuf_dhmsg1.mutable_target()->add_mr_enclave(temp32);
  333. }
  334. for(counter=0;counter<SGX_TARGET_INFO_RESERVED1_BYTES;counter++)
  335. {
  336. temp32=native_dhmsg1->target.reserved1[counter];
  337. protobuf_dhmsg1.mutable_target()->add_reserved1(temp32);
  338. }
  339. for(counter=0;counter<SGX_TARGET_INFO_RESERVED2_BYTES;counter++)
  340. {
  341. temp32=native_dhmsg1->target.reserved2[counter];
  342. protobuf_dhmsg1.mutable_target()->add_reserved2(temp32);
  343. }
  344. encode_attributes_to_protobuf(protobuf_dhmsg1.mutable_target()->mutable_attributes(), &(native_dhmsg1->target.attributes));
  345. temp32=native_dhmsg1->target.misc_select ;
  346. protobuf_dhmsg1.mutable_target()->set_misc_select(temp32);
  347. }
  348. void encode_msg3_to_protobuf(protobuf_sgx_dh_msg3_t& protobuf_dhmsg3, sgx_dh_msg3_t* native_dhmsg3)
  349. {
  350. int counter; google::protobuf::uint32 temp32;
  351. for(counter=0;counter<SGX_DH_MAC_SIZE;counter++)
  352. {
  353. temp32=native_dhmsg3->cmac[counter];
  354. protobuf_dhmsg3.add_cmac(temp32);
  355. }
  356. encode_report_to_protobuf(protobuf_dhmsg3.mutable_msg3_body()->mutable_report(), &(native_dhmsg3->msg3_body.report));
  357. int max_counter=native_dhmsg3->msg3_body.additional_prop_length;
  358. unsigned char*temp;
  359. for(counter=0,temp=native_dhmsg3->msg3_body.additional_prop;counter<max_counter;counter++,temp++)
  360. {
  361. protobuf_dhmsg3.mutable_msg3_body()->add_additional_prop(*temp);
  362. }
  363. }
  364. int decode_msg2_from_protobuf(protobuf_sgx_dh_msg2_t& protobuf_dhmsg2, sgx_dh_msg2_t* native_dhmsg2)
  365. {
  366. int counter; google::protobuf::uint32 temp32; //google::protobuf::uint64 temp64;
  367. printf("\ncmac\n");
  368. for(counter=0;counter<SGX_DH_MAC_SIZE;counter++)
  369. {
  370. temp32=protobuf_dhmsg2.cmac(counter);
  371. if(fit_32_into_uint8_t(temp32, &(native_dhmsg2->cmac[counter]))!=0)
  372. return -1;
  373. printf("%d ",native_dhmsg2->cmac[counter]);
  374. }
  375. if(decode_ec256_public_key_from_protobuf(protobuf_dhmsg2.mutable_g_b(), &(native_dhmsg2->g_b)) !=0)
  376. return -1;
  377. if(decode_report_from_protobuf(protobuf_dhmsg2.mutable_report(), &(native_dhmsg2->report)) !=0)
  378. return -1;
  379. return 0;
  380. }
  381. int print_initialized_msg1( protobuf_sgx_dh_msg1_t& protobuf_dhmsg1, sgx_dh_msg1_t* native_dhmsg1)
  382. {
  383. int counter;
  384. printf("gx\n");
  385. for(counter=0;counter<SGX_ECP256_KEY_SIZE;counter++)
  386. {
  387. printf("%d ", protobuf_dhmsg1.g_a().gx(counter));
  388. printf("%d ", native_dhmsg1->g_a.gx[counter]);
  389. }
  390. printf("\ngy\n");
  391. for(counter=0;counter<SGX_ECP256_KEY_SIZE;counter++)
  392. {
  393. printf("%d ", protobuf_dhmsg1.g_a().gy(counter));
  394. printf("%d ", native_dhmsg1->g_a.gy[counter]);
  395. }
  396. printf("\nmrenclave in target\n");
  397. for(counter=0;counter<SGX_HASH_SIZE;counter++)
  398. {
  399. printf("%" PRIu32 " ", protobuf_dhmsg1.target().mr_enclave(counter));
  400. printf("%d ", native_dhmsg1->target.mr_enclave.m[counter]);
  401. }
  402. printf("\nreserved1 in target\n");
  403. for(counter=0;counter<SGX_TARGET_INFO_RESERVED1_BYTES;counter++)
  404. {
  405. printf("%" PRIu32 " ", protobuf_dhmsg1.target().reserved1(counter));
  406. printf("%d ", native_dhmsg1->target.reserved1[counter]);
  407. }
  408. printf("\nreserved2 in target\n");
  409. for(counter=0;counter<SGX_TARGET_INFO_RESERVED2_BYTES;counter++)
  410. {
  411. printf("%" PRIu32 " ", protobuf_dhmsg1.target().reserved2(counter));
  412. printf("%d ", native_dhmsg1->target.reserved2[counter]);
  413. }
  414. printf("\n %" PRIu64 "\n", native_dhmsg1->target.attributes.flags);
  415. printf("\n %" PRIu64 "\n", protobuf_dhmsg1.target().attributes().flags());
  416. printf("\n %" PRIu64 "\n", native_dhmsg1->target.attributes.xfrm);
  417. printf("\n %" PRIu64 "\n", protobuf_dhmsg1.target().attributes().xfrm());
  418. printf("\n %" PRIu32 "\n", native_dhmsg1->target.misc_select);
  419. printf("\n %" PRIu32 "\n", protobuf_dhmsg1.target().misc_select());
  420. return 0;
  421. }
  422. int read_protobuf_msg_from_fd(int accept_fd, google::protobuf::MessageLite& message)
  423. {
  424. ZeroCopyInputStream* raw_input;
  425. CodedInputStream* coded_input;
  426. uint32_t size;
  427. CodedInputStream::Limit limit;
  428. raw_input = new FileInputStream(accept_fd);
  429. coded_input = new CodedInputStream(raw_input);
  430. if(!coded_input->ReadVarint32(&size))
  431. {
  432. printf("Error in reading size of msg");
  433. fflush(stdout);
  434. return -1;
  435. }
  436. //printf("size of msg was read to be %" PRIu32 " \n", size);
  437. fflush(stdout);
  438. limit = coded_input->PushLimit(size);
  439. if(!message.ParseFromCodedStream(coded_input))
  440. {
  441. printf("Error in parsing msg");
  442. fflush(stdout);
  443. return -1;
  444. }
  445. coded_input->PopLimit(limit);
  446. return 0;
  447. }
  448. int write_protobuf_msg_to_fd(int accept_fd, google::protobuf::MessageLite& message)
  449. {
  450. ZeroCopyOutputStream* raw_output = new FileOutputStream(accept_fd);
  451. CodedOutputStream* coded_output = new CodedOutputStream(raw_output);
  452. coded_output->WriteVarint32(message.ByteSize());
  453. if(!message.SerializeToCodedStream(coded_output))
  454. {
  455. printf("SerializeToCodedStream failed");
  456. fflush(stdout);
  457. return -1;
  458. }
  459. // As per this - https://stackoverflow.com/questions/22881876/protocol-buffers-how-to-serialize-and-deserialize-multiple-messages-into-a-file?noredirect=1&lq=1
  460. // TODO: There may be a better way to do this - 1) this happens with every accept now and 2) make it happen on the stack vs heap - destructor will be called on return from this function (main) and the items will then be written out. (We probably don't want that, actually)
  461. delete coded_output;
  462. delete raw_output;
  463. fflush(stdout);
  464. return 0;
  465. }
  466. int _tmain(int argc, _TCHAR* argv[])
  467. {
  468. uint32_t ret_status;
  469. sgx_status_t status;
  470. // For sgx setup
  471. int launch_token_updated;
  472. sgx_launch_token_t launch_token;
  473. // declare msg1, msg2, msg3 protobuf objects and native SGX structs
  474. protobuf_sgx_dh_msg1_t protobuf_msg1;
  475. protobuf_sgx_dh_msg2_t protobuf_msg2;
  476. protobuf_sgx_dh_msg3_t protobuf_msg3;
  477. // For sgx - ecalls and storing msgs rcvd from pipes for processing.
  478. uint32_t session_id;
  479. sgx_dh_msg1_t dh_msg1; //Diffie-Hellman Message 1
  480. sgx_dh_msg2_t dh_msg2; //Diffie-Hellman Message 2
  481. sgx_dh_msg3_t dh_msg3; //Diffie-Hellman Message 3
  482. sgx_key_128bit_t dh_aek; // Session Key
  483. // initializing native sgx structs
  484. memset(&dh_aek,0, sizeof(sgx_key_128bit_t));
  485. memset(&dh_msg1, 0, sizeof(sgx_dh_msg1_t));
  486. memset(&dh_msg2, 0, sizeof(sgx_dh_msg2_t));
  487. memset(&dh_msg3, 0, sizeof(sgx_dh_msg3_t));
  488. // For socket to listen to the Apache enclave.
  489. int apache_port = 3825; int server_fd=0; int accept_fd = 0;
  490. struct sockaddr_in own_addr;
  491. struct sockaddr_storage apache_addr; socklen_t apache_addr_size = sizeof(apache_addr);
  492. int counter;
  493. UNUSED(argc);
  494. UNUSED(argv);
  495. server_fd=set_up_socket(apache_port, &own_addr);
  496. if(server_fd==-1)
  497. return -1;
  498. printf("Successfully set up a socket to communicate with the Apache enclave.");
  499. fflush(stdout);
  500. status = sgx_create_enclave(Decryptor_PATH, SGX_DEBUG_FLAG, &launch_token, &launch_token_updated, &e2_enclave_id, NULL);
  501. if(status != SGX_SUCCESS)
  502. {
  503. printf("\nLoad Enclave Failure");
  504. close(server_fd);
  505. return -1;
  506. }
  507. printf("\nDecryptor - EnclaveID %" PRIx64, e2_enclave_id);
  508. fflush(stdout);
  509. // Generating a signing ECDSA key to sign the encryption key.
  510. sgx_ec256_public_t pub_key;
  511. Decryptor_create_ecdsa_signing_key_pair(e2_enclave_id, &ret_status, &pub_key);
  512. if(ret_status != SGX_SUCCESS)
  513. {
  514. printf("\n error in generating the ecdsa signing key pair \n");
  515. fflush(stdout);
  516. return 0xFFFFFFFF;
  517. }
  518. printf("\n Generated the ecdsa key pair successfully - gx, gy\n");
  519. fflush(stdout);
  520. for(counter=0; counter<SGX_ECP256_KEY_SIZE; counter++)
  521. {
  522. printf("%x ", pub_key.gx[counter]);
  523. }
  524. printf("\n"); fflush(stdout);
  525. for(counter=0; counter<SGX_ECP256_KEY_SIZE; counter++)
  526. {
  527. printf("%x ", pub_key.gy[counter]);
  528. }
  529. printf("\n"); fflush(stdout);
  530. Decryptor_session_request(e2_enclave_id, &ret_status, &dh_msg1, &session_id); // TODO: Check Return status
  531. encode_msg1_to_protobuf(protobuf_msg1, &dh_msg1);
  532. print_initialized_msg1(protobuf_msg1, &dh_msg1);
  533. printf("Done initialization");
  534. fflush(stdout);
  535. //
  536. int no_of_msgs_xchanged=0;
  537. // do
  538. // {
  539. // accept incoming connection
  540. accept_fd = accept(server_fd, (struct sockaddr *)&apache_addr,&apache_addr_size);
  541. if (accept_fd <0)
  542. {
  543. printf("Error in accepting %d", errno);
  544. return -1;
  545. }
  546. printf("Accepted the fd");
  547. do
  548. {
  549. // read msg(none,2) and then write(1,3)
  550. if(no_of_msgs_xchanged==0){
  551. // call SerializeToFileDescriptor on msg1 object, accept_fd.
  552. if(write_protobuf_msg_to_fd(accept_fd, protobuf_msg1)!=0)
  553. return -1;
  554. }
  555. else
  556. {
  557. printf("about to read msg2\n");
  558. fflush(stdout);
  559. // read msg2 -
  560. if(read_protobuf_msg_from_fd(accept_fd, protobuf_msg2)!=0)
  561. return -1;
  562. if(decode_msg2_from_protobuf(protobuf_msg2, &dh_msg2)!=0)
  563. return -1;
  564. printf("Done decoding msg2\n");
  565. // TODO: call fn to generate msg3 for the responder.
  566. Decryptor_exchange_report(e2_enclave_id, &ret_status, &dh_msg2, &dh_msg3, &session_id);
  567. if(ret_status!=SGX_SUCCESS)
  568. {
  569. fflush(stdout);
  570. printf("exchange report failed:%x\n", ret_status);
  571. fflush(stdout);
  572. return -1;
  573. }
  574. printf("return status %d", ret_status);
  575. fflush(stdout);
  576. // convert msg3 sgx_dh_msg3_t object to a protobuf msg3 object.
  577. encode_msg3_to_protobuf(protobuf_msg3, &dh_msg3);
  578. printf("about to write msg3\n");
  579. fflush(stdout);
  580. // write msg3 -
  581. if(write_protobuf_msg_to_fd(accept_fd, protobuf_msg3)!=0)
  582. return -1;
  583. no_of_msgs_xchanged+=1;
  584. }
  585. no_of_msgs_xchanged++;
  586. }
  587. while(no_of_msgs_xchanged!=3);
  588. printf("Out of while loop\n");
  589. fflush(stdout);
  590. // TODO: Continue with other msgs.
  591. // sleep(500);
  592. sgx_destroy_enclave(e2_enclave_id);
  593. return 0;
  594. }
  595. /*
  596. // Reads from a server fd that is set up successfully for accept, into a buffer buf and upto the length len.
  597. // Returns the number of characters read on success and -1 on error. Also, on error, prints the errno for the accept system call.
  598. int accept_and_read(int server_fd, char* buf, int len)
  599. {
  600. int initialize_msg1( protobuf_sgx_dh_msg1_t& protobuf_dhmsg1, sgx_dh_msg1_t* native_dhmsg1)
  601. int accept_fd=0;
  602. // accept connections and then forward them to the main enclave.
  603. if ((accept_fd = accept(server_fd, (struct sockaddr *)&address,
  604. (socklen_t*)&addrlen))<0)
  605. {
  606. printf("Error in accepting %d", errno);
  607. return -1;
  608. }
  609. valread = read(accept_fd, buf, len);
  610. if(valread == -1)
  611. printf("Error in reading %d", errno);
  612. return valread;
  613. }
  614. */