MainLogic.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. //
  2. // Created by miti on 2019-12-28.
  3. //
  4. #include "MainLogic.h"
  5. #include "crypto.h"
  6. #include "stdio.h"
  7. #include <errno.h>
  8. #include <string.h>
  9. // For socket programming
  10. #include <arpa/inet.h>
  11. #include <sys/socket.h>
  12. #include <netinet/in.h>
  13. #define DECRYPTOR_PORT_DATA 3825
  14. #define DECRYPTOR_PORT_HEADERS 3826
  15. // Sets up a socket connected to the port passed as input - returns the socket FD on success and -1 on error.
  16. // Also prints the errno on error.
  17. int MainLogic::set_up_socket_connect(int port)
  18. {
  19. int sock = 0;
  20. if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  21. {
  22. printf("\n Error in socket call - errno is %d \n", errno); fflush(stdout);
  23. return -1;
  24. }
  25. struct sockaddr_in serv_addr;
  26. memset(&serv_addr, '0', sizeof(serv_addr));
  27. serv_addr.sin_family = AF_INET;
  28. serv_addr.sin_port = htons(port);
  29. // Convert IPv4 and IPv6 addresses from text to binary form
  30. if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0)
  31. {
  32. printf("\nError in inet_pton - errno is %d\n", errno); fflush(stdout);
  33. return -1;
  34. }
  35. if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
  36. {
  37. printf("\nError in connect - errno is %d \n", errno); fflush(stdout);
  38. return -1;
  39. }
  40. return sock;
  41. }
  42. int MainLogic::conduct_la()
  43. {
  44. uint8_t key[16];
  45. uint32_t ret_status;
  46. int data_fd;
  47. data_fd = set_up_socket_connect(DECRYPTOR_PORT_DATA);
  48. printf("About to setup socket.\n");
  49. if(data_fd <= 0)
  50. {
  51. printf("Could not set up a socket with port %d\n", DECRYPTOR_PORT_DATA);
  52. fflush(stdout);
  53. return 0x1;
  54. }
  55. // Conduct LA.
  56. ret_status = laInitiator.conduct_la(data_fd);
  57. if (ret_status != 0) {
  58. printf("LA initiator returned an error: %d\n", ret_status);
  59. fflush(stdout);
  60. return ret_status;
  61. }
  62. printf("\nSuccessful LA with port %d.\n", data_fd);
  63. fflush(stdout);
  64. printf("Setting up fds for post LA messaging.\n");
  65. fflush(stdout);
  66. // Use the same channel for sending client data as the one used above for LA messages.
  67. postLaMessaging.set_data_fd(data_fd);
  68. // Use the symmetric key from LA to send messages for the rest of the deployment stage.
  69. printf("Retrieving key from LA inititator.\n");
  70. fflush(stdout);
  71. laInitiator.get_la_symmetric_key(key);
  72. printf("Setting key for post LA messaging for both data and headers channels.\n");
  73. fflush(stdout);
  74. postLaMessaging.set_la_symmetric_key(key);
  75. return 0;
  76. }
  77. int MainLogic::get_initial_headers()
  78. {
  79. int headers_fd, ret_status;
  80. headers_fd = set_up_socket_connect(DECRYPTOR_PORT_HEADERS);
  81. if(headers_fd <= 0)
  82. {
  83. // TODO: Proper error handling.
  84. printf("Could not set up a socket with port %d\n", DECRYPTOR_PORT_HEADERS);
  85. fflush(stdout);
  86. return 0x1;
  87. }
  88. printf("Set up a socket to receive headers from the decryptor.\n"); fflush(stdout);
  89. // Use a different channel for receiving headers asynchronously.
  90. postLaMessaging.set_headers_fd(headers_fd);
  91. ret_status = postLaMessaging.receive_header_from_decryptor(last_header_value);
  92. if(ret_status != 0)
  93. {
  94. printf("Could not receive the header from the decryptor. Had error %d\n", ret_status);
  95. fflush(stdout);
  96. }
  97. return 0;
  98. }
  99. int MainLogic::decode_base64_fields_list(std::vector <std::string> &base64_fields_list, std::vector <std::vector<unsigned char>> &binary_fields_list)
  100. {
  101. int binary_field_size, base64_field_size, counter;
  102. const char* base64_field_ptr;
  103. unsigned char* binary_field_ptr = NULL;
  104. printf("In the base64 decode list function.\n"); fflush(stdout);
  105. for (auto &base64_field : base64_fields_list)
  106. {
  107. base64_field_size = base64_field.size();
  108. base64_field_ptr = base64_field.c_str();
  109. // upper limit - the binary data will always be smaller than this (base64 length ~= 4/3 * binary length)
  110. binary_field_ptr = (unsigned char*) realloc(binary_field_ptr, base64_field_size);
  111. printf("About to call the decoder on the following field:%s of the following size%d\n", base64_field_ptr, base64_field_size); fflush(stdout);
  112. binary_field_size = base64_decoding_wrapper(base64_field_ptr, base64_field_size, binary_field_ptr);
  113. printf("Called the decoder and got the following size: %d.\n", binary_field_size );
  114. if(binary_field_size <= 0)
  115. {
  116. printf("Had an error in decoding.\n"); fflush(stdout);
  117. free(binary_field_ptr);
  118. return 0x1;
  119. }
  120. std::vector<unsigned char> binary_field(binary_field_ptr, binary_field_ptr + binary_field_size);
  121. binary_fields_list.push_back(binary_field);
  122. }
  123. free(binary_field_ptr);
  124. for (auto binaryField : binary_fields_list)
  125. {
  126. for(auto myByte : binaryField)
  127. printf("%02x ", myByte);
  128. printf("\n");
  129. }
  130. return 0;
  131. }
  132. void MainLogic::deployment_stage() {
  133. int ret_status = conduct_la();
  134. if(ret_status != 0) {
  135. printf("Error in conducting LA: %d.\n", ret_status);
  136. fflush(stdout);
  137. return;
  138. }
  139. ret_status = get_initial_headers();
  140. if(ret_status !=0 )
  141. {
  142. printf("Error in getting initial headers: %d.\n", ret_status);
  143. fflush(stdout);
  144. return;
  145. }
  146. // time_file_fd=open("target_time.txt", O_APPEND | O_WRONLY);
  147. }
  148. Php::Value MainLogic::php_decrypt_wrapper(Php::Parameters &params ) {
  149. std::vector <std::string> base64_fields_list;
  150. std::vector <std::vector<unsigned char>> binary_fields_list, plaintext_fields_list;
  151. uint32_t ret_status, no_of_fields, counter, counter2;
  152. Php::Object ret_object;
  153. ret_object["success"]="false";
  154. no_of_fields = params[0].size();
  155. printf("Received client's ciphertext fields, performing base 64 decoding.\n"); fflush(stdout);
  156. base64_fields_list = params[0]; //Php::array_values(Php::array_values(params)[0]);
  157. printf("In the base64 decode list function.\n"); fflush(stdout);
  158. /*
  159. printf("Field: \n");
  160. for(counter2=0; counter2<base64_fields_list[counter].length(); counter2++)
  161. printf("%02x ", base64_fields_list[counter][counter2]);
  162. */
  163. ret_status = decode_base64_fields_list(base64_fields_list, binary_fields_list);
  164. if (ret_status != 0) {
  165. printf("Could not perform base64 decoding correctly.");
  166. fflush(stdout);
  167. ret_object["error"] = "Could not perform base64 decoding correctly.";
  168. return ret_object;
  169. }
  170. printf("Sending data to the decryptor.\n"); fflush(stdout);
  171. ret_status = postLaMessaging.send_data_to_decryptor(binary_fields_list);
  172. if(ret_status != 0)
  173. {
  174. printf("Cannot send messages to the decryptor.\n"); fflush(stdout);
  175. ret_object["error"]="Cannot send messages to the decryptor.\n";
  176. return ret_object;
  177. }
  178. printf("Receiving data from the decryptor.\n"); fflush(stdout);
  179. ret_status = postLaMessaging.receive_data_from_decryptor(plaintext_fields_list);
  180. if(ret_status != 0)
  181. {
  182. printf("Cannot receive messages from the decryptor.\n"); fflush(stdout);
  183. ret_object["error"]="Cannot receive messages from the decryptor.\n";
  184. return ret_object;
  185. }
  186. ret_object["success"]="true";
  187. no_of_fields = plaintext_fields_list.size();
  188. std::vector<unsigned char> temp_vector;
  189. Php::Array ret_php_array;
  190. for(counter=0; counter<no_of_fields; counter++)
  191. {
  192. temp_vector = plaintext_fields_list[counter];
  193. ret_php_array[counter] = std::string((char*) &temp_vector.front(), (ssize_t) temp_vector.size());
  194. }
  195. ret_object["fields"]=ret_php_array;
  196. for (auto binaryField : plaintext_fields_list)
  197. {
  198. for(auto myByte : binaryField)
  199. printf("%c", myByte);
  200. printf("\n");
  201. }
  202. printf("Returning back to good old php.\n"); fflush(stdout);
  203. return ret_object;
  204. }
  205. Php::Value MainLogic::get_mitigator_header() {
  206. std::string header_value;
  207. uint32_t ret_status;
  208. if(header_refresh_counter < 100)
  209. {
  210. header_value = last_header_value;
  211. header_refresh_counter++;
  212. }
  213. else
  214. {
  215. ret_status = postLaMessaging.receive_header_from_decryptor(header_value);
  216. if(ret_status != 0)
  217. {
  218. printf(" Cannot obtain a header from the decryptor.\n"); fflush(stdout);
  219. header_value = "!! Cannot obtain a header from the decryptor.";
  220. }
  221. header_refresh_counter = 0;
  222. }
  223. printf("Returning this header value:%s\n", header_value.c_str()); fflush(stdout);
  224. return header_value;
  225. }