Browse Source

Partial refactor - LA class

dettanym 4 years ago
parent
commit
6c78f03dbf

+ 97 - 0
App/LAResponder/LA.cpp

@@ -0,0 +1,97 @@
+//
+// Created by miti on 2020-01-01.
+//
+#include <stdio.h>
+#include "../../Decryptor/Decryptor_u.h"
+#include "sgx_eid.h"
+#include "sgx_urts.h"
+#define __STDC_FORMAT_MACROS
+#include <inttypes.h>
+#include "ProtobufLAMessages.pb.h"
+#include "Transforms.h"
+
+int LA::generate_protobuf_dh_msg1(uint32_t own_enclave_id, protobuf_sgx_dh_msg1_t& protobuf_msg1)
+{
+    sgx_dh_msg1_t dh_msg1;            //Diffie-Hellman Message 1
+    memset(&dh_msg1, 0, sizeof(sgx_dh_msg1_t));
+    uint32_t ret_status;
+    Decryptor_session_request_wrapper(own_enclave_id, &ret_status, &dh_msg1);
+    if(ret_status != SGX_SUCCESS)
+        return 0xffffffff;
+
+    Transforms::encode_msg1_to_protobuf(protobuf_msg1, &dh_msg1);
+    printf("Generated the following Msg1: ------------- \n"); fflush(stdout);
+    Transforms::print_initialized_msg1(protobuf_msg1, &dh_msg1);
+    return 0;
+}
+
+int LA::process_protobuf_dh_msg2_generate_protobuf_dh_msg3(uint32_t own_enclave_id, protobuf_sgx_dh_msg2_t& protobuf_msg2, protobuf_sgx_dh_msg3_t& protobuf_msg3, uint32_t* session_id)
+{
+    uint32_t ret_status;
+    sgx_dh_msg2_t dh_msg2;            //Diffie-Hellman Message 2
+    sgx_dh_msg3_t dh_msg3;            //Diffie-Hellman Message 3
+    sgx_key_128bit_t dh_aek;        // Session Key
+    memset(&dh_aek,0, sizeof(sgx_key_128bit_t));
+    memset(&dh_msg2, 0, sizeof(sgx_dh_msg2_t));
+    memset(&dh_msg3, 0, sizeof(sgx_dh_msg3_t));
+
+    if(decode_msg2_from_protobuf(protobuf_msg2, &dh_msg2)!=0)
+        return -1;
+
+    // process msg2 and generate msg3
+    Decryptor_exchange_report_wrapper(own_enclave_id, &ret_status, &dh_msg2, &dh_msg3);
+    if(ret_status!=SGX_SUCCESS)
+        return 0x35;
+
+    // convert msg3 sgx_dh_msg3_t object to a protobuf msg3 object.
+    encode_msg3_to_protobuf(protobuf_msg3, &dh_msg3);
+    return 0;
+}
+
+int LA::conduct_la(uint32_t own_enclave_id, int fd)
+{
+    // declare msg1, msg2, msg3 protobuf objects
+    protobuf_sgx_dh_msg1_t protobuf_msg1;
+    protobuf_sgx_dh_msg2_t protobuf_msg2;
+    protobuf_sgx_dh_msg3_t protobuf_msg3;
+    uint32_t protobuf_sgx_ret;
+
+    setbuf(stdout,NULL);
+
+    protobufReaderWriter.set_fd(fd);
+
+    protobuf_sgx_ret = generate_protobuf_dh_msg1(own_enclave_id, protobuf_msg1);
+    if(protobuf_sgx_ret != 0)
+    {
+        printf("Error in generate_protobuf_dh_msg1: 0x%x", protobuf_sgx_ret); fflush(stdout); return protobuf_sgx_ret;
+    }
+
+    printf("Writing message 1\n"); fflush(stdout);
+    if(protobufReaderWriter.write_msg(protobuf_msg1)!=0)
+        return 0x1;
+
+    printf("Reading message 2\n"); fflush(stdout);
+    if(protobufReaderWriter.read_msg(protobuf_msg2)!=0)
+        return 0x2;
+
+    protobuf_sgx_ret = process_protobuf_dh_msg2_generate_protobuf_dh_msg3(own_enclave_id, protobuf_msg2, protobuf_msg3);
+    if(protobuf_sgx_ret != 0)
+    {
+        printf("Error in process_protobuf_dh_msg2_generate_protobuf_dh_msg3: 0x%x", protobuf_sgx_ret);
+        fflush(stdout);
+        return protobuf_sgx_ret;
+    }
+
+    printf("Writing message 3\n"); fflush(stdout);
+    if(protobufReaderWriter.write_msg(protobuf_msg3)!=0)
+        return 0x3;
+    return 0;
+
+}
+
+void LA::get_la_symmetric_key(uint8_t* op_key)
+{
+    uint32_t counter;
+    for(counter=0; counter<16; counter++)
+        op_key[counter] = key[counter];
+}

+ 21 - 0
Include/LA.h

@@ -0,0 +1,21 @@
+//
+// Created by miti on 2020-01-01.
+//
+
+#ifndef DECRYPTORAPP_LA_H
+#define DECRYPTORAPP_LA_H
+
+#include "ProtobufMessageRW.h"
+#include "ProtobufLAMessages.pb.h"
+
+class LA {
+    ProtobufMessageRW protobufReaderWriter;
+    uint8_t key[16];
+    uint32_t process_protobuf_dh_msg2_generate_protobuf_dh_msg3(uint32_t own_enclave_id, protobuf_sgx_dh_msg2_t& protobuf_msg2, protobuf_sgx_dh_msg3_t& protobuf_msg3, uint32_t* session_id);
+    uint32_t generate_protobuf_dh_msg1(uint32_t own_enclave_id, protobuf_sgx_dh_msg1_t& protobuf_msg1, uint32_t* session_id);
+public:
+    uint32_t conduct_la(uint32_t own_enclave_id, int fd);
+    void get_la_symmetric_key(uint8_t* key);
+};
+
+#endif //DECRYPTORAPP_LA_H

+ 2 - 2
Include/LocalAttestationTrusted.h

@@ -43,8 +43,8 @@
 class LocalAttestationTrusted {
   static dh_session_t global_session_info;
 public:
-    static uint32_t session_request(sgx_dh_msg1_t *dh_msg1, uint32_t *session_id);
-    static uint32_t exchange_report(sgx_dh_msg2_t *dh_msg2, sgx_dh_msg3_t *dh_msg3, uint32_t* session_id);
+    static uint32_t session_request(sgx_dh_msg1_t *dh_msg1);
+    static uint32_t exchange_report(sgx_dh_msg2_t *dh_msg2, sgx_dh_msg3_t *dh_msg3);
 };
 
 #endif

+ 2 - 2
LocalAttestationCode/LocalAttestationCode.edl

@@ -34,7 +34,7 @@ enclave  {
     include "datatypes.h"
     include "../Include/dh_session_protocol.h"
     trusted{
-        public uint32_t session_request_wrapper([out] sgx_dh_msg1_t *dh_msg1, [out] uint32_t *session_id);
-        public uint32_t exchange_report_wrapper([in] sgx_dh_msg2_t *dh_msg2, [out] sgx_dh_msg3_t *dh_msg3, [in] uint32_t *session_id);
+        public uint32_t session_request_wrapper([out] sgx_dh_msg1_t *dh_msg1);
+        public uint32_t exchange_report_wrapper([in] sgx_dh_msg2_t *dh_msg2, [out] sgx_dh_msg3_t *dh_msg3);
     };
 };

+ 3 - 21
LocalAttestationCode/LocalAttestationTrusted.cpp

@@ -41,16 +41,11 @@
 
 
 //Handle the request from Source Enclave for a session
-uint32_t LocalAttestationTrusted::session_request(sgx_dh_msg1_t *dh_msg1, uint32_t *session_id)
+uint32_t LocalAttestationTrusted::session_request(sgx_dh_msg1_t *dh_msg1)
 {
         sgx_dh_session_t sgx_dh_session;
         sgx_status_t status = SGX_SUCCESS;
 
-        if(!session_id || !dh_msg1)
-        {
-            return INVALID_PARAMETER_ERROR;
-        }
-
         //Intialize the session as a session responder
         status = sgx_dh_init_session(SGX_DH_SESSION_RESPONDER, &sgx_dh_session);
         if(SGX_SUCCESS != status)
@@ -58,8 +53,6 @@ uint32_t LocalAttestationTrusted::session_request(sgx_dh_msg1_t *dh_msg1, uint32
             return status;
         }
 
-        *session_id=1;
-
         global_session_info.status = IN_PROGRESS;
 
         //Generate Message1 that will be returned to Source Enclave
@@ -74,7 +67,7 @@ uint32_t LocalAttestationTrusted::session_request(sgx_dh_msg1_t *dh_msg1, uint32
 
 // TODO: Hope to edit the sgx_dh_responder_proc_msg2 call to return 32 byte key.
 //Verify Message 2, generate Message3 and exchange Message 3 with Source Enclave
-uint32_t LocalAttestationTrusted::exchange_report(sgx_dh_msg2_t *dh_msg2, sgx_dh_msg3_t *dh_msg3, uint32_t* session_id)
+uint32_t LocalAttestationTrusted::exchange_report(sgx_dh_msg2_t *dh_msg2, sgx_dh_msg3_t *dh_msg3)
 {
         sgx_key_128bit_t dh_aek;
         uint32_t status = 0;
@@ -83,11 +76,8 @@ uint32_t LocalAttestationTrusted::exchange_report(sgx_dh_msg2_t *dh_msg2, sgx_dh
         uint32_t verify_return;
         memset(&dh_aek,0, sizeof(sgx_key_128bit_t));
 
-        if(!dh_msg2 || !dh_msg3)
-          return INVALID_PARAMETER_ERROR;
-
         if(global_session_info.status != IN_PROGRESS)
-          return INVALID_SESSION; // end_session(); // TODO: DA FUQ RETURN STH HERE.
+          return INVALID_SESSION;
 
         memcpy(&sgx_dh_session, &global_session_info.in_progress.dh_session, sizeof(sgx_dh_session_t));
 
@@ -103,13 +93,5 @@ uint32_t LocalAttestationTrusted::exchange_report(sgx_dh_msg2_t *dh_msg2, sgx_dh
         if(verify_return != 0)
          return verify_return;
 
-        /*
-        //save the session ID, status and initialize the session nonce
-        global_session_info.session_id = *session_id;
-        global_session_info.status = ACTIVE; // This means that you can't keep calling exchange_report over and over again.
-        global_session_info.active.counter = 0;
-        memcpy(&global_session_info.active.AEK, &dh_aek, sizeof(sgx_key_128bit_t));
-        memset(&dh_aek,0, sizeof(sgx_key_128bit_t));
-        */
         return 0;
     }

+ 4 - 4
LocalAttestationCode/LocalAttestationTrustedWrapper.cpp

@@ -1,11 +1,11 @@
 #include "LocalAttestationCode_t.h"
 #include "LocalAttestationTrusted.h"
-uint32_t session_request_wrapper(sgx_dh_msg1_t *dh_msg1, uint32_t *session_id)
+uint32_t session_request_wrapper(sgx_dh_msg1_t *dh_msg)
 {
-  return LocalAttestationTrusted::session_request(dh_msg1, session_id);
+  return LocalAttestationTrusted::session_request(dh_msg1);
 }
 
-uint32_t exchange_report_wrapper(sgx_dh_msg2_t *dh_msg2, sgx_dh_msg3_t *dh_msg3, uint32_t *session_id)
+uint32_t exchange_report_wrapper(sgx_dh_msg2_t *dh_msg2, sgx_dh_msg3_t *dh_msg3)
 {
-  return LocalAttestationTrusted::exchange_report(dh_msg2, dh_msg3, session_id);
+  return LocalAttestationTrusted::exchange_report(dh_msg2, dh_msg3);
 }