|
@@ -1,23 +1,24 @@
|
|
|
// Knows only protobuf_sgx objects, protobuf header.
|
|
|
-#include <stdlib.h>
|
|
|
-#include <string.h>
|
|
|
-#include <errno.h>
|
|
|
+#include<stdlib.h>
|
|
|
+#include<string.h>
|
|
|
+#include<errno.h>
|
|
|
#include<unistd.h>
|
|
|
-#include <stdio.h>
|
|
|
-#include "ProtobufLAMessages.pb.h"
|
|
|
-#include <google/protobuf/io/coded_stream.h>
|
|
|
-#include <google/protobuf/io/zero_copy_stream_impl.h>
|
|
|
-using namespace google::protobuf::io;
|
|
|
-#include "SgxProtobufLAInitiator.h"
|
|
|
-#include "crypto.h"
|
|
|
+#include<stdio.h>
|
|
|
+#include"ProtobufLAMessages.pb.h"
|
|
|
+#include<google/protobuf/io/coded_stream.h>
|
|
|
+#include<google/protobuf/io/zero_copy_stream_impl.h>
|
|
|
+#include"SgxProtobufLAInitiator.h"
|
|
|
+#include"crypto.h"
|
|
|
// For socket programming
|
|
|
-#include <arpa/inet.h>
|
|
|
-#include <sys/socket.h>
|
|
|
-#include <netinet/in.h>
|
|
|
-#include <queue>
|
|
|
+#include<arpa/inet.h>
|
|
|
+#include<sys/socket.h>
|
|
|
+#include<netinet/in.h>
|
|
|
+#include<queue>
|
|
|
+using namespace google::protobuf::io;
|
|
|
|
|
|
int global_decryptor_fd;
|
|
|
-#define TOKEN_H_MAX_LENGTH 300;
|
|
|
+#define TOKEN_H_MAX_LENGTH 300
|
|
|
+uint8_t ciphertext_to_decryptor[4092];
|
|
|
|
|
|
// TODO: Make these private functions
|
|
|
int read_protobuf_msg_from_fd(int accept_fd, google::protobuf::MessageLite& message)
|
|
@@ -34,8 +35,8 @@ int read_protobuf_msg_from_fd(int accept_fd, google::protobuf::MessageLite& mess
|
|
|
fflush(stdout);
|
|
|
return -1;
|
|
|
}
|
|
|
- printf("size of msg was read to be %d \n", size);
|
|
|
- fflush(stdout);
|
|
|
+ // printf("size of msg was read to be %d \n", size);
|
|
|
+ // fflush(stdout);
|
|
|
limit = coded_input->PushLimit(size);
|
|
|
if(!message.ParseFromCodedStream(coded_input))
|
|
|
{
|
|
@@ -43,7 +44,7 @@ int read_protobuf_msg_from_fd(int accept_fd, google::protobuf::MessageLite& mess
|
|
|
fflush(stdout);
|
|
|
return -1;
|
|
|
}
|
|
|
- printf("Done parsing msg\n"); fflush(stdout);
|
|
|
+ // printf("Done parsing msg\n"); fflush(stdout);
|
|
|
coded_input->PopLimit(limit);
|
|
|
return 0;
|
|
|
}
|
|
@@ -64,7 +65,7 @@ int write_protobuf_msg_to_fd(int accept_fd, google::protobuf::MessageLite& messa
|
|
|
// TODO: There may be a better way to do this - 1) this happens with every accept now and 2) make it happen on the stack vs heap - destructor will be called on return from this function (main) and the items will then be written out. (We probably don't want that, actually)
|
|
|
delete coded_output;
|
|
|
delete raw_output;
|
|
|
- fflush(stdout);
|
|
|
+ // fflush(stdout);
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
@@ -149,6 +150,7 @@ int local_attestation_initiator(int port)
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
uint32_t post_local_attestation_get_mitigator_header(unsigned char* base64_encoded_token_H, uint32_t* base64_encoded_token_H_length)
|
|
|
{
|
|
|
unsigned char* encrypted_token_H_and_tag, *plaintext_token_H;
|
|
@@ -174,13 +176,12 @@ uint32_t post_local_attestation_get_mitigator_header(unsigned char* base64_encod
|
|
|
printf("0x%02x ", encrypted_token_H_and_tag[count]);
|
|
|
printf("\n"); fflush(stdout);
|
|
|
plaintext_token_H=(unsigned char*) malloc(encrypted_token_H_and_tag_length);
|
|
|
- // TODO: Modify aes_gcm_wrapper to do the thing where it takes care of the tag internally.
|
|
|
internal_return_status = aes_gcm_wrapper(0, encrypted_token_H_and_tag, encrypted_token_H_and_tag_length, plaintext_token_H, &plaintext_token_H_length);
|
|
|
if(internal_return_status != 0)
|
|
|
{
|
|
|
- free(encrypted_token_H_and_tag);
|
|
|
- free(plaintext_token_H);
|
|
|
- printf("Error in decryption 0x%x", internal_return_status); fflush(stdout); return internal_return_status;
|
|
|
+ free(encrypted_token_H_and_tag);
|
|
|
+ free(plaintext_token_H);
|
|
|
+ printf("Error in decryption 0x%x", internal_return_status); fflush(stdout); return internal_return_status;
|
|
|
}
|
|
|
|
|
|
printf("Decryptor's short-term public key\n");
|
|
@@ -194,19 +195,20 @@ uint32_t post_local_attestation_get_mitigator_header(unsigned char* base64_encod
|
|
|
printf("\n"); fflush(stdout);
|
|
|
|
|
|
*base64_encoded_token_H_length=base64_encoding_wrapper(base64_encoded_token_H, plaintext_token_H, plaintext_token_H_length);
|
|
|
- /*
|
|
|
+
|
|
|
// TODO: Better return value handling.
|
|
|
- if(!(base64_encoded_token_H_length <= 4 * Math.ceil(plaintext_token_H_length/3) && base64_encoded_token_H_length > 4 * (Math.ceil(plaintext_token_H_length/3) - 1)))
|
|
|
- {
|
|
|
- printf("Somehow not the entire token was encoded in base64:0x%x\n", base64_encoded_token_H_length); fflush(stdout); return 0x55;
|
|
|
- }
|
|
|
- */
|
|
|
+ //if(!(base64_encoded_token_H_length <= 4 * Math.ceil(plaintext_token_H_length/3) && base64_encoded_token_H_length > 4 * (Math.ceil(plaintext_token_H_length/3) - 1)))
|
|
|
+ //{
|
|
|
+ // printf("Somehow not the entire token was encoded in base64:0x%x\n", base64_encoded_token_H_length); fflush(stdout); return 0x55;
|
|
|
+ //}
|
|
|
+
|
|
|
free(encrypted_token_H_and_tag);
|
|
|
free(plaintext_token_H);
|
|
|
fflush(stdout);
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
uint32_t base64_decoding_on_all_client_data(unsigned char* ip_base64_client_public_key_ciphertext,
|
|
|
uint32_t ip_base64_client_public_key_ciphertext_length,
|
|
|
unsigned char* op_client_public_key_ciphertext,
|
|
@@ -218,76 +220,51 @@ uint32_t base64_decoding_on_all_client_data(unsigned char* ip_base64_client_publ
|
|
|
if(openssl_ret_status == -1)
|
|
|
return 0xfe;
|
|
|
*op_client_public_key_ciphertext_length = openssl_ret_status;
|
|
|
-*/ return 0;
|
|
|
+*/
|
|
|
+ return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-// should be called with the base64encoded public key and data to be decrypted
|
|
|
-// base64 encoded public key should be of length 4*(ceil(64/3)) = 88 (at least)
|
|
|
-int decrypt_client_data_through_decryptor( unsigned char* ip_base64_client_public_key_ciphertext,
|
|
|
- uint32_t ip_base64_client_public_key_ciphertext_length,
|
|
|
+int decrypt_client_data_through_decryptor( unsigned char* ip_client_public_key_ciphertext,
|
|
|
+ uint32_t ip_client_public_key_ciphertext_length,
|
|
|
unsigned char* op_plaintext,
|
|
|
uint32_t* op_plaintext_length
|
|
|
)
|
|
|
{
|
|
|
- unsigned char* ciphertext_to_decryptor, *ciphertext_from_decryptor;
|
|
|
- uint32_t ciphertext_to_decryptor_length, ciphertext_from_decryptor_length;
|
|
|
- uint32_t internal_return_status; uint32_t counter;
|
|
|
- protobuf_post_LA_encrypted_msg_t protobuf_encrypted_msg;
|
|
|
- std::string protobuf_encrypted_msg_string;
|
|
|
- // Encrypting to the decryptor enclave
|
|
|
- ciphertext_to_decryptor=(unsigned char*) malloc(ip_base64_client_public_key_ciphertext_length + 16);
|
|
|
- internal_return_status = aes_gcm_wrapper(1, ip_base64_client_public_key_ciphertext, ip_base64_client_public_key_ciphertext_length,
|
|
|
- ciphertext_to_decryptor, &ciphertext_to_decryptor_length );
|
|
|
- // Error checking on ciphertext that is to be sent to decryptor
|
|
|
- if(internal_return_status != 0)
|
|
|
- {
|
|
|
- free(ciphertext_to_decryptor);
|
|
|
- return internal_return_status;
|
|
|
- }
|
|
|
+ uint32_t ciphertext_to_decryptor_length;
|
|
|
+ uint32_t internal_return_status;
|
|
|
+ protobuf_post_LA_encrypted_msg_t protobuf_encrypted_msg;
|
|
|
+ std::string protobuf_encrypted_msg_string;
|
|
|
+ uint32_t counter;
|
|
|
+ // Encrypting to the decryptor enclave
|
|
|
+ internal_return_status = aes_gcm_wrapper(1, ip_client_public_key_ciphertext, ip_client_public_key_ciphertext_length, ciphertext_to_decryptor, &ciphertext_to_decryptor_length );
|
|
|
+ // Error checking on ciphertext that is to be sent to decryptor
|
|
|
+ if(internal_return_status != 0)
|
|
|
+ {
|
|
|
+ printf("Error in encrypting content to the decryptor.\n"); fflush(stdout);
|
|
|
+ return internal_return_status;
|
|
|
+ }
|
|
|
|
|
|
-/* printf("Sending this ciphertext and tag to the decryptor enclave:\n");
|
|
|
- for(counter=0;counter< ciphertext_to_decryptor_length;counter++)
|
|
|
- printf("0x%02x ", ciphertext_to_decryptor[counter]);
|
|
|
- printf("\n"); fflush(stdout);
|
|
|
-*/
|
|
|
- // write message to decryptor
|
|
|
- protobuf_encrypted_msg.set_msg( ciphertext_to_decryptor , ciphertext_to_decryptor_length );
|
|
|
- free(ciphertext_to_decryptor);
|
|
|
+ // write message to decryptor
|
|
|
+ protobuf_encrypted_msg.set_msg( ciphertext_to_decryptor , ciphertext_to_decryptor_length );
|
|
|
if(write_protobuf_msg_to_fd(global_decryptor_fd, protobuf_encrypted_msg)!=0)
|
|
|
{
|
|
|
- // printf("Not all of the client's pub key and ciphertext data was written\n"); fflush(stdout);
|
|
|
+ printf("Not all of the client's pub key and ciphertext data was written\n"); fflush(stdout);
|
|
|
return 0xfe;
|
|
|
}
|
|
|
|
|
|
// clear the message before setting it again
|
|
|
protobuf_encrypted_msg.clear_msg();
|
|
|
|
|
|
- // read encrypted data
|
|
|
-// printf("Reading msg from decryptor"); fflush(stdout);
|
|
|
+ // read encrypted data
|
|
|
if(read_protobuf_msg_from_fd(global_decryptor_fd, protobuf_encrypted_msg)!=0)
|
|
|
{
|
|
|
- // printf("Not all of the decryptor's message was read\n"); fflush(stdout);
|
|
|
+ printf("Not all of the decryptor's message was read\n"); fflush(stdout);
|
|
|
return 0xf3;
|
|
|
}
|
|
|
-// printf("Done reading msg from decryptor\n"); fflush(stdout);
|
|
|
-
|
|
|
- protobuf_encrypted_msg_string=std::string(protobuf_encrypted_msg.msg());
|
|
|
- ciphertext_from_decryptor_length=protobuf_encrypted_msg_string.length();
|
|
|
- ciphertext_from_decryptor = (unsigned char*) malloc(ciphertext_from_decryptor_length);
|
|
|
- memcpy(ciphertext_from_decryptor, protobuf_encrypted_msg_string.c_str(), protobuf_encrypted_msg_string.length());
|
|
|
-
|
|
|
-/* printf("Got this ciphertext and tag from the decryptor enclave");
|
|
|
- for(counter=0;counter< ciphertext_from_decryptor_length;counter++)
|
|
|
- printf("0x%02x ", ciphertext_from_decryptor[counter]);
|
|
|
- printf("\n");
|
|
|
-*/
|
|
|
- // Decrypt data from decryptor
|
|
|
- internal_return_status = aes_gcm_wrapper(0, ciphertext_from_decryptor, ciphertext_from_decryptor_length,
|
|
|
- op_plaintext, op_plaintext_length
|
|
|
- );
|
|
|
- free(ciphertext_from_decryptor);
|
|
|
- return internal_return_status;
|
|
|
|
|
|
+ protobuf_encrypted_msg_string=std::string(protobuf_encrypted_msg.msg());
|
|
|
+ // Decrypt data from decryptor
|
|
|
+ internal_return_status = aes_gcm_wrapper(0, (unsigned char*) protobuf_encrypted_msg.msg().c_str(), protobuf_encrypted_msg_string.length(), op_plaintext, op_plaintext_length);
|
|
|
+ return internal_return_status;
|
|
|
}
|