MainLogic.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 3830
  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_get_initial_header(int data_fd, int headers_fd)
  43. {
  44. uint8_t key[16];
  45. uint32_t ret_status;
  46. // Conduct LA.
  47. ret_status = laInitiator.conduct_la(data_fd);
  48. if (ret_status != 0) {
  49. printf("LA initiator returned an error: %d\n", ret_status);
  50. fflush(stdout);
  51. return ret_status;
  52. }
  53. printf("\nSuccessful LA with port %d.\n", data_fd);
  54. fflush(stdout);
  55. printf("Setting up fds for post LA messaging.\n");
  56. fflush(stdout);
  57. // Use the same channel for sending client data as the one used above for LA messages.
  58. postLaMessagingData.set_fd(data_fd);
  59. // Use a different channel for receiving headers asynchronously.
  60. postLaMessagingHeaders.set_fd(headers_fd);
  61. // Use the symmetric key from LA to send messages for the rest of the deployment stage.
  62. printf("Retrieving key from LA inititator.\n ");
  63. fflush(stdout);
  64. laInitiator.get_la_symmetric_key(key);
  65. printf("Setting key for post LA messaging for both data and headers channels.\n ");
  66. fflush(stdout);
  67. postLaMessagingData.set_la_symmetric_key(key);
  68. postLaMessagingHeaders.set_la_symmetric_key(key);
  69. // Mitigator-Public-Key:
  70. return postLaMessagingHeaders.receive_secure_msg(last_header_value);
  71. }
  72. int MainLogic::decode_base64_fields_list(std::vector <std::string> &base64_fields_list, std::vector <std::string> &binary_fields_list)
  73. {
  74. uint32_t binary_field_size, base64_field_size;
  75. const char* base64_field_ptr;
  76. unsigned char* binary_field_ptr = NULL;
  77. for (auto &base64_field : base64_fields_list)
  78. {
  79. base64_field_size = base64_field.size();
  80. base64_field_ptr = base64_field.c_str();
  81. // upper limit - the binary data will always be smaller than this (base64 length ~= 4/3 * binary length)
  82. binary_field_ptr = (unsigned char*) realloc(binary_field_ptr, base64_field_size);
  83. binary_field_size = base64_decoding_wrapper(base64_field_ptr, base64_field_size, binary_field_ptr);
  84. if(binary_field_size <= 0)
  85. {
  86. free(binary_field_ptr);
  87. return 0x1;
  88. }
  89. binary_fields_list.push_back(std::string(reinterpret_cast<const char*> (binary_field_ptr), binary_field_size));
  90. }
  91. return 0;
  92. }
  93. void MainLogic::deployment_stage() {
  94. setbuf(stdout,NULL);
  95. int data_fd = set_up_socket_connect(DECRYPTOR_PORT_DATA);
  96. if(data_fd <= 0)
  97. {
  98. // TODO: Proper error handling.
  99. printf("Could not set up a socket with port %d\n", DECRYPTOR_PORT_DATA);
  100. fflush(stdout);
  101. return;
  102. }
  103. int headers_fd = set_up_socket_connect(DECRYPTOR_PORT_HEADERS);
  104. if(headers_fd <= 0)
  105. {
  106. // TODO: Proper error handling.
  107. printf("Could not set up a socket with port %d\n", DECRYPTOR_PORT_HEADERS);
  108. fflush(stdout);
  109. return;
  110. }
  111. int ret_status = conduct_la_get_initial_header(data_fd, headers_fd);
  112. if(ret_status != 0) {
  113. printf("Error in deployment stage: %d.\n", ret_status);
  114. fflush(stdout);
  115. return;
  116. }
  117. // time_file_fd=open("target_time.txt", O_APPEND | O_WRONLY);
  118. }
  119. Php::Value MainLogic::php_decrypt_wrapper(Php::Parameters &params ) {
  120. std::vector <std::string> base64_fields_list, binary_fields_list, plaintext_fields_list;
  121. uint32_t ret_status;
  122. Php::Object ret_object;
  123. ret_object["success"]="false";
  124. base64_fields_list = Php::array_values(params);
  125. ret_status = decode_base64_fields_list(base64_fields_list, binary_fields_list);
  126. if(ret_status != 0)
  127. {
  128. printf("Could not perform base64 decoding correctly."); fflush(stdout);
  129. ret_object["error"]="Could not perform base64 decoding correctly.";
  130. return ret_object;
  131. }
  132. ret_status = postLaMessagingData.send_secure_msgs(binary_fields_list);
  133. if(ret_status != 0)
  134. {
  135. printf("Cannot send messages to the decryptor.\n"); fflush(stdout);
  136. ret_object["error"]="Cannot send messages to the decryptor.\n";
  137. return ret_object;
  138. }
  139. ret_status = postLaMessagingData.receive_secure_msgs(plaintext_fields_list);
  140. if(ret_status != 0)
  141. {
  142. printf("Cannot receive messages from the decryptor.\n"); fflush(stdout);
  143. ret_object["error"]="Cannot receive messages from the decryptor.\n";
  144. return ret_object;
  145. }
  146. ret_object["success"]="true";
  147. ret_object["fields"]=Php::Array(plaintext_fields_list);
  148. return ret_object;
  149. }
  150. Php::Value MainLogic::get_mitigator_header() {
  151. std::string header_value;
  152. uint32_t ret_status;
  153. if(header_refresh_counter < 100)
  154. {
  155. header_value = last_header_value;
  156. header_refresh_counter++;
  157. }
  158. else
  159. {
  160. ret_status = postLaMessagingHeaders.receive_secure_msg(header_value);
  161. if(ret_status != 0)
  162. {
  163. printf(" Cannot obtain a header from the decryptor.\n"); fflush(stdout);
  164. header_value = "!! Cannot obtain a header from the decryptor.";
  165. }
  166. header_refresh_counter = 0;
  167. }
  168. return header_value;
  169. }