networkClient.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. #include <fstream>
  2. #include <cstring>
  3. #include <cstdio>
  4. #include "networkClient.hpp"
  5. template <>
  6. EGCiphertext get_committed_val_from_file<EGCiphertext>(
  7. struct synchronization_tool *sync,
  8. const char *filename,
  9. Proof& pi)
  10. {
  11. std::unique_lock<std::mutex> lck(sync->mtx);
  12. std::ifstream valFile(filename);
  13. EGCiphertext retval;
  14. valFile >> pi;
  15. valFile >> retval;
  16. return retval;
  17. }
  18. template <>
  19. std::vector<TwistBipoint> get_committed_val_from_file<std::vector<TwistBipoint>>(
  20. struct synchronization_tool *sync,
  21. const char *filename,
  22. Proof& pi)
  23. {
  24. std::unique_lock<std::mutex> lck(sync->mtx);
  25. std::ifstream valFile(filename);
  26. valFile >> pi;
  27. std::vector<TwistBipoint> retval;
  28. BinarySizeT sizeOfVector;
  29. valFile >> sizeOfVector;
  30. for (size_t i = 0; i < sizeOfVector.val(); i++)
  31. {
  32. TwistBipoint currVote;
  33. valFile >> currVote;
  34. retval.push_back(currVote);
  35. }
  36. return retval;
  37. }
  38. template <typename T>
  39. T get_first_committed_val(
  40. const std::string& server,
  41. Proof& pi,
  42. const Twistpoint& shortTermPublicKey,
  43. const char *firstUri)
  44. {
  45. struct synchronization_tool sync;
  46. std::stringstream buffer;
  47. std::string data;
  48. char *filename = NULL;
  49. buffer << shortTermPublicKey;
  50. data = buffer.str();
  51. bool flag = false;
  52. while (!flag)
  53. {
  54. struct mg_connection *conn =
  55. mg_connect_websocket_client(
  56. server.c_str(),
  57. PRSONA_PORT,
  58. USE_SSL,
  59. NULL,
  60. 0,
  61. firstUri,
  62. "null",
  63. file_websocket_data_handler,
  64. file_websocket_close_handler,
  65. (void *) &sync);
  66. if (!conn)
  67. {
  68. std::cerr << "Trouble getting encrypted score from server at " << server << std::endl;
  69. continue;
  70. }
  71. std::unique_lock<std::mutex> lck(sync.mtx);
  72. filename = set_temp_filename(conn);
  73. sync.val = 0;
  74. mg_websocket_client_write(
  75. conn,
  76. MG_WEBSOCKET_OPCODE_BINARY,
  77. data.c_str(),
  78. data.length());
  79. mg_websocket_client_write(
  80. conn,
  81. MG_WEBSOCKET_OPCODE_DATACOMPLETE,
  82. "",
  83. 0);
  84. while (!sync.val)
  85. sync.cv.wait(lck);
  86. mg_close_connection(conn);
  87. flag = true;
  88. }
  89. T retval = get_committed_val_from_file<T>(&sync, filename, pi);
  90. remove(filename);
  91. delete filename;
  92. return retval;
  93. }
  94. Proof get_commitment_from_file(
  95. struct synchronization_tool *sync,
  96. char *filename)
  97. {
  98. std::unique_lock<std::mutex> lck(sync->mtx);
  99. std::ifstream scoreFile(filename);
  100. Proof retval;
  101. scoreFile >> retval;
  102. return retval;
  103. }
  104. void get_additional_commitment(
  105. const std::vector<std::string>& servers,
  106. const std::string& skip,
  107. std::vector<Proof>& pi,
  108. const Twistpoint& shortTermPublicKey,
  109. const char *commitUri)
  110. {
  111. std::vector<char *> commitmentFilenames;
  112. std::vector<struct synchronization_tool *> commitmentSyncs;
  113. for (size_t i = 0; i < servers.size(); i++)
  114. {
  115. if (servers[i] == skip)
  116. continue;
  117. struct synchronization_tool *currSync = new struct synchronization_tool;
  118. commitmentSyncs.push_back(currSync);
  119. bool flag = false;
  120. while (!flag)
  121. {
  122. struct mg_connection *conn =
  123. mg_connect_websocket_client(
  124. servers[i].c_str(),
  125. PRSONA_PORT,
  126. USE_SSL,
  127. NULL,
  128. 0,
  129. commitUri,
  130. "null",
  131. file_websocket_data_handler,
  132. file_websocket_close_handler,
  133. (void *) currSync);
  134. if (!conn)
  135. {
  136. std::cerr << "Trouble getting commitment from server at " << servers[i] << std::endl;
  137. continue;
  138. }
  139. std::unique_lock<std::mutex> lck(currSync->mtx);
  140. currSync->val = 0;
  141. commitmentFilenames.push_back(set_temp_filename(conn));
  142. mg_websocket_client_write(
  143. conn,
  144. MG_WEBSOCKET_OPCODE_DATACOMPLETE,
  145. "",
  146. 0);
  147. while (!currSync->val)
  148. currSync->cv.wait(lck);
  149. mg_close_connection(conn);
  150. flag = true;
  151. }
  152. }
  153. for (size_t i = 0; i < commitmentFilenames.size(); i++)
  154. {
  155. pi.push_back(
  156. get_commitment_from_file(
  157. commitmentSyncs[i], commitmentFilenames[i]));
  158. delete commitmentSyncs[i];
  159. remove(commitmentFilenames[i]);
  160. delete commitmentFilenames[i];
  161. }
  162. }
  163. template <typename T>
  164. T get_server_committed_val(
  165. std::default_random_engine *generator,
  166. const std::vector<std::string>& serverIPs,
  167. std::vector<Proof>& pi,
  168. const Twistpoint& shortTermPublicKey,
  169. const char *firstUri,
  170. const char *commitUri)
  171. {
  172. std::uniform_int_distribution<size_t> distribution(0, serverIPs.size() - 1);
  173. size_t whichServer = distribution(*generator);
  174. pi.clear();
  175. Proof firstProof;
  176. T retval =
  177. get_first_committed_val<T>(
  178. serverIPs[whichServer],
  179. firstProof,
  180. shortTermPublicKey,
  181. firstUri);
  182. pi.push_back(firstProof);
  183. get_additional_commitment(
  184. serverIPs,
  185. serverIPs[whichServer],
  186. pi,
  187. shortTermPublicKey,
  188. commitUri);
  189. return retval;
  190. }
  191. template EGCiphertext get_server_committed_val<EGCiphertext>(
  192. std::default_random_engine *generator,
  193. const std::vector<std::string>& serverIPs,
  194. std::vector<Proof>& pi,
  195. const Twistpoint& shortTermPublicKey,
  196. const char *firstUri,
  197. const char *commitUri);
  198. template std::vector<TwistBipoint> get_server_committed_val<std::vector<TwistBipoint>>(
  199. std::default_random_engine *generator,
  200. const std::vector<std::string>& serverIPs,
  201. std::vector<Proof>& pi,
  202. const Twistpoint& shortTermPublicKey,
  203. const char *firstUri,
  204. const char *commitUri);
  205. Twistpoint get_generator_from_file(
  206. struct synchronization_tool *sync,
  207. const char *filename,
  208. std::vector<Proof>& pi)
  209. {
  210. std::ifstream genFile(filename);
  211. BinarySizeT sizeOfVector;
  212. genFile >> sizeOfVector;
  213. for (size_t i = 0; i < sizeOfVector.val(); i++)
  214. {
  215. Proof currProof;
  216. genFile >> currProof;
  217. pi.push_back(currProof);
  218. }
  219. Twistpoint retval;
  220. genFile >> retval;
  221. return retval;
  222. }
  223. Twistpoint get_generator(
  224. std::default_random_engine *randomGenerator,
  225. const std::vector<std::string>& serverIPs,
  226. std::vector<Proof>& pi,
  227. bool fresh)
  228. {
  229. struct synchronization_tool sync;
  230. std::uniform_int_distribution<size_t> distribution(0, serverIPs.size() - 1);
  231. size_t whichServer = distribution(*randomGenerator);
  232. char *filename = NULL;
  233. pi.clear();
  234. const char* whichUri = (fresh ? FRESH_GEN_URI : BLIND_GEN_URI);
  235. bool flag = false;
  236. while (!flag)
  237. {
  238. struct mg_connection *conn = mg_connect_websocket_client(
  239. serverIPs[whichServer].c_str(),
  240. PRSONA_PORT,
  241. USE_SSL,
  242. NULL,
  243. 0,
  244. whichUri,
  245. "null",
  246. file_websocket_data_handler,
  247. file_websocket_close_handler,
  248. (void *) &sync);
  249. if (!conn)
  250. {
  251. std::cerr << "Couldn't obtain BGN details" << std::endl;
  252. continue;
  253. }
  254. std::unique_lock<std::mutex> lck(sync.mtx);
  255. filename = set_temp_filename(conn);
  256. sync.val = 0;
  257. mg_websocket_client_write(
  258. conn,
  259. MG_WEBSOCKET_OPCODE_DATACOMPLETE,
  260. "",
  261. 0);
  262. while (!sync.val)
  263. sync.cv.wait(lck);
  264. mg_close_connection(conn);
  265. flag = true;
  266. }
  267. Twistpoint retval =
  268. get_generator_from_file(&sync, filename, pi);
  269. remove(filename);
  270. delete filename;
  271. return retval;
  272. }
  273. PrsonaClientWebSocketHandler::PrsonaClientWebSocketHandler(
  274. PrsonaClient *prsonaClient,
  275. const std::vector<std::string>& serverIPs,
  276. const std::string& selfIP,
  277. std::default_random_engine *generator)
  278. : prsonaClient(prsonaClient), serverIPs(serverIPs),
  279. selfIP(selfIP), generator(generator)
  280. { /* */ }
  281. bool PrsonaClientWebSocketHandler::handleConnection(
  282. CivetServer *server,
  283. const struct mg_connection *conn)
  284. {
  285. const struct mg_request_info *info = mg_get_request_info(conn);
  286. bool flag = (info->query_string && info->query_string[0] == PRSONA_VERIFY_REPUTATION_PROOF);
  287. return flag;
  288. }
  289. void PrsonaClientWebSocketHandler::handleReadyState(
  290. CivetServer *server,
  291. struct mg_connection *conn)
  292. {
  293. const struct mg_request_info *info = mg_get_request_info(conn);
  294. switch (info->query_string[0])
  295. {
  296. case PRSONA_VERIFY_REPUTATION_PROOF:
  297. set_temp_filename(conn);
  298. break;
  299. default:
  300. mg_set_user_connection_data(conn, NULL);
  301. break;
  302. }
  303. }
  304. bool PrsonaClientWebSocketHandler::handleData(
  305. CivetServer *server,
  306. struct mg_connection *conn,
  307. int bits,
  308. char *data,
  309. size_t data_len)
  310. {
  311. char *filename = (char *) mg_get_user_connection_data(conn);
  312. if ((bits & 0xf) == MG_WEBSOCKET_OPCODE_DATACOMPLETE)
  313. {
  314. generate_response(conn, filename);
  315. return false;
  316. }
  317. if ((bits & 0xf) != MG_WEBSOCKET_OPCODE_BINARY && (bits & 0xf) != MG_WEBSOCKET_OPCODE_CONTINUATION)
  318. {
  319. std::cerr << "Unknown opcode: failing." << std::endl;
  320. return false;
  321. }
  322. FILE *currFile = fopen(filename, "ab");
  323. fwrite(data, sizeof(char), data_len, currFile);
  324. fclose(currFile);
  325. return true;
  326. }
  327. void PrsonaClientWebSocketHandler::generate_response(
  328. struct mg_connection *conn,
  329. const char *filename) const
  330. {
  331. const struct mg_request_info *info = mg_get_request_info(conn);
  332. switch (info->query_string[0])
  333. {
  334. case PRSONA_VERIFY_REPUTATION_PROOF:
  335. verify_reputation_proof(conn, filename);
  336. break;
  337. default:
  338. break;
  339. }
  340. }
  341. void PrsonaClientWebSocketHandler::handleClose(
  342. CivetServer *server,
  343. const struct mg_connection *conn)
  344. {
  345. char *filename = (char *) mg_get_user_connection_data(conn);
  346. if (!filename)
  347. return;
  348. remove(filename);
  349. delete filename;
  350. }
  351. void PrsonaClientWebSocketHandler::verify_reputation_proof(
  352. struct mg_connection *conn, const char *filename) const
  353. {
  354. std::ifstream file(filename);
  355. std::vector<Proof> pi;
  356. Twistpoint shortTermPublicKey;
  357. Scalar threshold;
  358. BinarySizeT sizeOfVector;
  359. file >> sizeOfVector;
  360. for (size_t i = 0; i < sizeOfVector.val(); i++)
  361. {
  362. Proof currProof;
  363. file >> currProof;
  364. pi.push_back(currProof);
  365. }
  366. file >> shortTermPublicKey;
  367. file >> threshold;
  368. std::vector<Proof> generatorProof;
  369. Twistpoint freshGenerator =
  370. get_generator(
  371. generator,
  372. serverIPs,
  373. generatorProof,
  374. true);
  375. prsonaClient->receive_fresh_generator(
  376. generatorProof,
  377. freshGenerator);
  378. std::vector<Proof> encryptedScoreProof;
  379. EGCiphertext encryptedScore =
  380. get_server_committed_val<EGCiphertext>(
  381. generator,
  382. serverIPs,
  383. encryptedScoreProof,
  384. shortTermPublicKey,
  385. USER_TALLY_URI,
  386. USER_TALLY_COMMIT_URI);
  387. bool flag =
  388. prsonaClient->verify_reputation_proof(
  389. pi,
  390. shortTermPublicKey,
  391. threshold,
  392. encryptedScoreProof,
  393. encryptedScore);
  394. std::string data = flag ? "\x01" : "\x00";
  395. mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_BINARY, data.c_str(), 1);
  396. mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_DATACOMPLETE, "", 0);
  397. }