Browse Source

Modified untrusted application to work with the decryptor calls.

Added in new files for marshalling/unmarshalling protobufs from arrays.
dettanym 6 years ago
parent
commit
cfc9ad32e1

+ 25 - 81
App/LocalAttestationUntrusted.cpp

@@ -35,72 +35,12 @@ namespace LocalAttestationUntrusted {
         }
     }
 
-    /*
-    void get_lengths_for_protobuf_serialized_array(extension_to_decryptor_enclosed_msg &protobuf_ext_to_decryptor,
-        uint32_t *output_lengths)
-    {
-        uint32_t number_of_ciphertext_fields, counter, total_length;
-        // Didn't use bytesize() or bytesizelong() for getting the lengths of the public key or the ciphertext string
-        // as that gives the *serialized* length of the message which should be an upper-bound.
-        // Can switch to that if necessary for time performance reasons.
-        total_length=protobuf_ext_to_decryptor.ciphertext_client_public_key().length();
-        number_of_ciphertext_fields=protobuf_ext_to_decryptor.ciphertext_fields_size();
-        for(counter=0; counter<number_of_ciphertext_fields; counter++)
-            total_length+=protobuf_ext_to_decryptor.ciphertext_fields(counter).field().length();
-        output_lengths[0]=total_length;
-        output_lengths[1]=number_of_ciphertext_fields;
-    }
-
-    void create_array_from_protobuf(extension_to_decryptor_enclosed_msg &protobuf_ext_to_decryptor,
-                                    unsigned char* double_ciphertext_client_data, uint32_t* sizes_array, uint32_t* sizes_array_length)
-    {
-        uint32_t counter, size_of_field, number_of_fields;
-        unsigned char* ptr;
-
-        // Set the size of the first element - the public key - and copy it to the output array.
-        sizes_array[0] = protobuf_ext_to_decryptor.ciphertext_client_public_key().length();
-        ptr=strncpy((char*)double_ciphertext_client_data, protobuf_ext_to_decryptor.ciphertext_client_public_key().c_str(),
-            size_of_field);
-
-        // Start copying past the length copied above, copy all ciphertext fields to the output string array
-        // and set their lengths in the output integers array
-        number_of_fields=protobuf_ext_to_decryptor.ciphertext_fields_size();
-        for(counter=0;counter<number_of_fields;counter++)
-        {
-            // First element of the LHS array is the length of the client's public key.
-            sizes_array[counter+1] = protobuf_ext_to_decryptor.ciphertext_fields(counter).field().length();
-            ptr = strncpy((char*)ptr, protobuf_ext_to_decryptor.ciphertext_fields(counter).field().c_str(), size_of_field);
-        }
-        sizes_array_length=number_of_fields+1;
-    }
-
-    void create_protobuf_from_array(   unsigned char* ciphertext_client_data, uint32_t* sizes_array, uint32_t sizes_array_length,
-            extension_to_decryptor_enclosed_msg protobuf_extension_decryptor_msg)
-    {
-        uint32_t counter;
-        void* ptr;
-
-        // Note that we don't care about setting the client public key as we don't include that in the outgoing message
-        // to the extension.
-        ptr=ciphertext_client_data;
-        for(counter=0; counter<sizes_array_length; counter++)
-        {
-            protobuf_extension_decryptor_msg.mutable_ciphertext_fields(counter).set_field(ptr, sizes_array[counter]);
-            ptr+=sizes_array[counter];
-        }
-    }
-    */
-    uint32_t get_decrypted_client_data(uint8_t* array, uint32_t array_length)
-        {
-            return 0;
-        }
-
     int decrypt_client_data(uint32_t own_enclave_id, int fd, int time_file_fd) {
-    /*    extension_to_decryptor_enclosed_msg protobuf_extension_decryptor_msg;
-        unsigned char* double_ciphertext_client_data, ciphertext_client_data;
-        uint32_t* input_sizes_array, output_sizes_array;
-        uint32_t ecall_input_lengths[2];
-        uint32_t ecall_output_lengths[2];
+        extension_to_decryptor_enclosed_msg protobuf_extension_decryptor_msg;
+        unsigned char *double_ciphertext, *ciphertext;
+        uint32_t *input_sizes_array, *output_sizes_array;
+        uint32_t double_ciphertext_length;
+        uint32_t number_of_double_ciphertext_fields;
         uint32_t sgx_ret;
 
         // Get a message of the type decryptor_to_extension msg
@@ -110,40 +50,44 @@ namespace LocalAttestationUntrusted {
             return 0xf3;
         }
 
-        get_length_of_protobuf_serialized_array(protobuf_extension_decryptor_msg, lengths);
-        double_ciphertext_client_data=(unsigned char*) malloc(ecall_input_lengths[0]);
-        ciphertext_client_data = (unsigned char*) malloc(ecall_input_lengths[0]);
+        PostLAProtobufNativeTransforms::get_lengths_for_protobuf_serialized_array(protobuf_extension_decryptor_msg, &double_ciphertext_length,
+                &number_of_double_ciphertext_fields);
 
-        input_sizes_array = (uint32_t*) malloc(ecall_input_lengths[1] * sizeof(uint32_t));
+        double_ciphertext=(unsigned char*) malloc(double_ciphertext_length);
+        ciphertext = (unsigned char*) malloc(double_ciphertext_length);
 
-        create_array_from_protobuf(protobuf_extension_decryptor_msg, double_ciphertext_client_data,
-            input_sizes_array, ecall_input_lengths[1]);
+        input_sizes_array = (uint32_t*) malloc(number_of_double_ciphertext_fields * sizeof(uint32_t));
+        output_sizes_array = (uint32_t*) malloc(number_of_double_ciphertext_fields * sizeof(uint32_t));
+        PostLAProtobufNativeTransforms::create_array_from_protobuf(protobuf_extension_decryptor_msg, double_ciphertext,
+            input_sizes_array, &number_of_double_ciphertext_fields);
 
         // Call the enclave's decryption function with these arguments and get back another vector of ciphertexts.
-        Decryptor_decrypt_client_data_wrapper(own_enclave_id, &sgx_ret,
-                double_ciphertext_client_data,
-                ecall_input_lengths[0],
+        Decryptor_process_apache_message_generate_response_wrapper(own_enclave_id, &sgx_ret,
+                double_ciphertext,
+                double_ciphertext_length,
                 input_sizes_array,
-                ecall_input_lengths[1]);
+                number_of_double_ciphertext_fields,
+                ciphertext,
+                output_sizes_array);
 
-        free(double_ciphertext_client_data);
+        free(double_ciphertext);
         free(input_sizes_array);
 
         // Error checking
         if(!sgx_ret)
         {
-            free(ciphertext_client_data);
+            free(ciphertext);
             free(output_sizes_array);
             return 0x32;
         }
-        output_sizes_array = (uint32_t *) malloc(ecall_input_lengths[1] * sizeof(uint32_t)); // Upper bound: it should be lengths[1]-1.
 
         // Clear the protobuf msg above and reset it with the output arguments of the ecall.
         protobuf_extension_decryptor_msg.clear_ciphertext_client_public_key();
         protobuf_extension_decryptor_msg.clear_ciphertext_fields();
-        create_protobuf_from_array(ciphertext_client_data, output_sizes_array, ecall_output_lengths[1],
-         protobuf_extension_decryptor_msg);
-        free(ciphertext_client_data);
+        PostLAProtobufNativeTransforms::create_protobuf_from_array(ciphertext, output_sizes_array,
+                number_of_double_ciphertext_fields,
+                protobuf_extension_decryptor_msg);
+        free(ciphertext);
         free(output_sizes_array);
 
         // write message to apache extension

+ 63 - 0
App/PostLAProtobufNativeTransforms.cpp

@@ -0,0 +1,63 @@
+//
+// Created by miti on 27/07/19.
+//
+
+#include "UntrustedInclude/PostLAProtobufNativeTransforms.h"
+#include <string.h>
+
+void PostLAProtobufNativeTransforms::get_lengths_for_protobuf_serialized_array(extension_to_decryptor_enclosed_msg &protobuf_ext_to_decryptor,
+                                               uint32_t *op_ciphertext_length, uint32_t *op_number_of_ciphertext_fields)
+{
+    uint32_t number_of_ciphertext_fields, counter, total_length;
+    // Didn't use bytesize() or bytesizelong() for getting the lengths of the public key or the ciphertext string
+    // as that gives the *serialized* length of the message which should be an upper-bound.
+    // Can switch to that if necessary for time performance reasons.
+    total_length=protobuf_ext_to_decryptor.ciphertext_client_public_key().length();
+    number_of_ciphertext_fields=protobuf_ext_to_decryptor.ciphertext_fields_size();
+    for(counter=0; counter<number_of_ciphertext_fields; counter++)
+        total_length+=protobuf_ext_to_decryptor.ciphertext_fields(counter).field().length();
+    *op_ciphertext_length=total_length;
+    *op_number_of_ciphertext_fields=number_of_ciphertext_fields;
+}
+
+void PostLAProtobufNativeTransforms::create_array_from_protobuf(extension_to_decryptor_enclosed_msg &protobuf_ext_to_decryptor,
+                                unsigned char* double_ciphertext_client_data, uint32_t* sizes_array,
+                                uint32_t* sizes_array_length)
+{
+    uint32_t counter, size_of_field, number_of_fields;
+    unsigned char* ptr;
+
+    // Set the size of the first element - the public key - and copy it to the output array.
+    sizes_array[0] = protobuf_ext_to_decryptor.ciphertext_client_public_key().length();
+    memcpy(double_ciphertext_client_data, protobuf_ext_to_decryptor.ciphertext_client_public_key().c_str(),
+           sizes_array[0]);
+    ptr=double_ciphertext_client_data + sizes_array[0];
+    // Start copying past the length copied above, copy all ciphertext fields to the output string array
+    // and set their lengths in the output integers array
+    number_of_fields=protobuf_ext_to_decryptor.ciphertext_fields_size();
+    for(counter=0;counter<number_of_fields;counter++)
+    {
+        // First element of the LHS array is the length of the client's public key.
+        size_of_field = protobuf_ext_to_decryptor.ciphertext_fields(counter).field().length();
+        memcpy(ptr, protobuf_ext_to_decryptor.ciphertext_fields(counter).field().c_str(), size_of_field);
+        sizes_array[counter + 1] = size_of_field;
+        ptr += sizes_array[counter + 1];
+    }
+    *sizes_array_length=number_of_fields+1;
+}
+
+void PostLAProtobufNativeTransforms::create_protobuf_from_array(   unsigned char* ciphertext_client_data, uint32_t* sizes_array, uint32_t sizes_array_length,
+                                   extension_to_decryptor_enclosed_msg protobuf_extension_decryptor_msg)
+{
+    uint32_t counter;
+    unsigned char* ptr;
+
+    // Note that we don't care about setting the client public key as we don't include that in the outgoing message
+    // to the extension.
+    ptr=ciphertext_client_data;
+    for(counter=0; counter<sizes_array_length; counter++)
+    {
+        protobuf_extension_decryptor_msg.mutable_ciphertext_fields(counter)->set_field(ptr, sizes_array[counter]);
+        ptr+=sizes_array[counter];
+    }
+}

+ 1 - 0
App/UntrustedInclude/LocalAttestationUntrusted.h

@@ -5,6 +5,7 @@
 #include <stdio.h>
 #include "Ipc.h"
 #include "protobufLAInitiator.h"
+#include "PostLAProtobufNativeTransforms.h"
 #include "protobufReadWrite.h"
 #include "PostLAMessages.pb.h"
 #include "FileIO.h"

+ 25 - 0
App/UntrustedInclude/PostLAProtobufNativeTransforms.h

@@ -0,0 +1,25 @@
+//
+// Created by miti on 27/07/19.
+//
+
+#ifndef DECRYPTORAPP_POSTLAPROTOBUFNATIVETRANSFORMS_H
+#define DECRYPTORAPP_POSTLAPROTOBUFNATIVETRANSFORMS_H
+#include "PostLAMessages.pb.h"
+
+namespace PostLAProtobufNativeTransforms {
+    void get_lengths_for_protobuf_serialized_array(extension_to_decryptor_enclosed_msg &protobuf_ext_to_decryptor,
+                                                                                   uint32_t *op_ciphertext_length,
+                                                                                   uint32_t *op_number_of_ciphertext_fields);
+
+    void create_array_from_protobuf(extension_to_decryptor_enclosed_msg &protobuf_ext_to_decryptor,
+                                                                    unsigned char* double_ciphertext_client_data,
+                                                                    uint32_t* sizes_array,
+                                                                    uint32_t* sizes_array_length);
+
+    void create_protobuf_from_array(   unsigned char* ciphertext_client_data, uint32_t* sizes_array,
+            uint32_t sizes_array_length,
+            extension_to_decryptor_enclosed_msg protobuf_extension_decryptor_msg);
+};
+
+
+#endif //DECRYPTORAPP_POSTLAPROTOBUFNATIVETRANSFORMS_H

+ 1 - 1
Makefile

@@ -216,7 +216,7 @@ App/%.o: App/%.cpp Decryptor/Decryptor_u.h
 	@$(CXX) $(App_Compile_Flags) -c $< -o $@
 	@echo "CXX  <=  $<"
 
-$(App_Name): App/App.o App/SealingUntrusted.o App/LocalAttestationUntrusted.o App/protobufLAInitiator.o App/protobufSgxTransformsInitiator.o App/protobufSgxTransformsHelper.o App/protobufReadWrite.o App/ProtobufLAMessages.pb.o App/PostLAMessages.pb.o App/Ipc.o App/FileIO.o App/Decryptor_u.o
+$(App_Name): App/App.o App/SealingUntrusted.o App/LocalAttestationUntrusted.o App/protobufLAInitiator.o App/protobufSgxTransformsInitiator.o App/protobufSgxTransformsHelper.o App/PostLAProtobufNativeTransforms.o  App/protobufReadWrite.o App/ProtobufLAMessages.pb.o App/PostLAMessages.pb.o App/Ipc.o App/FileIO.o App/Decryptor_u.o
 	@$(CXX) -Wl,--no-undefined $^ -o $@ $(App_Link_Flags)
 	@echo "LINK =>  $@"