|
@@ -14,7 +14,7 @@
|
|
#include <netinet/in.h>
|
|
#include <netinet/in.h>
|
|
|
|
|
|
#define DECRYPTOR_PORT_DATA 3825
|
|
#define DECRYPTOR_PORT_DATA 3825
|
|
-#define DECRYPTOR_PORT_HEADERS 3830
|
|
|
|
|
|
+#define DECRYPTOR_PORT_HEADERS 3826
|
|
|
|
|
|
// Sets up a socket connected to the port passed as input - returns the socket FD on success and -1 on error.
|
|
// 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.
|
|
// Also prints the errno on error.
|
|
@@ -49,10 +49,20 @@ int MainLogic::set_up_socket_connect(int port)
|
|
return sock;
|
|
return sock;
|
|
}
|
|
}
|
|
|
|
|
|
-int MainLogic::conduct_la_get_initial_header(int data_fd, int headers_fd)
|
|
|
|
|
|
+int MainLogic::conduct_la()
|
|
{
|
|
{
|
|
uint8_t key[16];
|
|
uint8_t key[16];
|
|
uint32_t ret_status;
|
|
uint32_t ret_status;
|
|
|
|
+ int data_fd;
|
|
|
|
+
|
|
|
|
+ data_fd = set_up_socket_connect(DECRYPTOR_PORT_DATA);
|
|
|
|
+ printf("About to setup socket.\n");
|
|
|
|
+ if(data_fd <= 0)
|
|
|
|
+ {
|
|
|
|
+ printf("Could not set up a socket with port %d\n", DECRYPTOR_PORT_DATA);
|
|
|
|
+ fflush(stdout);
|
|
|
|
+ return 0x1;
|
|
|
|
+ }
|
|
|
|
|
|
// Conduct LA.
|
|
// Conduct LA.
|
|
ret_status = laInitiator.conduct_la(data_fd);
|
|
ret_status = laInitiator.conduct_la(data_fd);
|
|
@@ -67,69 +77,93 @@ int MainLogic::conduct_la_get_initial_header(int data_fd, int headers_fd)
|
|
printf("Setting up fds for post LA messaging.\n");
|
|
printf("Setting up fds for post LA messaging.\n");
|
|
fflush(stdout);
|
|
fflush(stdout);
|
|
// Use the same channel for sending client data as the one used above for LA messages.
|
|
// 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);
|
|
|
|
|
|
+ postLaMessaging.set_data_fd(data_fd);
|
|
|
|
|
|
// Use the symmetric key from LA to send messages for the rest of the deployment stage.
|
|
// Use the symmetric key from LA to send messages for the rest of the deployment stage.
|
|
- printf("Retrieving key from LA inititator.\n ");
|
|
|
|
|
|
+ printf("Retrieving key from LA inititator.\n");
|
|
fflush(stdout);
|
|
fflush(stdout);
|
|
laInitiator.get_la_symmetric_key(key);
|
|
laInitiator.get_la_symmetric_key(key);
|
|
- printf("Setting key for post LA messaging for both data and headers channels.\n ");
|
|
|
|
|
|
+ printf("Setting key for post LA messaging for both data and headers channels.\n");
|
|
fflush(stdout);
|
|
fflush(stdout);
|
|
- postLaMessagingData.set_la_symmetric_key(key);
|
|
|
|
- postLaMessagingHeaders.set_la_symmetric_key(key);
|
|
|
|
|
|
+ postLaMessaging.set_la_symmetric_key(key);
|
|
|
|
|
|
- // Mitigator-Public-Key:
|
|
|
|
- return postLaMessagingHeaders.receive_secure_msg(last_header_value);
|
|
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+int MainLogic::get_initial_headers()
|
|
|
|
+{
|
|
|
|
+ int headers_fd, ret_status;
|
|
|
|
+
|
|
|
|
+ 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 0x1;
|
|
|
|
+ }
|
|
|
|
+ printf("Set up a socket to receive headers from the decryptor.\n"); fflush(stdout);
|
|
|
|
+
|
|
|
|
+ // Use a different channel for receiving headers asynchronously.
|
|
|
|
+ postLaMessaging.set_headers_fd(headers_fd);
|
|
|
|
+
|
|
|
|
+ ret_status = postLaMessaging.receive_header_from_decryptor(last_header_value);
|
|
|
|
+ if(ret_status != 0)
|
|
|
|
+ {
|
|
|
|
+ printf("Could not receive the header from the decryptor. Had error %d\n", ret_status);
|
|
|
|
+ fflush(stdout);
|
|
|
|
+ }
|
|
|
|
+ return 0;
|
|
}
|
|
}
|
|
|
|
|
|
-int MainLogic::decode_base64_fields_list(std::vector <std::string> &base64_fields_list, std::vector <std::string> &binary_fields_list)
|
|
|
|
|
|
+int MainLogic::decode_base64_fields_list(std::vector <std::string> &base64_fields_list, std::vector <std::vector<unsigned char>> &binary_fields_list)
|
|
{
|
|
{
|
|
- uint32_t binary_field_size, base64_field_size;
|
|
|
|
|
|
+ int binary_field_size, base64_field_size, counter;
|
|
const char* base64_field_ptr;
|
|
const char* base64_field_ptr;
|
|
unsigned char* binary_field_ptr = NULL;
|
|
unsigned char* binary_field_ptr = NULL;
|
|
|
|
|
|
|
|
+ printf("In the base64 decode list function.\n"); fflush(stdout);
|
|
for (auto &base64_field : base64_fields_list)
|
|
for (auto &base64_field : base64_fields_list)
|
|
{
|
|
{
|
|
base64_field_size = base64_field.size();
|
|
base64_field_size = base64_field.size();
|
|
base64_field_ptr = base64_field.c_str();
|
|
base64_field_ptr = base64_field.c_str();
|
|
// upper limit - the binary data will always be smaller than this (base64 length ~= 4/3 * binary length)
|
|
// 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_ptr = (unsigned char*) realloc(binary_field_ptr, base64_field_size);
|
|
|
|
+ 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);
|
|
binary_field_size = base64_decoding_wrapper(base64_field_ptr, base64_field_size, binary_field_ptr);
|
|
binary_field_size = base64_decoding_wrapper(base64_field_ptr, base64_field_size, binary_field_ptr);
|
|
|
|
+ printf("Called the decoder and got the following size: %d.\n", binary_field_size );
|
|
if(binary_field_size <= 0)
|
|
if(binary_field_size <= 0)
|
|
{
|
|
{
|
|
|
|
+ printf("Had an error in decoding.\n"); fflush(stdout);
|
|
free(binary_field_ptr);
|
|
free(binary_field_ptr);
|
|
return 0x1;
|
|
return 0x1;
|
|
}
|
|
}
|
|
- binary_fields_list.push_back(std::string(reinterpret_cast<const char*> (binary_field_ptr), binary_field_size));
|
|
|
|
|
|
+ std::vector<unsigned char> binary_field(binary_field_ptr, binary_field_ptr + binary_field_size);
|
|
|
|
+ binary_fields_list.push_back(binary_field);
|
|
|
|
+ }
|
|
|
|
+ free(binary_field_ptr);
|
|
|
|
+ for (auto binaryField : binary_fields_list)
|
|
|
|
+ {
|
|
|
|
+ for(auto myByte : binaryField)
|
|
|
|
+ printf("%02x ", myByte);
|
|
|
|
+ printf("\n");
|
|
}
|
|
}
|
|
|
|
+
|
|
return 0;
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
|
|
void MainLogic::deployment_stage() {
|
|
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);
|
|
|
|
|
|
+ int ret_status = conduct_la();
|
|
|
|
+ if(ret_status != 0) {
|
|
|
|
+ printf("Error in conducting LA: %d.\n", ret_status);
|
|
fflush(stdout);
|
|
fflush(stdout);
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
- int headers_fd = set_up_socket_connect(DECRYPTOR_PORT_HEADERS);
|
|
|
|
- if(headers_fd <= 0)
|
|
|
|
|
|
+ ret_status = get_initial_headers();
|
|
|
|
+ if(ret_status !=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);
|
|
|
|
|
|
+ printf("Error in getting initial headers: %d.\n", ret_status);
|
|
fflush(stdout);
|
|
fflush(stdout);
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
@@ -138,21 +172,31 @@ void MainLogic::deployment_stage() {
|
|
}
|
|
}
|
|
|
|
|
|
Php::Value MainLogic::php_decrypt_wrapper(Php::Parameters ¶ms ) {
|
|
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;
|
|
|
|
|
|
+ std::vector <std::string> base64_fields_list;
|
|
|
|
+ std::vector <std::vector<unsigned char>> binary_fields_list, plaintext_fields_list;
|
|
|
|
+ uint32_t ret_status, no_of_fields, counter, counter2;
|
|
Php::Object ret_object;
|
|
Php::Object ret_object;
|
|
ret_object["success"]="false";
|
|
ret_object["success"]="false";
|
|
|
|
|
|
- base64_fields_list = Php::array_values(params);
|
|
|
|
|
|
+ no_of_fields = params[0].size();
|
|
|
|
+ printf("Received client's ciphertext fields, performing base 64 decoding.\n"); fflush(stdout);
|
|
|
|
+ base64_fields_list = params[0]; //Php::array_values(Php::array_values(params)[0]);
|
|
|
|
+ printf("In the base64 decode list function.\n"); fflush(stdout);
|
|
|
|
+ /*
|
|
|
|
+ printf("Field: \n");
|
|
|
|
+ for(counter2=0; counter2<base64_fields_list[counter].length(); counter2++)
|
|
|
|
+ printf("%02x ", base64_fields_list[counter][counter2]);
|
|
|
|
+ */
|
|
ret_status = decode_base64_fields_list(base64_fields_list, binary_fields_list);
|
|
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.";
|
|
|
|
|
|
+ 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;
|
|
return ret_object;
|
|
}
|
|
}
|
|
|
|
|
|
- ret_status = postLaMessagingData.send_secure_msgs(binary_fields_list);
|
|
|
|
|
|
+ printf("Sending data to the decryptor.\n"); fflush(stdout);
|
|
|
|
+ ret_status = postLaMessaging.send_data_to_decryptor(binary_fields_list);
|
|
if(ret_status != 0)
|
|
if(ret_status != 0)
|
|
{
|
|
{
|
|
printf("Cannot send messages to the decryptor.\n"); fflush(stdout);
|
|
printf("Cannot send messages to the decryptor.\n"); fflush(stdout);
|
|
@@ -160,7 +204,8 @@ Php::Value MainLogic::php_decrypt_wrapper(Php::Parameters ¶ms ) {
|
|
return ret_object;
|
|
return ret_object;
|
|
}
|
|
}
|
|
|
|
|
|
- ret_status = postLaMessagingData.receive_secure_msgs(plaintext_fields_list);
|
|
|
|
|
|
+ printf("Receiving data from the decryptor.\n"); fflush(stdout);
|
|
|
|
+ ret_status = postLaMessaging.receive_data_from_decryptor(plaintext_fields_list);
|
|
if(ret_status != 0)
|
|
if(ret_status != 0)
|
|
{
|
|
{
|
|
printf("Cannot receive messages from the decryptor.\n"); fflush(stdout);
|
|
printf("Cannot receive messages from the decryptor.\n"); fflush(stdout);
|
|
@@ -169,7 +214,13 @@ Php::Value MainLogic::php_decrypt_wrapper(Php::Parameters ¶ms ) {
|
|
}
|
|
}
|
|
ret_object["success"]="true";
|
|
ret_object["success"]="true";
|
|
ret_object["fields"]=Php::Array(plaintext_fields_list);
|
|
ret_object["fields"]=Php::Array(plaintext_fields_list);
|
|
-
|
|
|
|
|
|
+ printf("Returning back to good old php.\n"); fflush(stdout);
|
|
|
|
+ for (auto binaryField : plaintext_fields_list)
|
|
|
|
+ {
|
|
|
|
+ for(auto myByte : binaryField)
|
|
|
|
+ printf("%c", myByte);
|
|
|
|
+ printf("\n");
|
|
|
|
+ }
|
|
return ret_object;
|
|
return ret_object;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -183,7 +234,7 @@ Php::Value MainLogic::get_mitigator_header() {
|
|
}
|
|
}
|
|
else
|
|
else
|
|
{
|
|
{
|
|
- ret_status = postLaMessagingHeaders.receive_secure_msg(header_value);
|
|
|
|
|
|
+ ret_status = postLaMessaging.receive_header_from_decryptor(header_value);
|
|
if(ret_status != 0)
|
|
if(ret_status != 0)
|
|
{
|
|
{
|
|
printf(" Cannot obtain a header from the decryptor.\n"); fflush(stdout);
|
|
printf(" Cannot obtain a header from the decryptor.\n"); fflush(stdout);
|
|
@@ -191,5 +242,6 @@ Php::Value MainLogic::get_mitigator_header() {
|
|
}
|
|
}
|
|
header_refresh_counter = 0;
|
|
header_refresh_counter = 0;
|
|
}
|
|
}
|
|
|
|
+ printf("Returning this header value:%s\n", header_value.c_str()); fflush(stdout);
|
|
return header_value;
|
|
return header_value;
|
|
}
|
|
}
|