123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- //
- // Created by miti on 2019-12-28.
- //
- #include "MainLogic.h"
- #include "crypto.h"
- #include "stdio.h"
- #include <errno.h>
- #include <string.h>
- // For socket programming
- #include <arpa/inet.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #define DECRYPTOR_PORT_DATA 3825
- #define DECRYPTOR_PORT_HEADERS 3830
- // Sets up a socket connected to the port passed as input - returns the socket FD on success and -1 on error.
- // Also prints the errno on error.
- int MainLogic::set_up_socket_connect(int port)
- {
- int sock = 0;
- if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
- {
- printf("\n Error in socket call - errno is %d \n", errno); fflush(stdout);
- return -1;
- }
- struct sockaddr_in serv_addr;
- memset(&serv_addr, '0', sizeof(serv_addr));
- serv_addr.sin_family = AF_INET;
- serv_addr.sin_port = htons(port);
- // Convert IPv4 and IPv6 addresses from text to binary form
- if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0)
- {
- printf("\nError in inet_pton - errno is %d\n", errno); fflush(stdout);
- return -1;
- }
- if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
- {
- printf("\nError in connect - errno is %d \n", errno); fflush(stdout);
- return -1;
- }
- return sock;
- }
- int MainLogic::conduct_la_get_initial_header(int data_fd, int headers_fd)
- {
- uint8_t key[16];
- uint32_t ret_status;
- // Conduct LA.
- ret_status = laInitiator.conduct_la(data_fd);
- if (ret_status != 0) {
- printf("LA initiator returned an error: %d\n", ret_status);
- fflush(stdout);
- return ret_status;
- }
- printf("\nSuccessful LA with port %d.\n", data_fd);
- fflush(stdout);
- printf("Setting up fds for post LA messaging.\n");
- fflush(stdout);
- // Use the same channel for sending client data as the one used above for LA messages.
- postLaMessagingData.set_fd(data_fd);
- // Use a different channel for receiving headers asynchronously.
- postLaMessagingHeaders.set_fd(headers_fd);
- // Use the symmetric key from LA to send messages for the rest of the deployment stage.
- printf("Retrieving key from LA inititator.\n ");
- fflush(stdout);
- laInitiator.get_la_symmetric_key(key);
- printf("Setting key for post LA messaging for both data and headers channels.\n ");
- fflush(stdout);
- postLaMessagingData.set_la_symmetric_key(key);
- postLaMessagingHeaders.set_la_symmetric_key(key);
- // Mitigator-Public-Key:
- return postLaMessagingHeaders.receive_secure_msg(last_header_value);
- }
- int MainLogic::decode_base64_fields_list(std::vector <std::string> &base64_fields_list, std::vector <std::string> &binary_fields_list)
- {
- uint32_t binary_field_size, base64_field_size;
- const char* base64_field_ptr;
- unsigned char* binary_field_ptr = NULL;
- for (auto &base64_field : base64_fields_list)
- {
- base64_field_size = base64_field.size();
- base64_field_ptr = base64_field.c_str();
- // upper limit - the binary data will always be smaller than this (base64 length ~= 4/3 * binary length)
- binary_field_ptr = (unsigned char*) realloc(binary_field_ptr, base64_field_size);
- binary_field_size = base64_decoding_wrapper(base64_field_ptr, base64_field_size, binary_field_ptr);
- if(binary_field_size <= 0)
- {
- free(binary_field_ptr);
- return 0x1;
- }
- binary_fields_list.push_back(std::string(reinterpret_cast<const char*> (binary_field_ptr), binary_field_size));
- }
- return 0;
- }
- void MainLogic::deployment_stage() {
- setbuf(stdout,NULL);
- int data_fd = set_up_socket_connect(DECRYPTOR_PORT_DATA);
- if(data_fd <= 0)
- {
- // TODO: Proper error handling.
- printf("Could not set up a socket with port %d\n", DECRYPTOR_PORT_DATA);
- fflush(stdout);
- return;
- }
- int headers_fd = set_up_socket_connect(DECRYPTOR_PORT_HEADERS);
- if(headers_fd <= 0)
- {
- // TODO: Proper error handling.
- printf("Could not set up a socket with port %d\n", DECRYPTOR_PORT_HEADERS);
- fflush(stdout);
- return;
- }
- int ret_status = conduct_la_get_initial_header(data_fd, headers_fd);
- if(ret_status != 0) {
- printf("Error in deployment stage: %d.\n", ret_status);
- fflush(stdout);
- return;
- }
- // time_file_fd=open("target_time.txt", O_APPEND | O_WRONLY);
- }
- Php::Value MainLogic::php_decrypt_wrapper(Php::Parameters ¶ms ) {
- std::vector <std::string> base64_fields_list, binary_fields_list, plaintext_fields_list;
- uint32_t ret_status;
- Php::Object ret_object;
- ret_object["success"]="false";
- base64_fields_list = Php::array_values(params);
- ret_status = decode_base64_fields_list(base64_fields_list, binary_fields_list);
- if(ret_status != 0)
- {
- printf("Could not perform base64 decoding correctly."); fflush(stdout);
- ret_object["error"]="Could not perform base64 decoding correctly.";
- return ret_object;
- }
- ret_status = postLaMessagingData.send_secure_msgs(binary_fields_list);
- if(ret_status != 0)
- {
- printf("Cannot send messages to the decryptor.\n"); fflush(stdout);
- ret_object["error"]="Cannot send messages to the decryptor.\n";
- return ret_object;
- }
- ret_status = postLaMessagingData.receive_secure_msgs(plaintext_fields_list);
- if(ret_status != 0)
- {
- printf("Cannot receive messages from the decryptor.\n"); fflush(stdout);
- ret_object["error"]="Cannot receive messages from the decryptor.\n";
- return ret_object;
- }
- ret_object["success"]="true";
- ret_object["fields"]=Php::Array(plaintext_fields_list);
- return ret_object;
- }
- Php::Value MainLogic::get_mitigator_header() {
- std::string header_value;
- uint32_t ret_status;
- if(header_refresh_counter < 100)
- {
- header_value = last_header_value;
- header_refresh_counter++;
- }
- else
- {
- ret_status = postLaMessagingHeaders.receive_secure_msg(header_value);
- if(ret_status != 0)
- {
- printf(" Cannot obtain a header from the decryptor.\n"); fflush(stdout);
- header_value = "!! Cannot obtain a header from the decryptor.";
- }
- header_refresh_counter = 0;
- }
- return header_value;
- }
|