Browse Source

Got signature to be verified in encrypt_mitigator_header_value - had to change LA function signature to send public key from untrusted app

dettanym 5 years ago
parent
commit
3e7ec25c87
5 changed files with 44 additions and 128 deletions
  1. 2 113
      App/App.cpp
  2. 3 2
      App/systemLA.cpp
  3. 37 11
      Decryptor/Decryptor.cpp
  4. 1 1
      Decryptor/Decryptor.edl
  5. 1 1
      Include/systemLA.h

+ 2 - 113
App/App.cpp

@@ -28,22 +28,6 @@
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.challa
  *
  */
-/*
-#define SGX_DH_MAC_SIZE 16
-#define SGX_TARGET_INFO_RESERVED1_BYTES 4
-#define SGX_TARGET_INFO_RESERVED2_BYTES 456
-#define SGX_ECP256_KEY_SIZE 32
-#define SGX_CPUSVN_SIZE 16
-#define SGX_REPORT_DATA_SIZE 64
-#define SGX_MAC_SIZE 16 // Message Authentication Code - 16 bytes 
-#define SGX_KEYID_SIZE 32
-#define SGX_HASH_SIZE 32 // SHA256 
-
-#define SGX_REPORT_BODY_RESERVED1 28
-#define SGX_REPORT_BODY_RESERVED2 32
-#define SGX_REPORT_BODY_RESERVED3 96
-#define SGX_REPORT_BODY_RESERVED4 60
-*/
 // App.cpp : Defines the entry point for the console application.
 #include <stdio.h>
 #include <map>
@@ -62,15 +46,7 @@
 #include <fcntl.h>
 #include <sys/types.h>
 #include <sys/stat.h>
-/*
-// For google proto buffers
-#include "dhmsgs.pb.h"
-#include <inttypes.h>
-#include <google/protobuf/io/coded_stream.h>
-#include <google/protobuf/io/zero_copy_stream_impl.h>
-#include "SgxProtobufLATransforms_initiator.h"
-using namespace google::protobuf::io;
-*/
+
 #include "systemLA.h"
 
 //#define UNUSED(val) (void)(val)
@@ -254,76 +230,12 @@ int main(__attribute__((unused)) int argc, __attribute__((unused)) char* argv[])
     close(sealed_signing_key_fd);
     
     // LA with the verifier 
-    ret_status=local_attestation_initiator(3825, e2_enclave_id);
+    ret_status=local_attestation_initiator(3825, e2_enclave_id, &pub_key);
     if(ret_status!=0)
     {
         printf("local attestation - with the verifier - did not successfully return: %x\n", ret_status); fflush(stdout);     sgx_destroy_enclave(e2_enclave_id);
         return 0xFFFFFFFF;
     }
-/*	uint8_t ciphertext[32]; uint8_t plaintext[32]; 
-Decryptor_test_encrypt_decrypt(e2_enclave_id, &ret_status, ciphertext, plaintext); 
-if(ret_status!=0)
-	{
-		printf("encryption/decryption failed\n"); fflush(stdout); sgx_destroy_enclave(e2_enclave_id); 
-		return 0xFFFFFFFF;
-	}
-else {
-	printf("received ciphertext: \n"); fflush(stdout); 
-	uint32_t count;
-	for(count=0;count<32;count++) 
-		printf("0x%x ",ciphertext[count]);
-	printf("\n"); fflush(stdout); 
-        printf("received plaintext: \n"); fflush(stdout); 
-        for(count=0;count<32;count++) 
-                printf("0x%x ",plaintext[count]);
-        printf("\n"); fflush(stdout); 
-}
-*/
-/*
-    uint32_t plaintext_length=32;
-    uint8_t plaintext[32]={0x42}; uint8_t ciphertext[48]={0}; uint8_t payload_tag[16]={0}; uint32_t active_counter;
-    uint32_t count;
-    //Allocate memory for the AES-GCM request message
-    secure_message_t* req_message = (secure_message_t*)malloc(sizeof(secure_message_t)+ plaintext_length); // WTF is this even - what happens to padding? 
-    memset(req_message,0,sizeof(secure_message_t)+ plaintext_length);
-    req_message->message_aes_gcm_data.payload_size = plaintext_length;
-    
-    Decryptor_encrypt(e2_enclave_id, &ret_status, &plaintext[0], plaintext_length, payload_tag, &ciphertext[0], &active_counter); 
-    if(ret_status!=0)
-    {
-	printf("error in encryption: 0x%x\n", ret_status); fflush(stdout); sgx_destroy_enclave(e2_enclave_id); 
-	return 0xFFFFFFFF;
-    }
-    printf("Successful encryption\n"); fflush(stdout); 
-    printf("Plaintext: \n"); fflush(stdout); 
-    for(count=0;count<32; count++)
-        printf("0x%02x ", plaintext[count]); 
-    printf("\n");fflush(stdout); 
-
-	printf("Counter: 0x%x\n", ret_status);
-	printf("Payload Tag: \n"); 
-    for(count=0;count<16; count++)
-        printf("0x%02x ", payload_tag[count]); 
-    printf("\n");fflush(stdout); 
-
-
-    for(count=0;count<48; count++)
- 	printf("0x%02x ", ciphertext[count]); 
-    printf("\n");fflush(stdout); 
-	uint8_t expected_plaintext[32]; 
-    Decryptor_decrypt(e2_enclave_id, &ret_status, ciphertext, 48, payload_tag, expected_plaintext, plaintext_length); 
-	if(ret_status == 0)
-	{
-    printf("Successful decryption\n"); fflush(stdout); 
-    for(count=0;count<32; count++)
-        printf("0x%02x ", expected_plaintext[count]); 
-    printf("\n");fflush(stdout); 
-	printf("Expected payload :\n");
-    for(count=0;count<16;count++)
-	printf("0x%02x ", payload_tag[count]);
-    printf("\n"); fflush(stdout); 
-	}
-	printf("Decryption return code:0x%x\n", ret_status);*/
 /*
     // LA with the apache - currently set to return failure - should change it to success when the code to send the mrsigner from the verifier to the decryptor is added- TODO: <--- that 
     ret_status=local_attestation_initiator(3826, e2_enclave_id); // TODO: Change port or sth
@@ -333,29 +245,6 @@ else {
 return 0xFFFFFFFF;
 
     }*/
- /*
-	sgx_ec256_public_t short_term_pub_key;
-	sgx_ec256_signature_t generated_signature;
- Decryptor_create_and_sign_client_side_pub_key(e2_enclave_id, &ret_status,&short_term_pub_key, &generated_signature);
-if(ret_status != SGX_SUCCESS)
-{
-	printf("Could not generate or sign another keypair for client-side, error:%x.\n", ret_status); fflush(stdout);
-	return 0xFFFFFFFF;
-}
-printf("Generated signature and pub key.\n");fflush(stdout);
-for(counter=0; counter<SGX_ECP256_KEY_SIZE; counter++)
-                        printf("0x%x ", short_term_pub_key.gx[counter]); printf("\n"); fflush(stdout);
-                for(counter=0;counter<SGX_NISTP_ECP256_KEY_SIZE ; counter++)
-                {
-                        printf("0x%x", generated_signature.x[counter]);
-                        printf("0x%x", generated_signature.y[counter]);
-                }
-printf("\n"); fflush(stdout);
-
-*/
-
-
-
     // TODO: Continue with other msgs - send sign(enc | verifier mr_enclave)
     sgx_destroy_enclave(e2_enclave_id);
 

+ 3 - 2
App/systemLA.cpp

@@ -13,6 +13,7 @@
 using namespace google::protobuf::io;
 #include "protobufLAInitiator.h"
 #include "../Decryptor/Decryptor_u.h"
+#include "sgx_tcrypto.h"
 // TODO: Make these private functions
 int read_protobuf_msg_from_fd(int accept_fd, google::protobuf::MessageLite& message)
 {
@@ -98,7 +99,7 @@ int set_up_socket(int port,   sockaddr_in* address)
   return server_fd;
 }
 
-int local_attestation_initiator(int port, uint32_t own_enclave_id)
+int local_attestation_initiator(int port, uint32_t own_enclave_id, sgx_ec256_public_t* pub_key)
 {
   // declare msg1, msg2, msg3 protobuf objects
   protobuf_sgx_dh_msg1_t protobuf_msg1;
@@ -185,7 +186,7 @@ Decryptor_decrypt_verifiers_message_set_apache_mrsigner(own_enclave_id, &sgx_ret
 	
 //uint32_t create_and_encrypt_mitigator_header_value(uint8_t* plaintext_sign_data_and_sign, uint8_t* encrypted_sign_data_and_sign, uint8_t* tag);
 
-	Decryptor_create_and_encrypt_mitigator_header_value(own_enclave_id, &sgx_ret, plaintext_sign_data_and_sign, encrypted_sign_data_and_sign_and_tag , encrypted_sign_data_and_sign_and_tag+160);
+	Decryptor_create_and_encrypt_mitigator_header_value(own_enclave_id, &sgx_ret, plaintext_sign_data_and_sign, encrypted_sign_data_and_sign_and_tag , encrypted_sign_data_and_sign_and_tag+160, pub_key);
 	if(sgx_ret!=0)
 	{
 		printf("Error in generating encrypted mitigator header:0x%x\n", sgx_ret); fflush(stdout); return 0xf3;

+ 37 - 11
Decryptor/Decryptor.cpp

@@ -49,6 +49,7 @@ uint32_t create_ec_key_pair(sgx_ec256_public_t* pub_key, sgx_ec256_private_t* pr
 void serialize_key_pair_to_string( sgx_ec256_public_t* pub_key, sgx_ec256_private_t* signing_priv_key, uint8_t* private_public_key_string);
 void deserialize_string_to_key_pair(uint8_t* private_public_key_string, sgx_ec256_public_t* pub_key, sgx_ec256_private_t* priv_key); 
 uint32_t create_mitigator_header_value(uint8_t* signature_data, uint8_t* signature); 
+uint32_t verify_mitigator_header_value(uint8_t* signature_data, uint8_t* signature, sgx_ec256_public_t* pub_key);
 
 uint32_t calculate_sealed_data_size( uint32_t input_size) ; 
 uint32_t create_and_seal_ecdsa_signing_key_pair(__attribute__((unused))   sgx_ec256_public_t* pub_key, __attribute__((unused))  uint32_t* sealed_data_length,  __attribute__((unused)) uint8_t* sealed_data); 
@@ -56,7 +57,7 @@ uint32_t unseal_and_restore_sealed_signing_key_pair(__attribute__((unused)) sgx_
 
 uint32_t decrypt_verifiers_message_set_apache_mrsigner(uint8_t* ciphertext, uint8_t* tag);
 //uint32_t create_and_encrypt_mitigator_header_value(uint8_t* signature_data, uint8_t* signature, uint8_t* tag);
-uint32_t create_and_encrypt_mitigator_header_value(uint8_t* plaintext_sign_data_and_sign, uint8_t* encrypted_sign_data_and_sign, uint8_t* tag);
+uint32_t create_and_encrypt_mitigator_header_value(uint8_t* plaintext_sign_data_and_sign, uint8_t* encrypted_sign_data_and_sign, uint8_t* tag, sgx_ec256_public_t* pub_key);
 
 
 uint32_t one_la_done=0; 
@@ -107,7 +108,7 @@ return 	decrypt(ciphertext, 32, tag, (uint8_t*) &(apache_mr_signer.m));
 }
 
 // signature_data - 96 bytes, encrypted_signature assumed to be at least 64 bytes, tag - at least 16 bytes
-uint32_t create_and_encrypt_mitigator_header_value(uint8_t* plaintext_sign_data_and_sign, uint8_t* encrypted_sign_data_and_sign, uint8_t* tag)
+uint32_t create_and_encrypt_mitigator_header_value(uint8_t* plaintext_sign_data_and_sign, uint8_t* encrypted_sign_data_and_sign, uint8_t* tag, sgx_ec256_public_t* pub_key)
 {
 	uint32_t count;
 	uint8_t sign_data_and_sign[160]; 
@@ -117,10 +118,13 @@ uint32_t create_and_encrypt_mitigator_header_value(uint8_t* plaintext_sign_data_
 		//      printf("Could not generate or sign another keypair for client-side, error:%x.\n", ret_status); fflush(stdout);
 		return 0xFFFFFFDD;
 	}
-	for(count=0; count<160; count++)
-		*(plaintext_sign_data_and_sign+count)=sign_data_and_sign[count]; 
+	ret_status=verify_mitigator_header_value(sign_data_and_sign, sign_data_and_sign+96, pub_key); 
+	if(ret_status !=0)
+		return ret_status; 
+//	for(count=0; count<160; count++)
+//		*(plaintext_sign_data_and_sign+count)=sign_data_and_sign[count]; 
 
-	ret_status = encrypt_internal(sign_data_and_sign, 160, tag, encrypted_sign_data_and_sign); 
+//	ret_status = encrypt_internal(sign_data_and_sign, 160, tag, encrypted_sign_data_and_sign); 
 	return ret_status; 
 }
 
@@ -310,7 +314,7 @@ uint32_t create_mitigator_header_value(__attribute__((unused)) uint8_t* signatur
 	ret_status2 = sgx_ecc256_close_context(ecc_handle); 
 //	free(public_key_string); 
 	if(ret_status == SGX_SUCCESS) 
-	{
+	{	// this only works for Little-endian architectures - need to do byte-wise swapping of the bytes obtained on RHS
 		uint8_t *current_sig_byte = (uint8_t*)(&(local_signature.x)); 
 	        uint32_t ecdsa_sig_count; 
         	for(ecdsa_sig_count=0;ecdsa_sig_count<32;ecdsa_sig_count++)
@@ -324,13 +328,35 @@ uint32_t create_mitigator_header_value(__attribute__((unused)) uint8_t* signatur
 	return 0; 
 }
 
-uint32_t verify_mitigator_header_value(uint8_t* signature_data, uint8_t* signature)
+uint32_t verify_mitigator_header_value(uint8_t* signature_data, uint8_t* signature, sgx_ec256_public_t* pub_key)
 {
+	sgx_ec256_public_t local_pub_key;	uint32_t counter;  uint32_t ret_status; uint32_t ret_status2; 
+	for(counter=0;counter<SGX_ECP256_KEY_SIZE;counter++)
+	{
+		local_pub_key.gx[counter] = pub_key->gx[counter]; 
+		local_pub_key.gy[counter] = pub_key->gy[counter]; 
+	}
+	sgx_ec256_signature_t local_signature; sgx_ecc_state_handle_t ecc_handle;
+	uint8_t *current_sig_byte = (uint8_t*)(&(local_signature.x));
+	uint32_t ecdsa_sig_count; uint8_t verification_result;
+        for(ecdsa_sig_count=0;ecdsa_sig_count<32;ecdsa_sig_count++)
+        	*(current_sig_byte+ecdsa_sig_count)=signature[ecdsa_sig_count];
+	current_sig_byte = (uint8_t*)(&(local_signature.y));
+	for(ecdsa_sig_count=0;ecdsa_sig_count<32;ecdsa_sig_count++)
+		*(current_sig_byte+ecdsa_sig_count)=signature[ecdsa_sig_count+32];
 
-
-	return 0;
-
-
+	//// opening context for signature
+        ret_status = sgx_ecc256_open_context(&ecc_handle);
+        if(ret_status != SGX_SUCCESS)
+                return ret_status;
+
+        ret_status = sgx_ecdsa_verify(signature_data,3*SGX_ECP256_KEY_SIZE, &local_pub_key, &local_signature, &verification_result, ecc_handle);
+        ret_status2 = sgx_ecc256_close_context(ecc_handle);
+        if(ret_status != SGX_SUCCESS || ret_status2 != SGX_SUCCESS)
+                return 0xFFFFFFFF;
+	if(verification_result != SGX_EC_VALID)
+		return 0xee; 
+        return 0;
 }
 
 uint32_t derive_shared_secret_for_client()

+ 1 - 1
Decryptor/Decryptor.edl

@@ -45,7 +45,7 @@ enclave {
         public uint32_t calculate_sealed_data_size(uint32_t input_size); 
 public uint32_t decrypt_verifiers_message_set_apache_mrsigner([in, size=32] uint8_t* ciphertext, [in, size=16] uint8_t* tag);
 //public uint32_t create_and_encrypt_mitigator_header_value([out, size=96] uint8_t* signature_data, [out, size=64] uint8_t* plaintext_signature, [out, size=64] uint8_t* encrypted_signature, [out, size=16] uint8_t* tag);
-public uint32_t create_and_encrypt_mitigator_header_value([out, size=160] uint8_t* plaintext_sign_data_and_sign, [out, size=160] uint8_t* encrypted_sign_data_and_sign, [out, size=16] uint8_t* tag);
+public uint32_t create_and_encrypt_mitigator_header_value([out, size=160] uint8_t* plaintext_sign_data_and_sign, [out, size=160] uint8_t* encrypted_sign_data_and_sign, [out, size=16] uint8_t* tag, [in] sgx_ec256_public_t* pub_key);
 
 
 

+ 1 - 1
Include/systemLA.h

@@ -1 +1 @@
-int local_attestation_initiator(int port, uint32_t own_enclave_id);
+int local_attestation_initiator(int port, uint32_t own_enclave_id, sgx_ec256_public_t* pub_key);