|
@@ -1,7 +1,6 @@
|
|
|
//
|
|
|
// Created by miti on 2019-12-24.
|
|
|
//
|
|
|
-
|
|
|
#include "PostLAMessaging.h"
|
|
|
#include "sgx_trts.h" // for sgx_read_rand
|
|
|
#include "crypto.h" // for aes_gcm_128
|
|
@@ -46,95 +45,179 @@ void PostLAMessaging::set_la_symmetric_key(uint8_t* given_key) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-void PostLAMessaging::set_fd(int given_fd)
|
|
|
+void PostLAMessaging::set_headers_fd(int given_fd)
|
|
|
{
|
|
|
- protobufReaderWriter.set_fd(given_fd);
|
|
|
+ headersChannel.set_fd(given_fd);
|
|
|
}
|
|
|
|
|
|
-uint32_t PostLAMessaging::encrypt_decrypt_msgs(int encrypt_decrypt, std::vector<std::string>& input_msgs,
|
|
|
- std::vector<std::string>& output_msgs)
|
|
|
+void PostLAMessaging::set_data_fd(int given_fd)
|
|
|
{
|
|
|
- unsigned char *input; unsigned char *output;
|
|
|
- uint32_t input_size, output_size, ret;
|
|
|
+ dataChannel.set_fd(given_fd);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+uint32_t PostLAMessaging::encrypt_decrypt_msgs(int encrypt_decrypt, std::vector<std::vector<unsigned char>>& input_msgs,
|
|
|
+ std::vector<std::vector<unsigned char>>& output_msgs)
|
|
|
+{
|
|
|
+ unsigned char *input, *output;
|
|
|
+ uint32_t input_size, output_size, ret, counter;
|
|
|
output=NULL;
|
|
|
|
|
|
- for (std::string msg:input_msgs)
|
|
|
+ printf("In encrypt_decrypt_msgs\n"); fflush(stdout);
|
|
|
+ for (auto msg:input_msgs)
|
|
|
{
|
|
|
- input_size = msg.length();
|
|
|
- input = (unsigned char*) msg.c_str();
|
|
|
+ printf("In the loop.\n"); fflush(stdout);
|
|
|
+ input_size = msg.size();
|
|
|
+ input = (unsigned char*) &msg[0];
|
|
|
+ printf("Input length %d\n", input_size);
|
|
|
+ for(counter=0; counter<input_size; counter++)
|
|
|
+ printf("%02x ", input[counter]);
|
|
|
+ printf("\n");
|
|
|
output = (unsigned char*) realloc(output, input_size + 28); // 16 for tag, 12 for IV
|
|
|
ret = aes_gcm_wrapper(encrypt_decrypt, input, input_size, output, &output_size );
|
|
|
if(ret!=0)
|
|
|
{
|
|
|
free(output);
|
|
|
printf("Failed to encrypt an input field.\n"); fflush(stdout);
|
|
|
- return 0x2;
|
|
|
+ return ret;
|
|
|
}
|
|
|
- output_msgs.push_back(std::string(reinterpret_cast<const char *> (output), output_size));
|
|
|
+ printf("Output size %d\n", output_size);
|
|
|
+ for(counter=0; counter<output_size; counter++)
|
|
|
+ printf("%02x ", output[counter]);
|
|
|
+ printf("\n");
|
|
|
+ std::vector<unsigned char> output_field(output, output + output_size);
|
|
|
+ output_msgs.push_back(output_field);
|
|
|
}
|
|
|
- free(output);
|
|
|
-
|
|
|
+ printf("Outside da loop.\n"); fflush(stdout);
|
|
|
+ if(output != NULL)
|
|
|
+ free(output);
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
+void PostLAMessaging::create_vector_from_protobuf(decryptor_to_extension_msg &protobuf_msg,
|
|
|
+ std::vector<std::vector<unsigned char>> &output_vector)
|
|
|
+{
|
|
|
+ uint32_t counter, no_of_fields;
|
|
|
+ std::string msg;
|
|
|
|
|
|
-/*
|
|
|
- * virtual void create_vector_from_protobuf(google::protobuf::MessageLite& protobuf_msg,
|
|
|
- std::vector<std::string> &native_msg_list) {}
|
|
|
-*/
|
|
|
+ no_of_fields=protobuf_msg.fields_size();
|
|
|
+ for(counter=0; counter<no_of_fields; counter++)
|
|
|
+ {
|
|
|
+ msg = protobuf_msg.fields(counter).field();
|
|
|
+ std::vector<unsigned char> output_field(msg.begin(), msg.end());
|
|
|
+ output_vector.push_back(output_field);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
|
|
|
-uint32_t PostLAMessaging::receive_secure_msgs(std::vector<std::string> &plaintext_msg_list) {
|
|
|
- std::vector<std::string> ciphertext_msg_list;
|
|
|
+void PostLAMessaging::create_protobuf_from_vector(std::vector<std::vector<unsigned char>> &input_vector,
|
|
|
+ extension_to_decryptor_msg &protobuf_msg)
|
|
|
+{
|
|
|
+ unsigned char* input_ptr;
|
|
|
+ std::vector<unsigned char> input_field;
|
|
|
+ size_t input_field_length;
|
|
|
|
|
|
- /*google::protobuf::MessageLite protobuf_msg;
|
|
|
+ input_field = input_vector.at(0);
|
|
|
+ input_ptr = &input_field[0];
|
|
|
+ input_field_length = input_field.size();
|
|
|
+ protobuf_msg.set_ciphertext_client_public_key(input_ptr, input_field_length);
|
|
|
+ input_vector.erase(input_vector.begin());
|
|
|
|
|
|
- // read encrypted data
|
|
|
- if(!protobufReaderWriter.read_msg(protobuf_msg))
|
|
|
+ for(auto input_field : input_vector) // counter=0; counter<no_of_fields; counter++
|
|
|
{
|
|
|
- printf("Not all of the decryptor's message was read\n"); fflush(stdout);
|
|
|
- return 0xf3;
|
|
|
+ input_field_length = input_field.size();
|
|
|
+ input_ptr = &input_field[0];
|
|
|
+ protobuf_msg.add_fields()->set_field(input_ptr, input_field_length);
|
|
|
}
|
|
|
|
|
|
- create_vector_from_protobuf(protobuf_msg, ciphertext_msg_list);
|
|
|
- */
|
|
|
-
|
|
|
- return encrypt_decrypt_msgs(0, ciphertext_msg_list, plaintext_msg_list);
|
|
|
}
|
|
|
|
|
|
-uint32_t PostLAMessaging::receive_secure_msg(std::string &plaintext_msg) {
|
|
|
- std::vector<std::string> ip_msg_list;
|
|
|
- uint32_t ret_status;
|
|
|
+uint32_t PostLAMessaging::receive_data_from_decryptor(std::vector<std::vector<unsigned char>> &plaintext_msg_list) {
|
|
|
+ std::vector<std::vector<unsigned char>> ciphertext_msg_list;
|
|
|
|
|
|
- ret_status = receive_secure_msgs(ip_msg_list);
|
|
|
- if(ret_status != 0)
|
|
|
- return ret_status;
|
|
|
+ decryptor_to_extension_msg msg;
|
|
|
|
|
|
- plaintext_msg = ip_msg_list.front();
|
|
|
- return 0;
|
|
|
+ // read encrypted data
|
|
|
+ if(dataChannel.read_msg(msg) != 0)
|
|
|
+ {
|
|
|
+ printf("Not all of the decryptor's message was read\n"); fflush(stdout);
|
|
|
+ return 0xf3;
|
|
|
+ }
|
|
|
+ printf("Read the msg from the decryptor.\n"); fflush(stdout);
|
|
|
+ create_vector_from_protobuf(msg, ciphertext_msg_list);
|
|
|
+ printf("created a vector\n"); fflush(stdout);
|
|
|
+ return encrypt_decrypt_msgs(0, ciphertext_msg_list, plaintext_msg_list);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-uint32_t PostLAMessaging::send_secure_msgs(std::vector<std::string> &plaintext_msg_list)
|
|
|
+uint32_t PostLAMessaging::send_data_to_decryptor(std::vector<std::vector<unsigned char>> &plaintext_msg_list)
|
|
|
{
|
|
|
uint32_t ret;
|
|
|
- std::vector<std::string> ciphertext_msg_list;
|
|
|
+ extension_to_decryptor_msg msg;
|
|
|
+ std::vector<std::vector<unsigned char>> ciphertext_msg_list;
|
|
|
|
|
|
ret=encrypt_decrypt_msgs(1, plaintext_msg_list, ciphertext_msg_list);
|
|
|
if(ret!=0)
|
|
|
return ret;
|
|
|
-
|
|
|
- // TODO: Fix with correct protobuf references. (write)
|
|
|
- /*
|
|
|
- // write message to decryptor
|
|
|
- create_protobuf_from_vector(ciphertext_msg_list, protobuf_msg);
|
|
|
-
|
|
|
- google::protobuf::MessageLite protobuf_msg;
|
|
|
- if(!protobufReaderWriter.write_msg(protobuf_msg))
|
|
|
+ printf("About to create a protobuf vector\n"); fflush(stdout);
|
|
|
+ create_protobuf_from_vector(ciphertext_msg_list, msg);
|
|
|
+ printf("About to write the message to the data channel.\n"); fflush(stdout);
|
|
|
+ if(dataChannel.write_msg(msg) != 0)
|
|
|
{
|
|
|
printf("Not all of the client's pub key and ciphertext data was written\n"); fflush(stdout);
|
|
|
return 0xfe;
|
|
|
}
|
|
|
- */
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+uint32_t PostLAMessaging::receive_header_from_decryptor(std::string &plaintext_header) {
|
|
|
+ std::vector<std::vector<unsigned char>> ip_list, binary_list;
|
|
|
+ unsigned char* binary_ptr;
|
|
|
+ std::vector<unsigned char> binary_header;
|
|
|
+ size_t binary_header_length;
|
|
|
+ uint8_t* base64_ciphertext_header;
|
|
|
+ int base64_ciphertext_header_length;
|
|
|
+ uint32_t ret_status;
|
|
|
+
|
|
|
+ printf("About to send-receive header.\n"); fflush(stdout);
|
|
|
+
|
|
|
+ mitigator_header headerMsg;
|
|
|
+ headerMsg.set_name("Name:", 5);
|
|
|
+ headerMsg.set_value("!!Value", 7);
|
|
|
+ if(headersChannel.write_msg(headerMsg) != 0)
|
|
|
+ {
|
|
|
+ printf("Error in writing msg for headers.\n"); fflush(stdout); return 0x1;
|
|
|
+ }
|
|
|
+ printf("Wrote a placeholder message to the header.\n"); fflush(stdout);
|
|
|
+
|
|
|
+ if(headersChannel.read_msg(headerMsg) != 0)
|
|
|
+ {
|
|
|
+ printf("Error in reading msg for headers.\n"); fflush(stdout); return 0x2;
|
|
|
+ }
|
|
|
+ printf("Read the following header name and value from the decryptor:\nName:-%s-\n", headerMsg.name().c_str()); fflush(stdout);
|
|
|
+ plaintext_header = headerMsg.name();
|
|
|
+
|
|
|
+ std::vector<unsigned char> input_field(headerMsg.value().begin(), headerMsg.value().end());
|
|
|
+ ip_list.push_back(input_field);
|
|
|
+
|
|
|
+ ret_status = encrypt_decrypt_msgs(0, ip_list, binary_list);
|
|
|
+ if(ret_status != 0)
|
|
|
+ return ret_status;
|
|
|
+
|
|
|
+ binary_header = binary_list.at(0);
|
|
|
+ binary_ptr = &binary_header[0];
|
|
|
+ binary_header_length = binary_header.size();
|
|
|
+
|
|
|
+ base64_ciphertext_header = (uint8_t* ) malloc(2*binary_header_length);
|
|
|
+ base64_ciphertext_header_length = base64_encoding_wrapper((unsigned char*) binary_ptr, binary_header_length , base64_ciphertext_header);
|
|
|
+ if(base64_ciphertext_header_length <= 0)
|
|
|
+ {
|
|
|
+ printf("Error encoding the ciphertext header into base64 format.\n"); fflush(stdout); return 0x3;
|
|
|
+ }
|
|
|
+ printf("Got this header value:-%s-\n", base64_ciphertext_header);
|
|
|
+ plaintext_header.append((const char* )base64_ciphertext_header, (size_t) base64_ciphertext_header_length);
|
|
|
+ printf("Got this header:%s\n", plaintext_header.c_str()); fflush(stdout);
|
|
|
+
|
|
|
return 0;
|
|
|
}
|
|
|
|