123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- #include "networkClient.hpp"
- std::string random_string(size_t length)
- {
- auto randchar = []() -> char
- {
- const char charset[] =
- "0123456789_-"
- "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- "abcdefghijklmnopqrstuvwxyz";
- const size_t max_index = (sizeof(charset) - 1);
- return charset[ rand() % max_index ];
- };
- std::string str(length,0);
- std::generate_n(str.begin(), length, randchar);
- return str;
- }
- PrsonaClientWebSocketHandler::PrsonaClientWebSocketHandler(
- const PrsonaClient *prsonaClient,
- const std::vector<std::string> serverIPs,
- const std::string selfIP)
- : prsonaClient(prsonaClient), serverIPs(serverIPs), selfIP(selfIP)
- { /* */ }
- virtual bool PrsonaClientWebSocketHandler::handleConnection(
- CivetServer *server,
- const struct mg_connection *conn)
- {
- const struct mg_request_info *info = mg_get_request_info(conn);
-
- flag = (info->query_string && info->query_string[0] == PRSONA_VERIFY_REPUTATION_PROOF);
- return flag;
- }
- void PrsonaServerWebSocketHandler::set_temp_filename(
- struct mg_connection *conn) const
- {
- std::string filename = random_string(TMP_FILE_SIZE);
-
- char *c_filename = new char[TMP_FILE_SIZE+TMP_DIR_SIZE+1];
- strncpy(c_filename, TMP_DIR, TMP_DIR_SIZE);
- for (size_t i = 0; i < TMP_FILE_SIZE; i++)
- c_filename[i + TMP_DIR_SIZE] = filename[i];
- c_filename[TMP_DIR_SIZE + TMP_FILE_SIZE] = 0;
- mg_set_user_connection_data(conn, c_filename);
- }
- virtual 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(conn);
- break;
- default:
- mg_set_user_connection_data(conn, NULL);
- break;
- }
- }
- virtual 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 PrsonaServerWebSocketHandler::generate_response(
- struct mg_connection *conn,
- char *filename)
- {
- 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;
- }
- }
- virtual 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;
- }
- EGCiphertext PrsonaClientWebSocketHandler::get_first_encrypted_score(
- const std::string& server,
- Proof& pi,
- const Twistpoint& shortTermPublicKey) const
- {
- struct synchronization_tool synch;
- std::unique_lock<std::mutex> lck(synch.mtx, std::defer_lock);
- bool flag = false;
- while (!flag)
- {
- struct mg_connection *conn =
- mg_connect_websocket_client(
- server.c_str(),
- PRSONA_PORT,
- USE_SSL,
- NULL,
- 0,
- USER_TALLY_URI,
- "null",
- score_websocket_data_handler,
- score_websocket_close_handler,
- (void *) &synch);
- if (!conn)
- {
- std::cerr << "Trouble getting encrypted score from server at " << recipient << std::endl;
- continue;
- }
- lck.lock();
- synch.val = 0;
- mg_websocket_client_write(
- conn,
- MG_WEBSOCKET_OPCODE_DATACOMPLETE,
- "",
- 0);
- flag = true;
- }
- while (!synch.val)
- synch.cv.wait(lck)
- // TODO: Finish retrieving first score
- }
- void PrsonaClientWebSocketHandler::get_additional_commitment(
- const std::vector<std::string>& server,
- std::vector<Proof>& pi,
- const Twistpoint& shortTermPublicKey) const
- {
- bool flag = false;
- while (!flag)
- {
- struct mg_connection *conn =
- mg_connect_websocket_client(
- server.c_str(),
- PRSONA_PORT,
- USE_SSL,
- NULL,
- 0,
- USER_TALLY_COMMIT_URI,
- "null",
- synchro_websocket_data_handler,
- empty_websocket_close_handler,
- (void *) synch);
- if (!conn)
- {
- std::cerr << "Trouble getting encrypted score commitment from server at " << recipient << std::endl;
- continue;
- }
- mg_websocket_client_write(
- conn,
- MG_WEBSOCKET_OPCODE_DATACOMPLETE,
- "",
- 0);
- flag = true;
- }
- // TODO: Finish retrieving rest of score commitments
- }
- EGCiphertext PrsonaClientWebSocketHandler::get_encrypted_score(
- std::vector<Proof>& pi,
- const Twistpoint& shortTermPublicKey) const
- {
- pi.clear();
- Proof firstProof;
- EGCiphertext retval =
- get_first_encrypted_score(
- serverIPs[0], firstProof, shortTermPublicKey);
- pi.push_back(firstProof);
- get_additional_commitment(serverIPs, pi, shortTermPublicKey);
- return retval;
- }
- void PrsonaClientWebSocketHandler::verify_reputation_proof(
- struct mg_connection *conn, const char *filename) const
- {
- ifstream file(filename);
- std::vector<Proof> pi;
- Twistpoint shortTermPublicKey;
- Scalar threshold;
- size_t sizeOfVector;
- file >> sizeOfVector;
- for (size_t i = 0; i < sizeOfVector; i++)
- {
- Proof currProof;
- file >> currProof;
- pi.push_back(currProof);
- }
- file >> shortTermPublicKey;
- file >> threshold;
- for (size_t i = 0; i < sizeOfVector; i++)
- {
- CurveBipoint currScore;
- file >> currScore;
- serverScores.push_back(currScore);
- }
- std::vector<Proof> encryptedScoreProof;
- EGCiphertext encryptedScore =
- get_encrypted_score(encryptedScoreProof);
- bool flag =
- prsonaClient->verify_reputation_proof(
- pi,
- shortTermPublicKey,
- threshold,
- encryptedScoreProof,
- encryptedScore);
- 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);
- }
|