123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610 |
- #include <fstream>
- #include <cstring>
- #include <cstdio>
- #include "networkClient.hpp"
- template <>
- EGCiphertext get_committed_val_from_file<EGCiphertext>(
- struct synchronization_tool *sync,
- const char *filename,
- Proof& pi)
- {
- std::unique_lock<std::mutex> lck(sync->mtx);
- std::ifstream valFile(filename);
- EGCiphertext retval;
- valFile >> pi;
- valFile >> retval;
- return retval;
- }
- template <>
- CurveBipoint get_committed_val_from_file<CurveBipoint>(
- struct synchronization_tool *sync,
- const char *filename,
- Proof& pi)
- {
- std::unique_lock<std::mutex> lck(sync->mtx);
- std::ifstream valFile(filename);
- CurveBipoint retval;
- valFile >> pi;
- valFile >> retval;
- return retval;
- }
- template <>
- std::vector<TwistBipoint> get_committed_val_from_file<std::vector<TwistBipoint>>(
- struct synchronization_tool *sync,
- const char *filename,
- Proof& pi)
- {
- std::unique_lock<std::mutex> lck(sync->mtx);
- std::ifstream valFile(filename);
- valFile >> pi;
- std::vector<TwistBipoint> retval;
- BinarySizeT sizeOfVector;
- valFile >> sizeOfVector;
- for (size_t i = 0; i < sizeOfVector.val(); i++)
- {
- TwistBipoint currVote;
- valFile >> currVote;
- retval.push_back(currVote);
- }
- return retval;
- }
- template <>
- std::vector<Twistpoint> get_committed_val_from_file<std::vector<Twistpoint>>(
- struct synchronization_tool *sync,
- const char *filename,
- Proof& pi)
- {
- std::unique_lock<std::mutex> lck(sync->mtx);
- std::ifstream valFile(filename);
- valFile >> pi;
- std::vector<Twistpoint> retval;
- BinarySizeT sizeOfVector;
- valFile >> sizeOfVector;
- for (size_t i = 0; i < sizeOfVector.val(); i++)
- {
- Twistpoint currVote;
- valFile >> currVote;
- retval.push_back(currVote);
- }
- return retval;
- }
- template <>
- std::vector<std::vector<TwistBipoint>> get_committed_val_from_file<std::vector<std::vector<TwistBipoint>>>(
- struct synchronization_tool *sync,
- const char *filename,
- Proof& pi)
- {
- std::unique_lock<std::mutex> lck(sync->mtx);
- std::ifstream valFile(filename);
- valFile >> pi;
- std::vector<std::vector<TwistBipoint>> retval;
- BinarySizeT sizeOfVector;
- valFile >> sizeOfVector;
- for (size_t i = 0; i < sizeOfVector.val(); i++)
- {
- std::vector<TwistBipoint> currRow;
- for (size_t j = 0; j < sizeOfVector.val(); j++)
- {
- TwistBipoint currVote;
- valFile >> currVote;
- currRow.push_back(currVote);
- }
- retval.push_back(currRow);
- }
- return retval;
- }
- template <typename T>
- T get_first_committed_val(
- std::default_random_engine *rng,
- const std::string& serverIP,
- int serverPort,
- Proof& pi,
- const Twistpoint& shortTermPublicKey,
- const char *firstUri)
- {
- struct synchronization_tool sync;
- std::stringstream buffer;
- std::string data;
- char *filename = NULL;
- buffer << shortTermPublicKey;
- data = buffer.str();
- bool flag = false;
- while (!flag)
- {
- struct mg_connection *conn =
- mg_connect_websocket_client(
- serverIP.c_str(),
- serverPort,
- USE_SSL,
- NULL,
- 0,
- firstUri,
- "null",
- file_websocket_data_handler,
- file_websocket_close_handler,
- (void *) &sync);
- if (!conn)
- {
- std::cerr << "Trouble getting encrypted score from server at " << serverIP << ":" << serverPort << std::endl;
- continue;
- }
- std::unique_lock<std::mutex> lck(sync.mtx);
- filename = set_temp_filename(*rng, conn);
- sync.val = 0;
- mg_websocket_client_write(
- conn,
- MG_WEBSOCKET_OPCODE_BINARY,
- data.c_str(),
- data.length());
- mg_websocket_client_write(
- conn,
- MG_WEBSOCKET_OPCODE_DATACOMPLETE,
- "",
- 0);
- while (!sync.val)
- sync.cv.wait(lck);
- mg_close_connection(conn);
- flag = true;
- }
- T retval = get_committed_val_from_file<T>(&sync, filename, pi);
- remove(filename);
- delete [] filename;
- return retval;
- }
- Proof get_commitment_from_file(
- struct synchronization_tool *sync,
- char *filename)
- {
- std::unique_lock<std::mutex> lck(sync->mtx);
- std::ifstream scoreFile(filename);
- Proof retval;
- scoreFile >> retval;
- return retval;
- }
- void get_additional_commitment(
- std::default_random_engine *rng,
- const std::vector<std::string>& serverIPs,
- const std::vector<int>& serverPorts,
- const std::string& skipIP,
- int skipPort,
- std::vector<Proof>& pi,
- const Twistpoint& shortTermPublicKey,
- const char *commitUri)
- {
- std::vector<char *> commitmentFilenames;
- std::vector<struct synchronization_tool *> commitmentSyncs;
- for (size_t i = 0; i < serverIPs.size(); i++)
- {
- if (serverIPs[i] == skipIP && serverPorts[i] == skipPort)
- continue;
- struct synchronization_tool *currSync = new struct synchronization_tool;
- commitmentSyncs.push_back(currSync);
- bool flag = false;
- while (!flag)
- {
- struct mg_connection *conn =
- mg_connect_websocket_client(
- serverIPs[i].c_str(),
- serverPorts[i],
- USE_SSL,
- NULL,
- 0,
- commitUri,
- "null",
- file_websocket_data_handler,
- file_websocket_close_handler,
- (void *) currSync);
- if (!conn)
- {
- std::cerr << "Trouble getting commitment from server at " << serverIPs[i] << ":" << serverPorts[i] << std::endl;
- continue;
- }
- std::unique_lock<std::mutex> lck(currSync->mtx);
- currSync->val = 0;
- commitmentFilenames.push_back(set_temp_filename(*rng, conn));
- mg_websocket_client_write(
- conn,
- MG_WEBSOCKET_OPCODE_DATACOMPLETE,
- "",
- 0);
- while (!currSync->val)
- currSync->cv.wait(lck);
- mg_close_connection(conn);
- flag = true;
- }
- }
-
- for (size_t i = 0; i < commitmentFilenames.size(); i++)
- {
- pi.push_back(
- get_commitment_from_file(
- commitmentSyncs[i], commitmentFilenames[i]));
- delete commitmentSyncs[i];
- remove(commitmentFilenames[i]);
- delete [] commitmentFilenames[i];
- }
- }
- template <typename T>
- T get_server_committed_val(
- std::default_random_engine *generator,
- const std::vector<std::string>& serverIPs,
- const std::vector<int>& serverPorts,
- std::vector<Proof>& pi,
- const Twistpoint& shortTermPublicKey,
- const char *firstUri,
- const char *commitUri)
- {
- std::uniform_int_distribution<size_t> distribution(0, serverIPs.size() - 1);
- size_t whichServer = distribution(*generator);
- pi.clear();
- Proof firstProof;
- T retval =
- get_first_committed_val<T>(
- generator,
- serverIPs[whichServer],
- serverPorts[whichServer],
- firstProof,
- shortTermPublicKey,
- firstUri);
- pi.push_back(firstProof);
- get_additional_commitment(
- generator,
- serverIPs,
- serverPorts,
- serverIPs[whichServer],
- serverPorts[whichServer],
- pi,
- shortTermPublicKey,
- commitUri);
- return retval;
- }
- template EGCiphertext get_server_committed_val<EGCiphertext>(
- std::default_random_engine *generator,
- const std::vector<std::string>& serverIPs,
- const std::vector<int>& serverPorts,
- std::vector<Proof>& pi,
- const Twistpoint& shortTermPublicKey,
- const char *firstUri,
- const char *commitUri);
- template CurveBipoint get_server_committed_val<CurveBipoint>(
- std::default_random_engine *generator,
- const std::vector<std::string>& serverIPs,
- const std::vector<int>& serverPorts,
- std::vector<Proof>& pi,
- const Twistpoint& shortTermPublicKey,
- const char *firstUri,
- const char *commitUri);
- template std::vector<Twistpoint> get_server_committed_val<std::vector<Twistpoint>>(
- std::default_random_engine *generator,
- const std::vector<std::string>& serverIPs,
- const std::vector<int>& serverPorts,
- std::vector<Proof>& pi,
- const Twistpoint& shortTermPublicKey,
- const char *firstUri,
- const char *commitUri);
- template std::vector<TwistBipoint> get_server_committed_val<std::vector<TwistBipoint>>(
- std::default_random_engine *generator,
- const std::vector<std::string>& serverIPs,
- const std::vector<int>& serverPorts,
- std::vector<Proof>& pi,
- const Twistpoint& shortTermPublicKey,
- const char *firstUri,
- const char *commitUri);
- template std::vector<std::vector<TwistBipoint>> get_server_committed_val<std::vector<std::vector<TwistBipoint>>>(
- std::default_random_engine *generator,
- const std::vector<std::string>& serverIPs,
- const std::vector<int>& serverPorts,
- std::vector<Proof>& pi,
- const Twistpoint& shortTermPublicKey,
- const char *firstUri,
- const char *commitUri);
- Twistpoint get_generator_from_file(
- struct synchronization_tool *sync,
- const char *filename,
- std::vector<Proof>& pi)
- {
- std::ifstream genFile(filename);
- BinarySizeT sizeOfVector;
- genFile >> sizeOfVector;
- for (size_t i = 0; i < sizeOfVector.val(); i++)
- {
- Proof currProof;
- genFile >> currProof;
- pi.push_back(currProof);
- }
- Twistpoint retval;
- genFile >> retval;
- return retval;
- }
- Twistpoint get_generator(
- std::default_random_engine *randomGenerator,
- const std::vector<std::string>& serverIPs,
- const std::vector<int>& serverPorts,
- std::vector<Proof>& pi,
- bool fresh)
- {
- struct synchronization_tool sync;
- std::uniform_int_distribution<size_t> distribution(0, serverIPs.size() - 1);
- size_t whichServer = distribution(*randomGenerator);
- char *filename = NULL;
- pi.clear();
- const char* whichUri = (fresh ? FRESH_GEN_URI : BLIND_GEN_URI);
- bool flag = false;
- while (!flag)
- {
- struct mg_connection *conn = mg_connect_websocket_client(
- serverIPs[whichServer].c_str(),
- serverPorts[whichServer],
- USE_SSL,
- NULL,
- 0,
- whichUri,
- "null",
- file_websocket_data_handler,
- file_websocket_close_handler,
- (void *) &sync);
- if (!conn)
- {
- std::cerr << "Couldn't obtain BGN details" << std::endl;
- continue;
- }
- std::unique_lock<std::mutex> lck(sync.mtx);
- filename = set_temp_filename(*randomGenerator, conn);
- sync.val = 0;
- mg_websocket_client_write(
- conn,
- MG_WEBSOCKET_OPCODE_DATACOMPLETE,
- "",
- 0);
- while (!sync.val)
- sync.cv.wait(lck);
- mg_close_connection(conn);
- flag = true;
- }
- Twistpoint retval =
- get_generator_from_file(&sync, filename, pi);
- remove(filename);
- delete [] filename;
- return retval;
- }
- PrsonaClientWebSocketHandler::PrsonaClientWebSocketHandler(
- PrsonaClient *prsonaClient,
- const std::vector<std::string>& serverIPs,
- const std::vector<int>& serverPorts,
- std::default_random_engine *generator)
- : prsonaClient(prsonaClient), serverIPs(serverIPs),
- serverPorts(serverPorts), generator(generator)
- { /* */ }
- bool PrsonaClientWebSocketHandler::handleConnection(
- CivetServer *server,
- const struct mg_connection *conn)
- {
- const struct mg_request_info *info = mg_get_request_info(conn);
-
- bool flag = (info->query_string && info->query_string[0] == PRSONA_VERIFY_REPUTATION_PROOF);
- return flag;
- }
- void PrsonaClientWebSocketHandler::handleReadyState(
- CivetServer *server,
- struct mg_connection *conn)
- {
- const struct mg_request_info *info = mg_get_request_info(conn);
- switch (info->query_string[0])
- {
- case PRSONA_VERIFY_REPUTATION_PROOF:
- set_temp_filename(*generator, conn);
- break;
- default:
- mg_set_user_connection_data(conn, NULL);
- break;
- }
- }
- bool PrsonaClientWebSocketHandler::handleData(
- CivetServer *server,
- struct mg_connection *conn,
- int bits,
- char *data,
- size_t data_len)
- {
- char *filename = (char *) mg_get_user_connection_data(conn);
- if ((bits & 0xf) == MG_WEBSOCKET_OPCODE_DATACOMPLETE)
- {
- generate_response(conn, filename);
- return false;
- }
- if ((bits & 0xf) != MG_WEBSOCKET_OPCODE_BINARY && (bits & 0xf) != MG_WEBSOCKET_OPCODE_CONTINUATION)
- {
- std::cerr << "Unknown opcode: failing." << std::endl;
- return false;
- }
- FILE *currFile = fopen(filename, "ab");
- fwrite(data, sizeof(char), data_len, currFile);
- fclose(currFile);
- return true;
- }
- void PrsonaClientWebSocketHandler::generate_response(
- struct mg_connection *conn,
- const char *filename) const
- {
- const struct mg_request_info *info = mg_get_request_info(conn);
- switch (info->query_string[0])
- {
- case PRSONA_VERIFY_REPUTATION_PROOF:
- verify_reputation_proof(conn, filename);
- break;
- default:
- break;
- }
- }
- void PrsonaClientWebSocketHandler::handleClose(
- CivetServer *server,
- const struct mg_connection *conn)
- {
- char *filename = (char *) mg_get_user_connection_data(conn);
- if (!filename)
- return;
- remove(filename);
- delete [] filename;
- }
- void PrsonaClientWebSocketHandler::verify_reputation_proof(
- struct mg_connection *conn, const char *filename) const
- {
- std::ifstream file(filename);
- std::vector<Proof> pi;
- Twistpoint shortTermPublicKey;
- Scalar threshold;
- BinarySizeT sizeOfVector;
- file >> sizeOfVector;
- for (size_t i = 0; i < sizeOfVector.val(); i++)
- {
- Proof currProof;
- file >> currProof;
- pi.push_back(currProof);
- }
- file >> shortTermPublicKey;
- file >> threshold;
- std::vector<Proof> generatorProof;
- Twistpoint freshGenerator =
- get_generator(
- generator,
- serverIPs,
- serverPorts,
- generatorProof,
- true);
- prsonaClient->receive_fresh_generator(
- generatorProof,
- freshGenerator);
- std::vector<Proof> encryptedScoreProof;
- EGCiphertext encryptedScore =
- get_server_committed_val<EGCiphertext>(
- generator,
- serverIPs,
- serverPorts,
- encryptedScoreProof,
- shortTermPublicKey,
- USER_TALLY_URI,
- USER_TALLY_COMMIT_URI);
- bool flag =
- prsonaClient->verify_reputation_proof(
- pi,
- shortTermPublicKey,
- threshold,
- encryptedScoreProof,
- encryptedScore);
- std::string data = flag ? "\x01" : "\x00";
- mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_BINARY, data.c_str(), 1);
- mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_DATACOMPLETE, "", 0);
- }
|