/* * Copyright (C) 2011-2017 Intel Corporation. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * Neither the name of Intel Corporation nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ #include "sgx_trts.h" #include "sgx_utils.h" #include "EnclaveMessageExchange.h" #include "sgx_eid.h" #include "error_codes.h" #include "sgx_ecp_types.h" #include "sgx_thread.h" #include #include "dh_session_protocol.h" #include "sgx_dh.h" #include "sgx_tcrypto.h" #include "LocalAttestationCode_t.h" #include "sgx_tseal.h" #ifdef __cplusplus extern "C" { #endif uint32_t verify_peer_enclave_trust(sgx_dh_session_enclave_identity_t* peer_enclave_identity, sgx_measurement_t* measurement); #ifdef __cplusplus } #endif uint32_t create_ecdsa_key_pair(sgx_ec256_public_t* pub_key, sgx_ec256_private_t* priv_key); 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); #define MAX_SESSION_COUNT 16 //number of open sessions // uint32_t g_session_count = 0; ATTESTATION_STATUS generate_session_id(uint32_t *session_id); ATTESTATION_STATUS end_session(); sgx_ec256_private_t signing_priv_key; sgx_ec256_private_t short_term_priv_key; sgx_ec256_public_t short_term_pub_key; // for testing only: to test verification of signature uint32_t one_la_done; sgx_measurement_t verifier_mr_enclave; sgx_measurement_t apache_mr_signer; sgx_ecc_state_handle_t ecc_state; uint32_t session_ids[MAX_SESSION_COUNT]; // Our enclave will not be doing LA with more than 1 decryptor enclave at a time. // We should not need this. //std::mapg_dest_session_info_map; dh_session_t global_session_info; // TODO: May be we need to store all previously assigned session IDs instead of just the counter; to prevent replay attacks - uint32_t global_session_id=0; //Handle the request from Source Enclave for a session ATTESTATION_STATUS session_request(sgx_dh_msg1_t *dh_msg1, uint32_t *session_id ) { // dh_session_t session_info; 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) { return status; } *session_id=1; global_session_info.status = IN_PROGRESS; //Generate Message1 that will be returned to Source Enclave status = sgx_dh_responder_gen_msg1((sgx_dh_msg1_t*)dh_msg1, &sgx_dh_session); if(SGX_SUCCESS != status) { global_session_id--; // SAFE_FREE(g_session_id_tracker[*session_id]); return status; } memcpy(&global_session_info.in_progress.dh_session, &sgx_dh_session, sizeof(sgx_dh_session_t)); //return sgx_seal_data(0, NULL, 0, NULL, 0, NULL); //Store the session information under the correspoding source enlave id key // g_dest_session_info_map.insert(std::pair(src_enclave_id, session_info)); return status; } //Verify Message 2, generate Message3 and exchange Message 3 with Source Enclave ATTESTATION_STATUS exchange_report( sgx_dh_msg2_t *dh_msg2, sgx_dh_msg3_t *dh_msg3, uint32_t* session_id, uint8_t* read) { sgx_key_128bit_t dh_aek; // Session key // dh_session_t session_info; ATTESTATION_STATUS status = SUCCESS; sgx_dh_session_t sgx_dh_session; sgx_dh_session_enclave_identity_t initiator_identity; if(!dh_msg2 || !dh_msg3) { return INVALID_PARAMETER_ERROR; } memset(&dh_aek,0, sizeof(sgx_key_128bit_t)); // Why is there a do-while loop anyway? It seems like there is no successful exit ... // do // { // TODO: Make sure that this works - pointers // session_info = global_session_info; if(global_session_info.status != IN_PROGRESS) { status = INVALID_SESSION; end_session(); } memcpy(&sgx_dh_session, &global_session_info.in_progress.dh_session, sizeof(sgx_dh_session_t)); dh_msg3->msg3_body.additional_prop_length = 0; //Process message 2 from source enclave and obtain message 3 sgx_status_t se_ret = sgx_dh_responder_proc_msg2(dh_msg2, dh_msg3, &sgx_dh_session, &dh_aek, &initiator_identity); if(SGX_SUCCESS != se_ret) { status = se_ret; end_session(); } uint32_t hash_count; // THIS IS WHERE THE DECRYPTOR VERIFIES THE APACHE'S MRSIGNER IS THE PUBLIC KEY GIVEN AFTER THE LOCAL ATTESTATION WITH THE VERIFIER. //Verify source enclave's trust if(one_la_done == 0) { one_la_done = 1; *read=1; uint32_t ret = verify_peer_enclave_trust(&initiator_identity, NULL); if(ret != SUCCESS) { return ret; //INVALID_SESSION; } for(hash_count=0; hash_count<32; hash_count++) verifier_mr_enclave.m[hash_count] = initiator_identity.mr_signer.m[hash_count]; } else { one_la_done=0; return 0xFFFFFFFF; /* uint32_t ret = verify_peer_enclave_trust(&initiator_identity, &apache_mrsigner); if(ret != SUCCESS) { return ret; //INVALID_SESSION; } */ } // TODO: Verify that these changes will be lost on update. //save the session ID, status and initialize the session nonce global_session_info.session_id = *session_id; global_session_info.status = ACTIVE; 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)); //g_session_count++;*/ // }while(0); return status; } ATTESTATION_STATUS encrypt(__attribute__((unused)) uint8_t *plaintext, __attribute__((unused)) size_t plaintext_length, __attribute__((unused)) uint8_t* payload_tag, __attribute__((unused)) uint8_t* ciphertext, __attribute__((unused)) uint32_t* active_counter) { // return 0; sgx_status_t status; if(plaintext == NULL) { return INVALID_PARAMETER_ERROR; } //Check if the nonce for the session has not exceeded 2^32-2 if so end session and start a new session if(global_session_info.active.counter == ((uint32_t) - 2)) { return 0xFF; // TODO: DO something here. // close_session(src_enclave_id); // create_session(src_enclave_id, session_info); } //return plaintext_length ; uint32_t count; uint8_t* temp_plaintext = (uint8_t*) malloc(plaintext_length); for(count=0; countmessage_aes_gcm_data.reserved,&global_session_info.active.counter,sizeof(global_session_info.active.counter)); // *active_counter=global_session_info.active.counter; memcpy(temp_req_message->message_aes_gcm_data.reserved,&global_session_info.active.counter,sizeof(global_session_info.active.counter)); //Set the session ID of the message to the current session id // req_message->session_id = global_session_info.session_id; uint32_t temp_plaintext_length = plaintext_length; uint8_t* shared_key = (uint8_t*)malloc(16); // 128 bit aes key for(count=0;count<16;count++) *(shared_key+count)=global_session_info.active.AEK[count]; // return 0; //Prepare the request message with the encrypted payload status = sgx_rijndael128GCM_encrypt((sgx_key_128bit_t*)shared_key, temp_plaintext, temp_plaintext_length, reinterpret_cast(&(temp_req_message->message_aes_gcm_data.payload)), reinterpret_cast(&(temp_req_message->message_aes_gcm_data.reserved)), 0xc, NULL, 0, &(temp_req_message->message_aes_gcm_data.payload_tag)); for(count=0;count<48;count++) *(ciphertext+count) = temp_req_message->message_aes_gcm_data.payload[count]; // tag length is 16 as per sgx_tseal.h for(count=0;count<16;count++) *(payload_tag+count) = temp_req_message->message_aes_gcm_data.payload_tag[count]; // TODO: Should this depend on whether the call has been successful or not? //Update the value of the session nonce in the source enclave // global_session_info.active.counter +=1; // TODO: Activate this again. free(shared_key); free(temp_plaintext); free(temp_req_message); return status; //return global_session_info.active.counter-1; } /* uint32_t decrypt(__attribute__((unused)) uint8_t* ciphertext, __attribute__((unused)) size_t ciphertext_length, __attribute__((unused)) uint8_t* payload_tag, __attribute__((unused)) uint8_t* plaintext, __attribute__((unused)) size_t plaintext_length) { uint32_t count; sgx_status_t status; uint8_t* shared_key = (uint8_t*)malloc(16); // 128 bit aes key secure_message_t* temp_req_message = (secure_message_t*)malloc(sizeof(secure_message_t)+ plaintext_length); // WTF is this even - what happens to padding? memset(temp_req_message,0,sizeof(secure_message_t)+ plaintext_length); for(count=0;count<16;count++) *(shared_key+count)=global_session_info.active.AEK[count]; uint8_t* temp_ciphertext = (uint8_t*) malloc(ciphertext_length); for(count=0; countmessage_aes_gcm_data.payload, resp_message->message_aes_gcm_data.payload_size, pl$reinterpret_cast(&(resp_message->message_aes_gcm_data.reserved)), sizeof(resp_message->message_aes_gcm_data.reserved), NULL, 0, &resp_message$ status = sgx_rijndael128GCM_decrypt((sgx_key_128bit_t*) shared_key, temp_ciphertext, ciphertext_length, temp_plaintext, iv, 0xc, NULL, 0, &(temp_req_message->message_aes_gcm_data.payload_tag)); for(count=0;count<16; count++) { // if(temp_req_message->message_aes_gcm_data.payload_tag[count] != *(payload_tag+count)) // return 0x2; *(payload_tag+count)=temp_req_message->message_aes_gcm_data.payload_tag[count]; } // for(count=0; countgx[counter]; for(counter=SGX_ECP256_KEY_SIZE;counter<2*SGX_ECP256_KEY_SIZE; counter++) *(private_public_key_string+counter)=pub_key->gy[counter-SGX_ECP256_KEY_SIZE]; } if(signing_priv_key != NULL) // private key to serialize { for(counter=2*SGX_ECP256_KEY_SIZE;counter<3*SGX_ECP256_KEY_SIZE; counter++) *(private_public_key_string+counter)=signing_priv_key->r[counter - 2*SGX_ECP256_KEY_SIZE]; } /* if(pub_key != NULL) // public key to serialize { for(counter=SGX_ECP256_KEY_SIZE;counter<2*SGX_ECP256_KEY_SIZE; counter++) *(private_public_key_string+counter)=pub_key->gx[counter-SGX_ECP256_KEY_SIZE]; for(counter=2*SGX_ECP256_KEY_SIZE;counter<3*SGX_ECP256_KEY_SIZE; counter++) *(private_public_key_string+counter)=pub_key->gy[counter-2*SGX_ECP256_KEY_SIZE]; }*/ } } // todo: set to private void deserialize_string_to_key_pair(uint8_t* private_public_key_string, sgx_ec256_public_t* pub_key, sgx_ec256_private_t* signing_priv_key) { if(private_public_key_string != NULL) // nowhere to deserialize from { uint32_t counter; if(signing_priv_key != NULL) { for(counter=2*SGX_ECP256_KEY_SIZE;counter<3*SGX_ECP256_KEY_SIZE; counter++) signing_priv_key->r[counter-2*SGX_ECP256_KEY_SIZE]=*(private_public_key_string+counter); } if(pub_key != NULL) { for(counter=0;countergx[counter]=*(private_public_key_string+counter); for(counter=SGX_ECP256_KEY_SIZE;counter<2*SGX_ECP256_KEY_SIZE; counter++) pub_key->gy[counter-SGX_ECP256_KEY_SIZE]=*(private_public_key_string+counter); } } } 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) { uint32_t ret_status; sgx_ec256_private_t private_key; uint32_t counter; ret_status=create_ecdsa_key_pair(pub_key, &private_key); if(ret_status!=SGX_SUCCESS) return ret_status; for(counter=0;counterm[counter]; // retrieve long-term private key from global variable - apparently, need to create a local copy or it crashes sgx_ec256_private_t long_term_priv_key; for(counter=0; countergx[counter] = short_term_pub_key.gx[counter]; for(counter=0;counterx[counter] = local_signature.x[counter]; generated_signature->y[counter] = local_signature.y[counter]; } } if(ret_status != SGX_SUCCESS || ret_status2 != SGX_SUCCESS) return 0xFFFFFFFF; return 0; } uint32_t calculate_sealed_data_size( uint32_t input_size) { // *op_size=sgx_calc_sealed_data_size(0, input_size); return sgx_calc_sealed_data_size(0, input_size); } // TODO: Fix this. //Respond to the request from the Source Enclave to close the session ATTESTATION_STATUS end_session(/**/) { return SUCCESS; } /* // Session_id is set to the first index of the pointer array that is non-null.(Not sure how it is ensured that all of them point to NULL at the start) // Why can't it just keep a counter that is incremented? What are the values of g_session_id_tracker array? //Returns a new sessionID for the source destination session ATTESTATION_STATUS generate_session_id(uint32_t *session_id) { ATTESTATION_STATUS status = SUCCESS; if(!session_id) { return INVALID_PARAMETER_ERROR; } //if the session structure is untintialized, set that as the next session ID for (int i = 0; i < MAX_SESSION_COUNT; i++) { if (g_session_id_tracker[i] == NULL) { *session_id = i; return status; } } status = NO_AVAILABLE_SESSION_ERROR; return status; */ // *session_id=++global_session_id; //} uint32_t decrypt(uint8_t* ip_ciphertext, uint32_t ciphertext_len, uint8_t* ip_tag, uint8_t* op_plaintext) { uint32_t return_status2; uint32_t count; // unsigned char key[16]; uint32_t count; unsigned char key[16]; // copying key to within the enclave as apparently it can't operate on it // TODO: Check if it works now. for(count=0;count<16;count++) key[count]=global_session_info.active.AEK[count]; // copying ciphertext to within the enclave (otherwise it crashes with NOT enclave signal) uint8_t* ciphertext = (uint8_t*) malloc(ciphertext_len); for(count=0;count