App.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  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 sealing - sgx_calc_sealed_data_size
  54. #include "sgx_tseal.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 reading from/writing to file -sealing.
  63. #include <fcntl.h>
  64. #include <sys/types.h>
  65. #include <sys/stat.h>
  66. // For google proto buffers
  67. #include "dhmsgs.pb.h"
  68. #include <inttypes.h>
  69. #include <google/protobuf/io/coded_stream.h>
  70. #include <google/protobuf/io/zero_copy_stream_impl.h>
  71. #include "SgxProtobufLATransforms_initiator.h"
  72. using namespace google::protobuf::io;
  73. //#define UNUSED(val) (void)(val)
  74. #define TCHAR char
  75. #define _TCHAR char
  76. #define _T(str) str
  77. #define scanf_s scanf
  78. // 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.
  79. //extern std::map<sgx_enclave_id_t, uint32_t>g_enclave_id_map;
  80. sgx_enclave_id_t e2_enclave_id = 0;
  81. #define Decryptor_PATH "libDecryptor.so"
  82. //////////////////////////////////////////////////
  83. // For google proto buffers
  84. #include "dhmsgs.pb.h"
  85. #include <inttypes.h>
  86. #include <google/protobuf/io/coded_stream.h>
  87. #include <google/protobuf/io/zero_copy_stream_impl.h>
  88. using namespace google::protobuf::io;
  89. #include <stdio.h>
  90. //#include "sgx_eid.h"
  91. //#include "sgx_urts.h"
  92. int fit_32_into_uint8_t(google::protobuf::uint32 temp32, uint8_t* temp8)
  93. {
  94. if(temp32 > UINT8_MAX)
  95. return -1;
  96. else
  97. {
  98. // *temp8 = *(uint8_t*)&temp32; // Probably works irrespective of endianness but not sure.
  99. *temp8 = (uint8_t)temp32;
  100. return 0;
  101. }
  102. }
  103. int fit_32_into_uint16_t(google::protobuf::uint32 temp32, uint16_t* temp16)
  104. {
  105. if(temp32 > UINT16_MAX)
  106. return -1;
  107. else
  108. {
  109. // *temp8 = *(uint8_t*)&temp32; // Probably works irrespective of endianness but not sure.
  110. *temp16 = (uint16_t)temp32;
  111. return 0;
  112. }
  113. }
  114. void encode_ec256_public_key_to_protobuf(protobuf_sgx_ec256_public_t* protobuf_g_a , sgx_ec256_public_t* g_a)
  115. {
  116. int counter; google::protobuf::uint32 temp32;
  117. for(counter=0;counter<SGX_ECP256_KEY_SIZE;counter++)
  118. {
  119. temp32 = g_a->gx[counter];
  120. protobuf_g_a->add_gx(temp32);
  121. temp32 = g_a->gy[counter];
  122. protobuf_g_a->add_gy(temp32);
  123. }
  124. }
  125. int decode_ec256_public_key_from_protobuf(protobuf_sgx_ec256_public_t* protobuf_g_a , sgx_ec256_public_t* g_a)
  126. {
  127. printf("\n ec256 pub key\n");
  128. int counter; google::protobuf::uint32 temp32;
  129. for(counter=0;counter<SGX_ECP256_KEY_SIZE;counter++)
  130. {
  131. temp32 = protobuf_g_a->gx(counter);
  132. if(fit_32_into_uint8_t(temp32, &(g_a->gx[counter]))!=0)
  133. return -1;
  134. printf("%d ",g_a->gx[counter]);
  135. temp32 = protobuf_g_a->gy(counter);
  136. if(fit_32_into_uint8_t(temp32, &(g_a->gy[counter]))!=0)
  137. return -1;
  138. printf("%d ",g_a->gy[counter]);
  139. }
  140. return 0;
  141. }
  142. void encode_attributes_to_protobuf(protobuf_sgx_attributes_t* protobuf_attributes, sgx_attributes_t* attributes)
  143. {
  144. protobuf_attributes->set_flags(attributes->flags); // 64 bit
  145. protobuf_attributes->set_xfrm(attributes->xfrm); // 64 bit
  146. }
  147. int decode_attributes_from_protobuf(protobuf_sgx_attributes_t* protobuf_attributes, sgx_attributes_t* attributes)
  148. {
  149. attributes->flags = protobuf_attributes->flags();
  150. printf("\n flags %" PRIu64 " \n", attributes->flags);
  151. attributes->xfrm = protobuf_attributes->xfrm();
  152. printf("\n xfrm %" PRIu64 " \n", attributes->xfrm);
  153. return 0;
  154. }
  155. void encode_report_to_protobuf(protobuf_sgx_report_t* protobuf_report, sgx_report_t* report)
  156. {
  157. int counter; google::protobuf::uint32 temp32;
  158. for(counter=0;counter<SGX_KEYID_SIZE;counter++)
  159. {
  160. temp32=report->key_id.id[counter];
  161. protobuf_report->add_key_id(temp32);
  162. }
  163. for(counter=0;counter<SGX_MAC_SIZE;counter++)
  164. {
  165. temp32=report->mac[counter];
  166. protobuf_report->add_mac(temp32);
  167. }
  168. protobuf_report->mutable_body()->set_misc_select(report->body.misc_select); // 32 bit
  169. protobuf_report->mutable_body()->set_isv_svn(report->body.isv_svn); // 16 bit
  170. protobuf_report->mutable_body()->set_isv_prod_id(report->body.isv_prod_id); // 16 bit
  171. encode_attributes_to_protobuf(protobuf_report->mutable_body()->mutable_attributes(), &(report->body.attributes));
  172. for(counter=0;counter<SGX_CPUSVN_SIZE;counter++)
  173. {
  174. temp32=report->body.cpu_svn.svn[counter];
  175. protobuf_report->mutable_body()->add_cpu_svn(temp32);
  176. }
  177. for(counter=0;counter<SGX_REPORT_BODY_RESERVED1;counter++)
  178. {
  179. temp32=report->body.reserved1[counter]; // TODO: Could be optimized out - if these are determined to be 0s.
  180. protobuf_report->mutable_body()->add_reserved1(temp32);
  181. }
  182. for(counter=0;counter<SGX_REPORT_BODY_RESERVED2;counter++)
  183. {
  184. temp32=report->body.reserved2[counter]; // TODO: Could be optimized out - if these are determined to be 0s.
  185. protobuf_report->mutable_body()->add_reserved2(temp32);
  186. }
  187. for(counter=0;counter<SGX_REPORT_BODY_RESERVED3;counter++)
  188. {
  189. temp32=report->body.reserved3[counter]; // TODO: Could be optimized out - if these are determined to be 0s.
  190. protobuf_report->mutable_body()->add_reserved3(temp32);
  191. }
  192. for(counter=0;counter<SGX_REPORT_BODY_RESERVED4;counter++)
  193. {
  194. temp32=report->body.reserved4[counter]; // TODO: Could be optimized out - if these are determined to be 0s.
  195. protobuf_report->mutable_body()->add_reserved4(temp32);
  196. }
  197. for(counter=0;counter<SGX_HASH_SIZE;counter++)
  198. {
  199. temp32=report->body.mr_enclave.m[counter];
  200. protobuf_report->mutable_body()->add_mr_enclave(temp32);
  201. }
  202. for(counter=0;counter<SGX_HASH_SIZE;counter++)
  203. {
  204. temp32=report->body.mr_signer.m[counter];
  205. protobuf_report->mutable_body()->add_mr_signer(temp32);
  206. }
  207. for(counter=0;counter<SGX_REPORT_DATA_SIZE;counter++)
  208. {
  209. temp32=report->body.report_data.d[counter];
  210. protobuf_report->mutable_body()->add_report_data(temp32);
  211. }
  212. }
  213. int decode_report_from_protobuf(protobuf_sgx_report_t* protobuf_report, sgx_report_t* report)
  214. {
  215. int counter; google::protobuf::uint32 temp32;
  216. printf("\nreport body keyid\n");
  217. for(counter=0;counter<SGX_KEYID_SIZE;counter++)
  218. {
  219. temp32=protobuf_report->key_id(counter);
  220. if(fit_32_into_uint8_t(temp32, &(report->key_id.id[counter]))!=0)
  221. return -1;
  222. printf("%d ", report->key_id.id[counter]);
  223. }
  224. printf("\nreport mac\n");
  225. for(counter=0;counter<SGX_MAC_SIZE;counter++)
  226. {
  227. temp32=protobuf_report->mac(counter);
  228. if(fit_32_into_uint8_t(temp32, &(report->mac[counter]))!=0)
  229. return -1;
  230. printf("%d ", report->mac[counter]);
  231. }
  232. report->body.misc_select=protobuf_report->mutable_body()->misc_select(); // 32 bit
  233. temp32=protobuf_report->mutable_body()->isv_svn();
  234. if(fit_32_into_uint16_t(temp32, &(report->body.isv_svn))!=0)
  235. return -1;
  236. printf("\nmisc select %d \n", report->body.misc_select);
  237. temp32=protobuf_report->mutable_body()->isv_prod_id();
  238. if(fit_32_into_uint16_t(temp32, &(report->body.isv_prod_id))!=0)
  239. return -1;
  240. printf("\nprod id %d \n", report->body.isv_prod_id);
  241. decode_attributes_from_protobuf(protobuf_report->mutable_body()->mutable_attributes(), &(report->body.attributes));
  242. printf("\n cpu svn\n");
  243. for(counter=0;counter<SGX_CPUSVN_SIZE;counter++)
  244. {
  245. temp32=protobuf_report->mutable_body()->cpu_svn(counter);
  246. if(fit_32_into_uint8_t(temp32, &(report->body.cpu_svn.svn[counter]))!=0)
  247. return -1;
  248. printf("%d ", report->body.cpu_svn.svn[counter]);
  249. }
  250. printf("\n reserved1 \n");
  251. for(counter=0;counter<SGX_REPORT_BODY_RESERVED1;counter++)
  252. {
  253. temp32=protobuf_report->mutable_body()->reserved1(counter);
  254. if(fit_32_into_uint8_t(temp32, &(report->body.reserved1[counter]))!=0)
  255. return -1;
  256. printf("%d ", report->body.reserved1[counter]);
  257. }
  258. printf("\n reserved2 \n");
  259. for(counter=0;counter<SGX_REPORT_BODY_RESERVED2;counter++)
  260. {
  261. temp32=protobuf_report->mutable_body()->reserved2(counter);
  262. if(fit_32_into_uint8_t(temp32, &(report->body.reserved2[counter]))!=0)
  263. return -1;
  264. printf("%d ", report->body.reserved2[counter]);
  265. }
  266. printf("\n reserved3 \n");
  267. for(counter=0;counter<SGX_REPORT_BODY_RESERVED3;counter++)
  268. {
  269. temp32=protobuf_report->mutable_body()->reserved3(counter);
  270. if(fit_32_into_uint8_t(temp32, &(report->body.reserved3[counter]))!=0)
  271. return -1;
  272. printf("%d ", report->body.reserved3[counter]);
  273. }
  274. printf("\n reserved4 \n");
  275. for(counter=0;counter<SGX_REPORT_BODY_RESERVED4;counter++)
  276. {
  277. temp32=protobuf_report->mutable_body()->reserved4(counter);
  278. if(fit_32_into_uint8_t(temp32, &(report->body.reserved4[counter]))!=0)
  279. return -1;
  280. printf("%d ", report->body.reserved4[counter]);
  281. }
  282. printf("\n mrenclave \n");
  283. for(counter=0;counter<SGX_HASH_SIZE;counter++)
  284. {
  285. temp32=protobuf_report->mutable_body()->mr_enclave(counter);
  286. if(fit_32_into_uint8_t(temp32, &(report->body.mr_enclave.m[counter]))!=0)
  287. return -1;
  288. printf("%x ", report->body.mr_enclave.m[counter]);
  289. }
  290. printf("\n mrsigner \n");
  291. for(counter=0;counter<SGX_HASH_SIZE;counter++)
  292. {
  293. temp32=protobuf_report->mutable_body()->mr_signer(counter);
  294. if(fit_32_into_uint8_t(temp32, &(report->body.mr_signer.m[counter]))!=0)
  295. return -1;
  296. printf("%x ", report->body.mr_signer.m[counter]);
  297. }
  298. printf("\n report data\n");
  299. for(counter=0;counter<SGX_REPORT_DATA_SIZE;counter++)
  300. {
  301. temp32=protobuf_report->mutable_body()->report_data(counter);
  302. if(fit_32_into_uint8_t(temp32, &(report->body.report_data.d[counter]))!=0)
  303. return -1;
  304. printf("%d ", report->body.report_data.d[counter]);
  305. }
  306. return 0;
  307. }
  308. void encode_msg1_to_protobuf( protobuf_sgx_dh_msg1_t& protobuf_dhmsg1, sgx_dh_msg1_t* native_dhmsg1)
  309. {
  310. int counter; google::protobuf::uint32 temp32; // google::protobuf::uint64 temp64;
  311. encode_ec256_public_key_to_protobuf(protobuf_dhmsg1.mutable_g_a(), &(native_dhmsg1->g_a));
  312. for(counter=0;counter<SGX_HASH_SIZE;counter++)
  313. {
  314. temp32=native_dhmsg1->target.mr_enclave.m[counter];
  315. protobuf_dhmsg1.mutable_target()->add_mr_enclave(temp32);
  316. }
  317. for(counter=0;counter<SGX_TARGET_INFO_RESERVED1_BYTES;counter++)
  318. {
  319. temp32=native_dhmsg1->target.reserved1[counter];
  320. protobuf_dhmsg1.mutable_target()->add_reserved1(temp32);
  321. }
  322. for(counter=0;counter<SGX_TARGET_INFO_RESERVED2_BYTES;counter++)
  323. {
  324. temp32=native_dhmsg1->target.reserved2[counter];
  325. protobuf_dhmsg1.mutable_target()->add_reserved2(temp32);
  326. }
  327. encode_attributes_to_protobuf(protobuf_dhmsg1.mutable_target()->mutable_attributes(), &(native_dhmsg1->target.attributes));
  328. temp32=native_dhmsg1->target.misc_select ;
  329. protobuf_dhmsg1.mutable_target()->set_misc_select(temp32);
  330. }
  331. void encode_msg3_to_protobuf(protobuf_sgx_dh_msg3_t& protobuf_dhmsg3, sgx_dh_msg3_t* native_dhmsg3)
  332. {
  333. int counter; google::protobuf::uint32 temp32;
  334. for(counter=0;counter<SGX_DH_MAC_SIZE;counter++)
  335. {
  336. temp32=native_dhmsg3->cmac[counter];
  337. protobuf_dhmsg3.add_cmac(temp32);
  338. }
  339. encode_report_to_protobuf(protobuf_dhmsg3.mutable_msg3_body()->mutable_report(), &(native_dhmsg3->msg3_body.report));
  340. int max_counter=native_dhmsg3->msg3_body.additional_prop_length;
  341. unsigned char*temp;
  342. for(counter=0,temp=native_dhmsg3->msg3_body.additional_prop;counter<max_counter;counter++,temp++)
  343. {
  344. protobuf_dhmsg3.mutable_msg3_body()->add_additional_prop(*temp);
  345. }
  346. }
  347. int decode_msg2_from_protobuf(protobuf_sgx_dh_msg2_t& protobuf_dhmsg2, sgx_dh_msg2_t* native_dhmsg2)
  348. {
  349. int counter; google::protobuf::uint32 temp32; //google::protobuf::uint64 temp64;
  350. printf("\ncmac\n");
  351. for(counter=0;counter<SGX_DH_MAC_SIZE;counter++)
  352. {
  353. temp32=protobuf_dhmsg2.cmac(counter);
  354. if(fit_32_into_uint8_t(temp32, &(native_dhmsg2->cmac[counter]))!=0)
  355. return -1;
  356. printf("%d ",native_dhmsg2->cmac[counter]);
  357. }
  358. if(decode_ec256_public_key_from_protobuf(protobuf_dhmsg2.mutable_g_b(), &(native_dhmsg2->g_b)) !=0)
  359. return -1;
  360. if(decode_report_from_protobuf(protobuf_dhmsg2.mutable_report(), &(native_dhmsg2->report)) !=0)
  361. return -1;
  362. return 0;
  363. }
  364. int print_initialized_msg1( protobuf_sgx_dh_msg1_t& protobuf_dhmsg1, sgx_dh_msg1_t* native_dhmsg1)
  365. {
  366. int counter;
  367. printf("gx\n");
  368. for(counter=0;counter<SGX_ECP256_KEY_SIZE;counter++)
  369. {
  370. printf("%d ", protobuf_dhmsg1.g_a().gx(counter));
  371. printf("%d ", native_dhmsg1->g_a.gx[counter]);
  372. }
  373. printf("\ngy\n");
  374. for(counter=0;counter<SGX_ECP256_KEY_SIZE;counter++)
  375. {
  376. printf("%d ", protobuf_dhmsg1.g_a().gy(counter));
  377. printf("%d ", native_dhmsg1->g_a.gy[counter]);
  378. }
  379. printf("\nmrenclave in target\n");
  380. for(counter=0;counter<SGX_HASH_SIZE;counter++)
  381. {
  382. printf("%" PRIu32 " ", protobuf_dhmsg1.target().mr_enclave(counter));
  383. printf("%d ", native_dhmsg1->target.mr_enclave.m[counter]);
  384. }
  385. printf("\nreserved1 in target\n");
  386. for(counter=0;counter<SGX_TARGET_INFO_RESERVED1_BYTES;counter++)
  387. {
  388. printf("%" PRIu32 " ", protobuf_dhmsg1.target().reserved1(counter));
  389. printf("%d ", native_dhmsg1->target.reserved1[counter]);
  390. }
  391. printf("\nreserved2 in target\n");
  392. for(counter=0;counter<SGX_TARGET_INFO_RESERVED2_BYTES;counter++)
  393. {
  394. printf("%" PRIu32 " ", protobuf_dhmsg1.target().reserved2(counter));
  395. printf("%d ", native_dhmsg1->target.reserved2[counter]);
  396. }
  397. printf("\n %" PRIu64 "\n", native_dhmsg1->target.attributes.flags);
  398. printf("\n %" PRIu64 "\n", protobuf_dhmsg1.target().attributes().flags());
  399. printf("\n %" PRIu64 "\n", native_dhmsg1->target.attributes.xfrm);
  400. printf("\n %" PRIu64 "\n", protobuf_dhmsg1.target().attributes().xfrm());
  401. printf("\n %" PRIu32 "\n", native_dhmsg1->target.misc_select);
  402. printf("\n %" PRIu32 "\n", protobuf_dhmsg1.target().misc_select());
  403. return 0;
  404. }
  405. int read_protobuf_msg_from_fd(int accept_fd, google::protobuf::MessageLite& message)
  406. {
  407. ZeroCopyInputStream* raw_input;
  408. CodedInputStream* coded_input;
  409. uint32_t size;
  410. CodedInputStream::Limit limit;
  411. raw_input = new FileInputStream(accept_fd);
  412. coded_input = new CodedInputStream(raw_input);
  413. if(!coded_input->ReadVarint32(&size))
  414. {
  415. printf("Error in reading size of msg");
  416. fflush(stdout);
  417. return -1;
  418. }
  419. //printf("size of msg was read to be %" PRIu32 " \n", size);
  420. fflush(stdout);
  421. limit = coded_input->PushLimit(size);
  422. if(!message.ParseFromCodedStream(coded_input))
  423. {
  424. printf("Error in parsing msg");
  425. fflush(stdout);
  426. return -1;
  427. }
  428. coded_input->PopLimit(limit);
  429. return 0;
  430. }
  431. int write_protobuf_msg_to_fd(int accept_fd, google::protobuf::MessageLite& message)
  432. {
  433. ZeroCopyOutputStream* raw_output = new FileOutputStream(accept_fd);
  434. CodedOutputStream* coded_output = new CodedOutputStream(raw_output);
  435. coded_output->WriteVarint32(message.ByteSize());
  436. if(!message.SerializeToCodedStream(coded_output))
  437. {
  438. printf("SerializeToCodedStream failed");
  439. fflush(stdout);
  440. return -1;
  441. }
  442. // As per this - https://stackoverflow.com/questions/22881876/protocol-buffers-how-to-serialize-and-deserialize-multiple-messages-into-a-file?noredirect=1&lq=1
  443. // 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)
  444. delete coded_output;
  445. delete raw_output;
  446. fflush(stdout);
  447. return 0;
  448. }
  449. ///////////// TRY TO INCLUDE RIGHT SGX HEADERS IN SGX_PROTOBUF CPP
  450. uint32_t write_to_fd(int fd, uint8_t* msg, uint32_t* expected_msg_length)
  451. {
  452. ssize_t bytes_written;
  453. bytes_written = write(fd, msg, *expected_msg_length);
  454. if(bytes_written <= 0)
  455. return 0xFFFFFFFF;
  456. fsync(fd);
  457. *expected_msg_length = bytes_written;
  458. return 0;
  459. }
  460. uint32_t read_from_fd(int fd, uint8_t* msg, uint32_t* expected_msg_length)
  461. {
  462. ssize_t bytes_read;
  463. lseek(fd, 0, SEEK_SET);
  464. bytes_read = read(fd, msg, *expected_msg_length);
  465. if(bytes_read <= 0)
  466. return 0xFFFFFFFF;
  467. return 0;
  468. }
  469. // 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)
  470. int set_up_socket(int port, sockaddr_in* address)
  471. {
  472. int server_fd = 0;
  473. // Creating socket file descriptor for listening for attestation requests.
  474. server_fd = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
  475. if (server_fd == -1)
  476. {
  477. printf("Error in creating a socket - %d", errno);
  478. return -1;
  479. }
  480. // Preparing the address struct for binding
  481. address->sin_family = AF_INET;
  482. address->sin_addr.s_addr = INADDR_ANY; // Todo: should this be localhost?
  483. address->sin_port = htons(port);
  484. // memset(address->sin_zero,0,sizeof(address->sin_zero));
  485. socklen_t addrlen = sizeof(*address);
  486. // Binding
  487. if (bind(server_fd, (sockaddr*)address, addrlen)<0)
  488. {
  489. printf("Error in binding %d - port was %d - ", errno, port);
  490. return -1;
  491. }
  492. // Listening
  493. if (listen(server_fd, 128) < 0)
  494. {
  495. printf("Error in listening %d", errno);
  496. return -1;
  497. }
  498. return server_fd;
  499. }
  500. int local_attestation_initiator(__attribute__((unused)) uint8_t* expected_mr_signer)
  501. {
  502. // declare msg1, msg2, msg3 protobuf objects and native SGX structs
  503. protobuf_sgx_dh_msg1_t protobuf_msg1;
  504. protobuf_sgx_dh_msg2_t protobuf_msg2;
  505. protobuf_sgx_dh_msg3_t protobuf_msg3;
  506. // For sgx - ecalls and storing msgs rcvd from pipes for processing.
  507. uint32_t session_id;
  508. sgx_dh_msg1_t dh_msg1; //Diffie-Hellman Message 1
  509. sgx_dh_msg2_t dh_msg2; //Diffie-Hellman Message 2
  510. sgx_dh_msg3_t dh_msg3; //Diffie-Hellman Message 3
  511. sgx_key_128bit_t dh_aek; // Session Key
  512. // initializing native sgx structs
  513. memset(&dh_aek,0, sizeof(sgx_key_128bit_t));
  514. memset(&dh_msg1, 0, sizeof(sgx_dh_msg1_t));
  515. memset(&dh_msg2, 0, sizeof(sgx_dh_msg2_t));
  516. memset(&dh_msg3, 0, sizeof(sgx_dh_msg3_t));
  517. // For socket to listen to the Apache enclave.
  518. int apache_port = 3825; int server_fd=0; int accept_fd = 0;
  519. struct sockaddr_in own_addr;
  520. struct sockaddr_storage apache_addr; socklen_t apache_addr_size = sizeof(apache_addr);
  521. // int counter;
  522. server_fd=set_up_socket(apache_port, &own_addr);
  523. if(server_fd==-1)
  524. return -1;
  525. printf("Successfully set up a socket to communicate with the Apache enclave.");
  526. fflush(stdout);
  527. uint32_t ret_status;
  528. Decryptor_session_request(e2_enclave_id, &ret_status, &dh_msg1, &session_id); // TODO: Check Return status
  529. if(ret_status != SGX_SUCCESS)
  530. {
  531. printf("session request returned failure\n"); fflush(stdout); return 0xffffffff;
  532. }
  533. encode_msg1_to_protobuf(protobuf_msg1, &dh_msg1);
  534. print_initialized_msg1(protobuf_msg1, &dh_msg1);
  535. printf("Done initialization");
  536. fflush(stdout);
  537. int no_of_msgs_xchanged=0;
  538. accept_fd = accept(server_fd, (struct sockaddr *)&apache_addr,&apache_addr_size);
  539. if (accept_fd <0)
  540. {
  541. printf("Error in accepting %d", errno);
  542. return -1;
  543. }
  544. do
  545. {
  546. if(no_of_msgs_xchanged==0){
  547. if(write_protobuf_msg_to_fd(accept_fd, protobuf_msg1)!=0)
  548. return -1;
  549. }
  550. else
  551. {
  552. printf("about to read msg2\n");
  553. fflush(stdout);
  554. // read msg2 -
  555. if(read_protobuf_msg_from_fd(accept_fd, protobuf_msg2)!=0)
  556. return -1;
  557. if(decode_msg2_from_protobuf(protobuf_msg2, &dh_msg2)!=0)
  558. return -1;
  559. printf("Done reading and decoding msg2\n");
  560. // process msg2 and generate msg3
  561. Decryptor_exchange_report(e2_enclave_id, &ret_status, &dh_msg2, &dh_msg3, &session_id);
  562. if(ret_status!=SGX_SUCCESS)
  563. {
  564. fflush(stdout);
  565. printf("exchange report failed:%x\n", ret_status);
  566. fflush(stdout);
  567. return -1;
  568. }
  569. printf("return status %d", ret_status);
  570. fflush(stdout);
  571. // convert msg3 sgx_dh_msg3_t object to a protobuf msg3 object.
  572. encode_msg3_to_protobuf(protobuf_msg3, &dh_msg3);
  573. printf("about to write msg3\n");
  574. fflush(stdout);
  575. // write msg3 -
  576. if(write_protobuf_msg_to_fd(accept_fd, protobuf_msg3)!=0)
  577. return -1;
  578. no_of_msgs_xchanged+=1;
  579. }
  580. no_of_msgs_xchanged++;
  581. }
  582. while(no_of_msgs_xchanged!=3);
  583. printf("Out of while loop\n");
  584. fflush(stdout);
  585. return 0;
  586. }
  587. /*
  588. uint32_t unseal_signing_key_pair_from_disk(int fd, __attribute__((unused)) sgx_ec256_public_t* pub_key, uint32_t* actual_sealed_msg_length)
  589. {
  590. uint32_t ret_status;
  591. uint8_t* sgx_sealed_msg;
  592. sgx_sealed_msg = (uint8_t*) malloc(*actual_sealed_msg_length);
  593. ret_status = read_from_fd(fd, sgx_sealed_msg, actual_sealed_msg_length);
  594. if(ret_status != 0)
  595. {
  596. free(sgx_sealed_msg);
  597. return 0xFFFFFFFF;
  598. }
  599. Decryptor_unseal_and_restore_sealed_signing_key_pair(e2_enclave_id, &ret_status, pub_key, sgx_sealed_msg, actual_sealed_msg_length);
  600. free(sgx_sealed_msg);
  601. return ret_status;
  602. }
  603. uint32_t create_and_seal_signing_key_pair_to_disk(int fd, sgx_ec256_public_t* pub_key, uint32_t* actual_sealed_msg_length)
  604. {
  605. uint32_t ret_status;
  606. // Generating a signing ECDSA key to sign the encryption key.
  607. Decryptor_calculate_sealed_data_size(e2_enclave_id, 3*SGX_ECP256_KEY_SIZE, actual_sealed_msg_length); // sgx_calc_sealed_data_size(0,3*SGX_ECP256_KEY_SIZE);
  608. if(*actual_sealed_msg_length == 0xFFFFFFFF)
  609. return 0xFFFFFFFF;
  610. printf("%x bytes for sealed msg\n", *actual_sealed_msg_length); fflush(stdout);
  611. uint8_t* sealed_data=(uint8_t*) malloc(*actual_sealed_msg_length);
  612. printf("Made call to sgx_calc_sealed_data_size\n"); fflush(stdout);
  613. Decryptor_create_and_seal_ecdsa_signing_key_pair(e2_enclave_id, &ret_status, (sgx_ec256_public_t*)pub_key, actual_sealed_msg_length, sealed_data);
  614. if(ret_status != SGX_SUCCESS)
  615. {
  616. printf("create_and_seal called returned an error: %x", ret_status);
  617. free(sealed_data);
  618. return 0xFFFFFFFF;
  619. }
  620. printf("It returned sgx_success\n"); fflush(stdout);
  621. ret_status = write_to_fd(fd, sealed_data, actual_sealed_msg_length);
  622. free(sealed_data);
  623. return ret_status;
  624. }
  625. */
  626. int main(__attribute__((unused)) int argc, __attribute__((unused)) char* argv[])
  627. {
  628. uint32_t ret_status;
  629. sgx_status_t status;
  630. // For sgx setup
  631. int launch_token_updated;
  632. sgx_launch_token_t launch_token;
  633. // uint8_t* pub_key = (uint8_t*) malloc(2*SGX_ECP256_KEY_SIZE);
  634. // sgx_ec256_public_t pub_key;
  635. // uint32_t actual_sealed_msg_length;
  636. status = sgx_create_enclave(Decryptor_PATH, SGX_DEBUG_FLAG, &launch_token, &launch_token_updated, &e2_enclave_id, NULL);
  637. if(status != SGX_SUCCESS)
  638. {
  639. printf("\nLoad Enclave Failure");
  640. return -1;
  641. }
  642. printf("\nDecryptor - EnclaveID %" PRIx64, e2_enclave_id);
  643. fflush(stdout);
  644. ret_status=local_attestation_initiator(NULL);
  645. if(ret_status!=0)
  646. {
  647. printf("local attestation did not successfully return: %x\n", ret_status); fflush(stdout); return 0xFFFFFFFF;
  648. }
  649. /* int sealed_signing_key_fd = open("sealed_signing_key.txt", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
  650. if(sealed_signing_key_fd == -1)
  651. {
  652. perror("\nError in opening the file sealed_signing_key.txt - ");
  653. fflush(stderr);
  654. return 0xFFFFFFFF;
  655. }
  656. printf("\nSuccessfully opened a file to seal the signing key pair for the client.\n");
  657. fflush(stdout);
  658. int start = lseek(sealed_signing_key_fd, 0, SEEK_CUR);
  659. int end = lseek(sealed_signing_key_fd, 0, SEEK_END);
  660. if(start == end && start != -1)
  661. {
  662. // TODO: file is empty. create signing key pair.
  663. start = lseek(sealed_signing_key_fd, 0, SEEK_CUR);
  664. ret_status = create_and_seal_signing_key_pair_to_disk(sealed_signing_key_fd, &pub_key, &actual_sealed_msg_length);
  665. if(ret_status != 0)
  666. {
  667. printf("\n error in generating the ecdsa signing key pair \n");
  668. fflush(stdout);
  669. return 0xFFFFFFFF;
  670. }
  671. fflush(stdout);
  672. printf("\n Generated the ecdsa key pair successfully - gx, gy\n");
  673. fflush(stdout);
  674. }
  675. else {
  676. start = lseek(sealed_signing_key_fd, 0, SEEK_CUR);
  677. if(actual_sealed_msg_length == 0)
  678. actual_sealed_msg_length = end - start;
  679. ret_status = unseal_signing_key_pair_from_disk(sealed_signing_key_fd, &pub_key, &actual_sealed_msg_length);
  680. if(ret_status != SGX_SUCCESS)
  681. {
  682. printf("\n error in unsealing the ecdsa signing key pair:%d \n", ret_status);
  683. fflush(stdout);
  684. return 0xFFFFFFFF;
  685. }
  686. printf("\n Recovered the ecdsa key pair successfully - gx, gy\n");
  687. fflush(stdout);
  688. }
  689. */
  690. // TODO: Continue with other msgs - send sign(enc | verifier mr_enclave)
  691. sgx_destroy_enclave(e2_enclave_id);
  692. return 0;
  693. }